blob: c91570c8982699ec4da77aeed7bafb98b803fbbc [file] [log] [blame]
Rushabh Mehtacca33b22016-07-08 18:24:46 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
4from __future__ import unicode_literals
5
6import frappe, random, erpnext
7from frappe.utils.make_random import how_many
8from frappe.desk import query_report
9
10def work():
11 frappe.set_user(frappe.db.get_global('demo_manufacturing_user'))
12
13 from erpnext.projects.doctype.timesheet.timesheet import OverlapError
14
15 ppt = frappe.get_doc("Production Planning Tool", "Production Planning Tool")
16 ppt.company = erpnext.get_default_company()
17 ppt.use_multi_level_bom = 1
18 ppt.get_items_from = "Sales Order"
19 ppt.purchase_request_for_warehouse = "Stores - WPL"
20 ppt.run_method("get_open_sales_orders")
21 ppt.run_method("get_items")
22 ppt.run_method("raise_production_orders")
23 ppt.run_method("raise_material_requests")
24 frappe.db.commit()
25
26 # submit production orders
27 for pro in frappe.db.get_values("Production Order", {"docstatus": 0}, "name"):
28 b = frappe.get_doc("Production Order", pro[0])
29 b.wip_warehouse = "Work in Progress - WPL"
30 b.submit()
31 frappe.db.commit()
32
33 # submit material requests
34 for pro in frappe.db.get_values("Material Request", {"docstatus": 0}, "name"):
35 b = frappe.get_doc("Material Request", pro[0])
36 b.submit()
37 frappe.db.commit()
38
39 # stores -> wip
40 if random.random() < 0.3:
41 for pro in query_report.run("Open Production Orders")["result"][:how_many("Stock Entry for WIP")]:
42 make_stock_entry_from_pro(pro[0], "Material Transfer for Manufacture")
43
44 # wip -> fg
45 if random.random() < 0.3:
46 for pro in query_report.run("Production Orders in Progress")["result"][:how_many("Stock Entry for FG")]:
47 make_stock_entry_from_pro(pro[0], "Manufacture")
48
49 # submit time logs
50 for timesheet in frappe.get_all("Timesheet", ["name"], {"docstatus": 0,
51 "production_order": ("!=", ""), "to_time": ("<", frappe.flags.current_date)}):
52 timesheet = frappe.get_doc("Timesheet", timesheet.name)
53 try:
54 timesheet.submit()
55 frappe.db.commit()
56 except OverlapError:
57 pass
58
59def make_stock_entry_from_pro(pro_id, purpose):
60 from erpnext.manufacturing.doctype.production_order.production_order import make_stock_entry
61 from erpnext.stock.stock_ledger import NegativeStockError
62 from erpnext.stock.doctype.stock_entry.stock_entry import IncorrectValuationRateError, \
63 DuplicateEntryForProductionOrderError, OperationsNotCompleteError
64
65 try:
66 st = frappe.get_doc(make_stock_entry(pro_id, purpose))
67 st.posting_date = frappe.flags.current_date
68 st.fiscal_year = str(frappe.flags.current_date.year)
69 for d in st.get("items"):
70 d.cost_center = "Main - " + frappe.db.get_value('Company', st.company, 'abbr')
71 st.insert()
72 frappe.db.commit()
73 st.submit()
74 frappe.db.commit()
75 except (NegativeStockError, IncorrectValuationRateError, DuplicateEntryForProductionOrderError,
76 OperationsNotCompleteError):
77 frappe.db.rollback()