changes as per the reviews

This commit is contained in:
Shreya 2018-04-23 16:21:59 +05:30
parent b47302dd52
commit ae0b31c31d
4 changed files with 75 additions and 125 deletions

View File

@ -99,9 +99,10 @@ erpnext.accounts.PurchaseInvoice = erpnext.buying.BuyingController.extend({
frappe.model.with_doc("Supplier", me.frm.doc.supplier, function() { frappe.model.with_doc("Supplier", me.frm.doc.supplier, function() {
var supplier = frappe.model.get_doc("Supplier", me.frm.doc.supplier); var supplier = frappe.model.get_doc("Supplier", me.frm.doc.supplier);
var internal = supplier.is_internal_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.frm.add_custom_button("Inter Company Invoice", function() {
me.validate_inter_company_invoice(me.frm); me.make_inter_company_invoice(me.frm);
}, __("Make")); }, __("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) { make_inter_company_invoice: function(frm) {
frappe.model.open_mapped_doc({ frappe.model.open_mapped_doc({

View File

@ -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.buying.utils import check_for_closed_status
from erpnext.accounts.general_ledger import get_round_off_account_and_cost_center from erpnext.accounts.general_ledger import get_round_off_account_and_cost_center
from frappe.model.mapper import get_mapped_doc 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 = { form_grid_templates = {
"items": "templates/form_grid/item_grid.html" "items": "templates/form_grid/item_grid.html"
@ -73,7 +75,7 @@ class PurchaseInvoice(BuyingController):
self.validate_fixed_asset_account() self.validate_fixed_asset_account()
self.create_remarks() self.create_remarks()
self.set_status() 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): def validate_cash(self):
if not self.cash_bank_account and flt(self.paid_amount): 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 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): def check_for_closed_status(self):
check_list = [] check_list = []
@ -319,12 +306,7 @@ class PurchaseInvoice(BuyingController):
self.update_project() self.update_project()
self.update_fixed_asset() self.update_fixed_asset()
self.update_linked_invoice() update_linked_invoice(self.doctype, self.name, self.inter_company_invoice_reference)
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)
def update_fixed_asset(self): def update_fixed_asset(self):
for d in self.get("items"): for d in self.get("items"):
@ -657,7 +639,7 @@ class PurchaseInvoice(BuyingController):
self.update_fixed_asset() self.update_fixed_asset()
frappe.db.set(self, 'status', 'Cancelled') 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): def update_project(self):
project_list = [] project_list = []
@ -696,13 +678,6 @@ class PurchaseInvoice(BuyingController):
pi = pi[0][0] pi = pi[0][0]
frappe.throw(_("Supplier Invoice No exists in Purchase Invoice {0}".format(pi))) 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): def update_billing_status_in_pr(self, update_modified=True):
updated_pr = [] updated_pr = []
for d in self.get("items"): for d in self.get("items"):

View File

@ -106,9 +106,10 @@ erpnext.accounts.SalesInvoiceController = erpnext.selling.SellingController.exte
frappe.model.with_doc("Customer", me.frm.doc.customer, function() { frappe.model.with_doc("Customer", me.frm.doc.customer, function() {
var customer = frappe.model.get_doc("Customer", me.frm.doc.customer); var customer = frappe.model.get_doc("Customer", me.frm.doc.customer);
var internal = customer.is_internal_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.frm.add_custom_button("Inter Company Invoice", function() {
me.validate_inter_company_invoice(me.frm); me.make_inter_company_invoice();
}, __("Make")); }, __("Make"));
} }
}); });
@ -239,24 +240,11 @@ erpnext.accounts.SalesInvoiceController = erpnext.selling.SellingController.exte
me.apply_pricing_rule(); 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({ frappe.model.open_mapped_doc({
method: "erpnext.accounts.doctype.sales_invoice.sales_invoice.make_inter_company_purchase_invoice", method: "erpnext.accounts.doctype.sales_invoice.sales_invoice.make_inter_company_purchase_invoice",
frm: frm frm: me.frm
}); });
}, },

View File

@ -73,7 +73,7 @@ class SalesInvoice(SellingController):
self.validate_account_for_change_amount() self.validate_account_for_change_amount()
self.validate_fixed_asset() self.validate_fixed_asset()
self.set_income_account_for_fixed_assets() 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): if cint(self.is_pos):
self.validate_pos() self.validate_pos()
@ -147,32 +147,12 @@ class SalesInvoice(SellingController):
update_company_current_month_sales(self.company) update_company_current_month_sales(self.company)
self.update_project() 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): def validate_pos_paid_amount(self):
if len(self.payments) == 0 and self.is_pos: if len(self.payments) == 0 and self.is_pos:
frappe.throw(_("At least one mode of payment is required for POS invoice.")) 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): def before_cancel(self):
self.update_time_sheet(None) self.update_time_sheet(None)
@ -208,7 +188,7 @@ class SalesInvoice(SellingController):
update_company_current_month_sales(self.company) update_company_current_month_sales(self.company)
self.update_project() 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): def update_status_updater_args(self):
if cint(self.update_stock): if cint(self.update_stock):
@ -931,18 +911,48 @@ class SalesInvoice(SellingController):
project.update_billed_amount() project.update_billed_amount()
project.save() 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): def verify_payment_amount_is_positive(self):
for entry in self.payments: for entry in self.payments:
if entry.amount < 0: if entry.amount < 0:
frappe.throw(_("Row #{0} (Payment Table): Amount must be positive").format(entry.idx)) 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): def get_list_context(context=None):
from erpnext.controllers.website_list_for_contact import get_list_context from erpnext.controllers.website_list_for_contact import get_list_context
list_context = get_list_context(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): def get_inter_company_details(doc, doctype):
if doctype == "Sales Invoice": if doctype == "Sales Invoice":
party = frappe.db.get_value("Supplier", {"is_internal_supplier":1, "represents_company": doc.get("company")}, "name") 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.get("customer")}, "represents_company") company = frappe.db.get_value("Customer", {"name": doc.customer}, "represents_company")
else: else:
party = frappe.db.get_value("Customer", {"is_internal_customer":1, "represents_company": doc.get("company")}, "name") 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.get("supplier")}, "represents_company") company = frappe.db.get_value("Supplier", {"name": doc.supplier}, "represents_company")
return { return {
"party": party, "party": party,
"company": company "company": company
} }
@frappe.whitelist()
def validate_inter_company_invoice(doc): def validate_inter_company_invoice(doc, doctype):
doc = json.loads(doc)
doctype = doc.get("doctype")
details = get_inter_company_details(doc, doctype) details = get_inter_company_details(doc, doctype)
if doctype == "Sales Invoice": price_list = doc.selling_price_list if doctype == "Sales Invoice" else doc.buying_price_list
buying = frappe.db.get_value("Price List", doc.get("selling_price_list"), "buying") valid_price_list = frappe.db.get_value("Price List", {"name": price_list, "buying": 1, "selling": 1})
if not buying: if not valid_price_list:
frappe.throw(_("Buying and Selling Price List should be same for Inter Company Transactions.")) frappe.throw(_("Selected Price List should have buying and selling fields checked."))
supplier = details.get("party") party = details.get("party")
if not supplier: if not party:
frappe.throw(_("No Supplier found for Inter Company Transactions.")) partytype = "Supplier" if doctype == "Sales Invoice" else "Customer"
frappe.throw(_("No {0} found for Inter Company Transactions.").format(partytype))
company = details.get("company") company = details.get("company")
default_currency = frappe.db.get_value("Company", company, "default_currency") default_currency = frappe.db.get_value("Company", company, "default_currency")
if default_currency != doc.get("currency"): if default_currency != doc.currency:
frappe.throw(_("Company currencies of both the companies should match for Inter Company Transactions.")) 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."))
customer = details.get("party") return
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
@frappe.whitelist() @frappe.whitelist()
def make_inter_company_purchase_invoice(source_name, target_doc=None): def make_inter_company_purchase_invoice(source_name, target_doc=None):
return make_inter_company_invoice("Sales Invoice", source_name, target_doc) return make_inter_company_invoice("Sales Invoice", source_name, target_doc)
def make_inter_company_invoice(doctype, source_name, target_doc=None): def make_inter_company_invoice(doctype, source_name, target_doc=None):
from frappe.model.mapper import get_mapped_doc from frappe.model.mapper import get_mapped_doc
if doctype == "Sales Invoice": if doctype == "Sales Invoice":
source_doc = frappe.get_doc("Sales Invoice", source_name) 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) source_doc = frappe.get_doc("Purchase Invoice", source_name)
target_doctype = "Sales Invoice" target_doctype = "Sales Invoice"
validate_inter_company_invoice(source_doc, doctype)
details = get_inter_company_details(source_doc, doctype) details = get_inter_company_details(source_doc, doctype)
def set_missing_values(source, target): 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.income_account = ""
target_doc.expense_account = "" target_doc.expense_account = ""
target_doc.cost_center = "" target_doc.cost_center = ""
target_doc.warehouse = ""
doclist = get_mapped_doc(doctype, source_name, { doclist = get_mapped_doc(doctype, source_name, {
doctype: { doctype: {