{
  "schemaVersion": 1,
  "slug": "profit-margin-calculator",
  "category": "finance",
  "kind": "implementation",
  "independentValidation": false,
  "implementationHash": "92f94e18aa74ba18d00d2afdaf6ed87d608a13a2153709f40fe4330b7f591983",
  "provenance": "Generated from the calculation contract used by the published page. Publication is not external validation.",
  "sourceFiles": [
    "data/calculators/profit-margin-calculator.json",
    "src/engine.js"
  ],
  "runtimeSource": "/sources/engine.txt",
  "runtimeHash": "5c888d8898afbdee36267bb9977829e1e2ac568f6677c8ee87469c139864645c",
  "scope": "The compute function records implemented equations, constants, lookup tables and custom rules. Inputs record defaults and allowed values; outputs record precision and units. Defaults are examples, not recommendations. General background references are not evidence for this specific algorithm.",
  "publishedFormula": {
    "formula": "M = (P − C) ÷ P × 100; Mk = (P − C) ÷ C × 100; T = (P − C) × q",
    "vars": [
      [
        "C",
        "Unit cost"
      ],
      [
        "P",
        "Selling price"
      ],
      [
        "q",
        "Quantity sold"
      ],
      [
        "M",
        "Profit margin (%)"
      ],
      [
        "Mk",
        "Markup (%)"
      ],
      [
        "T",
        "Total profit"
      ]
    ],
    "note": "Gross profit per unit = selling price − unit cost; margin = profit / selling price × 100; markup = profit / cost × 100. Margin is undefined at zero selling price and markup at zero cost."
  },
  "externalEvidence": null,
  "contract": {
    "compute": "const mergedResult=(()=>{if((true&&![\"unit\", \"total\"].includes(String(amountMode))))return{__error:'inputError'};\nif((true&&(!Number.isFinite(cost)||cost<0||cost>100000000))||(true&&(!Number.isFinite(price)||price<0||price>100000000))||([\"unit\"].includes(String(amountMode))&&(!Number.isFinite(quantity)||quantity<0||quantity>100000000)))return{__error:'inputError'};\nif(![cost,price].every(Number.isFinite)||cost<0||price<0||!['unit','total'].includes(amountMode)||(amountMode==='unit'&&(!Number.isFinite(quantity)||quantity<0)))return{__error:'inputError'};const profitPerUnit=price-cost;return{margin:price>0?profitPerUnit/price*100:null,profitPerUnit,totalProfit:profitPerUnit*(amountMode==='total'?1:quantity),markup:cost>0?profitPerUnit/cost*100:null};\n})();return mergedResult.__error?{\"margin\":null,\"profitPerUnit\":null,\"totalProfit\":null,\"markup\":null,...mergedResult}:mergedResult;",
    "inputs": [
      {
        "name": "amountMode",
        "type": "select",
        "default": "unit",
        "options": [
          {
            "value": "unit"
          },
          {
            "value": "total"
          }
        ]
      },
      {
        "name": "cost",
        "type": "number",
        "default": 30,
        "min": 0,
        "max": 100000000,
        "step": 1,
        "requiredValue": true,
        "strictRange": true
      },
      {
        "name": "price",
        "type": "number",
        "default": 50,
        "min": 0,
        "max": 100000000,
        "step": 1,
        "requiredValue": true,
        "strictRange": true
      },
      {
        "name": "quantity",
        "type": "number",
        "default": 100,
        "min": 0,
        "max": 100000000,
        "step": 1,
        "showWhen": {
          "amountMode": [
            "unit"
          ]
        },
        "requiredValue": true,
        "strictRange": true
      }
    ],
    "outputs": [
      {
        "name": "margin",
        "precision": 2,
        "unit": "%",
        "optional": true
      },
      {
        "name": "profitPerUnit",
        "precision": 2,
        "unit": "$"
      },
      {
        "name": "totalProfit",
        "precision": 2,
        "unit": "$"
      },
      {
        "name": "markup",
        "precision": 2,
        "unit": "%",
        "optional": true
      }
    ],
    "interpret": null
  }
}
