From 17ff928e39d9e1fa4b829286378bf5b0509fc3b4 Mon Sep 17 00:00:00 2001 From: Ashkar Date: Wed, 30 Apr 2025 16:29:51 +0530 Subject: [PATCH] fix: posnext sales invoice stock update --- posnext/doc_events/sales_invoice.py | 57 +++++++++++++++++++++++++++++ posnext/hooks.py | 3 ++ posnext/public/js/pos_controller.js | 18 ++++++++- 3 files changed, 77 insertions(+), 1 deletion(-) diff --git a/posnext/doc_events/sales_invoice.py b/posnext/doc_events/sales_invoice.py index 0366115..7b882b7 100644 --- a/posnext/doc_events/sales_invoice.py +++ b/posnext/doc_events/sales_invoice.py @@ -13,3 +13,60 @@ def validate_si(doc,method): "mode_of_payment": mop[0].mode_of_payment, "amount": doc.rounded_total or doc.grand_total }) +def create_delivery_note(doc, method): + if doc.update_stock: + return + + if doc.is_return != 1: + delivery_note = frappe.new_doc("Delivery Note") + delivery_note.customer = doc.customer + delivery_note.posting_date = doc.posting_date + delivery_note.posting_time = doc.posting_time + delivery_note.taxes_and_charges = doc.taxes_and_charges + delivery_note.company = doc.company + + all_items_sufficient = True + + for item in doc.items: + available_qty = frappe.db.get_value( + "Bin", {"item_code": item.item_code, "warehouse": item.warehouse}, "actual_qty" + ) or 0 + + delivery_note.append("items", { + "item_code": item.item_code, + "uom": item.uom, + "qty": item.qty, + "rate": item.rate, + "warehouse": item.warehouse, + "against_sales_invoice": item.parent, + "si_detail": item.name, + "cost_center": item.cost_center + }) + + if item.qty > available_qty: + all_items_sufficient = False + + for tax in doc.taxes: + delivery_note.append("taxes", { + "charge_type": tax.charge_type, + "account_head": tax.account_head, + "description": tax.description, + "rate": tax.rate, + "tax_amount": tax.tax_amount, + "total": tax.total, + "cost_center": tax.cost_center + }) + + for sp in doc.sales_team: + delivery_note.append("sales_team", { + "sales_person": sp.sales_person, + "allocated_percentage": sp.allocated_percentage + }) + + delivery_note.save() + + if all_items_sufficient: + delivery_note.submit() + frappe.msgprint(f"Delivery Note {delivery_note.name} submitted as sufficient stock is available.") + else: + frappe.msgprint(f"Delivery Note {delivery_note.name} saved as draft due to insufficient stock.") diff --git a/posnext/hooks.py b/posnext/hooks.py index 9bc76bb..5a3e569 100644 --- a/posnext/hooks.py +++ b/posnext/hooks.py @@ -141,6 +141,9 @@ "Sales Invoice": { "validate": [ "posnext.doc_events.sales_invoice.validate_si", + ], + "on_submit": [ + "posnext.doc_events.sales_invoice.create_delivery_note", ] }, "POS Profile": { diff --git a/posnext/public/js/pos_controller.js b/posnext/public/js/pos_controller.js index 656326a..c86d1cd 100644 --- a/posnext/public/js/pos_controller.js +++ b/posnext/public/js/pos_controller.js @@ -12,9 +12,25 @@ posnext.PointOfSale.Controller = class { () => this.reload_status = true, ]); + this.setup_form_events(); - } + setup_form_events() { + frappe.ui.form.on('Sales Invoice', { + after_save: function(frm) { + if (!frm.doc.pos_profile) return; + + frappe.db.get_doc('POS Profile', frm.doc.pos_profile) + .then(pos_profile => { + if (pos_profile.custom_stock_update) { + frm.set_value('update_stock', 0); + // frm.save(); + } + }); + } + }); + } + fetch_opening_entry(value) { return frappe.call("posnext.posnext.page.posnext.point_of_sale.check_opening_entry", { "user": frappe.session.user, "value": value });