Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 1 | # ERPNext - web based ERP (http://erpnext.com) |
| 2 | # Copyright (C) 2012 Web Notes Technologies Pvt Ltd |
| 3 | # |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | import webnotes |
| 18 | from webnotes import msgprint, _ |
| 19 | import json |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 20 | from webnotes.utils import flt, cstr |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 21 | |
| 22 | def validate_end_of_life(item_code, end_of_life=None, verbose=1): |
| 23 | if not end_of_life: |
| 24 | end_of_life = webnotes.conn.get_value("Item", item_code, "end_of_life") |
| 25 | |
| 26 | from webnotes.utils import getdate, now_datetime, formatdate |
| 27 | if end_of_life and getdate(end_of_life) > now_datetime().date(): |
| 28 | msg = (_("Item") + " %(item_code)s: " + _("reached its end of life on") + \ |
| 29 | " %(date)s. " + _("Please check") + ": %(end_of_life_label)s " + \ |
| 30 | "in Item master") % { |
| 31 | "item_code": item_code, |
| 32 | "date": formatdate(end_of_life), |
| 33 | "end_of_life_label": webnotes.get_label("Item", "end_of_life") |
| 34 | } |
| 35 | |
| 36 | _msgprint(msg, verbose) |
| 37 | |
| 38 | def validate_is_stock_item(item_code, is_stock_item=None, verbose=1): |
| 39 | if not is_stock_item: |
| 40 | is_stock_item = webnotes.conn.get_value("Item", item_code, "is_stock_item") |
| 41 | |
| 42 | if is_stock_item != "Yes": |
| 43 | msg = (_("Item") + " %(item_code)s: " + _("is not a Stock Item")) % { |
| 44 | "item_code": item_code, |
| 45 | } |
| 46 | |
| 47 | _msgprint(msg, verbose) |
| 48 | |
| 49 | def validate_cancelled_item(item_code, docstatus=None, verbose=1): |
| 50 | if docstatus is None: |
| 51 | docstatus = webnotes.conn.get_value("Item", item_code, "docstatus") |
| 52 | |
| 53 | if docstatus == 2: |
| 54 | msg = (_("Item") + " %(item_code)s: " + _("is a cancelled Item")) % { |
| 55 | "item_code": item_code, |
| 56 | } |
| 57 | |
| 58 | _msgprint(msg, verbose) |
| 59 | |
| 60 | def _msgprint(msg, verbose): |
| 61 | if verbose: |
| 62 | msgprint(msg, raise_exception=True) |
| 63 | else: |
| 64 | raise webnotes.ValidationError, msg |
| 65 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 66 | def get_incoming_rate(args): |
| 67 | """Get Incoming Rate based on valuation method""" |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 68 | from stock.stock_ledger import get_previous_sle |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 69 | |
| 70 | in_rate = 0 |
| 71 | if args.get("serial_no"): |
| 72 | in_rate = get_avg_purchase_rate(args.get("serial_no")) |
| 73 | elif args.get("bom_no"): |
| 74 | result = webnotes.conn.sql("""select ifnull(total_cost, 0) / ifnull(quantity, 1) |
| 75 | from `tabBOM` where name = %s and docstatus=1 and is_active=1""", args.get("bom_no")) |
| 76 | in_rate = result and flt(result[0][0]) or 0 |
| 77 | else: |
| 78 | valuation_method = get_valuation_method(args.get("item_code")) |
| 79 | previous_sle = get_previous_sle(args) |
| 80 | if valuation_method == 'FIFO': |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 81 | if not previous_sle: |
| 82 | return 0.0 |
| 83 | previous_stock_queue = json.loads(previous_sle.get('stock_queue', '[]')) |
| 84 | in_rate = previous_stock_queue and \ |
Nabin Hait | 6b1f21d | 2013-01-16 17:17:17 +0530 | [diff] [blame] | 85 | get_fifo_rate(previous_stock_queue, args.get("qty") or 0) or 0 |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 86 | elif valuation_method == 'Moving Average': |
| 87 | in_rate = previous_sle.get('valuation_rate') or 0 |
| 88 | return in_rate |
| 89 | |
| 90 | def get_avg_purchase_rate(serial_nos): |
| 91 | """get average value of serial numbers""" |
| 92 | |
| 93 | serial_nos = get_valid_serial_nos(serial_nos) |
| 94 | return flt(webnotes.conn.sql("""select avg(ifnull(purchase_rate, 0)) from `tabSerial No` |
| 95 | where name in (%s)""" % ", ".join(["%s"] * len(serial_nos)), |
| 96 | tuple(serial_nos))[0][0]) |
| 97 | |
| 98 | def get_valuation_method(item_code): |
| 99 | """get valuation method from item or default""" |
| 100 | val_method = webnotes.conn.get_value('Item', item_code, 'valuation_method') |
| 101 | if not val_method: |
| 102 | from webnotes.utils import get_defaults |
| 103 | val_method = get_defaults().get('valuation_method', 'FIFO') |
| 104 | return val_method |
| 105 | |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 106 | def get_fifo_rate(previous_stock_queue, qty): |
| 107 | """get FIFO (average) Rate from Queue""" |
| 108 | if qty >= 0: |
| 109 | total = sum(f[0] for f in previous_stock_queue) |
| 110 | return total and sum(f[0] * f[1] for f in previous_stock_queue) / flt(total) or 0.0 |
| 111 | else: |
| 112 | outgoing_cost = 0 |
| 113 | qty_to_pop = abs(qty) |
Nabin Hait | 64d7c4b | 2013-01-17 12:22:59 +0530 | [diff] [blame] | 114 | while qty_to_pop and previous_stock_queue: |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 115 | batch = previous_stock_queue[0] |
| 116 | if 0 < batch[0] <= qty_to_pop: |
| 117 | # if batch qty > 0 |
| 118 | # not enough or exactly same qty in current batch, clear batch |
| 119 | outgoing_cost += flt(batch[0]) * flt(batch[1]) |
| 120 | qty_to_pop -= batch[0] |
| 121 | previous_stock_queue.pop(0) |
| 122 | else: |
| 123 | # all from current batch |
| 124 | outgoing_cost += flt(qty_to_pop) * flt(batch[1]) |
| 125 | batch[0] -= qty_to_pop |
| 126 | qty_to_pop = 0 |
Nabin Hait | 64d7c4b | 2013-01-17 12:22:59 +0530 | [diff] [blame] | 127 | # if queue gets blank and qty_to_pop remaining, get average rate of full queue |
| 128 | return outgoing_cost / abs(qty) - qty_to_pop |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 129 | |
| 130 | def get_valid_serial_nos(sr_nos, qty=0, item_code=''): |
| 131 | """split serial nos, validate and return list of valid serial nos""" |
| 132 | # TODO: remove duplicates in client side |
| 133 | serial_nos = cstr(sr_nos).strip().replace(',', '\n').split('\n') |
| 134 | |
| 135 | valid_serial_nos = [] |
| 136 | for val in serial_nos: |
| 137 | if val: |
| 138 | val = val.strip() |
| 139 | if val in valid_serial_nos: |
| 140 | msgprint("You have entered duplicate serial no: '%s'" % val, raise_exception=1) |
| 141 | else: |
| 142 | valid_serial_nos.append(val) |
| 143 | |
| 144 | if qty and len(valid_serial_nos) != abs(qty): |
| 145 | msgprint("Please enter serial nos for " |
| 146 | + cstr(abs(qty)) + " quantity against item code: " + item_code, |
| 147 | raise_exception=1) |
| 148 | |
| 149 | return valid_serial_nos |