Esempio n. 1
0
def new_invoice():
    print(session['email'])
    if request.method == 'POST':
        string = {}
        data = request.form
        print(data)
        print()
        for key in data.keys():
            for value in data.getlist(key):
                string[key] = value
        # ---- Invoice Creation ----

        no = string['number']
        party_id = Party.get_by_gstin(string['select_party'])

        date = string['date']
        narration = string['narration']

        invoice_amount = string['total_amount']
        e_way = string['e_way']

        invoice = Invoice(no, party_id._id, invoice_amount, date, narration,
                          e_way)
        invoice.save_to_mongo()
        s = invoice._id
        print('invoice_id', s)
        # --- Find Items
        items = []
        serial_no = {}

        for i in string:
            print(i[:7])
            if i[:12] == 'select_item_':
                items.append([i, string[i][14::]])

            if i[:7] == 'myModal':
                if i[8] != '_':
                    num = int(i[7]) * 10 + int(i[8])
                else:
                    num = int(i[7])
                if str(num) not in serial_no:
                    serial_no[str(num)] = []

                serial_no[str(num)].append(string[i])

        #print(items)
        item_details = []
        for i in range(len(items)):
            item = items[i][0][12::]
            quantity = string['quantity_' + item]
            rate_per = string['rate_per_' + item]
            item_details.append(
                [item, items[i][1], quantity, rate_per, serial_no[item]])

        #print()
        #print(item_details)

        for i in item_details:
            item_id = Item.get_by_name(i[1])
            print(invoice._id, item_id, float(i[3]), int(i[2]))
            invoice_item = Invoice_item(invoice._id, item_id._id, float(i[3]),
                                        int(i[2]))
            invoice_item.save_to_mongo()
            for j in i[4]:
                serial_no = Serial_no(invoice_item._id, j)
                serial_no.save_to_mongo()
        #print()
        #print(serial_no)
        #print(string)

        return redirect(url_for('.print_invoice', invoice_id=s))

    parties = Party.get_parties()
    items = Item.get_items()
    return render_template('invoices/new_invoice.jinja2',
                           new_invoice_page=True,
                           parties=parties,
                           items=items,
                           company=Company.get_company()[0])
Esempio n. 2
0
def edit_invoice(invoice_id):
    parties = Party.get_parties()
    items = Item.get_items()
    invoice_items = Invoice_item.get_by_invoice_id(invoice_id)

    company = Company.get_company()
    invoice = Invoice.get_by_id(invoice_id)
    invoice.date = datetime.datetime.strftime(invoice.date, "%d-%m-%Y")
    invoice_package = {}
    invoice_package['invoice'] = invoice

    party = Party.get_by_id(invoice.party_id)

    invoice_package['party'] = party
    tax_type = party.tax_type()
    tax, igst, round_off, total_amount, hsn_taxable_tot, total_quantity = 0.0, 0.0, 0.0, 0.0, 0.0, 0

    invoice_package['invoice_items'] = []
    sr = 0

    for invoice_item in invoice_items:
        sr += 1
        serial_no = Serial_no.get_by_invoice_item_id(invoice_item._id)
        total_quantity += invoice_item.quantity
        item = Item.get_by_id(invoice_item.item_id)
        amount_item = int(invoice_item.quantity) * float(invoice_item.rate_per)
        if tax_type == "gst":
            tax1 = amount_item * float(item.tax) / 200
            total_amount += (tax1 * 2)
        else:
            tax1 = amount_item * float(item.tax) / 100
            total_amount += tax1
        tax += tax1
        hsn_taxable_tot += amount_item
        invoice_package['invoice_items'].append({
            "invoice_item": invoice_item,
            "item": item,
            "serial_no": serial_no,
            "amount": amount_item,
            "sr_no": sr
        })

    if tax_type == "gst":
        x = tax * 2
    else:
        x = tax
    invoice_package['total_tax_amount'] = x
    total = hsn_taxable_tot + x
    round_off = abs(round(total) - total)
    invoice_package['tax'] = tax
    invoice_package['total_items'] = len(invoice_items)
    invoice_package['round_off'] = round_off
    invoice_package['total'] = total
    invoice_package['tax_type'] = tax_type
    invoice_package['company'] = company[0]

    #    for i in invoice_package:
    #        print(i,invoice_package[i])

    if request.method == 'POST':
        Invoice.get_by_id(invoice_id).delete()
        string = {}
        data = request.form
        print(data)
        print()
        for key in data.keys():
            for value in data.getlist(key):
                string[key] = value

        for i in string:
            print(i, string[i])
        # ---- Invoice Creation ----

        no = string['number']
        party_id = Party.get_by_gstin(string['select_party'])

        date = string['date']
        narration = string['narration']

        invoice_amount = string['total_amount']
        e_way = string['e_way']

        invoice = Invoice(no, party_id._id, invoice_amount, date, narration,
                          e_way)
        invoice.save_to_mongo()
        # --- Find Items
        items = []
        serial_no = {}

        for i in string:
            print(i[:7])
            if i[:12] == 'select_item_':
                items.append([i, string[i][14::]])

            if i[:7] == 'myModal':
                if i[8] != '_':
                    num = int(i[7]) * 10 + int(i[8])
                else:
                    num = int(i[7])
                if str(num) not in serial_no:
                    serial_no[str(num)] = []

                serial_no[str(num)].append(string[i])

        # print(items)
        item_details = []
        for i in range(len(items)):
            item = items[i][0][12::]
            quantity = string['quantity_' + item]
            rate_per = string['rate_per_' + item]
            item_details.append(
                [item, items[i][1], quantity, rate_per, serial_no[item]])

        # print()
        # print(item_details)

        for i in item_details:
            item_id = Item.get_by_name(i[1])
            print(invoice._id, item_id, float(i[3]), int(i[2]))
            invoice_item = Invoice_item(invoice._id, item_id._id, float(i[3]),
                                        int(i[2]))
            invoice_item.save_to_mongo()
            for j in i[4]:
                serial_no = Serial_no(invoice_item._id, j)
                serial_no.save_to_mongo()
        # print()
        # print(serial_no)
        # print(string)

        return redirect(url_for('.print_invoice', invoice_id=invoice._id))

    return render_template('invoices/edit_invoice.jinja2',
                           invoice_package=invoice_package,
                           edit_invoice_page=True,
                           parties=parties,
                           items=items,
                           company=Company.get_company()[0])