I am creating a module for Vehicle maintanance, now I have some doctypes and among them there are two doctypes which i want to talk about.
Fleet Bill of Material
Fleet Work Order
Now why i don’t use erpnext core BOM and work order i have my own reasons.
Now I want to the stock which will be used in the fleet maintanence.
keep in mind i have a seperate doctype called Fleet.
In the work order I have a child table of items and what I want is that whenever i select an item it should check the stock availability and if there will be no stock in a particalar warehouse then it should be allowed to add that item and a message of no stock available.
I have tried the following
function check_stock_availability(frm, cdt, cdn, item_code) {
const row = locals[cdt][cdn];
const warehouse = frm.doc.warehouse;
if (warehouse) {
// Fetch all batches for the item in the specified warehouse
frappe.call({
method: 'frappe.client.get_list',
args: {
doctype: 'Batch',
filters: {
item: item_code,
},
fields: ['name']
},
callback: function(r) {
if (r.message) {
let available_qty = 0;
const batches = r.message;
// For each batch, calculate the quantity in the specified warehouse
let batch_qty_promises = batches.map(batch => {
return new Promise((resolve) => {
frappe.call({
method: 'erpnext.stock.doctype.batch.batch.get_batch_qty',
args: {
batch_no: batch.name,
warehouse: warehouse,
item_code: item_code
},
callback: function(batch_res) {
if (batch_res.message) {
available_qty += batch_res.message;
}
resolve();
}
});
});
});
// Wait for all batch quantities to be fetched
Promise.all(batch_qty_promises).then(() => {
if (available_qty < row.qty) {
frappe.msgprint(`Not enough stock for Item: ${item_code}. Available quantity is ${available_qty} in warehouse: ${warehouse}.`);
frm.get_field('items').grid.grid_rows_by_docname[cdn].remove();
frm.refresh_field('items');
} else {
frappe.msgprint(`Available quantity for Item: ${item_code} is ${available_qty} in warehouse: ${warehouse}.`);
}
});
} else {
frappe.msgprint(`No batches found for Item: ${item_code}.`);
}
}
});
} else {
frm.get_field('items').grid.grid_rows_by_docname[cdn].remove();
frm.refresh_field('items');
frappe.msgprint('Please select a warehouse.');
}
}
can you please fix my code or can advice me what to do.
i will aslo consume the stock in the work order so please also suggest me that how can i properly handle the stock.