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

91 lines
3.4 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
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():
frappe.set_user(frappe.db.get_global('demo_manufacturing_user'))
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")
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
if random.random() < 0.3:
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
if random.random() < 0.3:
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)
2016-07-08 12:54:46 +00:00
# submit time logs
for timesheet in frappe.get_all("Timesheet", ["name"], {"docstatus": 0,
"work_order": ("!=", ""), "to_time": ("<", frappe.flags.current_date)}):
2016-07-08 12:54:46 +00:00
timesheet = frappe.get_doc("Timesheet", timesheet.name)
try:
timesheet.submit()
frappe.db.commit()
except OverlapError:
pass
2016-07-13 10:33:05 +00:00
except WorkstationHolidayError:
pass
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()