2015-03-03 09:25:30 +00:00
|
|
|
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2015-02-22 14:44:49 +00:00
|
|
|
// License: GNU General Public License v3. See license.txt
|
|
|
|
|
2013-10-10 11:05:09 +00:00
|
|
|
// get tax rate
|
2015-02-25 09:38:42 +00:00
|
|
|
frappe.provide("erpnext.taxes");
|
|
|
|
frappe.provide("erpnext.taxes.flags");
|
|
|
|
|
2015-05-03 16:50:44 +00:00
|
|
|
frappe.ui.form.on(cur_frm.doctype, {
|
2016-06-15 11:15:03 +00:00
|
|
|
setup: function(frm) {
|
|
|
|
// set conditional display for rate column in taxes
|
|
|
|
$(frm.wrapper).on('grid-row-render', function(e, grid_row) {
|
|
|
|
if(in_list(['Sales Taxes and Charges', 'Purchase Taxes and Charges'], grid_row.doc.doctype)) {
|
|
|
|
erpnext.taxes.set_conditional_mandatory_rate_or_amount(grid_row);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2015-05-03 16:50:44 +00:00
|
|
|
onload: function(frm) {
|
|
|
|
if(frm.get_field("taxes")) {
|
|
|
|
frm.set_query("account_head", "taxes", function(doc) {
|
|
|
|
if(frm.cscript.tax_table == "Sales Taxes and Charges") {
|
|
|
|
var account_type = ["Tax", "Chargeable", "Expense Account"];
|
|
|
|
} else {
|
2017-11-27 06:18:09 +00:00
|
|
|
var account_type = ["Tax", "Chargeable", "Income Account", "Expenses Included In Valuation"];
|
2015-05-03 16:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
query: "erpnext.controllers.queries.tax_account_query",
|
|
|
|
filters: {
|
|
|
|
"account_type": account_type,
|
2022-05-11 07:31:06 +00:00
|
|
|
"company": doc.company,
|
|
|
|
"disabled": 0
|
2015-05-03 16:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-08-10 11:56:35 +00:00
|
|
|
frm.set_query("cost_center", "taxes", function(doc) {
|
|
|
|
return {
|
|
|
|
filters: {
|
|
|
|
"company": doc.company,
|
|
|
|
"is_group": 0
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
2015-05-03 16:50:44 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
validate: function(frm) {
|
|
|
|
// neither is absolutely mandatory
|
2015-05-05 11:56:35 +00:00
|
|
|
if(frm.get_docfield("taxes")) {
|
|
|
|
frm.get_docfield("taxes", "rate").reqd = 0;
|
|
|
|
frm.get_docfield("taxes", "tax_amount").reqd = 0;
|
|
|
|
}
|
2016-06-15 11:15:03 +00:00
|
|
|
|
2015-05-03 16:50:44 +00:00
|
|
|
},
|
|
|
|
taxes_on_form_rendered: function(frm) {
|
2016-06-15 11:15:03 +00:00
|
|
|
erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm.open_grid_row());
|
2018-08-01 12:37:39 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
allocate_advances_automatically: function(frm) {
|
|
|
|
if(frm.doc.allocate_advances_automatically) {
|
|
|
|
frappe.call({
|
|
|
|
doc: frm.doc,
|
|
|
|
method: "set_advances",
|
|
|
|
callback: function(r, rt) {
|
|
|
|
refresh_field("advances");
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-11-18 10:25:32 +00:00
|
|
|
}
|
2015-05-03 16:50:44 +00:00
|
|
|
});
|
|
|
|
|
2016-08-08 17:24:38 +00:00
|
|
|
frappe.ui.form.on('Sales Invoice Payment', {
|
|
|
|
mode_of_payment: function(frm, cdt, cdn) {
|
|
|
|
var d = locals[cdt][cdn];
|
|
|
|
get_payment_mode_account(frm, d.mode_of_payment, function(account){
|
|
|
|
frappe.model.set_value(cdt, cdn, 'account', account)
|
|
|
|
})
|
|
|
|
}
|
2017-09-06 11:36:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
frappe.ui.form.on("Sales Invoice", {
|
|
|
|
payment_terms_template: function() {
|
|
|
|
cur_frm.trigger("disable_due_date");
|
|
|
|
}
|
|
|
|
});
|
2016-08-08 17:24:38 +00:00
|
|
|
|
|
|
|
frappe.ui.form.on('Purchase Invoice', {
|
|
|
|
mode_of_payment: function(frm) {
|
|
|
|
get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
|
|
|
|
frm.set_value('cash_bank_account', account);
|
|
|
|
})
|
2017-09-06 11:36:45 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
payment_terms_template: function() {
|
|
|
|
cur_frm.trigger("disable_due_date");
|
2016-08-08 17:24:38 +00:00
|
|
|
}
|
2017-09-06 11:36:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
frappe.ui.form.on("Payment Schedule", {
|
|
|
|
payment_schedule_remove: function() {
|
|
|
|
cur_frm.trigger("disable_due_date");
|
|
|
|
},
|
|
|
|
|
|
|
|
});
|
2016-08-08 17:24:38 +00:00
|
|
|
|
|
|
|
frappe.ui.form.on('Payment Entry', {
|
|
|
|
mode_of_payment: function(frm) {
|
|
|
|
get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
|
|
|
|
var payment_account_field = frm.doc.payment_type == "Receive" ? "paid_to" : "paid_from";
|
|
|
|
frm.set_value(payment_account_field, account);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-08-26 05:41:17 +00:00
|
|
|
frappe.ui.form.on('Salary Structure', {
|
|
|
|
mode_of_payment: function(frm) {
|
|
|
|
get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
|
|
|
|
frm.set_value("payment_account", account);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-07-13 06:14:29 +00:00
|
|
|
var get_payment_mode_account = function(frm, mode_of_payment, callback) {
|
|
|
|
if(!frm.doc.company) {
|
2020-09-24 06:21:03 +00:00
|
|
|
frappe.throw({message:__("Please select a Company first."), title: __("Mandatory")});
|
2017-07-13 06:14:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 12:01:36 +00:00
|
|
|
if(!mode_of_payment) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-08 17:24:38 +00:00
|
|
|
return frappe.call({
|
|
|
|
method: "erpnext.accounts.doctype.sales_invoice.sales_invoice.get_bank_cash_account",
|
|
|
|
args: {
|
|
|
|
"mode_of_payment": mode_of_payment,
|
|
|
|
"company": frm.doc.company
|
|
|
|
},
|
|
|
|
callback: function(r, rt) {
|
|
|
|
if(r.message) {
|
|
|
|
callback(r.message.account)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-10-10 11:05:09 +00:00
|
|
|
cur_frm.cscript.account_head = function(doc, cdt, cdn) {
|
|
|
|
var d = locals[cdt][cdn];
|
|
|
|
if(!d.charge_type && d.account_head){
|
2018-02-14 12:14:06 +00:00
|
|
|
frappe.msgprint(__("Please select Charge Type first"));
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.model.set_value(cdt, cdn, "account_head", "");
|
2020-11-11 04:22:27 +00:00
|
|
|
} else if (d.account_head) {
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.call({
|
2013-10-10 11:05:09 +00:00
|
|
|
type:"GET",
|
2015-02-22 14:44:49 +00:00
|
|
|
method: "erpnext.controllers.accounts_controller.get_tax_rate",
|
2013-10-10 11:05:09 +00:00
|
|
|
args: {"account_head":d.account_head},
|
|
|
|
callback: function(r) {
|
2020-11-11 04:22:27 +00:00
|
|
|
if (d.charge_type!=="Actual") {
|
2020-11-05 10:54:33 +00:00
|
|
|
frappe.model.set_value(cdt, cdn, "rate", r.message.tax_rate || 0);
|
|
|
|
}
|
2016-06-15 11:15:03 +00:00
|
|
|
frappe.model.set_value(cdt, cdn, "description", r.message.account_name);
|
2013-10-10 11:05:09 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2015-02-22 14:44:49 +00:00
|
|
|
|
2015-02-23 06:28:15 +00:00
|
|
|
cur_frm.cscript.validate_taxes_and_charges = function(cdt, cdn) {
|
2015-02-22 14:44:49 +00:00
|
|
|
var d = locals[cdt][cdn];
|
2015-02-23 06:28:15 +00:00
|
|
|
var msg = "";
|
2016-06-15 11:15:03 +00:00
|
|
|
|
2021-05-13 08:34:51 +00:00
|
|
|
if (d.account_head && !d.description) {
|
2016-06-15 11:15:03 +00:00
|
|
|
// set description from account head
|
|
|
|
d.description = d.account_head.split(' - ').slice(0, -1).join(' - ');
|
|
|
|
}
|
|
|
|
|
2021-05-13 08:34:51 +00:00
|
|
|
if (!d.charge_type && (d.row_id || d.rate || d.tax_amount)) {
|
2015-02-23 06:28:15 +00:00
|
|
|
msg = __("Please select Charge Type first");
|
2015-02-22 17:33:07 +00:00
|
|
|
d.row_id = "";
|
|
|
|
d.rate = d.tax_amount = 0.0;
|
2021-05-13 08:34:51 +00:00
|
|
|
} else if ((d.charge_type == 'Actual' || d.charge_type == 'On Net Total' || d.charge_type == 'On Paid Amount') && d.row_id) {
|
2015-02-23 06:28:15 +00:00
|
|
|
msg = __("Can refer row only if the charge type is 'On Previous Row Amount' or 'Previous Row Total'");
|
2015-02-22 14:44:49 +00:00
|
|
|
d.row_id = "";
|
2021-05-13 08:34:51 +00:00
|
|
|
} else if ((d.charge_type == 'On Previous Row Amount' || d.charge_type == 'On Previous Row Total') && d.row_id) {
|
2015-02-22 14:44:49 +00:00
|
|
|
if (d.idx == 1) {
|
2015-02-23 06:28:15 +00:00
|
|
|
msg = __("Cannot select charge type as 'On Previous Row Amount' or 'On Previous Row Total' for first row");
|
2015-02-22 14:44:49 +00:00
|
|
|
d.charge_type = '';
|
2015-02-23 06:28:15 +00:00
|
|
|
} else if (!d.row_id) {
|
|
|
|
msg = __("Please specify a valid Row ID for row {0} in table {1}", [d.idx, __(d.doctype)]);
|
|
|
|
d.row_id = "";
|
2021-05-13 08:34:51 +00:00
|
|
|
} else if (d.row_id && d.row_id >= d.idx) {
|
2015-02-23 06:28:15 +00:00
|
|
|
msg = __("Cannot refer row number greater than or equal to current row number for this Charge type");
|
2015-02-22 14:44:49 +00:00
|
|
|
d.row_id = "";
|
|
|
|
}
|
|
|
|
}
|
2021-05-13 08:34:51 +00:00
|
|
|
if (msg) {
|
2017-05-30 07:24:42 +00:00
|
|
|
frappe.validated = false;
|
2015-02-23 06:28:15 +00:00
|
|
|
refresh_field("taxes");
|
|
|
|
frappe.throw(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
cur_frm.cscript.validate_inclusive_tax = function(tax) {
|
|
|
|
var actual_type_error = function() {
|
|
|
|
var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
|
|
|
|
frappe.throw(msg);
|
|
|
|
};
|
|
|
|
|
|
|
|
var on_previous_row_error = function(row_range) {
|
|
|
|
var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
|
|
|
|
[tax.idx, __(tax.doctype), tax.charge_type, row_range])
|
|
|
|
frappe.throw(msg);
|
|
|
|
};
|
|
|
|
|
|
|
|
if(cint(tax.included_in_print_rate)) {
|
|
|
|
if(tax.charge_type == "Actual") {
|
|
|
|
// inclusive tax cannot be of type Actual
|
|
|
|
actual_type_error();
|
|
|
|
} else if(tax.charge_type == "On Previous Row Amount" &&
|
2017-05-30 07:24:42 +00:00
|
|
|
!cint(this.frm.doc["taxes"][tax.row_id - 1].included_in_print_rate)
|
|
|
|
) {
|
|
|
|
// referred row should also be an inclusive tax
|
|
|
|
on_previous_row_error(tax.row_id);
|
2015-02-23 06:28:15 +00:00
|
|
|
} else if(tax.charge_type == "On Previous Row Total") {
|
|
|
|
var taxes_not_included = $.map(this.frm.doc["taxes"].slice(0, tax.row_id),
|
|
|
|
function(t) { return cint(t.included_in_print_rate) ? null : t; });
|
|
|
|
if(taxes_not_included.length > 0) {
|
|
|
|
// all rows above this tax should be inclusive
|
|
|
|
on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
|
|
|
|
}
|
2015-02-24 10:35:19 +00:00
|
|
|
} else if(tax.category == "Valuation") {
|
|
|
|
frappe.throw(__("Valuation type charges can not marked as Inclusive"));
|
2015-02-23 06:28:15 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-22 14:44:49 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 09:38:42 +00:00
|
|
|
if(!erpnext.taxes.flags[cur_frm.cscript.tax_table]) {
|
|
|
|
erpnext.taxes.flags[cur_frm.cscript.tax_table] = true;
|
2015-02-22 14:44:49 +00:00
|
|
|
|
2015-02-25 09:38:42 +00:00
|
|
|
frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
|
|
|
|
cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
|
|
|
|
});
|
2015-02-22 14:44:49 +00:00
|
|
|
|
2015-02-25 09:38:42 +00:00
|
|
|
frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
|
|
|
|
cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
|
|
|
|
});
|
2015-02-22 14:44:49 +00:00
|
|
|
|
2015-02-25 09:38:42 +00:00
|
|
|
frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
|
|
|
|
cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
|
|
|
|
});
|
2015-02-22 14:44:49 +00:00
|
|
|
|
2015-02-25 09:38:42 +00:00
|
|
|
frappe.ui.form.on(cur_frm.cscript.tax_table, "charge_type", function(frm, cdt, cdn) {
|
2016-06-15 11:15:03 +00:00
|
|
|
frm.cscript.validate_taxes_and_charges(cdt, cdn);
|
|
|
|
var open_form = frm.open_grid_row();
|
|
|
|
if(open_form) {
|
|
|
|
erpnext.taxes.set_conditional_mandatory_rate_or_amount(open_form);
|
|
|
|
} else {
|
|
|
|
// apply in current row
|
2017-07-03 06:23:07 +00:00
|
|
|
erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm.get_field('taxes').grid.get_row(cdn));
|
2016-06-15 11:15:03 +00:00
|
|
|
}
|
2015-02-25 09:38:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
|
|
|
|
var tax = frappe.get_doc(cdt, cdn);
|
|
|
|
try {
|
|
|
|
cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
|
|
|
|
cur_frm.cscript.validate_inclusive_tax(tax);
|
|
|
|
} catch(e) {
|
|
|
|
tax.included_in_print_rate = 0;
|
|
|
|
refresh_field("included_in_print_rate", tax.name, tax.parentfield);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-15 11:15:03 +00:00
|
|
|
erpnext.taxes.set_conditional_mandatory_rate_or_amount = function(grid_row) {
|
|
|
|
if(grid_row) {
|
|
|
|
if(grid_row.doc.charge_type==="Actual") {
|
|
|
|
grid_row.toggle_editable("tax_amount", true);
|
|
|
|
grid_row.toggle_reqd("tax_amount", true);
|
|
|
|
grid_row.toggle_editable("rate", false);
|
|
|
|
grid_row.toggle_reqd("rate", false);
|
|
|
|
} else {
|
|
|
|
grid_row.toggle_editable("rate", true);
|
|
|
|
grid_row.toggle_reqd("rate", true);
|
|
|
|
grid_row.toggle_editable("tax_amount", false);
|
|
|
|
grid_row.toggle_reqd("tax_amount", false);
|
|
|
|
}
|
2015-02-23 06:28:15 +00:00
|
|
|
}
|
2015-02-25 09:38:42 +00:00
|
|
|
}
|