2015-03-03 09:25:30 +00:00
|
|
|
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2013-08-05 09:29:54 +00:00
|
|
|
// License: GNU General Public License v3. See license.txt
|
2013-05-24 13:55:01 +00:00
|
|
|
|
2020-11-25 09:19:10 +00:00
|
|
|
frappe.provide('erpnext.accounts.dimensions');
|
|
|
|
|
2015-01-22 11:43:13 +00:00
|
|
|
erpnext.TransactionController = erpnext.taxes_and_totals.extend({
|
2016-04-29 11:52:42 +00:00
|
|
|
setup: function() {
|
2016-06-15 11:15:03 +00:00
|
|
|
this._super();
|
2020-03-26 07:48:57 +00:00
|
|
|
frappe.flags.hide_serial_batch_dialog = true;
|
2016-04-29 11:52:42 +00:00
|
|
|
frappe.ui.form.on(this.frm.doctype + " Item", "rate", function(frm, cdt, cdn) {
|
|
|
|
var item = frappe.get_doc(cdt, cdn);
|
2017-05-17 14:10:40 +00:00
|
|
|
var has_margin_field = frappe.meta.has_field(cdt, 'margin_type');
|
|
|
|
|
2016-04-29 11:52:42 +00:00
|
|
|
frappe.model.round_floats_in(item, ["rate", "price_list_rate"]);
|
|
|
|
|
|
|
|
if(item.price_list_rate) {
|
2017-05-17 14:10:40 +00:00
|
|
|
if(item.rate > item.price_list_rate && has_margin_field) {
|
|
|
|
// if rate is greater than price_list_rate, set margin
|
|
|
|
// or set discount
|
|
|
|
item.discount_percentage = 0;
|
2017-05-24 03:31:44 +00:00
|
|
|
item.margin_type = 'Amount';
|
2017-06-20 04:00:53 +00:00
|
|
|
item.margin_rate_or_amount = flt(item.rate - item.price_list_rate,
|
2017-05-24 03:31:44 +00:00
|
|
|
precision("margin_rate_or_amount", item));
|
|
|
|
item.rate_with_margin = item.rate;
|
2017-05-17 14:10:40 +00:00
|
|
|
} else {
|
2017-06-20 04:00:53 +00:00
|
|
|
item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0,
|
2017-05-17 14:10:40 +00:00
|
|
|
precision("discount_percentage", item));
|
2019-04-05 14:05:02 +00:00
|
|
|
item.discount_amount = flt(item.price_list_rate) - flt(item.rate);
|
2017-05-17 14:10:40 +00:00
|
|
|
item.margin_type = '';
|
|
|
|
item.margin_rate_or_amount = 0;
|
|
|
|
item.rate_with_margin = 0;
|
|
|
|
}
|
2016-04-29 11:52:42 +00:00
|
|
|
} else {
|
|
|
|
item.discount_percentage = 0.0;
|
2017-05-17 14:10:40 +00:00
|
|
|
item.margin_type = '';
|
|
|
|
item.margin_rate_or_amount = 0;
|
|
|
|
item.rate_with_margin = 0;
|
2016-04-29 11:52:42 +00:00
|
|
|
}
|
2018-11-26 13:14:06 +00:00
|
|
|
item.base_rate_with_margin = item.rate_with_margin * flt(frm.doc.conversion_rate);
|
2016-04-29 11:52:42 +00:00
|
|
|
|
|
|
|
cur_frm.cscript.set_gross_profit(item);
|
|
|
|
cur_frm.cscript.calculate_taxes_and_totals();
|
2017-11-29 10:39:59 +00:00
|
|
|
|
2017-11-17 09:01:09 +00:00
|
|
|
});
|
2016-04-29 11:52:42 +00:00
|
|
|
|
2017-11-29 10:39:59 +00:00
|
|
|
|
|
|
|
|
2016-04-29 11:52:42 +00:00
|
|
|
frappe.ui.form.on(this.frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
|
|
|
|
cur_frm.cscript.calculate_taxes_and_totals();
|
2016-05-02 07:58:46 +00:00
|
|
|
});
|
2016-04-29 11:52:42 +00:00
|
|
|
|
|
|
|
frappe.ui.form.on(this.frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
|
|
|
|
cur_frm.cscript.calculate_taxes_and_totals();
|
2016-05-02 07:58:46 +00:00
|
|
|
});
|
2016-04-29 11:52:42 +00:00
|
|
|
|
|
|
|
frappe.ui.form.on(this.frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
|
|
|
|
cur_frm.cscript.calculate_taxes_and_totals();
|
2016-05-02 07:58:46 +00:00
|
|
|
});
|
2016-04-29 11:52:42 +00:00
|
|
|
|
|
|
|
frappe.ui.form.on(this.frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
|
|
|
|
cur_frm.cscript.set_dynamic_labels();
|
|
|
|
cur_frm.cscript.calculate_taxes_and_totals();
|
2016-05-02 07:58:46 +00:00
|
|
|
});
|
2016-04-29 11:52:42 +00:00
|
|
|
|
|
|
|
frappe.ui.form.on(this.frm.doctype, "apply_discount_on", function(frm) {
|
|
|
|
if(frm.doc.additional_discount_percentage) {
|
|
|
|
frm.trigger("additional_discount_percentage");
|
|
|
|
} else {
|
|
|
|
cur_frm.cscript.calculate_taxes_and_totals();
|
|
|
|
}
|
2016-05-02 07:58:46 +00:00
|
|
|
});
|
2016-04-29 11:52:42 +00:00
|
|
|
|
|
|
|
frappe.ui.form.on(this.frm.doctype, "additional_discount_percentage", function(frm) {
|
|
|
|
if(!frm.doc.apply_discount_on) {
|
|
|
|
frappe.msgprint(__("Please set 'Apply Additional Discount On'"));
|
2017-07-07 07:35:11 +00:00
|
|
|
return;
|
2016-04-29 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
frm.via_discount_percentage = true;
|
|
|
|
|
|
|
|
if(frm.doc.additional_discount_percentage && frm.doc.discount_amount) {
|
|
|
|
// Reset discount amount and net / grand total
|
2017-07-07 07:44:08 +00:00
|
|
|
frm.doc.discount_amount = 0;
|
|
|
|
frm.cscript.calculate_taxes_and_totals();
|
2016-04-29 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var total = flt(frm.doc[frappe.model.scrub(frm.doc.apply_discount_on)]);
|
|
|
|
var discount_amount = flt(total*flt(frm.doc.additional_discount_percentage) / 100,
|
|
|
|
precision("discount_amount"));
|
|
|
|
|
2017-07-07 07:35:11 +00:00
|
|
|
frm.set_value("discount_amount", discount_amount)
|
|
|
|
.then(() => delete frm.via_discount_percentage);
|
2016-04-29 11:52:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
frappe.ui.form.on(this.frm.doctype, "discount_amount", function(frm) {
|
|
|
|
frm.cscript.set_dynamic_labels();
|
|
|
|
|
|
|
|
if (!frm.via_discount_percentage) {
|
2017-07-07 07:35:11 +00:00
|
|
|
frm.doc.additional_discount_percentage = 0;
|
2016-04-29 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
frm.cscript.calculate_taxes_and_totals();
|
|
|
|
});
|
2017-06-02 11:58:40 +00:00
|
|
|
|
2018-11-13 07:07:53 +00:00
|
|
|
frappe.ui.form.on(this.frm.doctype + " Item", {
|
|
|
|
items_add: function(frm, cdt, cdn) {
|
|
|
|
var item = frappe.get_doc(cdt, cdn);
|
2021-01-28 07:39:56 +00:00
|
|
|
if (!item.warehouse && frm.doc.set_warehouse) {
|
2018-11-13 07:07:53 +00:00
|
|
|
item.warehouse = frm.doc.set_warehouse;
|
|
|
|
}
|
2020-11-25 09:19:10 +00:00
|
|
|
|
2021-01-28 07:39:56 +00:00
|
|
|
if (!item.target_warehouse && frm.doc.set_target_warehouse) {
|
|
|
|
item.target_warehouse = frm.doc.set_target_warehouse;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!item.from_warehouse && frm.doc.set_from_warehouse) {
|
|
|
|
item.from_warehouse = frm.doc.set_from_warehouse;
|
|
|
|
}
|
|
|
|
|
2020-11-25 09:19:10 +00:00
|
|
|
erpnext.accounts.dimensions.copy_dimension_from_first_row(frm, cdt, cdn, 'items');
|
2018-11-13 07:07:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-06-02 11:58:40 +00:00
|
|
|
var me = this;
|
|
|
|
if(this.frm.fields_dict["items"].grid.get_field('batch_no')) {
|
|
|
|
this.frm.set_query("batch_no", "items", function(doc, cdt, cdn) {
|
2017-11-17 09:01:09 +00:00
|
|
|
return me.set_query_for_batch(doc, cdt, cdn);
|
2017-06-02 11:58:40 +00:00
|
|
|
});
|
|
|
|
}
|
2013-10-17 06:01:11 +00:00
|
|
|
|
2017-12-25 06:10:19 +00:00
|
|
|
if(
|
2018-05-14 10:16:45 +00:00
|
|
|
this.frm.docstatus < 2
|
2018-04-02 05:02:00 +00:00
|
|
|
&& this.frm.fields_dict["payment_terms_template"]
|
2017-12-25 06:10:19 +00:00
|
|
|
&& this.frm.fields_dict["payment_schedule"]
|
|
|
|
&& this.frm.doc.payment_terms_template
|
|
|
|
&& !this.frm.doc.payment_schedule.length
|
|
|
|
){
|
2017-09-11 10:02:57 +00:00
|
|
|
this.frm.trigger("payment_terms_template");
|
|
|
|
}
|
|
|
|
|
2014-12-26 07:45:21 +00:00
|
|
|
if(this.frm.fields_dict["taxes"]) {
|
|
|
|
this["taxes_remove"] = this.calculate_taxes_and_totals;
|
2013-08-23 07:30:47 +00:00
|
|
|
}
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2014-12-26 07:45:21 +00:00
|
|
|
if(this.frm.fields_dict["items"]) {
|
2017-12-14 12:36:02 +00:00
|
|
|
this["items_remove"] = this.calculate_net_weight;
|
2013-08-23 07:26:33 +00:00
|
|
|
}
|
2015-08-03 07:48:54 +00:00
|
|
|
|
2015-06-22 13:36:15 +00:00
|
|
|
if(this.frm.fields_dict["recurring_print_format"]) {
|
|
|
|
this.frm.set_query("recurring_print_format", function(doc) {
|
|
|
|
return{
|
|
|
|
filters: [
|
|
|
|
['Print Format', 'doc_type', '=', cur_frm.doctype],
|
|
|
|
]
|
2017-11-17 09:01:09 +00:00
|
|
|
};
|
2015-06-22 13:36:15 +00:00
|
|
|
});
|
|
|
|
}
|
2015-08-03 07:48:54 +00:00
|
|
|
|
2015-07-17 09:49:02 +00:00
|
|
|
if(this.frm.fields_dict["return_against"]) {
|
|
|
|
this.frm.set_query("return_against", function(doc) {
|
|
|
|
var filters = {
|
|
|
|
"docstatus": 1,
|
|
|
|
"is_return": 0,
|
|
|
|
"company": doc.company
|
|
|
|
};
|
|
|
|
if (me.frm.fields_dict["customer"] && doc.customer) filters["customer"] = doc.customer;
|
|
|
|
if (me.frm.fields_dict["supplier"] && doc.supplier) filters["supplier"] = doc.supplier;
|
2015-08-03 07:48:54 +00:00
|
|
|
|
2015-07-17 09:49:02 +00:00
|
|
|
return {
|
|
|
|
filters: filters
|
2017-10-02 19:39:46 +00:00
|
|
|
};
|
2015-07-17 09:49:02 +00:00
|
|
|
});
|
|
|
|
}
|
2020-06-26 14:26:00 +00:00
|
|
|
|
2020-06-28 05:42:20 +00:00
|
|
|
if (this.frm.fields_dict["items"].grid.get_field("expense_account")) {
|
2020-06-26 14:26:00 +00:00
|
|
|
this.frm.set_query("expense_account", "items", function(doc) {
|
2020-06-28 05:42:20 +00:00
|
|
|
return {
|
2020-06-26 14:26:00 +00:00
|
|
|
filters: {
|
|
|
|
"company": doc.company
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-31 11:12:41 +00:00
|
|
|
if(frappe.meta.get_docfield(this.frm.doc.doctype, "pricing_rules")) {
|
2019-03-31 15:03:15 +00:00
|
|
|
this.frm.set_indicator_formatter('pricing_rule', function(doc) {
|
|
|
|
return (doc.rule_applied) ? "green" : "red";
|
|
|
|
});
|
2019-03-31 11:12:41 +00:00
|
|
|
}
|
2020-03-27 07:24:01 +00:00
|
|
|
|
|
|
|
let batch_no_field = this.frm.get_docfield("items", "batch_no");
|
|
|
|
if (batch_no_field) {
|
|
|
|
batch_no_field.get_route_options_for_new_doc = function(row) {
|
|
|
|
return {
|
|
|
|
"item": row.doc.item_code
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-28 07:30:04 +00:00
|
|
|
if (this.frm.fields_dict["items"].grid.get_field('blanket_order')) {
|
|
|
|
this.frm.set_query("blanket_order", "items", function(doc, cdt, cdn) {
|
|
|
|
var item = locals[cdt][cdn];
|
|
|
|
return {
|
|
|
|
query: "erpnext.controllers.queries.get_blanket_orders",
|
|
|
|
filters: {
|
|
|
|
"company": doc.company,
|
|
|
|
"blanket_order_type": doc.doctype === "Sales Order" ? "Selling" : "Purchasing",
|
|
|
|
"item": item.item_code
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-25 10:06:41 +00:00
|
|
|
if (this.frm.fields_dict.taxes_and_charges) {
|
|
|
|
this.frm.set_query("taxes_and_charges", function() {
|
|
|
|
return {
|
|
|
|
filters: [
|
|
|
|
['company', '=', me.frm.doc.company],
|
|
|
|
['docstatus', '!=', 2]
|
|
|
|
]
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-02 19:39:46 +00:00
|
|
|
},
|
|
|
|
onload: function() {
|
|
|
|
var me = this;
|
2017-03-14 11:29:11 +00:00
|
|
|
|
2017-10-02 19:39:46 +00:00
|
|
|
if(this.frm.doc.__islocal) {
|
|
|
|
var currency = frappe.defaults.get_user_default("currency");
|
|
|
|
|
|
|
|
let set_value = (fieldname, value) => {
|
|
|
|
if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname]) {
|
|
|
|
return me.frm.set_value(fieldname, value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-28 07:39:56 +00:00
|
|
|
this.frm.trigger('set_default_internal_warehouse');
|
|
|
|
|
2017-10-02 19:39:46 +00:00
|
|
|
return frappe.run_serially([
|
|
|
|
() => set_value('currency', currency),
|
|
|
|
() => set_value('price_list_currency', currency),
|
|
|
|
() => set_value('status', 'Draft'),
|
|
|
|
() => set_value('is_subcontracted', 'No'),
|
|
|
|
() => {
|
|
|
|
if(this.frm.doc.company && !this.frm.doc.amended_from) {
|
|
|
|
this.frm.trigger("company");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
}
|
2016-11-14 16:27:05 +00:00
|
|
|
},
|
2015-08-03 07:48:54 +00:00
|
|
|
|
2018-11-26 14:32:01 +00:00
|
|
|
is_return: function() {
|
|
|
|
if(!this.frm.doc.is_return && this.frm.doc.return_against) {
|
|
|
|
this.frm.set_value('return_against', '');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-11-16 11:51:59 +00:00
|
|
|
setup_quality_inspection: function() {
|
|
|
|
if(!in_list(["Delivery Note", "Sales Invoice", "Purchase Receipt", "Purchase Invoice"], this.frm.doc.doctype)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-14 16:27:05 +00:00
|
|
|
var me = this;
|
2017-03-14 11:29:11 +00:00
|
|
|
var inspection_type = in_list(["Purchase Receipt", "Purchase Invoice"], this.frm.doc.doctype)
|
2016-11-16 11:51:59 +00:00
|
|
|
? "Incoming" : "Outgoing";
|
2017-03-14 11:29:11 +00:00
|
|
|
|
2016-11-15 09:18:23 +00:00
|
|
|
var quality_inspection_field = this.frm.get_docfield("items", "quality_inspection");
|
2016-11-16 11:51:59 +00:00
|
|
|
quality_inspection_field.get_route_options_for_new_doc = function(row) {
|
2016-11-14 16:27:05 +00:00
|
|
|
if(me.frm.is_new()) return;
|
|
|
|
return {
|
|
|
|
"inspection_type": inspection_type,
|
2016-11-16 09:06:28 +00:00
|
|
|
"reference_type": me.frm.doc.doctype,
|
2016-11-16 09:36:08 +00:00
|
|
|
"reference_name": me.frm.doc.name,
|
2016-11-16 11:51:59 +00:00
|
|
|
"item_code": row.doc.item_code,
|
|
|
|
"description": row.doc.description,
|
|
|
|
"item_serial_no": row.doc.serial_no ? row.doc.serial_no.split("\n")[0] : null,
|
|
|
|
"batch_no": row.doc.batch_no
|
2016-11-14 16:27:05 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-14 11:29:11 +00:00
|
|
|
|
2016-11-14 16:27:05 +00:00
|
|
|
this.frm.set_query("quality_inspection", "items", function(doc, cdt, cdn) {
|
|
|
|
var d = locals[cdt][cdn];
|
|
|
|
return {
|
|
|
|
filters: {
|
|
|
|
docstatus: 1,
|
|
|
|
inspection_type: inspection_type,
|
2018-02-22 05:33:48 +00:00
|
|
|
reference_name: doc.name,
|
2016-11-14 16:27:05 +00:00
|
|
|
item_code: d.item_code
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-05-24 13:55:01 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2018-09-11 12:10:37 +00:00
|
|
|
make_payment_request: function() {
|
2018-10-03 10:25:51 +00:00
|
|
|
var me = this;
|
|
|
|
const payment_request_type = (in_list(['Sales Order', 'Sales Invoice'], this.frm.doc.doctype))
|
2018-09-11 12:10:37 +00:00
|
|
|
? "Inward" : "Outward";
|
|
|
|
|
|
|
|
frappe.call({
|
|
|
|
method:"erpnext.accounts.doctype.payment_request.payment_request.make_payment_request",
|
|
|
|
args: {
|
|
|
|
dt: me.frm.doc.doctype,
|
|
|
|
dn: me.frm.doc.name,
|
|
|
|
recipient_id: me.frm.doc.contact_email,
|
|
|
|
payment_request_type: payment_request_type,
|
|
|
|
party_type: payment_request_type == 'Outward' ? "Supplier" : "Customer",
|
|
|
|
party: payment_request_type == 'Outward' ? me.frm.doc.supplier : me.frm.doc.customer
|
|
|
|
},
|
|
|
|
callback: function(r) {
|
|
|
|
if(!r.exc){
|
|
|
|
var doc = frappe.model.sync(r.message);
|
|
|
|
frappe.set_route("Form", r.message.doctype, r.message.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2013-08-06 11:53:44 +00:00
|
|
|
onload_post_render: function() {
|
2015-05-12 09:37:02 +00:00
|
|
|
if(this.frm.doc.__islocal && !(this.frm.doc.taxes || []).length
|
|
|
|
&& !(this.frm.doc.__onload ? this.frm.doc.__onload.load_after_mapping : false)) {
|
2017-10-02 19:39:46 +00:00
|
|
|
frappe.after_ajax(() => this.apply_default_taxes());
|
2015-11-27 09:07:40 +00:00
|
|
|
} else if(this.frm.doc.__islocal && this.frm.doc.company && this.frm.doc["items"]
|
2015-10-28 10:08:08 +00:00
|
|
|
&& !this.frm.doc.is_pos) {
|
2017-10-02 19:39:46 +00:00
|
|
|
frappe.after_ajax(() => this.calculate_taxes_and_totals());
|
2013-08-06 11:53:44 +00:00
|
|
|
}
|
2014-12-26 07:45:21 +00:00
|
|
|
if(frappe.meta.get_docfield(this.frm.doc.doctype + " Item", "item_code")) {
|
2016-04-07 09:55:43 +00:00
|
|
|
this.setup_item_selector();
|
2019-05-16 09:18:10 +00:00
|
|
|
this.frm.get_field("items").grid.set_multiple_add("item_code", "qty");
|
2014-06-04 07:40:41 +00:00
|
|
|
}
|
2013-08-06 11:53:44 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
refresh: function() {
|
2014-04-24 12:41:49 +00:00
|
|
|
erpnext.toggle_naming_series();
|
2013-07-09 07:33:39 +00:00
|
|
|
erpnext.hide_company();
|
2013-07-29 13:30:53 +00:00
|
|
|
this.set_dynamic_labels();
|
2015-02-18 14:21:48 +00:00
|
|
|
this.setup_sms();
|
2018-06-12 06:50:52 +00:00
|
|
|
this.setup_quality_inspection();
|
2019-09-16 09:50:10 +00:00
|
|
|
let scan_barcode_field = this.frm.get_field('scan_barcode');
|
2020-04-26 17:55:40 +00:00
|
|
|
if (scan_barcode_field && scan_barcode_field.get_value()) {
|
2019-09-16 09:50:10 +00:00
|
|
|
scan_barcode_field.set_value("");
|
|
|
|
scan_barcode_field.set_new_description("");
|
|
|
|
|
|
|
|
if (frappe.is_mobile()) {
|
|
|
|
if (scan_barcode_field.$input_wrapper.find('.input-group').length) return;
|
|
|
|
|
|
|
|
let $input_group = $('<div class="input-group">');
|
|
|
|
scan_barcode_field.$input_wrapper.find('.control-input').append($input_group);
|
|
|
|
$input_group.append(scan_barcode_field.$input);
|
|
|
|
$(`<span class="input-group-btn" style="vertical-align: top">
|
|
|
|
<button class="btn btn-default border" type="button">
|
|
|
|
<i class="fa fa-camera text-muted"></i>
|
|
|
|
</button>
|
|
|
|
</span>`)
|
|
|
|
.on('click', '.btn', () => {
|
|
|
|
frappe.barcode.scan_barcode().then(barcode => {
|
|
|
|
scan_barcode_field.set_value(barcode);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.appendTo($input_group);
|
|
|
|
}
|
|
|
|
}
|
2018-11-13 05:41:32 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
scan_barcode: function() {
|
|
|
|
let scan_barcode_field = this.frm.fields_dict["scan_barcode"];
|
|
|
|
|
2019-02-12 11:42:48 +00:00
|
|
|
let show_description = function(idx, exist = null) {
|
|
|
|
if (exist) {
|
2020-10-29 13:25:52 +00:00
|
|
|
frappe.show_alert({
|
|
|
|
message: __('Row #{0}: Qty increased by 1', [idx]),
|
|
|
|
indicator: 'green'
|
|
|
|
});
|
2018-11-13 05:41:32 +00:00
|
|
|
} else {
|
2020-10-29 13:25:52 +00:00
|
|
|
frappe.show_alert({
|
|
|
|
message: __('Row #{0}: Item added', [idx]),
|
|
|
|
indicator: 'green'
|
|
|
|
});
|
2018-11-13 05:41:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this.frm.doc.scan_barcode) {
|
|
|
|
frappe.call({
|
|
|
|
method: "erpnext.selling.page.point_of_sale.point_of_sale.search_serial_or_batch_or_barcode_number",
|
|
|
|
args: { search_value: this.frm.doc.scan_barcode }
|
|
|
|
}).then(r => {
|
2019-02-12 11:42:48 +00:00
|
|
|
const data = r && r.message;
|
2019-07-03 05:04:31 +00:00
|
|
|
if (!data || Object.keys(data).length === 0) {
|
2020-10-29 13:25:52 +00:00
|
|
|
frappe.show_alert({
|
|
|
|
message: __('Cannot find Item with this Barcode'),
|
|
|
|
indicator: 'red'
|
|
|
|
});
|
2019-02-12 11:42:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-11-13 05:41:32 +00:00
|
|
|
|
2019-02-12 11:42:48 +00:00
|
|
|
let cur_grid = this.frm.fields_dict.items.grid;
|
2018-11-13 05:41:32 +00:00
|
|
|
|
2019-02-12 11:42:48 +00:00
|
|
|
let row_to_modify = null;
|
|
|
|
const existing_item_row = this.frm.doc.items.find(d => d.item_code === data.item_code);
|
|
|
|
const blank_item_row = this.frm.doc.items.find(d => !d.item_code);
|
|
|
|
|
|
|
|
if (existing_item_row) {
|
|
|
|
row_to_modify = existing_item_row;
|
|
|
|
} else if (blank_item_row) {
|
|
|
|
row_to_modify = blank_item_row;
|
2018-11-13 05:41:32 +00:00
|
|
|
}
|
2019-02-12 11:42:48 +00:00
|
|
|
|
|
|
|
if (!row_to_modify) {
|
|
|
|
// add new row
|
|
|
|
row_to_modify = frappe.model.add_child(this.frm.doc, cur_grid.doctype, 'items');
|
2018-11-13 05:41:32 +00:00
|
|
|
}
|
2019-02-12 11:42:48 +00:00
|
|
|
|
|
|
|
show_description(row_to_modify.idx, row_to_modify.item_code);
|
|
|
|
|
2020-12-10 12:18:05 +00:00
|
|
|
this.frm.from_barcode = this.frm.from_barcode ? this.frm.from_barcode + 1 : 1;
|
2019-02-12 11:42:48 +00:00
|
|
|
frappe.model.set_value(row_to_modify.doctype, row_to_modify.name, {
|
|
|
|
item_code: data.item_code,
|
|
|
|
qty: (row_to_modify.qty || 0) + 1
|
|
|
|
});
|
|
|
|
|
2019-02-28 07:51:28 +00:00
|
|
|
['serial_no', 'batch_no', 'barcode'].forEach(field => {
|
|
|
|
if (data[field] && frappe.meta.has_field(row_to_modify.doctype, field)) {
|
2020-03-31 09:56:45 +00:00
|
|
|
|
|
|
|
let value = (row_to_modify[field] && field === "serial_no")
|
|
|
|
? row_to_modify[field] + '\n' + data[field] : data[field];
|
|
|
|
|
2019-02-28 07:51:28 +00:00
|
|
|
frappe.model.set_value(row_to_modify.doctype,
|
2020-03-31 09:56:45 +00:00
|
|
|
row_to_modify.name, field, value);
|
2019-02-28 07:51:28 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
scan_barcode_field.set_value('');
|
2020-03-31 09:56:45 +00:00
|
|
|
refresh_field("items");
|
2018-11-13 05:41:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return false;
|
2013-09-17 06:26:17 +00:00
|
|
|
},
|
|
|
|
|
2015-05-12 09:37:02 +00:00
|
|
|
apply_default_taxes: function() {
|
2013-09-17 06:26:17 +00:00
|
|
|
var me = this;
|
2015-05-21 07:21:07 +00:00
|
|
|
var taxes_and_charges_field = frappe.meta.get_docfield(me.frm.doc.doctype, "taxes_and_charges",
|
|
|
|
me.frm.doc.name);
|
|
|
|
|
2020-09-18 05:58:23 +00:00
|
|
|
if (!this.frm.doc.taxes_and_charges && this.frm.doc.taxes && this.frm.doc.taxes.length > 0) {
|
2018-01-05 06:55:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (taxes_and_charges_field) {
|
2015-10-28 10:08:08 +00:00
|
|
|
return frappe.call({
|
2015-05-21 07:21:07 +00:00
|
|
|
method: "erpnext.controllers.accounts_controller.get_default_taxes_and_charges",
|
|
|
|
args: {
|
2017-12-11 09:22:28 +00:00
|
|
|
"master_doctype": taxes_and_charges_field.options,
|
2020-12-10 07:07:16 +00:00
|
|
|
"tax_template": me.frm.doc.taxes_and_charges || "",
|
2017-12-11 09:22:28 +00:00
|
|
|
"company": me.frm.doc.company
|
2015-05-21 07:21:07 +00:00
|
|
|
},
|
2020-12-10 07:07:16 +00:00
|
|
|
debounce: 2000,
|
2015-05-21 07:21:07 +00:00
|
|
|
callback: function(r) {
|
2018-01-15 12:15:46 +00:00
|
|
|
if(!r.exc && r.message) {
|
2017-11-14 04:02:32 +00:00
|
|
|
frappe.run_serially([
|
|
|
|
() => {
|
|
|
|
// directly set in doc, so as not to call triggers
|
2017-12-16 05:19:58 +00:00
|
|
|
if(r.message.taxes_and_charges) {
|
|
|
|
me.frm.doc.taxes_and_charges = r.message.taxes_and_charges;
|
|
|
|
}
|
2017-11-14 04:02:32 +00:00
|
|
|
|
|
|
|
// set taxes table
|
2017-12-16 05:19:58 +00:00
|
|
|
if(r.message.taxes) {
|
|
|
|
me.frm.set_value("taxes", r.message.taxes);
|
|
|
|
}
|
2017-11-14 04:02:32 +00:00
|
|
|
},
|
2018-10-18 12:27:56 +00:00
|
|
|
() => me.set_dynamic_labels(),
|
2017-11-14 04:02:32 +00:00
|
|
|
() => me.calculate_taxes_and_totals()
|
|
|
|
]);
|
2015-05-21 07:21:07 +00:00
|
|
|
}
|
2015-05-12 09:37:02 +00:00
|
|
|
}
|
2015-05-21 07:21:07 +00:00
|
|
|
});
|
|
|
|
}
|
2015-05-12 09:37:02 +00:00
|
|
|
},
|
2014-08-08 10:00:49 +00:00
|
|
|
|
2015-02-18 14:21:48 +00:00
|
|
|
setup_sms: function() {
|
|
|
|
var me = this;
|
2019-07-04 12:17:28 +00:00
|
|
|
let blacklist = ['Purchase Invoice', 'BOM'];
|
2016-04-13 09:49:37 +00:00
|
|
|
if(this.frm.doc.docstatus===1 && !in_list(["Lost", "Stopped", "Closed"], this.frm.doc.status)
|
2019-07-04 12:17:28 +00:00
|
|
|
&& !blacklist.includes(this.frm.doctype)) {
|
2015-02-18 14:21:48 +00:00
|
|
|
this.frm.page.add_menu_item(__('Send SMS'), function() { me.send_sms(); });
|
2014-08-08 10:00:49 +00:00
|
|
|
}
|
2013-09-17 06:26:17 +00:00
|
|
|
},
|
|
|
|
|
2015-02-18 14:21:48 +00:00
|
|
|
send_sms: function() {
|
2017-05-30 07:24:42 +00:00
|
|
|
var sms_man = new erpnext.SMSManager(this.frm.doc);
|
2015-01-15 08:51:41 +00:00
|
|
|
},
|
2013-09-17 06:26:17 +00:00
|
|
|
|
2015-06-09 09:51:50 +00:00
|
|
|
barcode: function(doc, cdt, cdn) {
|
|
|
|
var d = locals[cdt][cdn];
|
|
|
|
if(d.barcode=="" || d.barcode==null) {
|
|
|
|
// barcode cleared, remove item
|
|
|
|
d.item_code = "";
|
|
|
|
}
|
2019-02-28 07:51:28 +00:00
|
|
|
|
2020-12-10 12:18:22 +00:00
|
|
|
this.frm.from_barcode = this.frm.from_barcode ? this.frm.from_barcode + 1 : 1;
|
2019-02-28 07:51:28 +00:00
|
|
|
this.item_code(doc, cdt, cdn);
|
2015-06-09 09:51:50 +00:00
|
|
|
},
|
|
|
|
|
2019-02-28 07:51:28 +00:00
|
|
|
item_code: function(doc, cdt, cdn) {
|
2014-02-12 09:23:18 +00:00
|
|
|
var me = this;
|
2014-03-27 08:47:33 +00:00
|
|
|
var item = frappe.get_doc(cdt, cdn);
|
2017-06-26 08:08:24 +00:00
|
|
|
var update_stock = 0, show_batch_dialog = 0;
|
2017-08-09 11:54:13 +00:00
|
|
|
if(['Sales Invoice'].includes(this.frm.doc.doctype)) {
|
2017-06-23 06:09:03 +00:00
|
|
|
update_stock = cint(me.frm.doc.update_stock);
|
|
|
|
show_batch_dialog = update_stock;
|
2017-06-26 10:01:46 +00:00
|
|
|
|
|
|
|
} else if((this.frm.doc.doctype === 'Purchase Receipt' && me.frm.doc.is_return) ||
|
|
|
|
this.frm.doc.doctype === 'Delivery Note') {
|
2017-06-26 12:15:49 +00:00
|
|
|
show_batch_dialog = 1;
|
2017-06-23 06:09:03 +00:00
|
|
|
}
|
2016-06-02 11:04:00 +00:00
|
|
|
// clear barcode if setting item (else barcode will take priority)
|
2020-12-09 07:06:14 +00:00
|
|
|
if (this.frm.from_barcode == 0) {
|
2016-06-02 11:04:00 +00:00
|
|
|
item.barcode = null;
|
|
|
|
}
|
2020-12-09 07:06:14 +00:00
|
|
|
this.frm.from_barcode = this.frm.from_barcode - 1 >= 0 ? this.frm.from_barcode - 1 : 0;
|
2020-12-09 06:40:28 +00:00
|
|
|
|
2019-02-28 07:51:28 +00:00
|
|
|
|
2014-02-12 09:23:18 +00:00
|
|
|
if(item.item_code || item.barcode || item.serial_no) {
|
|
|
|
if(!this.validate_company_and_party()) {
|
2017-06-23 06:09:03 +00:00
|
|
|
this.frm.fields_dict["items"].grid.grid_rows[item.idx - 1].remove();
|
2014-02-12 09:23:18 +00:00
|
|
|
} else {
|
|
|
|
return this.frm.call({
|
|
|
|
method: "erpnext.stock.get_item_details.get_item_details",
|
|
|
|
child: item,
|
|
|
|
args: {
|
2019-03-31 11:12:41 +00:00
|
|
|
doc: me.frm.doc,
|
2014-02-12 09:23:18 +00:00
|
|
|
args: {
|
|
|
|
item_code: item.item_code,
|
|
|
|
barcode: item.barcode,
|
|
|
|
serial_no: item.serial_no,
|
2020-04-06 16:43:13 +00:00
|
|
|
batch_no: item.batch_no,
|
2019-01-09 08:43:06 +00:00
|
|
|
set_warehouse: me.frm.doc.set_warehouse,
|
2014-02-12 09:23:18 +00:00
|
|
|
warehouse: item.warehouse,
|
2019-07-03 05:04:31 +00:00
|
|
|
customer: me.frm.doc.customer || me.frm.doc.party_name,
|
|
|
|
quotation_to: me.frm.doc.quotation_to,
|
2014-02-12 09:23:18 +00:00
|
|
|
supplier: me.frm.doc.supplier,
|
|
|
|
currency: me.frm.doc.currency,
|
2017-06-23 06:09:03 +00:00
|
|
|
update_stock: update_stock,
|
2014-02-12 09:23:18 +00:00
|
|
|
conversion_rate: me.frm.doc.conversion_rate,
|
2017-05-30 07:24:42 +00:00
|
|
|
price_list: me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
|
2014-02-12 09:23:18 +00:00
|
|
|
price_list_currency: me.frm.doc.price_list_currency,
|
|
|
|
plc_conversion_rate: me.frm.doc.plc_conversion_rate,
|
|
|
|
company: me.frm.doc.company,
|
|
|
|
order_type: me.frm.doc.order_type,
|
|
|
|
is_pos: cint(me.frm.doc.is_pos),
|
2021-01-05 08:23:51 +00:00
|
|
|
is_return: cint(me.frm.doc.is_return),
|
2014-02-12 09:23:18 +00:00
|
|
|
is_subcontracted: me.frm.doc.is_subcontracted,
|
2015-03-09 09:24:37 +00:00
|
|
|
transaction_date: me.frm.doc.transaction_date || me.frm.doc.posting_date,
|
2014-06-20 10:29:49 +00:00
|
|
|
ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
|
2016-01-21 15:34:16 +00:00
|
|
|
doctype: me.frm.doc.doctype,
|
|
|
|
name: me.frm.doc.name,
|
2016-03-09 11:32:59 +00:00
|
|
|
project: item.project || me.frm.doc.project,
|
2017-04-19 07:05:51 +00:00
|
|
|
qty: item.qty || 1,
|
2017-04-03 08:47:08 +00:00
|
|
|
stock_qty: item.stock_qty,
|
2017-11-28 10:41:15 +00:00
|
|
|
conversion_factor: item.conversion_factor,
|
2017-11-29 10:39:59 +00:00
|
|
|
weight_per_unit: item.weight_per_unit,
|
|
|
|
weight_uom: item.weight_uom,
|
2019-06-02 10:33:05 +00:00
|
|
|
manufacturer: item.manufacturer,
|
2019-01-04 05:34:37 +00:00
|
|
|
stock_uom: item.stock_uom,
|
2018-09-06 09:09:35 +00:00
|
|
|
pos_profile: me.frm.doc.doctype == 'Sales Invoice' ? me.frm.doc.pos_profile : '',
|
2018-12-26 21:11:07 +00:00
|
|
|
cost_center: item.cost_center,
|
|
|
|
tax_category: me.frm.doc.tax_category,
|
2019-03-18 09:04:19 +00:00
|
|
|
item_tax_template: item.item_tax_template,
|
2020-02-18 06:58:41 +00:00
|
|
|
child_docname: item.name
|
2014-02-12 09:23:18 +00:00
|
|
|
}
|
|
|
|
},
|
2014-09-21 14:15:49 +00:00
|
|
|
|
2014-02-12 09:23:18 +00:00
|
|
|
callback: function(r) {
|
|
|
|
if(!r.exc) {
|
2017-12-14 12:36:02 +00:00
|
|
|
frappe.run_serially([
|
2018-12-27 23:53:25 +00:00
|
|
|
() => {
|
|
|
|
var d = locals[cdt][cdn];
|
2018-12-28 20:49:11 +00:00
|
|
|
me.add_taxes_from_item_tax_template(d.item_tax_rate);
|
2019-12-18 12:18:39 +00:00
|
|
|
if (d.free_item_data) {
|
|
|
|
me.apply_product_discount(d.free_item_data);
|
|
|
|
}
|
2018-12-27 23:53:25 +00:00
|
|
|
},
|
2020-02-18 06:58:41 +00:00
|
|
|
() => {
|
|
|
|
// for internal customer instead of pricing rule directly apply valuation rate on item
|
|
|
|
if (me.frm.doc.is_internal_customer || me.frm.doc.is_internal_supplier) {
|
|
|
|
me.get_incoming_rate(item, me.frm.posting_date, me.frm.posting_time,
|
|
|
|
me.frm.doc.doctype, me.frm.doc.company);
|
|
|
|
} else {
|
|
|
|
me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
if (me.frm.doc.is_internal_customer || me.frm.doc.is_internal_supplier) {
|
|
|
|
me.calculate_taxes_and_totals();
|
|
|
|
}
|
|
|
|
},
|
2017-12-14 12:36:02 +00:00
|
|
|
() => me.toggle_conversion_factor(item),
|
2020-03-26 07:48:57 +00:00
|
|
|
() => {
|
|
|
|
if (show_batch_dialog)
|
|
|
|
return frappe.db.get_value("Item", item.item_code, ["has_batch_no", "has_serial_no"])
|
|
|
|
.then((r) => {
|
2020-06-10 12:25:24 +00:00
|
|
|
if (r.message &&
|
2021-01-28 06:39:35 +00:00
|
|
|
(r.message.has_batch_no || r.message.has_serial_no)) {
|
2020-03-26 07:48:57 +00:00
|
|
|
frappe.flags.hide_serial_batch_dialog = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2021-01-28 06:39:35 +00:00
|
|
|
() => {
|
|
|
|
// check if batch serial selector is disabled or not
|
|
|
|
if (show_batch_dialog && !frappe.flags.hide_serial_batch_dialog)
|
|
|
|
return frappe.db.get_single_value('Stock Settings', 'disable_serial_no_and_batch_selector')
|
|
|
|
.then((value) => {
|
|
|
|
if (value) {
|
|
|
|
frappe.flags.hide_serial_batch_dialog = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2017-12-14 12:36:02 +00:00
|
|
|
() => {
|
|
|
|
if(show_batch_dialog && !frappe.flags.hide_serial_batch_dialog) {
|
|
|
|
var d = locals[cdt][cdn];
|
|
|
|
$.each(r.message, function(k, v) {
|
|
|
|
if(!d[k]) d[k] = v;
|
|
|
|
});
|
|
|
|
|
2020-04-16 10:51:35 +00:00
|
|
|
if (d.has_batch_no && d.has_serial_no) {
|
|
|
|
d.batch_no = undefined;
|
|
|
|
}
|
|
|
|
|
2017-12-14 12:36:02 +00:00
|
|
|
erpnext.show_serial_batch_selector(me.frm, d, (item) => {
|
|
|
|
me.frm.script_manager.trigger('qty', item.doctype, item.name);
|
2020-03-26 07:48:57 +00:00
|
|
|
if (!me.frm.doc.set_warehouse)
|
|
|
|
me.frm.script_manager.trigger('warehouse', item.doctype, item.name);
|
|
|
|
}, undefined, !frappe.flags.hide_serial_batch_dialog);
|
2017-12-14 12:36:02 +00:00
|
|
|
}
|
|
|
|
},
|
2019-03-18 09:04:19 +00:00
|
|
|
() => me.conversion_factor(doc, cdt, cdn, true),
|
2020-04-14 07:23:54 +00:00
|
|
|
() => me.remove_pricing_rule(item),
|
|
|
|
() => {
|
|
|
|
if (item.apply_rule_on_other_items) {
|
|
|
|
let key = item.name;
|
|
|
|
me.apply_rule_on_other_items({key: item});
|
|
|
|
}
|
|
|
|
}
|
2017-12-14 12:36:02 +00:00
|
|
|
]);
|
2014-02-12 09:23:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2014-04-15 13:10:00 +00:00
|
|
|
},
|
2013-10-18 08:54:21 +00:00
|
|
|
|
2020-02-18 06:58:41 +00:00
|
|
|
get_incoming_rate: function(item, posting_date, posting_time, voucher_type, company) {
|
|
|
|
|
|
|
|
let item_args = {
|
|
|
|
'item_code': item.item_code,
|
|
|
|
'warehouse': in_list('Purchase Receipt', 'Purchase Invoice') ? item.from_warehouse : item.warehouse,
|
|
|
|
'posting_date': posting_date,
|
|
|
|
'posting_time': posting_time,
|
|
|
|
'qty': item.qty * item.conversion_factor,
|
|
|
|
'serial_no': item.serial_no,
|
|
|
|
'voucher_type': voucher_type,
|
|
|
|
'company': company,
|
|
|
|
'allow_zero_valuation_rate': item.allow_zero_valuation_rate
|
|
|
|
}
|
|
|
|
|
|
|
|
frappe.call({
|
|
|
|
method: 'erpnext.stock.utils.get_incoming_rate',
|
|
|
|
args: {
|
|
|
|
args: item_args
|
|
|
|
},
|
|
|
|
callback: function(r) {
|
2021-01-28 07:39:56 +00:00
|
|
|
frappe.model.set_value(item.doctype, item.name, 'rate', r.message * item.conversion_factor);
|
2020-02-18 06:58:41 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-12-27 23:53:25 +00:00
|
|
|
add_taxes_from_item_tax_template: function(item_tax_map) {
|
|
|
|
let me = this;
|
|
|
|
|
2018-12-28 20:49:11 +00:00
|
|
|
if(item_tax_map && cint(frappe.defaults.get_default("add_taxes_from_item_tax_template"))) {
|
2018-12-27 23:53:25 +00:00
|
|
|
if(typeof (item_tax_map) == "string") {
|
|
|
|
item_tax_map = JSON.parse(item_tax_map);
|
|
|
|
}
|
|
|
|
|
|
|
|
$.each(item_tax_map, function(tax, rate) {
|
|
|
|
let found = (me.frm.doc.taxes || []).find(d => d.account_head === tax);
|
|
|
|
if(!found) {
|
|
|
|
let child = frappe.model.add_child(me.frm.doc, "taxes");
|
|
|
|
child.charge_type = "On Net Total";
|
|
|
|
child.account_head = tax;
|
2020-10-28 11:37:10 +00:00
|
|
|
child.rate = 0;
|
2018-12-27 23:53:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-10-18 08:54:21 +00:00
|
|
|
serial_no: function(doc, cdt, cdn) {
|
|
|
|
var me = this;
|
2014-03-27 08:47:33 +00:00
|
|
|
var item = frappe.get_doc(cdt, cdn);
|
2013-10-18 08:54:21 +00:00
|
|
|
|
2019-01-25 11:11:01 +00:00
|
|
|
if (item && item.serial_no) {
|
2013-10-18 08:54:21 +00:00
|
|
|
if (!item.item_code) {
|
2017-03-30 15:51:56 +00:00
|
|
|
this.frm.trigger("item_code", cdt, cdn);
|
2013-10-18 08:54:21 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-07-12 08:40:43 +00:00
|
|
|
var valid_serial_nos = [];
|
2013-10-18 08:54:21 +00:00
|
|
|
|
|
|
|
// Replacing all occurences of comma with carriage return
|
|
|
|
var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
|
|
|
|
|
|
|
|
serial_nos = serial_nos.trim().split('\n');
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-10-18 08:54:21 +00:00
|
|
|
// Trim each string and push unique string to new list
|
|
|
|
for (var x=0; x<=serial_nos.length - 1; x++) {
|
2017-07-12 08:40:43 +00:00
|
|
|
if (serial_nos[x].trim() != "" && valid_serial_nos.indexOf(serial_nos[x].trim()) == -1) {
|
|
|
|
valid_serial_nos.push(serial_nos[x].trim());
|
2013-10-18 08:54:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the new list to the serial no. field in grid with each in new line
|
2017-07-12 08:40:43 +00:00
|
|
|
item.serial_no = valid_serial_nos.join('\n');
|
2020-04-06 16:43:13 +00:00
|
|
|
item.conversion_factor = item.conversion_factor || 1;
|
2013-10-18 08:54:21 +00:00
|
|
|
|
|
|
|
refresh_field("serial_no", item.name, item.parentfield);
|
2018-05-03 10:54:52 +00:00
|
|
|
if(!doc.is_return && cint(user_defaults.set_qty_in_transactions_based_on_serial_no_input)) {
|
2017-03-14 11:29:11 +00:00
|
|
|
frappe.model.set_value(item.doctype, item.name,
|
2017-07-12 08:40:43 +00:00
|
|
|
"qty", valid_serial_nos.length / item.conversion_factor);
|
|
|
|
frappe.model.set_value(item.doctype, item.name, "stock_qty", valid_serial_nos.length);
|
2015-08-04 06:48:12 +00:00
|
|
|
}
|
2013-10-18 08:54:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-28 11:53:36 +00:00
|
|
|
validate: function() {
|
2014-04-16 09:51:46 +00:00
|
|
|
this.calculate_taxes_and_totals(false);
|
2013-05-28 11:53:36 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2021-01-28 07:39:56 +00:00
|
|
|
update_stock: function() {
|
|
|
|
this.frm.trigger('set_default_internal_warehouse');
|
|
|
|
},
|
|
|
|
|
|
|
|
set_default_internal_warehouse: function() {
|
|
|
|
let me = this;
|
|
|
|
if ((this.frm.doc.doctype === 'Sales Invoice' && me.frm.doc.update_stock)
|
|
|
|
|| this.frm.doc.doctype == 'Delivery Note') {
|
|
|
|
if (this.frm.doc.is_internal_customer && this.frm.doc.company === this.frm.doc.represents_company) {
|
|
|
|
frappe.db.get_value('Company', this.frm.doc.company, 'default_in_transit_warehouse', function(value) {
|
|
|
|
me.frm.set_value('set_target_warehouse', value.default_in_transit_warehouse);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((this.frm.doc.doctype === 'Purchase Invoice' && me.frm.doc.update_stock)
|
|
|
|
|| this.frm.doc.doctype == 'Purchase Receipt') {
|
|
|
|
if (this.frm.doc.is_internal_supplier && this.frm.doc.company === this.frm.doc.represents_company) {
|
|
|
|
frappe.db.get_value('Company', this.frm.doc.company, 'default_in_transit_warehouse', function(value) {
|
|
|
|
me.frm.set_value('set_from_warehouse', value.default_in_transit_warehouse);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
company: function() {
|
2014-10-09 09:30:10 +00:00
|
|
|
var me = this;
|
2016-02-04 11:51:28 +00:00
|
|
|
var set_pricing = function() {
|
2014-10-09 09:30:10 +00:00
|
|
|
if(me.frm.doc.company && me.frm.fields_dict.currency) {
|
|
|
|
var company_currency = me.get_company_currency();
|
2015-05-19 06:30:34 +00:00
|
|
|
var company_doc = frappe.get_doc(":Company", me.frm.doc.company);
|
2017-09-29 09:51:59 +00:00
|
|
|
|
2017-10-02 07:50:51 +00:00
|
|
|
if (!me.frm.doc.currency) {
|
2014-10-09 09:30:10 +00:00
|
|
|
me.frm.set_value("currency", company_currency);
|
|
|
|
}
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2014-10-09 09:30:10 +00:00
|
|
|
if (me.frm.doc.currency == company_currency) {
|
|
|
|
me.frm.set_value("conversion_rate", 1.0);
|
|
|
|
}
|
|
|
|
if (me.frm.doc.price_list_currency == company_currency) {
|
|
|
|
me.frm.set_value('plc_conversion_rate', 1.0);
|
|
|
|
}
|
2015-05-19 06:30:34 +00:00
|
|
|
if (company_doc.default_letter_head) {
|
2015-05-27 06:59:57 +00:00
|
|
|
if(me.frm.fields_dict.letter_head) {
|
|
|
|
me.frm.set_value("letter_head", company_doc.default_letter_head);
|
|
|
|
}
|
2015-05-19 06:30:34 +00:00
|
|
|
}
|
2019-07-04 17:16:16 +00:00
|
|
|
let selling_doctypes_for_tc = ["Sales Invoice", "Quotation", "Sales Order", "Delivery Note"];
|
|
|
|
if (company_doc.default_selling_terms && frappe.meta.has_field(me.frm.doc.doctype, "tc_name") &&
|
|
|
|
selling_doctypes_for_tc.indexOf(me.frm.doc.doctype) != -1) {
|
|
|
|
me.frm.set_value("tc_name", company_doc.default_selling_terms);
|
|
|
|
}
|
|
|
|
let buying_doctypes_for_tc = ["Request for Quotation", "Supplier Quotation", "Purchase Order",
|
|
|
|
"Material Request", "Purchase Receipt"];
|
|
|
|
// Purchase Invoice is excluded as per issue #3345
|
|
|
|
if (company_doc.default_buying_terms && frappe.meta.has_field(me.frm.doc.doctype, "tc_name") &&
|
|
|
|
buying_doctypes_for_tc.indexOf(me.frm.doc.doctype) != -1) {
|
|
|
|
me.frm.set_value("tc_name", company_doc.default_buying_terms);
|
2015-05-19 06:30:34 +00:00
|
|
|
}
|
2013-10-11 08:04:10 +00:00
|
|
|
|
2017-12-11 09:22:28 +00:00
|
|
|
frappe.run_serially([
|
|
|
|
() => me.frm.script_manager.trigger("currency"),
|
2018-12-28 21:25:00 +00:00
|
|
|
() => me.update_item_tax_map(),
|
2017-12-11 09:22:28 +00:00
|
|
|
() => me.apply_default_taxes(),
|
|
|
|
() => me.apply_pricing_rule()
|
|
|
|
]);
|
2013-10-11 13:36:30 +00:00
|
|
|
}
|
2013-05-24 13:55:01 +00:00
|
|
|
}
|
2016-02-10 07:38:42 +00:00
|
|
|
|
2016-02-04 11:51:28 +00:00
|
|
|
var set_party_account = function(set_pricing) {
|
|
|
|
if (in_list(["Sales Invoice", "Purchase Invoice"], me.frm.doc.doctype)) {
|
|
|
|
if(me.frm.doc.doctype=="Sales Invoice") {
|
|
|
|
var party_type = "Customer";
|
|
|
|
var party_account_field = 'debit_to';
|
|
|
|
} else {
|
|
|
|
var party_type = "Supplier";
|
|
|
|
var party_account_field = 'credit_to';
|
|
|
|
}
|
2016-02-15 06:12:18 +00:00
|
|
|
|
2016-02-10 09:17:25 +00:00
|
|
|
var party = me.frm.doc[frappe.model.scrub(party_type)];
|
2018-02-08 05:39:54 +00:00
|
|
|
if(party && me.frm.doc.company) {
|
2016-02-10 07:38:42 +00:00
|
|
|
return frappe.call({
|
|
|
|
method: "erpnext.accounts.party.get_party_account",
|
|
|
|
args: {
|
|
|
|
company: me.frm.doc.company,
|
|
|
|
party_type: party_type,
|
2016-02-10 09:17:25 +00:00
|
|
|
party: party
|
2016-02-10 07:38:42 +00:00
|
|
|
},
|
|
|
|
callback: function(r) {
|
|
|
|
if(!r.exc && r.message) {
|
|
|
|
me.frm.set_value(party_account_field, r.message);
|
|
|
|
set_pricing();
|
|
|
|
}
|
2016-02-04 11:51:28 +00:00
|
|
|
}
|
2016-02-10 07:38:42 +00:00
|
|
|
});
|
2016-02-10 09:17:25 +00:00
|
|
|
} else {
|
|
|
|
set_pricing();
|
2016-02-10 07:38:42 +00:00
|
|
|
}
|
2016-02-04 11:51:28 +00:00
|
|
|
} else {
|
|
|
|
set_pricing();
|
|
|
|
}
|
2016-02-10 07:38:42 +00:00
|
|
|
|
2016-02-04 11:51:28 +00:00
|
|
|
}
|
2013-10-11 08:04:10 +00:00
|
|
|
|
2014-10-09 09:30:10 +00:00
|
|
|
if (this.frm.doc.posting_date) var date = this.frm.doc.posting_date;
|
|
|
|
else var date = this.frm.doc.transaction_date;
|
2016-04-26 07:29:08 +00:00
|
|
|
|
|
|
|
if (frappe.meta.get_docfield(this.frm.doctype, "shipping_address") &&
|
2020-08-07 14:14:20 +00:00
|
|
|
in_list(['Purchase Order', 'Purchase Receipt', 'Purchase Invoice'], this.frm.doctype)) {
|
2017-05-30 07:24:42 +00:00
|
|
|
erpnext.utils.get_shipping_address(this.frm, function(){
|
|
|
|
set_party_account(set_pricing);
|
2021-01-28 07:39:56 +00:00
|
|
|
});
|
2020-08-07 14:14:20 +00:00
|
|
|
|
|
|
|
// Get default company billing address in Purchase Invoice, Order and Receipt
|
|
|
|
frappe.call({
|
|
|
|
'method': 'frappe.contacts.doctype.address.address.get_default_address',
|
|
|
|
'args': {
|
|
|
|
'doctype': 'Company',
|
|
|
|
'name': this.frm.doc.company
|
|
|
|
},
|
|
|
|
'callback': function(r) {
|
|
|
|
me.frm.set_value('billing_address', r.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-05-30 07:24:42 +00:00
|
|
|
} else {
|
2016-04-11 06:40:20 +00:00
|
|
|
set_party_account(set_pricing);
|
|
|
|
}
|
2015-04-28 10:32:09 +00:00
|
|
|
|
|
|
|
if(this.frm.doc.company) {
|
|
|
|
erpnext.last_selected_company = this.frm.doc.company;
|
2013-05-24 13:55:01 +00:00
|
|
|
}
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2014-10-09 09:30:10 +00:00
|
|
|
transaction_date: function() {
|
2015-10-19 09:29:32 +00:00
|
|
|
if (this.frm.doc.transaction_date) {
|
|
|
|
this.frm.transaction_date = this.frm.doc.transaction_date;
|
2016-12-08 10:06:23 +00:00
|
|
|
frappe.ui.form.trigger(this.frm.doc.doctype, "currency");
|
2015-10-19 09:29:32 +00:00
|
|
|
}
|
2014-10-03 12:13:02 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
posting_date: function() {
|
2015-07-09 11:42:23 +00:00
|
|
|
var me = this;
|
|
|
|
if (this.frm.doc.posting_date) {
|
2015-10-19 09:29:32 +00:00
|
|
|
this.frm.posting_date = this.frm.doc.posting_date;
|
|
|
|
|
2015-08-03 07:48:54 +00:00
|
|
|
if ((this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.customer) ||
|
2015-07-09 11:42:23 +00:00
|
|
|
(this.frm.doc.doctype == "Purchase Invoice" && this.frm.doc.supplier)) {
|
|
|
|
return frappe.call({
|
|
|
|
method: "erpnext.accounts.party.get_due_date",
|
|
|
|
args: {
|
|
|
|
"posting_date": me.frm.doc.posting_date,
|
|
|
|
"party_type": me.frm.doc.doctype == "Sales Invoice" ? "Customer" : "Supplier",
|
2018-02-16 07:35:21 +00:00
|
|
|
"bill_date": me.frm.doc.bill_date,
|
2015-07-09 11:42:23 +00:00
|
|
|
"party": me.frm.doc.doctype == "Sales Invoice" ? me.frm.doc.customer : me.frm.doc.supplier,
|
|
|
|
"company": me.frm.doc.company
|
2015-08-03 07:48:54 +00:00
|
|
|
},
|
2015-07-09 11:42:23 +00:00
|
|
|
callback: function(r, rt) {
|
|
|
|
if(r.message) {
|
2018-01-29 10:37:21 +00:00
|
|
|
me.frm.doc.due_date = r.message;
|
|
|
|
refresh_field("due_date");
|
2016-12-08 09:13:11 +00:00
|
|
|
frappe.ui.form.trigger(me.frm.doc.doctype, "currency");
|
2018-01-02 06:32:30 +00:00
|
|
|
me.recalculate_terms();
|
2015-07-09 11:42:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2016-12-08 09:13:11 +00:00
|
|
|
} else {
|
|
|
|
frappe.ui.form.trigger(me.frm.doc.doctype, "currency");
|
2015-07-09 11:42:23 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-24 13:55:01 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2018-01-09 05:28:44 +00:00
|
|
|
due_date: function() {
|
|
|
|
// due_date is to be changed, payment terms template and/or payment schedule must
|
|
|
|
// be removed as due_date is automatically changed based on payment terms
|
2018-01-29 12:10:44 +00:00
|
|
|
if (this.frm.doc.due_date && !this.frm.updating_party_details && !this.frm.doc.is_pos) {
|
2018-01-23 10:10:29 +00:00
|
|
|
if (this.frm.doc.payment_terms_template ||
|
|
|
|
(this.frm.doc.payment_schedule && this.frm.doc.payment_schedule.length)) {
|
2018-01-09 05:28:44 +00:00
|
|
|
var message1 = "";
|
|
|
|
var message2 = "";
|
|
|
|
var final_message = "Please clear the ";
|
|
|
|
|
|
|
|
if (this.frm.doc.payment_terms_template) {
|
|
|
|
message1 = "selected Payment Terms Template";
|
|
|
|
final_message = final_message + message1;
|
|
|
|
}
|
|
|
|
|
2018-01-17 09:09:05 +00:00
|
|
|
if ((this.frm.doc.payment_schedule || []).length) {
|
2018-01-09 05:28:44 +00:00
|
|
|
message2 = "Payment Schedule Table";
|
|
|
|
if (message1.length !== 0) message2 = " and " + message2;
|
|
|
|
final_message = final_message + message2;
|
|
|
|
}
|
|
|
|
frappe.msgprint(final_message);
|
|
|
|
}
|
2018-01-29 10:37:21 +00:00
|
|
|
}
|
2018-01-09 05:28:44 +00:00
|
|
|
},
|
|
|
|
|
2018-02-16 07:35:21 +00:00
|
|
|
bill_date: function() {
|
|
|
|
this.posting_date();
|
|
|
|
},
|
|
|
|
|
2018-01-02 06:32:30 +00:00
|
|
|
recalculate_terms: function() {
|
|
|
|
const doc = this.frm.doc;
|
|
|
|
if (doc.payment_terms_template) {
|
|
|
|
this.payment_terms_template();
|
|
|
|
} else if (doc.payment_schedule) {
|
|
|
|
const me = this;
|
|
|
|
doc.payment_schedule.forEach(
|
|
|
|
function(term) {
|
|
|
|
if (term.payment_term) {
|
|
|
|
me.payment_term(doc, term.doctype, term.name);
|
|
|
|
} else {
|
|
|
|
frappe.model.set_value(
|
|
|
|
term.doctype, term.name, 'due_date',
|
|
|
|
doc.posting_date || doc.transaction_date
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
get_company_currency: function() {
|
|
|
|
return erpnext.get_currency(this.frm.doc.company);
|
|
|
|
},
|
2015-08-03 07:48:54 +00:00
|
|
|
|
2015-07-07 11:11:37 +00:00
|
|
|
contact_person: function() {
|
|
|
|
erpnext.utils.get_contact_details(this.frm);
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
currency: function() {
|
2016-09-19 02:53:37 +00:00
|
|
|
/* manqala 19/09/2016: let the translation date be whichever of the transaction_date or posting_date is available */
|
2016-12-08 10:06:23 +00:00
|
|
|
var transaction_date = this.frm.doc.transaction_date || this.frm.doc.posting_date;
|
2016-09-19 02:53:37 +00:00
|
|
|
/* end manqala */
|
2013-06-21 12:25:31 +00:00
|
|
|
var me = this;
|
|
|
|
this.set_dynamic_labels();
|
|
|
|
var company_currency = this.get_company_currency();
|
2015-05-02 13:15:02 +00:00
|
|
|
// Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
|
2016-07-18 09:00:18 +00:00
|
|
|
if(this.frm.doc.currency && this.frm.doc.currency !== company_currency
|
2016-07-07 05:41:32 +00:00
|
|
|
&& !this.frm.doc.ignore_pricing_rule) {
|
2018-05-15 05:55:46 +00:00
|
|
|
|
2016-12-08 10:06:23 +00:00
|
|
|
this.get_exchange_rate(transaction_date, this.frm.doc.currency, company_currency,
|
2013-06-21 12:25:31 +00:00
|
|
|
function(exchange_rate) {
|
2019-05-27 11:45:47 +00:00
|
|
|
if(exchange_rate != me.frm.doc.conversion_rate) {
|
|
|
|
me.set_margin_amount_based_on_currency(exchange_rate);
|
|
|
|
me.set_actual_charges_based_on_currency(exchange_rate);
|
|
|
|
me.frm.set_value("conversion_rate", exchange_rate);
|
|
|
|
}
|
2013-06-21 12:25:31 +00:00
|
|
|
});
|
|
|
|
} else {
|
2014-04-15 13:10:00 +00:00
|
|
|
this.conversion_rate();
|
2013-06-21 12:25:31 +00:00
|
|
|
}
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-06-21 12:25:31 +00:00
|
|
|
conversion_rate: function() {
|
2017-09-21 09:20:39 +00:00
|
|
|
const me = this.frm;
|
2013-07-19 08:21:07 +00:00
|
|
|
if(this.frm.doc.currency === this.get_company_currency()) {
|
|
|
|
this.frm.set_value("conversion_rate", 1.0);
|
2013-10-17 07:34:26 +00:00
|
|
|
}
|
|
|
|
if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
|
2013-06-21 12:25:31 +00:00
|
|
|
this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
|
2017-05-30 07:24:42 +00:00
|
|
|
this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
|
2013-06-21 12:25:31 +00:00
|
|
|
}
|
2015-05-19 05:40:12 +00:00
|
|
|
|
2014-05-28 07:19:20 +00:00
|
|
|
if(flt(this.frm.doc.conversion_rate)>0.0) {
|
2014-06-20 10:29:49 +00:00
|
|
|
if(this.frm.doc.ignore_pricing_rule) {
|
|
|
|
this.calculate_taxes_and_totals();
|
2014-07-04 10:14:26 +00:00
|
|
|
} else if (!this.in_apply_price_list){
|
|
|
|
this.apply_price_list();
|
2014-06-20 10:29:49 +00:00
|
|
|
}
|
|
|
|
|
2014-05-28 07:19:20 +00:00
|
|
|
}
|
2017-09-21 09:20:39 +00:00
|
|
|
// Make read only if Accounts Settings doesn't allow stale rates
|
2017-11-28 17:10:05 +00:00
|
|
|
this.frm.set_df_property("conversion_rate", "read_only", erpnext.stale_rate_allowed() ? 0 : 1);
|
2013-05-24 13:55:01 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2017-11-17 09:01:09 +00:00
|
|
|
shipping_rule: function() {
|
|
|
|
var me = this;
|
2020-06-03 09:02:07 +00:00
|
|
|
if(this.frm.doc.shipping_rule) {
|
2017-11-17 09:01:09 +00:00
|
|
|
return this.frm.call({
|
|
|
|
doc: this.frm.doc,
|
|
|
|
method: "apply_shipping_rule",
|
|
|
|
callback: function(r) {
|
|
|
|
if(!r.exc) {
|
|
|
|
me.calculate_taxes_and_totals();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).fail(() => this.frm.set_value('shipping_rule', ''));
|
|
|
|
}
|
2018-01-22 07:38:05 +00:00
|
|
|
else {
|
|
|
|
me.calculate_taxes_and_totals();
|
|
|
|
}
|
2017-11-17 09:01:09 +00:00
|
|
|
},
|
|
|
|
|
2019-05-27 11:45:47 +00:00
|
|
|
set_margin_amount_based_on_currency: function(exchange_rate) {
|
|
|
|
if (in_list(["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]), this.frm.doc.doctype) {
|
|
|
|
var me = this;
|
|
|
|
$.each(this.frm.doc.items || [], function(i, d) {
|
|
|
|
if(d.margin_type == "Amount") {
|
|
|
|
frappe.model.set_value(d.doctype, d.name, "margin_rate_or_amount",
|
|
|
|
flt(d.margin_rate_or_amount) / flt(exchange_rate));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
set_actual_charges_based_on_currency: function(exchange_rate) {
|
2017-08-31 14:10:09 +00:00
|
|
|
var me = this;
|
|
|
|
$.each(this.frm.doc.taxes || [], function(i, d) {
|
|
|
|
if(d.charge_type == "Actual") {
|
|
|
|
frappe.model.set_value(d.doctype, d.name, "tax_amount",
|
2019-05-27 11:45:47 +00:00
|
|
|
flt(d.tax_amount) / flt(exchange_rate));
|
2017-08-31 14:10:09 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-12-08 10:06:23 +00:00
|
|
|
get_exchange_rate: function(transaction_date, from_currency, to_currency, callback) {
|
2018-05-15 11:29:20 +00:00
|
|
|
var args;
|
|
|
|
if (["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"].includes(this.frm.doctype)) {
|
|
|
|
args = "for_selling";
|
2018-05-15 05:55:46 +00:00
|
|
|
}
|
2018-05-15 11:29:20 +00:00
|
|
|
else if (["Purchase Order", "Purchase Receipt", "Purchase Invoice"].includes(this.frm.doctype)) {
|
|
|
|
args = "for_buying";
|
|
|
|
}
|
|
|
|
|
2017-05-06 07:53:12 +00:00
|
|
|
if (!transaction_date || !from_currency || !to_currency) return;
|
2015-10-28 10:08:08 +00:00
|
|
|
return frappe.call({
|
2015-05-07 06:55:33 +00:00
|
|
|
method: "erpnext.setup.utils.get_exchange_rate",
|
|
|
|
args: {
|
2016-12-08 10:06:23 +00:00
|
|
|
transaction_date: transaction_date,
|
2015-05-07 06:55:33 +00:00
|
|
|
from_currency: from_currency,
|
2018-05-15 05:55:46 +00:00
|
|
|
to_currency: to_currency,
|
|
|
|
args: args
|
2015-05-07 06:55:33 +00:00
|
|
|
},
|
|
|
|
callback: function(r) {
|
|
|
|
callback(flt(r.message));
|
|
|
|
}
|
2013-06-21 12:25:31 +00:00
|
|
|
});
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
price_list_currency: function() {
|
2013-09-11 07:35:24 +00:00
|
|
|
var me=this;
|
2013-06-21 12:25:31 +00:00
|
|
|
this.set_dynamic_labels();
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2015-05-11 12:44:45 +00:00
|
|
|
var company_currency = this.get_company_currency();
|
|
|
|
// Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
|
|
|
|
if(this.frm.doc.price_list_currency !== company_currency && !this.frm.doc.ignore_pricing_rule) {
|
2016-09-07 02:06:08 +00:00
|
|
|
this.get_exchange_rate(this.frm.doc.posting_date, this.frm.doc.price_list_currency, company_currency,
|
2015-05-11 12:44:45 +00:00
|
|
|
function(exchange_rate) {
|
|
|
|
me.frm.set_value("plc_conversion_rate", exchange_rate);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.plc_conversion_rate();
|
2013-05-24 13:55:01 +00:00
|
|
|
}
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2015-05-11 12:44:45 +00:00
|
|
|
plc_conversion_rate: function() {
|
2013-06-21 12:25:31 +00:00
|
|
|
if(this.frm.doc.price_list_currency === this.get_company_currency()) {
|
|
|
|
this.frm.set_value("plc_conversion_rate", 1.0);
|
2015-08-03 07:48:54 +00:00
|
|
|
} else if(this.frm.doc.price_list_currency === this.frm.doc.currency
|
2015-07-17 09:49:02 +00:00
|
|
|
&& this.frm.doc.plc_conversion_rate && cint(this.frm.doc.plc_conversion_rate) != 1 &&
|
2015-05-11 12:44:45 +00:00
|
|
|
cint(this.frm.doc.plc_conversion_rate) != cint(this.frm.doc.conversion_rate)) {
|
2017-05-30 07:24:42 +00:00
|
|
|
this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
|
2013-10-17 06:01:11 +00:00
|
|
|
}
|
2015-05-19 05:40:12 +00:00
|
|
|
|
2015-05-11 12:44:45 +00:00
|
|
|
if(!this.in_apply_price_list) {
|
2018-02-12 09:48:57 +00:00
|
|
|
this.apply_price_list(null, true);
|
2013-06-21 12:25:31 +00:00
|
|
|
}
|
2013-05-24 13:55:01 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2017-02-10 08:57:11 +00:00
|
|
|
uom: function(doc, cdt, cdn) {
|
|
|
|
var me = this;
|
|
|
|
var item = frappe.get_doc(cdt, cdn);
|
|
|
|
if(item.item_code && item.uom) {
|
|
|
|
return this.frm.call({
|
|
|
|
method: "erpnext.stock.get_item_details.get_conversion_factor",
|
|
|
|
args: {
|
|
|
|
item_code: item.item_code,
|
|
|
|
uom: item.uom
|
|
|
|
},
|
|
|
|
callback: function(r) {
|
|
|
|
if(!r.exc) {
|
2020-10-26 05:47:04 +00:00
|
|
|
frappe.model.set_value(cdt, cdn, 'conversion_factor', r.message.conversion_factor);
|
2017-02-10 08:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-05-06 07:17:14 +00:00
|
|
|
conversion_factor: function(doc, cdt, cdn, dont_fetch_price_list_rate) {
|
2020-02-18 05:21:54 +00:00
|
|
|
if(frappe.meta.get_docfield(cdt, "stock_qty", cdn)) {
|
2017-02-10 08:57:11 +00:00
|
|
|
var item = frappe.get_doc(cdt, cdn);
|
|
|
|
frappe.model.round_floats_in(item, ["qty", "conversion_factor"]);
|
|
|
|
item.stock_qty = flt(item.qty * item.conversion_factor, precision("stock_qty", item));
|
|
|
|
refresh_field("stock_qty", item.name, item.parentfield);
|
2017-04-10 13:45:57 +00:00
|
|
|
this.toggle_conversion_factor(item);
|
2020-02-18 05:21:54 +00:00
|
|
|
|
|
|
|
if(doc.doctype != "Material Request") {
|
|
|
|
item.total_weight = flt(item.stock_qty * item.weight_per_unit);
|
|
|
|
refresh_field("total_weight", item.name, item.parentfield);
|
|
|
|
this.calculate_net_weight();
|
|
|
|
}
|
|
|
|
|
2017-11-21 10:47:22 +00:00
|
|
|
if (!dont_fetch_price_list_rate &&
|
|
|
|
frappe.meta.has_field(doc.doctype, "price_list_currency")) {
|
|
|
|
this.apply_price_list(item, true);
|
|
|
|
}
|
2017-02-10 08:57:11 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-01-28 06:35:57 +00:00
|
|
|
batch_no: function(doc, cdt, cdn) {
|
|
|
|
let item = frappe.get_doc(cdt, cdn);
|
|
|
|
this.apply_price_list(item, true);
|
|
|
|
},
|
|
|
|
|
2017-04-10 13:45:57 +00:00
|
|
|
toggle_conversion_factor: function(item) {
|
|
|
|
// toggle read only property for conversion factor field if the uom and stock uom are same
|
2017-06-20 04:00:53 +00:00
|
|
|
if(this.frm.get_field('items').grid.fields_map.conversion_factor) {
|
|
|
|
this.frm.fields_dict.items.grid.toggle_enable("conversion_factor",
|
2017-12-29 08:36:47 +00:00
|
|
|
((item.uom != item.stock_uom) && !frappe.meta.get_docfield(cur_frm.fields_dict.items.grid.doctype, "conversion_factor").read_only)? true: false);
|
2017-06-20 04:00:53 +00:00
|
|
|
}
|
2017-12-14 12:36:02 +00:00
|
|
|
|
2017-04-10 13:45:57 +00:00
|
|
|
},
|
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
qty: function(doc, cdt, cdn) {
|
2019-03-18 09:04:19 +00:00
|
|
|
let item = frappe.get_doc(cdt, cdn);
|
2020-01-21 07:34:21 +00:00
|
|
|
this.conversion_factor(doc, cdt, cdn, true);
|
2019-03-18 09:04:19 +00:00
|
|
|
this.apply_pricing_rule(item, true);
|
2017-11-29 10:39:59 +00:00
|
|
|
},
|
|
|
|
|
2018-09-21 10:15:40 +00:00
|
|
|
service_stop_date: function(frm, cdt, cdn) {
|
|
|
|
var child = locals[cdt][cdn];
|
|
|
|
|
|
|
|
if(child.service_stop_date) {
|
|
|
|
let start_date = Date.parse(child.service_start_date);
|
|
|
|
let end_date = Date.parse(child.service_end_date);
|
|
|
|
let stop_date = Date.parse(child.service_stop_date);
|
|
|
|
|
|
|
|
if(stop_date < start_date) {
|
|
|
|
frappe.model.set_value(cdt, cdn, "service_stop_date", "");
|
|
|
|
frappe.throw(__("Service Stop Date cannot be before Service Start Date"));
|
|
|
|
} else if (stop_date > end_date) {
|
|
|
|
frappe.model.set_value(cdt, cdn, "service_stop_date", "");
|
|
|
|
frappe.throw(__("Service Stop Date cannot be after Service End Date"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
service_start_date: function(frm, cdt, cdn) {
|
|
|
|
var child = locals[cdt][cdn];
|
|
|
|
|
|
|
|
if(child.service_start_date) {
|
|
|
|
frappe.call({
|
|
|
|
"method": "erpnext.stock.get_item_details.calculate_service_end_date",
|
|
|
|
args: {"args": child},
|
|
|
|
callback: function(r) {
|
|
|
|
frappe.model.set_value(cdt, cdn, "service_end_date", r.message.service_end_date);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-12-14 12:36:02 +00:00
|
|
|
calculate_net_weight: function(){
|
|
|
|
/* Calculate Total Net Weight then further applied shipping rule to calculate shipping charges.*/
|
|
|
|
var me = this;
|
|
|
|
this.frm.doc.total_net_weight= 0.0;
|
|
|
|
|
|
|
|
$.each(this.frm.doc["items"] || [], function(i, item) {
|
|
|
|
me.frm.doc.total_net_weight += flt(item.total_weight);
|
|
|
|
});
|
|
|
|
refresh_field("total_net_weight");
|
|
|
|
this.shipping_rule();
|
2013-05-24 13:55:01 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-06-21 12:25:31 +00:00
|
|
|
set_dynamic_labels: function() {
|
|
|
|
// What TODO? should we make price list system non-mandatory?
|
|
|
|
this.frm.toggle_reqd("plc_conversion_rate",
|
|
|
|
!!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2018-10-18 12:27:56 +00:00
|
|
|
var company_currency = this.get_company_currency();
|
|
|
|
this.change_form_labels(company_currency);
|
|
|
|
this.change_grid_labels(company_currency);
|
|
|
|
this.frm.refresh_fields();
|
2013-06-21 12:25:31 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2015-02-23 06:28:15 +00:00
|
|
|
change_form_labels: function(company_currency) {
|
|
|
|
var me = this;
|
2016-10-21 14:01:54 +00:00
|
|
|
|
|
|
|
this.frm.set_currency_labels(["base_total", "base_net_total", "base_total_taxes_and_charges",
|
2015-02-23 06:28:15 +00:00
|
|
|
"base_discount_amount", "base_grand_total", "base_rounded_total", "base_in_words",
|
|
|
|
"base_taxes_and_charges_added", "base_taxes_and_charges_deducted", "total_amount_to_pay",
|
2017-03-14 11:29:11 +00:00
|
|
|
"base_paid_amount", "base_write_off_amount", "base_change_amount", "base_operating_cost",
|
2017-09-19 09:23:16 +00:00
|
|
|
"base_raw_material_cost", "base_total_cost", "base_scrap_material_cost",
|
|
|
|
"base_rounding_adjustment"], company_currency);
|
2015-02-23 06:28:15 +00:00
|
|
|
|
2016-10-21 14:01:54 +00:00
|
|
|
this.frm.set_currency_labels(["total", "net_total", "total_taxes_and_charges", "discount_amount",
|
2015-02-23 06:28:15 +00:00
|
|
|
"grand_total", "taxes_and_charges_added", "taxes_and_charges_deducted",
|
2017-09-19 09:23:16 +00:00
|
|
|
"rounded_total", "in_words", "paid_amount", "write_off_amount", "operating_cost",
|
|
|
|
"scrap_material_cost", "rounding_adjustment", "raw_material_cost",
|
|
|
|
"total_cost"], this.frm.doc.currency);
|
2015-10-19 09:29:32 +00:00
|
|
|
|
2017-09-19 09:23:16 +00:00
|
|
|
this.frm.set_currency_labels(["outstanding_amount", "total_advance"],
|
|
|
|
this.frm.doc.party_account_currency);
|
2015-02-23 06:28:15 +00:00
|
|
|
|
|
|
|
cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
|
2017-09-19 09:23:16 +00:00
|
|
|
+ " = [?] " + company_currency);
|
2015-02-23 06:28:15 +00:00
|
|
|
|
|
|
|
if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
|
2017-09-19 09:23:16 +00:00
|
|
|
cur_frm.set_df_property("plc_conversion_rate", "description", "1 "
|
|
|
|
+ this.frm.doc.price_list_currency + " = [?] " + company_currency);
|
2015-02-23 06:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// toggle fields
|
2017-09-19 09:23:16 +00:00
|
|
|
this.frm.toggle_display(["conversion_rate", "base_total", "base_net_total",
|
|
|
|
"base_total_taxes_and_charges", "base_taxes_and_charges_added", "base_taxes_and_charges_deducted",
|
2015-08-27 06:58:36 +00:00
|
|
|
"base_grand_total", "base_rounded_total", "base_in_words", "base_discount_amount",
|
2017-09-19 09:23:16 +00:00
|
|
|
"base_paid_amount", "base_write_off_amount", "base_operating_cost", "base_raw_material_cost",
|
|
|
|
"base_total_cost", "base_scrap_material_cost", "base_rounding_adjustment"],
|
2017-11-17 09:01:09 +00:00
|
|
|
this.frm.doc.currency != company_currency);
|
2015-02-23 06:28:15 +00:00
|
|
|
|
|
|
|
this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
|
|
|
|
this.frm.doc.price_list_currency != company_currency);
|
|
|
|
|
2017-11-17 09:01:09 +00:00
|
|
|
var show = cint(cur_frm.doc.discount_amount) ||
|
2015-02-24 09:54:49 +00:00
|
|
|
((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
|
|
|
|
|
|
|
|
if(frappe.meta.get_docfield(cur_frm.doctype, "net_total"))
|
|
|
|
cur_frm.toggle_display("net_total", show);
|
|
|
|
|
|
|
|
if(frappe.meta.get_docfield(cur_frm.doctype, "base_net_total"))
|
|
|
|
cur_frm.toggle_display("base_net_total", (show && (me.frm.doc.currency != company_currency)));
|
|
|
|
|
2015-02-23 06:28:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
change_grid_labels: function(company_currency) {
|
|
|
|
var me = this;
|
|
|
|
|
2016-10-21 14:01:54 +00:00
|
|
|
this.frm.set_currency_labels(["base_rate", "base_net_rate", "base_price_list_rate", "base_amount", "base_net_amount"],
|
2015-02-23 06:28:15 +00:00
|
|
|
company_currency, "items");
|
|
|
|
|
2016-10-21 14:01:54 +00:00
|
|
|
this.frm.set_currency_labels(["rate", "net_rate", "price_list_rate", "amount", "net_amount"],
|
2015-02-23 06:28:15 +00:00
|
|
|
this.frm.doc.currency, "items");
|
|
|
|
|
2016-10-21 14:01:54 +00:00
|
|
|
if(this.frm.fields_dict["operations"]) {
|
|
|
|
this.frm.set_currency_labels(["operating_cost", "hour_rate"], this.frm.doc.currency, "operations");
|
|
|
|
this.frm.set_currency_labels(["base_operating_cost", "base_hour_rate"], company_currency, "operations");
|
|
|
|
|
|
|
|
var item_grid = this.frm.fields_dict["operations"].grid;
|
|
|
|
$.each(["base_operating_cost", "base_hour_rate"], function(i, fname) {
|
|
|
|
if(frappe.meta.get_docfield(item_grid.doctype, fname))
|
|
|
|
item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this.frm.fields_dict["scrap_items"]) {
|
|
|
|
this.frm.set_currency_labels(["rate", "amount"], this.frm.doc.currency, "scrap_items");
|
|
|
|
this.frm.set_currency_labels(["base_rate", "base_amount"], company_currency, "scrap_items");
|
|
|
|
|
|
|
|
var item_grid = this.frm.fields_dict["scrap_items"].grid;
|
|
|
|
$.each(["base_rate", "base_amount"], function(i, fname) {
|
|
|
|
if(frappe.meta.get_docfield(item_grid.doctype, fname))
|
|
|
|
item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-02-23 06:28:15 +00:00
|
|
|
if(this.frm.fields_dict["taxes"]) {
|
2016-10-21 14:01:54 +00:00
|
|
|
this.frm.set_currency_labels(["tax_amount", "total", "tax_amount_after_discount"], this.frm.doc.currency, "taxes");
|
2015-02-23 06:28:15 +00:00
|
|
|
|
2016-10-21 14:01:54 +00:00
|
|
|
this.frm.set_currency_labels(["base_tax_amount", "base_total", "base_tax_amount_after_discount"], company_currency, "taxes");
|
2015-02-23 06:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(this.frm.fields_dict["advances"]) {
|
2016-10-21 14:01:54 +00:00
|
|
|
this.frm.set_currency_labels(["advance_amount", "allocated_amount"],
|
2015-10-16 07:38:33 +00:00
|
|
|
this.frm.doc.party_account_currency, "advances");
|
2015-02-23 06:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// toggle columns
|
|
|
|
var item_grid = this.frm.fields_dict["items"].grid;
|
|
|
|
$.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
|
|
|
|
if(frappe.meta.get_docfield(item_grid.doctype, fname))
|
|
|
|
item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
|
|
|
|
});
|
|
|
|
|
|
|
|
var show = (cint(cur_frm.doc.discount_amount)) ||
|
|
|
|
((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
|
|
|
|
|
|
|
|
$.each(["net_rate", "net_amount"], function(i, fname) {
|
|
|
|
if(frappe.meta.get_docfield(item_grid.doctype, fname))
|
|
|
|
item_grid.set_column_disp(fname, show);
|
|
|
|
});
|
|
|
|
|
|
|
|
$.each(["base_net_rate", "base_net_amount"], function(i, fname) {
|
|
|
|
if(frappe.meta.get_docfield(item_grid.doctype, fname))
|
|
|
|
item_grid.set_column_disp(fname, (show && (me.frm.doc.currency != company_currency)));
|
|
|
|
});
|
|
|
|
|
|
|
|
// set labels
|
|
|
|
var $wrapper = $(this.frm.wrapper);
|
|
|
|
},
|
|
|
|
|
2013-05-28 11:53:36 +00:00
|
|
|
recalculate: function() {
|
|
|
|
this.calculate_taxes_and_totals();
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-28 11:53:36 +00:00
|
|
|
recalculate_values: function() {
|
|
|
|
this.calculate_taxes_and_totals();
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-28 13:24:48 +00:00
|
|
|
calculate_charges: function() {
|
|
|
|
this.calculate_taxes_and_totals();
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2014-06-20 10:29:49 +00:00
|
|
|
ignore_pricing_rule: function() {
|
2017-04-28 08:53:03 +00:00
|
|
|
if(this.frm.doc.ignore_pricing_rule) {
|
|
|
|
var me = this;
|
|
|
|
var item_list = [];
|
2017-06-20 04:00:53 +00:00
|
|
|
|
2017-04-28 08:53:03 +00:00
|
|
|
$.each(this.frm.doc["items"] || [], function(i, d) {
|
2019-03-18 09:04:19 +00:00
|
|
|
if (d.item_code && !d.is_free_item) {
|
2017-04-28 08:53:03 +00:00
|
|
|
item_list.push({
|
|
|
|
"doctype": d.doctype,
|
|
|
|
"name": d.name,
|
2019-03-18 09:04:19 +00:00
|
|
|
"item_code": d.item_code,
|
|
|
|
"pricing_rules": d.pricing_rules,
|
|
|
|
"parenttype": d.parenttype,
|
|
|
|
"parent": d.parent
|
2017-04-28 08:53:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return this.frm.call({
|
|
|
|
method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.remove_pricing_rules",
|
|
|
|
args: { item_list: item_list },
|
|
|
|
callback: function(r) {
|
|
|
|
if (!r.exc && r.message) {
|
2019-03-18 09:04:19 +00:00
|
|
|
r.message.forEach(row_item => {
|
2019-11-19 13:17:48 +00:00
|
|
|
me.remove_pricing_rule(row_item);
|
2019-03-18 09:04:19 +00:00
|
|
|
});
|
2017-04-28 08:53:03 +00:00
|
|
|
me._set_values_for_item_list(r.message);
|
|
|
|
me.calculate_taxes_and_totals();
|
2017-11-17 09:01:09 +00:00
|
|
|
if(me.frm.doc.apply_discount_on) me.frm.trigger("apply_discount_on");
|
2017-04-28 08:53:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.apply_pricing_rule();
|
|
|
|
}
|
2014-06-20 10:29:49 +00:00
|
|
|
},
|
2014-05-28 07:19:20 +00:00
|
|
|
|
2014-06-20 10:29:49 +00:00
|
|
|
apply_pricing_rule: function(item, calculate_taxes_and_totals) {
|
|
|
|
var me = this;
|
2015-11-27 09:07:40 +00:00
|
|
|
var args = this._get_args(item);
|
2016-01-15 11:29:26 +00:00
|
|
|
if (!(args.items && args.items.length)) {
|
2015-11-27 09:07:40 +00:00
|
|
|
if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
|
|
|
|
return;
|
|
|
|
}
|
2019-03-18 09:04:19 +00:00
|
|
|
|
2014-07-01 12:15:15 +00:00
|
|
|
return this.frm.call({
|
|
|
|
method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
|
2019-03-31 11:12:41 +00:00
|
|
|
args: { args: args, doc: me.frm.doc },
|
2014-07-01 12:15:15 +00:00
|
|
|
callback: function(r) {
|
2014-07-16 12:22:08 +00:00
|
|
|
if (!r.exc && r.message) {
|
2014-07-01 12:15:15 +00:00
|
|
|
me._set_values_for_item_list(r.message);
|
2016-02-26 05:39:20 +00:00
|
|
|
if(item) me.set_gross_profit(item);
|
2016-09-22 08:32:53 +00:00
|
|
|
if(me.frm.doc.apply_discount_on) me.frm.trigger("apply_discount_on")
|
2014-07-01 12:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_get_args: function(item) {
|
|
|
|
var me = this;
|
|
|
|
return {
|
2016-01-15 11:29:26 +00:00
|
|
|
"items": this._get_item_list(item),
|
2019-07-03 05:04:31 +00:00
|
|
|
"customer": me.frm.doc.customer || me.frm.doc.party_name,
|
|
|
|
"quotation_to": me.frm.doc.quotation_to,
|
2014-06-20 10:29:49 +00:00
|
|
|
"customer_group": me.frm.doc.customer_group,
|
|
|
|
"territory": me.frm.doc.territory,
|
|
|
|
"supplier": me.frm.doc.supplier,
|
2018-04-19 13:07:53 +00:00
|
|
|
"supplier_group": me.frm.doc.supplier_group,
|
2014-06-20 10:29:49 +00:00
|
|
|
"currency": me.frm.doc.currency,
|
|
|
|
"conversion_rate": me.frm.doc.conversion_rate,
|
|
|
|
"price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
|
2015-05-11 11:19:17 +00:00
|
|
|
"price_list_currency": me.frm.doc.price_list_currency,
|
2014-06-20 10:29:49 +00:00
|
|
|
"plc_conversion_rate": me.frm.doc.plc_conversion_rate,
|
|
|
|
"company": me.frm.doc.company,
|
|
|
|
"transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
|
|
|
|
"campaign": me.frm.doc.campaign,
|
|
|
|
"sales_partner": me.frm.doc.sales_partner,
|
|
|
|
"ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
|
2016-01-15 11:29:26 +00:00
|
|
|
"doctype": me.frm.doc.doctype,
|
2017-03-28 06:39:07 +00:00
|
|
|
"name": me.frm.doc.name,
|
2017-04-03 08:47:08 +00:00
|
|
|
"is_return": cint(me.frm.doc.is_return),
|
|
|
|
"update_stock": in_list(['Sales Invoice', 'Purchase Invoice'], me.frm.doc.doctype) ? cint(me.frm.doc.update_stock) : 0,
|
2017-11-28 10:41:15 +00:00
|
|
|
"conversion_factor": me.frm.doc.conversion_factor,
|
2019-08-12 08:09:25 +00:00
|
|
|
"pos_profile": me.frm.doc.doctype == 'Sales Invoice' ? me.frm.doc.pos_profile : '',
|
|
|
|
"coupon_code": me.frm.doc.coupon_code
|
2014-06-20 10:29:49 +00:00
|
|
|
};
|
|
|
|
},
|
2014-05-28 07:19:20 +00:00
|
|
|
|
2014-06-20 10:29:49 +00:00
|
|
|
_get_item_list: function(item) {
|
|
|
|
var item_list = [];
|
|
|
|
var append_item = function(d) {
|
2014-07-28 10:30:19 +00:00
|
|
|
if (d.item_code) {
|
|
|
|
item_list.push({
|
|
|
|
"doctype": d.doctype,
|
|
|
|
"name": d.name,
|
2019-03-18 09:04:19 +00:00
|
|
|
"child_docname": d.name,
|
2014-07-28 10:30:19 +00:00
|
|
|
"item_code": d.item_code,
|
|
|
|
"item_group": d.item_group,
|
|
|
|
"brand": d.brand,
|
2016-02-15 06:12:18 +00:00
|
|
|
"qty": d.qty,
|
2019-03-18 09:04:19 +00:00
|
|
|
"stock_qty": d.stock_qty,
|
2018-08-01 12:14:34 +00:00
|
|
|
"uom": d.uom,
|
2019-04-29 17:48:47 +00:00
|
|
|
"stock_uom": d.stock_uom,
|
2016-02-15 06:12:18 +00:00
|
|
|
"parenttype": d.parenttype,
|
2016-10-24 08:34:46 +00:00
|
|
|
"parent": d.parent,
|
2019-03-18 09:04:19 +00:00
|
|
|
"pricing_rules": d.pricing_rules,
|
2016-11-13 15:49:09 +00:00
|
|
|
"warehouse": d.warehouse,
|
2017-04-03 08:47:08 +00:00
|
|
|
"serial_no": d.serial_no,
|
2021-01-28 06:35:57 +00:00
|
|
|
"batch_no": d.batch_no,
|
2019-03-18 09:04:19 +00:00
|
|
|
"price_list_rate": d.price_list_rate,
|
2017-04-10 13:45:57 +00:00
|
|
|
"conversion_factor": d.conversion_factor || 1.0
|
2014-07-28 10:30:19 +00:00
|
|
|
});
|
2016-01-18 11:02:24 +00:00
|
|
|
|
|
|
|
// if doctype is Quotation Item / Sales Order Iten then add Margin Type and rate in item_list
|
2016-03-05 09:40:25 +00:00
|
|
|
if (in_list(["Quotation Item", "Sales Order Item", "Delivery Note Item", "Sales Invoice Item"]), d.doctype){
|
2017-11-17 09:01:09 +00:00
|
|
|
item_list[0]["margin_type"] = d.margin_type;
|
|
|
|
item_list[0]["margin_rate_or_amount"] = d.margin_rate_or_amount;
|
2016-03-29 12:22:22 +00:00
|
|
|
}
|
2014-07-28 10:30:19 +00:00
|
|
|
}
|
2014-06-20 10:29:49 +00:00
|
|
|
};
|
2014-05-28 07:19:20 +00:00
|
|
|
|
2014-06-20 10:29:49 +00:00
|
|
|
if (item) {
|
|
|
|
append_item(item);
|
2014-05-30 06:13:00 +00:00
|
|
|
} else {
|
2015-01-21 11:52:11 +00:00
|
|
|
$.each(this.frm.doc["items"] || [], function(i, d) {
|
2014-06-20 10:29:49 +00:00
|
|
|
append_item(d);
|
2014-05-28 07:19:20 +00:00
|
|
|
});
|
|
|
|
}
|
2014-06-20 10:29:49 +00:00
|
|
|
return item_list;
|
2014-05-28 07:19:20 +00:00
|
|
|
},
|
|
|
|
|
2014-07-01 12:15:15 +00:00
|
|
|
_set_values_for_item_list: function(children) {
|
2014-12-17 05:51:32 +00:00
|
|
|
var me = this;
|
2014-12-30 10:05:45 +00:00
|
|
|
var price_list_rate_changed = false;
|
2019-11-19 13:17:48 +00:00
|
|
|
var items_rule_dict = {};
|
|
|
|
|
2015-02-05 09:35:58 +00:00
|
|
|
for(var i=0, l=children.length; i<l; i++) {
|
|
|
|
var d = children[i];
|
2019-03-18 09:04:19 +00:00
|
|
|
var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rules");
|
2015-02-05 09:35:58 +00:00
|
|
|
for(var k in d) {
|
|
|
|
var v = d[k];
|
2014-07-01 12:15:15 +00:00
|
|
|
if (["doctype", "name"].indexOf(k)===-1) {
|
2014-12-30 10:05:45 +00:00
|
|
|
if(k=="price_list_rate") {
|
|
|
|
if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true;
|
|
|
|
}
|
2017-04-10 13:45:57 +00:00
|
|
|
frappe.model.set_value(d.doctype, d.name, k, v);
|
2014-07-01 12:15:15 +00:00
|
|
|
}
|
2015-02-05 09:35:58 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 05:51:32 +00:00
|
|
|
// if pricing rule set as blank from an existing value, apply price_list
|
2019-03-18 09:04:19 +00:00
|
|
|
if(!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rules) {
|
2014-12-17 05:51:32 +00:00
|
|
|
me.apply_price_list(frappe.get_doc(d.doctype, d.name));
|
2019-11-19 13:17:48 +00:00
|
|
|
} else if(!d.pricing_rules) {
|
|
|
|
me.remove_pricing_rule(frappe.get_doc(d.doctype, d.name));
|
|
|
|
}
|
|
|
|
|
2019-12-13 10:48:38 +00:00
|
|
|
if (d.free_item_data) {
|
|
|
|
me.apply_product_discount(d.free_item_data);
|
|
|
|
}
|
|
|
|
|
2019-11-19 13:17:48 +00:00
|
|
|
if (d.apply_rule_on_other_items) {
|
|
|
|
items_rule_dict[d.name] = d;
|
2014-12-17 05:51:32 +00:00
|
|
|
}
|
2015-02-05 09:35:58 +00:00
|
|
|
}
|
2014-12-30 10:05:45 +00:00
|
|
|
|
2019-11-19 13:17:48 +00:00
|
|
|
me.apply_rule_on_other_items(items_rule_dict);
|
|
|
|
|
2015-02-05 09:35:58 +00:00
|
|
|
if(!price_list_rate_changed) me.calculate_taxes_and_totals();
|
2014-07-01 12:15:15 +00:00
|
|
|
},
|
|
|
|
|
2019-11-19 13:17:48 +00:00
|
|
|
apply_rule_on_other_items: function(args) {
|
|
|
|
const me = this;
|
2020-04-14 07:23:54 +00:00
|
|
|
const fields = ["discount_percentage", "pricing_rules", "discount_amount", "rate"];
|
2019-11-19 13:17:48 +00:00
|
|
|
|
|
|
|
for(var k in args) {
|
|
|
|
let data = args[k];
|
2020-04-24 11:11:48 +00:00
|
|
|
|
2020-04-14 07:23:54 +00:00
|
|
|
if (data && data.apply_rule_on_other_items) {
|
|
|
|
me.frm.doc.items.forEach(d => {
|
|
|
|
if (in_list(data.apply_rule_on_other_items, d[data.apply_rule_on])) {
|
|
|
|
for(var k in data) {
|
2020-04-24 09:21:14 +00:00
|
|
|
if (in_list(fields, k) && data[k] && (data.price_or_product_discount === 'price' || k === 'pricing_rules')) {
|
2020-04-14 07:23:54 +00:00
|
|
|
frappe.model.set_value(d.doctype, d.name, k, data[k]);
|
|
|
|
}
|
2019-11-19 13:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-14 07:23:54 +00:00
|
|
|
});
|
|
|
|
}
|
2019-11-19 13:17:48 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-12-13 10:48:38 +00:00
|
|
|
apply_product_discount: function(free_item_data) {
|
|
|
|
const items = this.frm.doc.items.filter(d => (d.item_code == free_item_data.item_code
|
|
|
|
&& d.is_free_item)) || [];
|
|
|
|
|
|
|
|
if (!items.length) {
|
|
|
|
let row_to_modify = frappe.model.add_child(this.frm.doc,
|
|
|
|
this.frm.doc.doctype + ' Item', 'items');
|
|
|
|
|
|
|
|
for (let key in free_item_data) {
|
|
|
|
row_to_modify[key] = free_item_data[key];
|
|
|
|
}
|
2020-04-20 18:49:19 +00:00
|
|
|
} if (items && items.length && free_item_data) {
|
|
|
|
items[0].qty = free_item_data.qty
|
2019-12-13 10:48:38 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-12 09:48:57 +00:00
|
|
|
apply_price_list: function(item, reset_plc_conversion) {
|
|
|
|
// We need to reset plc_conversion_rate sometimes because the call to
|
|
|
|
// `erpnext.stock.get_item_details.apply_price_list` is sensitive to its value
|
|
|
|
if (!reset_plc_conversion) {
|
|
|
|
this.frm.set_value("plc_conversion_rate", "");
|
|
|
|
}
|
|
|
|
|
2014-07-01 12:15:15 +00:00
|
|
|
var me = this;
|
2015-01-30 11:27:24 +00:00
|
|
|
var args = this._get_args(item);
|
2016-01-15 11:29:26 +00:00
|
|
|
if (!((args.items && args.items.length) || args.price_list)) {
|
2015-11-27 09:07:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-02-23 10:31:33 +00:00
|
|
|
|
2017-11-14 04:02:32 +00:00
|
|
|
if (me.in_apply_price_list == true) return;
|
|
|
|
|
|
|
|
me.in_apply_price_list = true;
|
2014-07-01 12:15:15 +00:00
|
|
|
return this.frm.call({
|
|
|
|
method: "erpnext.stock.get_item_details.apply_price_list",
|
2015-01-30 11:27:24 +00:00
|
|
|
args: { args: args },
|
2014-07-01 12:15:15 +00:00
|
|
|
callback: function(r) {
|
|
|
|
if (!r.exc) {
|
2017-11-14 04:02:32 +00:00
|
|
|
frappe.run_serially([
|
|
|
|
() => me.frm.set_value("price_list_currency", r.message.parent.price_list_currency),
|
|
|
|
() => me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate),
|
|
|
|
() => {
|
|
|
|
if(args.items.length) {
|
|
|
|
me._set_values_for_item_list(r.message.children);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
() => { me.in_apply_price_list = false; }
|
|
|
|
]);
|
2014-07-01 12:15:15 +00:00
|
|
|
|
2017-11-14 04:02:32 +00:00
|
|
|
} else {
|
|
|
|
me.in_apply_price_list = false;
|
2013-05-24 13:55:01 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-14 10:16:45 +00:00
|
|
|
}).always(() => {
|
|
|
|
me.in_apply_price_list = false;
|
2014-07-01 12:15:15 +00:00
|
|
|
});
|
2013-05-24 13:55:01 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2019-11-19 13:17:48 +00:00
|
|
|
remove_pricing_rule: function(item) {
|
2019-03-31 11:12:41 +00:00
|
|
|
let me = this;
|
2020-01-02 11:59:50 +00:00
|
|
|
const fields = ["discount_percentage",
|
|
|
|
"discount_amount", "margin_rate_or_amount", "rate_with_margin"];
|
2019-03-18 09:04:19 +00:00
|
|
|
|
2019-11-19 13:17:48 +00:00
|
|
|
if(item.remove_free_item) {
|
2019-03-18 09:04:19 +00:00
|
|
|
var items = [];
|
|
|
|
|
|
|
|
me.frm.doc.items.forEach(d => {
|
|
|
|
if(d.item_code != item.remove_free_item || !d.is_free_item) {
|
|
|
|
items.push(d);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
me.frm.doc.items = items;
|
|
|
|
refresh_field('items');
|
2019-03-31 11:12:41 +00:00
|
|
|
} else if(item.applied_on_items && item.apply_on) {
|
|
|
|
const applied_on_items = item.applied_on_items.split(',');
|
|
|
|
me.frm.doc.items.forEach(row => {
|
|
|
|
if(applied_on_items.includes(row[item.apply_on])) {
|
|
|
|
fields.forEach(f => {
|
|
|
|
row[f] = 0;
|
|
|
|
});
|
2020-01-02 11:59:50 +00:00
|
|
|
|
|
|
|
["pricing_rules", "margin_type"].forEach(field => {
|
|
|
|
if (row[field]) {
|
|
|
|
row[field] = '';
|
|
|
|
}
|
|
|
|
})
|
2019-03-31 11:12:41 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
me.trigger_price_list_rate();
|
2019-03-18 09:04:19 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-03-31 11:12:41 +00:00
|
|
|
trigger_price_list_rate: function() {
|
|
|
|
var me = this;
|
|
|
|
|
|
|
|
this.frm.doc.items.forEach(child_row => {
|
|
|
|
me.frm.script_manager.trigger("price_list_rate",
|
|
|
|
child_row.doctype, child_row.name);
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2014-02-12 09:23:18 +00:00
|
|
|
validate_company_and_party: function() {
|
2013-05-28 11:53:36 +00:00
|
|
|
var me = this;
|
2013-05-24 13:55:01 +00:00
|
|
|
var valid = true;
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2014-02-20 05:57:47 +00:00
|
|
|
$.each(["company", "customer"], function(i, fieldname) {
|
2017-06-02 08:27:08 +00:00
|
|
|
if(frappe.meta.has_field(me.frm.doc.doctype, fieldname) && me.frm.doc.doctype != "Purchase Order") {
|
2014-02-12 09:23:18 +00:00
|
|
|
if (!me.frm.doc[fieldname]) {
|
2017-05-30 07:24:42 +00:00
|
|
|
frappe.msgprint(__("Please specify") + ": " +
|
2014-04-15 13:10:00 +00:00
|
|
|
frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
|
2014-04-14 10:55:30 +00:00
|
|
|
". " + __("It is needed to fetch Item Details."));
|
2017-05-30 07:24:42 +00:00
|
|
|
valid = false;
|
2014-02-12 09:23:18 +00:00
|
|
|
}
|
2013-05-24 13:55:01 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return valid;
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-06-05 08:41:32 +00:00
|
|
|
get_terms: function() {
|
|
|
|
var me = this;
|
2017-05-26 16:02:33 +00:00
|
|
|
|
|
|
|
erpnext.utils.get_terms(this.frm.doc.tc_name, this.frm.doc, function(r) {
|
|
|
|
if(!r.exc) {
|
|
|
|
me.frm.set_value("terms", r.message);
|
|
|
|
}
|
|
|
|
});
|
2013-06-05 08:41:32 +00:00
|
|
|
},
|
2014-01-29 11:01:38 +00:00
|
|
|
|
|
|
|
taxes_and_charges: function() {
|
|
|
|
var me = this;
|
|
|
|
if(this.frm.doc.taxes_and_charges) {
|
|
|
|
return this.frm.call({
|
2014-05-02 10:15:10 +00:00
|
|
|
method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
|
|
|
|
args: {
|
2014-05-13 11:08:31 +00:00
|
|
|
"master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
|
|
|
|
this.frm.doc.name).options,
|
2019-04-11 13:05:15 +00:00
|
|
|
"master_name": this.frm.doc.taxes_and_charges
|
2014-05-02 10:15:10 +00:00
|
|
|
},
|
2014-01-29 11:01:38 +00:00
|
|
|
callback: function(r) {
|
|
|
|
if(!r.exc) {
|
2019-07-15 13:27:25 +00:00
|
|
|
if(me.frm.doc.shipping_rule && me.frm.doc.taxes) {
|
|
|
|
for (let tax of r.message) {
|
|
|
|
me.frm.add_child("taxes", tax);
|
|
|
|
}
|
2019-04-11 13:05:15 +00:00
|
|
|
|
2019-07-15 13:27:25 +00:00
|
|
|
refresh_field("taxes");
|
2019-04-11 13:05:15 +00:00
|
|
|
} else {
|
2019-07-15 13:27:25 +00:00
|
|
|
me.frm.set_value("taxes", r.message);
|
2019-04-11 13:05:15 +00:00
|
|
|
me.calculate_taxes_and_totals();
|
2019-03-11 09:18:26 +00:00
|
|
|
}
|
2014-01-29 11:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-05-02 11:33:18 +00:00
|
|
|
}
|
2014-01-29 11:01:38 +00:00
|
|
|
},
|
|
|
|
|
2018-12-26 21:11:07 +00:00
|
|
|
tax_category: function() {
|
|
|
|
var me = this;
|
2018-12-28 20:49:11 +00:00
|
|
|
if(me.frm.updating_party_details) return;
|
|
|
|
|
2018-12-27 23:59:42 +00:00
|
|
|
frappe.run_serially([
|
2018-12-28 21:25:00 +00:00
|
|
|
() => this.update_item_tax_map(),
|
2018-12-27 23:59:42 +00:00
|
|
|
() => erpnext.utils.set_taxes(this.frm, "tax_category"),
|
|
|
|
]);
|
2018-12-26 21:11:07 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
item_tax_template: function(doc, cdt, cdn) {
|
|
|
|
var me = this;
|
2018-12-28 20:49:11 +00:00
|
|
|
if(me.frm.updating_party_details) return;
|
|
|
|
|
2018-12-26 21:11:07 +00:00
|
|
|
var item = frappe.get_doc(cdt, cdn);
|
|
|
|
|
2018-12-27 23:53:25 +00:00
|
|
|
if(item.item_tax_template) {
|
|
|
|
return this.frm.call({
|
|
|
|
method: "erpnext.stock.get_item_details.get_item_tax_map",
|
|
|
|
args: {
|
2018-12-28 21:25:00 +00:00
|
|
|
company: me.frm.doc.company,
|
2018-12-27 23:53:25 +00:00
|
|
|
item_tax_template: item.item_tax_template,
|
|
|
|
as_json: true
|
|
|
|
},
|
|
|
|
callback: function(r) {
|
|
|
|
if(!r.exc) {
|
|
|
|
item.item_tax_rate = r.message;
|
|
|
|
me.add_taxes_from_item_tax_template(item.item_tax_rate);
|
|
|
|
me.calculate_taxes_and_totals();
|
|
|
|
}
|
2018-12-26 21:11:07 +00:00
|
|
|
}
|
2018-12-27 23:53:25 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
item.item_tax_rate = "{}";
|
|
|
|
me.calculate_taxes_and_totals();
|
|
|
|
}
|
2018-12-26 21:11:07 +00:00
|
|
|
},
|
|
|
|
|
2018-12-28 21:25:00 +00:00
|
|
|
update_item_tax_map: function() {
|
|
|
|
var me = this;
|
|
|
|
var item_codes = [];
|
|
|
|
$.each(this.frm.doc.items || [], function(i, item) {
|
|
|
|
if(item.item_code) {
|
|
|
|
item_codes.push(item.item_code);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if(item_codes.length) {
|
|
|
|
return this.frm.call({
|
|
|
|
method: "erpnext.stock.get_item_details.get_item_tax_info",
|
|
|
|
args: {
|
|
|
|
company: me.frm.doc.company,
|
|
|
|
tax_category: cstr(me.frm.doc.tax_category),
|
|
|
|
item_codes: item_codes
|
|
|
|
},
|
|
|
|
callback: function(r) {
|
|
|
|
if(!r.exc) {
|
|
|
|
$.each(me.frm.doc.items || [], function(i, item) {
|
|
|
|
if(item.item_code && r.message.hasOwnProperty(item.item_code)) {
|
2020-05-22 06:58:45 +00:00
|
|
|
if (!item.item_tax_template) {
|
|
|
|
item.item_tax_template = r.message[item.item_code].item_tax_template;
|
|
|
|
item.item_tax_rate = r.message[item.item_code].item_tax_rate;
|
|
|
|
}
|
2018-12-28 21:25:00 +00:00
|
|
|
me.add_taxes_from_item_tax_template(item.item_tax_rate);
|
|
|
|
} else {
|
|
|
|
item.item_tax_template = "";
|
|
|
|
item.item_tax_rate = "{}";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
me.calculate_taxes_and_totals();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-09-21 14:15:49 +00:00
|
|
|
is_recurring: function() {
|
|
|
|
// set default values for recurring documents
|
2016-03-03 07:30:17 +00:00
|
|
|
if(this.frm.doc.is_recurring && this.frm.doc.__islocal) {
|
|
|
|
frappe.msgprint(__("Please set recurring after saving"));
|
|
|
|
this.frm.set_value('is_recurring', 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-21 14:15:49 +00:00
|
|
|
if(this.frm.doc.is_recurring) {
|
2016-03-21 06:33:16 +00:00
|
|
|
if(!this.frm.doc.recurring_id) {
|
|
|
|
this.frm.set_value('recurring_id', this.frm.doc.name);
|
|
|
|
}
|
2016-03-29 12:22:22 +00:00
|
|
|
|
2014-09-21 14:15:49 +00:00
|
|
|
var owner_email = this.frm.doc.owner=="Administrator"
|
|
|
|
? frappe.user_info("Administrator").email
|
|
|
|
: this.frm.doc.owner;
|
|
|
|
|
|
|
|
this.frm.doc.notification_email_address = $.map([cstr(owner_email),
|
|
|
|
cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", ");
|
|
|
|
this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate();
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh_many(["notification_email_address", "repeat_on_day_of_month"]);
|
|
|
|
},
|
|
|
|
|
|
|
|
from_date: function() {
|
|
|
|
// set to_date
|
|
|
|
if(this.frm.doc.from_date) {
|
|
|
|
var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6,
|
|
|
|
'Yearly': 12};
|
|
|
|
|
|
|
|
var months = recurring_type_map[this.frm.doc.recurring_type];
|
|
|
|
if(months) {
|
|
|
|
var to_date = frappe.datetime.add_months(this.frm.doc.from_date,
|
|
|
|
months);
|
|
|
|
this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1);
|
|
|
|
refresh_field('to_date');
|
|
|
|
}
|
|
|
|
}
|
2016-02-25 13:29:20 +00:00
|
|
|
},
|
2016-03-29 12:22:22 +00:00
|
|
|
|
2016-02-26 05:39:20 +00:00
|
|
|
set_gross_profit: function(item) {
|
2020-05-26 13:07:02 +00:00
|
|
|
if (["Sales Order", "Quotation"].includes(this.frm.doc.doctype) && item.valuation_rate) {
|
2017-05-30 07:24:42 +00:00
|
|
|
var rate = flt(item.rate) * flt(this.frm.doc.conversion_rate || 1);
|
2017-02-10 08:57:11 +00:00
|
|
|
item.gross_profit = flt(((rate - item.valuation_rate) * item.stock_qty), precision("amount", item));
|
2016-02-26 05:39:20 +00:00
|
|
|
}
|
2016-04-07 09:55:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setup_item_selector: function() {
|
2016-05-29 17:38:22 +00:00
|
|
|
// TODO: remove item selector
|
|
|
|
|
|
|
|
return;
|
2017-05-30 07:24:42 +00:00
|
|
|
// if(!this.item_selector) {
|
|
|
|
// this.item_selector = new erpnext.ItemSelector({frm: this.frm});
|
|
|
|
// }
|
2016-06-27 12:11:39 +00:00
|
|
|
},
|
2016-07-18 09:00:18 +00:00
|
|
|
|
2016-06-27 12:11:39 +00:00
|
|
|
get_advances: function() {
|
|
|
|
if(!this.frm.is_return) {
|
|
|
|
return this.frm.call({
|
|
|
|
method: "set_advances",
|
|
|
|
doc: this.frm.doc,
|
|
|
|
callback: function(r, rt) {
|
|
|
|
refresh_field("advances");
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-06-28 14:12:19 +00:00
|
|
|
},
|
2016-07-18 09:00:18 +00:00
|
|
|
|
2016-06-28 14:12:19 +00:00
|
|
|
make_payment_entry: function() {
|
|
|
|
return frappe.call({
|
2016-10-05 10:54:41 +00:00
|
|
|
method: cur_frm.cscript.get_method_for_payment(),
|
2016-06-28 14:12:19 +00:00
|
|
|
args: {
|
|
|
|
"dt": cur_frm.doc.doctype,
|
|
|
|
"dn": cur_frm.doc.name
|
|
|
|
},
|
|
|
|
callback: function(r) {
|
|
|
|
var doclist = frappe.model.sync(r.message);
|
|
|
|
frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
|
2016-07-01 10:28:39 +00:00
|
|
|
// cur_frm.refresh_fields()
|
2016-06-28 14:12:19 +00:00
|
|
|
}
|
|
|
|
});
|
2016-10-05 10:54:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
get_method_for_payment: function(){
|
2017-11-17 09:01:09 +00:00
|
|
|
var method = "erpnext.accounts.doctype.payment_entry.payment_entry.get_payment_entry";
|
2016-10-06 09:05:04 +00:00
|
|
|
if(cur_frm.doc.__onload && cur_frm.doc.__onload.make_payment_via_journal_entry){
|
2016-10-05 10:54:41 +00:00
|
|
|
if(in_list(['Sales Invoice', 'Purchase Invoice'], cur_frm.doc.doctype)){
|
2017-11-17 09:01:09 +00:00
|
|
|
method = "erpnext.accounts.doctype.journal_entry.journal_entry.get_payment_entry_against_invoice";
|
2016-10-05 10:54:41 +00:00
|
|
|
}else {
|
2017-11-17 09:01:09 +00:00
|
|
|
method= "erpnext.accounts.doctype.journal_entry.journal_entry.get_payment_entry_against_order";
|
2016-10-05 10:54:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return method
|
2016-11-10 13:45:11 +00:00
|
|
|
},
|
2017-06-02 11:58:40 +00:00
|
|
|
|
|
|
|
set_query_for_batch: function(doc, cdt, cdn) {
|
|
|
|
// Show item's batches in the dropdown of batch no
|
|
|
|
|
|
|
|
var me = this;
|
|
|
|
var item = frappe.get_doc(cdt, cdn);
|
|
|
|
|
|
|
|
if(!item.item_code) {
|
|
|
|
frappe.throw(__("Please enter Item Code to get batch no"));
|
2017-06-20 04:00:53 +00:00
|
|
|
} else if (doc.doctype == "Purchase Receipt" ||
|
2017-06-02 11:58:40 +00:00
|
|
|
(doc.doctype == "Purchase Invoice" && doc.update_stock)) {
|
|
|
|
|
|
|
|
return {
|
|
|
|
filters: {'item': item.item_code}
|
|
|
|
}
|
|
|
|
} else {
|
2017-11-14 04:02:32 +00:00
|
|
|
let filters = {
|
2017-06-02 11:58:40 +00:00
|
|
|
'item_code': item.item_code,
|
|
|
|
'posting_date': me.frm.doc.posting_date || frappe.datetime.nowdate(),
|
|
|
|
}
|
2019-10-22 08:33:27 +00:00
|
|
|
|
|
|
|
if (doc.is_return) {
|
|
|
|
filters["is_return"] = 1;
|
|
|
|
}
|
|
|
|
|
2017-11-17 09:01:09 +00:00
|
|
|
if (item.warehouse) filters["warehouse"] = item.warehouse;
|
2017-06-02 11:58:40 +00:00
|
|
|
|
2017-11-17 14:38:15 +00:00
|
|
|
return {
|
2017-06-02 11:58:40 +00:00
|
|
|
query : "erpnext.controllers.queries.get_batch_no",
|
|
|
|
filters: filters
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2017-08-15 02:53:51 +00:00
|
|
|
|
2020-01-06 10:04:15 +00:00
|
|
|
set_query_for_item_tax_template: function(doc, cdt, cdn) {
|
|
|
|
var item = frappe.get_doc(cdt, cdn);
|
|
|
|
if(!item.item_code) {
|
|
|
|
frappe.throw(__("Please enter Item Code to get item taxes"));
|
|
|
|
} else {
|
|
|
|
|
|
|
|
let filters = {
|
|
|
|
'item_code': item.item_code,
|
2020-08-10 09:18:13 +00:00
|
|
|
'valid_from': ["<=", doc.transaction_date || doc.bill_date || doc.posting_date],
|
2020-01-06 10:04:15 +00:00
|
|
|
'item_group': item.item_group,
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doc.tax_category)
|
|
|
|
filters['tax_category'] = doc.tax_category;
|
2020-06-18 06:18:44 +00:00
|
|
|
if (doc.company)
|
|
|
|
filters['company'] = doc.company;
|
2020-01-06 10:04:15 +00:00
|
|
|
return {
|
|
|
|
query: "erpnext.controllers.queries.get_tax_template",
|
|
|
|
filters: filters
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-08-15 02:53:51 +00:00
|
|
|
payment_terms_template: function() {
|
|
|
|
var me = this;
|
2018-01-05 07:16:50 +00:00
|
|
|
const doc = this.frm.doc;
|
|
|
|
if(doc.payment_terms_template && doc.doctype !== 'Delivery Note') {
|
2018-02-16 07:35:21 +00:00
|
|
|
var posting_date = doc.posting_date || doc.transaction_date;
|
2017-08-15 02:53:51 +00:00
|
|
|
frappe.call({
|
|
|
|
method: "erpnext.controllers.accounts_controller.get_payment_terms",
|
|
|
|
args: {
|
2018-01-05 07:16:50 +00:00
|
|
|
terms_template: doc.payment_terms_template,
|
2018-01-17 10:53:04 +00:00
|
|
|
posting_date: posting_date,
|
2018-02-16 07:35:21 +00:00
|
|
|
grand_total: doc.rounded_total || doc.grand_total,
|
|
|
|
bill_date: doc.bill_date
|
2017-08-15 02:53:51 +00:00
|
|
|
},
|
|
|
|
callback: function(r) {
|
|
|
|
if(r.message && !r.exc) {
|
|
|
|
me.frm.set_value("payment_schedule", r.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
payment_term: function(doc, cdt, cdn) {
|
|
|
|
var row = locals[cdt][cdn];
|
|
|
|
if(row.payment_term) {
|
|
|
|
frappe.call({
|
|
|
|
method: "erpnext.controllers.accounts_controller.get_payment_term_details",
|
|
|
|
args: {
|
|
|
|
term: row.payment_term,
|
2018-02-16 07:35:21 +00:00
|
|
|
bill_date: this.frm.doc.bill_date,
|
2017-08-15 02:53:51 +00:00
|
|
|
posting_date: this.frm.doc.posting_date || this.frm.doc.transaction_date,
|
2017-11-21 14:28:16 +00:00
|
|
|
grand_total: this.frm.doc.rounded_total || this.frm.doc.grand_total
|
2017-08-15 02:53:51 +00:00
|
|
|
},
|
|
|
|
callback: function(r) {
|
|
|
|
if(r.message && !r.exc) {
|
|
|
|
for (var d in r.message) {
|
|
|
|
frappe.model.set_value(cdt, cdn, d, r.message[d]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-05-28 14:37:08 +00:00
|
|
|
},
|
|
|
|
|
2019-12-04 10:37:11 +00:00
|
|
|
against_blanket_order: function(doc, cdt, cdn) {
|
|
|
|
var item = locals[cdt][cdn];
|
|
|
|
if(!item.against_blanket_order) {
|
|
|
|
frappe.model.set_value(this.frm.doctype + " Item", item.name, "blanket_order", null);
|
|
|
|
frappe.model.set_value(this.frm.doctype + " Item", item.name, "blanket_order_rate", 0.00);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-05-28 14:37:08 +00:00
|
|
|
blanket_order: function(doc, cdt, cdn) {
|
|
|
|
var me = this;
|
|
|
|
var item = locals[cdt][cdn];
|
|
|
|
if (item.blanket_order && (item.parenttype=="Sales Order" || item.parenttype=="Purchase Order")) {
|
|
|
|
frappe.call({
|
|
|
|
method: "erpnext.stock.get_item_details.get_blanket_order_details",
|
|
|
|
args: {
|
|
|
|
args:{
|
|
|
|
item_code: item.item_code,
|
|
|
|
customer: doc.customer,
|
|
|
|
supplier: doc.supplier,
|
|
|
|
company: doc.company,
|
|
|
|
transaction_date: doc.transaction_date,
|
|
|
|
blanket_order: item.blanket_order
|
|
|
|
}
|
|
|
|
},
|
|
|
|
callback: function(r) {
|
|
|
|
if (!r.message) {
|
2018-05-29 07:26:14 +00:00
|
|
|
frappe.throw(__("Invalid Blanket Order for the selected Customer and Item"));
|
2018-05-28 14:37:08 +00:00
|
|
|
} else {
|
|
|
|
frappe.run_serially([
|
|
|
|
() => frappe.model.set_value(cdt, cdn, "blanket_order_rate", r.message.blanket_order_rate),
|
|
|
|
() => me.frm.script_manager.trigger("price_list_rate", cdt, cdn)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-11-13 07:07:53 +00:00
|
|
|
},
|
|
|
|
|
2019-12-23 12:54:56 +00:00
|
|
|
set_reserve_warehouse: function() {
|
2020-04-22 18:48:21 +00:00
|
|
|
this.autofill_warehouse(this.frm.doc.supplied_items, "reserve_warehouse", this.frm.doc.set_reserve_warehouse);
|
2019-12-23 12:54:56 +00:00
|
|
|
},
|
|
|
|
|
2018-11-13 07:07:53 +00:00
|
|
|
set_warehouse: function() {
|
2020-04-22 18:48:21 +00:00
|
|
|
this.autofill_warehouse(this.frm.doc.items, "warehouse", this.frm.doc.set_warehouse);
|
2019-12-23 12:54:56 +00:00
|
|
|
},
|
|
|
|
|
2021-01-28 07:39:56 +00:00
|
|
|
set_target_warehouse: function() {
|
|
|
|
this.autofill_warehouse(this.frm.doc.items, "target_warehouse", this.frm.doc.set_target_warehouse);
|
|
|
|
},
|
|
|
|
|
|
|
|
set_from_warehouse: function() {
|
|
|
|
this.autofill_warehouse(this.frm.doc.items, "from_warehouse", this.frm.doc.set_from_warehouse);
|
|
|
|
},
|
|
|
|
|
2020-04-22 18:48:21 +00:00
|
|
|
autofill_warehouse : function (child_table, warehouse_field, warehouse) {
|
2020-04-28 13:48:45 +00:00
|
|
|
if (warehouse && child_table && child_table.length) {
|
|
|
|
let doctype = child_table[0].doctype;
|
2019-12-23 12:54:56 +00:00
|
|
|
$.each(child_table || [], function(i, item) {
|
|
|
|
frappe.model.set_value(doctype, item.name, warehouse_field, warehouse);
|
2018-11-13 07:07:53 +00:00
|
|
|
});
|
|
|
|
}
|
2019-08-12 08:09:25 +00:00
|
|
|
},
|
2019-12-23 12:54:56 +00:00
|
|
|
|
2019-08-12 08:09:25 +00:00
|
|
|
coupon_code: function() {
|
|
|
|
var me = this;
|
|
|
|
frappe.run_serially([
|
|
|
|
() => this.frm.doc.ignore_pricing_rule=1,
|
|
|
|
() => me.ignore_pricing_rule(),
|
|
|
|
() => this.frm.doc.ignore_pricing_rule=0,
|
|
|
|
() => me.apply_pricing_rule()
|
|
|
|
]);
|
2017-08-15 02:53:51 +00:00
|
|
|
}
|
2017-05-06 07:15:16 +00:00
|
|
|
});
|
2017-06-26 10:01:46 +00:00
|
|
|
|
2019-12-24 12:49:58 +00:00
|
|
|
erpnext.show_serial_batch_selector = function (frm, d, callback, on_close, show_dialog) {
|
|
|
|
let warehouse, receiving_stock, existing_stock;
|
|
|
|
if (frm.doc.is_return) {
|
|
|
|
if (["Purchase Receipt", "Purchase Invoice"].includes(frm.doc.doctype)) {
|
|
|
|
existing_stock = true;
|
|
|
|
warehouse = d.warehouse;
|
|
|
|
} else if (["Delivery Note", "Sales Invoice"].includes(frm.doc.doctype)) {
|
|
|
|
receiving_stock = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (frm.doc.doctype == "Stock Entry") {
|
|
|
|
if (frm.doc.purpose == "Material Receipt") {
|
|
|
|
receiving_stock = true;
|
|
|
|
} else {
|
|
|
|
existing_stock = true;
|
|
|
|
warehouse = d.s_warehouse;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
existing_stock = true;
|
|
|
|
warehouse = d.warehouse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!warehouse) {
|
|
|
|
if (receiving_stock) {
|
|
|
|
warehouse = ["like", ""];
|
|
|
|
} else if (existing_stock) {
|
|
|
|
warehouse = ["!=", ""];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-26 10:01:46 +00:00
|
|
|
frappe.require("assets/erpnext/js/utils/serial_no_batch_selector.js", function() {
|
2017-06-26 12:15:49 +00:00
|
|
|
new erpnext.SerialNoBatchSelector({
|
2017-06-26 10:01:46 +00:00
|
|
|
frm: frm,
|
|
|
|
item: d,
|
|
|
|
warehouse_details: {
|
|
|
|
type: "Warehouse",
|
2019-12-24 12:49:58 +00:00
|
|
|
name: warehouse
|
2017-06-26 10:01:46 +00:00
|
|
|
},
|
2017-12-14 10:36:23 +00:00
|
|
|
callback: callback,
|
|
|
|
on_close: on_close
|
2017-09-01 09:58:44 +00:00
|
|
|
}, show_dialog);
|
2017-06-26 10:01:46 +00:00
|
|
|
});
|
|
|
|
}
|
2020-12-08 13:41:51 +00:00
|
|
|
|
2020-12-14 06:18:04 +00:00
|
|
|
erpnext.apply_putaway_rule = (frm, purpose=null) => {
|
2020-12-08 13:41:51 +00:00
|
|
|
if (!frm.doc.company) {
|
|
|
|
frappe.throw({message: __("Please select a Company first."), title: __("Mandatory")});
|
|
|
|
}
|
|
|
|
if (!frm.doc.items.length) return;
|
|
|
|
|
|
|
|
frappe.call({
|
|
|
|
method: "erpnext.stock.doctype.putaway_rule.putaway_rule.apply_putaway_rule",
|
|
|
|
args: {
|
|
|
|
doctype: frm.doctype,
|
|
|
|
items: frm.doc.items,
|
|
|
|
company: frm.doc.company,
|
2020-12-14 06:18:04 +00:00
|
|
|
sync: true,
|
|
|
|
purpose: purpose
|
2020-12-08 13:41:51 +00:00
|
|
|
},
|
|
|
|
callback: (result) => {
|
|
|
|
if (!result.exc && result.message) {
|
|
|
|
frm.clear_table("items");
|
|
|
|
|
|
|
|
let items = result.message;
|
|
|
|
items.forEach((row) => {
|
2020-12-09 10:57:18 +00:00
|
|
|
delete row["name"]; // dont overwrite name from server side
|
2020-12-08 13:41:51 +00:00
|
|
|
let child = frm.add_child("items");
|
|
|
|
Object.assign(child, row);
|
2020-12-09 10:57:18 +00:00
|
|
|
frm.script_manager.trigger("qty", child.doctype, child.name);
|
2020-12-08 13:41:51 +00:00
|
|
|
});
|
|
|
|
frm.get_field("items").grid.refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|