
CREATE TABLE IF NOT EXISTS PO_companies (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 code VARCHAR(30) NOT NULL UNIQUE,
 name_ar VARCHAR(180) NOT NULL,
 name_en VARCHAR(180) NULL,
 active TINYINT(1) NOT NULL DEFAULT 1,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_locations (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NOT NULL,
 erp_code VARCHAR(50) NOT NULL,
 code VARCHAR(50) NOT NULL,
 name_ar VARCHAR(180) NOT NULL,
 name_en VARCHAR(180) NULL,
 type ENUM('STORE','CENTRAL_WAREHOUSE','WAREHOUSE','TRANSIT','OTHER') NOT NULL DEFAULT 'STORE',
 replenishment_source_location_id BIGINT UNSIGNED NULL,
 direct_vendor_delivery TINYINT(1) NOT NULL DEFAULT 0,
 active TINYINT(1) NOT NULL DEFAULT 1,
 last_erp_modified_at DATETIME NULL,
 last_synced_at DATETIME NULL,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 UNIQUE KEY uq_PO_locations_company_erp (company_id, erp_code),
 KEY idx_PO_locations_type (type),
 CONSTRAINT fk_PO_locations_company FOREIGN KEY (company_id) REFERENCES PO_companies(id),
 CONSTRAINT fk_PO_locations_source FOREIGN KEY (replenishment_source_location_id) REFERENCES PO_locations(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_categories (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NOT NULL,
 erp_code VARCHAR(50) NOT NULL,
 parent_id BIGINT UNSIGNED NULL,
 name_ar VARCHAR(180) NOT NULL,
 name_en VARCHAR(180) NULL,
 active TINYINT(1) NOT NULL DEFAULT 1,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 UNIQUE KEY uq_PO_categories_company_erp (company_id, erp_code),
 CONSTRAINT fk_PO_categories_company FOREIGN KEY (company_id) REFERENCES PO_companies(id),
 CONSTRAINT fk_PO_categories_parent FOREIGN KEY (parent_id) REFERENCES PO_categories(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_items (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NOT NULL,
 erp_item_no VARCHAR(60) NOT NULL,
 barcode VARCHAR(80) NULL,
 description_ar VARCHAR(255) NULL,
 description_en VARCHAR(255) NULL,
 category_id BIGINT UNSIGNED NULL,
 brand VARCHAR(120) NULL,
 base_uom VARCHAR(30) NULL,
 purchase_uom VARCHAR(30) NULL,
 pack_size DECIMAL(18,4) NOT NULL DEFAULT 1,
 shelf_life_days INT NULL,
 vat_percent DECIMAL(8,4) NOT NULL DEFAULT 0,
 unit_cost DECIMAL(18,6) NULL,
 last_purchase_cost DECIMAL(18,6) NULL,
 blocked TINYINT(1) NOT NULL DEFAULT 0,
 discontinued TINYINT(1) NOT NULL DEFAULT 0,
 seasonal TINYINT(1) NOT NULL DEFAULT 0,
 critical_item TINYINT(1) NOT NULL DEFAULT 0,
 abc_class CHAR(1) NULL,
 xyz_class CHAR(1) NULL,
 movement_class ENUM('FAST','NORMAL','SLOW','DEAD','NEW','SEASONAL') NULL,
 last_sale_date DATE NULL,
 last_purchase_date DATE NULL,
 last_erp_modified_at DATETIME NULL,
 last_synced_at DATETIME NULL,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 UNIQUE KEY uq_PO_items_company_item (company_id, erp_item_no),
 KEY idx_PO_items_barcode (barcode),
 KEY idx_PO_items_category (category_id),
 KEY idx_PO_items_movement (movement_class),
 CONSTRAINT fk_PO_items_company FOREIGN KEY (company_id) REFERENCES PO_companies(id),
 CONSTRAINT fk_PO_items_category FOREIGN KEY (category_id) REFERENCES PO_categories(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_vendors (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NOT NULL,
 erp_vendor_no VARCHAR(60) NOT NULL,
 name_ar VARCHAR(220) NULL,
 name_en VARCHAR(220) NULL,
 payment_terms_code VARCHAR(50) NULL,
 currency_code VARCHAR(10) NULL,
 default_lead_time_days INT NULL,
 minimum_order_value DECIMAL(18,4) NULL,
 auto_po_mode ENUM('MANUAL','ADVICE','AUTO_DRAFT','FULL_AUTO') NOT NULL DEFAULT 'AUTO_DRAFT',
 score DECIMAL(8,4) NULL,
 blocked TINYINT(1) NOT NULL DEFAULT 0,
 active TINYINT(1) NOT NULL DEFAULT 1,
 last_erp_modified_at DATETIME NULL,
 last_synced_at DATETIME NULL,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 UNIQUE KEY uq_PO_vendors_company_vendor (company_id, erp_vendor_no),
 CONSTRAINT fk_PO_vendors_company FOREIGN KEY (company_id) REFERENCES PO_companies(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_vendor_items (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NOT NULL,
 vendor_id BIGINT UNSIGNED NOT NULL,
 item_id BIGINT UNSIGNED NOT NULL,
 vendor_item_no VARCHAR(80) NULL,
 purchase_uom VARCHAR(30) NULL,
 pack_size DECIMAL(18,4) NOT NULL DEFAULT 1,
 moq DECIMAL(18,4) NOT NULL DEFAULT 0,
 order_multiple DECIMAL(18,4) NOT NULL DEFAULT 1,
 lead_time_days INT NULL,
 contract_price DECIMAL(18,6) NULL,
 current_price DECIMAL(18,6) NULL,
 preferred TINYINT(1) NOT NULL DEFAULT 0,
 ranking INT NOT NULL DEFAULT 100,
 active TINYINT(1) NOT NULL DEFAULT 1,
 last_erp_modified_at DATETIME NULL,
 last_synced_at DATETIME NULL,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 UNIQUE KEY uq_PO_vendor_items (company_id, vendor_id, item_id),
 KEY idx_PO_vendor_items_item (item_id),
 CONSTRAINT fk_PO_vendor_items_company FOREIGN KEY (company_id) REFERENCES PO_companies(id),
 CONSTRAINT fk_PO_vendor_items_vendor FOREIGN KEY (vendor_id) REFERENCES PO_vendors(id),
 CONSTRAINT fk_PO_vendor_items_item FOREIGN KEY (item_id) REFERENCES PO_items(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_inventory (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NOT NULL,
 location_id BIGINT UNSIGNED NOT NULL,
 item_id BIGINT UNSIGNED NOT NULL,
 on_hand DECIMAL(18,4) NOT NULL DEFAULT 0,
 reserved_qty DECIMAL(18,4) NOT NULL DEFAULT 0,
 available_qty DECIMAL(18,4) NOT NULL DEFAULT 0,
 qty_on_sales_order DECIMAL(18,4) NOT NULL DEFAULT 0,
 qty_on_purchase_order DECIMAL(18,4) NOT NULL DEFAULT 0,
 qty_in_transfer_in DECIMAL(18,4) NOT NULL DEFAULT 0,
 qty_in_transfer_out DECIMAL(18,4) NOT NULL DEFAULT 0,
 last_stock_change_at DATETIME NULL,
 last_synced_at DATETIME NULL,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 UNIQUE KEY uq_PO_inventory (company_id, location_id, item_id),
 KEY idx_PO_inventory_item_location (item_id, location_id),
 CONSTRAINT fk_PO_inventory_company FOREIGN KEY (company_id) REFERENCES PO_companies(id),
 CONSTRAINT fk_PO_inventory_location FOREIGN KEY (location_id) REFERENCES PO_locations(id),
 CONSTRAINT fk_PO_inventory_item FOREIGN KEY (item_id) REFERENCES PO_items(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_inventory_snapshots (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NOT NULL,
 location_id BIGINT UNSIGNED NOT NULL,
 item_id BIGINT UNSIGNED NOT NULL,
 snapshot_at DATETIME NOT NULL,
 on_hand DECIMAL(18,4) NOT NULL,
 available_qty DECIMAL(18,4) NOT NULL,
 qty_on_purchase_order DECIMAL(18,4) NOT NULL DEFAULT 0,
 qty_in_transfer_in DECIMAL(18,4) NOT NULL DEFAULT 0,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 KEY idx_PO_inv_snap_item_loc_date (item_id, location_id, snapshot_at),
 CONSTRAINT fk_PO_inv_snap_company FOREIGN KEY (company_id) REFERENCES PO_companies(id),
 CONSTRAINT fk_PO_inv_snap_location FOREIGN KEY (location_id) REFERENCES PO_locations(id),
 CONSTRAINT fk_PO_inv_snap_item FOREIGN KEY (item_id) REFERENCES PO_items(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_sales_daily (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NOT NULL,
 location_id BIGINT UNSIGNED NOT NULL,
 item_id BIGINT UNSIGNED NOT NULL,
 sales_date DATE NOT NULL,
 qty_sold DECIMAL(18,4) NOT NULL DEFAULT 0,
 qty_returned DECIMAL(18,4) NOT NULL DEFAULT 0,
 net_qty DECIMAL(18,4) NOT NULL DEFAULT 0,
 net_sales DECIMAL(18,4) NOT NULL DEFAULT 0,
 cost_amount DECIMAL(18,4) NOT NULL DEFAULT 0,
 stockout_minutes INT NOT NULL DEFAULT 0,
 in_stock_minutes INT NOT NULL DEFAULT 1440,
 transaction_count INT NOT NULL DEFAULT 0,
 last_synced_at DATETIME NULL,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 UNIQUE KEY uq_PO_sales_daily (company_id, location_id, item_id, sales_date),
 KEY idx_PO_sales_daily_period (sales_date, location_id, item_id),
 CONSTRAINT fk_PO_sales_company FOREIGN KEY (company_id) REFERENCES PO_companies(id),
 CONSTRAINT fk_PO_sales_location FOREIGN KEY (location_id) REFERENCES PO_locations(id),
 CONSTRAINT fk_PO_sales_item FOREIGN KEY (item_id) REFERENCES PO_items(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_calendar_days (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 calendar_date DATE NOT NULL UNIQUE,
 hijri_year SMALLINT NULL,
 hijri_month TINYINT NULL,
 hijri_day TINYINT NULL,
 hijri_month_name_ar VARCHAR(40) NULL,
 weekday_no TINYINT NOT NULL,
 is_weekend TINYINT(1) NOT NULL DEFAULT 0,
 is_ramadan TINYINT(1) NOT NULL DEFAULT 0,
 ramadan_day TINYINT NULL,
 is_eid_fitr TINYINT(1) NOT NULL DEFAULT 0,
 is_eid_adha TINYINT(1) NOT NULL DEFAULT 0,
 is_hajj_period TINYINT(1) NOT NULL DEFAULT 0,
 is_salary_period TINYINT(1) NOT NULL DEFAULT 0,
 is_school_holiday TINYINT(1) NOT NULL DEFAULT 0,
 is_national_event TINYINT(1) NOT NULL DEFAULT 0,
 event_code VARCHAR(60) NULL,
 event_name_ar VARCHAR(180) NULL,
 event_name_en VARCHAR(180) NULL,
 demand_weight DECIMAL(8,4) NOT NULL DEFAULT 1.0000,
 source VARCHAR(50) NULL,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 KEY idx_PO_calendar_hijri (hijri_month, hijri_day),
 KEY idx_PO_calendar_event (event_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_replenishment_profiles (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NOT NULL,
 name VARCHAR(120) NOT NULL,
 strategy ENUM('LAST_N_DAYS','WEIGHTED_PERIODS','MIN_MAX','DAYS_COVER','SAME_HIJRI_PERIOD','SEASONAL_AI','AUTO') NOT NULL DEFAULT 'AUTO',
 lookback_days INT NOT NULL DEFAULT 15,
 short_window_days INT NOT NULL DEFAULT 7,
 medium_window_days INT NOT NULL DEFAULT 15,
 long_window_days INT NOT NULL DEFAULT 30,
 target_cover_days DECIMAL(8,2) NOT NULL DEFAULT 21,
 safety_stock_days DECIMAL(8,2) NOT NULL DEFAULT 5,
 use_hijri_seasonality TINYINT(1) NOT NULL DEFAULT 1,
 use_stockout_correction TINYINT(1) NOT NULL DEFAULT 1,
 use_other_location_excess TINYINT(1) NOT NULL DEFAULT 1,
 max_cover_days DECIMAL(8,2) NOT NULL DEFAULT 45,
 auto_po_allowed TINYINT(1) NOT NULL DEFAULT 0,
 active TINYINT(1) NOT NULL DEFAULT 1,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 CONSTRAINT fk_PO_repl_profile_company FOREIGN KEY (company_id) REFERENCES PO_companies(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_replenishment_rules (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NOT NULL,
 profile_id BIGINT UNSIGNED NOT NULL,
 item_id BIGINT UNSIGNED NULL,
 category_id BIGINT UNSIGNED NULL,
 vendor_id BIGINT UNSIGNED NULL,
 location_id BIGINT UNSIGNED NULL,
 min_qty DECIMAL(18,4) NULL,
 max_qty DECIMAL(18,4) NULL,
 reorder_point DECIMAL(18,4) NULL,
 target_cover_days DECIMAL(8,2) NULL,
 safety_stock_days DECIMAL(8,2) NULL,
 priority INT NOT NULL DEFAULT 100,
 active TINYINT(1) NOT NULL DEFAULT 1,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 KEY idx_PO_repl_rules_scope (company_id, item_id, category_id, vendor_id, location_id, priority),
 CONSTRAINT fk_PO_repl_rules_company FOREIGN KEY (company_id) REFERENCES PO_companies(id),
 CONSTRAINT fk_PO_repl_rules_profile FOREIGN KEY (profile_id) REFERENCES PO_replenishment_profiles(id),
 CONSTRAINT fk_PO_repl_rules_item FOREIGN KEY (item_id) REFERENCES PO_items(id),
 CONSTRAINT fk_PO_repl_rules_category FOREIGN KEY (category_id) REFERENCES PO_categories(id),
 CONSTRAINT fk_PO_repl_rules_vendor FOREIGN KEY (vendor_id) REFERENCES PO_vendors(id),
 CONSTRAINT fk_PO_repl_rules_location FOREIGN KEY (location_id) REFERENCES PO_locations(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_forecasts (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NOT NULL,
 location_id BIGINT UNSIGNED NOT NULL,
 item_id BIGINT UNSIGNED NOT NULL,
 forecast_date DATE NOT NULL,
 model_code VARCHAR(60) NOT NULL,
 base_daily_demand DECIMAL(18,6) NOT NULL DEFAULT 0,
 seasonal_factor DECIMAL(12,6) NOT NULL DEFAULT 1,
 event_factor DECIMAL(12,6) NOT NULL DEFAULT 1,
 trend_factor DECIMAL(12,6) NOT NULL DEFAULT 1,
 forecast_qty DECIMAL(18,4) NOT NULL DEFAULT 0,
 confidence DECIMAL(8,4) NULL,
 actual_qty DECIMAL(18,4) NULL,
 absolute_error DECIMAL(18,4) NULL,
 percentage_error DECIMAL(12,4) NULL,
 generated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 UNIQUE KEY uq_PO_forecast (company_id, location_id, item_id, forecast_date, model_code),
 KEY idx_PO_forecast_date (forecast_date),
 CONSTRAINT fk_PO_forecast_company FOREIGN KEY (company_id) REFERENCES PO_companies(id),
 CONSTRAINT fk_PO_forecast_location FOREIGN KEY (location_id) REFERENCES PO_locations(id),
 CONSTRAINT fk_PO_forecast_item FOREIGN KEY (item_id) REFERENCES PO_items(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_purchase_history (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NOT NULL,
 erp_document_no VARCHAR(60) NOT NULL,
 erp_line_no INT NOT NULL,
 vendor_id BIGINT UNSIGNED NOT NULL,
 item_id BIGINT UNSIGNED NOT NULL,
 location_id BIGINT UNSIGNED NULL,
 document_date DATE NOT NULL,
 quantity DECIMAL(18,4) NOT NULL,
 received_quantity DECIMAL(18,4) NOT NULL DEFAULT 0,
 unit_cost DECIMAL(18,6) NOT NULL DEFAULT 0,
 discount_amount DECIMAL(18,4) NOT NULL DEFAULT 0,
 net_amount DECIMAL(18,4) NOT NULL DEFAULT 0,
 expected_receipt_date DATE NULL,
 actual_receipt_date DATE NULL,
 last_synced_at DATETIME NULL,
 UNIQUE KEY uq_PO_purchase_hist (company_id, erp_document_no, erp_line_no),
 KEY idx_PO_purchase_hist_vendor_date (vendor_id, document_date),
 KEY idx_PO_purchase_hist_item_date (item_id, document_date),
 CONSTRAINT fk_PO_purchase_hist_company FOREIGN KEY (company_id) REFERENCES PO_companies(id),
 CONSTRAINT fk_PO_purchase_hist_vendor FOREIGN KEY (vendor_id) REFERENCES PO_vendors(id),
 CONSTRAINT fk_PO_purchase_hist_item FOREIGN KEY (item_id) REFERENCES PO_items(id),
 CONSTRAINT fk_PO_purchase_hist_location FOREIGN KEY (location_id) REFERENCES PO_locations(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_open_erp_purchase_orders (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NOT NULL,
 erp_document_no VARCHAR(60) NOT NULL,
 erp_line_no INT NOT NULL,
 vendor_id BIGINT UNSIGNED NOT NULL,
 item_id BIGINT UNSIGNED NOT NULL,
 location_id BIGINT UNSIGNED NULL,
 order_date DATE NOT NULL,
 expected_receipt_date DATE NULL,
 quantity DECIMAL(18,4) NOT NULL,
 outstanding_quantity DECIMAL(18,4) NOT NULL,
 unit_cost DECIMAL(18,6) NULL,
 status VARCHAR(30) NULL,
 last_synced_at DATETIME NULL,
 UNIQUE KEY uq_PO_open_erp_po (company_id, erp_document_no, erp_line_no),
 KEY idx_PO_open_po_item_loc (item_id, location_id),
 CONSTRAINT fk_PO_open_po_company FOREIGN KEY (company_id) REFERENCES PO_companies(id),
 CONSTRAINT fk_PO_open_po_vendor FOREIGN KEY (vendor_id) REFERENCES PO_vendors(id),
 CONSTRAINT fk_PO_open_po_item FOREIGN KEY (item_id) REFERENCES PO_items(id),
 CONSTRAINT fk_PO_open_po_location FOREIGN KEY (location_id) REFERENCES PO_locations(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_transfers (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NOT NULL,
 erp_document_no VARCHAR(60) NULL,
 source_location_id BIGINT UNSIGNED NOT NULL,
 destination_location_id BIGINT UNSIGNED NOT NULL,
 item_id BIGINT UNSIGNED NOT NULL,
 quantity DECIMAL(18,4) NOT NULL,
 outstanding_quantity DECIMAL(18,4) NOT NULL DEFAULT 0,
 status ENUM('SUGGESTED','APPROVED','SENT_TO_ERP','IN_TRANSIT','RECEIVED','CANCELLED') NOT NULL DEFAULT 'SUGGESTED',
 source ENUM('ERP','SYSTEM','AI') NOT NULL DEFAULT 'ERP',
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 KEY idx_PO_transfers_item_status (item_id, status),
 CONSTRAINT fk_PO_transfers_company FOREIGN KEY (company_id) REFERENCES PO_companies(id),
 CONSTRAINT fk_PO_transfers_source FOREIGN KEY (source_location_id) REFERENCES PO_locations(id),
 CONSTRAINT fk_PO_transfers_dest FOREIGN KEY (destination_location_id) REFERENCES PO_locations(id),
 CONSTRAINT fk_PO_transfers_item FOREIGN KEY (item_id) REFERENCES PO_items(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_vendor_offers (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NOT NULL,
 vendor_id BIGINT UNSIGNED NOT NULL,
 item_id BIGINT UNSIGNED NULL,
 offer_code VARCHAR(80) NULL,
 start_date DATE NOT NULL,
 end_date DATE NOT NULL,
 offer_type ENUM('PRICE','DISCOUNT','BUY_GET','REBATE','BONUS_QTY') NOT NULL,
 min_qty DECIMAL(18,4) NULL,
 buy_qty DECIMAL(18,4) NULL,
 free_qty DECIMAL(18,4) NULL,
 discount_percent DECIMAL(8,4) NULL,
 offer_price DECIMAL(18,6) NULL,
 active TINYINT(1) NOT NULL DEFAULT 1,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 KEY idx_PO_vendor_offers_active (vendor_id, item_id, start_date, end_date),
 CONSTRAINT fk_PO_vendor_offers_company FOREIGN KEY (company_id) REFERENCES PO_companies(id),
 CONSTRAINT fk_PO_vendor_offers_vendor FOREIGN KEY (vendor_id) REFERENCES PO_vendors(id),
 CONSTRAINT fk_PO_vendor_offers_item FOREIGN KEY (item_id) REFERENCES PO_items(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_replenishment_runs (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NOT NULL,
 run_code VARCHAR(60) NOT NULL UNIQUE,
 requested_period_days INT NULL,
 mode ENUM('MANUAL','SCHEDULED','SIMULATION') NOT NULL DEFAULT 'SCHEDULED',
 status ENUM('QUEUED','RUNNING','COMPLETED','FAILED','PARTIAL') NOT NULL DEFAULT 'QUEUED',
 started_at DATETIME NULL,
 finished_at DATETIME NULL,
 items_analyzed INT NOT NULL DEFAULT 0,
 recommendations_count INT NOT NULL DEFAULT 0,
 purchase_value DECIMAL(18,4) NOT NULL DEFAULT 0,
 transfer_value_avoided DECIMAL(18,4) NOT NULL DEFAULT 0,
 error_message TEXT NULL,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 CONSTRAINT fk_PO_repl_runs_company FOREIGN KEY (company_id) REFERENCES PO_companies(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_replenishment_recommendations (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 run_id BIGINT UNSIGNED NOT NULL,
 company_id BIGINT UNSIGNED NOT NULL,
 location_id BIGINT UNSIGNED NOT NULL,
 item_id BIGINT UNSIGNED NOT NULL,
 vendor_id BIGINT UNSIGNED NULL,
 action ENUM('BUY','TRANSFER','DO_NOT_ORDER','REDUCE','INCREASE','PROMOTION','PRICE_REVIEW','VENDOR_REVIEW') NOT NULL,
 analysis_days INT NOT NULL,
 avg_daily_sales DECIMAL(18,6) NOT NULL DEFAULT 0,
 corrected_daily_demand DECIMAL(18,6) NOT NULL DEFAULT 0,
 forecast_daily_demand DECIMAL(18,6) NOT NULL DEFAULT 0,
 seasonal_factor DECIMAL(12,6) NOT NULL DEFAULT 1,
 current_available DECIMAL(18,4) NOT NULL DEFAULT 0,
 incoming_po DECIMAL(18,4) NOT NULL DEFAULT 0,
 incoming_transfer DECIMAL(18,4) NOT NULL DEFAULT 0,
 target_stock DECIMAL(18,4) NOT NULL DEFAULT 0,
 raw_suggested_qty DECIMAL(18,4) NOT NULL DEFAULT 0,
 final_suggested_qty DECIMAL(18,4) NOT NULL DEFAULT 0,
 transfer_source_location_id BIGINT UNSIGNED NULL,
 transfer_suggested_qty DECIMAL(18,4) NOT NULL DEFAULT 0,
 days_cover_before DECIMAL(12,4) NULL,
 days_cover_after DECIMAL(12,4) NULL,
 confidence DECIMAL(8,4) NULL,
 risk_level ENUM('LOW','MEDIUM','HIGH','CRITICAL') NOT NULL DEFAULT 'LOW',
 reason_ar TEXT NULL,
 reason_en TEXT NULL,
 buyer_override_qty DECIMAL(18,4) NULL,
 buyer_override_reason VARCHAR(255) NULL,
 status ENUM('NEW','ACCEPTED','OVERRIDDEN','REJECTED','CONVERTED_TO_PO','CONVERTED_TO_TRANSFER') NOT NULL DEFAULT 'NEW',
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 KEY idx_PO_repl_reco_run_vendor (run_id, vendor_id),
 KEY idx_PO_repl_reco_item_loc (item_id, location_id),
 CONSTRAINT fk_PO_repl_reco_run FOREIGN KEY (run_id) REFERENCES PO_replenishment_runs(id),
 CONSTRAINT fk_PO_repl_reco_company FOREIGN KEY (company_id) REFERENCES PO_companies(id),
 CONSTRAINT fk_PO_repl_reco_location FOREIGN KEY (location_id) REFERENCES PO_locations(id),
 CONSTRAINT fk_PO_repl_reco_item FOREIGN KEY (item_id) REFERENCES PO_items(id),
 CONSTRAINT fk_PO_repl_reco_vendor FOREIGN KEY (vendor_id) REFERENCES PO_vendors(id),
 CONSTRAINT fk_PO_repl_reco_transfer_source FOREIGN KEY (transfer_source_location_id) REFERENCES PO_locations(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_purchase_orders (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NOT NULL,
 system_po_no VARCHAR(60) NOT NULL UNIQUE,
 erp_po_no VARCHAR(60) NULL,
 vendor_id BIGINT UNSIGNED NOT NULL,
 location_id BIGINT UNSIGNED NULL,
 order_date DATE NOT NULL,
 expected_receipt_date DATE NULL,
 currency_code VARCHAR(10) NULL,
 subtotal DECIMAL(18,4) NOT NULL DEFAULT 0,
 discount_amount DECIMAL(18,4) NOT NULL DEFAULT 0,
 vat_amount DECIMAL(18,4) NOT NULL DEFAULT 0,
 total_amount DECIMAL(18,4) NOT NULL DEFAULT 0,
 source ENUM('MANUAL','REPLENISHMENT','AI','IMPORT') NOT NULL DEFAULT 'REPLENISHMENT',
 status ENUM('DRAFT','PENDING_APPROVAL','APPROVED','QUEUED_ERP','ERP_CREATED','ERP_FAILED','SENT_VENDOR','PARTIAL_RECEIPT','RECEIVED','CANCELLED') NOT NULL DEFAULT 'DRAFT',
 auto_created TINYINT(1) NOT NULL DEFAULT 0,
 approval_required TINYINT(1) NOT NULL DEFAULT 1,
 notes TEXT NULL,
 erp_error TEXT NULL,
 approved_at DATETIME NULL,
 erp_created_at DATETIME NULL,
 created_by BIGINT UNSIGNED NULL,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 KEY idx_PO_orders_vendor_status (vendor_id, status),
 CONSTRAINT fk_PO_orders_company FOREIGN KEY (company_id) REFERENCES PO_companies(id),
 CONSTRAINT fk_PO_orders_vendor FOREIGN KEY (vendor_id) REFERENCES PO_vendors(id),
 CONSTRAINT fk_PO_orders_location FOREIGN KEY (location_id) REFERENCES PO_locations(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_purchase_order_lines (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 purchase_order_id BIGINT UNSIGNED NOT NULL,
 line_no INT NOT NULL,
 item_id BIGINT UNSIGNED NOT NULL,
 location_id BIGINT UNSIGNED NULL,
 vendor_item_no VARCHAR(80) NULL,
 uom VARCHAR(30) NULL,
 quantity DECIMAL(18,4) NOT NULL,
 free_quantity DECIMAL(18,4) NOT NULL DEFAULT 0,
 unit_cost DECIMAL(18,6) NOT NULL DEFAULT 0,
 discount_percent DECIMAL(8,4) NOT NULL DEFAULT 0,
 discount_amount DECIMAL(18,4) NOT NULL DEFAULT 0,
 vat_percent DECIMAL(8,4) NOT NULL DEFAULT 0,
 line_amount DECIMAL(18,4) NOT NULL DEFAULT 0,
 recommendation_id BIGINT UNSIGNED NULL,
 validation_status ENUM('PASSED','WARNING','BLOCKED') NOT NULL DEFAULT 'PASSED',
 validation_message TEXT NULL,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 UNIQUE KEY uq_PO_order_lines (purchase_order_id, line_no),
 KEY idx_PO_order_lines_item (item_id),
 CONSTRAINT fk_PO_order_lines_order FOREIGN KEY (purchase_order_id) REFERENCES PO_purchase_orders(id),
 CONSTRAINT fk_PO_order_lines_item FOREIGN KEY (item_id) REFERENCES PO_items(id),
 CONSTRAINT fk_PO_order_lines_location FOREIGN KEY (location_id) REFERENCES PO_locations(id),
 CONSTRAINT fk_PO_order_lines_reco FOREIGN KEY (recommendation_id) REFERENCES PO_replenishment_recommendations(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_approval_rules (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NOT NULL,
 name VARCHAR(120) NOT NULL,
 min_amount DECIMAL(18,4) NULL,
 max_amount DECIMAL(18,4) NULL,
 price_increase_percent DECIMAL(8,4) NULL,
 risk_level VARCHAR(20) NULL,
 required_role VARCHAR(80) NOT NULL,
 sequence_no INT NOT NULL DEFAULT 1,
 active TINYINT(1) NOT NULL DEFAULT 1,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 CONSTRAINT fk_PO_approval_rules_company FOREIGN KEY (company_id) REFERENCES PO_companies(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_approvals (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 purchase_order_id BIGINT UNSIGNED NOT NULL,
 rule_id BIGINT UNSIGNED NULL,
 sequence_no INT NOT NULL DEFAULT 1,
 approver_user_id BIGINT UNSIGNED NULL,
 approver_role VARCHAR(80) NULL,
 status ENUM('PENDING','APPROVED','REJECTED','SKIPPED') NOT NULL DEFAULT 'PENDING',
 comment TEXT NULL,
 action_at DATETIME NULL,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 CONSTRAINT fk_PO_approvals_order FOREIGN KEY (purchase_order_id) REFERENCES PO_purchase_orders(id),
 CONSTRAINT fk_PO_approvals_rule FOREIGN KEY (rule_id) REFERENCES PO_approval_rules(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_sync_endpoints (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 code VARCHAR(60) NOT NULL UNIQUE,
 name VARCHAR(120) NOT NULL,
 direction ENUM('ERP_TO_SYSTEM','SYSTEM_TO_ERP') NOT NULL,
 base_url VARCHAR(500) NOT NULL,
 path VARCHAR(500) NOT NULL,
 http_method VARCHAR(10) NOT NULL DEFAULT 'GET',
 auth_type ENUM('NONE','BASIC','BEARER','API_KEY','NTLM') NOT NULL DEFAULT 'BASIC',
 auth_config_json JSON NULL,
 page_size INT NOT NULL DEFAULT 1000,
 timeout_seconds INT NOT NULL DEFAULT 60,
 sync_interval_minutes INT NOT NULL DEFAULT 15,
 incremental_field VARCHAR(80) NULL,
 enabled TINYINT(1) NOT NULL DEFAULT 1,
 priority INT NOT NULL DEFAULT 100,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_sync_state (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 endpoint_id BIGINT UNSIGNED NOT NULL,
 company_id BIGINT UNSIGNED NULL,
 last_success_at DATETIME NULL,
 last_attempt_at DATETIME NULL,
 last_cursor VARCHAR(500) NULL,
 last_erp_modified_at DATETIME NULL,
 rows_last_sync INT NOT NULL DEFAULT 0,
 total_rows_synced BIGINT NOT NULL DEFAULT 0,
 consecutive_failures INT NOT NULL DEFAULT 0,
 last_error TEXT NULL,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 UNIQUE KEY uq_PO_sync_state (endpoint_id, company_id),
 CONSTRAINT fk_PO_sync_state_endpoint FOREIGN KEY (endpoint_id) REFERENCES PO_sync_endpoints(id),
 CONSTRAINT fk_PO_sync_state_company FOREIGN KEY (company_id) REFERENCES PO_companies(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_sync_jobs (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 endpoint_id BIGINT UNSIGNED NOT NULL,
 company_id BIGINT UNSIGNED NULL,
 status ENUM('QUEUED','RUNNING','COMPLETED','FAILED','PARTIAL') NOT NULL DEFAULT 'QUEUED',
 requested_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 started_at DATETIME NULL,
 finished_at DATETIME NULL,
 cursor_from VARCHAR(500) NULL,
 cursor_to VARCHAR(500) NULL,
 rows_received INT NOT NULL DEFAULT 0,
 rows_inserted INT NOT NULL DEFAULT 0,
 rows_updated INT NOT NULL DEFAULT 0,
 rows_failed INT NOT NULL DEFAULT 0,
 error_message TEXT NULL,
 retry_count INT NOT NULL DEFAULT 0,
 KEY idx_PO_sync_jobs_status (status, requested_at),
 CONSTRAINT fk_PO_sync_jobs_endpoint FOREIGN KEY (endpoint_id) REFERENCES PO_sync_endpoints(id),
 CONSTRAINT fk_PO_sync_jobs_company FOREIGN KEY (company_id) REFERENCES PO_companies(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_erp_outbound_queue (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NOT NULL,
 entity_type ENUM('PURCHASE_ORDER','TRANSFER_ORDER','OTHER') NOT NULL,
 entity_id BIGINT UNSIGNED NOT NULL,
 endpoint_code VARCHAR(60) NOT NULL,
 idempotency_key VARCHAR(120) NOT NULL UNIQUE,
 payload_json JSON NOT NULL,
 status ENUM('PENDING','PROCESSING','SUCCESS','FAILED','DEAD') NOT NULL DEFAULT 'PENDING',
 attempts INT NOT NULL DEFAULT 0,
 next_attempt_at DATETIME NULL,
 response_code INT NULL,
 response_body MEDIUMTEXT NULL,
 erp_document_no VARCHAR(80) NULL,
 last_error TEXT NULL,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 KEY idx_PO_outbound_status (status, next_attempt_at),
 CONSTRAINT fk_PO_outbound_company FOREIGN KEY (company_id) REFERENCES PO_companies(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_users (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NULL,
 username VARCHAR(80) NOT NULL UNIQUE,
 full_name VARCHAR(180) NOT NULL,
 email VARCHAR(180) NULL,
 password_hash VARCHAR(255) NOT NULL,
 role VARCHAR(80) NOT NULL DEFAULT 'BUYER',
 active TINYINT(1) NOT NULL DEFAULT 1,
 last_login_at DATETIME NULL,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 CONSTRAINT fk_PO_users_company FOREIGN KEY (company_id) REFERENCES PO_companies(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_audit_log (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NULL,
 user_id BIGINT UNSIGNED NULL,
 action VARCHAR(100) NOT NULL,
 entity_type VARCHAR(100) NULL,
 entity_id BIGINT UNSIGNED NULL,
 old_values JSON NULL,
 new_values JSON NULL,
 ip_address VARCHAR(64) NULL,
 user_agent VARCHAR(500) NULL,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 KEY idx_PO_audit_entity (entity_type, entity_id, created_at),
 KEY idx_PO_audit_user (user_id, created_at),
 CONSTRAINT fk_PO_audit_company FOREIGN KEY (company_id) REFERENCES PO_companies(id),
 CONSTRAINT fk_PO_audit_user FOREIGN KEY (user_id) REFERENCES PO_users(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS PO_system_settings (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 company_id BIGINT UNSIGNED NULL,
 setting_key VARCHAR(120) NOT NULL,
 setting_value TEXT NULL,
 value_type ENUM('STRING','INT','DECIMAL','BOOL','JSON') NOT NULL DEFAULT 'STRING',
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 UNIQUE KEY uq_PO_settings (company_id, setting_key),
 CONSTRAINT fk_PO_settings_company FOREIGN KEY (company_id) REFERENCES PO_companies(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT IGNORE INTO PO_companies(code,name_ar,name_en) VALUES ('DEFAULT','الشركة الافتراضية','Default Company');

INSERT IGNORE INTO PO_replenishment_profiles
(company_id,name,strategy,lookback_days,short_window_days,medium_window_days,long_window_days,target_cover_days,safety_stock_days,use_hijri_seasonality,use_stockout_correction,use_other_location_excess,max_cover_days,auto_po_allowed)
SELECT id,'Enterprise Auto Replenishment','AUTO',15,7,15,30,21,5,1,1,1,45,0 FROM PO_companies WHERE code='DEFAULT';

INSERT IGNORE INTO PO_sync_endpoints(code,name,direction,base_url,path,http_method,auth_type,page_size,sync_interval_minutes,incremental_field,priority) VALUES
('ERP_ITEMS','Items','ERP_TO_SYSTEM','http://nav-server','/api/po/items','GET','BASIC',1000,60,'LastModifiedDateTime',10),
('ERP_VENDORS','Vendors','ERP_TO_SYSTEM','http://nav-server','/api/po/vendors','GET','BASIC',1000,60,'LastModifiedDateTime',20),
('ERP_VENDOR_ITEMS','Vendor Items','ERP_TO_SYSTEM','http://nav-server','/api/po/vendor-items','GET','BASIC',1000,60,'LastModifiedDateTime',30),
('ERP_LOCATIONS','Locations','ERP_TO_SYSTEM','http://nav-server','/api/po/locations','GET','BASIC',500,60,'LastModifiedDateTime',40),
('ERP_INVENTORY','Inventory','ERP_TO_SYSTEM','http://nav-server','/api/po/inventory','GET','BASIC',2000,15,'LastModifiedDateTime',50),
('ERP_SALES_DAILY','Sales Daily','ERP_TO_SYSTEM','http://nav-server','/api/po/sales-daily','GET','BASIC',5000,15,'LastModifiedDateTime',60),
('ERP_PURCHASE_HISTORY','Purchase History','ERP_TO_SYSTEM','http://nav-server','/api/po/purchase-history','GET','BASIC',5000,60,'LastModifiedDateTime',70),
('ERP_OPEN_PO','Open Purchase Orders','ERP_TO_SYSTEM','http://nav-server','/api/po/open-purchase-orders','GET','BASIC',2000,15,'LastModifiedDateTime',80),
('ERP_TRANSFERS','Transfers','ERP_TO_SYSTEM','http://nav-server','/api/po/transfers','GET','BASIC',2000,15,'LastModifiedDateTime',90),
('ERP_OFFERS','Vendor Offers','ERP_TO_SYSTEM','http://nav-server','/api/po/vendor-offers','GET','BASIC',1000,60,'LastModifiedDateTime',100),
('ERP_CREATE_PO','Create Purchase Order','SYSTEM_TO_ERP','http://nav-server','/api/po/purchase-orders','POST','BASIC',100,0,NULL,10),
('ERP_CREATE_TRANSFER','Create Transfer Order','SYSTEM_TO_ERP','http://nav-server','/api/po/transfer-orders','POST','BASIC',100,0,NULL,20);

