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
|
|
|
|
|
|
|
|
|
2018-09-27 13:21:18 +00:00
|
|
|
import random
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2018-09-27 13:21:18 +00:00
|
|
|
import frappe
|
2016-07-08 12:54:46 +00:00
|
|
|
from frappe.desk import query_report
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2016-07-08 12:54:46 +00:00
|
|
|
import erpnext
|
2017-09-28 06:21:03 +00:00
|
|
|
from erpnext.stock.doctype.batch.batch import UnableToSelectBatchError
|
2016-07-29 12:55:40 +00:00
|
|
|
from erpnext.stock.doctype.delivery_note.delivery_note import make_sales_return
|
|
|
|
from erpnext.stock.doctype.purchase_receipt.purchase_receipt import make_purchase_return
|
2016-07-08 12:54:46 +00:00
|
|
|
from erpnext.stock.doctype.serial_no.serial_no import SerialNoQtyError, SerialNoRequiredError
|
|
|
|
from erpnext.stock.stock_ledger import NegativeStockError
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2016-07-08 12:54:46 +00:00
|
|
|
|
|
|
|
def work():
|
|
|
|
frappe.set_user(frappe.db.get_global('demo_manufacturing_user'))
|
|
|
|
|
|
|
|
make_purchase_receipt()
|
|
|
|
make_delivery_note()
|
|
|
|
make_stock_reconciliation()
|
|
|
|
submit_draft_stock_entries()
|
2016-07-29 12:55:40 +00:00
|
|
|
make_sales_return_records()
|
|
|
|
make_purchase_return_records()
|
2016-07-08 12:54:46 +00:00
|
|
|
|
|
|
|
def make_purchase_receipt():
|
|
|
|
if random.random() < 0.6:
|
|
|
|
from erpnext.buying.doctype.purchase_order.purchase_order import make_purchase_receipt
|
|
|
|
report = "Purchase Order Items To Be Received"
|
2019-01-07 08:55:58 +00:00
|
|
|
po_list =list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:random.randint(1, 10)]
|
2016-07-08 12:54:46 +00:00
|
|
|
for po in po_list:
|
|
|
|
pr = frappe.get_doc(make_purchase_receipt(po))
|
|
|
|
|
|
|
|
if pr.is_subcontracted=="Yes":
|
|
|
|
pr.supplier_warehouse = "Supplier - WPL"
|
|
|
|
|
|
|
|
pr.posting_date = frappe.flags.current_date
|
|
|
|
pr.insert()
|
2016-07-13 12:11:03 +00:00
|
|
|
try:
|
|
|
|
pr.submit()
|
|
|
|
except NegativeStockError:
|
2017-08-28 12:47:36 +00:00
|
|
|
print('Negative stock for {0}'.format(po))
|
2016-07-13 12:11:03 +00:00
|
|
|
pass
|
2016-07-11 10:37:06 +00:00
|
|
|
frappe.db.commit()
|
2016-07-08 12:54:46 +00:00
|
|
|
|
|
|
|
def make_delivery_note():
|
|
|
|
# make purchase requests
|
|
|
|
|
|
|
|
# make delivery notes (if possible)
|
2018-09-27 13:21:18 +00:00
|
|
|
if random.random() < 0.6:
|
2016-07-08 12:54:46 +00:00
|
|
|
from erpnext.selling.doctype.sales_order.sales_order import make_delivery_note
|
|
|
|
report = "Ordered Items To Be Delivered"
|
|
|
|
for so in list(set([r[0] for r in query_report.run(report)["result"]
|
2019-01-07 08:55:58 +00:00
|
|
|
if r[0]!="Total"]))[:random.randint(1, 3)]:
|
2016-07-08 12:54:46 +00:00
|
|
|
dn = frappe.get_doc(make_delivery_note(so))
|
|
|
|
dn.posting_date = frappe.flags.current_date
|
|
|
|
for d in dn.get("items"):
|
|
|
|
if not d.expense_account:
|
|
|
|
d.expense_account = ("Cost of Goods Sold - {0}".format(
|
2018-08-08 11:07:31 +00:00
|
|
|
frappe.get_cached_value('Company', dn.company, 'abbr')))
|
2018-10-01 05:19:06 +00:00
|
|
|
|
2016-07-08 12:54:46 +00:00
|
|
|
try:
|
2018-10-01 05:19:06 +00:00
|
|
|
dn.insert()
|
2016-07-08 12:54:46 +00:00
|
|
|
dn.submit()
|
|
|
|
frappe.db.commit()
|
2017-09-28 06:21:03 +00:00
|
|
|
except (NegativeStockError, SerialNoRequiredError, SerialNoQtyError, UnableToSelectBatchError):
|
2016-07-08 12:54:46 +00:00
|
|
|
frappe.db.rollback()
|
|
|
|
|
|
|
|
def make_stock_reconciliation():
|
|
|
|
# random set some items as damaged
|
|
|
|
from erpnext.stock.doctype.stock_reconciliation.stock_reconciliation import (
|
|
|
|
EmptyStockReconciliationItemsError,
|
|
|
|
OpeningEntryAccountError,
|
2021-09-02 11:14:59 +00:00
|
|
|
)
|
2016-07-08 12:54:46 +00:00
|
|
|
|
2018-09-27 13:21:18 +00:00
|
|
|
if random.random() < 0.4:
|
2016-07-08 12:54:46 +00:00
|
|
|
stock_reco = frappe.new_doc("Stock Reconciliation")
|
|
|
|
stock_reco.posting_date = frappe.flags.current_date
|
2018-09-27 13:21:18 +00:00
|
|
|
stock_reco.company = erpnext.get_default_company()
|
2019-04-26 20:09:50 +00:00
|
|
|
stock_reco.get_items_for("Stores - WPL")
|
2016-07-08 12:54:46 +00:00
|
|
|
if stock_reco.items:
|
|
|
|
for item in stock_reco.items:
|
|
|
|
if item.qty:
|
|
|
|
item.qty = item.qty - round(random.randint(1, item.qty))
|
|
|
|
try:
|
2020-11-04 14:40:57 +00:00
|
|
|
stock_reco.insert(ignore_permissions=True, ignore_mandatory=True)
|
2016-07-08 12:54:46 +00:00
|
|
|
stock_reco.submit()
|
|
|
|
frappe.db.commit()
|
|
|
|
except OpeningEntryAccountError:
|
|
|
|
frappe.db.rollback()
|
|
|
|
except EmptyStockReconciliationItemsError:
|
|
|
|
frappe.db.rollback()
|
|
|
|
|
|
|
|
def submit_draft_stock_entries():
|
|
|
|
from erpnext.stock.doctype.stock_entry.stock_entry import (
|
2018-09-27 13:21:18 +00:00
|
|
|
DuplicateEntryForWorkOrderError,
|
|
|
|
IncorrectValuationRateError,
|
|
|
|
OperationsNotCompleteError,
|
2021-09-02 11:14:59 +00:00
|
|
|
)
|
2016-07-08 12:54:46 +00:00
|
|
|
|
|
|
|
# try posting older drafts (if exists)
|
2016-10-28 13:29:33 +00:00
|
|
|
frappe.db.commit()
|
2016-07-08 12:54:46 +00:00
|
|
|
for st in frappe.db.get_values("Stock Entry", {"docstatus":0}, "name"):
|
|
|
|
try:
|
|
|
|
ste = frappe.get_doc("Stock Entry", st[0])
|
|
|
|
ste.posting_date = frappe.flags.current_date
|
|
|
|
ste.save()
|
|
|
|
ste.submit()
|
|
|
|
frappe.db.commit()
|
2018-09-27 13:21:18 +00:00
|
|
|
except (NegativeStockError, IncorrectValuationRateError, DuplicateEntryForWorkOrderError,
|
2016-07-08 12:54:46 +00:00
|
|
|
OperationsNotCompleteError):
|
|
|
|
frappe.db.rollback()
|
|
|
|
|
2016-07-29 12:55:40 +00:00
|
|
|
def make_sales_return_records():
|
2016-12-19 06:21:02 +00:00
|
|
|
if random.random() < 0.1:
|
|
|
|
for data in frappe.get_all('Delivery Note', fields=["name"], filters={"docstatus": 1}):
|
|
|
|
if random.random() < 0.1:
|
|
|
|
try:
|
|
|
|
dn = make_sales_return(data.name)
|
|
|
|
dn.insert()
|
|
|
|
dn.submit()
|
|
|
|
frappe.db.commit()
|
|
|
|
except Exception:
|
|
|
|
frappe.db.rollback()
|
2016-07-29 12:55:40 +00:00
|
|
|
|
|
|
|
def make_purchase_return_records():
|
2016-12-19 06:21:02 +00:00
|
|
|
if random.random() < 0.1:
|
|
|
|
for data in frappe.get_all('Purchase Receipt', fields=["name"], filters={"docstatus": 1}):
|
|
|
|
if random.random() < 0.1:
|
|
|
|
try:
|
|
|
|
pr = make_purchase_return(data.name)
|
|
|
|
pr.insert()
|
|
|
|
pr.submit()
|
|
|
|
frappe.db.commit()
|
|
|
|
except Exception:
|
|
|
|
frappe.db.rollback()
|