Created
August 28, 2015 06:54
-
-
Save rmehta/390f9c25b1d2355d67c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// refactored by Rushabh Mehta | |
// original script https://gist.github.com/rmehta/5a7dbf05494990b838f8 | |
cur_frm.add_fetch('item_code','amount_allocated_on_permit', | |
'amount_allocated_on_permit'); | |
window.naivasha = { | |
recalculate: function(doc, child_doctype, child_name) { | |
if(!doc) { | |
doc = cur_frm.doc; | |
child_doctype = cur_frm.doc.items[0].doctype; | |
child_name = cur_frm.doc.items[0].name; | |
} | |
setTimeout(function() { | |
frappe.after_ajax(function() { | |
naivasha._recalculate(doc, child_doctype, child_name); | |
}, 1000); | |
}) | |
}, | |
_recalculate: function(doc, child_doctype, child_name) { | |
var row = frappe.model.get_doc(child_doctype, child_name); | |
if (!row.amount_allocated_on_permit || !row.consumption) { | |
return; | |
} | |
if(cur_frm.cur_grid) { | |
cur_frm.cur_grid.hide_form(); | |
} | |
if(!row.water_use_fee) { | |
row.water_use_fee = "Normal (Ksh 0.50)"; | |
} | |
naivasha.set_accounting_period(row, doc); | |
// rate | |
row.rate = naivasha.get_rate(row.water_use_fee); | |
row.total_amount_allocated_for_accounting_period = | |
flt(row.amount_allocated_on_permit) * flt(row.number_of_days); | |
row.total_amount_lawfully_used = | |
Math.min(row.total_amount_allocated_for_accounting_period, | |
flt(row.consumption)); | |
if (row.rate == 0.5) { | |
row.volume_of_water_charged_at_the_selected_fee = | |
Math.min(flt(row.number_of_days) * 300, flt(row.consumption)); // 50 cent | |
} else if (row.rate == 0.75) { | |
row.volume_of_water_charged_at_the_selected_fee = | |
Math.max(0, flt(row.total_amount_lawfully_used) - | |
Math.min(flt(row.number_of_days) * 300, flt(row.consumption))); | |
// 75 cent | |
} else if (row.rate == 1.0) { | |
row.volume_of_water_charged_at_the_selected_fee = | |
flt(row.consumption) - flt(row.total_amount_lawfully_used); | |
} else { | |
row.volume_of_water_charged_at_the_selected_fee = 0; | |
} | |
row.qty = row.volume_of_water_charged_at_the_selected_fee; | |
row.amount = row.rate * row.qty; | |
row.description = row.item_name + ", " + row.water_use_fee; | |
if (row.rate == 0.5) { | |
naivasha.make_additional_rows(row); | |
} | |
cur_frm.refresh_field("items"); | |
}, | |
set_accounting_period: function(row, doc) { | |
if (!row.start_date_of_accounting_period) | |
row.start_date_of_accounting_period = | |
doc.start_date_of_accounting_period; | |
if (!row.end_date_of_accounting_period) | |
row.end_date_of_accounting_period = | |
doc.end_date_of_accounting_period; | |
// days | |
row.number_of_days = moment(row.end_date_of_accounting_period) | |
.diff(row.start_date_of_accounting_period, "day"); | |
}, | |
make_additional_rows: function(row) { | |
var row1 = naivasha.get_dependant_row(row, 1); | |
var fields_to_copy = ["consumption", "item_name", | |
"item_code", "amount_allocated_on_permit", "number_of_days", | |
"total_amount_allocated_for_accounting_period", | |
"total_amount_lawfully_used"]; | |
// row 1 | |
row1.water_use_fee = "High (Ksh 0.75)"; | |
row1.rate = 0.75; | |
for(var i=0; i < fields_to_copy.length; i++) { | |
row1[fields_to_copy[i]] = row[fields_to_copy[i]]; | |
} | |
row1.volume_of_water_charged_at_the_selected_fee = | |
Math.max(0, row1.total_amount_lawfully_used - Math.min(row1.number_of_days * 300, row1.consumption)); // 75 cent | |
row1.qty = row1.volume_of_water_charged_at_the_selected_fee; | |
row1.amount = row1.rate * row1.qty; | |
row1.description = row1.item_name + ", " + row1.water_use_fee; | |
// row 2 | |
var row2 = naivasha.get_dependant_row(row, 2); | |
row2.water_use_fee = "Penalty (Ksh 1.00)"; | |
row2.rate = 1.0; | |
for(var i=0; i < fields_to_copy.length; i++) { | |
row2[fields_to_copy[i]] = row[fields_to_copy[i]]; | |
} | |
row2.volume_of_water_charged_at_the_selected_fee = | |
row2.consumption - row2.total_amount_lawfully_used; | |
row2.qty = row2.volume_of_water_charged_at_the_selected_fee; | |
row2.amount = row2.rate * row2.qty; | |
row2.description = row2.item_name + ", " + row2.water_use_fee; | |
}, | |
get_rate: function(text) { | |
if (text != null) { | |
var i = text.indexOf('Ksh'); | |
if (i > 0) { | |
text = text.split("Ksh ")[1].split(")")[0]; | |
return parseFloat(text); | |
} | |
} | |
return -1.0; | |
}, | |
get_dependant_row: function(row, seq) { | |
// returns next row or creates a new one | |
for (i = 0; i < cur_frm.doc.items.length; i++) { | |
var row1 = cur_frm.doc.items[i]; | |
if (row1.idx == row.idx + seq) { | |
return row1; | |
} | |
} | |
// add new row | |
return cur_frm.add_child("items"); | |
} | |
} | |
frappe.ui.form.on("Sales Invoice", { | |
onload: function(frm) { | |
if(!frm.doc.start_date_of_accounting_period) { | |
frm.set_value("start_date_of_accounting_period", | |
moment().startOf("month").format()); | |
} | |
if(!frm.doc.end_date_of_accounting_period) { | |
frm.set_value("end_date_of_accounting_period", | |
moment().endOf("month").format()); | |
} | |
}, | |
entries_add: function(frm, child_doctype, child_name) { | |
var row = frappe.model.get_doc(child_doctype, child_name); | |
naivasha.set_accounting_period(row, frm.doc); | |
cur_frm.refresh_field("items"); | |
}, | |
entries_on_form_rendered: function(frm) { | |
var cur_grid = cur_frm.cur_grid; | |
var row = cur_grid.doc; | |
var ro = row.water_use_fee | |
&& naivasha.get_rate(row.water_use_fee) != 0.5; | |
cur_grid.grid.get_docfield( | |
"total_amount_allocated_for_accounting_period").read_only = ro; | |
cur_grid.grid.get_docfield("consumption").read_only = ro; | |
cur_grid.refresh_field("total_amount_allocated_for_accounting_period"); | |
cur_grid.refresh_field("consumption"); | |
} | |
}); | |
frappe.ui.form.on("Sales Invoice Item", { | |
item_code: function(frm, child_doctype, child_name) { | |
naivasha.recalculate(frm.doc, child_doctype, child_name) | |
}, | |
start_date_of_accounting_period: function(frm, child_doctype, child_name) { | |
naivasha.recalculate(frm.doc, child_doctype, child_name) | |
}, | |
end_date_of_accounting_period: function(frm, child_doctype, child_name) { | |
naivasha.recalculate(frm.doc, child_doctype, child_name) | |
}, | |
water_use_fee: function(frm, child_doctype, child_name) { | |
naivasha.recalculate(frm.doc, child_doctype, child_name) | |
}, | |
consumption: function(frm, child_doctype, child_name) { | |
naivasha.recalculate(frm.doc, child_doctype, child_name) | |
}, | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment