Skip to content

Commit

Permalink
fix: refactored the code and fix the existing error
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmalroshan123 committed Nov 8, 2024
1 parent bd12801 commit 5fbd736
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 26 deletions.
125 changes: 103 additions & 22 deletions aumms/aumms/doctype/jewellery_invoice/jewellery_invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand All @@ -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);
}
});

Expand Down Expand Up @@ -744,38 +786,42 @@ 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",
args: {
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');
Expand Down Expand Up @@ -824,4 +870,39 @@ function update_making_charge(frm, row, making_charge) {
);

frm.refresh_field('items');
}
}


// 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);
}
4 changes: 2 additions & 2 deletions aumms/aumms/doctype/jewellery_invoice/jewellery_invoice.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion aumms/aumms/doctype/jewellery_invoice/jewellery_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -544,3 +544,4 @@ def get_pricing_rule_and_items(customer):
}

return {}

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"amount_without_making_charge",
"amount",
"hallmarked",
"is_sales_item",
"huid",
"section_break_lkhd",
"has_stone",
Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions aumms/aumms/doctype/jewellery_receipt/jewellery_receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions aumms/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"
}
]
}

0 comments on commit 5fbd736

Please sign in to comment.