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 | |
| 4 | from __future__ import unicode_literals |
| 5 | |
| 6 | import frappe, random, erpnext |
| 7 | from frappe.utils.make_random import how_many |
| 8 | from frappe.desk import query_report |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 9 | from erpnext.manufacturing.doctype.workstation.workstation import WorkstationHolidayError |
| 10 | from erpnext.manufacturing.doctype.production_order.test_production_order import make_prod_order_test_record |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 11 | |
| 12 | def work(): |
| 13 | frappe.set_user(frappe.db.get_global('demo_manufacturing_user')) |
| 14 | |
| 15 | from erpnext.projects.doctype.timesheet.timesheet import OverlapError |
| 16 | |
| 17 | ppt = frappe.get_doc("Production Planning Tool", "Production Planning Tool") |
| 18 | ppt.company = erpnext.get_default_company() |
| 19 | ppt.use_multi_level_bom = 1 |
| 20 | ppt.get_items_from = "Sales Order" |
| 21 | ppt.purchase_request_for_warehouse = "Stores - WPL" |
| 22 | ppt.run_method("get_open_sales_orders") |
| 23 | ppt.run_method("get_items") |
| 24 | ppt.run_method("raise_production_orders") |
| 25 | ppt.run_method("raise_material_requests") |
| 26 | frappe.db.commit() |
| 27 | |
| 28 | # submit production orders |
| 29 | for pro in frappe.db.get_values("Production Order", {"docstatus": 0}, "name"): |
| 30 | b = frappe.get_doc("Production Order", pro[0]) |
| 31 | b.wip_warehouse = "Work in Progress - WPL" |
| 32 | b.submit() |
| 33 | frappe.db.commit() |
| 34 | |
| 35 | # submit material requests |
| 36 | for pro in frappe.db.get_values("Material Request", {"docstatus": 0}, "name"): |
| 37 | b = frappe.get_doc("Material Request", pro[0]) |
| 38 | b.submit() |
| 39 | frappe.db.commit() |
| 40 | |
| 41 | # stores -> wip |
| 42 | if random.random() < 0.3: |
| 43 | for pro in query_report.run("Open Production Orders")["result"][:how_many("Stock Entry for WIP")]: |
| 44 | make_stock_entry_from_pro(pro[0], "Material Transfer for Manufacture") |
| 45 | |
| 46 | # wip -> fg |
| 47 | if random.random() < 0.3: |
| 48 | for pro in query_report.run("Production Orders in Progress")["result"][:how_many("Stock Entry for FG")]: |
| 49 | make_stock_entry_from_pro(pro[0], "Manufacture") |
| 50 | |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 51 | for bom in frappe.get_all('BOM', fields=['item'], filters = {'with_operations': 1}): |
| 52 | pro_order = make_prod_order_test_record(item=bom.item, qty=2, |
| 53 | source_warehouse="Stores - WPL", wip_warehouse = "Work in Progress - WPL", |
| 54 | fg_warehouse = "Stores - WPL", company = erpnext.get_default_company(), |
| 55 | stock_uom = frappe.db.get_value('Item', bom.item, 'stock_uom'), |
| 56 | planned_start_date = frappe.flags.current_date) |
| 57 | |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 58 | # submit time logs |
| 59 | for timesheet in frappe.get_all("Timesheet", ["name"], {"docstatus": 0, |
| 60 | "production_order": ("!=", ""), "to_time": ("<", frappe.flags.current_date)}): |
| 61 | timesheet = frappe.get_doc("Timesheet", timesheet.name) |
| 62 | try: |
| 63 | timesheet.submit() |
| 64 | frappe.db.commit() |
| 65 | except OverlapError: |
| 66 | pass |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 67 | except WorkstationHolidayError: |
| 68 | pass |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 69 | |
| 70 | def make_stock_entry_from_pro(pro_id, purpose): |
| 71 | from erpnext.manufacturing.doctype.production_order.production_order import make_stock_entry |
| 72 | from erpnext.stock.stock_ledger import NegativeStockError |
| 73 | from erpnext.stock.doctype.stock_entry.stock_entry import IncorrectValuationRateError, \ |
| 74 | DuplicateEntryForProductionOrderError, OperationsNotCompleteError |
| 75 | |
| 76 | try: |
| 77 | st = frappe.get_doc(make_stock_entry(pro_id, purpose)) |
| 78 | st.posting_date = frappe.flags.current_date |
| 79 | st.fiscal_year = str(frappe.flags.current_date.year) |
| 80 | for d in st.get("items"): |
| 81 | d.cost_center = "Main - " + frappe.db.get_value('Company', st.company, 'abbr') |
| 82 | st.insert() |
| 83 | frappe.db.commit() |
| 84 | st.submit() |
| 85 | frappe.db.commit() |
| 86 | except (NegativeStockError, IncorrectValuationRateError, DuplicateEntryForProductionOrderError, |
| 87 | OperationsNotCompleteError): |
| 88 | frappe.db.rollback() |