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 | 145e5e2 | 2013-10-22 23:51:41 +0530 | [diff] [blame] | 19 | warehouse_account = self.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: |
| 26 | items, warehouse_account = self.get_items_and_warehouse_accounts(warehouse_account) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 27 | update_gl_entries_after(self.posting_date, self.posting_time, |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 28 | warehouse_account, items) |
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): |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 32 | from erpnext.accounts.general_ledger import process_gl_map |
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 | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 91 | def get_items_and_warehouse_accounts(self, warehouse_account=None): |
| 92 | items, warehouses = [], [] |
| 93 | if not warehouse_account: |
| 94 | warehouse_account = get_warehouse_account() |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 95 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 96 | if hasattr(self, "fname"): |
Rushabh Mehta | f191f85 | 2014-04-02 18:09:34 +0530 | [diff] [blame] | 97 | item_doclist = self.get(self.fname) |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 98 | elif self.doctype == "Stock Reconciliation": |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 99 | import json |
| 100 | item_doclist = [] |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 101 | data = json.loads(self.reconciliation_json) |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 102 | for row in data[data.index(self.head_row)+1:]: |
| 103 | d = frappe._dict(zip(["item_code", "warehouse", "qty", "valuation_rate"], row)) |
| 104 | item_doclist.append(d) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 105 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 106 | if item_doclist: |
| 107 | for d in item_doclist: |
| 108 | if d.item_code and d.item_code not in items: |
| 109 | items.append(d.item_code) |
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 d.get("warehouse") and d.warehouse not in warehouses: |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 112 | warehouses.append(d.warehouse) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 113 | |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 114 | if self.doctype == "Stock Entry": |
| 115 | if d.get("s_warehouse") and d.s_warehouse not in warehouses: |
| 116 | warehouses.append(d.s_warehouse) |
| 117 | if d.get("t_warehouse") and d.t_warehouse not in warehouses: |
| 118 | warehouses.append(d.t_warehouse) |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 119 | |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 120 | warehouse_account = {wh: warehouse_account[wh] for wh in warehouses |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 121 | if warehouse_account.get(wh)} |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 122 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 123 | return items, warehouse_account |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 124 | |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 125 | def get_stock_ledger_details(self): |
| 126 | stock_ledger = {} |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 127 | 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] | 128 | from `tabStock Ledger Entry` where voucher_type=%s and voucher_no=%s""", |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 129 | (self.doctype, self.name), as_dict=True): |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 130 | stock_ledger.setdefault(sle.voucher_detail_no, []).append(sle) |
| 131 | return stock_ledger |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 132 | |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 133 | def get_warehouse_account(self): |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 134 | warehouse_account = dict(frappe.db.sql("""select master_name, name from tabAccount |
Nabin Hait | 7a75e10 | 2013-09-17 10:21:20 +0530 | [diff] [blame] | 135 | where account_type = 'Warehouse' and ifnull(master_name, '') != ''""")) |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 136 | return warehouse_account |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 137 | |
Nabin Hait | 142007a | 2013-09-17 15:15:16 +0530 | [diff] [blame] | 138 | def update_gl_entries_after(self, warehouse_account=None): |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 139 | future_stock_vouchers = self.get_future_stock_vouchers() |
| 140 | gle = self.get_voucherwise_gl_entries(future_stock_vouchers) |
Nabin Hait | 142007a | 2013-09-17 15:15:16 +0530 | [diff] [blame] | 141 | if not warehouse_account: |
| 142 | warehouse_account = self.get_warehouse_account() |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 143 | for voucher_type, voucher_no in future_stock_vouchers: |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 144 | existing_gle = gle.get((voucher_type, voucher_no), []) |
Rushabh Mehta | 0a0f249 | 2014-03-31 17:27:06 +0530 | [diff] [blame] | 145 | voucher_obj = frappe.get_doc(voucher_type, voucher_no) |
Nabin Hait | e83069c | 2013-11-14 18:40:08 +0530 | [diff] [blame] | 146 | expected_gle = voucher_obj.get_gl_entries(warehouse_account) |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 147 | if expected_gle: |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 148 | matched = True |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 149 | if existing_gle: |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 150 | for entry in expected_gle: |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 151 | for e in existing_gle: |
| 152 | if entry.account==e.account \ |
| 153 | and entry.against_account==e.against_account\ |
| 154 | and entry.cost_center==e.cost_center: |
| 155 | if entry.debit != e.debit or entry.credit != e.credit: |
| 156 | matched = False |
| 157 | break |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 158 | else: |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 159 | matched = False |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 160 | |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 161 | if not matched: |
| 162 | self.delete_gl_entries(voucher_type, voucher_no) |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 163 | voucher_obj.make_gl_entries(repost_future_gle=False) |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 164 | else: |
| 165 | self.delete_gl_entries(voucher_type, voucher_no) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 166 | |
| 167 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 168 | def get_future_stock_vouchers(self): |
| 169 | future_stock_vouchers = [] |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 170 | |
Nabin Hait | bf4d27e | 2013-09-04 17:55:45 +0530 | [diff] [blame] | 171 | if hasattr(self, "fname"): |
Rushabh Mehta | d2b34dc | 2014-03-27 16:12:56 +0530 | [diff] [blame] | 172 | item_list = [d.item_code for d in self.get(self.fname)] |
Nabin Hait | bf4d27e | 2013-09-04 17:55:45 +0530 | [diff] [blame] | 173 | condition = ''.join(['and item_code in (\'', '\', \''.join(item_list) ,'\')']) |
| 174 | else: |
| 175 | condition = "" |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 176 | |
| 177 | for d in frappe.db.sql("""select distinct sle.voucher_type, sle.voucher_no |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 178 | from `tabStock Ledger Entry` sle |
Nabin Hait | bf4d27e | 2013-09-04 17:55:45 +0530 | [diff] [blame] | 179 | where timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s) %s |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 180 | order by timestamp(sle.posting_date, sle.posting_time) asc, name asc""" % |
| 181 | ('%s', '%s', condition), (self.posting_date, self.posting_time), |
Nabin Hait | bf4d27e | 2013-09-04 17:55:45 +0530 | [diff] [blame] | 182 | as_dict=True): |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 183 | future_stock_vouchers.append([d.voucher_type, d.voucher_no]) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 184 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 185 | return future_stock_vouchers |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 186 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 187 | def get_voucherwise_gl_entries(self, future_stock_vouchers): |
| 188 | gl_entries = {} |
| 189 | if future_stock_vouchers: |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 190 | for d in frappe.db.sql("""select * from `tabGL Entry` |
| 191 | where posting_date >= %s and voucher_no in (%s)""" % |
| 192 | ('%s', ', '.join(['%s']*len(future_stock_vouchers))), |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 193 | tuple([self.posting_date] + [d[1] for d in future_stock_vouchers]), as_dict=1): |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 194 | gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 195 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 196 | return gl_entries |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 197 | |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 198 | def delete_gl_entries(self, voucher_type, voucher_no): |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 199 | frappe.db.sql("""delete from `tabGL Entry` |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 200 | where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no)) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 201 | |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 202 | def make_adjustment_entry(self, expected_gle, voucher_obj): |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 203 | from erpnext.accounts.utils import get_stock_and_account_difference |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 204 | account_list = [d.account for d in expected_gle] |
| 205 | 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] | 206 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 207 | cost_center = self.get_company_default("cost_center") |
| 208 | stock_adjustment_account = self.get_company_default("stock_adjustment_account") |
| 209 | |
Nabin Hait | d474194 | 2013-08-06 15:57:25 +0530 | [diff] [blame] | 210 | gl_entries = [] |
| 211 | for account, diff in acc_diff.items(): |
| 212 | if diff: |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 213 | gl_entries.append([ |
| 214 | # stock in hand account |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 215 | voucher_obj.get_gl_dict({ |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 216 | "account": account, |
| 217 | "against": stock_adjustment_account, |
| 218 | "debit": diff, |
| 219 | "remarks": "Adjustment Accounting Entry for Stock", |
| 220 | }), |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 221 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 222 | # account against stock in hand |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 223 | voucher_obj.get_gl_dict({ |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 224 | "account": stock_adjustment_account, |
| 225 | "against": account, |
| 226 | "credit": diff, |
| 227 | "cost_center": cost_center or None, |
| 228 | "remarks": "Adjustment Accounting Entry for Stock", |
| 229 | }), |
| 230 | ]) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 231 | |
Nabin Hait | d474194 | 2013-08-06 15:57:25 +0530 | [diff] [blame] | 232 | if gl_entries: |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 233 | from erpnext.accounts.general_ledger import make_gl_entries |
Nabin Hait | d474194 | 2013-08-06 15:57:25 +0530 | [diff] [blame] | 234 | make_gl_entries(gl_entries) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 235 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 236 | def check_expense_account(self, item): |
Rushabh Mehta | 052fe82 | 2014-04-16 19:20:11 +0530 | [diff] [blame] | 237 | if not item.get("expense_account"): |
Nabin Hait | d341254 | 2014-05-01 17:42:21 +0530 | [diff] [blame] | 238 | 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] | 239 | |
Rushabh Mehta | eea8022 | 2014-04-16 19:38:40 +0530 | [diff] [blame] | 240 | if item.get("expense_account") and not item.get("cost_center"): |
| 241 | frappe.throw(_("""Cost Center is mandatory for Item {0}""").format(item.get("item_code"))) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 242 | |
| 243 | def get_sl_entries(self, d, args): |
Nabin Hait | 1e2f20a | 2013-08-02 11:42:11 +0530 | [diff] [blame] | 244 | sl_dict = { |
Nabin Hait | 296d626 | 2014-05-02 17:36:13 +0530 | [diff] [blame] | 245 | "item_code": d.get("item_code", None), |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 246 | "warehouse": d.get("warehouse", None), |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 247 | "posting_date": self.posting_date, |
| 248 | "posting_time": self.posting_time, |
| 249 | "voucher_type": self.doctype, |
| 250 | "voucher_no": self.name, |
Nabin Hait | 1e2f20a | 2013-08-02 11:42:11 +0530 | [diff] [blame] | 251 | "voucher_detail_no": d.name, |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 252 | "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] | 253 | "stock_uom": d.get("stock_uom"), |
Nabin Hait | 1e2f20a | 2013-08-02 11:42:11 +0530 | [diff] [blame] | 254 | "incoming_rate": 0, |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 255 | "company": self.company, |
| 256 | "fiscal_year": self.fiscal_year, |
Nabin Hait | 296d626 | 2014-05-02 17:36:13 +0530 | [diff] [blame] | 257 | "batch_no": cstr(d.get("batch_no")).strip(), |
| 258 | "serial_no": d.get("serial_no"), |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 259 | "project": d.get("project_name"), |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 260 | "is_cancelled": self.docstatus==2 and "Yes" or "No" |
Nabin Hait | 1e2f20a | 2013-08-02 11:42:11 +0530 | [diff] [blame] | 261 | } |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 262 | |
Nabin Hait | 1e2f20a | 2013-08-02 11:42:11 +0530 | [diff] [blame] | 263 | sl_dict.update(args) |
| 264 | return sl_dict |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 265 | |
Nabin Hait | 1e2f20a | 2013-08-02 11:42:11 +0530 | [diff] [blame] | 266 | def make_sl_entries(self, sl_entries, is_amended=None): |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 267 | from erpnext.stock.stock_ledger import make_sl_entries |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 268 | make_sl_entries(sl_entries, is_amended) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 269 | |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 270 | def make_cancel_gl_entries(self): |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 271 | 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] | 272 | and voucher_no=%s""", (self.doctype, self.name)): |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 273 | self.make_gl_entries() |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 274 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 275 | def update_gl_entries_after(posting_date, posting_time, warehouse_account=None, for_items=None): |
| 276 | def _delete_gl_entries(voucher_type, voucher_no): |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 277 | frappe.db.sql("""delete from `tabGL Entry` |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 278 | where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no)) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 279 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 280 | if not warehouse_account: |
| 281 | warehouse_account = get_warehouse_account() |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 282 | future_stock_vouchers = get_future_stock_vouchers(posting_date, posting_time, |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 283 | warehouse_account, for_items) |
| 284 | gle = get_voucherwise_gl_entries(future_stock_vouchers, posting_date) |
| 285 | |
| 286 | for voucher_type, voucher_no in future_stock_vouchers: |
| 287 | existing_gle = gle.get((voucher_type, voucher_no), []) |
Rushabh Mehta | 0a0f249 | 2014-03-31 17:27:06 +0530 | [diff] [blame] | 288 | voucher_obj = frappe.get_doc(voucher_type, voucher_no) |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 289 | expected_gle = voucher_obj.get_gl_entries(warehouse_account) |
| 290 | if expected_gle: |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 291 | 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] | 292 | expected_gle): |
| 293 | _delete_gl_entries(voucher_type, voucher_no) |
| 294 | voucher_obj.make_gl_entries(repost_future_gle=False) |
| 295 | else: |
| 296 | _delete_gl_entries(voucher_type, voucher_no) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 297 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 298 | def compare_existing_and_expected_gle(existing_gle, expected_gle): |
| 299 | matched = True |
| 300 | for entry in expected_gle: |
| 301 | for e in existing_gle: |
| 302 | if entry.account==e.account and entry.against_account==e.against_account \ |
| 303 | and entry.cost_center==e.cost_center \ |
| 304 | and (entry.debit != e.debit or entry.credit != e.credit): |
| 305 | matched = False |
| 306 | break |
| 307 | return matched |
| 308 | |
| 309 | def get_future_stock_vouchers(posting_date, posting_time, warehouse_account=None, for_items=None): |
| 310 | future_stock_vouchers = [] |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 311 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 312 | condition = "" |
| 313 | if for_items: |
| 314 | condition = ''.join([' and item_code in (\'', '\', \''.join(for_items) ,'\')']) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 315 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 316 | if warehouse_account: |
| 317 | condition += ''.join([' and warehouse in (\'', '\', \''.join(warehouse_account.keys()) ,'\')']) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 318 | |
| 319 | 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] | 320 | from `tabStock Ledger Entry` sle |
| 321 | where timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s) %s |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 322 | order by timestamp(sle.posting_date, sle.posting_time) asc, name asc""" % |
| 323 | ('%s', '%s', condition), (posting_date, posting_time), |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 324 | as_dict=True): |
| 325 | future_stock_vouchers.append([d.voucher_type, d.voucher_no]) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 326 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 327 | return future_stock_vouchers |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 328 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 329 | def get_voucherwise_gl_entries(future_stock_vouchers, posting_date): |
| 330 | gl_entries = {} |
| 331 | if future_stock_vouchers: |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 332 | for d in frappe.db.sql("""select * from `tabGL Entry` |
| 333 | where posting_date >= %s and voucher_no in (%s)""" % |
| 334 | ('%s', ', '.join(['%s']*len(future_stock_vouchers))), |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 335 | tuple([posting_date] + [d[1] for d in future_stock_vouchers]), as_dict=1): |
| 336 | gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 337 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 338 | return gl_entries |
| 339 | |
| 340 | def get_warehouse_account(): |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 341 | warehouse_account = dict(frappe.db.sql("""select master_name, name from tabAccount |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 342 | where account_type = 'Warehouse' and ifnull(master_name, '') != ''""")) |
Anand Doshi | 67d6a4e | 2014-03-28 13:41:59 +0530 | [diff] [blame] | 343 | return warehouse_account |