Rushabh Mehta | ad45e31 | 2013-11-20 12:59:58 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
Nabin Hait | 2df4d54 | 2013-01-29 11:34:39 +0530 | [diff] [blame] | 3 | |
| 4 | from __future__ import unicode_literals |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
| 6 | from frappe import _, throw |
Nabin Hait | 48f5fa6 | 2014-09-18 15:03:54 +0530 | [diff] [blame] | 7 | from frappe.utils import cint, today, flt |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 8 | from erpnext.setup.utils import get_company_currency, get_exchange_rate |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 9 | from erpnext.accounts.utils import get_fiscal_year, validate_fiscal_year |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 10 | from erpnext.utilities.transaction_base import TransactionBase |
Anand Doshi | 1394509 | 2014-09-21 19:45:49 +0530 | [diff] [blame] | 11 | from erpnext.controllers.recurring_document import convert_to_recurring, validate_recurring_document |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 12 | import json |
Nabin Hait | 2df4d54 | 2013-01-29 11:34:39 +0530 | [diff] [blame] | 13 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 14 | class AccountsController(TransactionBase): |
Saurabh | 6f75318 | 2013-03-20 12:55:28 +0530 | [diff] [blame] | 15 | def validate(self): |
nabinhait | 23cce73 | 2014-07-03 12:25:06 +0530 | [diff] [blame] | 16 | if self.get("_action") and self._action != "update_after_submit": |
Nabin Hait | 704a453 | 2014-06-05 16:55:31 +0530 | [diff] [blame] | 17 | self.set_missing_values(for_validate=True) |
Nabin Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 18 | self.validate_date_with_fiscal_year() |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 19 | if self.meta.get_field("currency"): |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 20 | self.calculate_taxes_and_totals() |
Saurabh | 6f75318 | 2013-03-20 12:55:28 +0530 | [diff] [blame] | 21 | self.validate_value("grand_total", ">=", 0) |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 22 | self.set_total_in_words() |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 23 | |
Nabin Hait | 3986bad | 2013-07-26 11:01:17 +0530 | [diff] [blame] | 24 | self.validate_for_freezed_account() |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 25 | |
Anand Doshi | 1394509 | 2014-09-21 19:45:49 +0530 | [diff] [blame] | 26 | if self.meta.get_field("is_recurring"): |
| 27 | validate_recurring_document(self) |
| 28 | |
| 29 | def on_submit(self): |
| 30 | if self.meta.get_field("is_recurring"): |
| 31 | convert_to_recurring(self, self.get("posting_date") or self.get("transaction_date")) |
| 32 | |
| 33 | def on_update_after_submit(self): |
| 34 | if self.meta.get_field("is_recurring"): |
| 35 | validate_recurring_document(self) |
| 36 | convert_to_recurring(self, self.get("posting_date") or self.get("transaction_date")) |
| 37 | |
| 38 | def before_recurring(self): |
| 39 | self.fiscal_year = None |
| 40 | for fieldname in ("due_date", "aging_date"): |
| 41 | if self.meta.get_field(fieldname): |
| 42 | self.set(fieldname, None) |
| 43 | |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 44 | def set_missing_values(self, for_validate=False): |
| 45 | for fieldname in ["posting_date", "transaction_date"]: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 46 | if not self.get(fieldname) and self.meta.get_field(fieldname): |
| 47 | self.set(fieldname, today()) |
| 48 | if not self.fiscal_year: |
Rushabh Mehta | f2227d0 | 2014-03-31 23:37:40 +0530 | [diff] [blame] | 49 | self.fiscal_year = get_fiscal_year(self.get(fieldname))[0] |
Anand Doshi | ac32bad | 2014-04-18 01:30:14 +0530 | [diff] [blame] | 50 | break |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 51 | |
Nabin Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 52 | def validate_date_with_fiscal_year(self): |
| 53 | if self.meta.get_field("fiscal_year") : |
| 54 | date_field = "" |
| 55 | if self.meta.get_field("posting_date"): |
| 56 | date_field = "posting_date" |
| 57 | elif self.meta.get_field("transaction_date"): |
| 58 | date_field = "transaction_date" |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 59 | |
Rushabh Mehta | f2227d0 | 2014-03-31 23:37:40 +0530 | [diff] [blame] | 60 | if date_field and self.get(date_field): |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 61 | validate_fiscal_year(self.get(date_field), self.fiscal_year, |
Nabin Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 62 | label=self.meta.get_label(date_field)) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 63 | |
Nabin Hait | 3986bad | 2013-07-26 11:01:17 +0530 | [diff] [blame] | 64 | def validate_for_freezed_account(self): |
Anand Doshi | 7474012 | 2013-07-26 16:07:52 +0530 | [diff] [blame] | 65 | for fieldname in ["customer", "supplier"]: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 66 | if self.meta.get_field(fieldname) and self.get(fieldname): |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 67 | accounts = frappe.db.get_values("Account", |
| 68 | {"master_type": fieldname.title(), "master_name": self.get(fieldname), |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 69 | "company": self.company}, "name") |
Nabin Hait | 3986bad | 2013-07-26 11:01:17 +0530 | [diff] [blame] | 70 | if accounts: |
Nabin Hait | 5ae6a61 | 2014-01-23 15:33:30 +0530 | [diff] [blame] | 71 | from erpnext.accounts.doctype.gl_entry.gl_entry import validate_frozen_account |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 72 | for account in accounts: |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 73 | validate_frozen_account(account[0]) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 74 | |
Nabin Hait | 096d363 | 2013-10-17 17:01:14 +0530 | [diff] [blame] | 75 | def set_price_list_currency(self, buying_or_selling): |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 76 | if self.meta.get_field("currency"): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 77 | company_currency = get_company_currency(self.company) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 78 | |
Nabin Hait | 7a75e10 | 2013-09-17 10:21:20 +0530 | [diff] [blame] | 79 | # price list part |
| 80 | fieldname = "selling_price_list" if buying_or_selling.lower() == "selling" \ |
| 81 | else "buying_price_list" |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 82 | if self.meta.get_field(fieldname) and self.get(fieldname): |
| 83 | self.price_list_currency = frappe.db.get_value("Price List", |
| 84 | self.get(fieldname), "currency") |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 85 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 86 | if self.price_list_currency == company_currency: |
| 87 | self.plc_conversion_rate = 1.0 |
Nabin Hait | 11f4195 | 2013-09-24 14:36:55 +0530 | [diff] [blame] | 88 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 89 | elif not self.plc_conversion_rate: |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 90 | self.plc_conversion_rate = get_exchange_rate( |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 91 | self.price_list_currency, company_currency) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 92 | |
Nabin Hait | 7a75e10 | 2013-09-17 10:21:20 +0530 | [diff] [blame] | 93 | # currency |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 94 | if not self.currency: |
| 95 | self.currency = self.price_list_currency |
| 96 | self.conversion_rate = self.plc_conversion_rate |
| 97 | elif self.currency == company_currency: |
| 98 | self.conversion_rate = 1.0 |
| 99 | elif not self.conversion_rate: |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 100 | self.conversion_rate = get_exchange_rate(self.currency, |
Nabin Hait | 7a75e10 | 2013-09-17 10:21:20 +0530 | [diff] [blame] | 101 | company_currency) |
| 102 | |
Nabin Hait | 0aa71a5 | 2014-02-11 16:14:52 +0530 | [diff] [blame] | 103 | def set_missing_item_details(self): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 104 | """set missing item values""" |
Nabin Hait | 0aa71a5 | 2014-02-11 16:14:52 +0530 | [diff] [blame] | 105 | from erpnext.stock.get_item_details import get_item_details |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 106 | if hasattr(self, "fname"): |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 107 | parent_dict = {} |
Pratik Vyas | f7daab7 | 2014-04-10 17:53:30 +0530 | [diff] [blame] | 108 | for fieldname in self.meta.get_valid_columns(): |
| 109 | parent_dict[fieldname] = self.get(fieldname) |
| 110 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 111 | for item in self.get(self.fname): |
| 112 | if item.get("item_code"): |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 113 | args = parent_dict.copy() |
| 114 | args.update(item.as_dict()) |
Nabin Hait | e2f054c | 2015-03-09 14:54:37 +0530 | [diff] [blame] | 115 | if not args.get("transaction_date"): |
| 116 | args["transaction_date"] = args.get("posting_date") |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 117 | ret = get_item_details(args) |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 118 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 119 | for fieldname, value in ret.items(): |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 120 | if item.meta.get_field(fieldname) and \ |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 121 | item.get(fieldname) is None and value is not None: |
| 122 | item.set(fieldname, value) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 123 | |
Nabin Hait | a74468b | 2014-12-30 18:33:52 +0530 | [diff] [blame] | 124 | if fieldname == "cost_center" and item.meta.get_field("cost_center") \ |
| 125 | and not item.get("cost_center") and value is not None: |
| 126 | item.set(fieldname, value) |
| 127 | |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 128 | if ret.get("pricing_rule"): |
| 129 | for field in ["base_price_list_rate", "price_list_rate", |
| 130 | "discount_percentage", "base_rate", "rate"]: |
| 131 | item.set(field, ret.get(field)) |
| 132 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 133 | def set_taxes(self, tax_parentfield, tax_master_field): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 134 | if not self.meta.get_field(tax_parentfield): |
| 135 | return |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 136 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 137 | tax_master_doctype = self.meta.get_field(tax_master_field).options |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 138 | |
Rushabh Mehta | d2b34dc | 2014-03-27 16:12:56 +0530 | [diff] [blame] | 139 | if not self.get(tax_parentfield): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 140 | if not self.get(tax_master_field): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 141 | # get the default tax master |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 142 | self.set(tax_master_field, frappe.db.get_value(tax_master_doctype, {"is_default": 1})) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 143 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 144 | self.append_taxes_from_master(tax_parentfield, tax_master_field, tax_master_doctype) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 145 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 146 | def append_taxes_from_master(self, tax_parentfield, tax_master_field, tax_master_doctype=None): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 147 | if self.get(tax_master_field): |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 148 | if not tax_master_doctype: |
| 149 | tax_master_doctype = self.meta.get_field(tax_master_field).options |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 150 | |
Nabin Hait | 6b03914 | 2014-05-02 15:45:10 +0530 | [diff] [blame] | 151 | self.extend(tax_parentfield, |
Anand Doshi | 69e0cb5 | 2014-05-04 19:37:45 +0530 | [diff] [blame] | 152 | get_taxes_and_charges(tax_master_doctype, self.get(tax_master_field), tax_parentfield)) |
Akhilesh Darjee | 4cdb799 | 2014-01-30 13:56:57 +0530 | [diff] [blame] | 153 | |
Anand Doshi | ac32bad | 2014-04-18 01:30:14 +0530 | [diff] [blame] | 154 | def set_other_charges(self): |
Rushabh Mehta | d2b34dc | 2014-03-27 16:12:56 +0530 | [diff] [blame] | 155 | self.set("other_charges", []) |
Akhilesh Darjee | 4cdb799 | 2014-01-30 13:56:57 +0530 | [diff] [blame] | 156 | self.set_taxes("other_charges", "taxes_and_charges") |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 157 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 158 | def calculate_taxes_and_totals(self): |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 159 | self.discount_amount_applied = False |
| 160 | self._calculate_taxes_and_totals() |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 161 | |
Nabin Hait | 4ba95d0 | 2014-01-23 16:45:22 +0530 | [diff] [blame] | 162 | if self.meta.get_field("discount_amount"): |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 163 | self.apply_discount_amount() |
| 164 | |
| 165 | def _calculate_taxes_and_totals(self): |
Anand Doshi | d4e76bc | 2013-07-15 18:10:51 +0530 | [diff] [blame] | 166 | # validate conversion rate |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 167 | company_currency = get_company_currency(self.company) |
| 168 | if not self.currency or self.currency == company_currency: |
| 169 | self.currency = company_currency |
| 170 | self.conversion_rate = 1.0 |
Anand Doshi | d4e76bc | 2013-07-15 18:10:51 +0530 | [diff] [blame] | 171 | else: |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 172 | from erpnext.setup.doctype.currency.currency import validate_conversion_rate |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 173 | validate_conversion_rate(self.currency, self.conversion_rate, |
| 174 | self.meta.get_label("conversion_rate"), self.company) |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 175 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 176 | self.conversion_rate = flt(self.conversion_rate) |
Rushabh Mehta | d2b34dc | 2014-03-27 16:12:56 +0530 | [diff] [blame] | 177 | self.item_doclist = self.get(self.fname) |
| 178 | self.tax_doclist = self.get(self.other_fname) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 179 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 180 | self.calculate_item_values() |
| 181 | self.initialize_taxes() |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 182 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 183 | if hasattr(self, "determine_exclusive_rate"): |
| 184 | self.determine_exclusive_rate() |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 185 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 186 | self.calculate_net_total() |
| 187 | self.calculate_taxes() |
Anand Doshi | d6822dd | 2015-03-13 18:23:10 +0530 | [diff] [blame] | 188 | self.manipulate_grand_total_for_inclusive_tax() |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 189 | self.calculate_totals() |
| 190 | self._cleanup() |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 191 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 192 | def initialize_taxes(self): |
| 193 | for tax in self.tax_doclist: |
| 194 | tax.item_wise_tax_detail = {} |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 195 | tax_fields = ["total", "tax_amount_after_discount_amount", |
| 196 | "tax_amount_for_current_item", "grand_total_for_current_item", |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 197 | "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"] |
| 198 | |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 199 | if not self.discount_amount_applied: |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 200 | tax_fields.append("tax_amount") |
| 201 | |
| 202 | for fieldname in tax_fields: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 203 | tax.set(fieldname, 0.0) |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 204 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 205 | self.validate_on_previous_row(tax) |
| 206 | self.validate_inclusive_tax(tax) |
| 207 | self.round_floats_in(tax) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 208 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 209 | def validate_on_previous_row(self, tax): |
| 210 | """ |
| 211 | validate if a valid row id is mentioned in case of |
| 212 | On Previous Row Amount and On Previous Row Total |
| 213 | """ |
| 214 | if tax.charge_type in ["On Previous Row Amount", "On Previous Row Total"] and \ |
| 215 | (not tax.row_id or cint(tax.row_id) >= tax.idx): |
Rushabh Mehta | 8a40c13 | 2014-04-15 16:30:55 +0530 | [diff] [blame] | 216 | throw(_("Please specify a valid Row ID for {0} in row {1}").format(_(tax.doctype), tax.idx)) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 217 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 218 | def validate_inclusive_tax(self, tax): |
| 219 | def _on_previous_row_error(row_range): |
Rushabh Mehta | 8a40c13 | 2014-04-15 16:30:55 +0530 | [diff] [blame] | 220 | throw(_("To include tax in row {0} in Item rate, taxes in rows {1} must also be included").format(tax.idx, |
| 221 | row_range)) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 222 | |
Nabin Hait | 312ba99 | 2014-04-04 11:06:10 +0530 | [diff] [blame] | 223 | if cint(getattr(tax, "included_in_print_rate", None)): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 224 | if tax.charge_type == "Actual": |
| 225 | # inclusive tax cannot be of type Actual |
Rushabh Mehta | 8a40c13 | 2014-04-15 16:30:55 +0530 | [diff] [blame] | 226 | throw(_("Charge of type 'Actual' in row {0} cannot be included in Item Rate").format(tax.idx)) |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 227 | elif tax.charge_type == "On Previous Row Amount" and \ |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 228 | not cint(self.tax_doclist[cint(tax.row_id) - 1].included_in_print_rate): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 229 | # referred row should also be inclusive |
| 230 | _on_previous_row_error(tax.row_id) |
| 231 | elif tax.charge_type == "On Previous Row Total" and \ |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 232 | not all([cint(t.included_in_print_rate) for t in self.tax_doclist[:cint(tax.row_id) - 1]]): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 233 | # all rows about the reffered tax should be inclusive |
| 234 | _on_previous_row_error("1 - %d" % (tax.row_id,)) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 235 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 236 | def calculate_taxes(self): |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 237 | # maintain actual tax rate based on idx |
Nabin Hait | 0a35eff | 2014-08-19 12:39:40 +0530 | [diff] [blame] | 238 | actual_tax_dict = dict([[tax.idx, flt(tax.rate, self.precision("tax_amount", tax))] for tax in self.tax_doclist |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 239 | if tax.charge_type == "Actual"]) |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 240 | |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 241 | for n, item in enumerate(self.item_doclist): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 242 | item_tax_map = self._load_item_tax_rate(item.item_tax_rate) |
| 243 | |
| 244 | for i, tax in enumerate(self.tax_doclist): |
| 245 | # tax_amount represents the amount of tax for the current step |
| 246 | current_tax_amount = self.get_current_tax_amount(item, tax, item_tax_map) |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 247 | |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 248 | # Adjust divisional loss to the last item |
| 249 | if tax.charge_type == "Actual": |
| 250 | actual_tax_dict[tax.idx] -= current_tax_amount |
| 251 | if n == len(self.item_doclist) - 1: |
| 252 | current_tax_amount += actual_tax_dict[tax.idx] |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 253 | |
| 254 | # store tax_amount for current item as it will be used for |
| 255 | # charge type = 'On Previous Row Amount' |
| 256 | tax.tax_amount_for_current_item = current_tax_amount |
| 257 | |
| 258 | # accumulate tax amount into tax.tax_amount |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 259 | if not self.discount_amount_applied: |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 260 | tax.tax_amount += current_tax_amount |
| 261 | |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 262 | tax.tax_amount_after_discount_amount += current_tax_amount |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 263 | |
Nabin Hait | 312ba99 | 2014-04-04 11:06:10 +0530 | [diff] [blame] | 264 | if getattr(tax, "category", None): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 265 | # if just for valuation, do not add the tax amount in total |
| 266 | # hence, setting it as 0 for further steps |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 267 | current_tax_amount = 0.0 if (tax.category == "Valuation") \ |
| 268 | else current_tax_amount |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 269 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 270 | current_tax_amount *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0 |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 271 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 272 | # Calculate tax.total viz. grand total till that step |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 273 | # note: grand_total_for_current_item contains the contribution of |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 274 | # item's amount, previously applied tax and the current tax on that item |
| 275 | if i==0: |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 276 | tax.grand_total_for_current_item = flt(item.base_amount + current_tax_amount, |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 277 | self.precision("total", tax)) |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 278 | else: |
| 279 | tax.grand_total_for_current_item = \ |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 280 | flt(self.tax_doclist[i-1].grand_total_for_current_item + |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 281 | current_tax_amount, self.precision("total", tax)) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 282 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 283 | # in tax.total, accumulate grand total of each item |
| 284 | tax.total += tax.grand_total_for_current_item |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 285 | |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 286 | # set precision in the last item iteration |
| 287 | if n == len(self.item_doclist) - 1: |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 288 | self.round_off_totals(tax) |
| 289 | |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 290 | # adjust Discount Amount loss in last tax iteration |
| 291 | if i == (len(self.tax_doclist) - 1) and self.discount_amount_applied: |
| 292 | self.adjust_discount_amount_loss(tax) |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 293 | |
| 294 | def round_off_totals(self, tax): |
| 295 | tax.total = flt(tax.total, self.precision("total", tax)) |
| 296 | tax.tax_amount = flt(tax.tax_amount, self.precision("tax_amount", tax)) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 297 | tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount, |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 298 | self.precision("tax_amount", tax)) |
| 299 | |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 300 | def adjust_discount_amount_loss(self, tax): |
Nabin Hait | 2244ac4 | 2015-01-12 17:35:14 +0530 | [diff] [blame] | 301 | discount_amount_loss = self.grand_total - flt(self.base_discount_amount) - tax.total |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 302 | tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount + |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 303 | discount_amount_loss, self.precision("tax_amount", tax)) |
| 304 | tax.total = flt(tax.total + discount_amount_loss, self.precision("total", tax)) |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 305 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 306 | def get_current_tax_amount(self, item, tax, item_tax_map): |
| 307 | tax_rate = self._get_tax_rate(tax, item_tax_map) |
| 308 | current_tax_amount = 0.0 |
| 309 | |
| 310 | if tax.charge_type == "Actual": |
| 311 | # distribute the tax amount proportionally to each item row |
| 312 | actual = flt(tax.rate, self.precision("tax_amount", tax)) |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 313 | current_tax_amount = (self.net_total |
| 314 | and ((item.base_amount / self.net_total) * actual) |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 315 | or 0) |
| 316 | elif tax.charge_type == "On Net Total": |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 317 | current_tax_amount = (tax_rate / 100.0) * item.base_amount |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 318 | elif tax.charge_type == "On Previous Row Amount": |
| 319 | current_tax_amount = (tax_rate / 100.0) * \ |
| 320 | self.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item |
| 321 | elif tax.charge_type == "On Previous Row Total": |
| 322 | current_tax_amount = (tax_rate / 100.0) * \ |
| 323 | self.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 324 | |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 325 | current_tax_amount = flt(current_tax_amount, self.precision("tax_amount", tax)) |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 326 | |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 327 | # store tax breakup for each item |
Anand Doshi | 179e377 | 2013-09-05 16:53:57 +0530 | [diff] [blame] | 328 | key = item.item_code or item.item_name |
| 329 | if tax.item_wise_tax_detail.get(key): |
| 330 | item_wise_tax_amount = tax.item_wise_tax_detail[key][1] + current_tax_amount |
Nabin Hait | 90f2d8d | 2014-05-07 14:55:09 +0530 | [diff] [blame] | 331 | tax.item_wise_tax_detail[key] = [tax_rate,item_wise_tax_amount] |
Anand Doshi | 179e377 | 2013-09-05 16:53:57 +0530 | [diff] [blame] | 332 | else: |
Nabin Hait | 90f2d8d | 2014-05-07 14:55:09 +0530 | [diff] [blame] | 333 | tax.item_wise_tax_detail[key] = [tax_rate,current_tax_amount] |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 334 | |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 335 | return current_tax_amount |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 336 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 337 | def _load_item_tax_rate(self, item_tax_rate): |
| 338 | return json.loads(item_tax_rate) if item_tax_rate else {} |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 339 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 340 | def _get_tax_rate(self, tax, item_tax_map): |
| 341 | if item_tax_map.has_key(tax.account_head): |
| 342 | return flt(item_tax_map.get(tax.account_head), self.precision("rate", tax)) |
| 343 | else: |
| 344 | return tax.rate |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 345 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 346 | def _cleanup(self): |
| 347 | for tax in self.tax_doclist: |
Nabin Hait | 90f2d8d | 2014-05-07 14:55:09 +0530 | [diff] [blame] | 348 | tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail, separators=(',', ':')) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 349 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 350 | def _set_in_company_currency(self, item, print_field, base_field): |
| 351 | """set values in base currency""" |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 352 | value_in_company_currency = flt(self.conversion_rate * |
Anand Doshi | 5b552b5 | 2014-04-02 15:03:35 +0530 | [diff] [blame] | 353 | flt(item.get(print_field), self.precision(print_field, item)), |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 354 | self.precision(base_field, item)) |
Anand Doshi | 5b552b5 | 2014-04-02 15:03:35 +0530 | [diff] [blame] | 355 | item.set(base_field, value_in_company_currency) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 356 | |
Anand Doshi | d6822dd | 2015-03-13 18:23:10 +0530 | [diff] [blame] | 357 | def manipulate_grand_total_for_inclusive_tax(self): |
| 358 | # if fully inclusive taxes and diff |
| 359 | if (self.meta.get_field("net_total_export") and self.tax_doclist |
| 360 | and all(cint(t.included_in_print_rate) for t in self.tax_doclist)): |
| 361 | |
| 362 | last_tax = self.tax_doclist[-1] |
| 363 | |
| 364 | diff = self.net_total_export - flt(last_tax.total / self.conversion_rate, |
| 365 | self.precision("grand_total_export")) |
| 366 | |
Nabin Hait | f2791f8 | 2015-03-16 17:01:09 +0530 | [diff] [blame] | 367 | if diff and abs(diff) <= (2.0 / 10**(self.precision("tax_amount", last_tax))): |
| 368 | adjustment_amount = flt(diff * self.conversion_rate, self.precision("tax_amount", last_tax)) |
| 369 | last_tax.tax_amount += adjustment_amount |
| 370 | last_tax.tax_amount_after_discount_amount += adjustment_amount |
| 371 | last_tax.total += adjustment_amount |
Anand Doshi | d6822dd | 2015-03-13 18:23:10 +0530 | [diff] [blame] | 372 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 373 | def calculate_total_advance(self, parenttype, advance_parentfield): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 374 | if self.doctype == parenttype and self.docstatus < 2: |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 375 | sum_of_allocated_amount = sum([flt(adv.allocated_amount, self.precision("allocated_amount", adv)) |
Rushabh Mehta | d2b34dc | 2014-03-27 16:12:56 +0530 | [diff] [blame] | 376 | for adv in self.get(advance_parentfield)]) |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 377 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 378 | self.total_advance = flt(sum_of_allocated_amount, self.precision("total_advance")) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 379 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 380 | self.calculate_outstanding_amount() |
Saurabh | 6f75318 | 2013-03-20 12:55:28 +0530 | [diff] [blame] | 381 | |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 382 | def get_gl_dict(self, args): |
Nabin Hait | 2df4d54 | 2013-01-29 11:34:39 +0530 | [diff] [blame] | 383 | """this method populates the common properties of a gl entry record""" |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 384 | gl_dict = frappe._dict({ |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 385 | 'company': self.company, |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 386 | 'posting_date': self.posting_date, |
| 387 | 'voucher_type': self.doctype, |
| 388 | 'voucher_no': self.name, |
| 389 | 'aging_date': self.get("aging_date") or self.posting_date, |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 390 | 'remarks': self.get("remarks"), |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 391 | 'fiscal_year': self.fiscal_year, |
Nabin Hait | 2df4d54 | 2013-01-29 11:34:39 +0530 | [diff] [blame] | 392 | 'debit': 0, |
| 393 | 'credit': 0, |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 394 | 'is_opening': self.get("is_opening") or "No", |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 395 | }) |
Nabin Hait | 2df4d54 | 2013-01-29 11:34:39 +0530 | [diff] [blame] | 396 | gl_dict.update(args) |
| 397 | return gl_dict |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 398 | |
Anand Doshi | 613cb6a | 2013-02-06 17:33:46 +0530 | [diff] [blame] | 399 | def clear_unallocated_advances(self, childtype, parentfield): |
Rushabh Mehta | f191f85 | 2014-04-02 18:09:34 +0530 | [diff] [blame] | 400 | self.set(parentfield, self.get(parentfield, {"allocated_amount": ["not in", [0, None, ""]]})) |
| 401 | |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 402 | frappe.db.sql("""delete from `tab%s` where parentfield=%s and parent = %s |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 403 | and ifnull(allocated_amount, 0) = 0""" % (childtype, '%s', '%s'), (parentfield, self.name)) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 404 | |
Nabin Hait | 48f5fa6 | 2014-09-18 15:03:54 +0530 | [diff] [blame] | 405 | def get_advances(self, account_head, child_doctype, parentfield, dr_or_cr, against_order_field): |
| 406 | so_list = list(set([d.get(against_order_field) for d in self.get("entries") if d.get(against_order_field)])) |
Nabin Hait | 7c831c3 | 2014-09-19 14:31:49 +0530 | [diff] [blame] | 407 | cond = "" |
| 408 | if so_list: |
| 409 | cond = "or (ifnull(t2.%s, '') in (%s))" % ("against_" + against_order_field, ', '.join(['%s']*len(so_list))) |
Nabin Hait | 48f5fa6 | 2014-09-18 15:03:54 +0530 | [diff] [blame] | 410 | |
Nabin Hait | 5b98340 | 2014-05-01 15:43:22 +0530 | [diff] [blame] | 411 | res = frappe.db.sql(""" |
| 412 | select |
Nabin Hait | bc8b20a | 2014-10-13 10:47:14 +0530 | [diff] [blame] | 413 | t1.name as jv_no, t1.remark, t2.%s as amount, t2.name as jv_detail_no, `against_%s` as against_order |
Nabin Hait | 5b98340 | 2014-05-01 15:43:22 +0530 | [diff] [blame] | 414 | from |
| 415 | `tabJournal Voucher` t1, `tabJournal Voucher Detail` t2 |
| 416 | where |
| 417 | t1.name = t2.parent and t2.account = %s and t2.is_advance = 'Yes' and t1.docstatus = 1 |
Nabin Hait | 48f5fa6 | 2014-09-18 15:03:54 +0530 | [diff] [blame] | 418 | and (( |
| 419 | ifnull(t2.against_voucher, '') = '' |
| 420 | and ifnull(t2.against_invoice, '') = '' |
| 421 | and ifnull(t2.against_jv, '') = '' |
| 422 | and ifnull(t2.against_sales_order, '') = '' |
| 423 | and ifnull(t2.against_purchase_order, '') = '' |
Nabin Hait | 7c831c3 | 2014-09-19 14:31:49 +0530 | [diff] [blame] | 424 | ) %s) |
Nabin Hait | 5b98340 | 2014-05-01 15:43:22 +0530 | [diff] [blame] | 425 | order by t1.posting_date""" % |
Nabin Hait | bc8b20a | 2014-10-13 10:47:14 +0530 | [diff] [blame] | 426 | (dr_or_cr, against_order_field, '%s', cond), |
Nabin Hait | 48f5fa6 | 2014-09-18 15:03:54 +0530 | [diff] [blame] | 427 | tuple([account_head] + so_list), as_dict= True) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 428 | |
Rushabh Mehta | d2b34dc | 2014-03-27 16:12:56 +0530 | [diff] [blame] | 429 | self.set(parentfield, []) |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 430 | for d in res: |
Nabin Hait | 48f5fa6 | 2014-09-18 15:03:54 +0530 | [diff] [blame] | 431 | self.append(parentfield, { |
| 432 | "doctype": child_doctype, |
| 433 | "journal_voucher": d.jv_no, |
| 434 | "jv_detail_no": d.jv_detail_no, |
| 435 | "remarks": d.remark, |
| 436 | "advance_amount": flt(d.amount), |
Nabin Hait | bc8b20a | 2014-10-13 10:47:14 +0530 | [diff] [blame] | 437 | "allocated_amount": flt(d.amount) if d.against_order else 0 |
Nabin Hait | 48f5fa6 | 2014-09-18 15:03:54 +0530 | [diff] [blame] | 438 | }) |
| 439 | |
| 440 | def validate_advance_jv(self, advance_table_fieldname, against_order_field): |
| 441 | order_list = list(set([d.get(against_order_field) for d in self.get("entries") if d.get(against_order_field)])) |
| 442 | if order_list: |
| 443 | account = self.get("debit_to" if self.doctype=="Sales Invoice" else "credit_to") |
| 444 | |
| 445 | jv_against_order = frappe.db.sql("""select parent, %s as against_order |
| 446 | from `tabJournal Voucher Detail` |
| 447 | where docstatus=1 and account=%s and ifnull(is_advance, 'No') = 'Yes' |
| 448 | and ifnull(against_sales_order, '') in (%s) |
| 449 | group by parent, against_sales_order""" % |
| 450 | ("against_" + against_order_field, '%s', ', '.join(['%s']*len(order_list))), |
| 451 | tuple([account] + order_list), as_dict=1) |
| 452 | |
| 453 | if jv_against_order: |
| 454 | order_jv_map = {} |
| 455 | for d in jv_against_order: |
| 456 | order_jv_map.setdefault(d.against_order, []).append(d.parent) |
| 457 | |
| 458 | advance_jv_against_si = [d.journal_voucher for d in self.get(advance_table_fieldname)] |
| 459 | |
| 460 | for order, jv_list in order_jv_map.items(): |
| 461 | for jv in jv_list: |
| 462 | if not advance_jv_against_si or jv not in advance_jv_against_si: |
Nabin Hait | cc11045 | 2014-11-28 15:53:07 +0530 | [diff] [blame] | 463 | frappe.msgprint(_("Journal Voucher {0} is linked against Order {1}, check if it should be pulled as advance in this invoice.") |
Nabin Hait | 48f5fa6 | 2014-09-18 15:03:54 +0530 | [diff] [blame] | 464 | .format(jv, order)) |
| 465 | |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 466 | |
Nabin Hait | 19d945a | 2013-07-29 18:35:39 +0530 | [diff] [blame] | 467 | def validate_multiple_billing(self, ref_dt, item_ref_dn, based_on, parentfield): |
Nabin Hait | 5ae6a61 | 2014-01-23 15:33:30 +0530 | [diff] [blame] | 468 | from erpnext.controllers.status_updater import get_tolerance_for |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 469 | item_tolerance = {} |
| 470 | global_tolerance = None |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 471 | |
Rushabh Mehta | d2b34dc | 2014-03-27 16:12:56 +0530 | [diff] [blame] | 472 | for item in self.get("entries"): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 473 | if item.get(item_ref_dn): |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 474 | ref_amt = flt(frappe.db.get_value(ref_dt + " Item", |
Rushabh Mehta | f2227d0 | 2014-03-31 23:37:40 +0530 | [diff] [blame] | 475 | item.get(item_ref_dn), based_on), self.precision(based_on, item)) |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 476 | if not ref_amt: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 477 | frappe.msgprint(_("Warning: System will not check overbilling since amount for Item {0} in {1} is zero").format(item.item_code, ref_dt)) |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 478 | else: |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 479 | already_billed = frappe.db.sql("""select sum(%s) from `tab%s` |
| 480 | where %s=%s and docstatus=1 and parent != %s""" % |
| 481 | (based_on, self.tname, item_ref_dn, '%s', '%s'), |
Rushabh Mehta | f2227d0 | 2014-03-31 23:37:40 +0530 | [diff] [blame] | 482 | (item.get(item_ref_dn), self.name))[0][0] |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 483 | |
| 484 | total_billed_amt = flt(flt(already_billed) + flt(item.get(based_on)), |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 485 | self.precision(based_on, item)) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 486 | |
| 487 | tolerance, item_tolerance, global_tolerance = get_tolerance_for(item.item_code, |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 488 | item_tolerance, global_tolerance) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 489 | |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 490 | max_allowed_amt = flt(ref_amt * (100 + tolerance) / 100) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 491 | |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 492 | if total_billed_amt - max_allowed_amt > 0.01: |
Nabin Hait | e24365f | 2015-01-06 12:56:37 +0530 | [diff] [blame] | 493 | frappe.throw(_("Cannot overbill for Item {0} in row {1} more than {2}. To allow overbilling, please set in Stock Settings").format(item.item_code, item.idx, max_allowed_amt)) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 494 | |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 495 | def get_company_default(self, fieldname): |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 496 | from erpnext.accounts.utils import get_company_default |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 497 | return get_company_default(self.company, fieldname) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 498 | |
Nabin Hait | a36adbd | 2013-08-02 14:50:12 +0530 | [diff] [blame] | 499 | def get_stock_items(self): |
| 500 | stock_items = [] |
Anand Doshi | a740f75 | 2014-06-25 13:31:02 +0530 | [diff] [blame] | 501 | item_codes = list(set(item.item_code for item in self.get(self.fname))) |
Nabin Hait | a36adbd | 2013-08-02 14:50:12 +0530 | [diff] [blame] | 502 | if item_codes: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 503 | stock_items = [r[0] for r in frappe.db.sql("""select name |
Nabin Hait | a36adbd | 2013-08-02 14:50:12 +0530 | [diff] [blame] | 504 | from `tabItem` where name in (%s) and is_stock_item='Yes'""" % \ |
| 505 | (", ".join((["%s"]*len(item_codes))),), item_codes)] |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 506 | |
Nabin Hait | a36adbd | 2013-08-02 14:50:12 +0530 | [diff] [blame] | 507 | return stock_items |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 508 | |
Ankit Javalkar | 8e7ca41 | 2014-09-12 15:18:53 +0530 | [diff] [blame] | 509 | def set_total_advance_paid(self): |
| 510 | if self.doctype == "Sales Order": |
| 511 | dr_or_cr = "credit" |
| 512 | against_field = "against_sales_order" |
| 513 | else: |
| 514 | dr_or_cr = "debit" |
| 515 | against_field = "against_purchase_order" |
| 516 | |
| 517 | advance_paid = frappe.db.sql(""" |
| 518 | select |
| 519 | sum(ifnull({dr_or_cr}, 0)) |
| 520 | from |
| 521 | `tabJournal Voucher Detail` |
| 522 | where |
| 523 | {against_field} = %s and docstatus = 1 and is_advance = "Yes" """.format(dr_or_cr=dr_or_cr, \ |
| 524 | against_field=against_field), self.name) |
| 525 | |
| 526 | if advance_paid: |
| 527 | advance_paid = flt(advance_paid[0][0], self.precision("advance_paid")) |
| 528 | if flt(self.grand_total) >= advance_paid: |
| 529 | frappe.db.set_value(self.doctype, self.name, "advance_paid", advance_paid) |
| 530 | else: |
| 531 | frappe.throw(_("Total advance ({0}) against Order {1} cannot be greater \ |
| 532 | than the Grand Total ({2})") |
| 533 | .format(advance_paid, self.name, self.grand_total)) |
| 534 | |
ankitjavalkarwork | e60822b | 2014-08-26 14:29:06 +0530 | [diff] [blame] | 535 | @property |
| 536 | def company_abbr(self): |
| 537 | if not hasattr(self, "_abbr"): |
| 538 | self._abbr = frappe.db.get_value("Company", self.company, "abbr") |
| 539 | |
| 540 | return self._abbr |
| 541 | |
| 542 | def check_credit_limit(self, account): |
| 543 | total_outstanding = frappe.db.sql(""" |
| 544 | select sum(ifnull(debit, 0)) - sum(ifnull(credit, 0)) |
| 545 | from `tabGL Entry` where account = %s""", account) |
| 546 | |
| 547 | total_outstanding = total_outstanding[0][0] if total_outstanding else 0 |
| 548 | if total_outstanding: |
| 549 | frappe.get_doc('Account', account).check_credit_limit(total_outstanding) |
| 550 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 551 | @frappe.whitelist() |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 552 | def get_tax_rate(account_head): |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 553 | return frappe.db.get_value("Account", account_head, "tax_rate") |
Nabin Hait | 6b03914 | 2014-05-02 15:45:10 +0530 | [diff] [blame] | 554 | |
| 555 | @frappe.whitelist() |
| 556 | def get_taxes_and_charges(master_doctype, master_name, tax_parentfield): |
| 557 | from frappe.model import default_fields |
| 558 | tax_master = frappe.get_doc(master_doctype, master_name) |
| 559 | |
| 560 | taxes_and_charges = [] |
| 561 | for i, tax in enumerate(tax_master.get(tax_parentfield)): |
| 562 | tax = tax.as_dict() |
| 563 | |
| 564 | for fieldname in default_fields: |
| 565 | if fieldname in tax: |
| 566 | del tax[fieldname] |
| 567 | |
| 568 | taxes_and_charges.append(tax) |
| 569 | |
| 570 | return taxes_and_charges |