blob: 6b6177617190a0df3e59abe82934f0cd75db1244 [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
Rushabh Mehtacca33b22016-07-08 18:24:46 +05304
Chillar Anand915b3432021-09-02 16:44:59 +05305import random
Zlash65275ef822018-10-02 12:55:56 +05306from datetime import timedelta
Chillar Anand915b3432021-09-02 16:44:59 +05307
8import frappe
Rushabh Mehtacca33b22016-07-08 18:24:46 +05309from frappe.desk import query_report
Chillar Anand915b3432021-09-02 16:44:59 +053010from frappe.utils.make_random import how_many
11
12import erpnext
Ameya Shenoy848d34f2018-04-12 13:21:16 +053013from erpnext.manufacturing.doctype.work_order.test_work_order import make_wo_order_test_record
Rushabh Mehtacca33b22016-07-08 18:24:46 +053014
Chillar Anand915b3432021-09-02 16:44:59 +053015
Rushabh Mehtacca33b22016-07-08 18:24:46 +053016def work():
Zlash65e64dcfc2018-10-01 10:48:38 +053017 if random.random() < 0.3: return
18
Rushabh Mehtacca33b22016-07-08 18:24:46 +053019 frappe.set_user(frappe.db.get_global('demo_manufacturing_user'))
Zlash65e64dcfc2018-10-01 10:48:38 +053020 if not frappe.get_all('Sales Order'): return
Rushabh Mehtacca33b22016-07-08 18:24:46 +053021
Zlash65143d4da2018-09-27 18:49:52 +053022 ppt = frappe.new_doc("Production Plan")
Rushabh Mehtacca33b22016-07-08 18:24:46 +053023 ppt.company = erpnext.get_default_company()
Zlash65143d4da2018-09-27 18:49:52 +053024 # ppt.use_multi_level_bom = 1 #refactored
Rushabh Mehtacca33b22016-07-08 18:24:46 +053025 ppt.get_items_from = "Sales Order"
Zlash65143d4da2018-09-27 18:49:52 +053026 # ppt.purchase_request_for_warehouse = "Stores - WPL" # refactored
Rushabh Mehtacca33b22016-07-08 18:24:46 +053027 ppt.run_method("get_open_sales_orders")
Zlash65e64dcfc2018-10-01 10:48:38 +053028 if not ppt.get("sales_orders"): return
Rushabh Mehtacca33b22016-07-08 18:24:46 +053029 ppt.run_method("get_items")
Rushabh Mehtacca33b22016-07-08 18:24:46 +053030 ppt.run_method("raise_material_requests")
Zlash65143d4da2018-09-27 18:49:52 +053031 ppt.save()
32 ppt.submit()
33 ppt.run_method("raise_work_orders")
Rushabh Mehtacca33b22016-07-08 18:24:46 +053034 frappe.db.commit()
35
Zarrar13ddc7e2018-03-20 12:38:43 +053036 # submit work orders
37 for pro in frappe.db.get_values("Work Order", {"docstatus": 0}, "name"):
38 b = frappe.get_doc("Work Order", pro[0])
Rushabh Mehtacca33b22016-07-08 18:24:46 +053039 b.wip_warehouse = "Work in Progress - WPL"
40 b.submit()
41 frappe.db.commit()
42
43 # submit material requests
44 for pro in frappe.db.get_values("Material Request", {"docstatus": 0}, "name"):
45 b = frappe.get_doc("Material Request", pro[0])
46 b.submit()
47 frappe.db.commit()
48
49 # stores -> wip
Zlash65e64dcfc2018-10-01 10:48:38 +053050 if random.random() < 0.4:
Zarrar13ddc7e2018-03-20 12:38:43 +053051 for pro in query_report.run("Open Work Orders")["result"][:how_many("Stock Entry for WIP")]:
Rushabh Mehtacca33b22016-07-08 18:24:46 +053052 make_stock_entry_from_pro(pro[0], "Material Transfer for Manufacture")
53
54 # wip -> fg
Zlash65e64dcfc2018-10-01 10:48:38 +053055 if random.random() < 0.4:
Zarrar13ddc7e2018-03-20 12:38:43 +053056 for pro in query_report.run("Work Orders in Progress")["result"][:how_many("Stock Entry for FG")]:
Rushabh Mehtacca33b22016-07-08 18:24:46 +053057 make_stock_entry_from_pro(pro[0], "Manufacture")
58
Rohit Waghchaure8002d472016-07-13 16:03:05 +053059 for bom in frappe.get_all('BOM', fields=['item'], filters = {'with_operations': 1}):
Ameya Shenoy848d34f2018-04-12 13:21:16 +053060 pro_order = make_wo_order_test_record(item=bom.item, qty=2,
Rohit Waghchaure8002d472016-07-13 16:03:05 +053061 source_warehouse="Stores - WPL", wip_warehouse = "Work in Progress - WPL",
62 fg_warehouse = "Stores - WPL", company = erpnext.get_default_company(),
63 stock_uom = frappe.db.get_value('Item', bom.item, 'stock_uom'),
64 planned_start_date = frappe.flags.current_date)
65
Zlash65e64dcfc2018-10-01 10:48:38 +053066 # submit job card
67 if random.random() < 0.4:
68 submit_job_cards()
Rushabh Mehtacca33b22016-07-08 18:24:46 +053069
70def make_stock_entry_from_pro(pro_id, purpose):
Zarrar13ddc7e2018-03-20 12:38:43 +053071 from erpnext.manufacturing.doctype.work_order.work_order import make_stock_entry
Chillar Anand915b3432021-09-02 16:44:59 +053072 from erpnext.stock.doctype.stock_entry.stock_entry import (
73 DuplicateEntryForWorkOrderError,
74 IncorrectValuationRateError,
75 OperationsNotCompleteError,
76 )
Rushabh Mehtacca33b22016-07-08 18:24:46 +053077 from erpnext.stock.stock_ledger import NegativeStockError
Rushabh Mehtacca33b22016-07-08 18:24:46 +053078
79 try:
80 st = frappe.get_doc(make_stock_entry(pro_id, purpose))
81 st.posting_date = frappe.flags.current_date
82 st.fiscal_year = str(frappe.flags.current_date.year)
83 for d in st.get("items"):
Rushabh Mehta708e47a2018-08-08 16:37:31 +053084 d.cost_center = "Main - " + frappe.get_cached_value('Company', st.company, 'abbr')
Rushabh Mehtacca33b22016-07-08 18:24:46 +053085 st.insert()
86 frappe.db.commit()
87 st.submit()
88 frappe.db.commit()
Zarrar13ddc7e2018-03-20 12:38:43 +053089 except (NegativeStockError, IncorrectValuationRateError, DuplicateEntryForWorkOrderError,
Rushabh Mehtacca33b22016-07-08 18:24:46 +053090 OperationsNotCompleteError):
91 frappe.db.rollback()
Zlash65e64dcfc2018-10-01 10:48:38 +053092
93def submit_job_cards():
94 work_orders = frappe.get_all("Work Order", ["name", "creation"], {"docstatus": 1, "status": "Not Started"})
95 work_order = random.choice(work_orders)
96 # for work_order in work_orders:
97 start_date = work_order.creation
98 work_order = frappe.get_doc("Work Order", work_order.name)
99 job = frappe.get_all("Job Card", ["name", "operation", "work_order"],
100 {"docstatus": 0, "work_order": work_order.name})
101
102 if not job: return
103 job_map = {}
104 for d in job:
105 job_map[d.operation] = frappe.get_doc("Job Card", d.name)
106
107 for operation in work_order.operations:
108 job = job_map[operation.operation]
Chinmay Paib5038962019-04-27 01:39:50 +0530109 job_time_log = frappe.new_doc("Job Card Time Log")
110 job_time_log.from_time = start_date
Zlash65e64dcfc2018-10-01 10:48:38 +0530111 minutes = operation.get("time_in_mins")
Chinmay Paib5038962019-04-27 01:39:50 +0530112 job_time_log.time_in_mins = random.randint(int(minutes/2), minutes)
113 job_time_log.to_time = job_time_log.from_time + \
114 timedelta(minutes=job_time_log.time_in_mins)
115 job_time_log.parent = job.name
116 job_time_log.parenttype = 'Job Card'
117 job_time_log.parentfield = 'time_logs'
118 job_time_log.completed_qty = work_order.qty
119 job_time_log.save(ignore_permissions=True)
120 job.time_logs.append(job_time_log)
121 job.save(ignore_permissions=True)
Zlash65e64dcfc2018-10-01 10:48:38 +0530122 job.submit()
Chinmay Paib5038962019-04-27 01:39:50 +0530123 start_date = job_time_log.to_time