brotherton-erpnext/erpnext/demo/user/manufacturing.py

120 lines
4.5 KiB
Python
Raw Normal View History

2016-07-08 12:54:46 +00:00
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt
from __future__ import unicode_literals
import frappe, random, erpnext
2018-10-02 07:25:56 +00:00
from datetime import timedelta
2016-07-08 12:54:46 +00:00
from frappe.utils.make_random import how_many
from frappe.desk import query_report
2016-07-13 10:33:05 +00:00
from erpnext.manufacturing.doctype.workstation.workstation import WorkstationHolidayError
from erpnext.manufacturing.doctype.work_order.test_work_order import make_wo_order_test_record
2016-07-08 12:54:46 +00:00
def work():
2018-10-01 05:18:38 +00:00
if random.random() < 0.3: return
2016-07-08 12:54:46 +00:00
frappe.set_user(frappe.db.get_global('demo_manufacturing_user'))
2018-10-01 05:18:38 +00:00
if not frappe.get_all('Sales Order'): return
2016-07-08 12:54:46 +00:00
from erpnext.projects.doctype.timesheet.timesheet import OverlapError
2018-09-27 13:19:52 +00:00
ppt = frappe.new_doc("Production Plan")
2016-07-08 12:54:46 +00:00
ppt.company = erpnext.get_default_company()
2018-09-27 13:19:52 +00:00
# ppt.use_multi_level_bom = 1 #refactored
2016-07-08 12:54:46 +00:00
ppt.get_items_from = "Sales Order"
2018-09-27 13:19:52 +00:00
# ppt.purchase_request_for_warehouse = "Stores - WPL" # refactored
2016-07-08 12:54:46 +00:00
ppt.run_method("get_open_sales_orders")
2018-10-01 05:18:38 +00:00
if not ppt.get("sales_orders"): return
2016-07-08 12:54:46 +00:00
ppt.run_method("get_items")
ppt.run_method("raise_material_requests")
2018-09-27 13:19:52 +00:00
ppt.save()
ppt.submit()
ppt.run_method("raise_work_orders")
2016-07-08 12:54:46 +00:00
frappe.db.commit()
# submit work orders
for pro in frappe.db.get_values("Work Order", {"docstatus": 0}, "name"):
b = frappe.get_doc("Work Order", pro[0])
2016-07-08 12:54:46 +00:00
b.wip_warehouse = "Work in Progress - WPL"
b.submit()
frappe.db.commit()
# submit material requests
for pro in frappe.db.get_values("Material Request", {"docstatus": 0}, "name"):
b = frappe.get_doc("Material Request", pro[0])
b.submit()
frappe.db.commit()
# stores -> wip
2018-10-01 05:18:38 +00:00
if random.random() < 0.4:
for pro in query_report.run("Open Work Orders")["result"][:how_many("Stock Entry for WIP")]:
2016-07-08 12:54:46 +00:00
make_stock_entry_from_pro(pro[0], "Material Transfer for Manufacture")
# wip -> fg
2018-10-01 05:18:38 +00:00
if random.random() < 0.4:
for pro in query_report.run("Work Orders in Progress")["result"][:how_many("Stock Entry for FG")]:
2016-07-08 12:54:46 +00:00
make_stock_entry_from_pro(pro[0], "Manufacture")
2016-07-13 10:33:05 +00:00
for bom in frappe.get_all('BOM', fields=['item'], filters = {'with_operations': 1}):
pro_order = make_wo_order_test_record(item=bom.item, qty=2,
2016-07-13 10:33:05 +00:00
source_warehouse="Stores - WPL", wip_warehouse = "Work in Progress - WPL",
fg_warehouse = "Stores - WPL", company = erpnext.get_default_company(),
stock_uom = frappe.db.get_value('Item', bom.item, 'stock_uom'),
planned_start_date = frappe.flags.current_date)
2018-10-01 05:18:38 +00:00
# submit job card
if random.random() < 0.4:
submit_job_cards()
2016-07-08 12:54:46 +00:00
def make_stock_entry_from_pro(pro_id, purpose):
from erpnext.manufacturing.doctype.work_order.work_order import make_stock_entry
2016-07-08 12:54:46 +00:00
from erpnext.stock.stock_ledger import NegativeStockError
from erpnext.stock.doctype.stock_entry.stock_entry import IncorrectValuationRateError, \
DuplicateEntryForWorkOrderError, OperationsNotCompleteError
2016-07-08 12:54:46 +00:00
try:
st = frappe.get_doc(make_stock_entry(pro_id, purpose))
st.posting_date = frappe.flags.current_date
st.fiscal_year = str(frappe.flags.current_date.year)
for d in st.get("items"):
2018-08-08 11:07:31 +00:00
d.cost_center = "Main - " + frappe.get_cached_value('Company', st.company, 'abbr')
2016-07-08 12:54:46 +00:00
st.insert()
frappe.db.commit()
st.submit()
frappe.db.commit()
except (NegativeStockError, IncorrectValuationRateError, DuplicateEntryForWorkOrderError,
2016-07-08 12:54:46 +00:00
OperationsNotCompleteError):
frappe.db.rollback()
2018-10-01 05:18:38 +00:00
def submit_job_cards():
work_orders = frappe.get_all("Work Order", ["name", "creation"], {"docstatus": 1, "status": "Not Started"})
work_order = random.choice(work_orders)
# for work_order in work_orders:
start_date = work_order.creation
work_order = frappe.get_doc("Work Order", work_order.name)
job = frappe.get_all("Job Card", ["name", "operation", "work_order"],
{"docstatus": 0, "work_order": work_order.name})
if not job: return
job_map = {}
for d in job:
job_map[d.operation] = frappe.get_doc("Job Card", d.name)
for operation in work_order.operations:
job = job_map[operation.operation]
job_time_log = frappe.new_doc("Job Card Time Log")
job_time_log.from_time = start_date
2018-10-01 05:18:38 +00:00
minutes = operation.get("time_in_mins")
job_time_log.time_in_mins = random.randint(int(minutes/2), minutes)
job_time_log.to_time = job_time_log.from_time + \
timedelta(minutes=job_time_log.time_in_mins)
job_time_log.parent = job.name
job_time_log.parenttype = 'Job Card'
job_time_log.parentfield = 'time_logs'
job_time_log.completed_qty = work_order.qty
job_time_log.save(ignore_permissions=True)
job.time_logs.append(job_time_log)
job.save(ignore_permissions=True)
2018-10-01 05:18:38 +00:00
job.submit()
start_date = job_time_log.to_time