def create_invoice_obj(invoice_dic, io): invoice = Invoice() invoice.DocNumber = invoice_dic['invoicenumber'] invoice.DueDate = invoice_dic['duedate'] customer = io.get_customer_by_id(invoice_dic['customerrefno']) invoice.CustomerRef = customer.to_ref() # Lines lines = invoice_dic['lines'] line_item_codes = get_lines_item_codes(lines) codes = io.get_items_from_list(line_item_codes) for line in lines: line_obj = create_line_obj(line, codes) invoice.Line.append(line_obj) io.save_invoice(invoice)
def create_invoice_obj(invoice_dic, io): taxcode = io.get_tax_codes()[0] invoice = Invoice() invoice.DocNumber = invoice_dic['invoicenumber'] invoice.DueDate = invoice_dic['duedate'] customer = io.get_customer_by_id(invoice_dic['customerrefno']) invoice.CustomerRef = customer.to_ref() # Lines lines = invoice_dic['lines'] line_item_codes = get_lines_item_codes(lines) codes = io.get_items_from_list(line_item_codes) for line in lines: line_obj = create_line_obj(line, codes) line_obj.SalesItemLineDetail.TaxCodeRef = taxcode.to_ref() invoice.Line.append(line_obj) io.save_invoice(invoice) return invoice
def save_invoice(order): customer = get_customer(order.student) invoice = Invoice.filter(max_results=1, DocNumber=order.number, qb=client) if invoice: invoice = invoice[0] else: invoice = Invoice() invoice.DocNumber = order.number invoice.CustomerRef = customer.to_ref() invoice.DueDate = order.date_paid # Term does not have a to_ref method, go figure, so we manually generate this value invoice.SalesTermRef = {"value": get_default_terms().Id} orderlineitem_set = list(order.orderlineitem_set.all()) for i, l in enumerate(invoice.Line): try: l.Amount = orderlineitem_set[i].total_charge l.Description = orderlineitem_set[i].product.name item = get_item(orderlineitem_set[i].product) l.SalesItemLineDetail.ItemRef = item.to_ref() l.SalesItemLineDetail.ServiceDate = order.date_paid l.SalesItemLineDetail.Qty = orderlineitem_set[i].qty l.SalesItemLineDetail.UnitPrice = orderlineitem_set[i].charge l.save(qb=client) except IndexError: # the order has been updated to remove a line item l.delete(qb=client) if len(orderlineitem_set) > len( invoice.Line ): # the order has been updated or created to add line items for l in orderlineitem_set[len(invoice.Line):]: line = SalesItemLine() line.Amount = l.total_charge line.Description = l.product.name line.SalesItemLineDetail = SalesItemLineDetail() item = get_item(l.product) line.SalesItemLineDetail.ItemRef = item.to_ref() line.SalesItemLineDetail.ServiceDate = order.date_paid line.SalesItemLineDetail.Qty = l.qty line.SalesItemLineDetail.UnitPrice = l.charge invoice.Line.append(line) invoice.save(qb=client) return invoice
def create_invoice_obj_for_post(validated_data, user): io = InvoiceOperations(user_id=user.id) invoice = Invoice() invoice.DocNumber = validated_data.get("invoice_id") invoice.DueDate = validated_data.get("due_date") customer = io.get_customer_by_id(validated_data.get('customer_ref_number')) invoice.CustomerRef = customer.to_ref() lines = validated_data.get("line_items") item_codes = list() for line in lines: item_codes.append(line['item_code']) codes = io.get_items_from_list(item_codes) print("codes", codes) if len(codes) != len(list(set(item_codes))): raise InvalidItemRef("Invalid item ref code") for line in lines: line_obj = create_line_obj_for_post(line, codes) invoice.Line.append(line_obj) io.save_invoice(invoice) return invoice
def qbo_create_invoice(so, customer_id): client = create_qbc() customer_ref = Customer.get(customer_id, qb=client).to_ref() line_detail = SalesItemLineDetail() line_detail.UnitPrice = 100 # in dollars line_detail.Qty = 1 # quantity can be decimal item_ref = Item.get(35, qb=client).to_ref() line_detail.ItemRef = item_ref line = SalesItemLine() line.Amount = 100 # in dollars line.SalesItemLineDetail = line_detail line.DetailType = "SalesItemLineDetail" invoice = Invoice() invoice.CustomerRef = customer_ref invoice.Line = [line] invoice.save(qb=client)
def send_to_quickbooks(self, request): # Before we ship to quickbooks, let's save the freshsheet used by this Order self.freshsheet = FreshSheet.objects.latest('published_at') self.save() client = get_qb_client() customer = Ref() # customer.value = 1 customer.value = self.created_by.qb_customer_id # customer.name = self.created_by.req_info.business_name customer.type = 'Customer' line_items = [] for item in self.items.all(): item_lookup = Item.where( f"Name = '{item.item.name}{item.unit_quantity}'", qb=client) if item_lookup: product = item_lookup[0] product.UnitPrice = item.unit_cost product.Type = 'NonInventory' product.IncomeAccountRef = Account.where( "Name = 'Sales'", qb=client)[0].to_ref() product.save(qb=client) else: product = Item() product.Name = f"{item.item.name}{item.unit_quantity}" product.UnitPrice = item.unit_cost product.Type = 'NonInventory' product.IncomeAccountRef = Account.where( "Name = 'Sales'", qb=client)[0].to_ref() product.save(qb=client) line_detail = SalesItemLineDetail() line_detail.ItemRef = product.to_ref() line_detail.UnitPrice = item.unit_cost # in dollars line_detail.Qty = item.quantity # quantity can be decimal # Need to change this date to be the DELIVERY DATE of shipment, # not the date on which it was created # Check if it's between Sunday and Tuesday (Yields Tuesday date().isoformat()) # Check if it's between Wednesday and Friday (Yields Friday date().isoformat()) line_detail.ServiceDate = get_next_service_date().isoformat() line = SalesItemLine() line.Id = '1' line.Amount = item.total_cost # in dollars line.Description = f"{item.quantity} {item.item.get_unit_verbose()} of {product.Name} from " \ f"{item.item.farm}." line.SalesItemLineDetail = line_detail line_items.append(line) invoice = Invoice() invoice.CustomerRef = customer invoice.Line = line_items invoice.save(qb=client) # NOTE: If we try to just save the user model, it _could_ overwrite some Quickbooks auth settings. # By getting a fresh model we'll for sure have the latest settings fresh_user_model = User.objects.get(pk=request.user.pk) fresh_user_model.cart = None fresh_user_model.save()