Beispiel #1
0
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)
Beispiel #2
0
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
Beispiel #3
0
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
Beispiel #4
0
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