Rushabh Mehta | ad45e31 | 2013-11-20 12:59:58 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 3 | |
| 4 | from __future__ import unicode_literals |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
| 6 | from frappe.utils import cint, flt, cstr |
| 7 | from frappe import msgprint, _ |
| 8 | import frappe.defaults |
Nabin Hait | d474194 | 2013-08-06 15:57:25 +0530 | [diff] [blame] | 9 | |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 10 | from erpnext.controllers.accounts_controller import AccountsController |
| 11 | from erpnext.accounts.general_ledger import make_gl_entries, delete_gl_entries |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 12 | |
| 13 | class StockController(AccountsController): |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 14 | def make_gl_entries(self, repost_future_gle=True): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 15 | if self.docstatus == 2: |
| 16 | delete_gl_entries(voucher_type=self.doctype, voucher_no=self.name) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 17 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 18 | if cint(frappe.defaults.get_global_default("auto_accounting_for_stock")): |
Nabin Hait | b9e0481 | 2014-09-26 14:22:18 +0530 | [diff] [blame] | 19 | warehouse_account = get_warehouse_account() |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 20 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 21 | if self.docstatus==1: |
Nabin Hait | e83069c | 2013-11-14 18:40:08 +0530 | [diff] [blame] | 22 | gl_entries = self.get_gl_entries(warehouse_account) |
Nabin Hait | 145e5e2 | 2013-10-22 23:51:41 +0530 | [diff] [blame] | 23 | make_gl_entries(gl_entries) |
| 24 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 25 | if repost_future_gle: |
Nabin Hait | b9e0481 | 2014-09-26 14:22:18 +0530 | [diff] [blame] | 26 | items, warehouses = self.get_items_and_warehouses() |
| 27 | update_gl_entries_after(self.posting_date, self.posting_time, warehouses, items, warehouse_account) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 28 | |
Nabin Hait | e83069c | 2013-11-14 18:40:08 +0530 | [diff] [blame] | 29 | def get_gl_entries(self, warehouse_account=None, default_expense_account=None, |
Nabin Hait | 142007a | 2013-09-17 15:15:16 +0530 | [diff] [blame] | 30 | default_cost_center=None): |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 31 | from erpnext.accounts.general_ledger import process_gl_map |
Nabin Hait | 142007a | 2013-09-17 15:15:16 +0530 | [diff] [blame] | 32 | if not warehouse_account: |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 33 | warehouse_account = get_warehouse_account() |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 34 | |
Anand Doshi | de1a97d | 2014-04-17 11:37:46 +0530 | [diff] [blame] | 35 | sle_map = self.get_stock_ledger_details() |
| 36 | voucher_details = self.get_voucher_details(default_expense_account, default_cost_center, sle_map) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 37 | |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 38 | gl_list = [] |
Nabin Hait | 7a75e10 | 2013-09-17 10:21:20 +0530 | [diff] [blame] | 39 | warehouse_with_no_account = [] |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 40 | for detail in voucher_details: |
Anand Doshi | de1a97d | 2014-04-17 11:37:46 +0530 | [diff] [blame] | 41 | sle_list = sle_map.get(detail.name) |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 42 | if sle_list: |
| 43 | for sle in sle_list: |
| 44 | if warehouse_account.get(sle.warehouse): |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 45 | # from warehouse account |
Rushabh Mehta | 052fe82 | 2014-04-16 19:20:11 +0530 | [diff] [blame] | 46 | |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 47 | self.check_expense_account(detail) |
| 48 | |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 49 | gl_list.append(self.get_gl_dict({ |
| 50 | "account": warehouse_account[sle.warehouse], |
| 51 | "against": detail.expense_account, |
| 52 | "cost_center": detail.cost_center, |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 53 | "remarks": self.get("remarks") or "Accounting Entry for Stock", |
Nabin Hait | 48bd4a1 | 2013-11-18 12:42:55 +0530 | [diff] [blame] | 54 | "debit": flt(sle.stock_value_difference, 2) |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 55 | })) |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 56 | |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 57 | # to target warehouse / expense account |
| 58 | gl_list.append(self.get_gl_dict({ |
| 59 | "account": detail.expense_account, |
| 60 | "against": warehouse_account[sle.warehouse], |
| 61 | "cost_center": detail.cost_center, |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 62 | "remarks": self.get("remarks") or "Accounting Entry for Stock", |
Nabin Hait | 48bd4a1 | 2013-11-18 12:42:55 +0530 | [diff] [blame] | 63 | "credit": flt(sle.stock_value_difference, 2) |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 64 | })) |
Nabin Hait | 7a75e10 | 2013-09-17 10:21:20 +0530 | [diff] [blame] | 65 | elif sle.warehouse not in warehouse_with_no_account: |
| 66 | warehouse_with_no_account.append(sle.warehouse) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 67 | |
| 68 | if warehouse_with_no_account: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 69 | msgprint(_("No accounting entries for the following warehouses") + ": \n" + |
Nabin Hait | 7a75e10 | 2013-09-17 10:21:20 +0530 | [diff] [blame] | 70 | "\n".join(warehouse_with_no_account)) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 71 | |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 72 | return process_gl_map(gl_list) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 73 | |
Anand Doshi | de1a97d | 2014-04-17 11:37:46 +0530 | [diff] [blame] | 74 | def get_voucher_details(self, default_expense_account, default_cost_center, sle_map): |
| 75 | if self.doctype == "Stock Reconciliation": |
| 76 | return [frappe._dict({ "name": voucher_detail_no, "expense_account": default_expense_account, |
| 77 | "cost_center": default_cost_center }) for voucher_detail_no, sle in sle_map.items()] |
| 78 | else: |
| 79 | details = self.get(self.fname) |
Anand Doshi | 094610d | 2014-04-16 19:56:53 +0530 | [diff] [blame] | 80 | |
Anand Doshi | de1a97d | 2014-04-17 11:37:46 +0530 | [diff] [blame] | 81 | if default_expense_account or default_cost_center: |
| 82 | for d in details: |
| 83 | if default_expense_account and not d.get("expense_account"): |
| 84 | d.expense_account = default_expense_account |
| 85 | if default_cost_center and not d.get("cost_center"): |
| 86 | d.cost_center = default_cost_center |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 87 | |
Anand Doshi | de1a97d | 2014-04-17 11:37:46 +0530 | [diff] [blame] | 88 | return details |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 89 | |
Nabin Hait | b9e0481 | 2014-09-26 14:22:18 +0530 | [diff] [blame] | 90 | def get_items_and_warehouses(self): |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 91 | items, warehouses = [], [] |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 92 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 93 | if hasattr(self, "fname"): |
Rushabh Mehta | f191f85 | 2014-04-02 18:09:34 +0530 | [diff] [blame] | 94 | item_doclist = self.get(self.fname) |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 95 | elif self.doctype == "Stock Reconciliation": |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 96 | import json |
| 97 | item_doclist = [] |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 98 | data = json.loads(self.reconciliation_json) |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 99 | for row in data[data.index(self.head_row)+1:]: |
| 100 | d = frappe._dict(zip(["item_code", "warehouse", "qty", "valuation_rate"], row)) |
| 101 | item_doclist.append(d) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 102 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 103 | if item_doclist: |
| 104 | for d in item_doclist: |
| 105 | if d.item_code and d.item_code not in items: |
| 106 | items.append(d.item_code) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 107 | |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 108 | if d.get("warehouse") and d.warehouse not in warehouses: |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 109 | warehouses.append(d.warehouse) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 110 | |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 111 | if self.doctype == "Stock Entry": |
| 112 | if d.get("s_warehouse") and d.s_warehouse not in warehouses: |
| 113 | warehouses.append(d.s_warehouse) |
| 114 | if d.get("t_warehouse") and d.t_warehouse not in warehouses: |
| 115 | warehouses.append(d.t_warehouse) |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 116 | |
Nabin Hait | b9e0481 | 2014-09-26 14:22:18 +0530 | [diff] [blame] | 117 | return items, warehouses |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 118 | |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 119 | def get_stock_ledger_details(self): |
| 120 | stock_ledger = {} |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 121 | for sle in frappe.db.sql("""select warehouse, stock_value_difference, voucher_detail_no |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 122 | from `tabStock Ledger Entry` where voucher_type=%s and voucher_no=%s""", |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 123 | (self.doctype, self.name), as_dict=True): |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 124 | stock_ledger.setdefault(sle.voucher_detail_no, []).append(sle) |
| 125 | return stock_ledger |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 126 | |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 127 | def make_adjustment_entry(self, expected_gle, voucher_obj): |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 128 | from erpnext.accounts.utils import get_stock_and_account_difference |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 129 | account_list = [d.account for d in expected_gle] |
| 130 | acc_diff = get_stock_and_account_difference(account_list, expected_gle[0].posting_date) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 131 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 132 | cost_center = self.get_company_default("cost_center") |
| 133 | stock_adjustment_account = self.get_company_default("stock_adjustment_account") |
| 134 | |
Nabin Hait | d474194 | 2013-08-06 15:57:25 +0530 | [diff] [blame] | 135 | gl_entries = [] |
| 136 | for account, diff in acc_diff.items(): |
| 137 | if diff: |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 138 | gl_entries.append([ |
| 139 | # stock in hand account |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 140 | voucher_obj.get_gl_dict({ |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 141 | "account": account, |
| 142 | "against": stock_adjustment_account, |
| 143 | "debit": diff, |
| 144 | "remarks": "Adjustment Accounting Entry for Stock", |
| 145 | }), |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 146 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 147 | # account against stock in hand |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 148 | voucher_obj.get_gl_dict({ |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 149 | "account": stock_adjustment_account, |
| 150 | "against": account, |
| 151 | "credit": diff, |
| 152 | "cost_center": cost_center or None, |
| 153 | "remarks": "Adjustment Accounting Entry for Stock", |
| 154 | }), |
| 155 | ]) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 156 | |
Nabin Hait | d474194 | 2013-08-06 15:57:25 +0530 | [diff] [blame] | 157 | if gl_entries: |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 158 | from erpnext.accounts.general_ledger import make_gl_entries |
Nabin Hait | d474194 | 2013-08-06 15:57:25 +0530 | [diff] [blame] | 159 | make_gl_entries(gl_entries) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 160 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 161 | def check_expense_account(self, item): |
Rushabh Mehta | 052fe82 | 2014-04-16 19:20:11 +0530 | [diff] [blame] | 162 | if not item.get("expense_account"): |
Nabin Hait | d341254 | 2014-05-01 17:42:21 +0530 | [diff] [blame] | 163 | frappe.throw(_("Expense or Difference account is mandatory for Item {0} as it impacts overall stock value").format(item.item_code)) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 164 | |
Anand Doshi | 496123a | 2014-06-19 19:25:19 +0530 | [diff] [blame] | 165 | else: |
Nabin Hait | 00778c9 | 2014-06-25 19:12:24 +0530 | [diff] [blame] | 166 | is_expense_account = frappe.db.get_value("Account", |
| 167 | item.get("expense_account"), "report_type")=="Profit and Loss" |
nabinhait | e6cee7b | 2014-07-10 19:06:39 +0530 | [diff] [blame] | 168 | if self.doctype not in ("Purchase Receipt", "Stock Reconciliation") and not is_expense_account: |
Nabin Hait | 00778c9 | 2014-06-25 19:12:24 +0530 | [diff] [blame] | 169 | frappe.throw(_("Expense / Difference account ({0}) must be a 'Profit or Loss' account") |
| 170 | .format(item.get("expense_account"))) |
Anand Doshi | 496123a | 2014-06-19 19:25:19 +0530 | [diff] [blame] | 171 | if is_expense_account and not item.get("cost_center"): |
| 172 | frappe.throw(_("{0} {1}: Cost Center is mandatory for Item {2}").format( |
| 173 | _(self.doctype), self.name, item.get("item_code"))) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 174 | |
| 175 | def get_sl_entries(self, d, args): |
Nabin Hait | 1e2f20a | 2013-08-02 11:42:11 +0530 | [diff] [blame] | 176 | sl_dict = { |
Nabin Hait | 296d626 | 2014-05-02 17:36:13 +0530 | [diff] [blame] | 177 | "item_code": d.get("item_code", None), |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 178 | "warehouse": d.get("warehouse", None), |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 179 | "posting_date": self.posting_date, |
| 180 | "posting_time": self.posting_time, |
| 181 | "voucher_type": self.doctype, |
| 182 | "voucher_no": self.name, |
Nabin Hait | 1e2f20a | 2013-08-02 11:42:11 +0530 | [diff] [blame] | 183 | "voucher_detail_no": d.name, |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 184 | "actual_qty": (self.docstatus==1 and 1 or -1)*flt(d.get("stock_qty")), |
Nabin Hait | 296d626 | 2014-05-02 17:36:13 +0530 | [diff] [blame] | 185 | "stock_uom": d.get("stock_uom"), |
Nabin Hait | 1e2f20a | 2013-08-02 11:42:11 +0530 | [diff] [blame] | 186 | "incoming_rate": 0, |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 187 | "company": self.company, |
| 188 | "fiscal_year": self.fiscal_year, |
Nabin Hait | 296d626 | 2014-05-02 17:36:13 +0530 | [diff] [blame] | 189 | "batch_no": cstr(d.get("batch_no")).strip(), |
| 190 | "serial_no": d.get("serial_no"), |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 191 | "project": d.get("project_name"), |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 192 | "is_cancelled": self.docstatus==2 and "Yes" or "No" |
Nabin Hait | 1e2f20a | 2013-08-02 11:42:11 +0530 | [diff] [blame] | 193 | } |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 194 | |
Nabin Hait | 1e2f20a | 2013-08-02 11:42:11 +0530 | [diff] [blame] | 195 | sl_dict.update(args) |
| 196 | return sl_dict |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 197 | |
Nabin Hait | 1e2f20a | 2013-08-02 11:42:11 +0530 | [diff] [blame] | 198 | def make_sl_entries(self, sl_entries, is_amended=None): |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 199 | from erpnext.stock.stock_ledger import make_sl_entries |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 200 | make_sl_entries(sl_entries, is_amended) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 201 | |
nabinhait | 87f2401 | 2014-07-16 19:48:29 +0530 | [diff] [blame] | 202 | def make_gl_entries_on_cancel(self): |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 203 | if frappe.db.sql("""select name from `tabGL Entry` where voucher_type=%s |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 204 | and voucher_no=%s""", (self.doctype, self.name)): |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 205 | self.make_gl_entries() |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 206 | |
Anand Doshi | a740f75 | 2014-06-25 13:31:02 +0530 | [diff] [blame] | 207 | def get_serialized_items(self): |
| 208 | serialized_items = [] |
| 209 | item_codes = list(set([d.item_code for d in self.get(self.fname)])) |
| 210 | if item_codes: |
| 211 | serialized_items = frappe.db.sql_list("""select name from `tabItem` |
| 212 | where has_serial_no='Yes' and name in ({})""".format(", ".join(["%s"]*len(item_codes))), |
| 213 | tuple(item_codes)) |
| 214 | |
| 215 | return serialized_items |
| 216 | |
Nabin Hait | b9e0481 | 2014-09-26 14:22:18 +0530 | [diff] [blame] | 217 | def update_gl_entries_after(posting_date, posting_time, for_warehouses=None, for_items=None, warehouse_account=None): |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 218 | def _delete_gl_entries(voucher_type, voucher_no): |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 219 | frappe.db.sql("""delete from `tabGL Entry` |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 220 | where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no)) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 221 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 222 | if not warehouse_account: |
| 223 | warehouse_account = get_warehouse_account() |
Nabin Hait | b9e0481 | 2014-09-26 14:22:18 +0530 | [diff] [blame] | 224 | |
| 225 | future_stock_vouchers = get_future_stock_vouchers(posting_date, posting_time, for_warehouses, for_items) |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 226 | gle = get_voucherwise_gl_entries(future_stock_vouchers, posting_date) |
| 227 | |
| 228 | for voucher_type, voucher_no in future_stock_vouchers: |
| 229 | existing_gle = gle.get((voucher_type, voucher_no), []) |
Rushabh Mehta | 0a0f249 | 2014-03-31 17:27:06 +0530 | [diff] [blame] | 230 | voucher_obj = frappe.get_doc(voucher_type, voucher_no) |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 231 | expected_gle = voucher_obj.get_gl_entries(warehouse_account) |
| 232 | if expected_gle: |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 233 | if not existing_gle or not compare_existing_and_expected_gle(existing_gle, |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 234 | expected_gle): |
| 235 | _delete_gl_entries(voucher_type, voucher_no) |
| 236 | voucher_obj.make_gl_entries(repost_future_gle=False) |
| 237 | else: |
| 238 | _delete_gl_entries(voucher_type, voucher_no) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 239 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 240 | def compare_existing_and_expected_gle(existing_gle, expected_gle): |
| 241 | matched = True |
| 242 | for entry in expected_gle: |
| 243 | for e in existing_gle: |
| 244 | if entry.account==e.account and entry.against_account==e.against_account \ |
| 245 | and entry.cost_center==e.cost_center \ |
| 246 | and (entry.debit != e.debit or entry.credit != e.credit): |
| 247 | matched = False |
| 248 | break |
| 249 | return matched |
| 250 | |
Nabin Hait | b9e0481 | 2014-09-26 14:22:18 +0530 | [diff] [blame] | 251 | def get_future_stock_vouchers(posting_date, posting_time, for_warehouses=None, for_items=None): |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 252 | future_stock_vouchers = [] |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 253 | |
Anand Doshi | 913c51b | 2014-08-27 22:09:03 +0530 | [diff] [blame] | 254 | values = [] |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 255 | condition = "" |
| 256 | if for_items: |
Anand Doshi | 913c51b | 2014-08-27 22:09:03 +0530 | [diff] [blame] | 257 | condition += " and item_code in ({})".format(", ".join(["%s"] * len(for_items))) |
| 258 | values += for_items |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 259 | |
Nabin Hait | b9e0481 | 2014-09-26 14:22:18 +0530 | [diff] [blame] | 260 | if for_warehouses: |
| 261 | condition += " and warehouse in ({})".format(", ".join(["%s"] * len(for_warehouses))) |
| 262 | values += for_warehouses |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 263 | |
| 264 | for d in frappe.db.sql("""select distinct sle.voucher_type, sle.voucher_no |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 265 | from `tabStock Ledger Entry` sle |
Anand Doshi | 913c51b | 2014-08-27 22:09:03 +0530 | [diff] [blame] | 266 | where timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s) {condition} |
| 267 | order by timestamp(sle.posting_date, sle.posting_time) asc, name asc""".format(condition=condition), |
| 268 | tuple([posting_date, posting_time] + values), as_dict=True): |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 269 | future_stock_vouchers.append([d.voucher_type, d.voucher_no]) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 270 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 271 | return future_stock_vouchers |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 272 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 273 | def get_voucherwise_gl_entries(future_stock_vouchers, posting_date): |
| 274 | gl_entries = {} |
| 275 | if future_stock_vouchers: |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 276 | for d in frappe.db.sql("""select * from `tabGL Entry` |
| 277 | where posting_date >= %s and voucher_no in (%s)""" % |
| 278 | ('%s', ', '.join(['%s']*len(future_stock_vouchers))), |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 279 | tuple([posting_date] + [d[1] for d in future_stock_vouchers]), as_dict=1): |
| 280 | gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 281 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 282 | return gl_entries |
| 283 | |
| 284 | def get_warehouse_account(): |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 285 | warehouse_account = dict(frappe.db.sql("""select master_name, name from tabAccount |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 286 | where account_type = 'Warehouse' and ifnull(master_name, '') != ''""")) |
Anand Doshi | 67d6a4e | 2014-03-28 13:41:59 +0530 | [diff] [blame] | 287 | return warehouse_account |