Exemple #1
0
 def show_billings(self):
     customer_id = self.request.matchdict.get('customer_id')
     customer = Customer.load(customer_id)
     self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id)
     return {
         'customer' : customer,
         'billings' : Journal.find_all_by_customer(customer, self.offset),
         'offset' : self.offset
         }
Exemple #2
0
 def show_billing_dialog(self):
     customer_id = self.request.matchdict.get('customer_id')
     journal_id = self.request.matchdict.get('journal_id')
     customer = Customer.load(customer_id)
     self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id)
     journal = Journal.load(journal_id)
     self.forbid_if(not journal or str(journal.customer_id) != str(customer_id))
     return {
         'customer' : customer,
         'journal' : journal
         }
Exemple #3
0
    def _apply_payment(self, customer_id, order_id, pmt_amount=None, pmt_method=None, pmt_note=None):  #pylint: disable-msg=R0913
        """ KB: [2011-03-09]: Check that everything is kosher
        Create a journal entry for the order for the amount and type specified in the UI
        Create a status noting the type and amount of the payment applied.
        """
        customer = Customer.load(customer_id)
        self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id)
        order = customer.get_order(order_id)
        self.forbid_if(not order)
        user = self.request.ctx.user
        current_customer_balance = customer.get_current_balance()
        #prior_payments_applied = order.total_payments_applied()
        prior_total_due = order.total_payments_due()
        balance_amount_to_apply = float(self.request.POST.get('pmt_balance_amount_to_apply', 0.0))
        amt = float(util.nvl(pmt_amount, self.request.POST.get('pmt_amount')))
        method = util.nvl(pmt_method, self.request.POST.get('pmt_method'))
        note = util.nvl(pmt_note, self.request.POST.get('pmt_note'))

        self.forbid_if(round(amt + balance_amount_to_apply, 2) > round(prior_total_due, 2),
                       "amt + balance_amount_to_apply > prior_total_due")
        self.forbid_if(current_customer_balance > 0 and round(balance_amount_to_apply, 2) > round(current_customer_balance, 2),
                       "balance_amount_to_apply > current_customer_balance")

        pmt_type = 'PartialPayment'
        if amt == prior_total_due:
            pmt_type = 'FullPayment'

        Journal.create_new(amt, customer, order, user, pmt_type, method, note)
        status_note = '%s applied: $%s' % (pmt_type, util.money(amt))
        Status.add(customer, order, Status.find_event(self.enterprise_id, order, 'PAYMENT_APPLIED'), status_note)
        self.flash(status_note)
        if balance_amount_to_apply > 0:
            Journal.create_new(balance_amount_to_apply, customer, order, user, 'CreditDecrease')
            status_note = '%s applied: $%s' % ('CreditDecrease', util.money(balance_amount_to_apply))
            Status.add(customer, order, Status.find_event(self.enterprise_id, order, 'PAYMENT_APPLIED'), status_note)
            self.flash(status_note)
        customer.invalidate_caches()
        customer.flush()
        return 'True'
Exemple #4
0
    def return_item(self):
        customer_id = self.request.matchdict.get('customer_id')
        order_id = self.request.matchdict.get('order_id')
        order_item_id = self.request.matchdict.get('order_item_id')
        customer = Customer.load(customer_id)
        self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id)
        order = customer.get_order(order_id)
        self.forbid_if(not order)
        order_item = OrderItem.load(order_item_id)
        self.forbid_if(not order_item or str(order_item.order.order_id) != str(order.order_id))
        user = self.request.ctx.user
        return_type = self.request.POST.get('rt_refund_type')
        quantity_returned = float(self.request.POST.get('quantity_returned'))
        credit_amount = float(self.request.POST.get('credit_amount'))

        jrnl = Journal.create_new(credit_amount, customer, order, user, return_type)
        ret = ProductReturn.create_new(order_item.product, order_item.order, quantity_returned, credit_amount, jrnl, user)
        status_note = "'%s' returned.  $%.2f refunded by %s" % (order_item.product.name, credit_amount, return_type)
        Status.add(customer, order_item, Status.find_event(self.enterprise_id, order_item, 'RETURN'), status_note)

        order_item.quantity -= quantity_returned
        if order_item.quantity == 0:
            order_item.delete_dt = util.today()
        order_item.save()
        if order_item.product.track_inventory:
            InventoryJournal.create_new(order_item.product, 'Return', quantity_returned, order_item, None, None, ret)

        for attr_kid in order_item.children:
            Status.add(customer, attr_kid, Status.find_event(self.enterprise_id, attr_kid, 'RETURN'), status_note)
            attr_kid_prod = attr_kid.product
            if attr_kid_prod.track_inventory:
                InventoryJournal.create_new(attr_kid_prod, 'Return', quantity_returned, attr_kid)

        self.flash(status_note)
        if len(order.active_items) == 0:
            # KB: [2012-09-06]: Deleted the one thing out of this
            # order.  Kill the order
            status_note = 'Only item in order returned. Order cancelled.'
            self._cancel_order_impl(order_id, status_note, False)
            self.flash(status_note)
            ret = HTTPFound('/crm/customer/show_orders/%s' % customer_id)
        else:
            ret = HTTPFound('/crm/customer/edit_order_dialog/%s/%s' % (customer_id, order_id))
        customer.invalidate_caches()
        return ret
