blob: a65406beb05c8dadf495eaa2dbd8bfbda683f6e6 [file] [log] [blame]
Nabin Hait9d0f6362013-01-07 18:51:11 +05301# 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
17import webnotes
18from webnotes import msgprint, _
19import json
Anand Doshi1b531862013-01-10 19:29:51 +053020from webnotes.utils import flt, cstr
Nabin Hait9d0f6362013-01-07 18:51:11 +053021
22def 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
38def 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
49def 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
60def _msgprint(msg, verbose):
61 if verbose:
62 msgprint(msg, raise_exception=True)
63 else:
64 raise webnotes.ValidationError, msg
65
Nabin Hait9d0f6362013-01-07 18:51:11 +053066def get_incoming_rate(args):
67 """Get Incoming Rate based on valuation method"""
Anand Doshi1b531862013-01-10 19:29:51 +053068 from stock.stock_ledger import get_previous_sle
Nabin Hait9d0f6362013-01-07 18:51:11 +053069
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':
81 # get rate based on the last item value?
82 if args.get("qty"):
83 if not previous_sle:
84 return 0.0
85 stock_queue = json.loads(previous_sle.get('stock_queue', '[]'))
86 in_rate = stock_queue and get_fifo_rate(stock_queue) or 0
87 elif valuation_method == 'Moving Average':
88 in_rate = previous_sle.get('valuation_rate') or 0
89 return in_rate
90
91def get_avg_purchase_rate(serial_nos):
92 """get average value of serial numbers"""
93
94 serial_nos = get_valid_serial_nos(serial_nos)
95 return flt(webnotes.conn.sql("""select avg(ifnull(purchase_rate, 0)) from `tabSerial No`
96 where name in (%s)""" % ", ".join(["%s"] * len(serial_nos)),
97 tuple(serial_nos))[0][0])
98
99def get_valuation_method(item_code):
100 """get valuation method from item or default"""
101 val_method = webnotes.conn.get_value('Item', item_code, 'valuation_method')
102 if not val_method:
103 from webnotes.utils import get_defaults
104 val_method = get_defaults().get('valuation_method', 'FIFO')
105 return val_method
106
107def get_fifo_rate(stock_queue):
108 """get FIFO (average) Rate from Stack"""
109 if not stock_queue:
110 return 0.0
111
112 total = sum(f[0] for f in stock_queue)
113 return total and sum(f[0] * f[1] for f in stock_queue) / flt(total) or 0.0
114
115def get_valid_serial_nos(sr_nos, qty=0, item_code=''):
116 """split serial nos, validate and return list of valid serial nos"""
117 # TODO: remove duplicates in client side
118 serial_nos = cstr(sr_nos).strip().replace(',', '\n').split('\n')
119
120 valid_serial_nos = []
121 for val in serial_nos:
122 if val:
123 val = val.strip()
124 if val in valid_serial_nos:
125 msgprint("You have entered duplicate serial no: '%s'" % val, raise_exception=1)
126 else:
127 valid_serial_nos.append(val)
128
129 if qty and len(valid_serial_nos) != abs(qty):
130 msgprint("Please enter serial nos for "
131 + cstr(abs(qty)) + " quantity against item code: " + item_code,
132 raise_exception=1)
133
134 return valid_serial_nos