ERPNext.com Frappe Cloud Support Partners Foundation Frappe School

Create sales invoice

How to create sales invoice if order is cart and return invoice I’d else none…required field is customer, items, address

This is my code…bt it’s not correct

@frappe.whitelist()
def create_sales_invoice(**kwargs)->Union[str,bool]:
“”“Try to create invoice if order mode is cart and
return invoice id else None
required fields: customer,items, address,
“””
try:
js = frappe._dict(kwargs)

	customer = js.get('customer',None)
	items = js.get('items',None)
	address= js.get('address',None)
	order_mode = js.get('order_creating_mode',None)	# default cart Cart/RFQ/Lease
	item_details = js.get('item_details',None)
	if not customer and not items and not address:
		response['error'] = {'error': 'Missing required fields'}
		
		#return False
	else:
		if order_mode == 'Cart':
			
			_invoice_id = generate_invoice_id()
			if _invoice_id:
				sales_invoice_doc= get_mapped_sales_invoice(ignore_permissions=True)
				_order= frappe.get_doc('Orders')
				# sales_invoice_doc.order_id = _order.order_id
				sales_invoice_doc.user = _order.user
				sales_invoice_doc.delivery_address = address
				sales_invoice_doc.order_date = _order.order_date
				sales_invoice_doc.total_amount = _order.total_amount
				sales_invoice_doc.total_gst_amount = _order.total_gst
				sales_invoice_doc.total_item_count = _order.len(item_details)
				sales_invoice_doc.order_creating_mode = _order.order_mode
				sales_invoice_doc.address= address
				sales_invoice_doc.customer=customer
				for items in item_details:
					sales_invoice_doc.append('order_details', {
										'item_id': items.get('item_id', None),
										'quantity': items.get('quantity', None),
										'unit_price': items.get('unit_price', None),
										'unit_gst': items.get('unit_gst', None),
										'estimated_delivery_date': items.get('estimated_delivery_date', None),
										'actual_delivery_date': items.get('actual_deliver_date', None)
									})
				
				sales_invoice_doc.save(ignore_permissions=True)
				sales_invoice_doc.submit()
			else:
							response['error'] = {'error': 'unable to generate Invoice_ID'}
		else:
			response['error'] = {'error':f'Invalid Order mode,allowed Cart only'}
		
		
		
	#pass
except (frappe.exceptions.DoesNotExistError,Exception) as e:
	print(format_exc())
	return False