{
  "info": {
    "name": "CSP Mobile API",
    "description": "API collection for the CSP Visit / TSM Mobile app. All endpoints return JSON with an `error` field (0 = success, 1 = failure) and an `error_msg` field.\n\nBase URL is controlled by the `{{base_url}}` collection variable / environment (default: http://104.143.0.113:251/cspmobile).",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "variable": [
    { "key": "base_url", "value": "http://104.143.0.113:251/cspmobile", "type": "string" },
    { "key": "user_id", "value": "39", "type": "string" },
    { "key": "entity_no", "value": "CUS-0588", "type": "string" },
    { "key": "visit_id", "value": "1", "type": "string" }
  ],
  "item": [
    {
      "name": "Auth",
      "item": [
        {
          "name": "Login - Internal User (TSM/Admin)",
          "request": {
            "method": "POST",
            "header": [],
            "body": {
              "mode": "urlencoded",
              "urlencoded": [
                { "key": "tag", "value": "login", "description": "Must be exactly 'login'" },
                { "key": "email", "value": "EMP0001", "description": "EMP# from CSP.UM_USERS" },
                { "key": "password", "value": "your_password" }
              ]
            },
            "url": {
              "raw": "{{base_url}}/login_api_cspmobile_user.php",
              "host": [ "{{base_url}}" ],
              "path": [ "login_api_cspmobile_user.php" ]
            },
            "description": "Authenticates an internal TSM/admin user against CSP.UM_USERS by EMP#. Returns USER_ID, USER_NAME, GROUP_TYPE, and a token.\n\nSuccess response:\n```json\n{\n  \"error\": 0,\n  \"error_msg\": \"Successfully logged in\",\n  \"token\": \"...\",\n  \"USER_ID\": \"39\",\n  \"USER_NAME\": \"John Doe\",\n  \"GROUP_TYPE\": \"TSM\"\n}\n```"
          }
        },
        {
          "name": "Login - Customer / Entity",
          "request": {
            "method": "POST",
            "header": [],
            "body": {
              "mode": "urlencoded",
              "urlencoded": [
                { "key": "tag", "value": "login", "description": "Must be exactly 'login'" },
                { "key": "email", "value": "03001234567", "description": "CELL_NUM from CSP.ENTITY" },
                { "key": "password", "value": "your_password" }
              ]
            },
            "url": {
              "raw": "{{base_url}}/login_api_cspmobile_cust.php",
              "host": [ "{{base_url}}" ],
              "path": [ "login_api_cspmobile_cust.php" ]
            },
            "description": "Authenticates an external/customer entity by cell number against CSP.ENTITY. Returns ENTITY_NO, ENTITY_NAME, CELL_NUM, CONTACT_PERSON, and a token.\n\nSuccess response:\n```json\n{\n  \"error\": 0,\n  \"error_msg\": \"Successfully logged in\",\n  \"token\": \"...\",\n  \"ENTITY_NO\": \"CUS-0588\",\n  \"ENTITY_NAME\": \"ABC Store\",\n  \"CELL_NUM\": \"03001234567\",\n  \"CONTACT_PERSON\": \"Ali\"\n}\n```"
          }
        }
      ]
    },
    {
      "name": "Attendance",
      "item": [
        {
          "name": "Check-In / Check-Out",
          "request": {
            "method": "POST",
            "header": [],
            "body": {
              "mode": "formdata",
              "formdata": [
                { "key": "tag", "value": "attendance", "type": "text", "description": "Must be exactly 'attendance'" },
                { "key": "user_id", "value": "{{user_id}}", "type": "text" },
                { "key": "type", "value": "CHECK-IN", "type": "text", "description": "CHECK-IN or CHECK-OUT" },
                { "key": "date", "value": "2026-08-11", "type": "text", "description": "YYYY-MM-DD" },
                { "key": "time", "value": "09:00 AM", "type": "text" },
                { "key": "latitude", "value": "24.8607", "type": "text" },
                { "key": "longitude", "value": "67.0011", "type": "text" },
                { "key": "reason", "value": "", "type": "text", "description": "Optional" },
                { "key": "address", "value": "Head Office", "type": "text", "description": "Optional" },
                { "key": "image", "type": "file", "description": "Optional. JPG/PNG only." }
              ]
            },
            "url": {
              "raw": "{{base_url}}/checkinout_api_cspmobile.php",
              "host": [ "{{base_url}}" ],
              "path": [ "checkinout_api_cspmobile.php" ]
            },
            "description": "Marks CHECK-IN or CHECK-OUT for a user for a given date. Fails if that type is already marked for the same date. Optional photo upload.\n\nSuccess response:\n```json\n{\n  \"error\": 0,\n  \"error_msg\": \"CHECK-IN successfully marked\",\n  \"image_url\": \"http://.../uploads/attendance/att_....jpg\"\n}\n```"
          }
        },
        {
          "name": "Get Attendance Status",
          "request": {
            "method": "GET",
            "header": [],
            "url": {
              "raw": "{{base_url}}/checkinout_status.php?user_id={{user_id}}&date=2026-08-11",
              "host": [ "{{base_url}}" ],
              "path": [ "checkinout_status.php" ],
              "query": [
                { "key": "user_id", "value": "{{user_id}}" },
                { "key": "date", "value": "2026-08-11" }
              ]
            },
            "description": "Returns the latest attendance status for a user on a given date: CHECKED IN / CHECKED OUT / NOT MARKED."
          }
        }
      ]
    },
    {
      "name": "Shops & Items",
      "item": [
        {
          "name": "Get Shops for Salesman",
          "request": {
            "method": "GET",
            "header": [],
            "url": {
              "raw": "{{base_url}}/GET_ENTITYLIST_SHOPS.PHP?user_id={{user_id}}",
              "host": [ "{{base_url}}" ],
              "path": [ "GET_ENTITYLIST_SHOPS.PHP" ],
              "query": [ { "key": "user_id", "value": "{{user_id}}" } ]
            },
            "description": "Returns shops (CSP.ENTITY) assigned to the salesman's EMP# resolved from user_id. Note: filename is uppercase, case-sensitive on Linux."
          }
        },
        {
          "name": "Get Sale Items (Price List) for Entity",
          "request": {
            "method": "GET",
            "header": [],
            "url": {
              "raw": "{{base_url}}/get_saleitem_paani.php?entity_no={{entity_no}}",
              "host": [ "{{base_url}}" ],
              "path": [ "get_saleitem_paani.php" ],
              "query": [ { "key": "entity_no", "value": "{{entity_no}}" } ]
            },
            "description": "Returns items with entity-specific pricing (saleprice/tosaleprice) for the given entity_no."
          }
        },
        {
          "name": "Get Product List for Entity",
          "request": {
            "method": "GET",
            "header": [],
            "url": {
              "raw": "{{base_url}}/itemlist_cspmobile.php?id={{entity_no}}",
              "host": [ "{{base_url}}" ],
              "path": [ "itemlist_cspmobile.php" ],
              "query": [ { "key": "id", "value": "{{entity_no}}" } ]
            },
            "description": "Returns product catalog (name, description, image, price) for the given entity (query param is 'id', not 'entity_no')."
          }
        }
      ]
    },
    {
      "name": "TSM Visits",
      "item": [
        {
          "name": "Submit Visit Proof",
          "request": {
            "method": "POST",
            "header": [],
            "body": {
              "mode": "formdata",
              "formdata": [
                { "key": "tag", "value": "tsm_visit", "type": "text", "description": "Must be exactly 'tsm_visit'" },
                { "key": "user_id", "value": "{{user_id}}", "type": "text" },
                { "key": "place_name", "value": "ABC Store", "type": "text" },
                { "key": "channel", "value": "Retail", "type": "text", "description": "Optional" },
                { "key": "owner_name", "value": "Ali Khan", "type": "text" },
                { "key": "phone_number", "value": "03001234567", "type": "text" },
                { "key": "address", "value": "Main Market", "type": "text", "description": "Optional" },
                { "key": "latitude", "value": "24.8607", "type": "text", "description": "Optional" },
                { "key": "longitude", "value": "67.0011", "type": "text", "description": "Optional" },
                { "key": "remarks", "value": "", "type": "text", "description": "Optional" },
                { "key": "photo_1", "type": "file", "description": "Optional. JPG/PNG only." },
                { "key": "photo_2", "type": "file", "description": "Optional. JPG/PNG only." }
              ]
            },
            "url": {
              "raw": "{{base_url}}/tsmvisits_insert.php",
              "host": [ "{{base_url}}" ],
              "path": [ "tsmvisits_insert.php" ]
            },
            "description": "Creates a new TSM visit record (CSP.TSM_VISITS) with optional proof photos. Returns the resulting visit_id is not returned directly - use Get Visits to look it up afterward."
          }
        },
        {
          "name": "Get Visits (with latest order + items)",
          "request": {
            "method": "GET",
            "header": [],
            "url": {
              "raw": "{{base_url}}/tsmvisits_get.php?user_id={{user_id}}&from_date=2026-08-01&to_date=2026-08-11&place_name=",
              "host": [ "{{base_url}}" ],
              "path": [ "tsmvisits_get.php" ],
              "query": [
                { "key": "user_id", "value": "{{user_id}}" },
                { "key": "from_date", "value": "2026-08-01", "description": "Optional, requires to_date if set" },
                { "key": "to_date", "value": "2026-08-11", "description": "Optional, requires from_date if set" },
                { "key": "place_name", "value": "", "description": "Optional, partial match" }
              ]
            },
            "description": "Returns visits for a user (optionally date-ranged / place-filtered), each with its most recent order and that order's items (if productive)."
          }
        }
      ]
    },
    {
      "name": "Orders",
      "item": [
        {
          "name": "Submit Order - Internal TSM",
          "request": {
            "method": "POST",
            "header": [],
            "body": {
              "mode": "formdata",
              "formdata": [
                { "key": "is_external_client", "value": "0", "type": "text" },
                { "key": "visit_id", "value": "{{visit_id}}", "type": "text" },
                { "key": "user_id", "value": "{{user_id}}", "type": "text" },
                { "key": "is_productive", "value": "1", "type": "text", "description": "1 or 0" },
                { "key": "remarks", "value": "", "type": "text", "description": "Reason if non-productive" },
                { "key": "order_date", "value": "2026-08-11 10:00:00", "type": "text", "description": "Optional, defaults to now" },
                { "key": "items", "value": "[{\"item_id\":1,\"item_name\":\"Pepsi\",\"qty_pcs\":2,\"qty_box\":1,\"qty_foc\":0,\"rate\":150}]", "type": "text", "description": "JSON array, required if is_productive=1" },
                { "key": "photo_1", "type": "file", "description": "Optional" },
                { "key": "photo_2", "type": "file", "description": "Optional" }
              ]
            },
            "url": {
              "raw": "{{base_url}}/submit_visit_order.php",
              "host": [ "{{base_url}}" ],
              "path": [ "submit_visit_order.php" ]
            },
            "description": "Records an order (productive or non-productive) against an existing internal TSM visit. Multiple orders per visit are allowed."
          }
        },
        {
          "name": "Submit Order - External Client",
          "request": {
            "method": "POST",
            "header": [],
            "body": {
              "mode": "formdata",
              "formdata": [
                { "key": "is_external_client", "value": "1", "type": "text" },
                { "key": "entity_no", "value": "{{entity_no}}", "type": "text" },
                { "key": "customer_note", "value": "", "type": "text", "description": "Optional" },
                { "key": "created_at", "value": "", "type": "text", "description": "Optional ISO datetime, defaults to now" },
                { "key": "items", "value": "[{\"item_id\":1830,\"item_name\":\"PET 200 ML\",\"price\":960,\"quantity\":2}]", "type": "text", "description": "JSON array, required" }
              ]
            },
            "url": {
              "raw": "{{base_url}}/submit_visit_order.php",
              "host": [ "{{base_url}}" ],
              "path": [ "submit_visit_order.php" ]
            },
            "description": "Records a self-service order placed directly by a customer/entity (no TSM visit involved). Always treated as productive."
          }
        }
      ]
    },
    {
      "name": "Reports & Admin",
      "item": [
        {
          "name": "Users List (Admin)",
          "request": {
            "method": "GET",
            "header": [],
            "url": {
              "raw": "{{base_url}}/tsm_users_list.php?search=",
              "host": [ "{{base_url}}" ],
              "path": [ "tsm_users_list.php" ],
              "query": [ { "key": "search", "value": "", "description": "Optional, partial match on user_name" } ]
            },
            "description": "Returns active internal users with EMP code, department, designation, profile picture URL."
          }
        },
        {
          "name": "Daily Report - Internal TSM",
          "request": {
            "method": "GET",
            "header": [],
            "url": {
              "raw": "{{base_url}}/tsm_daily_report.php?user_id={{user_id}}&date=2026-08-11&place_name=",
              "host": [ "{{base_url}}" ],
              "path": [ "tsm_daily_report.php" ],
              "query": [
                { "key": "user_id", "value": "{{user_id}}" },
                { "key": "date", "value": "2026-08-11", "description": "Optional, defaults to today" },
                { "key": "place_name", "value": "", "description": "Optional" }
              ]
            },
            "description": "Dashboard for one TSM user: today's check-in/check-out, last-30-day visit count, and visits+orders+items for the given date."
          }
        },
        {
          "name": "Daily Report - External Clients",
          "request": {
            "method": "GET",
            "header": [],
            "url": {
              "raw": "{{base_url}}/tsm_daily_report.php?is_external_client=1&entity_no=&date=",
              "host": [ "{{base_url}}" ],
              "path": [ "tsm_daily_report.php" ],
              "query": [
                { "key": "is_external_client", "value": "1" },
                { "key": "entity_no", "value": "", "description": "Optional - omit for all clients" },
                { "key": "date", "value": "", "description": "Optional YYYY-MM-DD - omit for all dates" }
              ]
            },
            "description": "Orders placed by external/self-service clients, optionally filtered by entity_no and/or date. All four combinations of entity_no/date presence are supported."
          }
        }
      ]
    }
  ]
}
