From ae0b31c31d3160e07a19c7d26da0bb855a9af657 Mon Sep 17 00:00:00 2001 From: Shreya Date: Mon, 23 Apr 2018 16:21:59 +0530 Subject: [PATCH] changes as per the reviews --- .../purchase_invoice/purchase_invoice.js | 17 +-- .../purchase_invoice/purchase_invoice.py | 35 +---- .../doctype/sales_invoice/sales_invoice.js | 22 +-- .../doctype/sales_invoice/sales_invoice.py | 126 +++++++++--------- 4 files changed, 75 insertions(+), 125 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js index a2ea0965aa7..e7fdd64a6d8 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js @@ -99,9 +99,10 @@ erpnext.accounts.PurchaseInvoice = erpnext.buying.BuyingController.extend({ frappe.model.with_doc("Supplier", me.frm.doc.supplier, function() { var supplier = frappe.model.get_doc("Supplier", me.frm.doc.supplier); var internal = supplier.is_internal_supplier; - if (internal == 1) { + var disabled = supplier.disabled; + if (internal == 1 && disabled == 0) { me.frm.add_custom_button("Inter Company Invoice", function() { - me.validate_inter_company_invoice(me.frm); + me.make_inter_company_invoice(me.frm); }, __("Make")); } }); @@ -144,18 +145,6 @@ erpnext.accounts.PurchaseInvoice = erpnext.buying.BuyingController.extend({ }); } }, - validate_inter_company_invoice: function(frm) { - var me = this; - frappe.call({ - method: "erpnext.accounts.doctype.sales_invoice.sales_invoice.validate_inter_company_invoice", - args: {"doc": frm.doc}, - callback: function(r) { - if (r && r.message) { - me.make_inter_company_invoice(frm); - } - } - }); - }, make_inter_company_invoice: function(frm) { frappe.model.open_mapped_doc({ diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index 66515e34525..67b41a3248d 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -17,6 +17,8 @@ from erpnext.accounts.doctype.gl_entry.gl_entry import update_outstanding_amt from erpnext.buying.utils import check_for_closed_status from erpnext.accounts.general_ledger import get_round_off_account_and_cost_center from frappe.model.mapper import get_mapped_doc +from erpnext.accounts.doctype.sales_invoice.sales_invoice import validate_inter_company_party, update_linked_invoice,\ + unlink_inter_company_invoice form_grid_templates = { "items": "templates/form_grid/item_grid.html" @@ -73,7 +75,7 @@ class PurchaseInvoice(BuyingController): self.validate_fixed_asset_account() self.create_remarks() self.set_status() - self.validate_inter_company_supplier() + validate_inter_company_party(self.doctype, self.supplier, self.company, self.inter_company_invoice_reference) def validate_cash(self): if not self.cash_bank_account and flt(self.paid_amount): @@ -120,21 +122,6 @@ class PurchaseInvoice(BuyingController): self.party_account_currency = account.account_currency - def validate_inter_company_supplier(self): - if frappe.db.get_value("Supplier", self.supplier, "is_internal_supplier") == 1: - if self.inter_company_invoice_reference: - doc = frappe.get_doc("Sales Invoice", self.inter_company_invoice_reference) - if not frappe.db.get_value("Supplier", {"represents_company": doc.company}, "name") == self.supplier: - frappe.throw(_("Invalid Supplier for Inter Company Invoice")) - if not frappe.db.get_value("Customer", {"name": doc.customer}, "represents_company") == self.company: - frappe.throw(_("Invalid Company for Inter Company Invoice")) - else: - companies = frappe.db.sql("""select company from `tabAllowed To Transact With` - where parenttype = "Supplier" and parent = '{0}'""".format(self.supplier), as_list = 1) - companies = [company[0] for company in companies] - if not self.company in companies: - frappe.throw(_("Supplier not allowed to transact with {0}. Please change the Company.").format(self.company)) - def check_for_closed_status(self): check_list = [] @@ -319,12 +306,7 @@ class PurchaseInvoice(BuyingController): self.update_project() self.update_fixed_asset() - self.update_linked_invoice() - - def update_linked_invoice(self): - if self.inter_company_invoice_reference: - frappe.db.set_value("Sales Invoice", self.inter_company_invoice_reference,\ - "inter_company_invoice_reference", self.name) + update_linked_invoice(self.doctype, self.name, self.inter_company_invoice_reference) def update_fixed_asset(self): for d in self.get("items"): @@ -657,7 +639,7 @@ class PurchaseInvoice(BuyingController): self.update_fixed_asset() frappe.db.set(self, 'status', 'Cancelled') - self.unlink_inter_company_invoice() + unlink_inter_company_invoice(self.doctype, self.name, self.inter_company_invoice_reference) def update_project(self): project_list = [] @@ -696,13 +678,6 @@ class PurchaseInvoice(BuyingController): pi = pi[0][0] frappe.throw(_("Supplier Invoice No exists in Purchase Invoice {0}".format(pi))) - def unlink_inter_company_invoice(self): - if self.inter_company_invoice_reference: - frappe.db.set_value("Purchase Invoice", self.name,\ - "inter_company_invoice_reference", "") - frappe.db.set_value("Sales Invoice", self.inter_company_invoice_reference,\ - "inter_company_invoice_reference", "") - def update_billing_status_in_pr(self, update_modified=True): updated_pr = [] for d in self.get("items"): diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js index 8e48cc3a89d..77babf41f5d 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js @@ -106,9 +106,10 @@ erpnext.accounts.SalesInvoiceController = erpnext.selling.SellingController.exte frappe.model.with_doc("Customer", me.frm.doc.customer, function() { var customer = frappe.model.get_doc("Customer", me.frm.doc.customer); var internal = customer.is_internal_customer; - if (internal == 1) { + var disabled = customer.disabled; + if (internal == 1 && disabled == 0) { me.frm.add_custom_button("Inter Company Invoice", function() { - me.validate_inter_company_invoice(me.frm); + me.make_inter_company_invoice(); }, __("Make")); } }); @@ -239,24 +240,11 @@ erpnext.accounts.SalesInvoiceController = erpnext.selling.SellingController.exte me.apply_pricing_rule(); }) }, - - validate_inter_company_invoice: function(frm) { - var me = this; - frappe.call({ - method: "erpnext.accounts.doctype.sales_invoice.sales_invoice.validate_inter_company_invoice", - args: {"doc": frm.doc}, - callback: function(r) { - if (r && r.message) { - me.make_inter_company_invoice(frm); - } - } - }); - }, - make_inter_company_invoice: function(frm) { + make_inter_company_invoice: function() { frappe.model.open_mapped_doc({ method: "erpnext.accounts.doctype.sales_invoice.sales_invoice.make_inter_company_purchase_invoice", - frm: frm + frm: me.frm }); }, diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 7a8ee2434f5..115cf141198 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -73,7 +73,7 @@ class SalesInvoice(SellingController): self.validate_account_for_change_amount() self.validate_fixed_asset() self.set_income_account_for_fixed_assets() - self.validate_inter_company_customer() + validate_inter_company_party(self.doctype, self.customer, self.company, self.inter_company_invoice_reference) if cint(self.is_pos): self.validate_pos() @@ -147,32 +147,12 @@ class SalesInvoice(SellingController): update_company_current_month_sales(self.company) self.update_project() - self.update_linked_invoice() + update_linked_invoice(self.doctype, self.name, self.inter_company_invoice_reference) def validate_pos_paid_amount(self): if len(self.payments) == 0 and self.is_pos: frappe.throw(_("At least one mode of payment is required for POS invoice.")) - def validate_inter_company_customer(self): - if frappe.db.get_value("Customer", self.customer, "is_internal_customer") == 1: - if self.inter_company_invoice_reference: - doc = frappe.get_doc("Purchase Invoice", self.inter_company_invoice_reference) - if not frappe.db.get_value("Customer", {"represents_company": doc.company}, "name") == self.customer: - frappe.throw(_("Invalid Customer for Inter Company Invoice")) - if not frappe.db.get_value("Supplier", {"name": doc.supplier}, "represents_company") == self.company: - frappe.throw(_("Invalid Company for Inter Company Invoice")) - else: - companies = frappe.db.sql("""select company from `tabAllowed To Transact With` - where parenttype = "Customer" and parent = '{0}'""".format(self.customer), as_list = 1) - companies = [company[0] for company in companies] - if not self.company in companies: - frappe.throw(_("Customer not allowed to transact with {0}. Please change the Company.").format(self.company)) - - def update_linked_invoice(self): - if self.inter_company_invoice_reference: - frappe.db.set_value("Purchase Invoice", self.inter_company_invoice_reference,\ - "inter_company_invoice_reference", self.name) - def before_cancel(self): self.update_time_sheet(None) @@ -208,7 +188,7 @@ class SalesInvoice(SellingController): update_company_current_month_sales(self.company) self.update_project() - self.unlink_inter_company_invoice() + unlink_inter_company_invoice(self.doctype, self.name, self.inter_company_invoice_reference) def update_status_updater_args(self): if cint(self.update_stock): @@ -931,18 +911,48 @@ class SalesInvoice(SellingController): project.update_billed_amount() project.save() - def unlink_inter_company_invoice(self): - if self.inter_company_invoice_reference: - frappe.db.set_value("Sales Invoice", self.name,\ - "inter_company_invoice_reference", "") - frappe.db.set_value("Purchase Invoice", self.inter_company_invoice_reference,\ - "inter_company_invoice_reference", "") def verify_payment_amount_is_positive(self): for entry in self.payments: if entry.amount < 0: frappe.throw(_("Row #{0} (Payment Table): Amount must be positive").format(entry.idx)) +def validate_inter_company_party(doctype, party, company, inter_company_invoice_reference): + if doctype == "Sales Invoice": + partytype, ref_partytype, internal = "Customer", "Supplier", "is_internal_customer" + ref_doc = "Purchase Invoice" + else: + partytype, ref_partytype, internal = "Supplier", "Customer", "is_internal_supplier" + ref_doc = "Sales Invoice" + + if inter_company_invoice_reference: + doc = frappe.get_doc(ref_doc, inter_company_invoice_reference) + ref_party = doc.supplier if doctype == "Sales Invoice" else doc.customer + if not frappe.db.get_value(partytype, {"represents_company": doc.company}, "name") == party: + frappe.throw(_("Invalid {0} for Inter Company Invoice.").format(partytype)) + if not frappe.db.get_value(ref_partytype, {"name": ref_party}, "represents_company") == company: + frappe.throw(_("Invalid Company for Inter Company Invoice.")) + + elif frappe.db.get_value(partytype, {"name": party, internal: 1}, "name") == party: + companies = frappe.db.sql("""select company from `tabAllowed To Transact With` + where parenttype = '{0}' and parent = '{1}'""".format(partytype, party), as_list = 1) + companies = [d[0] for d in companies] + if not company in companies: + frappe.throw(_("{0} not allowed to transact with {1}. Please change the Company.").format(partytype, company)) + +def update_linked_invoice(doctype, name, inter_company_invoice_reference): + if inter_company_invoice_reference: + frappe.db.set_value(doctype, inter_company_invoice_reference,\ + "inter_company_invoice_reference", name) + +def unlink_inter_company_invoice(doctype, name, inter_company_invoice_reference): + ref_doc = "Purchase Invoice" if doctype == "Sales Invoice" else "Sales Invoice" + if inter_company_invoice_reference: + frappe.db.set_value(doctype, name,\ + "inter_company_invoice_reference", "") + frappe.db.set_value(ref_doc, inter_company_invoice_reference,\ + "inter_company_invoice_reference", "") + def get_list_context(context=None): from erpnext.controllers.website_list_for_contact import get_list_context list_context = get_list_context(context) @@ -1026,58 +1036,44 @@ def set_account_for_mode_of_payment(self): def get_inter_company_details(doc, doctype): if doctype == "Sales Invoice": - party = frappe.db.get_value("Supplier", {"is_internal_supplier":1, "represents_company": doc.get("company")}, "name") - company = frappe.db.get_value("Customer", {"name": doc.get("customer")}, "represents_company") + party = frappe.db.get_value("Supplier", {"disabled": 0, "is_internal_supplier": 1, "represents_company": doc.company}, "name") + company = frappe.db.get_value("Customer", {"name": doc.customer}, "represents_company") else: - party = frappe.db.get_value("Customer", {"is_internal_customer":1, "represents_company": doc.get("company")}, "name") - company = frappe.db.get_value("Supplier", {"name": doc.get("supplier")}, "represents_company") + party = frappe.db.get_value("Customer", {"disabled": 0, "is_internal_customer": 1, "represents_company": doc.company}, "name") + company = frappe.db.get_value("Supplier", {"name": doc.supplier}, "represents_company") return { "party": party, "company": company } -@frappe.whitelist() -def validate_inter_company_invoice(doc): - doc = json.loads(doc) - doctype = doc.get("doctype") + +def validate_inter_company_invoice(doc, doctype): + details = get_inter_company_details(doc, doctype) - if doctype == "Sales Invoice": - buying = frappe.db.get_value("Price List", doc.get("selling_price_list"), "buying") - if not buying: - frappe.throw(_("Buying and Selling Price List should be same for Inter Company Transactions.")) + price_list = doc.selling_price_list if doctype == "Sales Invoice" else doc.buying_price_list + valid_price_list = frappe.db.get_value("Price List", {"name": price_list, "buying": 1, "selling": 1}) + if not valid_price_list: + frappe.throw(_("Selected Price List should have buying and selling fields checked.")) - supplier = details.get("party") - if not supplier: - frappe.throw(_("No Supplier found for Inter Company Transactions.")) + party = details.get("party") + if not party: + partytype = "Supplier" if doctype == "Sales Invoice" else "Customer" + frappe.throw(_("No {0} found for Inter Company Transactions.").format(partytype)) - company = details.get("company") - default_currency = frappe.db.get_value("Company", company, "default_currency") - if default_currency != doc.get("currency"): - frappe.throw(_("Company currencies of both the companies should match for Inter Company Transactions.")) - - else: - selling = frappe.db.get_value("Price List", doc.get("buying_price_list"), "selling") - if not selling: - frappe.throw(_("Buying and Selling Price List should be same for Inter Company Transactions.")) + company = details.get("company") + default_currency = frappe.db.get_value("Company", company, "default_currency") + if default_currency != doc.currency: + frappe.throw(_("Company currencies of both the companies should match for Inter Company Transactions.")) - customer = details.get("party") - if not customer: - frappe.throw(_("No Customer found for Inter Company Transactions.")) - - company = details.get("company") - default_currency = frappe.db.get_value("Company", company, "default_currency") - - if default_currency != doc.get("currency"): - frappe.throw(_("Company currencies of both the companies should match for Inter Company Transactions.")) - - return company + return @frappe.whitelist() def make_inter_company_purchase_invoice(source_name, target_doc=None): return make_inter_company_invoice("Sales Invoice", source_name, target_doc) def make_inter_company_invoice(doctype, source_name, target_doc=None): + from frappe.model.mapper import get_mapped_doc if doctype == "Sales Invoice": source_doc = frappe.get_doc("Sales Invoice", source_name) @@ -1086,6 +1082,7 @@ def make_inter_company_invoice(doctype, source_name, target_doc=None): source_doc = frappe.get_doc("Purchase Invoice", source_name) target_doctype = "Sales Invoice" + validate_inter_company_invoice(source_doc, doctype) details = get_inter_company_details(source_doc, doctype) def set_missing_values(source, target): @@ -1106,6 +1103,7 @@ def make_inter_company_invoice(doctype, source_name, target_doc=None): target_doc.income_account = "" target_doc.expense_account = "" target_doc.cost_center = "" + target_doc.warehouse = "" doclist = get_mapped_doc(doctype, source_name, { doctype: {