From 5fbd73625c5c9e0e7a0e6aa70fe6c2fb15c52682 Mon Sep 17 00:00:00 2001 From: ajmalroshan123 Date: Fri, 8 Nov 2024 15:18:10 +0530 Subject: [PATCH] fix: refactored the code and fix the existing error --- .../jewellery_invoice/jewellery_invoice.js | 125 +++++++++++++++--- .../jewellery_invoice/jewellery_invoice.json | 4 +- .../jewellery_invoice/jewellery_invoice.py | 3 +- .../jewellery_item_receipt.json | 9 +- .../jewellery_receipt/jewellery_receipt.py | 1 + aumms/setup.py | 46 +++++++ 6 files changed, 162 insertions(+), 26 deletions(-) diff --git a/aumms/aumms/doctype/jewellery_invoice/jewellery_invoice.js b/aumms/aumms/doctype/jewellery_invoice/jewellery_invoice.js index bcafe0c..b5daa1c 100644 --- a/aumms/aumms/doctype/jewellery_invoice/jewellery_invoice.js +++ b/aumms/aumms/doctype/jewellery_invoice/jewellery_invoice.js @@ -638,30 +638,69 @@ frappe.ui.form.on('Jewellery Invoice Item', { } }); -// discount +// // discount -frappe.ui.form.on('Stone Detals - 2', { +// frappe.ui.form.on('Stone Detals - 2', { +// discount: function(frm, cdt, cdn) { +// calculate_discount_and_total(frm, cdt, cdn); +// } +// }); + +// function calculate_discount_and_total(frm, cdt, cdn) { +// let row = locals[cdt][cdn]; +// let discounted_amount = flt(row.stone_charge) * (1 - flt(row.discount) / 100); + +// // Update the row's final discounted amount +// frappe.model.set_value(cdt, cdn, 'custom_discount_final', discounted_amount); + +// // Calculate and set the total discounted amount +// let total = frm.doc.stone_details.reduce((sum, r) => +// sum + flt(r.stone_charge) * (1 - flt(r.discount) / 100), 0); + +// frm.set_value('custom_discount_final', total); +// frm.refresh_fields(['stone_details', 'custom_discount_final']); +// } + + +frappe.ui.form.on('Stone Details - 2', { discount: function(frm, cdt, cdn) { + calculate_discount_and_total(frm, cdt, cdn); + }, + stone_weight: function(frm, cdt, cdn) { calculate_discount_and_total(frm, cdt, cdn); } + }); function calculate_discount_and_total(frm, cdt, cdn) { let row = locals[cdt][cdn]; - let discounted_amount = flt(row.stone_charge) * (1 - flt(row.discount) / 100); + let final_amount; + + if (row.discount && flt(row.discount) !== 0) { + final_amount = flt(row.stone_charge) * (1 - flt(row.discount) / 100); + } else { + final_amount = flt(row.stone_charge); + } - // Update the row's final discounted amount - frappe.model.set_value(cdt, cdn, 'custom_discount_final', discounted_amount); + // Update the row's final amount + frappe.model.set_value(cdt, cdn, 'custom_discount_final', final_amount); + + // Calculate total by checking each row + let total = frm.doc.stone_details.reduce((sum, r) => { + if (r.discount && flt(r.discount) !== 0) { + return sum + flt(r.stone_charge) * (1 - flt(r.discount) / 100); + } else { + return sum + flt(r.stone_charge); + } + }, 0); - // Calculate and set the total discounted amount - let total = frm.doc.stone_details.reduce((sum, r) => - sum + flt(r.stone_charge) * (1 - flt(r.discount) / 100), 0); - frm.set_value('custom_discount_final', total); frm.refresh_fields(['stone_details', 'custom_discount_final']); } + + // code to set Grand Total frappe.ui.form.on('Jewellery Invoice', { @@ -674,6 +713,9 @@ frappe.ui.form.on('Jewellery Invoice', { }, custom_discount_final: function(frm) { calculate_total(frm); + }, + total_gold_amount: function(frm) { + calculate_total(frm); } }); @@ -744,6 +786,7 @@ function add_stone_details_to_table(frm, item_code, stone_details) { } // Apply pricing rules based on customer + function apply_pricing_rules(frm) { frappe.call({ method: "aumms.aumms.doctype.jewellery_invoice.jewellery_invoice.get_pricing_rule_and_items", @@ -751,31 +794,34 @@ function apply_pricing_rules(frm) { customer: frm.doc.customer }, callback: function(r) { - if (!r.message) return; + let discount_percentage = 0; + let rule_items = []; - let discount_percentage = r.message.discount_percentage; - let rule_items = r.message.rule_items || []; + if (r.message) { + discount_percentage = r.message.discount_percentage || 0; + rule_items = r.message.rule_items || []; + } update_stone_discounts(frm, discount_percentage, rule_items); } }); } -// Update discounts for matching stones +// Update discounts function update_stone_discounts(frm, discount_percentage, rule_items) { frm.doc.stone_details.forEach(function(stone) { + let matched_item = rule_items.find(function(item) { return item.item_code === stone.item_name; }); - if (matched_item) { - frappe.model.set_value( - stone.doctype, - stone.name, - 'discount', - discount_percentage - ); - } + // Apply discount if matched; otherwise, set discount to 0 + frappe.model.set_value( + stone.doctype, + stone.name, + 'discount', + matched_item ? discount_percentage : 0 + ); }); frm.refresh_field('stone_details'); @@ -824,4 +870,39 @@ function update_making_charge(frm, row, making_charge) { ); frm.refresh_field('items'); -} \ No newline at end of file +} + + +// code for discounted amount + +frappe.ui.form.on("Jewellery Invoice", { + refresh: function(frm) { + calculate_discounted_stone_charge_and_print(frm); + } +}); + +frappe.ui.form.on("Stone Details - 2", { + stone_charge: function(frm, cdt, cdn) { + calculate_discounted_stone_charge_and_print(frm); + }, + stone_details_add: function(frm) { + calculate_discounted_stone_charge_and_print(frm); + }, + custom_discount_final: function(frm) { + calculate_discounted_stone_charge_and_print(frm); + } +}); + +function calculate_discounted_stone_charge_and_print(frm) { + let total_stone_charge = 0; + + if (frm.doc.stone_details && frm.doc.stone_details.length) { + frm.doc.stone_details.forEach(row => { + let stone_charge = row.stone_charge || 0; + total_stone_charge += stone_charge ; + }); + } + let discount = total_stone_charge - frm.doc.custom_discount_final; + + frm.set_value("discount_amount", discount); +} diff --git a/aumms/aumms/doctype/jewellery_invoice/jewellery_invoice.json b/aumms/aumms/doctype/jewellery_invoice/jewellery_invoice.json index dc542c8..02a59fe 100644 --- a/aumms/aumms/doctype/jewellery_invoice/jewellery_invoice.json +++ b/aumms/aumms/doctype/jewellery_invoice/jewellery_invoice.json @@ -406,14 +406,14 @@ "fieldname": "stone_details", "fieldtype": "Table", "label": "Stone Details", - "options": "Stone Detals - 2" + "options": "Stone Details - 2" } ], "hide_toolbar": 1, "index_web_pages_for_search": 1, "is_submittable": 1, "links": [], - "modified": "2024-11-02 10:57:46.369218", + "modified": "2024-11-08 15:09:59.576364", "modified_by": "Administrator", "module": "AuMMS", "name": "Jewellery Invoice", diff --git a/aumms/aumms/doctype/jewellery_invoice/jewellery_invoice.py b/aumms/aumms/doctype/jewellery_invoice/jewellery_invoice.py index f58cbcc..53fc8e9 100644 --- a/aumms/aumms/doctype/jewellery_invoice/jewellery_invoice.py +++ b/aumms/aumms/doctype/jewellery_invoice/jewellery_invoice.py @@ -529,7 +529,7 @@ def get_pricing_rule_and_items(customer): if pricing_rule: pricing_rule_name = pricing_rule[0].name - discount_percentage = pricing_rule[0].discount_percentage + discount_percentage = pricing_rule[0].discount_percentage if pricing_rule[0].discount_percentage else 0 rule_items = frappe.get_all( "Pricing Rule Item Code", @@ -544,3 +544,4 @@ def get_pricing_rule_and_items(customer): } return {} + diff --git a/aumms/aumms/doctype/jewellery_item_receipt/jewellery_item_receipt.json b/aumms/aumms/doctype/jewellery_item_receipt/jewellery_item_receipt.json index a17f822..c216041 100644 --- a/aumms/aumms/doctype/jewellery_item_receipt/jewellery_item_receipt.json +++ b/aumms/aumms/doctype/jewellery_item_receipt/jewellery_item_receipt.json @@ -24,6 +24,7 @@ "amount_without_making_charge", "amount", "hallmarked", + "is_sales_item", "huid", "section_break_lkhd", "has_stone", @@ -221,12 +222,18 @@ "fieldtype": "Data", "label": "HUID", "mandatory_depends_on": "eval: doc.hallmarked" + }, + { + "default": "0", + "fieldname": "is_sales_item", + "fieldtype": "Check", + "label": "Is Sales Item" } ], "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2024-08-31 11:10:52.640578", + "modified": "2024-11-07 14:25:09.197843", "modified_by": "Administrator", "module": "AuMMS", "name": "Jewellery Item Receipt", diff --git a/aumms/aumms/doctype/jewellery_receipt/jewellery_receipt.py b/aumms/aumms/doctype/jewellery_receipt/jewellery_receipt.py index f11788d..8862169 100644 --- a/aumms/aumms/doctype/jewellery_receipt/jewellery_receipt.py +++ b/aumms/aumms/doctype/jewellery_receipt/jewellery_receipt.py @@ -46,6 +46,7 @@ def create_item(self): aumms_item.gold_weight = item_detail.gold_weight aumms_item.item_category = item_detail.item_category aumms_item.is_purchase_item = 1 + aumms_item.is_sales_item = 1 if item_detail.is_sales_item else 0 if item_detail.hallmarked: aumms_item.hallmarked = 1 diff --git a/aumms/setup.py b/aumms/setup.py index 0b6eb83..1b63f26 100644 --- a/aumms/setup.py +++ b/aumms/setup.py @@ -8,6 +8,8 @@ def after_install(): create_custom_fields(get_stock_reconciliation_custom_fields(), ignore_validate=True) create_custom_fields(get_metal_ledger_custom_fields(), ignore_validate=True) create_custom_fields(get_purchase_receipt_custom_fields(), ignore_validate=True) + create_custom_fields(get_sales_invoice_custom_fields(), ignore_validate=True) + create_custom_fields(get_jewellery_invoice_custom_fields(), ignore_validate=True) def after_migrate(): after_install() @@ -159,3 +161,47 @@ def get_purchase_receipt_custom_fields(): ] } + + +def get_sales_invoice_custom_fields(): + ''' + Custom fields that need to be added to the Sales Invoice Doctype + ''' + return { + "Sales Invoice": [ + + ], + "Sales Invoice Item": [ + { + "fieldname": "board_rate", + "fieldtype": "Data", + "label": "Board Rate", + "insert_after": "amount" + } + ] + } + + +def get_jewellery_invoice_custom_fields(): + return { + "Jewellery Invoice": [ + { + "fieldname": "discounts", + "fieldtype": "Section Break", + "label": "Discounts", + "insert_after": "stone_details" + }, + { + "fieldname": "discount_amount", + "fieldtype": "Data", + "label": "Discount Amount", + "insert_after": "discounts" + }, + { + "fieldname": "discount_column_break", + "fieldtype": "Column Break", + "label": "", + "insert_after": "discount_amount" + } + ] + }