Created
August 27, 2015 08:27
-
-
Save rmehta/5a7dbf05494990b838f8 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
cur_frm.cscript.custom_entries_add = function(doc, cdt, cdn) { | |
var row = frappe.model.get_doc(cdt, cdn); | |
row.start_date_of_accounting_period = doc.start_date_of_accounting_period; | |
row.end_date_of_accounting_period = doc.end_date_of_accounting_period; | |
row.number_of_days = new Date(row.end_date_of_accounting_period) - new Date(row.start_date_of_accounting_period); // milliseconds | |
row.number_of_days = Math.round(row.number_of_days / 86400000); | |
refresh_field("start_date_of_accounting_period"); | |
refresh_field("end_date_of_accounting_period"); | |
refresh_field("number_of_days"); | |
} | |
cur_frm.add_fetch('item_code','amount_allocated_on_permit','amount_allocated_on_permit'); | |
cur_frm.add_fetch('item_code','item_name','item_name'); // fetch this as it no longer auto-fetches since we added a trigger to cur_frm.cscript.item_code | |
cur_frm.add_fetch('item_code','income_account','income_account'); | |
cur_frm.add_fetch('item_code','stock_uom','uom'); | |
function recalculate_on_change(doc, cdt, cdn){ | |
var row = frappe.model.get_doc(cdt, cdn); | |
if (row.amount_allocated_on_permit == null || row.amount_allocated_on_permit.length == 0) // prevent closing the item when it was just created | |
return; | |
if (row.start_date_of_accounting_period == null || row.start_date_of_accounting_period.length == 0) | |
row.start_date_of_accounting_period = doc.start_date_of_accounting_period; | |
if (row.end_date_of_accounting_period == null || row.end_date_of_accounting_period.length == 0) | |
row.end_date_of_accounting_period = doc.end_date_of_accounting_period; | |
if(!row.water_use_fee) row.water_use_fee = "Normal (Ksh 0.50)"; | |
row.rate = getRate(row.water_use_fee); | |
row.number_of_days = new Date(row.end_date_of_accounting_period) - new Date(row.start_date_of_accounting_period); // milliseconds | |
row.number_of_days = Math.round(row.number_of_days / 86400000); | |
if (row.amount_allocated_on_permit != null && row.amount_allocated_on_permit.length > 0) { | |
row.total_amount_allocated_for_accounting_period = row.amount_allocated_on_permit * row.number_of_days; | |
row.total_amount_lawfully_used = Math.min(row.total_amount_allocated_for_accounting_period, row.consumption); | |
if (row.rate == 0.5) | |
row.volume_of_water_charged_at_the_selected_fee = Math.min(row.number_of_days * 300, row.consumption); // 50 cent | |
else if (row.rate == 0.75) | |
row.volume_of_water_charged_at_the_selected_fee = Math.max(0, row.total_amount_lawfully_used - Math.min(row.number_of_days * 300, row.consumption)); // 75 cent | |
else if (row.rate == 1.0) | |
row.volume_of_water_charged_at_the_selected_fee = row.consumption - 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; | |
} | |
//var row1 = frappe.model.get_new_doc(cdt, doc); | |
//var row1 = frappe.model.add_child(doc, cdt); | |
if (row.rate == 0.5) { | |
var row1 = getDependentRow(row, 1); | |
var row2 = getDependentRow(row, 2); | |
row1.water_use_fee = "High (Ksh 0.75)"; | |
row1.rate = 0.75; | |
row1.consumption = row.consumption; | |
row1.item_name = row.item_name; | |
row1.item_code = row.item_code; | |
row1.amount_allocated_on_permit = row.amount_allocated_on_permit; | |
row1.number_of_days = row.number_of_days; | |
row1.total_amount_allocated_for_accounting_period = row.total_amount_allocated_for_accounting_period; | |
row1.total_amount_lawfully_used = row.total_amount_lawfully_used; | |
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; | |
//row1.uom = row.uom; | |
row2.water_use_fee = "Penalty (Ksh 1.00)"; | |
row2.rate = 1.0; | |
row2.consumption = row.consumption; | |
row2.item_name = row.item_name; | |
row2.item_code = row.item_code; | |
row2.amount_allocated_on_permit = row.amount_allocated_on_permit; | |
row2.number_of_days = row.number_of_days; | |
row2.total_amount_allocated_for_accounting_period = row.total_amount_allocated_for_accounting_period; | |
row2.total_amount_lawfully_used = row.total_amount_lawfully_used; | |
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; | |
//row2.uom = row.uom; | |
} | |
//cur_frm.refresh(); | |
cur_frm.cscript.calculate_taxes_and_totals(); | |
//refresh_field("total_amount_lawfully_used"); | |
// The following 3 lines are an experiment that would disable the "+" for inserting a row inbetween a group-of-3; | |
// Unfortunately the "+" comes back in too many cases, and I didn't find a way to prevent it, besides, there are more ways to insert a row. | |
//getRowFromDoc(row).row[0].lastChild.firstElementChild.innerHTML = ""; | |
//getRowFromDoc(getDependentRow(row, 1)).row[0].lastChild.firstElementChild.innerHTML = ""; | |
//getRowFromDoc(getDependentRow(row, 2)).row[0].lastChild.firstElementChild.innerHTML = ""; | |
} | |
function getDependentRow(row, seq) { | |
var expectedName = row.name.substring(0, row.name.lastIndexOf(" ")); | |
var idx = parseInt(row.name.substring(row.name.lastIndexOf(" ") + 1)); | |
expectedName += " " + (idx + seq); | |
for (i = 0; i < cur_frm.grids[0].grid.grid_rows.length; ++i) { | |
if (cur_frm.grids[0].grid.grid_rows[i].doc.idx == row.idx + seq) | |
return cur_frm.grids[0].grid.grid_rows[i].doc; | |
} | |
cur_frm.grids[0].grid.add_new_row(null,null,false); | |
cur_frm.refresh(); | |
var newrow = cur_frm.grids[0].grid.grid_rows[cur_frm.grids[0].grid.grid_rows.length - 1].doc; | |
//newrow.name = expectedName; | |
return newrow; | |
} | |
function getRowFromDoc(doc) { | |
// you can use cur_frm.grids[0].grid.grid_rows_by_docname[doc.name]; | |
for (i = 0; i < cur_frm.grids[0].grid.grid_rows.length; ++i) { | |
if (cur_frm.grids[0].grid.grid_rows[i].doc == doc) | |
return cur_frm.grids[0].grid.grid_rows[i]; | |
} | |
return null; | |
} | |
function getRate(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; | |
} | |
// add trigger everywhere it is needed | |
// cur_frm.cscript.amount_allocated_on_permit = recalculate_on_change; | |
cur_frm.cscript.custom_item_code = recalculate_on_change; // closes the form before the item name is filled, as a result item_name isn't fetched | |
cur_frm.cscript.start_date_of_accounting_period = recalculate_on_change; | |
cur_frm.cscript.end_date_of_accounting_period = recalculate_on_change; | |
cur_frm.cscript.water_use_fee = recalculate_on_change; | |
cur_frm.cscript.consumption = recalculate_on_change; | |
cur_frm.cscript.custom_entries_on_form_rendered = function(doc, grid_row) { | |
var ro = ((grid_row.fields_dict.water_use_fee != null) && (grid_row.fields_dict.water_use_fee.value != null) && (grid_row.fields_dict.water_use_fee.value.length > 0) && (getRate(grid_row.fields_dict.water_use_fee.value) != 0.5)); | |
grid_row.fields_dict.total_amount_allocated_for_accounting_period.df.read_only = ro; | |
grid_row.fields_dict.consumption.df.read_only = ro; | |
cur_frm.cscript.calculate_taxes_and_totals(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment