Error on Creating Sales Invoice on python script

Hi,

i need help on this
`record = frappe.new_doc(“Sales Invoice”)
record.update({
“doctype”:“Sales Invoice”,
“customer”:so[‘customer’],
“sales_partner”:so[‘sales_partner’],
“customer_name”:so[‘customer_name’],
“is_pos”:0,
“is_return”:0,
“posting_date”:date,
“delivery_date”:date,
“delivery_instructions”:so[‘delivery_instructions’],
“company”:so[‘company’],
“customer_address”:so[‘customer_address’],
“address_display”:so[‘address_display’],
“customer_group”:so[‘customer_group’],
“territory”:so[‘territory’],
“update_stock”:0,
“apply_discount_on”:so[‘apply_discount_on’],
“base_discount_amount”:flt(so[‘base_discount_amount’]),
“additional_discount_percentage”:flt(so[‘additional_discount_percentage’]),
“discount_amount”:flt(so[‘discount_amount’])

        })

    for x in items:
        record.append("items",{
            "doctype":"Sales Invoice Item",
            "item_code":x['item_code'],
            "item_name":x['item_name'],
            "description":x['description'],
            "qty":flt(x['qty']),
            "stock_uom":x['stock_uom'],
            "rate":flt(x['rate']),
            "amount":flt(x['amount']),
            "base_rate":flt(x['base_rate']),
            "base_amount":flt(x['base_amount']),
            "income_account":x['income_account'],
            "cost_center":x['selling_cost_center'],
            "warehouse":x['warehouse'],
            })`

some how this resulted
frappe.exceptions.MandatoryError: [Sales Invoice, INV-012043]: items

please advise me :slight_smile:

@bobzz_zone
Check below code it will help you.

def make_sales_invoice(asset, item_code, company):
	si = frappe.new_doc("Sales Invoice")
	si.company = company
	si.currency = frappe.db.get_value("Company", company, "default_currency")
	disposal_account, depreciation_cost_center = get_disposal_account_and_cost_center(company)
	si.append("items", {
		"item_code": item_code,
		"is_fixed_asset": 1,
		"asset": asset,
		"income_account": disposal_account,
		"cost_center": depreciation_cost_center,
		"qty": 1
	})
	si.set_missing_values()
	return si`

hi,

thanks for the reply but im still getting error validation error items is mandatory

THanks,
BObby

@bobzz_zone

That means no items added in Item table.
Check your script by adding print statement
There should be at least one item in Item Table

yes i did… i had aready do print… and it had an item…and i was make sure that all requires field are filled

im tried to add the items in the update and with append but both is showing error… :frowning:

Please share your code.

sure

please have a look

def check_sales_order():
    date = add_days(nowdate(),1)
    data = frappe.db.sql("""select * from `tabSales Order` where transaction_date="{}" and docstatus=1""".format(date),as_dict=1)
    for so in data:
        items = frappe.db.sql("""select soi.item_code,soi.item_name,soi.description,soi.qty,soi.price_list_rate,soi.
        discount_percentage,soi.margin_type,soi.margin_rate_or_amount,soi.total_margin,soi.rate,soi.
    base_price_list_rate,soi.stock_uom,soi.amount,soi.base_rate,soi.base_amount,soi.pricing_rule,soi.
    net_rate,soi.net_amount,soi.base_net_rate,soi.base_net_amount,soi.delivered_by_supplier,soi.warehouse,soi.
    target_warehouse,soi.item_group,soi.brand,i.income_account,i.selling_cost_center from "tabSales Order Item" soi 
    join tabItem i on i.name = soi.item_code
    where soi.parent="{}" """.format(so['name']),as_dict=1)
    
    #origin=frappe.get_doc("Sales Order",so['name'])
    #data_item=[]

    record = frappe.new_doc("Sales Invoice")
    record.update({
        "doctype":"Sales Invoice",
        "customer":so['customer'],
        "sales_partner":so['sales_partner'],
        "customer_name":so['customer_name'],
        "is_pos":0,
        "is_return":0,
        "posting_date":date,
        "delivery_date":date,
        "delivery_instructions":so['delivery_instructions'],
        "company":so['company'],
        "customer_address":so['customer_address'],
        "address_display":so['address_display'],
        "customer_group":so['customer_group'],
        "territory":so['territory'],
        "update_stock":0,
        "apply_discount_on":so['apply_discount_on'],
        "base_discount_amount":flt(so['base_discount_amount']),
        "additional_discount_percentage":flt(so['additional_discount_percentage']),
        "discount_amount":flt(so['discount_amount'])
        })

    for x in items:
        record.append("items",{
            #"doctype":"Sales Invoice Item",
            "item_code":x['item_code'],
            "item_name":x['item_name'],
            "description":x['description'],
            "qty":flt(x['qty']),
            "stock_uom":x['stock_uom'],
            "rate":flt(x['rate']),
            "amount":flt(x['amount']),
            "base_rate":flt(x['base_rate']),
            "base_amount":flt(x['base_amount']),
            "income_account":x['income_account'],
            "cost_center":flt(x['selling_cost_center']),
            "warehouse":x['warehouse'],
            })
    record.set_missing_values()
    record.flags.ignore_permissions = 1
    record.save()`

Thanks
bobby

@bobzz_zone

print data and check what you get

Same for items. print and check whether you get any item or not

add print statement step by step and debug the issue.

here what i get whn show the items…
{‘base_price_list_rate’: 0.0, ‘selling_cost_center’: u’Main - DM’, ‘base_amount’: 0.6, ‘qty’: 12.0, ‘margin_rate_or_amount’: 0.0, ‘rate’: 0.05, ‘target_warehouse’: None, ‘base_net_rate’: 0.0, ‘discount_percentage’: 0.0, ‘item_name’: u’California Redemption Value under 24 fl oz’, ‘base_net_amount’: 0.6, ‘net_rate’: 0.0, ‘stock_uom’: None, ‘warehouse’: u’Compton - DM’, ‘description’: u’California Redemption Value under 24 fl oz’, ‘brand’: None, ‘base_rate’: 0.05, ‘item_code’: u’CACRV’, ‘margin_type’: None, ‘pricing_rule’: None, ‘net_amount’: 0.6, ‘price_list_rate’: 0.0, ‘income_account’: u’Cost of Goods Sold - DM’, ‘item_group’: None, ‘amount’: 0.6, ‘delivered_by_supplier’: 0L, ‘total_margin’: 0.0}

thats the items should be added…

for the sales order should be correct becuse without the items it can be added (**i made the items table not mandatory)

THanks