Script to create Sales Invoice from Sales Order

Hi,

i new in erpnext and i try to write a method to conver sales order in invoice:

from future import unicode_literals

import frappe, json
import frappe.permissions

from frappe import _

from frappe.utils.csvutils import getlink
from frappe.utils.dateutils import parse_date

from frappe.utils import cint, cstr, flt
from frappe.core.page.data_import_tool.data_import_tool import get_data_keys

@frappe.whitelist(allow_guest=True)
def ping():
from erpnext.selling.doctype.sales_order.sales_order import make_sales_invoice
so = frappe.get_doc({“doctype”: “Sales Order”, “name”: “SO-00012”})
si = make_sales_invoice(so.name)
si.posting_date = “2014-12-12”
si.due_date = “2014-12-12”
si.insert()
si.submit()
si.save()
return ‘pong…v9’ + str(so.name) + ‘–’ +str(si.name)

i call in : http://172.50.100.3:8080/api/method/frappe.core.page.data_import_tool.alcarama.ping
It seems fine and run with no error found but the invoice it is not saved.
¿What do you think that is the problem?

Thanks in advance,
Rosendo.

Try adding frappe.db.commit() at the end, to commit the data into the database.

Hi nabinhait,
thank you for your help. It’s correct your comment and all run fine.
Rosendo.