-- ============================================================================
-- Migration: Update Inventory Management Stocks Tab Columns
-- Date: 2026-01-11
-- Description: Restrict columns shown in Stocks table (product_quantity_view)
--              to Quantity, Product Name, Product Price, Product Barcode,
--              Branch Name, Product Base Unit. Adds explicit column labels.
--              Idempotent: updates only the InventoryManagement module config.
-- ============================================================================

UPDATE modules 
SET config = $json$
{
  "title": "Inventory Management",
  "header": [],
  "body": [
    {
      "type": "tabs",
      "tabs": [
        {
          "label": "Receive",
          "icon": "fa-arrow-down",
          "body": [
            {
              "type": "button",
              "label": "Receive Inventory",
              "icon": "fa-plus",
              "class": "btn btn-primary mb-4",
              "formName": "inventory_receive_form"
            },
            {
              "type": "table",
              "options": {
                "tableName": "inventory_receive",
                "showGlobalSearch": true,
                "addButton": false,
                "columnsToShow": ["delivery_note_number", "vendor", "branch", "lpo_number", "invoice_number", "truck", "date"],
                "rowActions": [
                  { "label": "View", "icon": "fa-eye", "onClick": "viewInventoryReceive" },
                  { "label": "Edit", "icon": "fa-edit", "onClick": "editInventoryReceive" }
                ]
              }
            }
          ]
        },
        {
          "label": "Stocks",
          "icon": "fa-warehouse",
          "body": [
            { "type": "button", "label": "Quick Quantity Entry", "icon": "fa-bolt", "class": "btn btn-primary", "action": "inventory:quick-quantity" },
            { "type": "button", "label": "Adjust Stock", "icon": "fa-edit", "class": "btn btn-warning", "formName": "inventory_adjustment_form" },
            { "type": "button", "label": "Transfer", "icon": "fa-exchange-alt", "class": "btn btn-secondary", "formName": "inventory_transfer_form" },
            { "type": "button", "label": "Break Bulk", "icon": "fa-cut", "class": "btn btn-secondary", "action": "inventory:break" },
            { "type": "button", "label": "Export", "icon": "fa-download", "class": "btn btn-secondary", "onClick": "exportInventory" },
            {
              "type": "table",
              "options": {
                "tableName": "product_quantity_view",
                "showGlobalSearch": true,
                "addButton": false,
                "editFormName": "inventory_adjustment_form"

              }
            }
          ]
        },
        {
          "label": "Transfers",
          "icon": "fa-exchange-alt",
          "body": [
            { "type": "button", "label": "New Transfer", "icon": "fa-plus", "class": "btn btn-primary mb-4", "formName": "inventory_transfer_form" },
            {
              "type": "table",
              "options": {
                "tableName": "inventory_transfers",
                "showGlobalSearch": true,
                "addButton": false,
                "columnsToShow": ["reference_number", "transfer_date", "status"],
                "rowActions": [
                  {
                    "label": "Complete Transfer",
                    "icon": "fa-check",
                    "confirm": "Complete this transfer? This will automatically move stock between branches.",
                    "update": { "status": "completed" },
                    "condition": "row.status === 'pending'"
                  }
                ]
              }
            }
          ]
        },
        {
          "label": "Warehouses",
          "icon": "fa-building",
          "body": [
            { "type": "button", "label": "Add Warehouse", "icon": "fa-plus", "class": "btn btn-primary mb-4", "formName": "warehouse_form" },
            {
              "type": "table",
              "options": {
                "tableName": "branches",
                "showGlobalSearch": true,
                "addButton": false,
                "editFormName": "warehouse_form",
                "columnsToShow": ["name", "code", "type", "address"],
                "rowActions": [
                  { "label": "View Stock", "icon": "fa-boxes", "onClick": "viewWarehouseStock" },
                  { "label": "Deactivate", "icon": "fa-ban", "onClick": "deactivateWarehouse" }
                ]
              }
            }
          ]
        },
        {
          "label": "Stock Movement",
          "icon": "fa-truck-moving",
          "body": [
            { "type": "html", "html": "<div class=\"alert alert-info mb-4\"><span class=\"font-semibold\">Stock Movement Report:</span> View detailed product movement history including sales, receipts, transfers, and adjustments. Use filters below to narrow by date, product, category, or warehouse.</div>" },
            {
              "type": "filter-group",
              "filters": [
                { "type": "filter-date-range", "options": { "data_column": "movement_date", "label": "Date Range", "default_from": "-30 days", "default_to": "today" } },
                { "type": "filter-select", "options": { "source_table": "branches", "source_column": "branch_id", "display_column": "name", "data_column": "branch_id", "placeholder": "All Warehouses", "default_label": "All Warehouses" } },
                { "type": "filter-select", "options": { "source_table": "product_categories", "source_column": "category_id", "display_column": "name", "data_column": "category_id", "placeholder": "All Categories", "default_label": "All Categories" } },
                { "type": "filter-select", "options": { "source_table": "products", "source_column": "product_id", "display_column": "name", "data_column": "product_id", "placeholder": "Search products...", "search_fields": ["name", "barcode", "product_number"], "max_results": 50, "debounce_ms": 300, "multiple": true } }
              ]
            },
            {
              "type": "table",
              "options": {
                "tableName": "stock_movement_daily",
                "customEndpoint": "../api/stock-movement/report",
                "showGlobalSearch": true,
                "addButton": false,
                "columnsToShow": [
                  "movement_date", "product_name", "product_category", "branch_name",
                  "opening_balance", "received_qty", "sold_qty", "transfer_in_qty", "transfer_out_qty",
                  "adjustment_in_qty", "adjustment_out_qty", "wastage_qty", "closing_balance"
                ],
                "columnLabels": {
                  "movement_date": "Date",
                  "product_name": "Product",
                  "product_category": "Category",
                  "branch_name": "Warehouse",
                  "opening_balance": "Opening",
                  "received_qty": "Received",
                  "sold_qty": "Sold",
                  "transfer_in_qty": "Transfer In",
                  "transfer_out_qty": "Transfer Out",
                  "adjustment_in_qty": "Adj. In",
                  "adjustment_out_qty": "Adj. Out",
                  "wastage_qty": "Wastage",
                  "closing_balance": "Closing"
                },
                "columnFormats": {
                  "movement_date": "date",
                  "opening_balance": "number",
                  "received_qty": "number",
                  "sold_qty": "number",
                  "transfer_in_qty": "number",
                  "transfer_out_qty": "number",
                  "adjustment_in_qty": "number",
                  "adjustment_out_qty": "number",
                  "wastage_qty": "number",
                  "closing_balance": "number"
                },
                "filterField": ["movement_date", "branch_id", "category_id", "product_id"],
                "rowActions": [ { "label": "View Details", "icon": "fa-eye", "onClick": "viewMovementDetails" } ],
                "sortable": true,
                "pagination": true,
                "pageSize": 50
              }
            }
          ]
        },
        {
          "label": "ReOrders",
          "icon": "fa-redo",
          "body": [
            { "type": "alert", "class": "alert-warning mb-4", "content": "<i class=\"fa fa-exclamation-triangle\"></i> <strong>Products Below Minimum Stock Levels</strong><br>These products need to be reordered based on your configured thresholds." },
            {
              "type": "table",
              "options": {
                "tableName": "auto_orders_reorder",
                "showGlobalSearch": true,
                "addButton": false,
                "columnsToShow": ["product_name", "current_qty", "minimum_stock_qty", "order_qty"],
                "rowActions": [ { "label": "Adjust Threshold", "icon": "fa-edit", "action": "edit" } ]
              }
            }
          ]
        },
        {
          "label": "Shrinkage/Wastage",
          "icon": "fa-exclamation-triangle",
          "body": [
            {
              "type": "table",
              "options": {
                "tableName": "inventory_txn",
                "showGlobalSearch": true,
                "addButton": false,
                "columnsToShow": ["updatedAt", "product_name", "change", "unit_type", "reason", "batch_number", "notes", "branch_name"],
                "searchInputTypes": {
                  "reason": { "type": "select", "options": [
                    {"value": "", "label": "All Shrinkage Types"},
                    {"value": "damage", "label": "Damaged Goods"},
                    {"value": "theft", "label": "Theft/Loss"},
                    {"value": "expired", "label": "Expired Product"}
                  ]},
                  "updatedAt": { "type": "dateRange" }
                },
                "rowActions": [ { "label": "View Details", "icon": "fa-eye", "onClick": "viewTransactionDetails" } ]
              }
            }
          ]
        }
      ]
    }
  ],
  "footer": [ { "type": "button", "label": "Close", "onClick": "closeModal" } ]
}
$json$::jsonb
WHERE module_id = '0194a8b2-0016-7123-8123-123456789abc'::uuid
  AND name = 'InventoryManagement';