Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe 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 | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 5 | import frappe, erpnext |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 6 | import json |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 7 | from frappe import _, throw |
tundebabzy | ad08d4c | 2018-05-16 07:01:41 +0100 | [diff] [blame] | 8 | from frappe.utils import today, flt, cint, fmt_money, formatdate, getdate, add_days, add_months, get_last_day, nowdate |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 9 | from erpnext.setup.utils import get_exchange_rate |
Nabin Hait | 2ed0b59 | 2016-03-29 13:14:17 +0530 | [diff] [blame] | 10 | from erpnext.accounts.utils import get_fiscal_years, validate_fiscal_year, get_account_currency |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 11 | from erpnext.utilities.transaction_base import TransactionBase |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 12 | from erpnext.buying.utils import update_last_purchase_rate |
Nabin Hait | 3cf67a4 | 2015-07-24 13:26:36 +0530 | [diff] [blame] | 13 | from erpnext.controllers.sales_and_purchase_return import validate_return |
shreyas | 29b565f | 2016-01-25 17:30:49 +0530 | [diff] [blame] | 14 | from erpnext.accounts.party import get_party_account_currency, validate_party_frozen_disabled |
shreyas | f76853a | 2016-01-25 21:25:11 +0530 | [diff] [blame] | 15 | from erpnext.exceptions import InvalidCurrency |
Achilles Rasquinha | 9050998 | 2018-03-08 12:55:41 +0530 | [diff] [blame] | 16 | from six import text_type |
Nabin Hait | 1d21842 | 2015-07-17 15:19:02 +0530 | [diff] [blame] | 17 | |
Rohit Waghchaure | a02640e | 2018-05-16 10:49:12 +0530 | [diff] [blame] | 18 | force_item_fields = ("item_group", "brand", "stock_uom", "is_fixed_asset") |
Rushabh Mehta | b16b9cd | 2015-08-03 16:13:33 +0530 | [diff] [blame] | 19 | |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 20 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 21 | class AccountsController(TransactionBase): |
Nabin Hait | 7eba1a3 | 2017-10-02 15:59:27 +0530 | [diff] [blame] | 22 | def __init__(self, *args, **kwargs): |
| 23 | super(AccountsController, self).__init__(*args, **kwargs) |
Anand Doshi | 979326b | 2015-09-11 16:22:37 +0530 | [diff] [blame] | 24 | |
Nabin Hait | 4ffd7f3 | 2015-08-27 12:28:36 +0530 | [diff] [blame] | 25 | @property |
| 26 | def company_currency(self): |
| 27 | if not hasattr(self, "__company_currency"): |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 28 | self.__company_currency = erpnext.get_company_currency(self.company) |
Anand Doshi | 979326b | 2015-09-11 16:22:37 +0530 | [diff] [blame] | 29 | |
Nabin Hait | 4ffd7f3 | 2015-08-27 12:28:36 +0530 | [diff] [blame] | 30 | return self.__company_currency |
Anand Doshi | 979326b | 2015-09-11 16:22:37 +0530 | [diff] [blame] | 31 | |
Rohit Waghchaure | 7d52989 | 2016-10-06 14:35:04 +0530 | [diff] [blame] | 32 | def onload(self): |
Nabin Hait | 0551f7b | 2017-11-21 19:58:16 +0530 | [diff] [blame] | 33 | self.get("__onload").make_payment_via_journal_entry \ |
| 34 | = frappe.db.get_single_value('Accounts Settings', 'make_payment_via_journal_entry') |
| 35 | |
tunde | e52bb82 | 2017-09-25 09:02:23 +0100 | [diff] [blame] | 36 | if self.is_new(): |
Nabin Hait | 0551f7b | 2017-11-21 19:58:16 +0530 | [diff] [blame] | 37 | relevant_docs = ("Quotation", "Purchase Order", "Sales Order", |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 38 | "Purchase Invoice", "Sales Invoice") |
tunde | e52bb82 | 2017-09-25 09:02:23 +0100 | [diff] [blame] | 39 | if self.doctype in relevant_docs: |
| 40 | self.set_payment_schedule() |
Rohit Waghchaure | 7d52989 | 2016-10-06 14:35:04 +0530 | [diff] [blame] | 41 | |
tundebabzy | ad08d4c | 2018-05-16 07:01:41 +0100 | [diff] [blame] | 42 | def ensure_supplier_is_not_blocked(self): |
| 43 | is_supplier_payment = self.doctype == 'Payment Entry' and self.party_type == 'Supplier' |
| 44 | is_buying_invoice = self.doctype in ['Purchase Invoice', 'Purchase Order'] |
| 45 | supplier = None |
| 46 | supplier_name = None |
| 47 | |
| 48 | if is_buying_invoice or is_supplier_payment: |
| 49 | supplier_name = self.supplier if is_buying_invoice else self.party |
| 50 | supplier = frappe.get_doc('Supplier', supplier_name) |
| 51 | |
| 52 | if supplier and supplier_name and supplier.on_hold: |
| 53 | if (is_buying_invoice and supplier.hold_type in ['All', 'Invoices']) or \ |
| 54 | (is_supplier_payment and supplier.hold_type in ['All', 'Payments']): |
| 55 | if not supplier.release_date or getdate(nowdate()) <= supplier.release_date: |
| 56 | frappe.msgprint( |
| 57 | _('{0} is blocked so this transaction cannot proceed'.format(supplier_name)), raise_exception=1) |
| 58 | |
Saurabh | 6f75318 | 2013-03-20 12:55:28 +0530 | [diff] [blame] | 59 | def validate(self): |
Anurag Mishra | e657fe8 | 2018-11-26 15:19:17 +0530 | [diff] [blame] | 60 | |
| 61 | self.validate_qty_is_not_zero() |
nabinhait | 23cce73 | 2014-07-03 12:25:06 +0530 | [diff] [blame] | 62 | if self.get("_action") and self._action != "update_after_submit": |
Nabin Hait | 704a453 | 2014-06-05 16:55:31 +0530 | [diff] [blame] | 63 | self.set_missing_values(for_validate=True) |
tunde | 62af5c5 | 2017-09-22 15:16:38 +0100 | [diff] [blame] | 64 | |
tundebabzy | ad08d4c | 2018-05-16 07:01:41 +0100 | [diff] [blame] | 65 | self.ensure_supplier_is_not_blocked() |
| 66 | |
Nabin Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 67 | self.validate_date_with_fiscal_year() |
Rushabh Mehta | b16b9cd | 2015-08-03 16:13:33 +0530 | [diff] [blame] | 68 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 69 | if self.meta.get_field("currency"): |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 70 | self.calculate_taxes_and_totals() |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 71 | |
Nabin Hait | 1d21842 | 2015-07-17 15:19:02 +0530 | [diff] [blame] | 72 | if not self.meta.get_field("is_return") or not self.is_return: |
| 73 | self.validate_value("base_grand_total", ">=", 0) |
Rushabh Mehta | b16b9cd | 2015-08-03 16:13:33 +0530 | [diff] [blame] | 74 | |
Nabin Hait | 3cf67a4 | 2015-07-24 13:26:36 +0530 | [diff] [blame] | 75 | validate_return(self) |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 76 | self.set_total_in_words() |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 77 | |
tunde | 62af5c5 | 2017-09-22 15:16:38 +0100 | [diff] [blame] | 78 | self.validate_all_documents_schedule() |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 79 | |
ankitjavalkarwork | 6c29d87 | 2014-10-06 13:20:53 +0530 | [diff] [blame] | 80 | if self.meta.get_field("taxes_and_charges"): |
| 81 | self.validate_enabled_taxes_and_charges() |
Nabin Hait | a2426fc | 2018-01-15 17:45:46 +0530 | [diff] [blame] | 82 | self.validate_tax_account_company() |
Rushabh Mehta | b16b9cd | 2015-08-03 16:13:33 +0530 | [diff] [blame] | 83 | |
Neil Trini Lasrado | 3fbbb71 | 2015-07-17 12:10:12 +0530 | [diff] [blame] | 84 | self.validate_party() |
Nabin Hait | 895029d | 2015-08-20 14:55:39 +0530 | [diff] [blame] | 85 | self.validate_currency() |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 86 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 87 | if self.doctype == 'Purchase Invoice': |
| 88 | self.validate_paid_amount() |
| 89 | |
Nabin Hait | 041a5c2 | 2018-08-01 18:07:39 +0530 | [diff] [blame] | 90 | if self.doctype in ['Purchase Invoice', 'Sales Invoice']: |
| 91 | if cint(self.allocate_advances_automatically): |
| 92 | self.set_advances() |
| 93 | |
| 94 | if self.is_return: |
| 95 | self.validate_qty() |
rohitwaghchaure | 7048925 | 2018-06-11 12:02:14 +0530 | [diff] [blame] | 96 | |
tunde | 62af5c5 | 2017-09-22 15:16:38 +0100 | [diff] [blame] | 97 | def validate_invoice_documents_schedule(self): |
Nabin Hait | 0551f7b | 2017-11-21 19:58:16 +0530 | [diff] [blame] | 98 | self.validate_payment_schedule_dates() |
| 99 | self.set_due_date() |
Nabin Hait | 0551f7b | 2017-11-21 19:58:16 +0530 | [diff] [blame] | 100 | self.set_payment_schedule() |
| 101 | self.validate_payment_schedule_amount() |
tunde | 62af5c5 | 2017-09-22 15:16:38 +0100 | [diff] [blame] | 102 | self.validate_due_date() |
| 103 | self.validate_advance_entries() |
| 104 | |
| 105 | def validate_non_invoice_documents_schedule(self): |
Nabin Hait | 0551f7b | 2017-11-21 19:58:16 +0530 | [diff] [blame] | 106 | self.set_payment_schedule() |
Nabin Hait | 2f9f4f9 | 2017-12-20 12:24:59 +0530 | [diff] [blame] | 107 | self.validate_payment_schedule_dates() |
Nabin Hait | 0551f7b | 2017-11-21 19:58:16 +0530 | [diff] [blame] | 108 | self.validate_payment_schedule_amount() |
tunde | 62af5c5 | 2017-09-22 15:16:38 +0100 | [diff] [blame] | 109 | |
| 110 | def validate_all_documents_schedule(self): |
| 111 | if self.doctype in ("Sales Invoice", "Purchase Invoice") and not self.is_return: |
| 112 | self.validate_invoice_documents_schedule() |
| 113 | elif self.doctype in ("Quotation", "Purchase Order", "Sales Order"): |
| 114 | self.validate_non_invoice_documents_schedule() |
| 115 | |
KanchanChauhan | 49a50fe | 2016-11-18 13:57:53 +0530 | [diff] [blame] | 116 | def before_print(self): |
Zarrar | b9f54ca | 2018-05-28 17:41:09 +0530 | [diff] [blame] | 117 | if self.doctype in ['Purchase Order', 'Sales Order', 'Sales Invoice', 'Purchase Invoice', |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 118 | 'Supplier Quotation', 'Purchase Receipt', 'Delivery Note', 'Quotation']: |
KanchanChauhan | 49a50fe | 2016-11-18 13:57:53 +0530 | [diff] [blame] | 119 | if self.get("group_same_items"): |
| 120 | self.group_similar_items() |
| 121 | |
Zarrar | 5be6d19 | 2018-11-08 12:16:26 +0530 | [diff] [blame] | 122 | df = self.meta.get_field("discount_amount") |
| 123 | if self.get("discount_amount") and hasattr(self, "taxes") and not len(self.taxes): |
| 124 | df.set("print_hide", 0) |
| 125 | self.discount_amount = -self.discount_amount |
| 126 | else: |
| 127 | df.set("print_hide", 1) |
| 128 | |
Saurabh | 43520f9 | 2016-03-21 18:32:48 +0530 | [diff] [blame] | 129 | def validate_paid_amount(self): |
| 130 | if hasattr(self, "is_pos") or hasattr(self, "is_paid"): |
| 131 | is_paid = self.get("is_pos") or self.get("is_paid") |
| 132 | if cint(is_paid) == 1: |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 133 | if flt(self.paid_amount) == 0 and flt(self.outstanding_amount) > 0: |
Saurabh | 43520f9 | 2016-03-21 18:32:48 +0530 | [diff] [blame] | 134 | if self.cash_bank_account: |
Nabin Hait | 877e1bb | 2017-11-17 12:27:43 +0530 | [diff] [blame] | 135 | self.paid_amount = flt(flt(self.outstanding_amount), self.precision("paid_amount")) |
| 136 | self.base_paid_amount = flt(self.paid_amount * self.conversion_rate, |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 137 | self.precision("base_paid_amount")) |
Saurabh | 43520f9 | 2016-03-21 18:32:48 +0530 | [diff] [blame] | 138 | else: |
| 139 | # show message that the amount is not paid |
Nabin Hait | ffd1e4e | 2016-04-15 14:42:08 +0530 | [diff] [blame] | 140 | self.paid_amount = 0 |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 141 | frappe.throw( |
| 142 | _("Note: Payment Entry will not be created since 'Cash or Bank Account' was not specified")) |
Saurabh | 43520f9 | 2016-03-21 18:32:48 +0530 | [diff] [blame] | 143 | else: |
tunde | 62af5c5 | 2017-09-22 15:16:38 +0100 | [diff] [blame] | 144 | frappe.db.set(self, 'paid_amount', 0) |
Saurabh | 43520f9 | 2016-03-21 18:32:48 +0530 | [diff] [blame] | 145 | |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 146 | def set_missing_values(self, for_validate=False): |
Rohit Waghchaure | 5d97d89 | 2016-05-12 12:58:29 +0530 | [diff] [blame] | 147 | if frappe.flags.in_test: |
tunde | 62af5c5 | 2017-09-22 15:16:38 +0100 | [diff] [blame] | 148 | for fieldname in ["posting_date", "transaction_date"]: |
Rohit Waghchaure | 5d97d89 | 2016-05-12 12:58:29 +0530 | [diff] [blame] | 149 | if self.meta.get_field(fieldname) and not self.get(fieldname): |
| 150 | self.set(fieldname, today()) |
| 151 | break |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 152 | |
Nabin Hait | 3237c75 | 2015-02-17 11:11:11 +0530 | [diff] [blame] | 153 | def calculate_taxes_and_totals(self): |
Nabin Hait | fe81da2 | 2015-02-18 12:23:18 +0530 | [diff] [blame] | 154 | from erpnext.controllers.taxes_and_totals import calculate_taxes_and_totals |
| 155 | calculate_taxes_and_totals(self) |
Nabin Hait | 3237c75 | 2015-02-17 11:11:11 +0530 | [diff] [blame] | 156 | |
| 157 | if self.doctype in ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]: |
| 158 | self.calculate_commission() |
| 159 | self.calculate_contribution() |
| 160 | |
Nabin Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 161 | def validate_date_with_fiscal_year(self): |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 162 | if self.meta.get_field("fiscal_year"): |
Nabin Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 163 | date_field = "" |
| 164 | if self.meta.get_field("posting_date"): |
| 165 | date_field = "posting_date" |
| 166 | elif self.meta.get_field("transaction_date"): |
| 167 | date_field = "transaction_date" |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 168 | |
Rushabh Mehta | f2227d0 | 2014-03-31 23:37:40 +0530 | [diff] [blame] | 169 | if date_field and self.get(date_field): |
Rushabh Mehta | 6695830 | 2017-01-16 16:57:53 +0530 | [diff] [blame] | 170 | validate_fiscal_year(self.get(date_field), self.fiscal_year, self.company, |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 171 | self.meta.get_label(date_field), self) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 172 | |
Nabin Hait | e9daefe | 2014-08-27 16:46:33 +0530 | [diff] [blame] | 173 | def validate_due_date(self): |
rohitwaghchaure | ea943c6 | 2018-07-06 12:28:58 +0530 | [diff] [blame] | 174 | if self.get('is_pos'): return |
| 175 | |
Nabin Hait | e9daefe | 2014-08-27 16:46:33 +0530 | [diff] [blame] | 176 | from erpnext.accounts.party import validate_due_date |
| 177 | if self.doctype == "Sales Invoice": |
Nabin Hait | 04d244a | 2015-07-22 17:47:49 +0530 | [diff] [blame] | 178 | if not self.due_date: |
| 179 | frappe.throw(_("Due Date is mandatory")) |
Rushabh Mehta | b16b9cd | 2015-08-03 16:13:33 +0530 | [diff] [blame] | 180 | |
rohitwaghchaure | 3ffe896 | 2018-07-13 17:40:48 +0530 | [diff] [blame] | 181 | validate_due_date(self.posting_date, self.due_date, |
| 182 | "Customer", self.customer, self.company, self.payment_terms_template) |
Nabin Hait | e9daefe | 2014-08-27 16:46:33 +0530 | [diff] [blame] | 183 | elif self.doctype == "Purchase Invoice": |
Rohit Waghchaure | d1a85a3 | 2018-11-26 18:42:29 +0530 | [diff] [blame] | 184 | validate_due_date(self.bill_date or self.posting_date, self.due_date, |
Saurabh | d7897f1 | 2018-07-18 17:08:16 +0530 | [diff] [blame] | 185 | "Supplier", self.supplier, self.company, self.bill_date, self.payment_terms_template) |
Nabin Hait | e9daefe | 2014-08-27 16:46:33 +0530 | [diff] [blame] | 186 | |
Nabin Hait | 096d363 | 2013-10-17 17:01:14 +0530 | [diff] [blame] | 187 | def set_price_list_currency(self, buying_or_selling): |
Nabin Hait | 1cc55fb | 2016-12-08 14:09:23 +0530 | [diff] [blame] | 188 | if self.meta.get_field("posting_date"): |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 189 | transaction_date = self.posting_date |
Nabin Hait | 1cc55fb | 2016-12-08 14:09:23 +0530 | [diff] [blame] | 190 | else: |
Nabin Hait | 288a18e | 2016-12-08 15:36:23 +0530 | [diff] [blame] | 191 | transaction_date = self.transaction_date |
Rushabh Mehta | 6695830 | 2017-01-16 16:57:53 +0530 | [diff] [blame] | 192 | |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 193 | if self.meta.get_field("currency"): |
Nabin Hait | 7a75e10 | 2013-09-17 10:21:20 +0530 | [diff] [blame] | 194 | # price list part |
Shreya | 3f77852 | 2018-05-15 16:59:20 +0530 | [diff] [blame] | 195 | if buying_or_selling.lower() == "selling": |
| 196 | fieldname = "selling_price_list" |
| 197 | args = "for_selling" |
| 198 | else: |
| 199 | fieldname = "buying_price_list" |
| 200 | args = "for_buying" |
| 201 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 202 | if self.meta.get_field(fieldname) and self.get(fieldname): |
| 203 | self.price_list_currency = frappe.db.get_value("Price List", |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 204 | self.get(fieldname), "currency") |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 205 | |
Nabin Hait | 6e439a5 | 2015-08-28 19:24:22 +0530 | [diff] [blame] | 206 | if self.price_list_currency == self.company_currency: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 207 | self.plc_conversion_rate = 1.0 |
Nabin Hait | 11f4195 | 2013-09-24 14:36:55 +0530 | [diff] [blame] | 208 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 209 | elif not self.plc_conversion_rate: |
Rushabh Mehta | 6695830 | 2017-01-16 16:57:53 +0530 | [diff] [blame] | 210 | self.plc_conversion_rate = get_exchange_rate(self.price_list_currency, |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 211 | self.company_currency, transaction_date, args) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 212 | |
Nabin Hait | 7a75e10 | 2013-09-17 10:21:20 +0530 | [diff] [blame] | 213 | # currency |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 214 | if not self.currency: |
| 215 | self.currency = self.price_list_currency |
| 216 | self.conversion_rate = self.plc_conversion_rate |
Nabin Hait | 6e439a5 | 2015-08-28 19:24:22 +0530 | [diff] [blame] | 217 | elif self.currency == self.company_currency: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 218 | self.conversion_rate = 1.0 |
| 219 | elif not self.conversion_rate: |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 220 | self.conversion_rate = get_exchange_rate(self.currency, |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 221 | self.company_currency, transaction_date, args) |
Nabin Hait | 7a75e10 | 2013-09-17 10:21:20 +0530 | [diff] [blame] | 222 | |
Nabin Hait | cccc45e | 2016-10-05 17:15:43 +0530 | [diff] [blame] | 223 | def set_missing_item_details(self, for_validate=False): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 224 | """set missing item values""" |
Nabin Hait | 0aa71a5 | 2014-02-11 16:14:52 +0530 | [diff] [blame] | 225 | from erpnext.stock.get_item_details import get_item_details |
Nabin Hait | f37d43d | 2017-07-18 12:14:42 +0530 | [diff] [blame] | 226 | from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 227 | |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 228 | if hasattr(self, "items"): |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 229 | parent_dict = {} |
Pratik Vyas | f7daab7 | 2014-04-10 17:53:30 +0530 | [diff] [blame] | 230 | for fieldname in self.meta.get_valid_columns(): |
| 231 | parent_dict[fieldname] = self.get(fieldname) |
| 232 | |
mbauskar | a52472c | 2016-03-05 15:10:25 +0530 | [diff] [blame] | 233 | if self.doctype in ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]: |
| 234 | document_type = "{} Item".format(self.doctype) |
mbauskar | c97becb | 2016-01-18 16:28:21 +0530 | [diff] [blame] | 235 | parent_dict.update({"document_type": document_type}) |
| 236 | |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 237 | for item in self.get("items"): |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 238 | if item.get("item_code"): |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 239 | args = parent_dict.copy() |
| 240 | args.update(item.as_dict()) |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 241 | |
Nabin Hait | 34d2822 | 2016-01-19 15:45:49 +0530 | [diff] [blame] | 242 | args["doctype"] = self.doctype |
| 243 | args["name"] = self.name |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 244 | |
Nabin Hait | e2f054c | 2015-03-09 14:54:37 +0530 | [diff] [blame] | 245 | if not args.get("transaction_date"): |
| 246 | args["transaction_date"] = args.get("posting_date") |
Anand Doshi | 2b2b639 | 2015-03-20 14:18:09 +0530 | [diff] [blame] | 247 | |
| 248 | if self.get("is_subcontracted"): |
| 249 | args["is_subcontracted"] = self.is_subcontracted |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 250 | ret = get_item_details(args) |
| 251 | for fieldname, value in ret.items(): |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 252 | if item.meta.get_field(fieldname) and value is not None: |
| 253 | if (item.get(fieldname) is None or fieldname in force_item_fields): |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 254 | item.set(fieldname, value) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 255 | |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 256 | elif fieldname in ['cost_center', 'conversion_factor'] and not item.get(fieldname): |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 257 | item.set(fieldname, value) |
| 258 | |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 259 | elif fieldname == "serial_no": |
Alchez | 9155e40 | 2018-07-09 16:57:42 +0530 | [diff] [blame] | 260 | # Ensure that serial numbers are matched against Stock UOM |
| 261 | item_conversion_factor = item.get("conversion_factor") or 1.0 |
| 262 | item_qty = abs(item.get("qty")) * item_conversion_factor |
Alchez | 8f2a0f3 | 2018-07-06 14:36:52 +0530 | [diff] [blame] | 263 | |
| 264 | if item_qty != len(get_serial_nos(item.get('serial_no'))): |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 265 | item.set(fieldname, value) |
Nabin Hait | a74468b | 2014-12-30 18:33:52 +0530 | [diff] [blame] | 266 | |
Rohit Waghchaure | f6f503a | 2018-12-19 15:06:44 +0530 | [diff] [blame] | 267 | if self.doctype in ["Purchase Invoice", "Sales Invoice"] and item.meta.get_field('is_fixed_asset'): |
| 268 | item.set('is_fixed_asset', ret.get('is_fixed_asset', 0)) |
| 269 | |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 270 | if ret.get("pricing_rule"): |
mbauskar | 46c97c6 | 2016-01-19 19:51:11 +0530 | [diff] [blame] | 271 | # if user changed the discount percentage then set user's discount percentage ? |
Nabin Hait | da32916 | 2019-01-22 19:06:06 +0530 | [diff] [blame] | 272 | item.set("pricing_rule", ret.get("pricing_rule")) |
mbauskar | a52472c | 2016-03-05 15:10:25 +0530 | [diff] [blame] | 273 | item.set("discount_percentage", ret.get("discount_percentage")) |
Nabin Hait | da32916 | 2019-01-22 19:06:06 +0530 | [diff] [blame] | 274 | if ret.get("pricing_rule_for") == "Rate": |
| 275 | item.set("price_list_rate", ret.get("price_list_rate")) |
Anand Doshi | 979326b | 2015-09-11 16:22:37 +0530 | [diff] [blame] | 276 | |
Anand Doshi | 53a0de7 | 2015-09-22 15:26:15 +0530 | [diff] [blame] | 277 | if item.price_list_rate: |
| 278 | item.rate = flt(item.price_list_rate * |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 279 | (1.0 - (flt(item.discount_percentage) / 100.0)), item.precision("rate")) |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 280 | |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 281 | if self.doctype == "Purchase Invoice": |
Nabin Hait | cccc45e | 2016-10-05 17:15:43 +0530 | [diff] [blame] | 282 | self.set_expense_account(for_validate) |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 283 | |
Nabin Hait | ffc7f3f | 2015-05-12 15:07:02 +0530 | [diff] [blame] | 284 | def set_taxes(self): |
| 285 | if not self.meta.get_field("taxes"): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 286 | return |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 287 | |
Nabin Hait | ffc7f3f | 2015-05-12 15:07:02 +0530 | [diff] [blame] | 288 | tax_master_doctype = self.meta.get_field("taxes_and_charges").options |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 289 | |
rohitwaghchaure | 57914f1 | 2018-04-24 19:19:47 +0530 | [diff] [blame] | 290 | if (self.is_new() or self.is_pos_profile_changed()) and not self.get("taxes"): |
rohitwaghchaure | 505661b | 2017-12-11 14:52:28 +0530 | [diff] [blame] | 291 | if self.company and not self.get("taxes_and_charges"): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 292 | # get the default tax master |
rohitwaghchaure | 505661b | 2017-12-11 14:52:28 +0530 | [diff] [blame] | 293 | self.taxes_and_charges = frappe.db.get_value(tax_master_doctype, |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 294 | {"is_default": 1, 'company': self.company}) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 295 | |
Nabin Hait | ffc7f3f | 2015-05-12 15:07:02 +0530 | [diff] [blame] | 296 | self.append_taxes_from_master(tax_master_doctype) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 297 | |
rohitwaghchaure | 57914f1 | 2018-04-24 19:19:47 +0530 | [diff] [blame] | 298 | def is_pos_profile_changed(self): |
| 299 | if (self.doctype == 'Sales Invoice' and self.is_pos and |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 300 | self.pos_profile != frappe.db.get_value('Sales Invoice', self.name, 'pos_profile')): |
rohitwaghchaure | 57914f1 | 2018-04-24 19:19:47 +0530 | [diff] [blame] | 301 | return True |
| 302 | |
Nabin Hait | ffc7f3f | 2015-05-12 15:07:02 +0530 | [diff] [blame] | 303 | def append_taxes_from_master(self, tax_master_doctype=None): |
| 304 | if self.get("taxes_and_charges"): |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 305 | if not tax_master_doctype: |
Nabin Hait | ffc7f3f | 2015-05-12 15:07:02 +0530 | [diff] [blame] | 306 | tax_master_doctype = self.meta.get_field("taxes_and_charges").options |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 307 | |
Nabin Hait | ffc7f3f | 2015-05-12 15:07:02 +0530 | [diff] [blame] | 308 | self.extend("taxes", get_taxes_and_charges(tax_master_doctype, self.get("taxes_and_charges"))) |
Akhilesh Darjee | 4cdb799 | 2014-01-30 13:56:57 +0530 | [diff] [blame] | 309 | |
Anand Doshi | ac32bad | 2014-04-18 01:30:14 +0530 | [diff] [blame] | 310 | def set_other_charges(self): |
Nabin Hait | e7d1536 | 2014-12-25 16:01:55 +0530 | [diff] [blame] | 311 | self.set("taxes", []) |
Nabin Hait | ffc7f3f | 2015-05-12 15:07:02 +0530 | [diff] [blame] | 312 | self.set_taxes() |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 313 | |
ankitjavalkarwork | 6c29d87 | 2014-10-06 13:20:53 +0530 | [diff] [blame] | 314 | def validate_enabled_taxes_and_charges(self): |
| 315 | taxes_and_charges_doctype = self.meta.get_options("taxes_and_charges") |
| 316 | if frappe.db.get_value(taxes_and_charges_doctype, self.taxes_and_charges, "disabled"): |
| 317 | frappe.throw(_("{0} '{1}' is disabled").format(taxes_and_charges_doctype, self.taxes_and_charges)) |
| 318 | |
Nabin Hait | a2426fc | 2018-01-15 17:45:46 +0530 | [diff] [blame] | 319 | def validate_tax_account_company(self): |
| 320 | for d in self.get("taxes"): |
| 321 | if d.account_head: |
| 322 | tax_account_company = frappe.db.get_value("Account", d.account_head, "company") |
| 323 | if tax_account_company != self.company: |
| 324 | frappe.throw(_("Row #{0}: Account {1} does not belong to company {2}") |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 325 | .format(d.idx, d.account_head, self.company)) |
Nabin Hait | a2426fc | 2018-01-15 17:45:46 +0530 | [diff] [blame] | 326 | |
Nabin Hait | 46bcbaf | 2015-08-19 13:49:10 +0530 | [diff] [blame] | 327 | def get_gl_dict(self, args, account_currency=None): |
Nabin Hait | 2df4d54 | 2013-01-29 11:34:39 +0530 | [diff] [blame] | 328 | """this method populates the common properties of a gl entry record""" |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 329 | |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 330 | posting_date = args.get('posting_date') or self.get('posting_date') |
| 331 | fiscal_years = get_fiscal_years(posting_date, company=self.company) |
Nabin Hait | 2ed0b59 | 2016-03-29 13:14:17 +0530 | [diff] [blame] | 332 | if len(fiscal_years) > 1: |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 333 | frappe.throw(_("Multiple fiscal years exist for the date {0}. Please set company in Fiscal Year").format( |
| 334 | formatdate(posting_date))) |
Nabin Hait | 2ed0b59 | 2016-03-29 13:14:17 +0530 | [diff] [blame] | 335 | else: |
| 336 | fiscal_year = fiscal_years[0][0] |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 337 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 338 | gl_dict = frappe._dict({ |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 339 | 'company': self.company, |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 340 | 'posting_date': posting_date, |
Nabin Hait | 2ed0b59 | 2016-03-29 13:14:17 +0530 | [diff] [blame] | 341 | 'fiscal_year': fiscal_year, |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 342 | 'voucher_type': self.doctype, |
| 343 | 'voucher_no': self.name, |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 344 | 'remarks': self.get("remarks"), |
Nabin Hait | 2df4d54 | 2013-01-29 11:34:39 +0530 | [diff] [blame] | 345 | 'debit': 0, |
| 346 | 'credit': 0, |
Nabin Hait | 46bcbaf | 2015-08-19 13:49:10 +0530 | [diff] [blame] | 347 | 'debit_in_account_currency': 0, |
| 348 | 'credit_in_account_currency': 0, |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 349 | 'is_opening': self.get("is_opening") or "No", |
Nabin Hait | 3225102 | 2014-08-29 11:18:32 +0530 | [diff] [blame] | 350 | 'party_type': None, |
Nabin Hait | 591a5ab | 2016-05-26 17:41:39 +0530 | [diff] [blame] | 351 | 'party': None, |
| 352 | 'project': self.get("project") |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 353 | }) |
Nabin Hait | 2df4d54 | 2013-01-29 11:34:39 +0530 | [diff] [blame] | 354 | gl_dict.update(args) |
Anand Doshi | 979326b | 2015-09-11 16:22:37 +0530 | [diff] [blame] | 355 | |
Nabin Hait | 895029d | 2015-08-20 14:55:39 +0530 | [diff] [blame] | 356 | if not account_currency: |
Anand Doshi | cd0989e | 2015-09-28 13:31:17 +0530 | [diff] [blame] | 357 | account_currency = get_account_currency(gl_dict.account) |
Anand Doshi | 979326b | 2015-09-11 16:22:37 +0530 | [diff] [blame] | 358 | |
Rushabh Mehta | bc4e2cd | 2017-10-17 12:30:34 +0530 | [diff] [blame] | 359 | if gl_dict.account and self.doctype not in ["Journal Entry", |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 360 | "Period Closing Voucher", "Payment Entry"]: |
Anand Doshi | be6cfdd | 2015-09-17 21:07:04 +0530 | [diff] [blame] | 361 | self.validate_account_currency(gl_dict.account, account_currency) |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 362 | set_balance_in_account_currency(gl_dict, account_currency, self.get("conversion_rate"), |
| 363 | self.company_currency) |
Anand Doshi | 979326b | 2015-09-11 16:22:37 +0530 | [diff] [blame] | 364 | |
Nabin Hait | c561a49 | 2015-08-19 19:22:34 +0530 | [diff] [blame] | 365 | return gl_dict |
Anand Doshi | 979326b | 2015-09-11 16:22:37 +0530 | [diff] [blame] | 366 | |
Anurag Mishra | e657fe8 | 2018-11-26 15:19:17 +0530 | [diff] [blame] | 367 | def validate_qty_is_not_zero(self): |
| 368 | for item in self.items: |
| 369 | if not item.qty: |
| 370 | frappe.throw("Item quantity can not be zero") |
| 371 | |
Nabin Hait | 895029d | 2015-08-20 14:55:39 +0530 | [diff] [blame] | 372 | def validate_account_currency(self, account, account_currency=None): |
Nabin Hait | 6e439a5 | 2015-08-28 19:24:22 +0530 | [diff] [blame] | 373 | valid_currency = [self.company_currency] |
| 374 | if self.get("currency") and self.currency != self.company_currency: |
| 375 | valid_currency.append(self.currency) |
| 376 | |
Nabin Hait | 895029d | 2015-08-20 14:55:39 +0530 | [diff] [blame] | 377 | if account_currency not in valid_currency: |
Nabin Hait | 4ffd7f3 | 2015-08-27 12:28:36 +0530 | [diff] [blame] | 378 | frappe.throw(_("Account {0} is invalid. Account Currency must be {1}") |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 379 | .format(account, _(" or ").join(valid_currency))) |
Anand Doshi | 979326b | 2015-09-11 16:22:37 +0530 | [diff] [blame] | 380 | |
Anand Doshi | 613cb6a | 2013-02-06 17:33:46 +0530 | [diff] [blame] | 381 | def clear_unallocated_advances(self, childtype, parentfield): |
Rushabh Mehta | f191f85 | 2014-04-02 18:09:34 +0530 | [diff] [blame] | 382 | self.set(parentfield, self.get(parentfield, {"allocated_amount": ["not in", [0, None, ""]]})) |
| 383 | |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 384 | frappe.db.sql("""delete from `tab%s` where parentfield=%s and parent = %s |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 385 | and allocated_amount = 0""" % (childtype, '%s', '%s'), (parentfield, self.name)) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 386 | |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 387 | def apply_shipping_rule(self): |
| 388 | if self.shipping_rule: |
| 389 | shipping_rule = frappe.get_doc("Shipping Rule", self.shipping_rule) |
| 390 | shipping_rule.apply(self) |
| 391 | self.calculate_taxes_and_totals() |
| 392 | |
| 393 | def get_shipping_address(self): |
| 394 | '''Returns Address object from shipping address fields if present''' |
| 395 | |
| 396 | # shipping address fields can be `shipping_address_name` or `shipping_address` |
| 397 | # try getting value from both |
| 398 | |
| 399 | for fieldname in ('shipping_address_name', 'shipping_address'): |
| 400 | shipping_field = self.meta.get_field(fieldname) |
| 401 | if shipping_field and shipping_field.fieldtype == 'Link': |
| 402 | if self.get(fieldname): |
| 403 | return frappe.get_doc('Address', self.get(fieldname)) |
| 404 | |
| 405 | return {} |
| 406 | |
Nabin Hait | 041a5c2 | 2018-08-01 18:07:39 +0530 | [diff] [blame] | 407 | def set_advances(self): |
| 408 | """Returns list of advances against Account, Party, Reference""" |
| 409 | |
| 410 | res = self.get_advance_entries() |
| 411 | |
| 412 | self.set("advances", []) |
| 413 | advance_allocated = 0 |
| 414 | for d in res: |
| 415 | if d.against_order: |
| 416 | allocated_amount = flt(d.amount) |
| 417 | else: |
Rohit Waghchaure | 6cc2f52 | 2018-11-27 12:07:03 +0530 | [diff] [blame] | 418 | amount = self.rounded_total or self.grand_total |
| 419 | allocated_amount = min(amount - advance_allocated, d.amount) |
Nabin Hait | 041a5c2 | 2018-08-01 18:07:39 +0530 | [diff] [blame] | 420 | advance_allocated += flt(allocated_amount) |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 421 | |
Nabin Hait | 041a5c2 | 2018-08-01 18:07:39 +0530 | [diff] [blame] | 422 | self.append("advances", { |
| 423 | "doctype": self.doctype + " Advance", |
| 424 | "reference_type": d.reference_type, |
| 425 | "reference_name": d.reference_name, |
| 426 | "reference_row": d.reference_row, |
| 427 | "remarks": d.remarks, |
| 428 | "advance_amount": flt(d.amount), |
| 429 | "allocated_amount": allocated_amount |
| 430 | }) |
| 431 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 432 | def get_advance_entries(self, include_unallocated=True): |
| 433 | if self.doctype == "Sales Invoice": |
| 434 | party_account = self.debit_to |
| 435 | party_type = "Customer" |
| 436 | party = self.customer |
| 437 | amount_field = "credit_in_account_currency" |
| 438 | order_field = "sales_order" |
| 439 | order_doctype = "Sales Order" |
| 440 | else: |
| 441 | party_account = self.credit_to |
| 442 | party_type = "Supplier" |
| 443 | party = self.supplier |
| 444 | amount_field = "debit_in_account_currency" |
| 445 | order_field = "purchase_order" |
| 446 | order_doctype = "Purchase Order" |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 447 | |
| 448 | order_list = list(set([d.get(order_field) |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 449 | for d in self.get("items") if d.get(order_field)])) |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 450 | |
| 451 | journal_entries = get_advance_journal_entries(party_type, party, party_account, |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 452 | amount_field, order_doctype, order_list, include_unallocated) |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 453 | |
| 454 | payment_entries = get_advance_payment_entries(party_type, party, party_account, |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 455 | order_doctype, order_list, include_unallocated) |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 456 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 457 | res = journal_entries + payment_entries |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 458 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 459 | return res |
Nabin Hait | 48f5fa6 | 2014-09-18 15:03:54 +0530 | [diff] [blame] | 460 | |
rohitwaghchaure | bf4c114 | 2018-01-08 15:20:15 +0530 | [diff] [blame] | 461 | def is_inclusive_tax(self): |
| 462 | is_inclusive = cint(frappe.db.get_single_value("Accounts Settings", |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 463 | "show_inclusive_tax_in_print")) |
rohitwaghchaure | bf4c114 | 2018-01-08 15:20:15 +0530 | [diff] [blame] | 464 | |
| 465 | if is_inclusive: |
| 466 | is_inclusive = 0 |
| 467 | if self.get("taxes", filters={"included_in_print_rate": 1}): |
| 468 | is_inclusive = 1 |
| 469 | |
| 470 | return is_inclusive |
| 471 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 472 | def validate_advance_entries(self): |
Nabin Hait | 05aefbb | 2016-06-28 19:42:19 +0530 | [diff] [blame] | 473 | order_field = "sales_order" if self.doctype == "Sales Invoice" else "purchase_order" |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 474 | order_list = list(set([d.get(order_field) |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 475 | for d in self.get("items") if d.get(order_field)])) |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 476 | |
Nabin Hait | 05aefbb | 2016-06-28 19:42:19 +0530 | [diff] [blame] | 477 | if not order_list: return |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 478 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 479 | advance_entries = self.get_advance_entries(include_unallocated=False) |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 480 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 481 | if advance_entries: |
| 482 | advance_entries_against_si = [d.reference_name for d in self.get("advances")] |
| 483 | for d in advance_entries: |
| 484 | if not advance_entries_against_si or d.reference_name not in advance_entries_against_si: |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 485 | frappe.msgprint(_( |
| 486 | "Payment Entry {0} is linked against Order {1}, check if it should be pulled as advance in this invoice.") |
| 487 | .format(d.reference_name, d.against_order)) |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 488 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 489 | def update_against_document_in_jv(self): |
| 490 | """ |
| 491 | Links invoice and advance voucher: |
| 492 | 1. cancel advance voucher |
| 493 | 2. split into multiple rows if partially adjusted, assign against voucher |
| 494 | 3. submit advance voucher |
| 495 | """ |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 496 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 497 | if self.doctype == "Sales Invoice": |
Nabin Hait | ff2f3ef | 2016-07-04 11:41:14 +0530 | [diff] [blame] | 498 | party_type = "Customer" |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 499 | party = self.customer |
| 500 | party_account = self.debit_to |
| 501 | dr_or_cr = "credit_in_account_currency" |
| 502 | else: |
Nabin Hait | ff2f3ef | 2016-07-04 11:41:14 +0530 | [diff] [blame] | 503 | party_type = "Supplier" |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 504 | party = self.supplier |
| 505 | party_account = self.credit_to |
| 506 | dr_or_cr = "debit_in_account_currency" |
Nabin Hait | 48f5fa6 | 2014-09-18 15:03:54 +0530 | [diff] [blame] | 507 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 508 | lst = [] |
| 509 | for d in self.get('advances'): |
| 510 | if flt(d.allocated_amount) > 0: |
| 511 | args = frappe._dict({ |
| 512 | 'voucher_type': d.reference_type, |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 513 | 'voucher_no': d.reference_name, |
| 514 | 'voucher_detail_no': d.reference_row, |
| 515 | 'against_voucher_type': self.doctype, |
| 516 | 'against_voucher': self.name, |
| 517 | 'account': party_account, |
Nabin Hait | ff2f3ef | 2016-07-04 11:41:14 +0530 | [diff] [blame] | 518 | 'party_type': party_type, |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 519 | 'party': party, |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 520 | 'is_advance': 'Yes', |
| 521 | 'dr_or_cr': dr_or_cr, |
| 522 | 'unadjusted_amount': flt(d.advance_amount), |
| 523 | 'allocated_amount': flt(d.allocated_amount), |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 524 | 'exchange_rate': (self.conversion_rate |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 525 | if self.party_account_currency != self.company_currency else 1), |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 526 | 'grand_total': (self.base_grand_total |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 527 | if self.party_account_currency == self.company_currency else self.grand_total), |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 528 | 'outstanding_amount': self.outstanding_amount |
| 529 | }) |
| 530 | lst.append(args) |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 531 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 532 | if lst: |
| 533 | from erpnext.accounts.utils import reconcile_against_document |
| 534 | reconcile_against_document(lst) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 535 | |
Nabin Hait | 19d945a | 2013-07-29 18:35:39 +0530 | [diff] [blame] | 536 | 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] | 537 | from erpnext.controllers.status_updater import get_tolerance_for |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 538 | item_tolerance = {} |
| 539 | global_tolerance = None |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 540 | |
Nabin Hait | 4b8185d | 2014-12-25 18:19:39 +0530 | [diff] [blame] | 541 | for item in self.get("items"): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 542 | if item.get(item_ref_dn): |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 543 | ref_amt = flt(frappe.db.get_value(ref_dt + " Item", |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 544 | item.get(item_ref_dn), based_on), self.precision(based_on, item)) |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 545 | if not ref_amt: |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 546 | frappe.msgprint( |
| 547 | _("Warning: System will not check overbilling since amount for Item {0} in {1} is zero").format( |
| 548 | item.item_code, ref_dt)) |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 549 | else: |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 550 | already_billed = frappe.db.sql("""select sum(%s) from `tab%s` |
| 551 | where %s=%s and docstatus=1 and parent != %s""" % |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 552 | (based_on, self.doctype + " Item", item_ref_dn, '%s', '%s'), |
| 553 | (item.get(item_ref_dn), self.name))[0][0] |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 554 | |
| 555 | total_billed_amt = flt(flt(already_billed) + flt(item.get(based_on)), |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 556 | self.precision(based_on, item)) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 557 | |
| 558 | tolerance, item_tolerance, global_tolerance = get_tolerance_for(item.item_code, |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 559 | item_tolerance, global_tolerance) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 560 | |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 561 | max_allowed_amt = flt(ref_amt * (100 + tolerance) / 100) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 562 | |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 563 | if total_billed_amt - max_allowed_amt > 0.01: |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 564 | frappe.throw(_( |
| 565 | "Cannot overbill for Item {0} in row {1} more than {2}. To allow over-billing, please set in Stock Settings").format( |
| 566 | item.item_code, item.idx, max_allowed_amt)) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 567 | |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 568 | def get_company_default(self, fieldname): |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 569 | from erpnext.accounts.utils import get_company_default |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 570 | return get_company_default(self.company, fieldname) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 571 | |
Nabin Hait | a36adbd | 2013-08-02 14:50:12 +0530 | [diff] [blame] | 572 | def get_stock_items(self): |
| 573 | stock_items = [] |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 574 | item_codes = list(set(item.item_code for item in self.get("items"))) |
Nabin Hait | a36adbd | 2013-08-02 14:50:12 +0530 | [diff] [blame] | 575 | if item_codes: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 576 | stock_items = [r[0] for r in frappe.db.sql("""select name |
Rushabh Mehta | 1e8025b | 2015-07-24 15:16:25 +0530 | [diff] [blame] | 577 | from `tabItem` where name in (%s) and is_stock_item=1""" % \ |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 578 | (", ".join((["%s"] * len(item_codes))),), item_codes)] |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 579 | |
Nabin Hait | a36adbd | 2013-08-02 14:50:12 +0530 | [diff] [blame] | 580 | return stock_items |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 581 | |
Ankit Javalkar | 8e7ca41 | 2014-09-12 15:18:53 +0530 | [diff] [blame] | 582 | def set_total_advance_paid(self): |
| 583 | if self.doctype == "Sales Order": |
Nabin Hait | 6e439a5 | 2015-08-28 19:24:22 +0530 | [diff] [blame] | 584 | dr_or_cr = "credit_in_account_currency" |
Nabin Hait | b2206d1 | 2016-01-27 15:43:12 +0530 | [diff] [blame] | 585 | party = self.customer |
Ankit Javalkar | 8e7ca41 | 2014-09-12 15:18:53 +0530 | [diff] [blame] | 586 | else: |
Nabin Hait | 6e439a5 | 2015-08-28 19:24:22 +0530 | [diff] [blame] | 587 | dr_or_cr = "debit_in_account_currency" |
Nabin Hait | b2206d1 | 2016-01-27 15:43:12 +0530 | [diff] [blame] | 588 | party = self.supplier |
Ankit Javalkar | 8e7ca41 | 2014-09-12 15:18:53 +0530 | [diff] [blame] | 589 | |
Nabin Hait | b2206d1 | 2016-01-27 15:43:12 +0530 | [diff] [blame] | 590 | advance = frappe.db.sql(""" |
Ankit Javalkar | 8e7ca41 | 2014-09-12 15:18:53 +0530 | [diff] [blame] | 591 | select |
Nabin Hait | b2206d1 | 2016-01-27 15:43:12 +0530 | [diff] [blame] | 592 | account_currency, sum({dr_or_cr}) as amount |
Ankit Javalkar | 8e7ca41 | 2014-09-12 15:18:53 +0530 | [diff] [blame] | 593 | from |
Nabin Hait | 12e2a51 | 2016-06-26 01:37:21 +0530 | [diff] [blame] | 594 | `tabGL Entry` |
Ankit Javalkar | 8e7ca41 | 2014-09-12 15:18:53 +0530 | [diff] [blame] | 595 | where |
Nabin Hait | 12e2a51 | 2016-06-26 01:37:21 +0530 | [diff] [blame] | 596 | against_voucher_type = %s and against_voucher = %s and party=%s |
| 597 | and docstatus = 1 |
Nabin Hait | b2206d1 | 2016-01-27 15:43:12 +0530 | [diff] [blame] | 598 | """.format(dr_or_cr=dr_or_cr), (self.doctype, self.name, party), as_dict=1) |
Ankit Javalkar | 8e7ca41 | 2014-09-12 15:18:53 +0530 | [diff] [blame] | 599 | |
Nabin Hait | b2206d1 | 2016-01-27 15:43:12 +0530 | [diff] [blame] | 600 | if advance: |
Anand Doshi | 7c0a58a | 2016-01-29 12:16:24 +0530 | [diff] [blame] | 601 | advance = advance[0] |
| 602 | advance_paid = flt(advance.amount, self.precision("advance_paid")) |
| 603 | formatted_advance_paid = fmt_money(advance_paid, precision=self.precision("advance_paid"), |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 604 | currency=advance.account_currency) |
Anand Doshi | 7c0a58a | 2016-01-29 12:16:24 +0530 | [diff] [blame] | 605 | |
| 606 | frappe.db.set_value(self.doctype, self.name, "party_account_currency", |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 607 | advance.account_currency) |
Anand Doshi | 7c0a58a | 2016-01-29 12:16:24 +0530 | [diff] [blame] | 608 | |
| 609 | if advance.account_currency == self.currency: |
finbyz | 5efc797 | 2019-01-05 11:12:11 +0530 | [diff] [blame] | 610 | order_total = self.get("rounded_total") or self.grand_total |
| 611 | precision = "rounded_total" if self.get("rounded_total") else "grand_total" |
Nabin Hait | b2206d1 | 2016-01-27 15:43:12 +0530 | [diff] [blame] | 612 | else: |
finbyz | 5efc797 | 2019-01-05 11:12:11 +0530 | [diff] [blame] | 613 | order_total = self.get("base_rounded_total") or self.base_grand_total |
| 614 | precision = "base_rounded_total" if self.get("base_rounded_total") else "base_grand_total" |
Sagar Vora | f733a39 | 2019-01-07 14:24:01 +0530 | [diff] [blame] | 615 | |
finbyz | 5efc797 | 2019-01-05 11:12:11 +0530 | [diff] [blame] | 616 | formatted_order_total = fmt_money(order_total, precision=self.precision(precision), |
| 617 | currency=advance.account_currency) |
Anand Doshi | 7c0a58a | 2016-01-29 12:16:24 +0530 | [diff] [blame] | 618 | |
Nabin Hait | 9db1b22 | 2016-06-30 12:37:53 +0530 | [diff] [blame] | 619 | if self.currency == self.company_currency and advance_paid > order_total: |
Nabin Hait | b2206d1 | 2016-01-27 15:43:12 +0530 | [diff] [blame] | 620 | frappe.throw(_("Total advance ({0}) against Order {1} cannot be greater than the Grand Total ({2})") |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 621 | .format(formatted_advance_paid, self.name, formatted_order_total)) |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 622 | |
Nabin Hait | 13093b4 | 2016-06-29 18:04:37 +0530 | [diff] [blame] | 623 | frappe.db.set_value(self.doctype, self.name, "advance_paid", advance_paid) |
Ankit Javalkar | 8e7ca41 | 2014-09-12 15:18:53 +0530 | [diff] [blame] | 624 | |
ankitjavalkarwork | e60822b | 2014-08-26 14:29:06 +0530 | [diff] [blame] | 625 | @property |
| 626 | def company_abbr(self): |
Nabin Hait | 6a5695f | 2018-08-09 11:30:21 +0530 | [diff] [blame] | 627 | if not hasattr(self, "_abbr"): |
| 628 | self._abbr = frappe.db.get_value('Company', self.company, "abbr") |
ankitjavalkarwork | e60822b | 2014-08-26 14:29:06 +0530 | [diff] [blame] | 629 | |
| 630 | return self._abbr |
| 631 | |
Neil Trini Lasrado | 3fbbb71 | 2015-07-17 12:10:12 +0530 | [diff] [blame] | 632 | def validate_party(self): |
Nabin Hait | 4ffd7f3 | 2015-08-27 12:28:36 +0530 | [diff] [blame] | 633 | party_type, party = self.get_party() |
shreyas | eba9ca4 | 2016-01-26 14:56:52 +0530 | [diff] [blame] | 634 | validate_party_frozen_disabled(party_type, party) |
Anand Doshi | 979326b | 2015-09-11 16:22:37 +0530 | [diff] [blame] | 635 | |
Nabin Hait | 4ffd7f3 | 2015-08-27 12:28:36 +0530 | [diff] [blame] | 636 | def get_party(self): |
Neil Trini Lasrado | 79bf233 | 2015-07-20 13:03:48 +0530 | [diff] [blame] | 637 | party_type = None |
shreyas | 29b565f | 2016-01-25 17:30:49 +0530 | [diff] [blame] | 638 | if self.doctype in ("Opportunity", "Quotation", "Sales Order", "Delivery Note", "Sales Invoice"): |
shreyas | eba9ca4 | 2016-01-26 14:56:52 +0530 | [diff] [blame] | 639 | party_type = 'Customer' |
shreyas | 29b565f | 2016-01-25 17:30:49 +0530 | [diff] [blame] | 640 | |
| 641 | elif self.doctype in ("Supplier Quotation", "Purchase Order", "Purchase Receipt", "Purchase Invoice"): |
shreyas | eba9ca4 | 2016-01-26 14:56:52 +0530 | [diff] [blame] | 642 | party_type = 'Supplier' |
shreyas | 29b565f | 2016-01-25 17:30:49 +0530 | [diff] [blame] | 643 | |
| 644 | elif self.meta.get_field("customer"): |
| 645 | party_type = "Customer" |
Neil Trini Lasrado | 3fbbb71 | 2015-07-17 12:10:12 +0530 | [diff] [blame] | 646 | |
Neil Trini Lasrado | 79bf233 | 2015-07-20 13:03:48 +0530 | [diff] [blame] | 647 | elif self.meta.get_field("supplier"): |
shreyas | 29b565f | 2016-01-25 17:30:49 +0530 | [diff] [blame] | 648 | party_type = "Supplier" |
Anand Doshi | 979326b | 2015-09-11 16:22:37 +0530 | [diff] [blame] | 649 | |
Nabin Hait | 4ffd7f3 | 2015-08-27 12:28:36 +0530 | [diff] [blame] | 650 | party = self.get(party_type.lower()) if party_type else None |
Anand Doshi | 979326b | 2015-09-11 16:22:37 +0530 | [diff] [blame] | 651 | |
Nabin Hait | 4ffd7f3 | 2015-08-27 12:28:36 +0530 | [diff] [blame] | 652 | return party_type, party |
Anand Doshi | 979326b | 2015-09-11 16:22:37 +0530 | [diff] [blame] | 653 | |
Nabin Hait | 4ffd7f3 | 2015-08-27 12:28:36 +0530 | [diff] [blame] | 654 | def validate_currency(self): |
Nabin Hait | 6e439a5 | 2015-08-28 19:24:22 +0530 | [diff] [blame] | 655 | if self.get("currency"): |
Nabin Hait | 4ffd7f3 | 2015-08-27 12:28:36 +0530 | [diff] [blame] | 656 | party_type, party = self.get_party() |
Nabin Hait | 6e439a5 | 2015-08-28 19:24:22 +0530 | [diff] [blame] | 657 | if party_type and party: |
Anand Doshi | b20baf8 | 2015-09-25 16:17:50 +0530 | [diff] [blame] | 658 | party_account_currency = get_party_account_currency(party_type, party, self.company) |
Anand Doshi | 979326b | 2015-09-11 16:22:37 +0530 | [diff] [blame] | 659 | |
Anand Doshi | 7afaeb0 | 2015-10-01 18:55:25 +0530 | [diff] [blame] | 660 | if (party_account_currency |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 661 | and party_account_currency != self.company_currency |
| 662 | and self.currency != party_account_currency): |
Nabin Hait | 9809677 | 2015-09-03 10:28:08 +0530 | [diff] [blame] | 663 | frappe.throw(_("Accounting Entry for {0}: {1} can only be made in currency: {2}") |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 664 | .format(party_type, party, party_account_currency), InvalidCurrency) |
Anand Doshi | 979326b | 2015-09-11 16:22:37 +0530 | [diff] [blame] | 665 | |
shreyas | 29b565f | 2016-01-25 17:30:49 +0530 | [diff] [blame] | 666 | # Note: not validating with gle account because we don't have the account |
| 667 | # at quotation / sales order level and we shouldn't stop someone |
| 668 | # from creating a sales invoice if sales order is already created |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 669 | |
Nabin Hait | 3d9c5c0 | 2016-04-07 18:32:37 +0530 | [diff] [blame] | 670 | def validate_fixed_asset(self): |
| 671 | for d in self.get("items"): |
| 672 | if d.is_fixed_asset: |
Rohit Waghchaure | 2868840 | 2018-05-16 23:57:28 +0530 | [diff] [blame] | 673 | # if d.qty > 1: |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 674 | # frappe.throw(_("Row #{0}: Qty must be 1, as item is a fixed asset. Please use separate row for multiple qty.").format(d.idx)) |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 675 | |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 676 | if d.meta.get_field("asset") and d.asset: |
| 677 | asset = frappe.get_doc("Asset", d.asset) |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 678 | |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 679 | if asset.company != self.company: |
| 680 | frappe.throw(_("Row #{0}: Asset {1} does not belong to company {2}") |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 681 | .format(d.idx, d.asset, self.company)) |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 682 | |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 683 | elif asset.item_code != d.item_code: |
| 684 | frappe.throw(_("Row #{0}: Asset {1} does not linked to Item {2}") |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 685 | .format(d.idx, d.asset, d.item_code)) |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 686 | |
Rohit Waghchaure | 992fb34 | 2018-05-17 11:18:34 +0530 | [diff] [blame] | 687 | # elif asset.docstatus != 1: |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 688 | # frappe.throw(_("Row #{0}: Asset {1} must be submitted").format(d.idx, d.asset)) |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 689 | |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 690 | elif self.doctype == "Purchase Invoice": |
Rohit Waghchaure | 4216972 | 2018-05-16 18:16:08 +0530 | [diff] [blame] | 691 | # if asset.status != "Submitted": |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 692 | # frappe.throw(_("Row #{0}: Asset {1} is already {2}") |
| 693 | # .format(d.idx, d.asset, asset.status)) |
Rohit Waghchaure | 4216972 | 2018-05-16 18:16:08 +0530 | [diff] [blame] | 694 | if getdate(asset.purchase_date) != getdate(self.posting_date): |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 695 | frappe.throw( |
| 696 | _("Row #{0}: Posting Date must be same as purchase date {1} of asset {2}").format(d.idx, |
| 697 | asset.purchase_date, |
| 698 | d.asset)) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 699 | elif asset.is_existing_asset: |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 700 | frappe.throw( |
| 701 | _("Row #{0}: Purchase Invoice cannot be made against an existing asset {1}").format( |
| 702 | d.idx, d.asset)) |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 703 | |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 704 | elif self.docstatus == "Sales Invoice" and self.docstatus == 1: |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 705 | if self.update_stock: |
| 706 | frappe.throw(_("'Update Stock' cannot be checked for fixed asset sale")) |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 707 | |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 708 | elif asset.status in ("Scrapped", "Cancelled", "Sold"): |
| 709 | frappe.throw(_("Row #{0}: Asset {1} cannot be submitted, it is already {2}") |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 710 | .format(d.idx, d.asset, asset.status)) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 711 | |
Nabin Hait | 297d74a | 2016-11-23 15:58:51 +0530 | [diff] [blame] | 712 | def delink_advance_entries(self, linked_doc_name): |
| 713 | total_allocated_amount = 0 |
| 714 | for adv in self.advances: |
| 715 | consider_for_total_advance = True |
| 716 | if adv.reference_name == linked_doc_name: |
| 717 | frappe.db.sql("""delete from `tab{0} Advance` |
| 718 | where name = %s""".format(self.doctype), adv.name) |
| 719 | consider_for_total_advance = False |
| 720 | |
| 721 | if consider_for_total_advance: |
| 722 | total_allocated_amount += flt(adv.allocated_amount, adv.precision("allocated_amount")) |
| 723 | |
Rushabh Mehta | 6695830 | 2017-01-16 16:57:53 +0530 | [diff] [blame] | 724 | frappe.db.set_value(self.doctype, self.name, "total_advance", |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 725 | total_allocated_amount, update_modified=False) |
Nabin Hait | 4fdb052 | 2016-03-09 12:40:56 +0530 | [diff] [blame] | 726 | |
KanchanChauhan | 49a50fe | 2016-11-18 13:57:53 +0530 | [diff] [blame] | 727 | def group_similar_items(self): |
| 728 | group_item_qty = {} |
| 729 | group_item_amount = {} |
Shreya Shah | 785f1aa | 2018-10-11 10:14:25 +0530 | [diff] [blame] | 730 | # to update serial number in print |
| 731 | count = 0 |
KanchanChauhan | 49a50fe | 2016-11-18 13:57:53 +0530 | [diff] [blame] | 732 | |
| 733 | for item in self.items: |
| 734 | group_item_qty[item.item_code] = group_item_qty.get(item.item_code, 0) + item.qty |
| 735 | group_item_amount[item.item_code] = group_item_amount.get(item.item_code, 0) + item.amount |
| 736 | |
| 737 | duplicate_list = [] |
KanchanChauhan | 49a50fe | 2016-11-18 13:57:53 +0530 | [diff] [blame] | 738 | for item in self.items: |
| 739 | if item.item_code in group_item_qty: |
Shreya Shah | 785f1aa | 2018-10-11 10:14:25 +0530 | [diff] [blame] | 740 | count += 1 |
KanchanChauhan | 49a50fe | 2016-11-18 13:57:53 +0530 | [diff] [blame] | 741 | item.qty = group_item_qty[item.item_code] |
| 742 | item.amount = group_item_amount[item.item_code] |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 743 | item.rate = flt(flt(item.amount) / flt(item.qty), item.precision("rate")) |
Shreya Shah | 785f1aa | 2018-10-11 10:14:25 +0530 | [diff] [blame] | 744 | item.idx = count |
KanchanChauhan | 49a50fe | 2016-11-18 13:57:53 +0530 | [diff] [blame] | 745 | del group_item_qty[item.item_code] |
| 746 | else: |
| 747 | duplicate_list.append(item) |
KanchanChauhan | 49a50fe | 2016-11-18 13:57:53 +0530 | [diff] [blame] | 748 | for item in duplicate_list: |
| 749 | self.remove(item) |
| 750 | |
tunde | 32aa7c1 | 2017-09-07 06:52:15 +0100 | [diff] [blame] | 751 | def set_payment_schedule(self): |
Manas Solanki | a7f5589 | 2018-04-02 10:32:00 +0530 | [diff] [blame] | 752 | if self.doctype == 'Sales Invoice' and self.is_pos: |
| 753 | self.payment_terms_template = '' |
| 754 | return |
rohitwaghchaure | db9fa78 | 2018-03-01 10:32:29 +0530 | [diff] [blame] | 755 | |
rohitwaghchaure | da941af | 2018-01-17 16:23:04 +0530 | [diff] [blame] | 756 | posting_date = self.get("bill_date") or self.get("posting_date") or self.get("transaction_date") |
tunde | df3a175 | 2017-09-11 11:02:57 +0100 | [diff] [blame] | 757 | date = self.get("due_date") |
| 758 | due_date = date or posting_date |
Nabin Hait | 0551f7b | 2017-11-21 19:58:16 +0530 | [diff] [blame] | 759 | grand_total = self.get("rounded_total") or self.grand_total |
Nabin Hait | 2f9f4f9 | 2017-12-20 12:24:59 +0530 | [diff] [blame] | 760 | if self.doctype in ("Sales Invoice", "Purchase Invoice"): |
| 761 | grand_total = grand_total - flt(self.write_off_amount) |
tunde | 96b8f22 | 2017-09-08 15:35:59 +0100 | [diff] [blame] | 762 | |
Nabin Hait | 0551f7b | 2017-11-21 19:58:16 +0530 | [diff] [blame] | 763 | if not self.get("payment_schedule"): |
| 764 | if self.get("payment_terms_template"): |
| 765 | data = get_payment_terms(self.payment_terms_template, posting_date, grand_total) |
| 766 | for item in data: |
| 767 | self.append("payment_schedule", item) |
| 768 | else: |
| 769 | data = dict(due_date=due_date, invoice_portion=100, payment_amount=grand_total) |
| 770 | self.append("payment_schedule", data) |
| 771 | else: |
| 772 | for d in self.get("payment_schedule"): |
Nabin Hait | 2f9f4f9 | 2017-12-20 12:24:59 +0530 | [diff] [blame] | 773 | if d.invoice_portion: |
| 774 | d.payment_amount = grand_total * flt(d.invoice_portion) / 100 |
tunde | 43870aa | 2017-08-18 11:59:30 +0100 | [diff] [blame] | 775 | |
tunde | be1b871 | 2017-08-19 08:21:44 +0100 | [diff] [blame] | 776 | def set_due_date(self): |
Nabin Hait | 0551f7b | 2017-11-21 19:58:16 +0530 | [diff] [blame] | 777 | due_dates = [d.due_date for d in self.get("payment_schedule") if d.due_date] |
| 778 | if due_dates: |
| 779 | self.due_date = max(due_dates) |
tunde | 62af5c5 | 2017-09-22 15:16:38 +0100 | [diff] [blame] | 780 | |
| 781 | def validate_payment_schedule_dates(self): |
tunde | 77ecacc | 2017-09-22 23:12:55 +0100 | [diff] [blame] | 782 | dates = [] |
tunde | 9bed2de | 2017-09-25 10:19:35 +0100 | [diff] [blame] | 783 | li = [] |
tunde | 43870aa | 2017-08-18 11:59:30 +0100 | [diff] [blame] | 784 | |
rohitwaghchaure | db9fa78 | 2018-03-01 10:32:29 +0530 | [diff] [blame] | 785 | if self.doctype == 'Sales Invoice' and self.is_pos: return |
| 786 | |
tunde | 43870aa | 2017-08-18 11:59:30 +0100 | [diff] [blame] | 787 | for d in self.get("payment_schedule"): |
Nabin Hait | 2f9f4f9 | 2017-12-20 12:24:59 +0530 | [diff] [blame] | 788 | if self.doctype == "Sales Order" and getdate(d.due_date) < getdate(self.transaction_date): |
tunde | 43870aa | 2017-08-18 11:59:30 +0100 | [diff] [blame] | 789 | frappe.throw(_("Row {0}: Due Date cannot be before posting date").format(d.idx)) |
tunde | 77ecacc | 2017-09-22 23:12:55 +0100 | [diff] [blame] | 790 | elif d.due_date in dates: |
mnatalia | 5f0e8d6 | 2018-05-22 06:38:50 +0300 | [diff] [blame] | 791 | li.append(_("{0} in row {1}").format(d.due_date, d.idx)) |
tunde | 9bed2de | 2017-09-25 10:19:35 +0100 | [diff] [blame] | 792 | dates.append(d.due_date) |
| 793 | |
| 794 | if li: |
| 795 | duplicates = '<br>' + '<br>'.join(li) |
Faris Ansari | 2ab6e0e | 2018-09-19 13:13:59 +0530 | [diff] [blame] | 796 | frappe.throw(_("Rows with duplicate due dates in other rows were found: {0}").format(duplicates)) |
tunde | 43870aa | 2017-08-18 11:59:30 +0100 | [diff] [blame] | 797 | |
tunde | 62af5c5 | 2017-09-22 15:16:38 +0100 | [diff] [blame] | 798 | def validate_payment_schedule_amount(self): |
rohitwaghchaure | db9fa78 | 2018-03-01 10:32:29 +0530 | [diff] [blame] | 799 | if self.doctype == 'Sales Invoice' and self.is_pos: return |
| 800 | |
Nabin Hait | 0551f7b | 2017-11-21 19:58:16 +0530 | [diff] [blame] | 801 | if self.get("payment_schedule"): |
| 802 | total = 0 |
| 803 | for d in self.get("payment_schedule"): |
Nabin Hait | f592f2c | 2017-12-19 11:31:34 +0530 | [diff] [blame] | 804 | total += flt(d.payment_amount) |
tundebabzy | 5825ded | 2017-12-19 07:09:20 +0100 | [diff] [blame] | 805 | total = flt(total, self.precision("grand_total")) |
tunde | 43870aa | 2017-08-18 11:59:30 +0100 | [diff] [blame] | 806 | |
tundebabzy | 5825ded | 2017-12-19 07:09:20 +0100 | [diff] [blame] | 807 | grand_total = flt(self.get("rounded_total") or self.grand_total, self.precision('grand_total')) |
Nabin Hait | e591c85 | 2017-12-21 11:46:30 +0530 | [diff] [blame] | 808 | if self.doctype in ("Sales Invoice", "Purchase Invoice"): |
| 809 | grand_total = grand_total - flt(self.write_off_amount) |
Nabin Hait | 0551f7b | 2017-11-21 19:58:16 +0530 | [diff] [blame] | 810 | if total != grand_total: |
| 811 | frappe.throw(_("Total Payment Amount in Payment Schedule must be equal to Grand / Rounded Total")) |
tunde | 43870aa | 2017-08-18 11:59:30 +0100 | [diff] [blame] | 812 | |
Nabin Hait | 877e1bb | 2017-11-17 12:27:43 +0530 | [diff] [blame] | 813 | def is_rounded_total_disabled(self): |
| 814 | if self.meta.get_field("disable_rounded_total"): |
| 815 | return self.disable_rounded_total |
| 816 | else: |
| 817 | return frappe.db.get_single_value("Global Defaults", "disable_rounded_total") |
| 818 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 819 | @frappe.whitelist() |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 820 | def get_tax_rate(account_head): |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 821 | return frappe.db.get_value("Account", account_head, ["tax_rate", "account_name"], as_dict=True) |
Rushabh Mehta | a33d468 | 2015-06-01 17:15:42 +0530 | [diff] [blame] | 822 | |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 823 | |
Nabin Hait | ffc7f3f | 2015-05-12 15:07:02 +0530 | [diff] [blame] | 824 | @frappe.whitelist() |
Nabin Hait | a2426fc | 2018-01-15 17:45:46 +0530 | [diff] [blame] | 825 | def get_default_taxes_and_charges(master_doctype, tax_template=None, company=None): |
rohitwaghchaure | 505661b | 2017-12-11 14:52:28 +0530 | [diff] [blame] | 826 | if not company: return {} |
| 827 | |
Nabin Hait | a2426fc | 2018-01-15 17:45:46 +0530 | [diff] [blame] | 828 | if tax_template and company: |
| 829 | tax_template_company = frappe.db.get_value(master_doctype, tax_template, "company") |
| 830 | if tax_template_company == company: |
| 831 | return |
| 832 | |
| 833 | default_tax = frappe.db.get_value(master_doctype, {"is_default": 1, "company": company}) |
rohitwaghchaure | 505661b | 2017-12-11 14:52:28 +0530 | [diff] [blame] | 834 | |
Rushabh Mehta | 98aa581 | 2017-11-14 09:32:32 +0530 | [diff] [blame] | 835 | return { |
| 836 | 'taxes_and_charges': default_tax, |
| 837 | 'taxes': get_taxes_and_charges(master_doctype, default_tax) |
| 838 | } |
Nabin Hait | 6b03914 | 2014-05-02 15:45:10 +0530 | [diff] [blame] | 839 | |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 840 | |
Nabin Hait | 6b03914 | 2014-05-02 15:45:10 +0530 | [diff] [blame] | 841 | @frappe.whitelist() |
Nabin Hait | ffc7f3f | 2015-05-12 15:07:02 +0530 | [diff] [blame] | 842 | def get_taxes_and_charges(master_doctype, master_name): |
Nabin Hait | 1fe5012 | 2015-05-16 12:14:39 +0530 | [diff] [blame] | 843 | if not master_name: |
| 844 | return |
Nabin Hait | 6b03914 | 2014-05-02 15:45:10 +0530 | [diff] [blame] | 845 | from frappe.model import default_fields |
| 846 | tax_master = frappe.get_doc(master_doctype, master_name) |
| 847 | |
| 848 | taxes_and_charges = [] |
Nabin Hait | ffc7f3f | 2015-05-12 15:07:02 +0530 | [diff] [blame] | 849 | for i, tax in enumerate(tax_master.get("taxes")): |
Nabin Hait | 6b03914 | 2014-05-02 15:45:10 +0530 | [diff] [blame] | 850 | tax = tax.as_dict() |
| 851 | |
| 852 | for fieldname in default_fields: |
| 853 | if fieldname in tax: |
| 854 | del tax[fieldname] |
| 855 | |
| 856 | taxes_and_charges.append(tax) |
| 857 | |
| 858 | return taxes_and_charges |
Rushabh Mehta | 66e08e3 | 2014-09-29 12:17:03 +0530 | [diff] [blame] | 859 | |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 860 | |
Rushabh Mehta | 66e08e3 | 2014-09-29 12:17:03 +0530 | [diff] [blame] | 861 | def validate_conversion_rate(currency, conversion_rate, conversion_rate_label, company): |
| 862 | """common validation for currency and price list currency""" |
| 863 | |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 864 | company_currency = frappe.get_cached_value('Company', company, "default_currency") |
Rushabh Mehta | 66e08e3 | 2014-09-29 12:17:03 +0530 | [diff] [blame] | 865 | |
| 866 | if not conversion_rate: |
| 867 | throw(_("{0} is mandatory. Maybe Currency Exchange record is not created for {1} to {2}.").format( |
| 868 | conversion_rate_label, currency, company_currency)) |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 869 | |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 870 | |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 871 | def validate_taxes_and_charges(tax): |
Nabin Hait | 37b047d | 2015-02-23 16:01:33 +0530 | [diff] [blame] | 872 | if tax.charge_type in ['Actual', 'On Net Total'] and tax.row_id: |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 873 | frappe.throw(_("Can refer row only if the charge type is 'On Previous Row Amount' or 'Previous Row Total'")) |
Nabin Hait | 37b047d | 2015-02-23 16:01:33 +0530 | [diff] [blame] | 874 | elif tax.charge_type in ['On Previous Row Amount', 'On Previous Row Total']: |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 875 | if cint(tax.idx) == 1: |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 876 | frappe.throw( |
| 877 | _("Cannot select charge type as 'On Previous Row Amount' or 'On Previous Row Total' for first row")) |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 878 | elif not tax.row_id: |
| 879 | frappe.throw(_("Please specify a valid Row ID for row {0} in table {1}".format(tax.idx, _(tax.doctype)))) |
| 880 | elif tax.row_id and cint(tax.row_id) >= cint(tax.idx): |
| 881 | frappe.throw(_("Cannot refer row number greater than or equal to current row number for this Charge type")) |
| 882 | |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 883 | if tax.charge_type == "Actual": |
Nabin Hait | bc6c360 | 2015-02-27 23:40:56 +0530 | [diff] [blame] | 884 | tax.rate = None |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 885 | |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 886 | |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 887 | def validate_inclusive_tax(tax, doc): |
| 888 | def _on_previous_row_error(row_range): |
| 889 | throw(_("To include tax in row {0} in Item rate, taxes in rows {1} must also be included").format(tax.idx, |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 890 | row_range)) |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 891 | |
| 892 | if cint(getattr(tax, "included_in_print_rate", None)): |
| 893 | if tax.charge_type == "Actual": |
| 894 | # inclusive tax cannot be of type Actual |
| 895 | throw(_("Charge of type 'Actual' in row {0} cannot be included in Item Rate").format(tax.idx)) |
| 896 | elif tax.charge_type == "On Previous Row Amount" and \ |
| 897 | not cint(doc.get("taxes")[cint(tax.row_id) - 1].included_in_print_rate): |
| 898 | # referred row should also be inclusive |
| 899 | _on_previous_row_error(tax.row_id) |
| 900 | elif tax.charge_type == "On Previous Row Total" and \ |
| 901 | not all([cint(t.included_in_print_rate) for t in doc.get("taxes")[:cint(tax.row_id) - 1]]): |
| 902 | # all rows about the reffered tax should be inclusive |
| 903 | _on_previous_row_error("1 - %d" % (tax.row_id,)) |
Nabin Hait | 93b3a37 | 2015-02-24 17:08:34 +0530 | [diff] [blame] | 904 | elif tax.get("category") == "Valuation": |
Nabin Hait | 72c359a | 2015-02-24 16:05:19 +0530 | [diff] [blame] | 905 | frappe.throw(_("Valuation type charges can not marked as Inclusive")) |
Revant Nandgaonkar | 5da941b | 2016-02-18 16:02:05 +0530 | [diff] [blame] | 906 | |
Revant Nandgaonkar | 5da941b | 2016-02-18 16:02:05 +0530 | [diff] [blame] | 907 | |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 908 | def set_balance_in_account_currency(gl_dict, account_currency=None, conversion_rate=None, company_currency=None): |
| 909 | if (not conversion_rate) and (account_currency != company_currency): |
| 910 | frappe.throw(_("Account: {0} with currency: {1} can not be selected") |
| 911 | .format(gl_dict.account, account_currency)) |
| 912 | |
| 913 | gl_dict["account_currency"] = company_currency if account_currency == company_currency \ |
Revant Nandgaonkar | 5da941b | 2016-02-18 16:02:05 +0530 | [diff] [blame] | 914 | else account_currency |
| 915 | |
| 916 | # set debit/credit in account currency if not provided |
| 917 | if flt(gl_dict.debit) and not flt(gl_dict.debit_in_account_currency): |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 918 | gl_dict.debit_in_account_currency = gl_dict.debit if account_currency == company_currency \ |
Revant Nandgaonkar | 5da941b | 2016-02-18 16:02:05 +0530 | [diff] [blame] | 919 | else flt(gl_dict.debit / conversion_rate, 2) |
| 920 | |
| 921 | if flt(gl_dict.credit) and not flt(gl_dict.credit_in_account_currency): |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 922 | gl_dict.credit_in_account_currency = gl_dict.credit if account_currency == company_currency \ |
Revant Nandgaonkar | 5da941b | 2016-02-18 16:02:05 +0530 | [diff] [blame] | 923 | else flt(gl_dict.credit / conversion_rate, 2) |
Nabin Hait | 1991c7b | 2016-06-27 20:09:05 +0530 | [diff] [blame] | 924 | |
| 925 | |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 926 | def get_advance_journal_entries(party_type, party, party_account, amount_field, |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 927 | order_doctype, order_list, include_unallocated=True): |
| 928 | dr_or_cr = "credit_in_account_currency" if party_type == "Customer" else "debit_in_account_currency" |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 929 | |
Nabin Hait | 1991c7b | 2016-06-27 20:09:05 +0530 | [diff] [blame] | 930 | conditions = [] |
| 931 | if include_unallocated: |
| 932 | conditions.append("ifnull(t2.reference_name, '')=''") |
| 933 | |
| 934 | if order_list: |
| 935 | order_condition = ', '.join(['%s'] * len(order_list)) |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 936 | conditions.append(" (t2.reference_type = '{0}' and ifnull(t2.reference_name, '') in ({1}))" \ |
| 937 | .format(order_doctype, order_condition)) |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 938 | |
Nabin Hait | 1991c7b | 2016-06-27 20:09:05 +0530 | [diff] [blame] | 939 | reference_condition = " and (" + " or ".join(conditions) + ")" if conditions else "" |
Rushabh Mehta | 6695830 | 2017-01-16 16:57:53 +0530 | [diff] [blame] | 940 | |
Nabin Hait | 1991c7b | 2016-06-27 20:09:05 +0530 | [diff] [blame] | 941 | journal_entries = frappe.db.sql(""" |
| 942 | select |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 943 | "Journal Entry" as reference_type, t1.name as reference_name, |
Nabin Hait | 1991c7b | 2016-06-27 20:09:05 +0530 | [diff] [blame] | 944 | t1.remark as remarks, t2.{0} as amount, t2.name as reference_row, |
| 945 | t2.reference_name as against_order |
| 946 | from |
| 947 | `tabJournal Entry` t1, `tabJournal Entry Account` t2 |
| 948 | where |
| 949 | t1.name = t2.parent and t2.account = %s |
| 950 | and t2.party_type = %s and t2.party = %s |
| 951 | and t2.is_advance = 'Yes' and t1.docstatus = 1 |
Nabin Hait | ca627fb | 2016-09-05 16:16:53 +0530 | [diff] [blame] | 952 | and {1} > 0 {2} |
Nabin Hait | 1991c7b | 2016-06-27 20:09:05 +0530 | [diff] [blame] | 953 | order by t1.posting_date""".format(amount_field, dr_or_cr, reference_condition), |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 954 | [party_account, party_type, party] + order_list, as_dict=1) |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 955 | |
Nabin Hait | 1991c7b | 2016-06-27 20:09:05 +0530 | [diff] [blame] | 956 | return list(journal_entries) |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 957 | |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 958 | |
Rohit Waghchaure | f725816 | 2019-01-15 18:12:04 +0530 | [diff] [blame] | 959 | def get_advance_payment_entries(party_type, party, party_account, order_doctype, |
| 960 | order_list=None, include_unallocated=True, against_all_orders=False, limit=1000): |
Nabin Hait | 1991c7b | 2016-06-27 20:09:05 +0530 | [diff] [blame] | 961 | party_account_field = "paid_from" if party_type == "Customer" else "paid_to" |
| 962 | payment_type = "Receive" if party_type == "Customer" else "Pay" |
| 963 | payment_entries_against_order, unallocated_payment_entries = [], [] |
Rohit Waghchaure | f725816 | 2019-01-15 18:12:04 +0530 | [diff] [blame] | 964 | limit_cond = "limit %s" % (limit or 1000) |
Nabin Hait | ff2f3ef | 2016-07-04 11:41:14 +0530 | [diff] [blame] | 965 | |
Nabin Hait | 1991c7b | 2016-06-27 20:09:05 +0530 | [diff] [blame] | 966 | if order_list or against_all_orders: |
| 967 | if order_list: |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 968 | reference_condition = " and t2.reference_name in ({0})" \ |
Nabin Hait | 1991c7b | 2016-06-27 20:09:05 +0530 | [diff] [blame] | 969 | .format(', '.join(['%s'] * len(order_list))) |
| 970 | else: |
| 971 | reference_condition = "" |
| 972 | order_list = [] |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 973 | |
Nabin Hait | 1991c7b | 2016-06-27 20:09:05 +0530 | [diff] [blame] | 974 | payment_entries_against_order = frappe.db.sql(""" |
| 975 | select |
| 976 | "Payment Entry" as reference_type, t1.name as reference_name, |
| 977 | t1.remarks, t2.allocated_amount as amount, t2.name as reference_row, |
| 978 | t2.reference_name as against_order, t1.posting_date |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 979 | from `tabPayment Entry` t1, `tabPayment Entry Reference` t2 |
Nabin Hait | 1991c7b | 2016-06-27 20:09:05 +0530 | [diff] [blame] | 980 | where |
| 981 | t1.name = t2.parent and t1.{0} = %s and t1.payment_type = %s |
| 982 | and t1.party_type = %s and t1.party = %s and t1.docstatus = 1 |
| 983 | and t2.reference_doctype = %s {1} |
Rohit Waghchaure | f725816 | 2019-01-15 18:12:04 +0530 | [diff] [blame] | 984 | order by t1.posting_date {2} |
| 985 | """.format(party_account_field, reference_condition, limit_cond), |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 986 | [party_account, payment_type, party_type, party, |
| 987 | order_doctype] + order_list, as_dict=1) |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 988 | |
Nabin Hait | 1991c7b | 2016-06-27 20:09:05 +0530 | [diff] [blame] | 989 | if include_unallocated: |
| 990 | unallocated_payment_entries = frappe.db.sql(""" |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 991 | select "Payment Entry" as reference_type, name as reference_name, |
Nabin Hait | 1991c7b | 2016-06-27 20:09:05 +0530 | [diff] [blame] | 992 | remarks, unallocated_amount as amount |
| 993 | from `tabPayment Entry` |
| 994 | where |
| 995 | {0} = %s and party_type = %s and party = %s and payment_type = %s |
| 996 | and docstatus = 1 and unallocated_amount > 0 |
Rohit Waghchaure | f725816 | 2019-01-15 18:12:04 +0530 | [diff] [blame] | 997 | order by posting_date {1} |
| 998 | """.format(party_account_field, limit_cond), (party_account, party_type, party, payment_type), as_dict=1) |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 999 | |
Rohit Waghchaure | 2f1db57 | 2016-11-08 12:39:33 +0530 | [diff] [blame] | 1000 | return list(payment_entries_against_order) + list(unallocated_payment_entries) |
| 1001 | |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 1002 | |
Rohit Waghchaure | 2f1db57 | 2016-11-08 12:39:33 +0530 | [diff] [blame] | 1003 | def update_invoice_status(): |
| 1004 | # Daily update the status of the invoices |
| 1005 | |
Rushabh Mehta | 6695830 | 2017-01-16 16:57:53 +0530 | [diff] [blame] | 1006 | frappe.db.sql(""" update `tabSales Invoice` set status = 'Overdue' |
Rohit Waghchaure | 2f1db57 | 2016-11-08 12:39:33 +0530 | [diff] [blame] | 1007 | where due_date < CURDATE() and docstatus = 1 and outstanding_amount > 0""") |
| 1008 | |
Rushabh Mehta | 6695830 | 2017-01-16 16:57:53 +0530 | [diff] [blame] | 1009 | frappe.db.sql(""" update `tabPurchase Invoice` set status = 'Overdue' |
Nabin Hait | 9275969 | 2017-08-15 08:23:51 +0530 | [diff] [blame] | 1010 | where due_date < CURDATE() and docstatus = 1 and outstanding_amount > 0""") |
| 1011 | |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 1012 | |
Nabin Hait | 9275969 | 2017-08-15 08:23:51 +0530 | [diff] [blame] | 1013 | @frappe.whitelist() |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 1014 | def get_payment_terms(terms_template, posting_date=None, grand_total=None, bill_date=None): |
Nabin Hait | 9275969 | 2017-08-15 08:23:51 +0530 | [diff] [blame] | 1015 | if not terms_template: |
| 1016 | return |
| 1017 | |
| 1018 | terms_doc = frappe.get_doc("Payment Terms Template", terms_template) |
| 1019 | |
| 1020 | schedule = [] |
tunde | fb14430 | 2017-08-19 15:01:40 +0100 | [diff] [blame] | 1021 | for d in terms_doc.get("terms"): |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 1022 | term_details = get_payment_term_details(d, posting_date, grand_total, bill_date) |
Nabin Hait | 9275969 | 2017-08-15 08:23:51 +0530 | [diff] [blame] | 1023 | schedule.append(term_details) |
| 1024 | |
| 1025 | return schedule |
| 1026 | |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 1027 | |
Nabin Hait | 9275969 | 2017-08-15 08:23:51 +0530 | [diff] [blame] | 1028 | @frappe.whitelist() |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 1029 | def get_payment_term_details(term, posting_date=None, grand_total=None, bill_date=None): |
Nabin Hait | 9275969 | 2017-08-15 08:23:51 +0530 | [diff] [blame] | 1030 | term_details = frappe._dict() |
Achilles Rasquinha | 9050998 | 2018-03-08 12:55:41 +0530 | [diff] [blame] | 1031 | if isinstance(term, text_type): |
Nabin Hait | 9275969 | 2017-08-15 08:23:51 +0530 | [diff] [blame] | 1032 | term = frappe.get_doc("Payment Term", term) |
| 1033 | else: |
| 1034 | term_details.payment_term = term.payment_term |
| 1035 | term_details.description = term.description |
| 1036 | term_details.invoice_portion = term.invoice_portion |
Rohit Waghchaure | 52bf56d | 2017-11-27 11:43:52 +0530 | [diff] [blame] | 1037 | term_details.payment_amount = flt(term.invoice_portion) * flt(grand_total) / 100 |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 1038 | if bill_date: |
| 1039 | term_details.due_date = get_due_date(term, bill_date) |
| 1040 | elif posting_date: |
| 1041 | term_details.due_date = get_due_date(term, posting_date) |
| 1042 | |
| 1043 | if getdate(term_details.due_date) < getdate(posting_date): |
| 1044 | term_details.due_date = posting_date |
tundebabzy | e69747c | 2018-03-09 07:53:53 +0100 | [diff] [blame] | 1045 | term_details.mode_of_payment = term.mode_of_payment |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 1046 | |
Nabin Hait | 9275969 | 2017-08-15 08:23:51 +0530 | [diff] [blame] | 1047 | return term_details |
| 1048 | |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 1049 | |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 1050 | def get_due_date(term, posting_date=None, bill_date=None): |
Nabin Hait | 9275969 | 2017-08-15 08:23:51 +0530 | [diff] [blame] | 1051 | due_date = None |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 1052 | date = bill_date or posting_date |
Nabin Hait | 9275969 | 2017-08-15 08:23:51 +0530 | [diff] [blame] | 1053 | if term.due_date_based_on == "Day(s) after invoice date": |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 1054 | due_date = add_days(date, term.credit_days) |
Nabin Hait | 9275969 | 2017-08-15 08:23:51 +0530 | [diff] [blame] | 1055 | elif term.due_date_based_on == "Day(s) after the end of the invoice month": |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 1056 | due_date = add_days(get_last_day(date), term.credit_days) |
Nabin Hait | 9275969 | 2017-08-15 08:23:51 +0530 | [diff] [blame] | 1057 | elif term.due_date_based_on == "Month(s) after the end of the invoice month": |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 1058 | due_date = add_months(get_last_day(date), term.credit_months) |
tunde | 96b8f22 | 2017-09-08 15:35:59 +0100 | [diff] [blame] | 1059 | return due_date |
tundebabzy | ad08d4c | 2018-05-16 07:01:41 +0100 | [diff] [blame] | 1060 | |
| 1061 | |
| 1062 | def get_supplier_block_status(party_name): |
| 1063 | """ |
| 1064 | Returns a dict containing the values of `on_hold`, `release_date` and `hold_type` of |
| 1065 | a `Supplier` |
| 1066 | """ |
| 1067 | supplier = frappe.get_doc('Supplier', party_name) |
| 1068 | info = { |
| 1069 | 'on_hold': supplier.on_hold, |
| 1070 | 'release_date': supplier.release_date, |
| 1071 | 'hold_type': supplier.hold_type |
| 1072 | } |
| 1073 | return info |
| 1074 | |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 1075 | |
| 1076 | @frappe.whitelist() |
| 1077 | def update_child_qty_rate(parent_doctype, trans_items, parent_doctype_name): |
| 1078 | data = json.loads(trans_items) |
| 1079 | for d in data: |
| 1080 | child_item = frappe.get_doc(parent_doctype + ' Item', d.get("docname")) |
deepeshgarg007 | ffb411d | 2018-12-27 14:12:05 +0530 | [diff] [blame] | 1081 | |
deepeshgarg007 | 77cde83 | 2018-12-27 15:56:51 +0530 | [diff] [blame] | 1082 | if parent_doctype == "Sales Order" and flt(d.get("qty")) < child_item.delivered_qty: |
deepeshgarg007 | ffb411d | 2018-12-27 14:12:05 +0530 | [diff] [blame] | 1083 | frappe.throw(_("Cannot set quantity less than delivered quantity")) |
| 1084 | |
deepeshgarg007 | 77cde83 | 2018-12-27 15:56:51 +0530 | [diff] [blame] | 1085 | if parent_doctype == "Purchase Order" and flt(d.get("qty")) < child_item.received_qty: |
| 1086 | frappe.throw(_("Cannot set quantity less than received quantity")) |
| 1087 | |
Nabin Hait | b2da082 | 2018-07-13 17:01:40 +0530 | [diff] [blame] | 1088 | child_item.qty = flt(d.get("qty")) |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 1089 | |
Nabin Hait | b2da082 | 2018-07-13 17:01:40 +0530 | [diff] [blame] | 1090 | if child_item.billed_amt > (flt(d.get("rate")) * flt(d.get("qty"))): |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 1091 | frappe.throw(_("Row #{0}: Cannot set Rate if amount is greater than billed amount for Item {1}.") |
| 1092 | .format(child_item.idx, child_item.item_code)) |
| 1093 | else: |
Nabin Hait | b2da082 | 2018-07-13 17:01:40 +0530 | [diff] [blame] | 1094 | child_item.rate = flt(d.get("rate")) |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 1095 | child_item.flags.ignore_validate_update_after_submit = True |
| 1096 | child_item.save() |
| 1097 | |
| 1098 | p_doctype = frappe.get_doc(parent_doctype, parent_doctype_name) |
| 1099 | |
| 1100 | p_doctype.flags.ignore_validate_update_after_submit = True |
| 1101 | p_doctype.set_qty_as_per_stock_uom() |
Nabin Hait | b2da082 | 2018-07-13 17:01:40 +0530 | [diff] [blame] | 1102 | p_doctype.calculate_taxes_and_totals() |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 1103 | frappe.get_doc('Authorization Control').validate_approving_authority(p_doctype.doctype, |
| 1104 | p_doctype.company, p_doctype.base_grand_total) |
| 1105 | |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 1106 | p_doctype.set_payment_schedule() |
Nabin Hait | b2da082 | 2018-07-13 17:01:40 +0530 | [diff] [blame] | 1107 | if parent_doctype == 'Purchase Order': |
| 1108 | p_doctype.validate_minimum_order_qty() |
| 1109 | p_doctype.validate_budget() |
| 1110 | if p_doctype.is_against_so(): |
| 1111 | p_doctype.update_status_updater() |
| 1112 | else: |
| 1113 | p_doctype.check_credit_limit() |
| 1114 | |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 1115 | p_doctype.save() |
Nabin Hait | b2da082 | 2018-07-13 17:01:40 +0530 | [diff] [blame] | 1116 | |
| 1117 | if parent_doctype == 'Purchase Order': |
| 1118 | update_last_purchase_rate(p_doctype, is_submit = 1) |
| 1119 | p_doctype.update_prevdoc_status() |
| 1120 | p_doctype.update_requested_qty() |
| 1121 | p_doctype.update_ordered_qty() |
| 1122 | p_doctype.update_ordered_and_reserved_qty() |
| 1123 | p_doctype.update_receiving_percentage() |
| 1124 | if p_doctype.is_subcontracted == "Yes": |
| 1125 | p_doctype.update_reserved_qty_for_subcontract() |
| 1126 | else: |
| 1127 | p_doctype.update_reserved_qty() |
| 1128 | p_doctype.update_project() |
| 1129 | p_doctype.update_prevdoc_status('submit') |
| 1130 | p_doctype.update_delivery_status() |
| 1131 | |
| 1132 | p_doctype.update_blanket_order() |
| 1133 | p_doctype.update_billing_percentage() |
| 1134 | p_doctype.set_status() |