Exemple #5
0
 def payment_history(self):
     try:
         journals = Journal.find_all_by_order(self)
         ret = '<table cellpadding="0" cellspacing="10" border="0"><tr><td><u>Date</u></td><td><u>Method</u></td><td><u>Note</u></td><td><u>Amount</u></td></tr>'
         for j in journals:
             ret += """<tr>
                         <td>{dt}</td><td nowrap>{meth}</td><td>{note}</td><td align="right">{amt}</td>
                       </tr>
                    """.format(
                 dt=util.slash_date(j.create_dt), amt=util.money(j.amount), meth=j.method, note=j.note
             )
         ret += '<tr><td colspan="4"><hr></td></tr>'
         ret += (
             '<tr><td><i>Total Payments Applied</i></td><td colspan="2">&nbsp;</td><td align="right">%s</td></tr>'
             % util.money(self.total_payments_applied(), True)
         )
         ret += "</table>"
         return ret
     except Exception as exc:  # pragma: no cover
         log.debug(exc)
Exemple #6
0
    def cancel(self, reason, by_customer=False):
        self.cancel_dt = datetime.datetime.date(datetime.datetime.now())
        for oitem in self.active_items:
            prod = oitem.product
            InventoryJournal.create_new(prod, "Cancelled Order", oitem.quantity, oitem)
            oitem.delete_dt = util.today()
            oitem.save()

        journals = Journal.find_all_by_order(self)
        for j in journals:
            j.delete_dt = util.today()
            j.save()

        msg = "Order Canceled" if not by_customer else "Order Cancelled by Customer"
        Status.add(
            self.customer,
            self,
            Status.find_event(self.customer.campaign.company.enterprise_id, self, "CREATED"),
            "%s : %s" % (msg, reason),
        )
        self.save()
Exemple #7
0
 def apply_payment_dialog(self):
     customer_id = self.request.matchdict.get('customer_id')
     order_id = self.request.matchdict.get('order_id')
     customer = Customer.load(customer_id)
     self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id)
     order = customer.get_order(order_id)
     self.forbid_if(not order)
     total_due = order.total_payments_due()
     pre_order_balance = customer.get_current_balance()
     return {
         'customer' : customer,
         'order' : order,
         'total_price' : order.total_price(),
         'payment_methods' : Journal.get_payment_methods(self.request.ctx.enterprise),
         'total_payments_applied' : order.total_payments_applied(),
         'total_discounts_applied' : order.total_discounts_applied(),
         'total_due' : total_due,
         'pre_order_balance' : pre_order_balance,
         'enterprise' : self.request.ctx.enterprise,
         'total_due_after_balance' : total_due+pre_order_balance if (total_due+pre_order_balance) > 0 else 0
         }
