Hi all,
i have a condition where the company doesnt need delivery note but they can make delivery trip directly from sales invoice. I already write the code but still run an error message “an error occured while creating…”. below i give my code. Is the anyone can help me with this? thankyou
py code:
@frappe.whitelist()
def set_delivery_trip(sales_invoice):
try:
sales_invoice = frappe.get_doc(“Sales Invoice”, sales_invoice)
# frappe.msgprint(f"Sales Invoice: {sales_invoice}")
if sales_invoice.workflow_state == "Waiting For Delivery Trip":
# frappe.msgprint("Creating Delivery Trip...")
delivery_trip = frappe.new_doc("Delivery Trip")
delivery_trip.customer = sales_invoice.customer
delivery_trip.contact = sales_invoice.contact_person
delivery_trip.customer_contact = sales_invoice.contact_display
delivery_trip.grand_total = sales_invoice.grand_total
if sales_invoice.company_address:
delivery_trip.customer_address = sales_invoice.customer_address
delivery_trip.append("delivery_stops", {
"customer": sales_invoice.customer,
"address": sales_invoice.customer_address
})
else:
frappe.msgprint("shipping address is missing. cannot create delivery trip")
return
if sales_invoice.driver_name:
delivery_trip.driver = sales_invoice.driver_name
if sales_invoice.vehicle_number:
delivery_trip.vehicle = sales_invoice.vehicle_number
delivery_trip.departure_time = frappe.utils.now_datetime()
delivery_trip.save()
# frappe.msgprint("Delivery Trip created successfully")
else:
frappe.msgprint("Cannot create Delivery Trip for this Sales Invoice.")
except Exception as e:
frappe.log_error(f"Error creating delivery trip: {e}")
frappe.throw("An error occured while creating while creating the delivery trip")
js code:
frappe.ui.form.on(‘Sales Invoice’, {
refresh: function(frm) {
if (frm.doc.docstatus == 0 && frm.doc.workflow_state == “Waiting For Delivery Trip”){
frm.add_custom_button(__(‘Delivery Trip’), function(){
create_delivery_trip(frm)
}, __(“Create”));
}
}
});
function create_delivery_trip(frm){
frappe.call({
method: “sanjaya.sanjaya.overide.sales_invoice.set_delivery_trip”,
args:{
sales_invoice: frm.doc.name
},
callback: function(r) {
if (r.message) {
frappe.msgprint(r.message);
} else {
frappe.msgprint(“An error occurred while creating the delivery trip.”);
}
}
})
}