feat: refactor invoice_discounting (#18629)
* feat: refactor invoice_discountig * Update invoice_discounting.py * Update invoice_discounting.js
This commit is contained in:
parent
88ee7d8dbe
commit
db88476f8b
@ -8,7 +8,8 @@
|
|||||||
"customer",
|
"customer",
|
||||||
"column_break_3",
|
"column_break_3",
|
||||||
"posting_date",
|
"posting_date",
|
||||||
"outstanding_amount"
|
"outstanding_amount",
|
||||||
|
"debit_to"
|
||||||
],
|
],
|
||||||
"fields": [
|
"fields": [
|
||||||
{
|
{
|
||||||
@ -48,10 +49,18 @@
|
|||||||
{
|
{
|
||||||
"fieldname": "column_break_3",
|
"fieldname": "column_break_3",
|
||||||
"fieldtype": "Column Break"
|
"fieldtype": "Column Break"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fetch_from": "sales_invoice.debit_to",
|
||||||
|
"fieldname": "debit_to",
|
||||||
|
"fieldtype": "Link",
|
||||||
|
"label": "Debit to",
|
||||||
|
"options": "Account",
|
||||||
|
"read_only": 1
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"istable": 1,
|
"istable": 1,
|
||||||
"modified": "2019-05-30 19:27:29.436153",
|
"modified": "2019-08-07 15:13:55.808349",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"module": "Accounts",
|
"module": "Accounts",
|
||||||
"name": "Discounted Invoice",
|
"name": "Discounted Invoice",
|
||||||
|
|||||||
@ -13,41 +13,57 @@ frappe.ui.form.on('Invoice Discounting', {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
frm.events.filter_accounts("bank_account", frm, {"account_type": "Bank"});
|
|
||||||
frm.events.filter_accounts("bank_charges_account", frm, {"root_type": "Expense"});
|
frm.events.filter_accounts("bank_account", frm, [["account_type", "=", "Bank"]]);
|
||||||
frm.events.filter_accounts("short_term_loan", frm, {"root_type": "Liability"});
|
frm.events.filter_accounts("bank_charges_account", frm, [["root_type", "=", "Expense"]]);
|
||||||
frm.events.filter_accounts("accounts_receivable_credit", frm, {"account_type": "Receivable"});
|
frm.events.filter_accounts("short_term_loan", frm, [["root_type", "=", "Liability"]]);
|
||||||
frm.events.filter_accounts("accounts_receivable_discounted", frm, {"account_type": "Receivable"});
|
frm.events.filter_accounts("accounts_receivable_discounted", frm, [["account_type", "=", "Receivable"]]);
|
||||||
frm.events.filter_accounts("accounts_receivable_unpaid", frm, {"account_type": "Receivable"});
|
frm.events.filter_accounts("accounts_receivable_credit", frm, [["account_type", "=", "Receivable"]]);
|
||||||
|
frm.events.filter_accounts("accounts_receivable_unpaid", frm, [["account_type", "=", "Receivable"]]);
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
filter_accounts: (fieldname, frm, addl_filters) => {
|
filter_accounts: (fieldname, frm, addl_filters) => {
|
||||||
let filters = {
|
let filters = [
|
||||||
"company": frm.doc.company,
|
["company", "=", frm.doc.company],
|
||||||
"is_group": 0
|
["is_group", "=", 0]
|
||||||
};
|
];
|
||||||
if(addl_filters) Object.assign(filters, addl_filters);
|
if(addl_filters){
|
||||||
|
filters = $.merge(filters , addl_filters);
|
||||||
|
}
|
||||||
|
|
||||||
frm.set_query(fieldname, () => { return { "filters": filters }; });
|
frm.set_query(fieldname, () => { return { "filters": filters }; });
|
||||||
},
|
},
|
||||||
|
|
||||||
|
refresh_filters: (frm) =>{
|
||||||
|
let invoice_accounts = Object.keys(frm.doc.invoices).map(function(key) {
|
||||||
|
return frm.doc.invoices[key].debit_to;
|
||||||
|
});
|
||||||
|
let filters = [
|
||||||
|
["account_type", "=", "Receivable"],
|
||||||
|
["name", "not in", invoice_accounts]
|
||||||
|
];
|
||||||
|
frm.events.filter_accounts("accounts_receivable_credit", frm, filters);
|
||||||
|
frm.events.filter_accounts("accounts_receivable_discounted", frm, filters);
|
||||||
|
frm.events.filter_accounts("accounts_receivable_unpaid", frm, filters);
|
||||||
|
},
|
||||||
|
|
||||||
refresh: (frm) => {
|
refresh: (frm) => {
|
||||||
frm.events.show_general_ledger(frm);
|
frm.events.show_general_ledger(frm);
|
||||||
|
|
||||||
if(frm.doc.docstatus === 0) {
|
if (frm.doc.docstatus === 0) {
|
||||||
frm.add_custom_button(__('Get Invoices'), function() {
|
frm.add_custom_button(__('Get Invoices'), function() {
|
||||||
frm.events.get_invoices(frm);
|
frm.events.get_invoices(frm);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if(frm.doc.docstatus === 1 && frm.doc.status !== "Settled") {
|
if (frm.doc.docstatus === 1 && frm.doc.status !== "Settled") {
|
||||||
if(frm.doc.status == "Sanctioned") {
|
if (frm.doc.status == "Sanctioned") {
|
||||||
frm.add_custom_button(__('Disburse Loan'), function() {
|
frm.add_custom_button(__('Disburse Loan'), function() {
|
||||||
frm.events.create_disbursement_entry(frm);
|
frm.events.create_disbursement_entry(frm);
|
||||||
}).addClass("btn-primary");
|
}).addClass("btn-primary");
|
||||||
}
|
}
|
||||||
if(frm.doc.status == "Disbursed") {
|
if (frm.doc.status == "Disbursed") {
|
||||||
frm.add_custom_button(__('Close Loan'), function() {
|
frm.add_custom_button(__('Close Loan'), function() {
|
||||||
frm.events.close_loan(frm);
|
frm.events.close_loan(frm);
|
||||||
}).addClass("btn-primary");
|
}).addClass("btn-primary");
|
||||||
@ -64,7 +80,7 @@ frappe.ui.form.on('Invoice Discounting', {
|
|||||||
},
|
},
|
||||||
|
|
||||||
set_end_date: (frm) => {
|
set_end_date: (frm) => {
|
||||||
if(frm.doc.loan_start_date && frm.doc.loan_period) {
|
if (frm.doc.loan_start_date && frm.doc.loan_period) {
|
||||||
let end_date = frappe.datetime.add_days(frm.doc.loan_start_date, frm.doc.loan_period);
|
let end_date = frappe.datetime.add_days(frm.doc.loan_start_date, frm.doc.loan_period);
|
||||||
frm.set_value("loan_end_date", end_date);
|
frm.set_value("loan_end_date", end_date);
|
||||||
}
|
}
|
||||||
@ -132,6 +148,7 @@ frappe.ui.form.on('Invoice Discounting', {
|
|||||||
frm.doc.invoices = frm.doc.invoices.filter(row => row.sales_invoice);
|
frm.doc.invoices = frm.doc.invoices.filter(row => row.sales_invoice);
|
||||||
let row = frm.add_child("invoices");
|
let row = frm.add_child("invoices");
|
||||||
$.extend(row, v);
|
$.extend(row, v);
|
||||||
|
frm.events.refresh_filters(frm);
|
||||||
});
|
});
|
||||||
refresh_field("invoices");
|
refresh_field("invoices");
|
||||||
}
|
}
|
||||||
@ -190,8 +207,10 @@ frappe.ui.form.on('Invoice Discounting', {
|
|||||||
frappe.ui.form.on('Discounted Invoice', {
|
frappe.ui.form.on('Discounted Invoice', {
|
||||||
sales_invoice: (frm) => {
|
sales_invoice: (frm) => {
|
||||||
frm.events.calculate_total_amount(frm);
|
frm.events.calculate_total_amount(frm);
|
||||||
|
frm.events.refresh_filters(frm);
|
||||||
},
|
},
|
||||||
invoices_remove: (frm) => {
|
invoices_remove: (frm) => {
|
||||||
frm.events.calculate_total_amount(frm);
|
frm.events.calculate_total_amount(frm);
|
||||||
|
frm.events.refresh_filters(frm);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -12,6 +12,7 @@ from erpnext.accounts.general_ledger import make_gl_entries
|
|||||||
class InvoiceDiscounting(AccountsController):
|
class InvoiceDiscounting(AccountsController):
|
||||||
def validate(self):
|
def validate(self):
|
||||||
self.validate_mandatory()
|
self.validate_mandatory()
|
||||||
|
self.validate_invoices()
|
||||||
self.calculate_total_amount()
|
self.calculate_total_amount()
|
||||||
self.set_status()
|
self.set_status()
|
||||||
self.set_end_date()
|
self.set_end_date()
|
||||||
@ -24,6 +25,15 @@ class InvoiceDiscounting(AccountsController):
|
|||||||
if self.docstatus == 1 and not (self.loan_start_date and self.loan_period):
|
if self.docstatus == 1 and not (self.loan_start_date and self.loan_period):
|
||||||
frappe.throw(_("Loan Start Date and Loan Period are mandatory to save the Invoice Discounting"))
|
frappe.throw(_("Loan Start Date and Loan Period are mandatory to save the Invoice Discounting"))
|
||||||
|
|
||||||
|
def validate_invoices(self):
|
||||||
|
discounted_invoices = [record.sales_invoice for record in
|
||||||
|
frappe.get_all("Discounted Invoice",fields = ["sales_invoice"], filters= {"docstatus":1})]
|
||||||
|
|
||||||
|
for record in self.invoices:
|
||||||
|
if record.sales_invoice in discounted_invoices:
|
||||||
|
frappe.throw("Row({0}): {1} is already discounted in {2}"
|
||||||
|
.format(record.idx, frappe.bold(record.sales_invoice), frappe.bold(record.parent)))
|
||||||
|
|
||||||
def calculate_total_amount(self):
|
def calculate_total_amount(self):
|
||||||
self.total_amount = sum([flt(d.outstanding_amount) for d in self.invoices])
|
self.total_amount = sum([flt(d.outstanding_amount) for d in self.invoices])
|
||||||
|
|
||||||
@ -212,7 +222,8 @@ def get_invoices(filters):
|
|||||||
name as sales_invoice,
|
name as sales_invoice,
|
||||||
customer,
|
customer,
|
||||||
posting_date,
|
posting_date,
|
||||||
outstanding_amount
|
outstanding_amount,
|
||||||
|
debit_to
|
||||||
from `tabSales Invoice` si
|
from `tabSales Invoice` si
|
||||||
where
|
where
|
||||||
docstatus = 1
|
docstatus = 1
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user