fix: Fetch Payment Terms from Sales/Purchase Orders

This commit is contained in:
GangaManoj 2021-07-17 22:53:21 +05:30
parent bf245e5e39
commit cde0cc0678
7 changed files with 66 additions and 68 deletions

View File

@ -508,8 +508,6 @@ frappe.ui.form.on("Purchase Invoice", {
}
}
}
frm.events.set_payment_terms(frm);
},
refresh: function(frm) {
@ -572,21 +570,4 @@ frappe.ui.form.on("Purchase Invoice", {
company: function(frm) {
erpnext.accounts.dimensions.update_dimension(frm, frm.doctype);
},
set_payment_terms: function (frm) {
frappe.call({
'method': 'erpnext.accounts.doctype.purchase_invoice.purchase_invoice.set_payment_terms_from_po',
'args': {
doc: frm.doc
},
'callback': (r) => {
if (r.message) {
var doc = frappe.model.sync(r.message)[0];
console.log("doc: ", doc)
frappe.set_route("Form", doc.doctype, doc.name);
}
}
});
},
})

View File

@ -1173,49 +1173,6 @@ class PurchaseInvoice(BuyingController):
if update:
self.db_set('status', self.status, update_modified = update_modified)
@frappe.whitelist()
def set_payment_terms_from_po(doc):
if isinstance(doc, six.string_types):
doc = json.loads(doc)
purchase_order = doc.get('items')[0].get('purchase_order')
if purchase_order and all_items_have_same_po(doc, purchase_order):
purchase_order = frappe.get_cached_doc('Purchase Order', purchase_order)
else:
return
if has_default_payment_terms(doc) and not has_default_payment_terms(purchase_order):
doc['payment_schedule'] = []
doc['payment_terms_template'] = purchase_order.payment_terms_template
for schedule in purchase_order.payment_schedule:
payment_schedule = {
'payment_term': schedule.payment_term,
'due_date': schedule.due_date,
'invoice_portion': schedule.invoice_portion,
'discount_type': schedule.discount_type,
'discount': schedule.discount,
'base_payment_amount': schedule.base_payment_amount,
'payment_amount': schedule.payment_amount,
'outstanding': schedule.outstanding
}
doc['payment_schedule'].append(payment_schedule)
return doc
def all_items_have_same_po(doc, purchase_order):
for item in doc.get('items'):
if item.get('purchase_order') != purchase_order:
return False
return True
def has_default_payment_terms(doc):
if doc.get('payment_schedule')[0].get('invoice_portion') == 100:
return True
return False
# to get details of purchase invoice/receipt from which this doc was created for exchange rate difference handling
def get_purchase_document_details(doc):
if doc.doctype == 'Purchase Invoice':

View File

@ -443,6 +443,8 @@ def make_purchase_invoice_from_portal(purchase_order_name):
frappe.response.location = '/purchase-invoices/' + doc.name
def get_mapped_purchase_invoice(source_name, target_doc=None, ignore_permissions=False):
from erpnext.controllers.accounts_controller import fetch_payment_terms_from_order
def postprocess(source, target):
target.flags.ignore_permissions = ignore_permissions
set_missing_values(source, target)
@ -489,15 +491,13 @@ def get_mapped_purchase_invoice(source_name, target_doc=None, ignore_permissions
},
}
if frappe.get_single("Accounts Settings").automatically_fetch_payment_terms == 1:
fields["Payment Schedule"] = {
"doctype": "Payment Schedule",
"add_if_empty": True
}
doc = get_mapped_doc("Purchase Order", source_name, fields,
target_doc, postprocess, ignore_permissions=ignore_permissions)
automatically_fetch_payment_terms = cint(frappe.db.get_single_value('Accounts Settings', 'automatically_fetch_payment_terms'))
if automatically_fetch_payment_terms:
fetch_payment_terms_from_order(doc)
return doc
@frappe.whitelist()

View File

