Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 1 | // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors |
| 2 | // License: GNU General Public License v3. See license.txt |
| 3 | |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 4 | // get tax rate |
| 5 | cur_frm.cscript.account_head = function(doc, cdt, cdn) { |
| 6 | var d = locals[cdt][cdn]; |
| 7 | if(!d.charge_type && d.account_head){ |
| 8 | msgprint("Please select Charge Type first"); |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 9 | frappe.model.set_value(cdt, cdn, "account_head", ""); |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 10 | } else if(d.account_head && d.charge_type!=="Actual") { |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 11 | frappe.call({ |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 12 | type:"GET", |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 13 | method: "erpnext.controllers.accounts_controller.get_tax_rate", |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 14 | args: {"account_head":d.account_head}, |
| 15 | callback: function(r) { |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 16 | frappe.model.set_value(cdt, cdn, "rate", r.message || 0); |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 17 | } |
| 18 | }) |
| 19 | } |
| 20 | } |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 21 | |
| 22 | |
| 23 | var validate_taxes_and_charges = function(cdt, cdn) { |
| 24 | var d = locals[cdt][cdn]; |
| 25 | if(!d.charge_type && (d.row_id || d.rate || d.tax_amount)) { |
| 26 | msgprint(__("Please select Charge Type first")); |
Nabin Hait | 2b019ed | 2015-02-22 23:03:07 +0530 | [diff] [blame] | 27 | d.row_id = ""; |
| 28 | d.rate = d.tax_amount = 0.0; |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 29 | } else if((d.charge_type == 'Actual' || d.charge_type == 'On Net Total') && d.row_id) { |
| 30 | msgprint(__("Can refer row only if the charge type is 'On Previous Row Amount' or 'Previous Row Total'")); |
| 31 | d.row_id = ""; |
| 32 | } else if((d.charge_type == 'On Previous Row Amount' || d.charge_type == 'On Previous Row Total') && d.row_id) { |
| 33 | if (d.idx == 1) { |
| 34 | msgprint(__("Cannot select charge type as 'On Previous Row Amount' or 'On Previous Row Total' for first row")); |
| 35 | d.charge_type = ''; |
| 36 | } else if (d.row_i && d.row_id >= d.idx) { |
| 37 | msgprint(__("Cannot refer row number greater than or equal to current row number for this Charge type")); |
| 38 | d.row_id = ""; |
| 39 | } |
| 40 | } |
| 41 | validated = false; |
| 42 | refresh_field('row_id', d.name, 'taxes'); |
| 43 | } |
| 44 | |
| 45 | frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) { |
| 46 | validate_taxes_and_charges(cdt, cdn); |
Nabin Hait | 2b019ed | 2015-02-22 23:03:07 +0530 | [diff] [blame] | 47 | }); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 48 | |
| 49 | frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) { |
| 50 | validate_taxes_and_charges(cdt, cdn); |
Nabin Hait | 2b019ed | 2015-02-22 23:03:07 +0530 | [diff] [blame] | 51 | }); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 52 | |
| 53 | frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) { |
| 54 | validate_taxes_and_charges(cdt, cdn); |
Nabin Hait | 2b019ed | 2015-02-22 23:03:07 +0530 | [diff] [blame] | 55 | }); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 56 | |
| 57 | frappe.ui.form.on(cur_frm.cscript.tax_table, "charge_type", function(frm, cdt, cdn) { |
| 58 | validate_taxes_and_charges(cdt, cdn); |
Nabin Hait | 2b019ed | 2015-02-22 23:03:07 +0530 | [diff] [blame] | 59 | }); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 60 | |
| 61 | |
| 62 | cur_frm.set_query("account_head", "taxes", function(doc) { |
| 63 | if(cur_frm.cscript.tax_table == "Sales Taxes and Charges") { |
| 64 | var account_type = ["Tax", "Chargeable", "Expense Account"]; |
| 65 | } else { |
| 66 | var account_type = ["Tax", "Chargeable", "Income Account"]; |
| 67 | } |
| 68 | |
| 69 | return { |
| 70 | query: "erpnext.controllers.queries.tax_account_query", |
| 71 | filters: { |
| 72 | "account_type": account_type, |
| 73 | "company": doc.company |
| 74 | } |
| 75 | } |
| 76 | }); |
| 77 | |
| 78 | cur_frm.set_query("cost_center", "taxes", function(doc) { |
| 79 | return { |
| 80 | filters: { |
| 81 | 'company': doc.company, |
| 82 | 'group_or_ledger': "Ledger" |
| 83 | } |
| 84 | } |
Nabin Hait | 2b019ed | 2015-02-22 23:03:07 +0530 | [diff] [blame] | 85 | }); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 86 | |
| 87 | // For customizing print |
Nabin Hait | f0bc9b6 | 2015-02-23 01:40:01 +0530 | [diff] [blame] | 88 | cur_frm.pformat.total = function(doc) { return ''; } |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 89 | cur_frm.pformat.discount_amount = function(doc) { return ''; } |
| 90 | cur_frm.pformat.grand_total = function(doc) { return ''; } |
| 91 | cur_frm.pformat.rounded_total = function(doc) { return ''; } |
| 92 | cur_frm.pformat.in_words = function(doc) { return ''; } |
| 93 | |
| 94 | cur_frm.pformat.taxes= function(doc){ |
| 95 | //function to make row of table |
Nabin Hait | de9c8a9 | 2015-02-23 01:06:00 +0530 | [diff] [blame] | 96 | var make_row = function(title, val, bold, is_negative) { |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 97 | var bstart = '<b>'; var bend = '</b>'; |
| 98 | return '<tr><td style="width:50%;">' + (bold?bstart:'') + title + (bold?bend:'') + '</td>' |
Nabin Hait | de9c8a9 | 2015-02-23 01:06:00 +0530 | [diff] [blame] | 99 | + '<td style="width:50%;text-align:right;">' + (is_negative ? '- ' : '') |
| 100 | + format_currency(val, doc.currency) + '</td></tr>'; |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 101 | } |
| 102 | |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 103 | function print_hide(fieldname) { |
| 104 | var doc_field = frappe.meta.get_docfield(doc.doctype, fieldname, doc.name); |
| 105 | return doc_field.print_hide; |
| 106 | } |
| 107 | |
| 108 | out =''; |
| 109 | if (!doc.print_without_amount) { |
| 110 | var cl = doc.taxes || []; |
| 111 | |
| 112 | // outer table |
| 113 | var out='<div><table class="noborder" style="width:100%"><tr><td style="width: 60%"></td><td>'; |
| 114 | |
| 115 | // main table |
| 116 | |
| 117 | out +='<table class="noborder" style="width:100%">'; |
| 118 | |
Nabin Hait | f0bc9b6 | 2015-02-23 01:40:01 +0530 | [diff] [blame] | 119 | if(!print_hide('total')) { |
| 120 | out += make_row('Total', doc.total, 1); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 121 | } |
| 122 | |
Nabin Hait | de9c8a9 | 2015-02-23 01:06:00 +0530 | [diff] [blame] | 123 | // Discount Amount on net total |
| 124 | if(!print_hide('discount_amount') && doc.apply_discount_on == "Net Total" && doc.discount_amount) |
| 125 | out += make_row('Discount Amount', doc.discount_amount, 0, 1); |
| 126 | |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 127 | // add rows |
| 128 | if(cl.length){ |
| 129 | for(var i=0;i<cl.length;i++) { |
Nabin Hait | 755ff60 | 2015-02-23 01:12:09 +0530 | [diff] [blame] | 130 | if(cl[i].tax_amount!=0 && !cl[i].included_in_print_rate) |
| 131 | out += make_row(cl[i].description, cl[i].tax_amount, 0); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Nabin Hait | de9c8a9 | 2015-02-23 01:06:00 +0530 | [diff] [blame] | 135 | // Discount Amount on grand total |
| 136 | if(!print_hide('discount_amount') && doc.apply_discount_on == "Grand Total" && doc.discount_amount) |
| 137 | out += make_row('Discount Amount', doc.discount_amount, 0, 1); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 138 | |
| 139 | // grand total |
| 140 | if(!print_hide('grand_total')) |
| 141 | out += make_row('Grand Total', doc.grand_total, 1); |
| 142 | |
| 143 | if(!print_hide('rounded_total')) |
| 144 | out += make_row('Rounded Total', doc.rounded_total, 1); |
| 145 | |
| 146 | if(doc.in_words && !print_hide('in_words')) { |
| 147 | out +='</table></td></tr>'; |
| 148 | out += '<tr><td colspan = "2">'; |
| 149 | out += '<table><tr><td style="width:25%;"><b>In Words</b></td>'; |
| 150 | out += '<td style="width:50%;">' + doc.in_words + '</td></tr>'; |
| 151 | } |
| 152 | out += '</table></td></tr></table></div>'; |
| 153 | } |
| 154 | return out; |
| 155 | } |