-- -----------------------------------------------------------
-- Internal ASN (Advance Shipment Notice) - Factory to Warehouse
-- Notifies destination warehouse of incoming factory FG shipments
-- -----------------------------------------------------------
CREATE TABLE IF NOT EXISTS `internal_asns` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `company_id` int unsigned NOT NULL DEFAULT 1,
  `asn_no` varchar(50) NOT NULL,
  `from_warehouse_id` int unsigned NOT NULL,
  `to_warehouse_id` int unsigned NOT NULL,
  `factory_id` int unsigned DEFAULT NULL,
  `dispatch_note_id` int unsigned DEFAULT NULL,
  `production_order_id` int unsigned DEFAULT NULL,
  `ship_date` date DEFAULT NULL,
  `eta_date` date DEFAULT NULL,
  `actual_arrival` date DEFAULT NULL,
  `vehicle_no` varchar(100) DEFAULT NULL,
  `driver_name` varchar(255) DEFAULT NULL,
  `driver_phone` varchar(50) DEFAULT NULL,
  `transporter_name` varchar(255) DEFAULT NULL,
  `total_qty` decimal(14,4) DEFAULT 0,
  `total_cartons` int unsigned DEFAULT 0,
  `total_pallets` int unsigned DEFAULT NULL,
  `total_weight_kg` decimal(14,2) DEFAULT NULL,
  `total_volume_cbm` decimal(10,3) DEFAULT NULL,
  `shipping_marks` text,
  `notes` text,
  `status` enum('draft','in_transit','arrived','received','cancelled') NOT NULL DEFAULT 'draft',
  `created_by` int unsigned DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_iasn_no` (`asn_no`, `company_id`),
  KEY `idx_iasn_status` (`status`),
  KEY `idx_iasn_company` (`company_id`),
  KEY `idx_iasn_from_wh` (`from_warehouse_id`),
  KEY `idx_iasn_to_wh` (`to_warehouse_id`),
  KEY `idx_iasn_dn` (`dispatch_note_id`),
  KEY `idx_iasn_factory` (`factory_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `internal_asn_items` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `asn_id` int unsigned NOT NULL,
  `item_id` int unsigned NOT NULL,
  `color` varchar(100) DEFAULT NULL,
  `size` varchar(100) DEFAULT NULL,
  `lot_no` varchar(100) DEFAULT NULL,
  `batch_no` varchar(100) DEFAULT NULL,
  `uom_id` int unsigned DEFAULT NULL,
  `shipped_qty` decimal(14,4) NOT NULL DEFAULT 0,
  `received_qty` decimal(14,4) DEFAULT 0,
  `cartons` int unsigned DEFAULT 0,
  `pallets` int unsigned DEFAULT NULL,
  `weight_kg` decimal(14,2) DEFAULT NULL,
  `volume_cbm` decimal(10,3) DEFAULT NULL,
  `notes` text,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_iasi_asn` (`asn_id`),
  KEY `idx_iasi_item` (`item_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