@ -1730,3 +1730,46 @@ def validate_regional(doc):
@erpnext.allow_regional
def validate_einvoice_fields(doc):
pass
def fetch_payment_terms_from_order(doc):
"""
Fetch Payment Terms from Purchase/Sales Order on creating a new Purchase/Sales Invoice.
"""
if doc.doctype == "Sales Invoice":
po_or_so = doc.get('items')[0].get('sales_order')
po_or_so_doctype = "Sales Order"
po_or_so_doctype_name = "sales_order"
else:
po_or_so = doc.get('items')[0].get('purchase_order')
po_or_so_doctype = "Purchase Order"
po_or_so_doctype_name = "purchase_order"
if po_or_so and all_items_have_same_po_or_so(doc, po_or_so, po_or_so_doctype_name):
po_or_so = frappe.get_cached_doc(po_or_so_doctype, po_or_so)
else:
doc.set_payment_schedule()
return
doc.payment_schedule = []
doc.payment_terms_template = po_or_so.payment_terms_template
for schedule in po_or_so.payment_schedule:
payment_schedule = {
'payment_term': schedule.payment_term,
'due_date': schedule.due_date,
'invoice_portion': schedule.invoice_portion,
'discount_type': schedule.discount_type,
'discount': schedule.discount,
'base_payment_amount': schedule.base_payment_amount,
'payment_amount': schedule.payment_amount,
'outstanding': schedule.outstanding
}
doc.append("payment_schedule", payment_schedule)
def all_items_have_same_po_or_so(doc, po_or_so, po_or_so_fieldname):
for item in doc.get('items'):
if item.get(po_or_so_fieldname) != po_or_so:
return False
return True

View File

@ -621,6 +621,8 @@ def make_delivery_note(source_name, target_doc=None, skip_item_mapping=False):
@frappe.whitelist()
def make_sales_invoice(source_name, target_doc=None, ignore_permissions=False):
from erpnext.controllers.accounts_controller import fetch_payment_terms_from_order
def postprocess(source, target):
set_missing_values(source, target)
#Get the advance paid Journal Entries in Sales Invoice Advance
@ -693,6 +695,10 @@ def make_sales_invoice(source_name, target_doc=None, ignore_permissions=False):
}
}, target_doc, postprocess, ignore_permissions=ignore_permissions)
automatically_fetch_payment_terms = cint(frappe.db.get_single_value('Accounts Settings', 'automatically_fetch_payment_terms'))
if automatically_fetch_payment_terms:
fetch_payment_terms_from_order(doclist)
return doclist
@frappe.whitelist()

View File

@ -414,6 +414,8 @@ def get_returned_qty_map(delivery_note):
@frappe.whitelist()
def make_sales_invoice(source_name, target_doc=None):
from erpnext.controllers.accounts_controller import fetch_payment_terms_from_order
doc = frappe.get_doc('Delivery Note', source_name)
to_make_invoice_qty_map = {}
@ -503,6 +505,10 @@ def make_sales_invoice(source_name, target_doc=None):
}
}, target_doc, set_missing_values)
automatically_fetch_payment_terms = cint(frappe.db.get_single_value('Accounts Settings', 'automatically_fetch_payment_terms'))
if automatically_fetch_payment_terms:
fetch_payment_terms_from_order(doc)
return doc
@frappe.whitelist()

View File

@ -604,6 +604,7 @@ def update_billing_percentage(pr_doc, update_modified=True):
@frappe.whitelist()
def make_purchase_invoice(source_name, target_doc=None):
from erpnext.accounts.party import get_payment_terms_template
from erpnext.controllers.accounts_controller import fetch_payment_terms_from_order
doc = frappe.get_doc('Purchase Receipt', source_name)
returned_qty_map = get_returned_qty_map(source_name)
@ -675,6 +676,10 @@ def make_purchase_invoice(source_name, target_doc=None):
}
}, target_doc, set_missing_values)
automatically_fetch_payment_terms = cint(frappe.db.get_single_value('Accounts Settings', 'automatically_fetch_payment_terms'))
if automatically_fetch_payment_terms:
fetch_payment_terms_from_order(doclist)
return doclist
def get_invoiced_qty_map(purchase_receipt):