Inventory Clearing Price API

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.

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.php 200 OK 400 Bad Request 405 Method Not Allowed

All requests and responses are JSON. Include Content-Type: application/json.

Request Schema

Common

FieldTypeRequiredDescription
actionstringYesOne of calculatePrice or calculateExtraProfit.

Action: calculatePrice

FieldTypeRequiredRulesDescription
inventorynumberYes≠ 0Total inventory units on hand (i).
lifetimeVolumenumberYesRemaining lifetime sales volume expected (v).
avgPricenumberYesAverage selling price for lifetime sales (p).
dealVolumenumberYes≠ 0Units to clear in this deal (o); remaining inventory is scrapped.
dieCostnumberYesUnfinished cost (die cost) per unit (d).
finalCostnumberYesFinal (finished) cost per unit (c).

Action: calculateExtraProfit

FieldTypeRequiredRulesDescription
globalSpecialPricenumberYesThe previously computed profit-equalizing price.
dealVolumenumberYesSame o used above (units sold in the deal).
offerPricenumberYesOffered price per unit for the deal.

Response Schema

Success

StatusBodyDescription
200
{
  "profitEqualizingPrice": number
}
Returned by calculatePrice. Rounded to 3 decimals.
200
{
  "extraProfit": number
}
Returned by calculateExtraProfit. Rounded to 2 decimals.

Errors

StatusBodyWhen
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.

Examples

1) Compute Profit-Equalizing Price

Request

POST https://www.promptbox.cn/api/inventory.php
Content-Type: application/json

{
  "action": "calculatePrice",
  "inventory": 12000,
  "lifetimeVolume": 7000,
  "avgPrice": 31.5,
  "dealVolume": 7500,
  "dieCost": 12.8,
  "finalCost": 22.4
}

Response

HTTP/1.1 200 OK
Content-Type: application/json

{
  "profitEqualizingPrice": 19.437
}

curl

curl -sS -X POST https://www.promptbox.cn/api/inventory.php \
  -H "Content-Type: application/json" \
  -d '{
    "action":"calculatePrice",
    "inventory":12000,
    "lifetimeVolume":7000,
    "avgPrice":31.5,
    "dealVolume":7500,
    "dieCost":12.8,
    "finalCost":22.4
  }'

JavaScript (fetch)

await fetch("https://www.promptbox.cn/api/inventory.php",{
  method:"POST",
  headers:{ "Content-Type":"application/json" },
  body: JSON.stringify({
    action:"calculatePrice",
    inventory:12000,
    lifetimeVolume:7000,
    avgPrice:31.5,
    dealVolume:7500,
    dieCost:12.8,
    finalCost:22.4
  })
}).then(r=>r.json());

Python (requests)

import requests
payload = {
  "action":"calculatePrice",
  "inventory":12000,
  "lifetimeVolume":7000,
  "avgPrice":31.5,
  "dealVolume":7500,
  "dieCost":12.8,
  "finalCost":22.4
}
r = requests.post("https://www.promptbox.cn/api/inventory.php", json=payload)
print(r.json())

2) Compute Extra Profit for an Offer

Request

POST https://www.promptbox.cn/api/inventory.php
Content-Type: application/json

{
  "action": "calculateExtraProfit",
  "globalSpecialPrice": 19.437,
  "dealVolume": 7500,
  "offerPrice": 20.25
}

Response

HTTP/1.1 200 OK
Content-Type: application/json

{
  "extraProfit": 6097.50
}

curl

curl -sS -X POST https://www.promptbox.cn/api/inventory.php \
  -H "Content-Type: application/json" \
  -d '{
    "action":"calculateExtraProfit",
    "globalSpecialPrice":19.437,
    "dealVolume":7500,
    "offerPrice":20.25
  }'

Validation Rules & Edge Cases

Security, Headers & CORS

Performance & Limits

Change Log

DateVersionNotes
2025-09-28v1Initial public documentation for /api/inventory.php with two actions.