fix: Add tds in PO and code cleanup

This commit is contained in:
Deepesh Garg
2020-11-29 21:40:04 +05:30
parent c26de28613
commit d18dde7757
11 changed files with 676 additions and 197 deletions

View File

@@ -700,6 +700,7 @@ class AccountsController(TransactionBase):
from erpnext.accounts.utils import unlink_ref_doc_from_payment_entries
if self.doctype in ["Sales Invoice", "Purchase Invoice"]:
self.update_allocated_advance_taxes()
if frappe.db.get_single_value('Accounts Settings', 'unlink_payment_on_cancellation_of_invoice'):
unlink_ref_doc_from_payment_entries(self)
@@ -707,6 +708,35 @@ class AccountsController(TransactionBase):
if frappe.db.get_single_value('Accounts Settings', 'unlink_advance_payment_on_cancelation_of_order'):
unlink_ref_doc_from_payment_entries(self)
def get_tax_map(self):
tax_map = {}
for tax in self.get('taxes'):
tax_map.setdefault(tax.account_head, 0.0)
tax_map[tax.account_head] += tax.tax_amount
return tax_map
def update_allocated_advance_taxes(self):
if self.get('advances'):
tax_accounts = [d.account_head for d in self.get('taxes')]
allocated_tax_map = frappe._dict(frappe.get_all('GL Entry', fields=['account', 'sum(credit - debit)'],
filters={'voucher_no': self.name, 'account': ('in', tax_accounts)},
group_by='account', as_list=1))
tax_map = self.get_tax_map()
for pe in self.get('advances'):
pe = frappe.get_doc('Payment Entry', pe.reference_name)
for tax in pe.get('taxes'):
allocated_amount = tax_map.get(tax.account_head) - allocated_tax_map.get(tax.account_head)
if allocated_amount > tax.tax_amount:
allocated_amount = tax.tax_amount
if allocated_amount:
frappe.db.set_value('Advance Taxes and Charges', tax.name, 'allocated_amount', tax.allocated_amount - allocated_amount)
tax_map[tax.account_head] -= allocated_amount
allocated_tax_map[tax.account_head] -= allocated_amount
def validate_multiple_billing(self, ref_dt, item_ref_dn, based_on, parentfield):
from erpnext.controllers.status_updater import get_allowance_for
item_allowance = {}

View File

@@ -40,7 +40,6 @@ class calculate_taxes_and_totals(object):
self.validate_conversion_rate()
self.calculate_item_values()
self.validate_item_tax_template()
self.apply_advance_taxes()
self.initialize_taxes()
self.determine_exclusive_rate()
self.calculate_net_total()
@@ -684,33 +683,6 @@ class calculate_taxes_and_totals(object):
self.calculate_paid_amount()
def apply_advance_taxes(self):
if cint(self.doc.get('adjust_advance_taxes')):
if self.doc.get('advances'):
payment_entry_list = [d.reference_name for d in self.doc.get('advances')]
advance_taxes = get_advance_taxes(payment_entry_list)
accounts = []
# Remove already added advance taxes if any
for tax in self.doc.get('taxes'):
if tax.is_advance_tax:
self.doc.remove(tax)
else:
accounts.append(tax.account_head)
for tax in advance_taxes:
# Reverse add deduct from payment entry in invoice
if tax.account_head in accounts:
add_deduct_tax = 'Deduct' if tax.add_deduct_tax == 'Add' else 'Add'
tax.update({
'add_deduct_tax': add_deduct_tax,
'category': tax.get('category') or 'Total',
'is_advance_tax': 1,
'charge_type': 'On Net Total' if tax.charge_type == 'On Paid Amount' else tax.charge_type
})
self.doc.append('taxes', tax)
def get_itemised_tax_breakup_html(doc):
if not doc.taxes:
return