From 9d5566634c100de645a5cab28a17072ad4bb1c06 Mon Sep 17 00:00:00 2001 From: Thura Hlaing Date: Mon, 27 Jan 2014 16:54:02 +0630 Subject: [PATCH] changed stock_frozen_upto to stock_frozen_upto_days, and added validation for it in stock ledger --- .../stock_ledger_entry/stock_ledger_entry.py | 49 ++++++++++++------- .../doctype/stock_settings/stock_settings.py | 4 +- .../doctype/stock_settings/stock_settings.txt | 6 +-- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/stock/doctype/stock_ledger_entry/stock_ledger_entry.py b/stock/doctype/stock_ledger_entry/stock_ledger_entry.py index f059451099..4828ca42bb 100644 --- a/stock/doctype/stock_ledger_entry/stock_ledger_entry.py +++ b/stock/doctype/stock_ledger_entry/stock_ledger_entry.py @@ -1,3 +1,4 @@ + # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors # License: GNU General Public License v3. See license.txt @@ -6,6 +7,7 @@ import webnotes from webnotes import msgprint from webnotes.utils import flt, getdate from webnotes.model.controller import DocListController +from datetime import timedelta, date class DocType(DocListController): def __init__(self, doc, doclist=[]): @@ -19,30 +21,30 @@ class DocType(DocListController): validate_warehouse_user(self.doc.warehouse) validate_warehouse_company(self.doc.warehouse, self.doc.company) self.scrub_posting_time() - + from accounts.utils import validate_fiscal_year validate_fiscal_year(self.doc.posting_date, self.doc.fiscal_year, self.meta.get_label("posting_date")) - + def on_submit(self): self.check_stock_frozen_date() self.actual_amt_check() - + from stock.doctype.serial_no.serial_no import process_serial_no process_serial_no(self.doc) - + #check for item quantity available in stock def actual_amt_check(self): if self.doc.batch_no: - batch_bal_after_transaction = flt(webnotes.conn.sql("""select sum(actual_qty) - from `tabStock Ledger Entry` - where warehouse=%s and item_code=%s and batch_no=%s""", + batch_bal_after_transaction = flt(webnotes.conn.sql("""select sum(actual_qty) + from `tabStock Ledger Entry` + where warehouse=%s and item_code=%s and batch_no=%s""", (self.doc.warehouse, self.doc.item_code, self.doc.batch_no))[0][0]) - + if batch_bal_after_transaction < 0: self.doc.fields.update({ 'batch_bal': batch_bal_after_transaction - self.doc.actual_qty }) - + webnotes.throw("""Not enough quantity (requested: %(actual_qty)s, \ current: %(batch_bal)s in Batch %(batch_no)s for Item \ %(item_code)s at Warehouse %(warehouse)s \ @@ -60,27 +62,27 @@ class DocType(DocListController): msgprint("Warehouse: '%s' does not exist in the system. Please check." % self.doc.fields.get(k), raise_exception = 1) def validate_item(self): - item_det = webnotes.conn.sql("""select name, has_batch_no, docstatus, - is_stock_item, has_serial_no, serial_no_series - from tabItem where name=%s""", + item_det = webnotes.conn.sql("""select name, has_batch_no, docstatus, + is_stock_item, has_serial_no, serial_no_series + from tabItem where name=%s""", self.doc.item_code, as_dict=True)[0] if item_det.is_stock_item != 'Yes': webnotes.throw("""Item: "%s" is not a Stock Item.""" % self.doc.item_code) - + # check if batch number is required if item_det.has_batch_no =='Yes' and self.doc.voucher_type != 'Stock Reconciliation': if not self.doc.batch_no: webnotes.throw("Batch number is mandatory for Item '%s'" % self.doc.item_code) - + # check if batch belongs to item - if not webnotes.conn.sql("""select name from `tabBatch` + if not webnotes.conn.sql("""select name from `tabBatch` where item='%s' and name ='%s' and docstatus != 2""" % (self.doc.item_code, self.doc.batch_no)): webnotes.throw("'%s' is not a valid Batch Number for Item '%s'" % (self.doc.batch_no, self.doc.item_code)) - + if not self.doc.stock_uom: self.doc.stock_uom = item_det.stock_uom - + def check_stock_frozen_date(self): stock_frozen_upto = webnotes.conn.get_value('Stock Settings', None, 'stock_frozen_upto') or '' if stock_frozen_upto: @@ -88,13 +90,22 @@ class DocType(DocListController): if getdate(self.doc.posting_date) <= getdate(stock_frozen_upto) and not stock_auth_role in webnotes.user.get_roles(): msgprint("You are not authorized to do / modify back dated stock entries before %s" % getdate(stock_frozen_upto).strftime('%d-%m-%Y'), raise_exception=1) + stock_frozen_upto_days = webnotes.conn.get_value('Stock Settings', None, 'stock_frozen_upto_days') or 0 + if stock_frozen_upto_days: + stock_auth_role = webnotes.conn.get_value('Stock Settings', None,'stock_auth_role') + posting_date = getdate(self.doc.posting_date) + frozen_days = timedelta(days=stock_frozen_upto_days) + if posting_date + frozen_days <= date.today() and not stock_auth_role in webnotes.user.get_roles(): + msgprint("You are not authorized to do / modify back dated stock entries %d ago" %stock_frozen_upto_days, raise_exception=1) + + def scrub_posting_time(self): if not self.doc.posting_time or self.doc.posting_time == '00:0': self.doc.posting_time = '00:00' def on_doctype_update(): - if not webnotes.conn.sql("""show index from `tabStock Ledger Entry` + if not webnotes.conn.sql("""show index from `tabStock Ledger Entry` where Key_name="posting_sort_index" """): webnotes.conn.commit() - webnotes.conn.sql("""alter table `tabStock Ledger Entry` + webnotes.conn.sql("""alter table `tabStock Ledger Entry` add index posting_sort_index(posting_date, posting_time, name)""") \ No newline at end of file diff --git a/stock/doctype/stock_settings/stock_settings.py b/stock/doctype/stock_settings/stock_settings.py index a98ed407e7..a061aaa493 100644 --- a/stock/doctype/stock_settings/stock_settings.py +++ b/stock/doctype/stock_settings/stock_settings.py @@ -21,7 +21,7 @@ class DocType: self.doc.get("item_naming_by")=="Naming Series", hide_name_field=True) stock_frozen_limit = 356 - submitted_stock_frozen = self.doc.fields.get("stock_frozen_upto") + submitted_stock_frozen = self.doc.fields.get("stock_frozen_upto_days") if submitted_stock_frozen > stock_frozen_limit: - self.doc.fields["stock_frozen_upto"] = stock_frozen_limit + self.doc.fields["stock_frozen_upto_days"] = stock_frozen_limit webnotes.msgprint (_("Stocks cannot be freezed for days larger than %d.") %stock_frozen_limit) diff --git a/stock/doctype/stock_settings/stock_settings.txt b/stock/doctype/stock_settings/stock_settings.txt index 59710ffc7c..ca1229619b 100644 --- a/stock/doctype/stock_settings/stock_settings.txt +++ b/stock/doctype/stock_settings/stock_settings.txt @@ -2,7 +2,7 @@ { "creation": "2013-06-24 16:37:54", "docstatus": 0, - "modified": "2014-01-27 13:29:56", + "modified": "2014-01-27 17:29:56", "modified_by": "Administrator", "owner": "Administrator" }, @@ -117,9 +117,9 @@ }, { "doctype": "DocField", - "fieldname": "stock_frozen_upto", + "fieldname": "stock_frozen_upto_days", "fieldtype": "Int", - "label": "Stock Frozen Upto" + "label": "Stock Frozen Upto [Days Ago]" }, { "doctype": "DocField",