REST API for computing the Profit-Equalizing Price and Extra Profit for an inventory-clearance deal.
Base URL: https://www.promptbox.cn/api/inventory.php
Version: v1 · Content Type: application/json · Method: POST
Overview
This endpoint exposes two computational operations via a single POST route.
action="calculatePrice" → returns the Profit-Equalizing Price (aka “special price”).
action="calculateExtraProfit" → given an offer price, returns the incremental profit over the profit-equalizing price.
No authentication required. CORS is typically allowed by most PHP setups when you control the domain; if you need cross-site use, enable appropriate headers at the server/proxy layer.
Endpoint
POST/api/inventory.php200 OK400 Bad Request405 Method Not Allowed
All requests and responses are JSON. Include Content-Type: application/json.
Request Schema
Common
Field
Type
Required
Description
action
string
Yes
One of calculatePrice or calculateExtraProfit.
Action: calculatePrice
Field
Type
Required
Rules
Description
inventory
number
Yes
≠ 0
Total inventory units on hand (i).
lifetimeVolume
number
Yes
—
Remaining lifetime sales volume expected (v).
avgPrice
number
Yes
—
Average selling price for lifetime sales (p).
dealVolume
number
Yes
≠ 0
Units to clear in this deal (o); remaining inventory is scrapped.
dieCost
number
Yes
—
Unfinished cost (die cost) per unit (d).
finalCost
number
Yes
—
Final (finished) cost per unit (c).
Action: calculateExtraProfit
Field
Type
Required
Rules
Description
globalSpecialPrice
number
Yes
—
The previously computed profit-equalizing price.
dealVolume
number
Yes
—
Same o used above (units sold in the deal).
offerPrice
number
Yes
—
Offered price per unit for the deal.
Response Schema
Success
Status
Body
Description
200
{
"profitEqualizingPrice": number
}
Returned by calculatePrice. Rounded to 3 decimals.
200
{
"extraProfit": number
}
Returned by calculateExtraProfit. Rounded to 2 decimals.
Errors
Status
Body
When
400
{"error":"Invalid JSON input."}
Malformed JSON payload.
400
{"error":"Please enter valid numerical values for all fields."}
Missing or non-numeric fields.
400
{"error":"Inventory and Deal Volume cannot be zero."}
inventory == 0 or dealVolume == 0 for calculatePrice.
400
{"error":"Missing or invalid values for calculation."}
Bad inputs to calculateExtraProfit.
400
{"error":"Invalid action."}
action not recognized.
405
{"error":"Only POST method is supported."}
Using GET/PUT/DELETE, etc.
Business Logic & Formulas
Given inputs: i=inventory, v=remaining lifetime volume, p=average selling price, o=deal volume, d=die cost, c=final cost.
Profit-Equalizing Price (“special price”): specialPrice = (o*c + d*(i - o) + p*v - c*v - d*(i - v)) / o
Rounded to 3 decimals.
Extra Profit at offer price offerPrice: extraProfit = (offerPrice - specialPrice) * o
Rounded to 2 decimals.
Interpretation: specialPrice is the per-unit price that makes total profit indifferent between clearing the deal now (with scrap of remainder) and proceeding with expected lifetime sales.