2023-03-21 05:24:41 +00:00
|
|
|
erpnext.SerialBatchPackageSelector = class SerialNoBatchBundleUpdate {
|
2022-10-10 07:58:19 +00:00
|
|
|
constructor(frm, item, callback) {
|
|
|
|
this.frm = frm;
|
|
|
|
this.item = item;
|
|
|
|
this.qty = item.qty;
|
|
|
|
this.callback = callback;
|
2023-03-13 09:21:43 +00:00
|
|
|
this.bundle = this.item?.is_rejected ?
|
|
|
|
this.item.rejected_serial_and_batch_bundle : this.item.serial_and_batch_bundle;
|
|
|
|
|
2022-10-10 07:58:19 +00:00
|
|
|
this.make();
|
|
|
|
this.render_data();
|
|
|
|
}
|
|
|
|
|
|
|
|
make() {
|
2023-04-11 07:52:15 +00:00
|
|
|
let label = this.item?.has_serial_no ? __('Serial Nos') : __('Batch Nos');
|
2023-03-13 09:21:43 +00:00
|
|
|
let primary_label = this.bundle
|
2022-12-05 09:18:18 +00:00
|
|
|
? __('Update') : __('Add');
|
|
|
|
|
|
|
|
if (this.item?.has_serial_no && this.item?.batch_no) {
|
2023-04-11 07:52:15 +00:00
|
|
|
label = __('Serial Nos / Batch Nos');
|
2022-12-05 09:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
primary_label += ' ' + label;
|
|
|
|
|
2022-10-10 07:58:19 +00:00
|
|
|
this.dialog = new frappe.ui.Dialog({
|
2022-12-05 09:18:18 +00:00
|
|
|
title: this.item?.title || primary_label,
|
2022-10-10 07:58:19 +00:00
|
|
|
fields: this.get_dialog_fields(),
|
2022-12-05 09:18:18 +00:00
|
|
|
primary_action_label: primary_label,
|
2023-06-15 07:24:43 +00:00
|
|
|
primary_action: () => this.update_bundle_entries(),
|
2023-04-11 07:52:15 +00:00
|
|
|
secondary_action_label: __('Edit Full Form'),
|
|
|
|
secondary_action: () => this.edit_full_form(),
|
2022-10-10 07:58:19 +00:00
|
|
|
});
|
2022-12-05 09:18:18 +00:00
|
|
|
|
2023-04-07 05:51:01 +00:00
|
|
|
this.dialog.set_value("qty", this.item.qty);
|
2022-10-10 07:58:19 +00:00
|
|
|
this.dialog.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
get_serial_no_filters() {
|
2023-06-15 07:24:43 +00:00
|
|
|
let warehouse = this.item?.type_of_transaction === "Outward" ?
|
2023-03-13 09:21:43 +00:00
|
|
|
(this.item.warehouse || this.item.s_warehouse) : "";
|
2022-12-05 09:18:18 +00:00
|
|
|
|
2022-10-10 07:58:19 +00:00
|
|
|
return {
|
|
|
|
'item_code': this.item.item_code,
|
2022-12-05 09:18:18 +00:00
|
|
|
'warehouse': ["=", warehouse]
|
2022-10-10 07:58:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
get_dialog_fields() {
|
|
|
|
let fields = [];
|
|
|
|
|
2023-06-04 10:39:01 +00:00
|
|
|
fields.push({
|
|
|
|
fieldtype: 'Link',
|
|
|
|
fieldname: 'warehouse',
|
|
|
|
label: __('Warehouse'),
|
|
|
|
options: 'Warehouse',
|
|
|
|
default: this.get_warehouse(),
|
|
|
|
onchange: () => {
|
|
|
|
this.item.warehouse = this.dialog.get_value('warehouse');
|
|
|
|
this.get_auto_data()
|
|
|
|
},
|
|
|
|
get_query: () => {
|
|
|
|
return {
|
|
|
|
filters: {
|
|
|
|
'is_group': 0,
|
|
|
|
'company': this.frm.doc.company,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-06-05 11:26:29 +00:00
|
|
|
if (this.frm.doc.doctype === 'Stock Entry'
|
|
|
|
&& this.frm.doc.purpose === 'Manufacture') {
|
|
|
|
fields.push({
|
|
|
|
fieldtype: 'Column Break',
|
|
|
|
});
|
|
|
|
|
|
|
|
fields.push({
|
|
|
|
fieldtype: 'Link',
|
|
|
|
fieldname: 'work_order',
|
|
|
|
label: __('For Work Order'),
|
|
|
|
options: 'Work Order',
|
|
|
|
read_only: 1,
|
|
|
|
default: this.frm.doc.work_order,
|
|
|
|
});
|
|
|
|
|
|
|
|
fields.push({
|
|
|
|
fieldtype: 'Section Break',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-04 10:39:01 +00:00
|
|
|
fields.push({
|
|
|
|
fieldtype: 'Column Break',
|
|
|
|
});
|
|
|
|
|
2022-10-10 07:58:19 +00:00
|
|
|
if (this.item.has_serial_no) {
|
|
|
|
fields.push({
|
2023-04-11 07:52:15 +00:00
|
|
|
fieldtype: 'Data',
|
2022-10-10 07:58:19 +00:00
|
|
|
fieldname: 'scan_serial_no',
|
|
|
|
label: __('Scan Serial No'),
|
|
|
|
get_query: () => {
|
|
|
|
return {
|
|
|
|
filters: this.get_serial_no_filters()
|
|
|
|
};
|
|
|
|
},
|
|
|
|
onchange: () => this.update_serial_batch_no()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.item.has_batch_no && this.item.has_serial_no) {
|
|
|
|
fields.push({
|
|
|
|
fieldtype: 'Column Break',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.item.has_batch_no) {
|
|
|
|
fields.push({
|
2023-05-27 13:48:03 +00:00
|
|
|
fieldtype: 'Data',
|
2022-10-10 07:58:19 +00:00
|
|
|
fieldname: 'scan_batch_no',
|
|
|
|
label: __('Scan Batch No'),
|
|
|
|
onchange: () => this.update_serial_batch_no()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-15 07:24:43 +00:00
|
|
|
if (this.item?.type_of_transaction === "Outward") {
|
2023-04-04 06:20:38 +00:00
|
|
|
fields = [...this.get_filter_fields(), ...fields];
|
2023-05-27 13:48:03 +00:00
|
|
|
} else {
|
|
|
|
fields = [...fields, ...this.get_attach_field()];
|
2022-10-10 07:58:19 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 09:18:18 +00:00
|
|
|
fields.push({
|
|
|
|
fieldtype: 'Section Break',
|
|
|
|
});
|
|
|
|
|
2022-10-10 07:58:19 +00:00
|
|
|
fields.push({
|
2023-03-21 05:24:41 +00:00
|
|
|
fieldname: 'entries',
|
2022-10-10 07:58:19 +00:00
|
|
|
fieldtype: 'Table',
|
|
|
|
allow_bulk_edit: true,
|
|
|
|
data: [],
|
|
|
|
fields: this.get_dialog_table_fields(),
|
|
|
|
});
|
|
|
|
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
2023-05-27 13:48:03 +00:00
|
|
|
get_attach_field() {
|
|
|
|
let label = this.item?.has_serial_no ? __('Serial Nos') : __('Batch Nos');
|
|
|
|
let primary_label = this.bundle
|
|
|
|
? __('Update') : __('Add');
|
|
|
|
|
|
|
|
if (this.item?.has_serial_no && this.item?.has_batch_no) {
|
|
|
|
label = __('Serial Nos / Batch Nos');
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
fieldtype: 'Section Break',
|
|
|
|
label: __('{0} {1} via CSV File', [primary_label, label])
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fieldtype: 'Button',
|
|
|
|
fieldname: 'download_csv',
|
|
|
|
label: __('Download CSV Template'),
|
|
|
|
click: () => this.download_csv_file()
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fieldtype: 'Column Break',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fieldtype: 'Attach',
|
|
|
|
fieldname: 'attach_serial_batch_csv',
|
|
|
|
label: __('Attach CSV File'),
|
|
|
|
onchange: () => this.upload_csv_file()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
download_csv_file() {
|
|
|
|
let csvFileData = ['Serial No'];
|
|
|
|
|
|
|
|
if (this.item.has_serial_no && this.item.has_batch_no) {
|
|
|
|
csvFileData = ['Serial No', 'Batch No', 'Quantity'];
|
|
|
|
} else if (this.item.has_batch_no) {
|
|
|
|
csvFileData = ['Batch No', 'Quantity'];
|
|
|
|
}
|
|
|
|
|
|
|
|
const method = `/api/method/erpnext.stock.doctype.serial_and_batch_bundle.serial_and_batch_bundle.download_blank_csv_template?content=${encodeURIComponent(JSON.stringify(csvFileData))}`;
|
|
|
|
const w = window.open(frappe.urllib.get_full_url(method));
|
|
|
|
if (!w) {
|
|
|
|
frappe.msgprint(__("Please enable pop-ups"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
upload_csv_file() {
|
|
|
|
const file_path = this.dialog.get_value("attach_serial_batch_csv")
|
|
|
|
|
|
|
|
frappe.call({
|
|
|
|
method: 'erpnext.stock.doctype.serial_and_batch_bundle.serial_and_batch_bundle.upload_csv_file',
|
|
|
|
args: {
|
|
|
|
item_code: this.item.item_code,
|
|
|
|
file_path: file_path
|
|
|
|
},
|
|
|
|
callback: (r) => {
|
|
|
|
if (r.message.serial_nos && r.message.serial_nos.length) {
|
|
|
|
this.set_data(r.message.serial_nos);
|
|
|
|
} else if (r.message.batch_nos && r.message.batch_nos.length) {
|
|
|
|
this.set_data(r.message.batch_nos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-05 09:18:18 +00:00
|
|
|
get_filter_fields() {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
fieldtype: 'Section Break',
|
|
|
|
label: __('Auto Fetch')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fieldtype: 'Float',
|
|
|
|
fieldname: 'qty',
|
|
|
|
label: __('Qty to Fetch'),
|
2023-04-04 06:20:38 +00:00
|
|
|
onchange: () => this.get_auto_data()
|
2022-12-05 09:18:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
fieldtype: 'Column Break',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fieldtype: 'Select',
|
|
|
|
options: ['FIFO', 'LIFO', 'Expiry'],
|
|
|
|
default: 'FIFO',
|
|
|
|
fieldname: 'based_on',
|
2023-04-04 06:20:38 +00:00
|
|
|
label: __('Fetch Based On'),
|
|
|
|
onchange: () => this.get_auto_data()
|
2022-12-05 09:18:18 +00:00
|
|
|
},
|
|
|
|
{
|
2023-04-04 06:20:38 +00:00
|
|
|
fieldtype: 'Section Break',
|
2022-12-05 09:18:18 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-10-10 07:58:19 +00:00
|
|
|
get_dialog_table_fields() {
|
|
|
|
let fields = []
|
|
|
|
|
|
|
|
if (this.item.has_serial_no) {
|
|
|
|
fields.push({
|
|
|
|
fieldtype: 'Link',
|
|
|
|
options: 'Serial No',
|
|
|
|
fieldname: 'serial_no',
|
|
|
|
label: __('Serial No'),
|
|
|
|
in_list_view: 1,
|
|
|
|
get_query: () => {
|
|
|
|
return {
|
|
|
|
filters: this.get_serial_no_filters()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2022-12-05 09:18:18 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 09:21:43 +00:00
|
|
|
let batch_fields = []
|
2022-12-05 09:18:18 +00:00
|
|
|
if (this.item.has_batch_no) {
|
2023-03-13 09:21:43 +00:00
|
|
|
batch_fields = [
|
2022-10-10 07:58:19 +00:00
|
|
|
{
|
|
|
|
fieldtype: 'Link',
|
|
|
|
options: 'Batch',
|
|
|
|
fieldname: 'batch_no',
|
|
|
|
label: __('Batch No'),
|
|
|
|
in_list_view: 1,
|
2023-04-04 06:20:38 +00:00
|
|
|
get_query: () => {
|
2023-06-15 07:24:43 +00:00
|
|
|
if (this.item.type_of_transaction !== "Outward") {
|
2023-06-04 10:39:01 +00:00
|
|
|
return {
|
|
|
|
filters: {
|
|
|
|
'item': this.item.item_code,
|
|
|
|
}
|
2023-04-04 06:20:38 +00:00
|
|
|
}
|
2023-06-04 10:39:01 +00:00
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
query : "erpnext.controllers.queries.get_batch_no",
|
|
|
|
filters: {
|
|
|
|
'item_code': this.item.item_code,
|
|
|
|
'warehouse': this.get_warehouse()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-04 06:20:38 +00:00
|
|
|
},
|
2023-03-13 09:21:43 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
if (!this.item.has_serial_no) {
|
|
|
|
batch_fields.push({
|
2022-10-10 07:58:19 +00:00
|
|
|
fieldtype: 'Float',
|
|
|
|
fieldname: 'qty',
|
|
|
|
label: __('Quantity'),
|
|
|
|
in_list_view: 1,
|
2023-03-13 09:21:43 +00:00
|
|
|
})
|
|
|
|
}
|
2022-10-10 07:58:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 09:21:43 +00:00
|
|
|
fields = [...fields, ...batch_fields];
|
|
|
|
|
2022-10-10 07:58:19 +00:00
|
|
|
fields.push({
|
|
|
|
fieldtype: 'Data',
|
|
|
|
fieldname: 'name',
|
|
|
|
label: __('Name'),
|
|
|
|
hidden: 1,
|
2023-03-13 09:21:43 +00:00
|
|
|
});
|
2022-10-10 07:58:19 +00:00
|
|
|
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
2022-12-05 09:18:18 +00:00
|
|
|
get_auto_data() {
|
2023-06-04 10:39:01 +00:00
|
|
|
let { qty, based_on } = this.dialog.get_values();
|
2022-12-05 09:18:18 +00:00
|
|
|
|
2023-04-04 06:20:38 +00:00
|
|
|
if (!based_on) {
|
|
|
|
based_on = 'FIFO';
|
|
|
|
}
|
|
|
|
|
2023-06-04 10:39:01 +00:00
|
|
|
if (qty) {
|
|
|
|
frappe.call({
|
|
|
|
method: 'erpnext.stock.doctype.serial_and_batch_bundle.serial_and_batch_bundle.get_auto_data',
|
|
|
|
args: {
|
|
|
|
item_code: this.item.item_code,
|
|
|
|
warehouse: this.item.warehouse || this.item.s_warehouse,
|
|
|
|
has_serial_no: this.item.has_serial_no,
|
|
|
|
has_batch_no: this.item.has_batch_no,
|
|
|
|
qty: qty,
|
|
|
|
based_on: based_on
|
|
|
|
},
|
|
|
|
callback: (r) => {
|
|
|
|
if (r.message) {
|
|
|
|
this.dialog.fields_dict.entries.df.data = r.message;
|
|
|
|
this.dialog.fields_dict.entries.grid.refresh();
|
|
|
|
}
|
2022-12-05 09:18:18 +00:00
|
|
|
}
|
2023-06-04 10:39:01 +00:00
|
|
|
});
|
|
|
|
}
|
2022-12-05 09:18:18 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 07:58:19 +00:00
|
|
|
update_serial_batch_no() {
|
|
|
|
const { scan_serial_no, scan_batch_no } = this.dialog.get_values();
|
|
|
|
|
|
|
|
if (scan_serial_no) {
|
2023-03-21 05:24:41 +00:00
|
|
|
this.dialog.fields_dict.entries.df.data.push({
|
2022-10-10 07:58:19 +00:00
|
|
|
serial_no: scan_serial_no
|
|
|
|
});
|
|
|
|
|
|
|
|
this.dialog.fields_dict.scan_serial_no.set_value('');
|
|
|
|
} else if (scan_batch_no) {
|
2023-03-21 05:24:41 +00:00
|
|
|
this.dialog.fields_dict.entries.df.data.push({
|
2022-10-10 07:58:19 +00:00
|
|
|
batch_no: scan_batch_no
|
|
|
|
});
|
|
|
|
|
|
|
|
this.dialog.fields_dict.scan_batch_no.set_value('');
|
|
|
|
}
|
|
|
|
|
2023-03-21 05:24:41 +00:00
|
|
|
this.dialog.fields_dict.entries.grid.refresh();
|
2022-10-10 07:58:19 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 07:24:43 +00:00
|
|
|
update_bundle_entries() {
|
2023-03-21 05:24:41 +00:00
|
|
|
let entries = this.dialog.get_values().entries;
|
2023-06-04 10:39:01 +00:00
|
|
|
let warehouse = this.dialog.get_value('warehouse');
|
2022-10-10 07:58:19 +00:00
|
|
|
|
2023-03-21 05:24:41 +00:00
|
|
|
if (entries && !entries.length || !entries) {
|
|
|
|
frappe.throw(__('Please add atleast one Serial No / Batch No'));
|
2022-10-10 07:58:19 +00:00
|
|
|
}
|
2023-03-21 05:24:41 +00:00
|
|
|
|
|
|
|
frappe.call({
|
|
|
|
method: 'erpnext.stock.doctype.serial_and_batch_bundle.serial_and_batch_bundle.add_serial_batch_ledgers',
|
|
|
|
args: {
|
|
|
|
entries: entries,
|
|
|
|
child_row: this.item,
|
|
|
|
doc: this.frm.doc,
|
2023-06-04 10:39:01 +00:00
|
|
|
warehouse: warehouse,
|
2023-03-21 05:24:41 +00:00
|
|
|
}
|
|
|
|
}).then(r => {
|
|
|
|
this.callback && this.callback(r.message);
|
2023-03-24 04:44:09 +00:00
|
|
|
this.frm.save();
|
2023-03-21 05:24:41 +00:00
|
|
|
this.dialog.hide();
|
|
|
|
})
|
2022-10-10 07:58:19 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 07:52:15 +00:00
|
|
|
edit_full_form() {
|
|
|
|
let bundle_id = this.item.serial_and_batch_bundle
|
|
|
|
if (!bundle_id) {
|
2023-07-22 17:37:18 +00:00
|
|
|
let _new = frappe.model.get_new_doc(
|
2023-04-11 07:52:15 +00:00
|
|
|
"Serial and Batch Bundle", null, null, true
|
|
|
|
);
|
|
|
|
|
|
|
|
_new.item_code = this.item.item_code;
|
|
|
|
_new.warehouse = this.get_warehouse();
|
|
|
|
_new.has_serial_no = this.item.has_serial_no;
|
|
|
|
_new.has_batch_no = this.item.has_batch_no;
|
2023-06-15 07:24:43 +00:00
|
|
|
_new.type_of_transaction = this.item.type_of_transaction;
|
2023-04-11 07:52:15 +00:00
|
|
|
_new.company = this.frm.doc.company;
|
|
|
|
_new.voucher_type = this.frm.doc.doctype;
|
|
|
|
bundle_id = _new.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
frappe.set_route("Form", "Serial and Batch Bundle", bundle_id);
|
|
|
|
this.dialog.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
get_warehouse() {
|
2023-06-15 07:24:43 +00:00
|
|
|
return (this.item?.type_of_transaction === "Outward" ?
|
2023-04-11 07:52:15 +00:00
|
|
|
(this.item.warehouse || this.item.s_warehouse)
|
|
|
|
: (this.item.warehouse || this.item.t_warehouse));
|
|
|
|
}
|
|
|
|
|
2022-10-10 07:58:19 +00:00
|
|
|
render_data() {
|
2023-03-13 09:21:43 +00:00
|
|
|
if (!this.frm.is_new() && this.bundle) {
|
2022-10-10 07:58:19 +00:00
|
|
|
frappe.call({
|
2022-11-24 11:46:21 +00:00
|
|
|
method: 'erpnext.stock.doctype.serial_and_batch_bundle.serial_and_batch_bundle.get_serial_batch_ledgers',
|
2022-10-10 07:58:19 +00:00
|
|
|
args: {
|
|
|
|
item_code: this.item.item_code,
|
2023-03-13 09:21:43 +00:00
|
|
|
name: this.bundle,
|
2022-10-10 07:58:19 +00:00
|
|
|
voucher_no: this.item.parent,
|
|
|
|
}
|
|
|
|
}).then(r => {
|
|
|
|
if (r.message) {
|
|
|
|
this.set_data(r.message);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set_data(data) {
|
|
|
|
data.forEach(d => {
|
2023-03-21 05:24:41 +00:00
|
|
|
this.dialog.fields_dict.entries.df.data.push(d);
|
2022-10-10 07:58:19 +00:00
|
|
|
});
|
|
|
|
|
2023-03-21 05:24:41 +00:00
|
|
|
this.dialog.fields_dict.entries.grid.refresh();
|
2022-10-10 07:58:19 +00:00
|
|
|
}
|
|
|
|
}
|