Try ERPNext Try Frappe Cloud Buy Support Partners Foundation

Automatically Create Invoice and Payment while creating order

Hi All,

I run a store on Shopify where payment is made right away while placing an order. In ERP Next this is broken down into three steps
1- Create Order
2- Create Invoice
3- Create Payment

Is there a way to automatically create the invoice and payment while making the order entry in ERPnext?

1 Like

It is not available out of the box but you maybe able to achieve it using some server scripts

Thank you Pawan.

@Pawan Can you maybe give some hints how this can be achieved? That would be very helpful
(or @farrukhmalik: If you already found a solution, would be great if you could share with the community)

1 Like

I haven’t tried it yet. Will post solution here when I am done. Right now, I am studying that how server scripts work :slight_smile:

2 Likes

Awesome. Please don’t forget to post your findings here when you found a solution so others can benefit as well (:
Cheers

Check this. This might be helpful. :slight_smile: https://python.hotexamples.com/examples/erpnext.selling.doctype.sales_order.sales_order/-/make_sales_invoice/python-make_sales_invoice-function-examples.html

So I created a Server Script as suggested by @Pawan.

Script Type : DocType Event
Reference Document Type : Sales Order
DocType Event : Before Save (Submitted Document)

and wrote the following code as per examples give in the previous post.

from erpnext.selling.doctype.sales_order.sales_order import make_sales_invoice

current_date = doc.transaction_date;
so = doc;

if so.docstatus==1 and not so.per_billed:
		si = make_sales_invoice(so)
		si.doc.posting_date = current_date
		si.insert()
		si.submit()

Unfortunately, I get the following error when a Sales Order is updated.

ImportError: __import__ not found

It seems that I am very close to the solution. I would be obliged if anyone (especially @Pawan) can help to fix that probelm. :pray:

1 Like

Hi @farrukhmalik, I continued trying to solve this issue and realized where your error message probably comes from. According to https://docs.erpnext.com/docs/v13/user/manual/en/customize-erpnext/server-script you are not allowed to call any function from the erpnext library. There is only a list of selected functions that are available to be called from a server script. As you try to import something from erpnext, python throws this error message (see https://pypi.org/project/RestrictedPython/, under “Problematic code example”).

@Pawan : You mentioned that this is available using server scripts. How would one do it then, if the erpnext functions are not available. Sales Order for example uses from frappe.model.mapper import get_mapped_doc to create a sales invoice (inmake_sales_invoice()). From what I can see, this function can not be called from a server script.

You can try to write your own function using frappe.get_doc and then use the insert method or if you have access to the server code, you can try to whitelist the function.

@bluesky kindly do not tag people as this is a voluntary forum and we do not have any obligation to answer your queries

Thanks Pawan. I will try.
(Concerning the tagging: Not sure what the purpose of the tags are if not to address specific people? Feel free to not answer if tagged (as you said, it’s a voluntary forum). If tagging should not be used, this feature should be disabled in my opinion or a clearer guideline should be provided in https://discuss.erpnext.com/faq. Any clarification in this regard is appreciated. Maybe I just didn’t understand so far what the purpose of the tags are…)