Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 4 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 5 | import random |
Zlash65 | 275ef82 | 2018-10-02 12:55:56 +0530 | [diff] [blame] | 6 | from datetime import timedelta |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 7 | |
| 8 | import frappe |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 9 | from frappe.desk import query_report |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 10 | from frappe.utils.make_random import how_many |
| 11 | |
| 12 | import erpnext |
Ameya Shenoy | 848d34f | 2018-04-12 13:21:16 +0530 | [diff] [blame] | 13 | from erpnext.manufacturing.doctype.work_order.test_work_order import make_wo_order_test_record |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 14 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 15 | |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 16 | def work(): |
Zlash65 | e64dcfc | 2018-10-01 10:48:38 +0530 | [diff] [blame] | 17 | if random.random() < 0.3: return |
| 18 | |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 19 | frappe.set_user(frappe.db.get_global('demo_manufacturing_user')) |
Zlash65 | e64dcfc | 2018-10-01 10:48:38 +0530 | [diff] [blame] | 20 | if not frappe.get_all('Sales Order'): return |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 21 | |
Zlash65 | 143d4da | 2018-09-27 18:49:52 +0530 | [diff] [blame] | 22 | ppt = frappe.new_doc("Production Plan") |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 23 | ppt.company = erpnext.get_default_company() |
Zlash65 | 143d4da | 2018-09-27 18:49:52 +0530 | [diff] [blame] | 24 | # ppt.use_multi_level_bom = 1 #refactored |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 25 | ppt.get_items_from = "Sales Order" |
Zlash65 | 143d4da | 2018-09-27 18:49:52 +0530 | [diff] [blame] | 26 | # ppt.purchase_request_for_warehouse = "Stores - WPL" # refactored |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 27 | ppt.run_method("get_open_sales_orders") |
Zlash65 | e64dcfc | 2018-10-01 10:48:38 +0530 | [diff] [blame] | 28 | if not ppt.get("sales_orders"): return |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 29 | ppt.run_method("get_items") |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 30 | ppt.run_method("raise_material_requests") |
Zlash65 | 143d4da | 2018-09-27 18:49:52 +0530 | [diff] [blame] | 31 | ppt.save() |
| 32 | ppt.submit() |
| 33 | ppt.run_method("raise_work_orders") |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 34 | frappe.db.commit() |
| 35 | |
Zarrar | 13ddc7e | 2018-03-20 12:38:43 +0530 | [diff] [blame] | 36 | # 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 Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 39 | 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 |
Zlash65 | e64dcfc | 2018-10-01 10:48:38 +0530 | [diff] [blame] | 50 | if random.random() < 0.4: |
Zarrar | 13ddc7e | 2018-03-20 12:38:43 +0530 | [diff] [blame] | 51 | for pro in query_report.run("Open Work Orders")["result"][:how_many("Stock Entry for WIP")]: |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 52 | make_stock_entry_from_pro(pro[0], "Material Transfer for Manufacture") |
| 53 | |
| 54 | # wip -> fg |
Zlash65 | e64dcfc | 2018-10-01 10:48:38 +0530 | [diff] [blame] | 55 | if random.random() < 0.4: |
Zarrar | 13ddc7e | 2018-03-20 12:38:43 +0530 | [diff] [blame] | 56 | for pro in query_report.run("Work Orders in Progress")["result"][:how_many("Stock Entry for FG")]: |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 57 | make_stock_entry_from_pro(pro[0], "Manufacture") |
| 58 | |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 59 | for bom in frappe.get_all('BOM', fields=['item'], filters = {'with_operations': 1}): |
Ameya Shenoy | 848d34f | 2018-04-12 13:21:16 +0530 | [diff] [blame] | 60 | pro_order = make_wo_order_test_record(item=bom.item, qty=2, |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 61 | 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 | |
Zlash65 | e64dcfc | 2018-10-01 10:48:38 +0530 | [diff] [blame] | 66 | # submit job card |
| 67 | if random.random() < 0.4: |
| 68 | submit_job_cards() |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 69 | |
| 70 | def make_stock_entry_from_pro(pro_id, purpose): |
Zarrar | 13ddc7e | 2018-03-20 12:38:43 +0530 | [diff] [blame] | 71 | from erpnext.manufacturing.doctype.work_order.work_order import make_stock_entry |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 72 | from erpnext.stock.doctype.stock_entry.stock_entry import ( |
| 73 | DuplicateEntryForWorkOrderError, |
| 74 | IncorrectValuationRateError, |
| 75 | OperationsNotCompleteError, |
| 76 | ) |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 77 | from erpnext.stock.stock_ledger import NegativeStockError |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 78 | |
| 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 Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 84 | d.cost_center = "Main - " + frappe.get_cached_value('Company', st.company, 'abbr') |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 85 | st.insert() |
| 86 | frappe.db.commit() |
| 87 | st.submit() |
| 88 | frappe.db.commit() |
Zarrar | 13ddc7e | 2018-03-20 12:38:43 +0530 | [diff] [blame] | 89 | except (NegativeStockError, IncorrectValuationRateError, DuplicateEntryForWorkOrderError, |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 90 | OperationsNotCompleteError): |
| 91 | frappe.db.rollback() |
Zlash65 | e64dcfc | 2018-10-01 10:48:38 +0530 | [diff] [blame] | 92 | |
| 93 | def 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 Pai | b503896 | 2019-04-27 01:39:50 +0530 | [diff] [blame] | 109 | job_time_log = frappe.new_doc("Job Card Time Log") |
| 110 | job_time_log.from_time = start_date |
Zlash65 | e64dcfc | 2018-10-01 10:48:38 +0530 | [diff] [blame] | 111 | minutes = operation.get("time_in_mins") |
Chinmay Pai | b503896 | 2019-04-27 01:39:50 +0530 | [diff] [blame] | 112 | 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) |
Zlash65 | e64dcfc | 2018-10-01 10:48:38 +0530 | [diff] [blame] | 122 | job.submit() |
Chinmay Pai | b503896 | 2019-04-27 01:39:50 +0530 | [diff] [blame] | 123 | start_date = job_time_log.to_time |