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 | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 3 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 4 | import frappe |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 5 | from frappe import _ |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 6 | import json |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 7 | from frappe.utils import flt, cstr, nowdate, nowtime |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 8 | from frappe.defaults import get_global_default |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 9 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 10 | class InvalidWarehouseCompany(frappe.ValidationError): pass |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 11 | |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 12 | def get_stock_value_on(warehouse=None, posting_date=None, item_code=None): |
Nabin Hait | 0dd7be1 | 2013-08-02 11:45:43 +0530 | [diff] [blame] | 13 | if not posting_date: posting_date = nowdate() |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 14 | |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 15 | values, condition = [posting_date], "" |
| 16 | |
| 17 | if warehouse: |
| 18 | values.append(warehouse) |
| 19 | condition += " AND warehouse = %s" |
| 20 | |
| 21 | if item_code: |
| 22 | values.append(item_code) |
| 23 | condition.append(" AND item_code = %s") |
| 24 | |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 25 | stock_ledger_entries = frappe.db.sql(""" |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 26 | SELECT item_code, stock_value |
| 27 | FROM `tabStock Ledger Entry` |
| 28 | WHERE posting_date <= %s {0} |
Nabin Hait | 0dd7be1 | 2013-08-02 11:45:43 +0530 | [diff] [blame] | 29 | ORDER BY timestamp(posting_date, posting_time) DESC, name DESC |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 30 | """.format(condition), values, as_dict=1) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 31 | |
Nabin Hait | 0dd7be1 | 2013-08-02 11:45:43 +0530 | [diff] [blame] | 32 | sle_map = {} |
| 33 | for sle in stock_ledger_entries: |
Nabin Hait | 625da79 | 2013-09-25 10:32:51 +0530 | [diff] [blame] | 34 | sle_map.setdefault(sle.item_code, flt(sle.stock_value)) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 35 | |
Nabin Hait | 625da79 | 2013-09-25 10:32:51 +0530 | [diff] [blame] | 36 | return sum(sle_map.values()) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 37 | |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 38 | def get_stock_balance(item_code, warehouse, posting_date=None, posting_time=None): |
| 39 | if not posting_date: posting_date = nowdate() |
| 40 | if not posting_time: posting_time = nowtime() |
| 41 | last_entry = frappe.db.sql("""select qty_after_transaction from `tabStock Ledger Entry` |
| 42 | where item_code=%s and warehouse=%s |
| 43 | and timestamp(posting_date, posting_time) < timestamp(%s, %s) |
| 44 | order by timestamp(posting_date, posting_time) limit 1""", |
| 45 | (item_code, warehouse, posting_date, posting_time)) |
| 46 | |
| 47 | if last_entry: |
| 48 | return last_entry[0][0] |
| 49 | else: |
| 50 | return 0.0 |
| 51 | |
Nabin Hait | 47dc318 | 2013-08-06 15:58:16 +0530 | [diff] [blame] | 52 | def get_latest_stock_balance(): |
| 53 | bin_map = {} |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 54 | for d in frappe.db.sql("""SELECT item_code, warehouse, stock_value as stock_value |
Nabin Hait | 47dc318 | 2013-08-06 15:58:16 +0530 | [diff] [blame] | 55 | FROM tabBin""", as_dict=1): |
Nabin Hait | 469ee71 | 2013-08-07 12:33:37 +0530 | [diff] [blame] | 56 | bin_map.setdefault(d.warehouse, {}).setdefault(d.item_code, flt(d.stock_value)) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 57 | |
Nabin Hait | 47dc318 | 2013-08-06 15:58:16 +0530 | [diff] [blame] | 58 | return bin_map |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 59 | |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 60 | def get_bin(item_code, warehouse): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 61 | bin = frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse}) |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 62 | if not bin: |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 63 | bin_obj = frappe.get_doc({ |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 64 | "doctype": "Bin", |
| 65 | "item_code": item_code, |
| 66 | "warehouse": warehouse, |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 67 | }) |
| 68 | bin_obj.ignore_permissions = 1 |
| 69 | bin_obj.insert() |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 70 | else: |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 71 | bin_obj = frappe.get_doc('Bin', bin) |
Anand Doshi | 094610d | 2014-04-16 19:56:53 +0530 | [diff] [blame] | 72 | bin_obj.ignore_permissions = True |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 73 | return bin_obj |
| 74 | |
| 75 | def update_bin(args): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 76 | is_stock_item = frappe.db.get_value('Item', args.get("item_code"), 'is_stock_item') |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 77 | if is_stock_item == 'Yes': |
| 78 | bin = get_bin(args.get("item_code"), args.get("warehouse")) |
| 79 | bin.update_stock(args) |
| 80 | return bin |
| 81 | else: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 82 | frappe.msgprint(_("Item {0} ignored since it is not a stock item").format(args.get("item_code"))) |
Nabin Hait | 0dd7be1 | 2013-08-02 11:45:43 +0530 | [diff] [blame] | 83 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 84 | def get_incoming_rate(args): |
| 85 | """Get Incoming Rate based on valuation method""" |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 86 | from erpnext.stock.stock_ledger import get_previous_sle |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 87 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 88 | in_rate = 0 |
Anand Doshi | 40a8ae2 | 2014-08-29 16:28:31 +0530 | [diff] [blame] | 89 | if (args.get("serial_no") or "").strip(): |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 90 | in_rate = get_avg_purchase_rate(args.get("serial_no")) |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 91 | else: |
| 92 | valuation_method = get_valuation_method(args.get("item_code")) |
| 93 | previous_sle = get_previous_sle(args) |
| 94 | if valuation_method == 'FIFO': |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 95 | if not previous_sle: |
| 96 | return 0.0 |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 97 | previous_stock_queue = json.loads(previous_sle.get('stock_queue', '[]') or '[]') |
Nabin Hait | 227db76 | 2014-05-08 19:06:01 +0530 | [diff] [blame] | 98 | in_rate = get_fifo_rate(previous_stock_queue, args.get("qty") or 0) if previous_stock_queue else 0 |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 99 | elif valuation_method == 'Moving Average': |
| 100 | in_rate = previous_sle.get('valuation_rate') or 0 |
Anand Doshi | 094610d | 2014-04-16 19:56:53 +0530 | [diff] [blame] | 101 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 102 | return in_rate |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 103 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 104 | def get_avg_purchase_rate(serial_nos): |
| 105 | """get average value of serial numbers""" |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 106 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 107 | serial_nos = get_valid_serial_nos(serial_nos) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 108 | return flt(frappe.db.sql("""select avg(ifnull(purchase_rate, 0)) from `tabSerial No` |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 109 | where name in (%s)""" % ", ".join(["%s"] * len(serial_nos)), |
| 110 | tuple(serial_nos))[0][0]) |
| 111 | |
| 112 | def get_valuation_method(item_code): |
| 113 | """get valuation method from item or default""" |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 114 | val_method = frappe.db.get_value('Item', item_code, 'valuation_method') |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 115 | if not val_method: |
Rushabh Mehta | 5117d9c | 2013-02-19 15:27:31 +0530 | [diff] [blame] | 116 | val_method = get_global_default('valuation_method') or "FIFO" |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 117 | return val_method |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 118 | |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 119 | def get_fifo_rate(previous_stock_queue, qty): |
| 120 | """get FIFO (average) Rate from Queue""" |
| 121 | if qty >= 0: |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 122 | total = sum(f[0] for f in previous_stock_queue) |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 123 | return total and sum(f[0] * f[1] for f in previous_stock_queue) / flt(total) or 0.0 |
| 124 | else: |
Nabin Hait | 227db76 | 2014-05-08 19:06:01 +0530 | [diff] [blame] | 125 | available_qty_for_outgoing, outgoing_cost = 0, 0 |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 126 | qty_to_pop = abs(qty) |
Nabin Hait | 64d7c4b | 2013-01-17 12:22:59 +0530 | [diff] [blame] | 127 | while qty_to_pop and previous_stock_queue: |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 128 | batch = previous_stock_queue[0] |
| 129 | if 0 < batch[0] <= qty_to_pop: |
| 130 | # if batch qty > 0 |
| 131 | # not enough or exactly same qty in current batch, clear batch |
Nabin Hait | 227db76 | 2014-05-08 19:06:01 +0530 | [diff] [blame] | 132 | available_qty_for_outgoing += flt(batch[0]) |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 133 | outgoing_cost += flt(batch[0]) * flt(batch[1]) |
| 134 | qty_to_pop -= batch[0] |
| 135 | previous_stock_queue.pop(0) |
| 136 | else: |
| 137 | # all from current batch |
Nabin Hait | 227db76 | 2014-05-08 19:06:01 +0530 | [diff] [blame] | 138 | available_qty_for_outgoing += flt(qty_to_pop) |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 139 | outgoing_cost += flt(qty_to_pop) * flt(batch[1]) |
| 140 | batch[0] -= qty_to_pop |
| 141 | qty_to_pop = 0 |
Anand Doshi | 094610d | 2014-04-16 19:56:53 +0530 | [diff] [blame] | 142 | |
Nabin Hait | 227db76 | 2014-05-08 19:06:01 +0530 | [diff] [blame] | 143 | return outgoing_cost / available_qty_for_outgoing |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 144 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 145 | def get_valid_serial_nos(sr_nos, qty=0, item_code=''): |
| 146 | """split serial nos, validate and return list of valid serial nos""" |
| 147 | # TODO: remove duplicates in client side |
| 148 | serial_nos = cstr(sr_nos).strip().replace(',', '\n').split('\n') |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 149 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 150 | valid_serial_nos = [] |
| 151 | for val in serial_nos: |
| 152 | if val: |
| 153 | val = val.strip() |
| 154 | if val in valid_serial_nos: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 155 | frappe.throw(_("Serial number {0} entered more than once").format(val)) |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 156 | else: |
| 157 | valid_serial_nos.append(val) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 158 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 159 | if qty and len(valid_serial_nos) != abs(qty): |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 160 | frappe.throw(_("{0} valid serial nos for Item {1}").format(abs(qty), item_code)) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 161 | |
Rushabh Mehta | 0dbe898 | 2013-02-04 13:56:50 +0530 | [diff] [blame] | 162 | return valid_serial_nos |
Nabin Hait | a72c512 | 2013-03-06 18:50:53 +0530 | [diff] [blame] | 163 | |
Anand Doshi | 373680b | 2013-10-10 16:04:40 +0530 | [diff] [blame] | 164 | def validate_warehouse_company(warehouse, company): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 165 | warehouse_company = frappe.db.get_value("Warehouse", warehouse, "company") |
Anand Doshi | 373680b | 2013-10-10 16:04:40 +0530 | [diff] [blame] | 166 | if warehouse_company and warehouse_company != company: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 167 | frappe.throw(_("Warehouse {0} does not belong to company {1}").format(warehouse, company), |
| 168 | InvalidWarehouseCompany) |
Rushabh Mehta | a65253b | 2013-08-27 10:32:56 +0530 | [diff] [blame] | 169 | |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 170 | def get_sales_bom_buying_amount(item_code, warehouse, voucher_type, voucher_no, voucher_detail_no, |
Nabin Hait | 94c90bd | 2013-08-30 22:48:19 +0530 | [diff] [blame] | 171 | stock_ledger_entries, item_sales_bom): |
| 172 | # sales bom item |
| 173 | buying_amount = 0.0 |
| 174 | for bom_item in item_sales_bom[item_code]: |
| 175 | if bom_item.get("parent_detail_docname")==voucher_detail_no: |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 176 | buying_amount += get_buying_amount(voucher_type, voucher_no, voucher_detail_no, |
Nabin Hait | 94c90bd | 2013-08-30 22:48:19 +0530 | [diff] [blame] | 177 | stock_ledger_entries.get((bom_item.item_code, warehouse), [])) |
| 178 | |
| 179 | return buying_amount |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 180 | |
ankitjavalkarwork | 9aa11d2 | 2014-09-18 19:06:11 +0530 | [diff] [blame] | 181 | def get_buying_amount(item_code, item_qty, voucher_type, voucher_no, item_row, stock_ledger_entries): |
Anand Doshi | 5dd6b1d | 2013-08-07 19:27:30 +0530 | [diff] [blame] | 182 | # IMP NOTE |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 183 | # stock_ledger_entries should already be filtered by item_code and warehouse and |
Anand Doshi | 5dd6b1d | 2013-08-07 19:27:30 +0530 | [diff] [blame] | 184 | # sorted by posting_date desc, posting_time desc |
ankitjavalkarwork | 9aa11d2 | 2014-09-18 19:06:11 +0530 | [diff] [blame] | 185 | if frappe.db.get_value("Item", item_code, "is_stock_item") == "Yes": |
| 186 | for i, sle in enumerate(stock_ledger_entries): |
| 187 | if sle.voucher_type == voucher_type and sle.voucher_no == voucher_no and \ |
| 188 | sle.voucher_detail_no == item_row: |
| 189 | previous_stock_value = len(stock_ledger_entries) > i+1 and \ |
| 190 | flt(stock_ledger_entries[i+1].stock_value) or 0.0 |
| 191 | buying_amount = previous_stock_value - flt(sle.stock_value) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 192 | |
ankitjavalkarwork | 9aa11d2 | 2014-09-18 19:06:11 +0530 | [diff] [blame] | 193 | return buying_amount |
| 194 | else: |
| 195 | item_rate = frappe.db.sql("""select sum(base_amount) / sum(qty) |
| 196 | from `tabPurchase Invoice Item` |
| 197 | where item_code = %s and docstatus=1""" % ('%s'), item_code) |
Nabin Hait | 5e080f9 | 2014-09-26 15:05:30 +0530 | [diff] [blame] | 198 | buying_amount = flt(item_qty) * flt(item_rate[0][0]) if item_rate else 0 |
ankitjavalkarwork | 9aa11d2 | 2014-09-18 19:06:11 +0530 | [diff] [blame] | 199 | |
| 200 | return buying_amount |
| 201 | |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 202 | return 0.0 |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 203 | |