From Python code of a custom document, I created a Purchase Order on button click. Purchase Order is getting made. When I open the list, it is on Draft status. But when I open the PO it changes to Unsaved.
Here are screenshots :
In List -
When Opening -
Here is the code that creates this PO –
@frappe.whitelist()
def make_purchase_order(tower_id):
"""Raise PO with current Tower PO as reference"""
#purchase order
tower = frappe.db.get_values('Tower PO', {'name' : tower_id}, '*', as_dict=1)[0]
#tower_po = dict(tower)
new_po = frappe.new_doc('Purchase Order')
new_po.supplier = tower.supplier
new_po.transaction_date = frappe.utils.nowdate()
new_po.purpose = "Tower IP Fee Payment"
new_po.taxes_and_charges = 'GST-18%'
new_po.append('items', {
'item_code' : '39029',
'schedule_date' : frappe.utils.nowdate(),
'rate' : tower.net_amount,
'tower_po' : tower.name,
'total_sites' : tower.total_sites,
'billing_from' : tower.from_date,
'billing_to' : tower.to_date
})
taxes = frappe.db.get_values('Purchase Taxes and Charges', {'parent': new_po.taxes_and_charges}, '*', as_dict=1)
print(taxes)
for tax in taxes:
new_po.append("taxes", {
"charge_type": tax.charge_type,
"account_head": tax.account_head,
"description": tax.description,
"included_in_print_rate":tax.included_in_print_rate,
"rate": tax.rate,
"cost_center": 'Main - RK',
})
new_po.insert()
frappe.db.commit()
I think I have not used the insert()
correctly. Also, when previously I used
new_po.docstatus = 0 new_po.insert() new_po.save() frappe.db.commit()
Instead of this (which is my current code)
new_po.insert() frappe.db.commit()
2 Purchase Orders were being made. How to save PO correctly ?