def status_dialog(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) order = order_item = events = redir = None if self.request.GET.get('order_id'): order = CustomerOrder.load(self.request.GET.get('order_id')) self.forbid_if(not order or order.campaign.company.enterprise_id != self.enterprise_id) events = util.select_list(StatusEvent.find_all_applicable(self.enterprise_id, order), 'event_id', 'display_name', True) redir = '/crm/customer/show_orders/%s' % customer_id elif self.request.GET.get('order_item_id'): order_item = OrderItem.load(self.request.GET.get('order_item_id')) self.forbid_if(not order_item or order_item.order.campaign.company.enterprise_id != self.enterprise_id) events = util.select_list(StatusEvent.find_all_applicable(self.enterprise_id, order_item), 'event_id', 'display_name', True) redir = '/crm/customer/edit_order_dialog/%s/%s' % (customer_id, order_item.order_id) else: events = util.select_list(StatusEvent.find_all_applicable(self.enterprise_id, customer), 'event_id', 'display_name', True) redir = '/crm/customer/edit/%s' % customer_id return { 'customer' : customer, 'order' : order, 'order_item' : order_item, 'events' : events, 'redir' : redir }
def return_item_dialog(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)) return { 'customer' : customer, 'order' : order, 'order_item' : order_item }
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
def save_status(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) event = StatusEvent.load(self.request.POST.get('event_id')) self.forbid_if(not event or not self.request.POST.get('event_id') or (not event.is_system and event.enterprise_id is not None and event.enterprise_id != self.enterprise_id)) order = None note = self.request.POST.get('note') if self.request.POST.get('order_id'): order = CustomerOrder.load(self.request.POST.get('order_id')) self.forbid_if(not order or order.campaign.company.enterprise_id != self.enterprise_id) Status.add(customer, order, event, note, self.request.ctx.user) self.flash('Statused Order to %s' % event.display_name) elif self.request.POST.get('order_item_id'): order_item = OrderItem.load(self.request.POST.get('order_item_id')) self.forbid_if(not order_item or order_item.order.campaign.company.enterprise_id != self.enterprise_id) Status.add(customer, order_item, event, note, self.request.ctx.user) self.flash('Statused Item to %s' % event.display_name) else: Status.add(customer, customer, event, note, self.request.ctx.user) self.flash('Statused Customer to %s' % event.display_name) customer.invalidate_caches() return self.find_redirect()
def order_item(self): from pvscore.model.crm.orderitem import OrderItem return OrderItem.load(self.order_item_id)
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
def augment_order(self, customer, product, campaign, user_created, quantity=0, incl_tax=True): enterprise_id = product.company.enterprise_id item = OrderItem() item.order = self item.product = product item.creator = user_created discount = product.get_discount_price(campaign) retail = product.get_price(campaign) item.unit_price = discount if discount else retail item.unit_cost = product.unit_cost item.unit_discount_price = discount if discount else 0.0 item.unit_retail_price = retail item.quantity = quantity if campaign.tax_rate and incl_tax: item.tax = (item.unit_price * item.quantity) * campaign.tax_rate if quantity > 0: if product.track_inventory: InventoryJournal.create_new(product, "Sale", item.quantity, item) item.save() if product.can_have_children(): item.flush() # we need this to get the parent ID. children = product.get_children() if children and len(children) > 0: for kid in children: child_item = OrderItem() child_item.order = self child_item.parent_id = item.order_item_id child_item.product = kid.child child_item.creator = user_created child_item.unit_price = 0.0 child_item.unit_discount_price = 0.0 child_item.unit_cost = product.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, self, Status.find_event(enterprise_id, self, "MODIFIED"), "Order Modified ") self.save() self.flush() return item
def edit_order(self): #pylint: disable-msg=R0915,R0912 customer_id = self.request.matchdict.get('customer_id') order_id = self.request.matchdict.get('order_id') oids_to_delete = self.request.POST.getall('order_items_to_delete[]') 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.shipping_total = self.request.POST.get('shipping_total') if self.request.POST.get('shipping_total') else 0.0 order.create_dt = self.request.POST.get('create_dt') if self.request.POST.get('create_dt') else order.create_dt order.save() total_payments_applied = order.total_payments_applied() for oid in oids_to_delete: oitem = OrderItem.load(oid) Status.add(customer, oitem, Status.find_event(self.enterprise_id, oitem, 'DELETED'), 'OrderItem deleted ') prod = oitem.product if prod.track_inventory: InventoryJournal.create_new(prod, 'Cancelled Item', oitem.quantity, oitem) for attr_kid in oitem.children: Status.add(customer, attr_kid, Status.find_event(self.enterprise_id, attr_kid, 'DELETED'), 'OrderItem deleted ') attr_kid_prod = attr_kid.product if attr_kid_prod.track_inventory: InventoryJournal.create_new(attr_kid_prod, 'Cancelled Item', oitem.quantity, attr_kid) attr_kid.soft_delete() oitem.soft_delete() # extract order_items[27][quantity] to set those properties. order_items = {} for key in self.request.POST.keys(): if key.startswith('order_items'): match = re.search(r'^.*\[(.*)\]\[(.*)\]', key) if match: order_item_id = match.group(1) attr = match.group(2) new_val = float(self.request.POST.get(key)) if attr != 'product_id' else self.request.POST.get(key) # KB: [2011-03-07]: If the ID ends in '_', its not really an ID but a new item. # product_id will only show up as non-null in the hash of a new product if order_item_id[-1] == '_': order_item_product = Product.load(self.request.POST.get('order_items[%s][product_id]' % order_item_id)) if not order_items.has_key(order_item_id): order_items[order_item_id] = order.augment_order(customer, order_item_product, customer.campaign, self.request.ctx.user) oitem = order_items[order_item_id] assert oitem.product is not None if 'quantity' == attr: new_val = float(new_val) if order_item_product.track_inventory: InventoryJournal.create_new(order_item_product, 'Sale', new_val, oitem) setattr(oitem, attr, new_val) oitem.save() else: if not order_items.has_key(order_item_id): order_items[order_item_id] = OrderItem.load(order_item_id) oitem = order_items[order_item_id] order_item_product = oitem.product if util.money(getattr(oitem, attr)) != util.money(new_val): Status.add(customer, oitem, Status.find_event(self.enterprise_id, oitem, 'MODIFIED'), 'Order Item modified: (id=%s). %s : %s -> %s' % (oitem.order_item_id, attr, util.money(getattr(oitem, attr)), util.money(new_val))) if 'quantity' == attr: new_val = float(new_val) if not total_payments_applied: if order_item_product.track_inventory: InventoryJournal.cleanup(oitem, 'Sale') InventoryJournal.create_new(order_item_product, 'Sale', new_val, oitem) setattr(oitem, attr, new_val) oitem.save() Status.add(customer, order, Status.find_event(self.enterprise_id, order, 'MODIFIED'), 'Order modified') customer.invalidate_caches() self.flash("Saved Order") return 'True'