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

158 lines
5.3 KiB
Python
Raw Normal View History

2016-06-29 13:08:32 +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
2016-07-07 08:32:26 +00:00
from frappe.utils.make_random import how_many, get_random
2016-06-29 13:08:32 +00:00
from frappe.desk import query_report
from erpnext.setup.utils import get_exchange_rate
from erpnext.accounts.party import get_party_account_currency
from erpnext.exceptions import InvalidCurrency
from erpnext.stock.doctype.material_request.material_request import make_request_for_quotation
from erpnext.buying.doctype.request_for_quotation.request_for_quotation import \
make_supplier_quotation as make_quotation_from_rfq
2016-07-07 08:32:26 +00:00
def work():
frappe.set_user(frappe.db.get_global('demo_purchase_user'))
2016-06-29 13:08:32 +00:00
if random.random() < 0.3:
report = "Items To Be Requested"
2016-07-07 08:32:26 +00:00
for row in query_report.run(report)["result"][:random.randint(1, 5)]:
2016-06-29 13:08:32 +00:00
item_code, qty = row[0], abs(row[-1])
mr = make_material_request(item_code, qty)
if random.random() < 0.3:
2016-07-07 08:32:26 +00:00
for mr in frappe.get_all('Material Request',
filters={'material_request_type': 'Purchase', 'status': 'Open'},
limit=random.randint(1,6)):
2016-07-08 12:54:46 +00:00
print mr.name
2016-07-07 08:32:26 +00:00
if not frappe.get_all('Request for Quotation',
filters={'material_request': mr.name}, limit=1):
rfq = make_request_for_quotation(mr.name)
rfq.transaction_date = frappe.flags.current_date
add_suppliers(rfq)
rfq.save()
rfq.submit()
# Make suppier quotation from RFQ against each supplier.
2016-06-29 13:08:32 +00:00
if random.random() < 0.3:
2016-07-07 08:32:26 +00:00
for rfq in frappe.get_all('Request for Quotation',
filters={'status': 'Open'}, limit=random.randint(1, 6)):
if not frappe.get_all('Supplier Quotation',
filters={'request_for_quotation': rfq.name}, limit=1):
rfq = frappe.get_doc('Request for Quotation', rfq.name)
for supplier in rfq.suppliers:
supplier_quotation = make_quotation_from_rfq(rfq.name, supplier.supplier)
supplier_quotation.save()
supplier_quotation.submit()
2016-06-29 13:08:32 +00:00
# get supplier details
supplier = get_random("Supplier")
company_currency = frappe.db.get_value("Company", "Wind Power LLC", "default_currency")
party_account_currency = get_party_account_currency("Supplier", supplier, "Wind Power LLC")
if company_currency == party_account_currency:
exchange_rate = 1
else:
exchange_rate = get_exchange_rate(party_account_currency, company_currency)
# make supplier quotations
2016-07-08 12:54:46 +00:00
if random.random() < 0.2:
2016-06-29 13:08:32 +00:00
from erpnext.stock.doctype.material_request.material_request import make_supplier_quotation
report = "Material Requests for which Supplier Quotations are not created"
2016-07-08 12:54:46 +00:00
for row in query_report.run(report)["result"][:random.randint(1, 3)]:
2016-06-29 13:08:32 +00:00
if row[0] != "'Total'":
sq = frappe.get_doc(make_supplier_quotation(row[0]))
2016-07-07 08:32:26 +00:00
sq.transaction_date = frappe.flags.current_date
2016-06-29 13:08:32 +00:00
sq.supplier = supplier
sq.currency = party_account_currency or company_currency
sq.conversion_rate = exchange_rate
sq.insert()
sq.submit()
frappe.db.commit()
# make purchase orders
2016-07-07 08:32:26 +00:00
if random.random() < 0.3:
2016-06-29 13:08:32 +00:00
from erpnext.stock.doctype.material_request.material_request import make_purchase_order
report = "Requested Items To Be Ordered"
for row in query_report.run(report)["result"][:how_many("Purchase Order")]:
if row[0] != "'Total'":
po = frappe.get_doc(make_purchase_order(row[0]))
po.supplier = supplier
po.currency = party_account_currency or company_currency
po.conversion_rate = exchange_rate
2016-07-07 08:32:26 +00:00
po.transaction_date = frappe.flags.current_date
2016-06-29 13:08:32 +00:00
po.insert()
po.submit()
frappe.db.commit()
2016-07-08 12:54:46 +00:00
if random.random() < 0.2:
2016-07-07 08:32:26 +00:00
make_subcontract()
2016-06-29 13:08:32 +00:00
def make_material_request(item_code, qty):
mr = frappe.new_doc("Material Request")
2016-07-08 12:54:46 +00:00
variant_of = frappe.db.get_value('Item', item_code, 'variant_of') or item_code
if frappe.db.get_value('BOM', {'item': variant_of, 'is_default': 1, 'is_active': 1}):
2016-06-29 13:08:32 +00:00
mr.material_request_type = 'Manufacture'
else:
mr.material_request_type = "Purchase"
mr.transaction_date = frappe.flags.current_date
2016-07-13 12:11:03 +00:00
moq = frappe.db.get_value('Item', item_code, 'min_order_qty')
2016-06-29 13:08:32 +00:00
mr.append("items", {
"doctype": "Material Request Item",
"schedule_date": frappe.utils.add_days(mr.transaction_date, 7),
"item_code": item_code,
2016-07-13 12:11:03 +00:00
"qty": qty if qty > moq else moq
2016-06-29 13:08:32 +00:00
})
mr.insert()
mr.submit()
return mr
def add_suppliers(rfq):
for i in xrange(2):
supplier = get_random("Supplier")
if supplier not in [d.supplier for d in rfq.get('suppliers')]:
rfq.append("suppliers", { "supplier": supplier })
def make_subcontract():
from erpnext.buying.doctype.purchase_order.purchase_order import make_stock_entry
# make sub-contract PO
po = frappe.new_doc("Purchase Order")
po.is_subcontracted = "Yes"
po.supplier = get_random("Supplier")
item_code = get_random("Item", {"is_sub_contracted_item": 1})
moq = frappe.db.get_value('Item', item_code, 'min_order_qty')
2016-06-29 13:08:32 +00:00
po.append("items", {
"item_code": item_code,
2016-06-29 13:08:32 +00:00
"schedule_date": frappe.utils.add_days(frappe.flags.current_date, 7),
"qty": moq
2016-06-29 13:08:32 +00:00
})
po.set_missing_values()
try:
po.insert()
except InvalidCurrency:
return
po.submit()
# make material request for
make_material_request(po.items[0].item_code, po.items[0].qty)
# transfer material for sub-contract
stock_entry = frappe.get_doc(make_stock_entry(po.name, po.items[0].item_code))
2016-07-08 12:54:46 +00:00
stock_entry.from_warehouse = "Stores - WPL"
stock_entry.to_warehouse = "Supplier - WPL"
2016-06-29 13:08:32 +00:00
stock_entry.insert()