It does not fetch the latest selling prices and always uses the rates set initially

we have developed an API to generate sales orders. While it works, it does not fetch the latest selling prices and always uses the rates set initially. Additionally, when adding new items manually, the rate shows as zero. Has anyone encountered this issue or know how to fix it?

Api is

import frappe

from frappe.utils import now

from erpnext.controllers.sales_and_purchase import get_item_details, get_default_price_list

@frappe.whitelist(allow_guest=True)

def create_pizza_hut_order():

try:

    data = frappe.request.get_json()



    branch = data.get("branch", "").strip()

    if not branch:

        return {"success": False, "error": "Branch is required"}



    \# Get existing customer

    customer = frappe.db.get_value("Customer", {"customer_name": branch}, "name")

    if not customer:

        return {"success": False, "error": f"Customer '{branch}' not found"}



    po_number = data.get("po_number")

    if not po_number:

        return {"success": False, "error": "PO Number is required"}



    po_date = data.get("po_date")

    delivery_date = data.get("delivery_date")



    \# Create Sales Order

    so = frappe.new_doc("Sales Order")

    so.customer = customer

    so.transaction_date = now()

    so.po_no = po_number

    so.delivery_date = delivery_date



    \# 🟢 Set price list BEFORE adding items

    price_list = get_default_price_list("Selling") or "Standard Selling"

    so.selling_price_list = price_list



    \# Map counts fields to item names

    counts_mapping = {

        "item 1": "item 11",

        "item 2": "item 12",

        "item 3": "item 13"

    }



    \# Add items

    for key, item_name in counts_mapping.items():

        qty = data.get(key)

        if not qty or float(qty) <= 0:

            continue



        \# Get item code

        item_code = frappe.db.get_value("Item", {"item_name": item_name}, "item_code")

        if not item_code:

            return {"success": False, "error": f"Item not found: {item_name}"}



        \# Fetch price details exactly like ERPNext UI

        item_args = {

            "item_code": item_code,

            "customer": customer,

            "doctype": "Sales Order",

            "price_list": price_list,

            "transaction_date": so.transaction_date

        }

        details = get_item_details(item_args)

        rate = details.get("price_list_rate")

        uom = details.get("uom") or "Nos"



        \# Append item — rate taken from ERPNext automatically

        so.append("items", {

            "item_code": item_code,

            "qty": qty,

            "rate": rate,

            "price_list_rate": rate,

            "uom": uom

        })



    \# Insert and submit

    so.insert()

    so.submit()



    return {

        "success": True,

        "sales_order": so.name,

        "message": f"Sales Order created for {branch}",

        "grand_total": so.grand_total

    }



except Exception as e:

    frappe.db.rollback()

    frappe.log_error(frappe.get_traceback(), "API Error")

    return {"success": False, "error": str(e)}

I asked ChatGPT and it said to add “company” and “qty” to “item_args” - it might work
also try to usefrappe.get_doc instead of frappe.db.get_value for Items as sometimes get_value fetches stale data

Hi, I just changed those values for security. The automation worked, but it doesn’t load default values like manual setups do, such as auto-fetching the tax table and the latest selling price