automatic purchase request from production planning tool

This commit is contained in:
Nabin Hait
2013-01-17 12:31:09 +05:30
parent 64d7c4b2d5
commit 35b6d94c06
3 changed files with 84 additions and 56 deletions

View File

@@ -2,9 +2,9 @@
{ {
"owner": "Administrator", "owner": "Administrator",
"docstatus": 0, "docstatus": 0,
"creation": "2012-12-20 18:14:55", "creation": "2013-01-16 14:48:56",
"modified_by": "Administrator", "modified_by": "Administrator",
"modified": "2012-12-24 19:37:34" "modified": "2013-01-17 11:21:46"
}, },
{ {
"istable": 1, "istable": 1,

View File

@@ -16,7 +16,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import webnotes import webnotes
from webnotes.utils import cstr, flt, nowdate from webnotes.utils import cstr, flt, cint, nowdate, add_days
from webnotes.model.doc import addchild, Document from webnotes.model.doc import addchild, Document
from webnotes.model.wrapper import getlist from webnotes.model.wrapper import getlist
from webnotes.model.code import get_obj from webnotes.model.code import get_obj
@@ -275,7 +275,6 @@ class DocType:
and bom_item.item_code = item.name and bom_item.item_code = item.name
group by item_code group by item_code
""", (flt(bom_dict[bom]), bom)) """, (flt(bom_dict[bom]), bom))
self.make_items_dict(fl_bom_items) self.make_items_dict(fl_bom_items)
def make_items_dict(self, item_list): def make_items_dict(self, item_list):
@@ -301,60 +300,82 @@ class DocType:
return item_list return item_list
def raise_purchase_request(self): def raise_purchase_request(self):
def _get_projected_qty(items): """
item_projected_qty = webnotes.conn.sql("""select item_code, sum(projected_qty) Raise Purchase Request if projected qty is less than qty required
from `tabBin` where item_code in (%s) group by item_code""" % Requested qty should be shortage qty considering minimum order qty
(", ".join(["%s"]*len(items)),), tuple(items)) """
if not self.doc.purchase_request_for_warehouse:
return dict(item_projected_qty) webnotes.msgprint("Please enter Warehouse for which Purchase Request will be raised",
raise_exception=1)
item_dict = self.get_raw_materials() bom_dict = self.get_distinct_items_and_boms()[0]
item_projected_qty = _get_projected_qty(item_dict.keys()) self.get_raw_materials(bom_dict)
item_projected_qty = self.get_projected_qty()
from accounts.utils import get_fiscal_year from accounts.utils import get_fiscal_year
fiscal_year = get_fiscal_year(nowdate()) fiscal_year = get_fiscal_year(nowdate())[0]
items_to_be_requested = [] items_to_be_requested = webnotes._dict()
for item in item_dict: for item in self.item_dict:
if flt(item_dict[item][0]) > item_projected_qty[item]: if flt(self.item_dict[item][0]) > item_projected_qty[item]:
# shortage # shortage
requested_qty = flt(item_dict[item][0]) - item_projected_qty[item] requested_qty = flt(self.item_dict[item][0]) - item_projected_qty[item]
# comsider minimum order qty # comsider minimum order qty
requested_qty = requested_qty > flt(item_dict[item][3]) and \ requested_qty = requested_qty > flt(self.item_dict[item][3]) and \
requested_qty or flt(item_dict[item][3]) requested_qty or flt(self.item_dict[item][3])
items_to_be_requested.append({ items_to_be_requested[item] = requested_qty
"item_code": item,
"qty": requested_qty,
"description": item_dict[item][1],
"stock_uom": item_dict[item][2]
})
webnotes.errprint(items_to_be_requested)
self.insert_purchase_request(items_to_be_requested, fiscal_year)
def insert_purchase_request(self, items, fiscal_year): self.insert_purchase_request(items_to_be_requested, fiscal_year)
for item in items:
item_wrapper = webnotes.model_wrapper("Item", args.item_code) def get_projected_qty(self):
pr = [ items = self.item_dict.keys()
{ item_projected_qty = webnotes.conn.sql("""select item_code, sum(projected_qty)
"doctype": "Purchase Request", from `tabBin` where item_code in (%s) group by item_code""" %
"naming_series": "IDT", (", ".join(["%s"]*len(items)),), tuple(items))
"transaction_date": nowdate(),
"status": "Draft", return dict(item_projected_qty)
"company": self.doc.company,
"fiscal_year": fiscal_year, def insert_purchase_request(self, items_to_be_requested, fiscal_year):
"requested_by": webnotes.session.user, purchase_request_list = []
"remark": "Automatically raised from Production Planning Tool" if items_to_be_requested:
}, for item in items_to_be_requested:
{ item_wrapper = webnotes.model_wrapper("Item", item)
"doctype": "Purchase Request Item", pr_doclist = [
"item_code": item.item_code, {
"item_name": item_wrapper.doc.item_name, "doctype": "Purchase Request",
"description": item.description, "__islocal": 1,
"uom": item.stock_uom, "naming_series": "IDT",
"item_group": item_wrapper.doc.item_group, "transaction_date": nowdate(),
"brand": item_wrapper.doc.brand, "status": "Draft",
"qty": item.qty, "company": self.doc.company,
"fiscal_year": fiscal_year,
"requested_by": webnotes.session.user,
} "remark": "Automatically raised from Production Planning Tool"
] },
{
"doctype": "Purchase Request Item",
"__islocal": 1,
"parentfield": "indent_details",
"item_code": item,
"item_name": item_wrapper.doc.item_name,
"description": item_wrapper.doc.description,
"uom": item_wrapper.doc.stock_uom,
"item_group": item_wrapper.doc.item_group,
"brand": item_wrapper.doc.brand,
"qty": items_to_be_requested[item],
"schedule_date": add_days(nowdate(), cint(item_wrapper.doc.lead_time_days)),
"warehouse": self.doc.purchase_request_for_warehouse
}
]
pr_wrapper = webnotes.model_wrapper(pr_doclist)
pr_wrapper.ignore_permissions = 1
pr_wrapper.submit()
purchase_request_list.append(pr_wrapper.doc.name)
if purchase_request_list:
pur_req = ["""<a href="#Form/Purchase Request/%s" target="_blank">%s</a>""" % \
(p, p) for p in purchase_request_list]
webnotes.msgprint("Following Purchase Request created successfully: \n%s" %
"\n".join(pur_req))
else:
webnotes.msgprint("Nothing to request")

View File

@@ -4,7 +4,7 @@
"docstatus": 0, "docstatus": 0,
"creation": "2013-01-16 14:48:56", "creation": "2013-01-16 14:48:56",
"modified_by": "Administrator", "modified_by": "Administrator",
"modified": "2013-01-16 15:46:26" "modified": "2013-01-17 11:39:55"
}, },
{ {
"read_only": 1, "read_only": 1,
@@ -199,7 +199,14 @@
"fieldtype": "Column Break" "fieldtype": "Column Break"
}, },
{ {
"description": "Raise Purchase Request automatically for items which are \"Out of Stock\" considering already requested, already ordered qty and minimum order qty", "doctype": "DocField",
"label": "Purchase Request For Warehouse",
"fieldname": "purchase_request_for_warehouse",
"fieldtype": "Link",
"options": "Warehouse"
},
{
"description": "Items to be requested which are \"Out of Stock\" considering all warehouses based on projected qty and minimum order qty",
"doctype": "DocField", "doctype": "DocField",
"label": "Raise Purchase Request", "label": "Raise Purchase Request",
"fieldname": "raise_purchase_request", "fieldname": "raise_purchase_request",