Skip to content

Commit

Permalink
feat: good in transit updates
Browse files Browse the repository at this point in the history
  • Loading branch information
AshishBarvaliya committed Aug 13, 2024
1 parent f8a0b45 commit a38e045
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 22 deletions.
15 changes: 14 additions & 1 deletion d2h/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def create_purchase_receipt(purchase_order, items):
new_item.item_name = item["item_name"]
new_item.qty = item["qty"]
new_item.uom = item["uom"]
new_item.purchase_order = purchase_order.name


purchase_receipt.insert(ignore_permissions=True)
Expand All @@ -72,4 +73,16 @@ def create_purchase_receipt(purchase_order, items):
item.custom_good_in_transit_qty += found_item['qty']

purchase_order.save(ignore_permissions=True)
return "OK"
return "OK"

@frappe.whitelist()
def get_purchase_order_good_in_transit(purchase_order):
purchase_receipts = frappe.get_all(
"Purchase Receipt Item",
filters={
"purchase_order": purchase_order,
"docstatus": 1
},
fields=["name", "item_code", "item_name", "qty"]
)
return purchase_receipts
1 change: 1 addition & 0 deletions d2h/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
doc_events = {
"Purchase Receipt": {
"before_save": "d2h.overrides.purchase_receipt_before_save",
"on_submit": "d2h.overrides.on_submit_purchase_receipt"
},
"Delivery Note": {
"before_save": "d2h.overrides.delivery_note_before_save",
Expand Down
16 changes: 13 additions & 3 deletions d2h/overrides.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import frappe

@frappe.whitelist()
def purchase_receipt_before_save(doc, method):
if len(doc.items) > 0:
doc.custom_item_duplicate = []
Expand All @@ -10,12 +9,23 @@ def purchase_receipt_before_save(doc, method):
new_item.qty = item.qty
new_item.rejected_qty = item.rejected_qty

@frappe.whitelist()
def delivery_note_before_save(doc, method):
if len(doc.items) > 0:
doc.custom_delivery_note_item_duplicate = []
for item in doc.items:
new_item = doc.append("custom_delivery_note_item_duplicate", {})
new_item.item_code = item.item_code
new_item.qty = item.qty
new_item.uom = item.uom
new_item.uom = item.uom

def on_submit_purchase_receipt(doc, method):
for item in doc.items:
item_order = frappe.get_doc("Purchase Order Item", {
"item_code": item.item_code,
"parent": item.purchase_order
})
if(item_order.custom_good_in_transit_qty < item.qty):
item_order.custom_good_in_transit_qty -= item.qty
else:
item_order.custom_good_in_transit_qty = 0
item_order.save()
58 changes: 40 additions & 18 deletions d2h/public/js/purchase_order.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
frappe.ui.form.on("Purchase Order", {
refresh: function (frm) {
if (!frm.is_new() && frm.doc.status !== "Draft") {
qties = frm.doc.items.map((item) => {
return (
item.qty -
item.custom_good_in_transit_qty -
item.custom_short_close_qty
);
if (frm.doc.docstatus == 1) {
frappe.call({
method: "d2h.api.get_purchase_order_good_in_transit",
args: {
purchase_order: frm.doc.name,
},
callback: function (r) {
pending_qty = [];
frm.doc.items.map((item) => {
received_qty = 0;
r.message.map((i) => {
if (i.item_code == item.item_code) {
received_qty += i.qty;
}
});
console.log(received_qty, item.qty);
if (received_qty < item.qty) {
pending_qty.push({
name: item.name,
pending: item.qty - received_qty,
});
}
});
console.log(frm.doc.items, r.message);
if (pending_qty.length > 0) {
frm.add_custom_button(__("Good In Transit"), function () {
show_items_dialog(frm, pending_qty);
});
frm.add_custom_button(__("Short Close"), function () {
show_confirm_dialog(frm);
});
}
},
});
total_qty = qties.reduce((a, b) => a + b, 0);
if (total_qty > 0) {
frm.add_custom_button(__("Good In Transit"), function () {
show_items_dialog(frm);
});
frm.add_custom_button(__("Short Close"), function () {
show_confirm_dialog(frm);
});
}
}
},
});

function show_items_dialog(frm) {
function show_items_dialog(frm, pending_qty) {
let d = new frappe.ui.Dialog({
title: "Items",
fields: [
Expand All @@ -35,7 +52,10 @@ function show_items_dialog(frm) {
return {
name: item.name,
new_item_code: item.item_code,
new_qty: item.qty - item.custom_good_in_transit_qty,
new_qty: pending_qty.find((i) => i.name == item.name)
? pending_qty.find((i) => i.name == item.name).pending -
item.custom_good_in_transit_qty
: qty,
new_good_in_transit_qty: 0,
};
}),
Expand Down Expand Up @@ -100,6 +120,7 @@ function show_items_dialog(frm) {
frm.dirty();
frappe.msgprint(__("Purchase Receipt has been created."));
frm.refresh_field("items");
d.hide();
},
});
}
Expand Down Expand Up @@ -128,6 +149,7 @@ function show_confirm_dialog(frm) {
callback: function (r) {
frappe.msgprint(__("Items have been short closed."));
frm.refresh();
d.hide();
},
});
}
Expand Down

0 comments on commit a38e045

Please sign in to comment.