Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe 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 | |
Anand Doshi | d57e793 | 2015-02-24 12:24:53 +0530 | [diff] [blame] | 4 | from __future__ import unicode_literals |
rohitwaghchaure | ce8adec | 2017-12-15 12:13:50 +0530 | [diff] [blame] | 5 | import frappe, erpnext |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 6 | from frappe import _ |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 7 | import json |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 8 | from frappe.utils import flt, cstr, nowdate, nowtime |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 9 | |
Achilles Rasquinha | 56b2e12 | 2018-02-13 14:42:40 +0530 | [diff] [blame] | 10 | from six import string_types |
| 11 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 12 | class InvalidWarehouseCompany(frappe.ValidationError): pass |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 13 | |
Shreya Shah | e0a47ae | 2018-08-28 13:46:22 +0530 | [diff] [blame] | 14 | def get_stock_value_from_bin(warehouse=None, item_code=None): |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 15 | values = {} |
| 16 | conditions = "" |
| 17 | if warehouse: |
| 18 | conditions += """ and warehouse in ( |
| 19 | select w2.name from `tabWarehouse` w1 |
| 20 | join `tabWarehouse` w2 on |
| 21 | w1.name = %(warehouse)s |
| 22 | and w2.lft between w1.lft and w1.rgt |
| 23 | ) """ |
| 24 | |
| 25 | values['warehouse'] = warehouse |
| 26 | |
| 27 | if item_code: |
| 28 | conditions += " and item_code = %(item_code)s" |
| 29 | |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 30 | values['item_code'] = item_code |
| 31 | |
Shreya Shah | e0a47ae | 2018-08-28 13:46:22 +0530 | [diff] [blame] | 32 | query = "select sum(stock_value) from `tabBin` where 1 = 1 %s" % conditions |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 33 | |
| 34 | stock_value = frappe.db.sql(query, values) |
| 35 | |
Shreya Shah | e0a47ae | 2018-08-28 13:46:22 +0530 | [diff] [blame] | 36 | return stock_value |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 37 | |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 38 | 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] | 39 | if not posting_date: posting_date = nowdate() |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 40 | |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 41 | values, condition = [posting_date], "" |
| 42 | |
| 43 | if warehouse: |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 44 | |
Saurabh | 4d02949 | 2016-06-23 12:44:06 +0530 | [diff] [blame] | 45 | lft, rgt, is_group = frappe.db.get_value("Warehouse", warehouse, ["lft", "rgt", "is_group"]) |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 46 | |
Saurabh | 93d68ac | 2016-06-26 22:50:11 +0530 | [diff] [blame] | 47 | if is_group: |
Saurabh | 4d02949 | 2016-06-23 12:44:06 +0530 | [diff] [blame] | 48 | values.extend([lft, rgt]) |
Saurabh | 554f6f7 | 2016-06-06 14:22:37 +0530 | [diff] [blame] | 49 | condition += "and exists (\ |
| 50 | select name from `tabWarehouse` wh where wh.name = sle.warehouse\ |
| 51 | and wh.lft >= %s and wh.rgt <= %s)" |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 52 | |
Saurabh | 554f6f7 | 2016-06-06 14:22:37 +0530 | [diff] [blame] | 53 | else: |
| 54 | values.append(warehouse) |
| 55 | condition += " AND warehouse = %s" |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 56 | |
| 57 | if item_code: |
| 58 | values.append(item_code) |
| 59 | condition.append(" AND item_code = %s") |
| 60 | |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 61 | stock_ledger_entries = frappe.db.sql(""" |
Saurabh | 554f6f7 | 2016-06-06 14:22:37 +0530 | [diff] [blame] | 62 | SELECT item_code, stock_value, name, warehouse |
| 63 | FROM `tabStock Ledger Entry` sle |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 64 | WHERE posting_date <= %s {0} |
Nabin Hait | 0dd7be1 | 2013-08-02 11:45:43 +0530 | [diff] [blame] | 65 | ORDER BY timestamp(posting_date, posting_time) DESC, name DESC |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 66 | """.format(condition), values, as_dict=1) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 67 | |
Nabin Hait | 0dd7be1 | 2013-08-02 11:45:43 +0530 | [diff] [blame] | 68 | sle_map = {} |
| 69 | for sle in stock_ledger_entries: |
Achilles Rasquinha | b4de7e3 | 2018-03-09 12:35:47 +0530 | [diff] [blame] | 70 | if not (sle.item_code, sle.warehouse) in sle_map: |
Nabin Hait | 949a920 | 2017-07-05 13:55:41 +0530 | [diff] [blame] | 71 | sle_map[(sle.item_code, sle.warehouse)] = flt(sle.stock_value) |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 72 | |
Nabin Hait | 625da79 | 2013-09-25 10:32:51 +0530 | [diff] [blame] | 73 | return sum(sle_map.values()) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 74 | |
nick9822 | 6f48d4b | 2017-01-09 12:12:36 +0530 | [diff] [blame] | 75 | @frappe.whitelist() |
Rushabh Mehta | 2712e36 | 2015-02-17 12:50:20 +0530 | [diff] [blame] | 76 | def get_stock_balance(item_code, warehouse, posting_date=None, posting_time=None, with_valuation_rate=False): |
| 77 | """Returns stock balance quantity at given warehouse on given posting date or current date. |
| 78 | |
| 79 | If `with_valuation_rate` is True, will return tuple (qty, rate)""" |
Rushabh Mehta | dc93e0a | 2015-02-20 15:11:56 +0530 | [diff] [blame] | 80 | |
| 81 | from erpnext.stock.stock_ledger import get_previous_sle |
| 82 | |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 83 | if not posting_date: posting_date = nowdate() |
| 84 | if not posting_time: posting_time = nowtime() |
Rushabh Mehta | dc93e0a | 2015-02-20 15:11:56 +0530 | [diff] [blame] | 85 | |
| 86 | last_entry = get_previous_sle({ |
| 87 | "item_code": item_code, |
| 88 | "warehouse":warehouse, |
| 89 | "posting_date": posting_date, |
| 90 | "posting_time": posting_time }) |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 91 | |
Rushabh Mehta | 2712e36 | 2015-02-17 12:50:20 +0530 | [diff] [blame] | 92 | if with_valuation_rate: |
Rushabh Mehta | dc93e0a | 2015-02-20 15:11:56 +0530 | [diff] [blame] | 93 | return (last_entry.qty_after_transaction, last_entry.valuation_rate) if last_entry else (0.0, 0.0) |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 94 | else: |
nick9822 | cc699a9 | 2017-06-20 17:13:29 +0530 | [diff] [blame] | 95 | return last_entry.qty_after_transaction if last_entry else 0.0 |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 96 | |
Nabin Hait | 949a920 | 2017-07-05 13:55:41 +0530 | [diff] [blame] | 97 | @frappe.whitelist() |
| 98 | def get_latest_stock_qty(item_code, warehouse=None): |
| 99 | values, condition = [item_code], "" |
| 100 | if warehouse: |
| 101 | lft, rgt, is_group = frappe.db.get_value("Warehouse", warehouse, ["lft", "rgt", "is_group"]) |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 102 | |
Nabin Hait | 949a920 | 2017-07-05 13:55:41 +0530 | [diff] [blame] | 103 | if is_group: |
| 104 | values.extend([lft, rgt]) |
| 105 | condition += "and exists (\ |
| 106 | select name from `tabWarehouse` wh where wh.name = tabBin.warehouse\ |
| 107 | and wh.lft >= %s and wh.rgt <= %s)" |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 108 | |
Nabin Hait | 949a920 | 2017-07-05 13:55:41 +0530 | [diff] [blame] | 109 | else: |
| 110 | values.append(warehouse) |
| 111 | condition += " AND warehouse = %s" |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 112 | |
Nabin Hait | 949a920 | 2017-07-05 13:55:41 +0530 | [diff] [blame] | 113 | actual_qty = frappe.db.sql("""select sum(actual_qty) from tabBin |
| 114 | where item_code=%s {0}""".format(condition), values)[0][0] |
| 115 | |
| 116 | return actual_qty |
| 117 | |
| 118 | |
Nabin Hait | 47dc318 | 2013-08-06 15:58:16 +0530 | [diff] [blame] | 119 | def get_latest_stock_balance(): |
| 120 | bin_map = {} |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 121 | 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] | 122 | FROM tabBin""", as_dict=1): |
Nabin Hait | 469ee71 | 2013-08-07 12:33:37 +0530 | [diff] [blame] | 123 | 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] | 124 | |
Nabin Hait | 47dc318 | 2013-08-06 15:58:16 +0530 | [diff] [blame] | 125 | return bin_map |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 126 | |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 127 | def get_bin(item_code, warehouse): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 128 | bin = frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse}) |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 129 | if not bin: |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 130 | bin_obj = frappe.get_doc({ |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 131 | "doctype": "Bin", |
| 132 | "item_code": item_code, |
| 133 | "warehouse": warehouse, |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 134 | }) |
Anand Doshi | 6dfd430 | 2015-02-10 14:41:27 +0530 | [diff] [blame] | 135 | bin_obj.flags.ignore_permissions = 1 |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 136 | bin_obj.insert() |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 137 | else: |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 138 | bin_obj = frappe.get_doc('Bin', bin) |
Anand Doshi | 6dfd430 | 2015-02-10 14:41:27 +0530 | [diff] [blame] | 139 | bin_obj.flags.ignore_permissions = True |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 140 | return bin_obj |
| 141 | |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 142 | def update_bin(args, allow_negative_stock=False, via_landed_cost_voucher=False): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 143 | is_stock_item = frappe.db.get_value('Item', args.get("item_code"), 'is_stock_item') |
Rushabh Mehta | 1e8025b | 2015-07-24 15:16:25 +0530 | [diff] [blame] | 144 | if is_stock_item: |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 145 | bin = get_bin(args.get("item_code"), args.get("warehouse")) |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 146 | bin.update_stock(args, allow_negative_stock, via_landed_cost_voucher) |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 147 | return bin |
| 148 | else: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 149 | 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] | 150 | |
Nabin Hait | 5eefff1 | 2015-12-07 10:44:56 +0530 | [diff] [blame] | 151 | @frappe.whitelist() |
Nabin Hait | 7ba092e | 2018-02-01 10:51:27 +0530 | [diff] [blame] | 152 | def get_incoming_rate(args, raise_error_if_no_rate=True): |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 153 | """Get Incoming Rate based on valuation method""" |
rohitwaghchaure | ce8adec | 2017-12-15 12:13:50 +0530 | [diff] [blame] | 154 | from erpnext.stock.stock_ledger import get_previous_sle, get_valuation_rate |
Achilles Rasquinha | 56b2e12 | 2018-02-13 14:42:40 +0530 | [diff] [blame] | 155 | if isinstance(args, string_types): |
Nabin Hait | 41c8cf6 | 2015-12-08 14:50:24 +0530 | [diff] [blame] | 156 | args = json.loads(args) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 157 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 158 | in_rate = 0 |
Anand Doshi | 40a8ae2 | 2014-08-29 16:28:31 +0530 | [diff] [blame] | 159 | if (args.get("serial_no") or "").strip(): |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 160 | in_rate = get_avg_purchase_rate(args.get("serial_no")) |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 161 | else: |
| 162 | valuation_method = get_valuation_method(args.get("item_code")) |
| 163 | previous_sle = get_previous_sle(args) |
| 164 | if valuation_method == 'FIFO': |
rohitwaghchaure | ce8adec | 2017-12-15 12:13:50 +0530 | [diff] [blame] | 165 | if previous_sle: |
| 166 | previous_stock_queue = json.loads(previous_sle.get('stock_queue', '[]') or '[]') |
| 167 | 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] | 168 | elif valuation_method == 'Moving Average': |
| 169 | in_rate = previous_sle.get('valuation_rate') or 0 |
Anand Doshi | 094610d | 2014-04-16 19:56:53 +0530 | [diff] [blame] | 170 | |
rohitwaghchaure | ce8adec | 2017-12-15 12:13:50 +0530 | [diff] [blame] | 171 | if not in_rate: |
| 172 | voucher_no = args.get('voucher_no') or args.get('name') |
rohitwaghchaure | ce8adec | 2017-12-15 12:13:50 +0530 | [diff] [blame] | 173 | in_rate = get_valuation_rate(args.get('item_code'), args.get('warehouse'), |
| 174 | args.get('voucher_type'), voucher_no, args.get('allow_zero_valuation'), |
Nabin Hait | 7ba092e | 2018-02-01 10:51:27 +0530 | [diff] [blame] | 175 | currency=erpnext.get_company_currency(args.get('company')), company=args.get('company'), |
| 176 | raise_error_if_no_rate=True) |
rohitwaghchaure | ce8adec | 2017-12-15 12:13:50 +0530 | [diff] [blame] | 177 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 178 | return in_rate |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 179 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 180 | def get_avg_purchase_rate(serial_nos): |
| 181 | """get average value of serial numbers""" |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 182 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 183 | serial_nos = get_valid_serial_nos(serial_nos) |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 184 | return flt(frappe.db.sql("""select avg(purchase_rate) from `tabSerial No` |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 185 | where name in (%s)""" % ", ".join(["%s"] * len(serial_nos)), |
| 186 | tuple(serial_nos))[0][0]) |
| 187 | |
| 188 | def get_valuation_method(item_code): |
| 189 | """get valuation method from item or default""" |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 190 | val_method = frappe.db.get_value('Item', item_code, 'valuation_method') |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 191 | if not val_method: |
Nabin Hait | 4d74216 | 2014-10-09 19:25:03 +0530 | [diff] [blame] | 192 | val_method = frappe.db.get_value("Stock Settings", None, "valuation_method") or "FIFO" |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 193 | return val_method |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 194 | |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 195 | def get_fifo_rate(previous_stock_queue, qty): |
| 196 | """get FIFO (average) Rate from Queue""" |
| 197 | if qty >= 0: |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 198 | total = sum(f[0] for f in previous_stock_queue) |
Nabin Hait | 38265ef | 2014-10-21 20:23:39 +0530 | [diff] [blame] | 199 | return sum(flt(f[0]) * flt(f[1]) for f in previous_stock_queue) / flt(total) if total else 0.0 |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 200 | else: |
Nabin Hait | 227db76 | 2014-05-08 19:06:01 +0530 | [diff] [blame] | 201 | available_qty_for_outgoing, outgoing_cost = 0, 0 |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 202 | qty_to_pop = abs(qty) |
Nabin Hait | 64d7c4b | 2013-01-17 12:22:59 +0530 | [diff] [blame] | 203 | while qty_to_pop and previous_stock_queue: |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 204 | batch = previous_stock_queue[0] |
Nabin Hait | 8142cd2 | 2015-08-05 18:57:26 +0530 | [diff] [blame] | 205 | if 0 < batch[0] <= qty_to_pop: |
| 206 | # if batch qty > 0 |
| 207 | # not enough or exactly same qty in current batch, clear batch |
| 208 | available_qty_for_outgoing += flt(batch[0]) |
| 209 | outgoing_cost += flt(batch[0]) * flt(batch[1]) |
| 210 | qty_to_pop -= batch[0] |
| 211 | previous_stock_queue.pop(0) |
| 212 | else: |
| 213 | # all from current batch |
| 214 | available_qty_for_outgoing += flt(qty_to_pop) |
| 215 | outgoing_cost += flt(qty_to_pop) * flt(batch[1]) |
| 216 | batch[0] -= qty_to_pop |
| 217 | qty_to_pop = 0 |
Anand Doshi | 094610d | 2014-04-16 19:56:53 +0530 | [diff] [blame] | 218 | |
Nabin Hait | 227db76 | 2014-05-08 19:06:01 +0530 | [diff] [blame] | 219 | return outgoing_cost / available_qty_for_outgoing |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 220 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 221 | def get_valid_serial_nos(sr_nos, qty=0, item_code=''): |
| 222 | """split serial nos, validate and return list of valid serial nos""" |
| 223 | # TODO: remove duplicates in client side |
| 224 | serial_nos = cstr(sr_nos).strip().replace(',', '\n').split('\n') |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 225 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 226 | valid_serial_nos = [] |
| 227 | for val in serial_nos: |
| 228 | if val: |
| 229 | val = val.strip() |
| 230 | if val in valid_serial_nos: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 231 | frappe.throw(_("Serial number {0} entered more than once").format(val)) |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 232 | else: |
| 233 | valid_serial_nos.append(val) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 234 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 235 | if qty and len(valid_serial_nos) != abs(qty): |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 236 | 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] | 237 | |
Rushabh Mehta | 0dbe898 | 2013-02-04 13:56:50 +0530 | [diff] [blame] | 238 | return valid_serial_nos |
Nabin Hait | a72c512 | 2013-03-06 18:50:53 +0530 | [diff] [blame] | 239 | |
Anand Doshi | 373680b | 2013-10-10 16:04:40 +0530 | [diff] [blame] | 240 | def validate_warehouse_company(warehouse, company): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 241 | warehouse_company = frappe.db.get_value("Warehouse", warehouse, "company") |
Anand Doshi | 373680b | 2013-10-10 16:04:40 +0530 | [diff] [blame] | 242 | if warehouse_company and warehouse_company != company: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 243 | frappe.throw(_("Warehouse {0} does not belong to company {1}").format(warehouse, company), |
| 244 | InvalidWarehouseCompany) |
Saurabh | 3d6aecd | 2016-06-20 17:25:45 +0530 | [diff] [blame] | 245 | |
Saurabh | 4d02949 | 2016-06-23 12:44:06 +0530 | [diff] [blame] | 246 | def is_group_warehouse(warehouse): |
Saurabh | 93d68ac | 2016-06-26 22:50:11 +0530 | [diff] [blame] | 247 | if frappe.db.get_value("Warehouse", warehouse, "is_group"): |
Saurabh | 4d02949 | 2016-06-23 12:44:06 +0530 | [diff] [blame] | 248 | frappe.throw(_("Group node warehouse is not allowed to select for transactions")) |
Saif | b4cf72c | 2018-10-18 17:29:47 +0500 | [diff] [blame] | 249 | |
| 250 | def update_included_uom_in_report(columns, result, include_uom, conversion_factors): |
| 251 | if not include_uom or not conversion_factors: |
| 252 | return |
| 253 | |
| 254 | convertible_cols = {} |
| 255 | for col_idx in reversed(range(0, len(columns))): |
| 256 | col = columns[col_idx] |
| 257 | if isinstance(col, dict) and col.get("convertible") in ['rate', 'qty']: |
| 258 | convertible_cols[col_idx] = col['convertible'] |
| 259 | columns.insert(col_idx+1, col.copy()) |
| 260 | columns[col_idx+1]['fieldname'] += "_alt" |
| 261 | if convertible_cols[col_idx] == 'rate': |
| 262 | columns[col_idx+1]['label'] += " (per {})".format(include_uom) |
| 263 | else: |
| 264 | columns[col_idx+1]['label'] += " ({})".format(include_uom) |
| 265 | |
| 266 | for row_idx, row in enumerate(result): |
| 267 | new_row = [] |
| 268 | for col_idx, d in enumerate(row): |
| 269 | new_row.append(d) |
| 270 | if col_idx in convertible_cols: |
| 271 | if conversion_factors[row_idx]: |
| 272 | if convertible_cols[col_idx] == 'rate': |
| 273 | new_row.append(flt(d) * conversion_factors[row_idx]) |
| 274 | else: |
| 275 | new_row.append(flt(d) / conversion_factors[row_idx]) |
| 276 | else: |
| 277 | new_row.append(None) |
| 278 | |
| 279 | result[row_idx] = new_row |