{
  "id": "OLI-002",
  "name": "Create Order from Quote auto-copies quote_product__c into order_product__c",
  "description": "When an Order is inserted with QuoteId pointing at a Quote that has quote_product__c children, PersistenceService.Insert clones each quote_product__c into order_product__c on the new Order inside the same transaction. Mirrors the existing Opportunity to Quote line-item clone. Regression for the staging Order 38803218-9987-4eaf-aeef-d65614438419 where line items were missing.",
  "object": "Order",
  "method": "POST",
  "endpoint": "/services/data/v63.0/sobjects/Order",
  "setup": [
    {
      "type": "sql",
      "name": "Look up Contract RecordType Id for TiDB Cloud Services Agreement",
      "sql": "SELECT rt.Id AS csa_rt_id FROM _System_RecordType rt JOIN _System_Object so ON so.Id = rt.ObjectId WHERE so.ApiName IN ('Contract','contract') AND rt.Name = 'TiDB Cloud Services Agreement' AND rt.IsActive = 1 AND (rt.IsDeleted = 0 OR rt.IsDeleted IS NULL) LIMIT 1",
      "fetch": "one",
      "extract": {
        "csa_rt_id": "csa_rt_id"
      }
    },
    {
      "type": "sql",
      "name": "Ensure CSA RecordType has a record_type_code",
      "sql": "UPDATE _System_RecordType SET record_type_code = 'CSA' WHERE Id = %s AND (record_type_code IS NULL OR record_type_code = '')",
      "params": [
        "{csa_rt_id}"
      ]
    },
    {
      "type": "sql",
      "name": "Create Account for OLI-002",
      "sql": "INSERT INTO Account (Id, Name, Type, BillingCountryCode, IsDeleted, CreatedDate, LastModifiedDate, SystemModstamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
      "params": [
        "oli002-acc-01",
        "OLI-002 Account",
        "Customer - Direct",
        "{billing_country_code}",
        0,
        "{db_timestamp}",
        "{db_timestamp}",
        "{db_timestamp}"
      ]
    },
    {
      "type": "sql",
      "name": "Create Effective CSA Contract for OLI-002",
      "sql": "INSERT INTO Contract (Id, AccountId, Status, RecordTypeId, ContractTerm, StartDate, EndDate, IsDeleted, CreatedDate, LastModifiedDate, SystemModstamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
      "params": [
        "oli002-cnt-01",
        "oli002-acc-01",
        "Effective",
        "{csa_rt_id}",
        12,
        "2026-01-01",
        "2027-01-01",
        0,
        "{db_timestamp}",
        "{db_timestamp}",
        "{db_timestamp}"
      ]
    },
    {
      "type": "sql",
      "name": "Create Opportunity for OLI-002",
      "sql": "INSERT INTO Opportunity (Id, Name, AccountId, StageName, CloseDate, Amount, IsDeleted, CreatedDate, LastModifiedDate, SystemModstamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
      "params": [
        "oli002-opp-01",
        "OLI-002 Opp",
        "oli002-acc-01",
        "Negotiation",
        "2027-01-01",
        420000,
        0,
        "{db_timestamp}",
        "{db_timestamp}",
        "{db_timestamp}"
      ]
    },
    {
      "type": "sql",
      "name": "Create Accepted Quote for OLI-002",
      "sql": "INSERT INTO Quote (Id, Name, OpportunityId, AccountId, Status, Service_Start_Date__c, Service_End_Date__c, IsDeleted, CreatedDate, LastModifiedDate, SystemModstamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
      "params": [
        "oli002-quote-01",
        "OLI-002-QT",
        "oli002-opp-01",
        "oli002-acc-01",
        "Accepted",
        "2026-05-01",
        "2026-12-31",
        0,
        "{db_timestamp}",
        "{db_timestamp}",
        "{db_timestamp}"
      ]
    },
    {
      "type": "sql",
      "name": "Create quote_product__c for OLI-002",
      "sql": "INSERT INTO quote_product__c (Id, Name, Quote__c, Product__c, Quantity__c, Unit_Net_Price__c, List_Price__c, Net_fees__c, Term_Months__c, IsDeleted, CreatedDate, LastModifiedDate, SystemModstamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
      "params": [
        "oli002-qp-01",
        "OLI-002-QP",
        "oli002-quote-01",
        "01t5g000000liLxAAI",
        1,
        420000,
        420000,
        420000,
        12,
        0,
        "{db_timestamp}",
        "{db_timestamp}",
        "{db_timestamp}"
      ]
    }
  ],
  "payload": {
    "QuoteId": "oli002-quote-01",
    "AccountId": "oli002-acc-01",
    "EffectiveDate": "2026-05-01",
    "Status": "Draft"
  },
  "expects": {
    "status_code": 201
  },
  "extract": {
    "order_id": "id"
  },
  "query_after": {
    "object": "order_product__c",
    "id_field": "Order__c",
    "record_id": "{order_id}",
    "assertions": [
      {
        "field": "Order__c",
        "operator": "equals",
        "value": "{order_id}"
      },
      {
        "field": "Quote_Product__c",
        "operator": "equals",
        "value": "oli002-qp-01"
      },
      {
        "field": "Product__c",
        "operator": "equals",
        "value": "01t5g000000liLxAAI"
      },
      {
        "field": "Quantity__c",
        "operator": "equals",
        "value": 1
      },
      {
        "field": "Net_fees__c",
        "operator": "equals",
        "value": 420000
      }
    ]
  },
  "teardown": [
    {
      "type": "sql",
      "sql": "DELETE FROM order_product__c WHERE Quote_Product__c = %s",
      "params": [
        "oli002-qp-01"
      ]
    },
    {
      "type": "sql",
      "sql": "DELETE FROM `Order` WHERE Id = %s",
      "params": [
        "{order_id}"
      ]
    },
    {
      "type": "sql",
      "sql": "DELETE FROM quote_product__c WHERE Id = %s",
      "params": [
        "oli002-qp-01"
      ]
    },
    {
      "type": "sql",
      "sql": "DELETE FROM Quote WHERE Id = %s",
      "params": [
        "oli002-quote-01"
      ]
    },
    {
      "type": "sql",
      "sql": "DELETE FROM Opportunity WHERE Id = %s",
      "params": [
        "oli002-opp-01"
      ]
    },
    {
      "type": "sql",
      "sql": "DELETE FROM Contract WHERE Id = %s",
      "params": [
        "oli002-cnt-01"
      ]
    },
    {
      "type": "sql",
      "sql": "DELETE FROM Account WHERE Id = %s",
      "params": [
        "oli002-acc-01"
      ]
    }
  ],
  "priority": "P0",
  "bug_ref": "#1514",
  "scene": "order_line",
  "skip": true,
  "skip_reason": "Regression case is structurally repaired, but current product behavior still creates no order_product__c rows when an Order is created from a Quote with quote_product__c children"
}
