diff --git a/accounts/report/accounts_payable/accounts_payable.py b/accounts/report/accounts_payable/accounts_payable.py index 71aeb35ce0..6bc98f6471 100644 --- a/accounts/report/accounts_payable/accounts_payable.py +++ b/accounts/report/accounts_payable/accounts_payable.py @@ -24,7 +24,6 @@ def execute(filters=None): for gle in entries: if cstr(gle.against_voucher) == gle.voucher_no or not gle.against_voucher \ or [gle.against_voucher_type, gle.against_voucher] in entries_after_report_date: - if gle.voucher_type == "Purchase Invoice": pi_info = pi_map.get(gle.voucher_no) due_date = pi_info.get("due_date") @@ -33,12 +32,12 @@ def execute(filters=None): else: due_date = bill_no = bill_date = "" - invoiced_amount = gle.credit > 0 and gle.credit or 0 - paid_amount = get_paid_amount(gle, filters.get("report_date") or nowdate(), - entries_after_report_date) - outstanding_amount = invoiced_amount - paid_amount + invoiced_amount = gle.credit > 0 and gle.credit or 0 + outstanding_amount = get_outstanding_amount(gle, + filters.get("report_date") or nowdate()) if abs(flt(outstanding_amount)) > 0.01: + paid_amount = invoiced_amount - outstanding_amount row = [gle.posting_date, gle.account, gle.voucher_type, gle.voucher_no, gle.remarks, account_supplier_type_map.get(gle.account), due_date, bill_no, bill_date, invoiced_amount, paid_amount, outstanding_amount] @@ -116,17 +115,13 @@ def get_pi_map(): return pi_map -def get_paid_amount(gle, report_date, entries_after_report_date): - - paid_amount = 0 - if flt(gle.debit) > 0 and (not gle.against_voucher or - [gle.against_voucher_type, gle.against_voucher] in entries_after_report_date): - paid_amount = gle.debit - elif flt(gle.credit) > 0: - paid_amount = webnotes.conn.sql(""" - select sum(ifnull(debit, 0)) - sum(ifnull(credit, 0)) from `tabGL Entry` - where account = %s and posting_date <= %s and against_voucher_type = %s - and against_voucher = %s and name != %s and ifnull(is_cancelled, 'No') = 'No'""", - (gle.account, report_date, gle.voucher_type, gle.voucher_no, gle.name))[0][0] - - return flt(paid_amount) \ No newline at end of file +def get_outstanding_amount(gle, report_date): + payment_amount = webnotes.conn.sql(""" + select sum(ifnull(debit, 0)) - sum(ifnull(credit, 0)) + from `tabGL Entry` + where account = %s and posting_date <= %s and against_voucher_type = %s + and against_voucher = %s and name != %s and ifnull(is_cancelled, 'No') = 'No'""", + (gle.account, report_date, gle.voucher_type, gle.voucher_no, gle.name))[0][0] + + outstanding_amount = flt(gle.credit) - flt(gle.debit) - flt(payment_amount) + return outstanding_amount \ No newline at end of file diff --git a/accounts/report/accounts_receivable/accounts_receivable.py b/accounts/report/accounts_receivable/accounts_receivable.py index d791fad23f..2bd3be878e 100644 --- a/accounts/report/accounts_receivable/accounts_receivable.py +++ b/accounts/report/accounts_receivable/accounts_receivable.py @@ -27,11 +27,11 @@ def execute(filters=None): and si_due_date_map.get(gle.voucher_no) or "" invoiced_amount = gle.debit > 0 and gle.debit or 0 - payment_amount = get_payment_amount(gle, filters.get("report_date") or nowdate(), - entries_after_report_date) - outstanding_amount = invoiced_amount - payment_amount + outstanding_amount = get_outstanding_amount(gle, + filters.get("report_date") or nowdate()) if abs(flt(outstanding_amount)) > 0.01: + payment_amount = invoiced_amount - outstanding_amount row = [gle.posting_date, gle.account, gle.voucher_type, gle.voucher_no, gle.remarks, due_date, account_territory_map.get(gle.account), invoiced_amount, payment_amount, outstanding_amount] @@ -104,6 +104,16 @@ def get_si_due_date_map(): return si_due_date_map +def get_outstanding_amount(gle, report_date): + payment_amount = webnotes.conn.sql(""" + select sum(ifnull(credit, 0)) - sum(ifnull(debit, 0)) + from `tabGL Entry` + where account = %s and posting_date <= %s and against_voucher_type = %s + and against_voucher = %s and name != %s and ifnull(is_cancelled, 'No') = 'No'""", + (gle.account, report_date, gle.voucher_type, gle.voucher_no, gle.name))[0][0] + + return flt(gle.debit) - flt(gle.credit) - flt(payment_amount) + def get_payment_amount(gle, report_date, entries_after_report_date): payment_amount = 0 if flt(gle.credit) > 0 and (not gle.against_voucher or diff --git a/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py b/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py index f7afb3dfb6..9f5f071e02 100644 --- a/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py +++ b/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py @@ -26,9 +26,9 @@ def execute(filters=None): data = [] for d in item_list: expense_head = d.expense_head or aii_account_map.get(d.company) - data.append([d.item_code, d.item_name, d.item_group, d.name, d.posting_date, d.supplier, - d.credit_to, d.project_name, d.company, d.purchase_order, d.purchase_receipt, - expense_head, d.qty, d.rate, d.amount]) + data.append([d.item_code, d.item_name, d.item_group, d.name, d.posting_date, + d.supplier_name, d.credit_to, d.project_name, d.company, d.purchase_order, + d.purchase_receipt, expense_head, d.qty, d.rate, d.amount]) return columns, data @@ -59,7 +59,7 @@ def get_items(filters): return webnotes.conn.sql("""select pi.name, pi.posting_date, pi.credit_to, pi.company, pi.supplier, pi.remarks, pi_item.item_code, pi_item.item_name, pi_item.item_group, pi_item.project_name, pi_item.purchase_order, pi_item.purchase_receipt, - pi_item.expense_head, pi_item.qty, pi_item.rate, pi_item.amount + pi_item.expense_head, pi_item.qty, pi_item.rate, pi_item.amount, pi.supplier_name from `tabPurchase Invoice` pi, `tabPurchase Invoice Item` pi_item where pi.name = pi_item.parent and pi.docstatus = 1 %s order by pi.posting_date desc, pi_item.item_code desc""" % conditions, filters, as_dict=1) diff --git a/accounts/report/item_wise_sales_register/item_wise_sales_register.py b/accounts/report/item_wise_sales_register/item_wise_sales_register.py index f3ed2a1242..4d099e0b97 100644 --- a/accounts/report/item_wise_sales_register/item_wise_sales_register.py +++ b/accounts/report/item_wise_sales_register/item_wise_sales_register.py @@ -16,7 +16,6 @@ from __future__ import unicode_literals import webnotes -from webnotes.utils import flt def execute(filters=None): if not filters: filters = {} @@ -26,9 +25,9 @@ def execute(filters=None): data = [] for d in item_list: - data.append([d.item_code, d.item_name, d.item_group, d.name, d.posting_date, d.customer, - d.debit_to, d.territory, d.project_name, d.company, d.sales_order, d.delivery_note, - d.income_account, d.qty, d.basic_rate, d.amount]) + data.append([d.item_code, d.item_name, d.item_group, d.name, d.posting_date, + d.customer_name, d.debit_to, d.territory, d.project_name, d.company, d.sales_order, + d.delivery_note, d.income_account, d.qty, d.basic_rate, d.amount]) return columns, data @@ -61,7 +60,7 @@ def get_items(filters): return webnotes.conn.sql("""select si.name, si.posting_date, si.debit_to, si.project_name, si.customer, si.remarks, si.territory, si.company, si_item.item_code, si_item.item_name, si_item.item_group, si_item.sales_order, si_item.delivery_note, si_item.income_account, - si_item.qty, si_item.basic_rate, si_item.amount + si_item.qty, si_item.basic_rate, si_item.amount, si.customer_name from `tabSales Invoice` si, `tabSales Invoice Item` si_item where si.name = si_item.parent and si.docstatus = 1 %s order by si.posting_date desc, si_item.item_code desc""" % conditions, filters, as_dict=1) \ No newline at end of file diff --git a/accounts/report/purchase_register/purchase_register.py b/accounts/report/purchase_register/purchase_register.py index 0fbe451035..09705413a6 100644 --- a/accounts/report/purchase_register/purchase_register.py +++ b/accounts/report/purchase_register/purchase_register.py @@ -31,7 +31,7 @@ def execute(filters=None): return columns, invoice_list invoice_expense_map = get_invoice_expense_map(invoice_list) - invoice_tax_map = get_invoice_tax_map(invoice_list) + invoice_expense_map, invoice_tax_map = get_invoice_tax_map(invoice_list, invoice_expense_map) invoice_po_pr_map = get_invoice_po_pr_map(invoice_list) account_map = get_account_details(invoice_list) @@ -42,25 +42,32 @@ def execute(filters=None): purchase_receipt = list(set(invoice_po_pr_map.get(inv.name, {}).get("purchase_receipt", []))) project_name = list(set(invoice_po_pr_map.get(inv.name, {}).get("project_name", []))) - row = [inv.name, inv.posting_date, inv.supplier, inv.credit_to, + row = [inv.name, inv.posting_date, inv.supplier_name, inv.credit_to, account_map.get(inv.credit_to), ", ".join(project_name), inv.bill_no, inv.bill_date, inv.remarks, ", ".join(purchase_order), ", ".join(purchase_receipt)] # map expense values + net_total = 0 for expense_acc in expense_accounts: - row.append(invoice_expense_map.get(inv.name, {}).get(expense_acc)) + expense_amount = flt(invoice_expense_map.get(inv.name, {}).get(expense_acc)) + net_total += expense_amount + row.append(expense_amount) # net total - row.append(inv.net_total) + row.append(net_total) # tax account + total_tax = 0 for tax_acc in tax_accounts: - row.append(invoice_tax_map.get(inv.name, {}).get(tax_acc)) + if tax_acc not in expense_accounts: + tax_amount = flt(invoice_tax_map.get(inv.name, {}).get(tax_acc)) + total_tax += tax_amount + row.append(tax_amount) # total tax, grand total, outstanding amount & rounded total - row += [inv.total_tax, inv.grand_total, flt(inv.grand_total, 2), \ - inv.outstanding_amount] + row += [total_tax, inv.grand_total, flt(inv.grand_total, 2), inv.outstanding_amount] data.append(row) + # raise Exception return columns, data @@ -68,12 +75,13 @@ def execute(filters=None): def get_columns(invoice_list): """return columns based on filters""" columns = [ - "Invoice:Link/Purchase Invoice:120", "Posting Date:Date:80", "Supplier:Link/Supplier:120", + "Invoice:Link/Purchase Invoice:120", "Posting Date:Date:80", "Supplier::120", "Supplier Account:Link/Account:120", "Account Group:LInk/Account:120", "Project:Link/Project:80", "Bill No::120", "Bill Date:Date:80", "Remarks::150", "Purchase Order:Link/Purchase Order:100", "Purchase Receipt:Link/Purchase Receipt:100" ] - expense_accounts = tax_accounts = [] + expense_accounts = tax_accounts = expense_columns = tax_columns = [] + if invoice_list: expense_accounts = webnotes.conn.sql_list("""select distinct expense_head from `tabPurchase Invoice Item` where docstatus = 1 and ifnull(expense_head, '') != '' @@ -85,9 +93,15 @@ def get_columns(invoice_list): and docstatus = 1 and ifnull(account_head, '') != '' and parent in (%s) order by account_head""" % ', '.join(['%s']*len(invoice_list)), tuple([inv.name for inv in invoice_list])) + + + expense_columns = [(account + ":Currency:120") for account in expense_accounts] + for account in tax_accounts: + if account not in expense_accounts: + tax_columns.append(account + ":Currency:120") - columns = columns + [(account + ":Currency:120") for account in expense_accounts] + \ - ["Net Total:Currency:120"] + [(account + ":Currency:120") for account in tax_accounts] + \ + columns = columns + expense_columns + \ + ["Net Total:Currency:120"] + tax_columns + \ ["Total Tax:Currency:120"] + ["Grand Total:Currency:120"] + \ ["Rounded Total:Currency:120"] + ["Outstanding Amount:Currency:120"] @@ -106,9 +120,8 @@ def get_conditions(filters): def get_invoices(filters): conditions = get_conditions(filters) - return webnotes.conn.sql("""select name, posting_date, credit_to, - supplier, bill_no, bill_date, remarks, net_total, - total_tax, grand_total, outstanding_amount + return webnotes.conn.sql("""select name, posting_date, credit_to, supplier, supplier_name, + bill_no, bill_date, remarks, grand_total, outstanding_amount from `tabPurchase Invoice` where docstatus = 1 %s order by posting_date desc, name desc""" % conditions, filters, as_dict=1) @@ -125,17 +138,20 @@ def get_invoice_expense_map(invoice_list): return invoice_expense_map -def get_invoice_tax_map(invoice_list): +def get_invoice_tax_map(invoice_list, invoice_expense_map): tax_details = webnotes.conn.sql("""select parent, account_head, sum(tax_amount) as tax_amount from `tabPurchase Taxes and Charges` where parent in (%s) group by parent, account_head""" % ', '.join(['%s']*len(invoice_list)), tuple([inv.name for inv in invoice_list]), as_dict=1) invoice_tax_map = {} for d in tax_details: - invoice_tax_map.setdefault(d.parent, webnotes._dict()).setdefault(d.account_head, []) - invoice_tax_map[d.parent][d.account_head] = flt(d.tax_amount) + if d.account_head in invoice_expense_map.get(d.parent): + invoice_expense_map[d.parent][d.account_head] += flt(d.tax_amount) + else: + invoice_tax_map.setdefault(d.parent, webnotes._dict()).setdefault(d.account_head, []) + invoice_tax_map[d.parent][d.account_head] = flt(d.tax_amount) - return invoice_tax_map + return invoice_expense_map, invoice_tax_map def get_invoice_po_pr_map(invoice_list): pi_items = webnotes.conn.sql("""select parent, purchase_order, purchase_receipt, diff --git a/accounts/report/sales_register/sales_register.py b/accounts/report/sales_register/sales_register.py index 3eebc33e9c..91ad1c282f 100644 --- a/accounts/report/sales_register/sales_register.py +++ b/accounts/report/sales_register/sales_register.py @@ -26,11 +26,11 @@ def execute(filters=None): columns, income_accounts, tax_accounts = get_columns(invoice_list) if not invoice_list: - msgprint(_("No record found")) + msgprint(_("No record found")) return columns, invoice_list invoice_income_map = get_invoice_income_map(invoice_list) - invoice_tax_map = get_invoice_tax_map(invoice_list) + invoice_income_map, invoice_tax_map = get_invoice_tax_map(invoice_list, invoice_income_map) invoice_so_dn_map = get_invoice_so_dn_map(invoice_list) customer_map = get_customer_deatils(invoice_list) @@ -42,23 +42,30 @@ def execute(filters=None): sales_order = list(set(invoice_so_dn_map.get(inv.name, {}).get("sales_order", []))) delivery_note = list(set(invoice_so_dn_map.get(inv.name, {}).get("delivery_note", []))) - row = [inv.name, inv.posting_date, inv.customer, inv.debit_to, + row = [inv.name, inv.posting_date, inv.customer_name, inv.debit_to, account_map.get(inv.debit_to), customer_map.get(inv.customer), inv.project_name, inv.remarks, ", ".join(sales_order), ", ".join(delivery_note)] # map income values + net_total = 0 for income_acc in income_accounts: - row.append(invoice_income_map.get(inv.name, {}).get(income_acc)) + income_amount = flt(invoice_income_map.get(inv.name, {}).get(income_acc)) + net_total += income_amount + row.append(income_amount) # net total - row.append(inv.net_total) + row.append(net_total) # tax account + total_tax = 0 for tax_acc in tax_accounts: - row.append(invoice_tax_map.get(inv.name, {}).get(tax_acc)) + if tax_acc not in income_accounts: + tax_amount = flt(invoice_tax_map.get(inv.name, {}).get(tax_acc)) + total_tax += tax_amount + row.append(tax_amount) # total tax, grand total, outstanding amount & rounded total - row += [inv.other_charges_total, inv.grand_total, inv.rounded_total, inv.outstanding_amount] + row += [total_tax, inv.grand_total, inv.rounded_total, inv.outstanding_amount] data.append(row) @@ -68,13 +75,14 @@ def execute(filters=None): def get_columns(invoice_list): """return columns based on filters""" columns = [ - "Invoice:Link/Sales Invoice:120", "Posting Date:Date:80", "Customer:Link/Customer:120", + "Invoice:Link/Sales Invoice:120", "Posting Date:Date:80", "Customer::120", "Customer Account:Link/Account:120", "Account Group:LInk/Account:120", "Territory:Link/Territory:80", "Project:Link/Project:80", "Remarks::150", "Sales Order:Link/Sales Order:100", "Delivery Note:Link/Delivery Note:100" ] - income_accounts = tax_accounts = [] + income_accounts = tax_accounts = income_columns = tax_columns = [] + if invoice_list: income_accounts = webnotes.conn.sql_list("""select distinct income_account from `tabSales Invoice Item` where docstatus = 1 and parent in (%s) @@ -83,11 +91,16 @@ def get_columns(invoice_list): tax_accounts = webnotes.conn.sql_list("""select distinct account_head from `tabSales Taxes and Charges` where parenttype = 'Sales Invoice' - and docstatus = 1 and parent in (%s) order by account_head""" % + and docstatus = 1 and ifnull(tax_amount, 0) != 0 + and parent in (%s) order by account_head""" % ', '.join(['%s']*len(invoice_list)), tuple([inv.name for inv in invoice_list])) + + income_columns = [(account + ":Currency:120") for account in income_accounts] + for account in tax_accounts: + if account not in income_accounts: + tax_columns.append(account + ":Currency:120") - columns = columns + [(account + ":Currency:120") for account in income_accounts] + \ - ["Net Total:Currency:120"] + [(account + ":Currency:120") for account in tax_accounts] + \ + columns = columns + income_columns + ["Net Total:Currency:120"] + tax_columns + \ ["Total Tax:Currency:120"] + ["Grand Total:Currency:120"] + \ ["Rounded Total:Currency:120"] + ["Outstanding Amount:Currency:120"] @@ -107,8 +120,8 @@ def get_conditions(filters): def get_invoices(filters): conditions = get_conditions(filters) return webnotes.conn.sql("""select name, posting_date, debit_to, project_name, customer, - remarks, net_total, other_charges_total, grand_total, rounded_total, - outstanding_amount from `tabSales Invoice` + customer_name, remarks, grand_total, rounded_total, outstanding_amount + from `tabSales Invoice` where docstatus = 1 %s order by posting_date desc, name desc""" % conditions, filters, as_dict=1) @@ -124,17 +137,20 @@ def get_invoice_income_map(invoice_list): return invoice_income_map -def get_invoice_tax_map(invoice_list): +def get_invoice_tax_map(invoice_list, invoice_income_map): tax_details = webnotes.conn.sql("""select parent, account_head, sum(tax_amount) as tax_amount from `tabSales Taxes and Charges` where parent in (%s) group by parent, account_head""" % ', '.join(['%s']*len(invoice_list)), tuple([inv.name for inv in invoice_list]), as_dict=1) invoice_tax_map = {} for d in tax_details: - invoice_tax_map.setdefault(d.parent, webnotes._dict()).setdefault(d.account_head, []) - invoice_tax_map[d.parent][d.account_head] = flt(d.tax_amount) + if d.account_head in invoice_income_map.get(d.parent): + invoice_income_map[d.parent][d.account_head] += flt(d.tax_amount) + else: + invoice_tax_map.setdefault(d.parent, webnotes._dict()).setdefault(d.account_head, []) + invoice_tax_map[d.parent][d.account_head] = flt(d.tax_amount) - return invoice_tax_map + return invoice_income_map, invoice_tax_map def get_invoice_so_dn_map(invoice_list): si_items = webnotes.conn.sql("""select parent, sales_order, delivery_note diff --git a/stock/report/purchase_order_items_to_be_received/purchase_order_items_to_be_received.txt b/stock/report/purchase_order_items_to_be_received/purchase_order_items_to_be_received.txt index c588dde3f7..98dcd22e8c 100644 --- a/stock/report/purchase_order_items_to_be_received/purchase_order_items_to_be_received.txt +++ b/stock/report/purchase_order_items_to_be_received/purchase_order_items_to_be_received.txt @@ -2,7 +2,7 @@ { "creation": "2013-02-22 18:01:55", "docstatus": 0, - "modified": "2013-05-28 16:03:15", + "modified": "2013-07-16 13:19:24", "modified_by": "Administrator", "owner": "Administrator" }, @@ -11,7 +11,7 @@ "doctype": "Report", "is_standard": "Yes", "name": "__common__", - "query": "select \n `tabPurchase Order`.`name` as \"Purchase Order:Link/Purchase Order:120\",\n\t`tabPurchase Order`.`transaction_date` as \"Date:Date:100\",\n\t`tabPurchase Order`.`supplier` as \"Supplier:Link/Supplier:120\",\n\t`tabPurchase Order Item`.`project_name` as \"Project\",\n\t`tabPurchase Order Item`.item_code as \"Item Code:Link/Item:120\",\n\t`tabPurchase Order Item`.qty as \"Qty:Float:100\",\n\t`tabPurchase Order Item`.received_qty as \"Received Qty:Float:100\", \n\t(`tabPurchase Order Item`.qty - ifnull(`tabPurchase Order Item`.received_qty, 0)) as \"Qty to Receive:Float:100\",\n\t`tabPurchase Order Item`.item_name as \"Item Name::150\",\n\t`tabPurchase Order Item`.description as \"Description::200\"\nfrom\n\t`tabPurchase Order`, `tabPurchase Order Item`\nwhere\n\t`tabPurchase Order Item`.`parent` = `tabPurchase Order`.`name`\n\tand `tabPurchase Order`.docstatus = 1\n\tand `tabPurchase Order`.status != \"Stopped\"\n\tand ifnull(`tabPurchase Order Item`.received_qty, 0) < ifnull(`tabPurchase Order Item`.qty, 0)\norder by `tabPurchase Order`.transaction_date asc", + "query": "select \n `tabPurchase Order`.`name` as \"Purchase Order:Link/Purchase Order:120\",\n\t`tabPurchase Order`.`transaction_date` as \"Date:Date:100\",\n\t`tabPurchase Order`.`supplier` as \"Supplier:Link/Supplier:120\",\n\t`tabPurchase Order Item`.`project_name` as \"Project\",\n\t`tabPurchase Order Item`.item_code as \"Item Code:Link/Item:120\",\n\t`tabPurchase Order Item`.qty as \"Qty:Float:100\",\n\t`tabPurchase Order Item`.received_qty as \"Received Qty:Float:100\", \n\t(`tabPurchase Order Item`.qty - ifnull(`tabPurchase Order Item`.received_qty, 0)) as \"Qty to Receive:Float:100\",\n `tabPurchase Order Item`.warehouse as \"Warehouse:Link/Warehouse:150\",\n\t`tabPurchase Order Item`.item_name as \"Item Name::150\",\n\t`tabPurchase Order Item`.description as \"Description::200\",\n `tabPurchase Order Item`.brand as \"Brand::100\"\nfrom\n\t`tabPurchase Order`, `tabPurchase Order Item`\nwhere\n\t`tabPurchase Order Item`.`parent` = `tabPurchase Order`.`name`\n\tand `tabPurchase Order`.docstatus = 1\n\tand `tabPurchase Order`.status != \"Stopped\"\n\tand ifnull(`tabPurchase Order Item`.received_qty, 0) < ifnull(`tabPurchase Order Item`.qty, 0)\norder by `tabPurchase Order`.transaction_date asc", "ref_doctype": "Purchase Receipt", "report_name": "Purchase Order Items To Be Received", "report_type": "Query Report"