Exemple #8
0
    def create_new(cart, customer, enterprise_id, campaign, user_created, order_note=None, incl_tax=True):
        """ KB: [2010-09-09]: Given a cart full of products, create a new order and return it.
        if a given product is a parent, then create an kid order_item of zero cost and attach it to the parent.
        """
        cord = CustomerOrder()
        cord.creator = user_created
        cord.customer = customer
        cord.campaign = campaign
        cord.shipping_total = util.nvl(cart.shipping_total, 0.0)
        cord.shipping_note = cart.shipping_selection_name
        cord.handling_total = util.nvl(cart.handling_total, 0.0)
        cord.shipping_addr1 = cart.shipping_addr1
        cord.shipping_addr2 = cart.shipping_addr2
        cord.shipping_city = cart.shipping_city
        cord.shipping_state = cart.shipping_state
        cord.shipping_zip = cart.shipping_zip
        cord.shipping_country = cart.shipping_country
        cord.shipping_phone = cart.shipping_phone
        cart.calculate_cart_discount_for_order(cord)
        cord.note = order_note
        cord.save()
        cord.flush()
        for cart_item in cart.items:
            prd = Product.load(cart_item["product"].product_id)
            item = OrderItem()
            item.order = cord
            item.product = prd
            item.creator = user_created
            item.start_dt = cart_item["start_dt"]
            item.note = cart_item["note"]
            item.save()
            item.flush()

            attribute_order_items = []
            for attribute_product_id in cart_item["attributes"].keys():
                attribute_order_item = OrderItem()
                attribute_order_item.parent_id = item.order_item_id
                attribute_order_item.order = cord
                ao_prd = Product.load(attribute_product_id)
                attribute_order_item.product = ao_prd
                attribute_order_item.creator = user_created
                attribute_order_item.unit_cost = ao_prd.unit_cost
                attribute_order_item.unit_discount_price = ao_prd.get_discount_price(campaign)
                attribute_order_item.unit_retail_price = ao_prd.get_retail_price(campaign)
                attribute_order_item.quantity = cart_item["attributes"][attribute_product_id]["quantity"]
                attribute_order_item.save()
                attribute_order_items.append(attribute_order_item)

            # KB: [2013-02-24]: Discount is calculated by using the highest price of the discounts for the product and all of its selected attributes
            discount = max(
                [util.nvl(aois.unit_discount_price) for aois in attribute_order_items]
                + [prd.get_discount_price(campaign)]
            )
            # KB: [2013-02-24]: Retail is calculated by using the highest price of the retail prices for the product and all its selected attributes.
            retail = max(
                [util.nvl(aois.unit_retail_price, 0.0) for aois in attribute_order_items]
                + [cart_item["base_price"] if "base_price" in cart_item else prd.get_retail_price(campaign)]
            )

            item.quantity = float(cart_item["quantity"])
            item.unit_price = discount if discount else retail
            if campaign.tax_rate and incl_tax:
                item.tax = (item.unit_price * item.quantity) * campaign.tax_rate
            item.unit_cost = prd.unit_cost
            item.unit_discount_price = discount if discount else None
            item.unit_retail_price = retail
            item.save()
            if prd.track_inventory:
                InventoryJournal.create_new(prd, "Sale", int(item.quantity), item)
            if item.unit_discount_price is not None:
                discount = DiscountProduct.find_by_product(prd)
                Journal.create_new(
                    (item.unit_retail_price - item.unit_discount_price) * int(item.quantity),
                    customer,
                    cord,
                    None,
                    typ="AutomaticDiscount",
                    attachment=discount,
                )
            Status.add(
                customer,
                item,
                Status.find_event(enterprise_id, item, "CREATED"),
                "Item added to order %s @ $%s" % (prd.name, util.money(item.unit_price)),
            )
            if prd.can_have_children():
                item.flush()  # we need this to get the parent ID.
                children = prd.get_children()
                if children and len(children) > 0:
                    for kid in children:
                        child_item = OrderItem()
                        child_item.order = cord
                        child_item.parent_id = item.order_item_id
                        child_item.product = kid.child
                        child_item.creator = user_created
                        child_item.start_dt = cart_item["start_dt"]
                        child_item.unit_price = 0.0
                        child_item.unit_discount_price = None
                        child_item.unit_retail_price = 0.0
                        child_item.unit_cost = prd.unit_cost
                        child_item.quantity = kid.child_quantity
                        if kid.child.track_inventory:
                            InventoryJournal.create_new(kid.child, "Sale", child_item.quantity, child_item)
        Status.add(customer, cord, Status.find_event(enterprise_id, cord, "CREATED"), "Order created ")
        if cord.discount:
            discount_amount = None
            if cord.discount.percent_off:
                item_price = cord.total_item_price()
                discount_amount = item_price - (item_price * cord.discount.percent_off)
            elif cord.discount.shipping_percent_off:
                # (9.0 / (1.0-0.1)) = 10.00
                discount_amount = cart.shipping_discount_total
            if discount_amount and int(discount_amount) > 0:
                Journal.create_new(
                    discount_amount, customer, cord, None, typ="AutomaticDiscount", attachment=cord.discount
                )
        cord.save()
        cord.flush()
        return cord
Exemple #9
0
 def total_payments_due(self):
     return Journal.total_due(self)
Exemple #10
0
 def total_discounts_applied(self):
     return Journal.total_discounts(self) + Journal.total_automatic_discounts(self)
Exemple #11
0
 def total_payments_applied(self):
     return Journal.total_applied(self)
Exemple #12
0
 def discounts(self):
     return Journal.filter_discounts(self)
Exemple #13
0
 def get_current_balance(self):
     return Journal.total_balance_for_customer(self)