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): |
Anand Doshi | 78b6366 | 2014-08-19 12:20:48 +0530 | [diff] [blame] | 169 | condition = "" |
| 170 | item_list = [] |
| 171 | if getattr(self, "fname", None): |
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)] |
Anand Doshi | 78b6366 | 2014-08-19 12:20:48 +0530 | [diff] [blame] | 173 | if item_list: |
| 174 | condition = "and item_code in ({})".format(", ".join(["%s"] * len(item_list))) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 175 | |
Anand Doshi | 78b6366 | 2014-08-19 12:20:48 +0530 | [diff] [blame] | 176 | future_stock_vouchers = frappe.db.sql("""select distinct sle.voucher_type, sle.voucher_no |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 177 | from `tabStock Ledger Entry` sle |
Anand Doshi | 78b6366 | 2014-08-19 12:20:48 +0530 | [diff] [blame] | 178 | where timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s) {condition} |
| 179 | order by timestamp(sle.posting_date, sle.posting_time) asc, name asc""".format( |
| 180 | condition=condition), tuple([self.posting_date, self.posting_date] + item_list), |
| 181 | as_list=True) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 182 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 183 | return future_stock_vouchers |
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 | def get_voucherwise_gl_entries(self, future_stock_vouchers): |
| 186 | gl_entries = {} |
| 187 | if future_stock_vouchers: |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 188 | for d in frappe.db.sql("""select * from `tabGL Entry` |
| 189 | where posting_date >= %s and voucher_no in (%s)""" % |
| 190 | ('%s', ', '.join(['%s']*len(future_stock_vouchers))), |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 191 | 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] | 192 | gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 193 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 194 | return gl_entries |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 195 | |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 196 | def delete_gl_entries(self, voucher_type, voucher_no): |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 197 | frappe.db.sql("""delete from `tabGL Entry` |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 198 | where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no)) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 199 | |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 200 | def make_adjustment_entry(self, expected_gle, voucher_obj): |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 201 | from erpnext.accounts.utils import get_stock_and_account_difference |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 202 | account_list = [d.account for d in expected_gle] |
| 203 | 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] | 204 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 205 | cost_center = self.get_company_default("cost_center") |
| 206 | stock_adjustment_account = self.get_company_default("stock_adjustment_account") |
| 207 | |
Nabin Hait | d474194 | 2013-08-06 15:57:25 +0530 | [diff] [blame] | 208 | gl_entries = [] |
| 209 | for account, diff in acc_diff.items(): |
| 210 | if diff: |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 211 | gl_entries.append([ |
| 212 | # stock in hand account |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 213 | voucher_obj.get_gl_dict({ |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 214 | "account": account, |
| 215 | "against": stock_adjustment_account, |
| 216 | "debit": diff, |
| 217 | "remarks": "Adjustment Accounting Entry for Stock", |
| 218 | }), |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 219 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 220 | # account against stock in hand |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 221 | voucher_obj.get_gl_dict({ |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 222 | "account": stock_adjustment_account, |
| 223 | "against": account, |
| 224 | "credit": diff, |
| 225 | "cost_center": cost_center or None, |
| 226 | "remarks": "Adjustment Accounting Entry for Stock", |
| 227 | }), |
| 228 | ]) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 229 | |
Nabin Hait | d474194 | 2013-08-06 15:57:25 +0530 | [diff] [blame] | 230 | if gl_entries: |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 231 | from erpnext.accounts.general_ledger import make_gl_entries |
Nabin Hait | d474194 | 2013-08-06 15:57:25 +0530 | [diff] [blame] | 232 | make_gl_entries(gl_entries) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 233 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 234 | def check_expense_account(self, item): |
Rushabh Mehta | 052fe82 | 2014-04-16 19:20:11 +0530 | [diff] [blame] | 235 | if not item.get("expense_account"): |
Nabin Hait | d341254 | 2014-05-01 17:42:21 +0530 | [diff] [blame] | 236 | 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] | 237 | |
Anand Doshi | 496123a | 2014-06-19 19:25:19 +0530 | [diff] [blame] | 238 | else: |
Nabin Hait | 00778c9 | 2014-06-25 19:12:24 +0530 | [diff] [blame] | 239 | is_expense_account = frappe.db.get_value("Account", |
| 240 | item.get("expense_account"), "report_type")=="Profit and Loss" |
nabinhait | e6cee7b | 2014-07-10 19:06:39 +0530 | [diff] [blame] | 241 | 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] | 242 | frappe.throw(_("Expense / Difference account ({0}) must be a 'Profit or Loss' account") |
| 243 | .format(item.get("expense_account"))) |
Anand Doshi | 496123a | 2014-06-19 19:25:19 +0530 | [diff] [blame] | 244 | if is_expense_account and not item.get("cost_center"): |
| 245 | frappe.throw(_("{0} {1}: Cost Center is mandatory for Item {2}").format( |
| 246 | _(self.doctype), self.name, item.get("item_code"))) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 247 | |
| 248 | def get_sl_entries(self, d, args): |
Nabin Hait | 1e2f20a | 2013-08-02 11:42:11 +0530 | [diff] [blame] | 249 | sl_dict = { |
Nabin Hait | 296d626 | 2014-05-02 17:36:13 +0530 | [diff] [blame] | 250 | "item_code": d.get("item_code", None), |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 251 | "warehouse": d.get("warehouse", None), |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 252 | "posting_date": self.posting_date, |
| 253 | "posting_time": self.posting_time, |
| 254 | "voucher_type": self.doctype, |
| 255 | "voucher_no": self.name, |
Nabin Hait | 1e2f20a | 2013-08-02 11:42:11 +0530 | [diff] [blame] | 256 | "voucher_detail_no": d.name, |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 257 | "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] | 258 | "stock_uom": d.get("stock_uom"), |
Nabin Hait | 1e2f20a | 2013-08-02 11:42:11 +0530 | [diff] [blame] | 259 | "incoming_rate": 0, |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 260 | "company": self.company, |
| 261 | "fiscal_year": self.fiscal_year, |
Nabin Hait | 296d626 | 2014-05-02 17:36:13 +0530 | [diff] [blame] | 262 | "batch_no": cstr(d.get("batch_no")).strip(), |
| 263 | "serial_no": d.get("serial_no"), |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 264 | "project": d.get("project_name"), |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 265 | "is_cancelled": self.docstatus==2 and "Yes" or "No" |
Nabin Hait | 1e2f20a | 2013-08-02 11:42:11 +0530 | [diff] [blame] | 266 | } |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 267 | |
Nabin Hait | 1e2f20a | 2013-08-02 11:42:11 +0530 | [diff] [blame] | 268 | sl_dict.update(args) |
| 269 | return sl_dict |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 270 | |
Nabin Hait | 1e2f20a | 2013-08-02 11:42:11 +0530 | [diff] [blame] | 271 | def make_sl_entries(self, sl_entries, is_amended=None): |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 272 | from erpnext.stock.stock_ledger import make_sl_entries |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 273 | make_sl_entries(sl_entries, is_amended) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 274 | |
nabinhait | 87f2401 | 2014-07-16 19:48:29 +0530 | [diff] [blame] | 275 | def make_gl_entries_on_cancel(self): |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 276 | 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] | 277 | and voucher_no=%s""", (self.doctype, self.name)): |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 278 | self.make_gl_entries() |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 279 | |
Anand Doshi | a740f75 | 2014-06-25 13:31:02 +0530 | [diff] [blame] | 280 | def get_serialized_items(self): |
| 281 | serialized_items = [] |
| 282 | item_codes = list(set([d.item_code for d in self.get(self.fname)])) |
| 283 | if item_codes: |
| 284 | serialized_items = frappe.db.sql_list("""select name from `tabItem` |
| 285 | where has_serial_no='Yes' and name in ({})""".format(", ".join(["%s"]*len(item_codes))), |
| 286 | tuple(item_codes)) |
| 287 | |
| 288 | return serialized_items |
| 289 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 290 | def update_gl_entries_after(posting_date, posting_time, warehouse_account=None, for_items=None): |
| 291 | def _delete_gl_entries(voucher_type, voucher_no): |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 292 | frappe.db.sql("""delete from `tabGL Entry` |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 293 | where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no)) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 294 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 295 | if not warehouse_account: |
| 296 | warehouse_account = get_warehouse_account() |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 297 | future_stock_vouchers = get_future_stock_vouchers(posting_date, posting_time, |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 298 | warehouse_account, for_items) |
| 299 | gle = get_voucherwise_gl_entries(future_stock_vouchers, posting_date) |
| 300 | |
| 301 | for voucher_type, voucher_no in future_stock_vouchers: |
| 302 | existing_gle = gle.get((voucher_type, voucher_no), []) |
Rushabh Mehta | 0a0f249 | 2014-03-31 17:27:06 +0530 | [diff] [blame] | 303 | voucher_obj = frappe.get_doc(voucher_type, voucher_no) |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 304 | expected_gle = voucher_obj.get_gl_entries(warehouse_account) |
| 305 | if expected_gle: |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 306 | 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] | 307 | expected_gle): |
| 308 | _delete_gl_entries(voucher_type, voucher_no) |
| 309 | voucher_obj.make_gl_entries(repost_future_gle=False) |
| 310 | else: |
| 311 | _delete_gl_entries(voucher_type, voucher_no) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 312 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 313 | def compare_existing_and_expected_gle(existing_gle, expected_gle): |
| 314 | matched = True |
| 315 | for entry in expected_gle: |
| 316 | for e in existing_gle: |
| 317 | if entry.account==e.account and entry.against_account==e.against_account \ |
| 318 | and entry.cost_center==e.cost_center \ |
| 319 | and (entry.debit != e.debit or entry.credit != e.credit): |
| 320 | matched = False |
| 321 | break |
| 322 | return matched |
| 323 | |
| 324 | def get_future_stock_vouchers(posting_date, posting_time, warehouse_account=None, for_items=None): |
| 325 | future_stock_vouchers = [] |
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 | condition = "" |
| 328 | if for_items: |
| 329 | condition = ''.join([' and item_code in (\'', '\', \''.join(for_items) ,'\')']) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 330 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 331 | if warehouse_account: |
| 332 | condition += ''.join([' and warehouse in (\'', '\', \''.join(warehouse_account.keys()) ,'\')']) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 333 | |
| 334 | 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] | 335 | from `tabStock Ledger Entry` sle |
| 336 | where timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s) %s |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 337 | order by timestamp(sle.posting_date, sle.posting_time) asc, name asc""" % |
| 338 | ('%s', '%s', condition), (posting_date, posting_time), |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 339 | as_dict=True): |
| 340 | future_stock_vouchers.append([d.voucher_type, d.voucher_no]) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 341 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 342 | return future_stock_vouchers |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 343 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 344 | def get_voucherwise_gl_entries(future_stock_vouchers, posting_date): |
| 345 | gl_entries = {} |
| 346 | if future_stock_vouchers: |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 347 | for d in frappe.db.sql("""select * from `tabGL Entry` |
| 348 | where posting_date >= %s and voucher_no in (%s)""" % |
| 349 | ('%s', ', '.join(['%s']*len(future_stock_vouchers))), |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 350 | tuple([posting_date] + [d[1] for d in future_stock_vouchers]), as_dict=1): |
| 351 | gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 352 | |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 353 | return gl_entries |
| 354 | |
| 355 | def get_warehouse_account(): |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 356 | warehouse_account = dict(frappe.db.sql("""select master_name, name from tabAccount |
Nabin Hait | becf75d | 2014-03-27 17:18:29 +0530 | [diff] [blame] | 357 | where account_type = 'Warehouse' and ifnull(master_name, '') != ''""")) |
Anand Doshi | 67d6a4e | 2014-03-28 13:41:59 +0530 | [diff] [blame] | 358 | return warehouse_account |