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