-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "public";

-- CreateTable
CREATE TABLE "roles" (
    "id" BIGSERIAL NOT NULL,
    "code" VARCHAR(50) NOT NULL,
    "name" VARCHAR(100) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "roles_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "users" (
    "id" BIGSERIAL NOT NULL,
    "role_id" BIGINT NOT NULL,
    "name" VARCHAR(150) NOT NULL,
    "username" VARCHAR(50),
    "email" VARCHAR(150),
    "email_verified_at" TIMESTAMP(3),
    "phone" VARCHAR(50),
    "password_hash" VARCHAR(255),
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "users_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "audit_trails" (
    "id" BIGSERIAL NOT NULL,
    "user_id" BIGINT,
    "action" VARCHAR(100) NOT NULL,
    "entity_type" VARCHAR(100) NOT NULL,
    "entity_id" VARCHAR(100),
    "method" VARCHAR(10) NOT NULL,
    "pathname" VARCHAR(255) NOT NULL,
    "status_code" INTEGER NOT NULL,
    "summary" VARCHAR(255),
    "metadata" JSONB,
    "occurred_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "audit_trails_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "password_reset_tokens" (
    "id" BIGSERIAL NOT NULL,
    "user_id" BIGINT NOT NULL,
    "token_hash" VARCHAR(255) NOT NULL,
    "purpose" VARCHAR(50) NOT NULL DEFAULT '[REDACTED]TIMESTAMP(3) NOT NULL,
    "used_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "password_reset_tokens_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "email_verification_tokens" (
    "id" BIGSERIAL NOT NULL,
    "user_id" BIGINT NOT NULL,
    "token_hash" VARCHAR(255) NOT NULL,
    "expires_at" TIMESTAMP(3) NOT NULL,
    "used_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "email_verification_tokens_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "buyers" (
    "id" BIGSERIAL NOT NULL,
    "code" VARCHAR(50) NOT NULL,
    "name" VARCHAR(150) NOT NULL,
    "phone" VARCHAR(50),
    "email" VARCHAR(150),
    "bank_name" VARCHAR(100),
    "bank_account_number" VARCHAR(100),
    "address" TEXT,
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "buyers_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "buyer_contact_people" (
    "id" BIGSERIAL NOT NULL,
    "buyer_id" BIGINT NOT NULL,
    "name" VARCHAR(150) NOT NULL,
    "mobile_phone" VARCHAR(50),
    "email" VARCHAR(150),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "buyer_contact_people_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "employees" (
    "id" BIGSERIAL NOT NULL,
    "code" VARCHAR(20) NOT NULL,
    "name" VARCHAR(150) NOT NULL,
    "email" VARCHAR(150),
    "mobile" VARCHAR(50),
    "position" VARCHAR(150) NOT NULL,
    "address" TEXT,
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "employees_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "couriers" (
    "id" BIGSERIAL NOT NULL,
    "code" VARCHAR(20) NOT NULL,
    "name" VARCHAR(150) NOT NULL,
    "address" TEXT,
    "phone" VARCHAR(50),
    "website" VARCHAR(255),
    "email" VARCHAR(150),
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "couriers_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "washing_places" (
    "id" BIGSERIAL NOT NULL,
    "code" VARCHAR(20) NOT NULL,
    "name" VARCHAR(150) NOT NULL,
    "address" TEXT,
    "phone" VARCHAR(50),
    "website" VARCHAR(255),
    "email" VARCHAR(150),
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "washing_places_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "washing_place_contact_people" (
    "id" BIGSERIAL NOT NULL,
    "washing_place_id" BIGINT NOT NULL,
    "name" VARCHAR(150) NOT NULL,
    "mobile_phone" VARCHAR(50),
    "email" VARCHAR(150),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "washing_place_contact_people_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "grades" (
    "id" BIGSERIAL NOT NULL,
    "code" VARCHAR(20) NOT NULL,
    "legacy_code" VARCHAR(20),
    "is_mangkok" BOOLEAN NOT NULL DEFAULT false,
    "name" VARCHAR(150) NOT NULL,
    "description" TEXT,
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "grades_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "grade_buy_price_standards" (
    "id" BIGSERIAL NOT NULL,
    "grade_id" BIGINT NOT NULL,
    "start_date" DATE NOT NULL,
    "end_date" DATE,
    "min_price" DECIMAL(18,2) NOT NULL,
    "max_price" DECIMAL(18,2) NOT NULL,
    "notes" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "grade_buy_price_standards_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "grade_sell_price_standards" (
    "id" BIGSERIAL NOT NULL,
    "grade_id" BIGINT NOT NULL,
    "start_date" DATE NOT NULL,
    "end_date" DATE,
    "min_price" DECIMAL(18,2) NOT NULL,
    "max_price" DECIMAL(18,2) NOT NULL,
    "notes" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "grade_sell_price_standards_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "warehouses" (
    "id" BIGSERIAL NOT NULL,
    "code" VARCHAR(50) NOT NULL,
    "name" VARCHAR(100) NOT NULL,
    "address" TEXT,
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "warehouses_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "units" (
    "id" BIGSERIAL NOT NULL,
    "code" VARCHAR(20) NOT NULL,
    "name" VARCHAR(50) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "units_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "currencies" (
    "id" BIGSERIAL NOT NULL,
    "code" VARCHAR(10) NOT NULL,
    "name" VARCHAR(100) NOT NULL,
    "description" TEXT,
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "currencies_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "banks" (
    "id" BIGSERIAL NOT NULL,
    "code" VARCHAR(3) NOT NULL,
    "name" VARCHAR(150) NOT NULL,
    "address" TEXT,
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "banks_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "profit_share_schemes" (
    "id" BIGSERIAL NOT NULL,
    "code" VARCHAR(20) NOT NULL,
    "name" VARCHAR(150) NOT NULL,
    "share_agent" DECIMAL(5,2) NOT NULL,
    "share_company" DECIMAL(5,2) NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "profit_share_schemes_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "agents" (
    "id" BIGSERIAL NOT NULL,
    "code" VARCHAR(20) NOT NULL,
    "name" VARCHAR(150) NOT NULL,
    "identity_type" VARCHAR(20) NOT NULL,
    "identity_number" VARCHAR(100) NOT NULL,
    "mobile_phone" VARCHAR(50),
    "email" VARCHAR(150),
    "address" TEXT,
    "notes" TEXT,
    "join_date" DATE NOT NULL,
    "profit_share_scheme_id" BIGINT NOT NULL,
    "current_balance" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "capital_balance" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "agents_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "agent_bank_accounts" (
    "id" BIGSERIAL NOT NULL,
    "agent_id" BIGINT NOT NULL,
    "bank_id" BIGINT NOT NULL,
    "account_number" VARCHAR(100) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "agent_bank_accounts_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "agent_balance_mutations" (
    "id" BIGSERIAL NOT NULL,
    "agent_id" BIGINT NOT NULL,
    "balance_type" VARCHAR(30) NOT NULL,
    "direction" VARCHAR(10) NOT NULL,
    "source" VARCHAR(50) NOT NULL,
    "amount" DECIMAL(18,2) NOT NULL,
    "balance_after" DECIMAL(18,2) NOT NULL,
    "occurred_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "effective_date" DATE,
    "reference_type" VARCHAR(50),
    "reference_id" VARCHAR(100),
    "reference_no" VARCHAR(100),
    "notes" TEXT,
    "metadata" JSONB,

    CONSTRAINT "agent_balance_mutations_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "fund_requests" (
    "id" BIGSERIAL NOT NULL,
    "request_no" VARCHAR(50) NOT NULL,
    "reference_no" VARCHAR(100) NOT NULL,
    "transfer_type" VARCHAR(30) NOT NULL,
    "agent_id" BIGINT NOT NULL,
    "agent_bank_account_id" BIGINT NOT NULL,
    "company_bank_account_id" BIGINT,
    "agent_bank_name_snapshot" VARCHAR(150) NOT NULL,
    "agent_account_number_snapshot" VARCHAR(100) NOT NULL,
    "company_bank_name_snapshot" VARCHAR(150) NOT NULL,
    "company_account_number_snapshot" VARCHAR(100) NOT NULL,
    "amount" DECIMAL(18,2) NOT NULL,
    "currency_code" VARCHAR(10) NOT NULL DEFAULT 'IDR',
    "transferred_at" TIMESTAMP(3) NOT NULL,
    "transfer_proof_file_url" VARCHAR(255),
    "status" VARCHAR(20) NOT NULL DEFAULT 'SUBMITTED',
    "created_by" BIGINT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "fund_requests_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "sales" (
    "id" BIGSERIAL NOT NULL,
    "code" VARCHAR(20) NOT NULL,
    "name" VARCHAR(150) NOT NULL,
    "identity_type" VARCHAR(20) NOT NULL,
    "identity_number" VARCHAR(100) NOT NULL,
    "mobile_phone" VARCHAR(50),
    "email" VARCHAR(150),
    "address" TEXT,
    "notes" TEXT,
    "join_date" DATE NOT NULL,
    "commission_balance" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "sales_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "sales_commission_mutations" (
    "id" BIGSERIAL NOT NULL,
    "sales_id" BIGINT NOT NULL,
    "source" VARCHAR(50) NOT NULL,
    "direction" VARCHAR(10) NOT NULL,
    "amount" DECIMAL(18,2) NOT NULL,
    "balance_after" DECIMAL(18,2) NOT NULL,
    "occurred_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "effective_date" DATE,
    "reference_type" VARCHAR(50),
    "reference_id" VARCHAR(100),
    "reference_no" VARCHAR(100),
    "notes" TEXT,
    "metadata" JSONB,

    CONSTRAINT "sales_commission_mutations_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "sales_bank_accounts" (
    "id" BIGSERIAL NOT NULL,
    "sales_id" BIGINT NOT NULL,
    "bank_id" BIGINT NOT NULL,
    "account_number" VARCHAR(100) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "sales_bank_accounts_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "warehouse_locations" (
    "id" BIGSERIAL NOT NULL,
    "warehouse_id" BIGINT NOT NULL,
    "code" VARCHAR(50) NOT NULL,
    "name" VARCHAR(100) NOT NULL,
    "location_type" VARCHAR(50),
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "warehouse_locations_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "adjustment_reasons" (
    "id" BIGSERIAL NOT NULL,
    "code" VARCHAR(50) NOT NULL,
    "name" VARCHAR(100) NOT NULL,
    "category" VARCHAR(50) NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "adjustment_reasons_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "purchases" (
    "id" BIGSERIAL NOT NULL,
    "purchase_no" VARCHAR(50) NOT NULL,
    "purchase_type" VARCHAR(30) NOT NULL DEFAULT 'REGULAR',
    "purchase_date" DATE NOT NULL,
    "supplier_invoice_no" VARCHAR(100),
    "agent_id" BIGINT,
    "buyout_source_agent_id" BIGINT,
    "profit_share_scheme_id" BIGINT,
    "courier_id" BIGINT,
    "received_by_employee_id" BIGINT,
    "received_at" TIMESTAMP(3),
    "moisture_buy_percent" DECIMAL(7,2),
    "moisture_received_percent" DECIMAL(7,2),
    "above_average_ratio_percent" DECIMAL(7,2),
    "mk_share_percent" DECIMAL(7,2),
    "non_mk_share_percent" DECIMAL(7,2),
    "shipping_cost" DECIMAL(18,2),
    "incoming_operational_cost" DECIMAL(18,2),
    "after_arrival_operational_cost" DECIMAL(18,2),
    "status" VARCHAR(20) NOT NULL DEFAULT 'DRAFT',
    "notes" TEXT,
    "created_by" BIGINT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "purchases_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "purchase_lines" (
    "id" BIGSERIAL NOT NULL,
    "purchase_id" BIGINT NOT NULL,
    "grade_id" BIGINT,
    "source_lot_id" BIGINT,
    "qty_ordered" DECIMAL(18,3) NOT NULL,
    "purchase_moisture_percent" DECIMAL(7,2),
    "qty_received" DECIMAL(18,3) NOT NULL DEFAULT 0,
    "qty_accepted" DECIMAL(18,3) NOT NULL DEFAULT 0,
    "qty_rejected" DECIMAL(18,3) NOT NULL,
    "moisture_received_percent" DECIMAL(7,2),
    "unit_id" BIGINT NOT NULL,
    "unit_price" DECIMAL(18,2) NOT NULL,
    "buyout_mal_unit_price_snapshot" DECIMAL(18,2),
    "buyout_agent_share_percent" DECIMAL(5,2),
    "buyout_profit_amount" DECIMAL(18,2),
    "buyout_agent_commission" DECIMAL(18,2),
    "market_reference_price" DECIMAL(18,2),
    "unit_cost" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "mal_unit_price" DECIMAL(18,2),
    "subtotal" DECIMAL(18,2) NOT NULL,
    "classification_status" VARCHAR(20) NOT NULL DEFAULT 'FINAL',
    "warehouse_id" BIGINT,
    "warehouse_location_id" BIGINT,
    "notes" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "purchase_lines_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "receipts" (
    "id" BIGSERIAL NOT NULL,
    "receipt_no" VARCHAR(50) NOT NULL,
    "purchase_id" BIGINT NOT NULL,
    "receipt_date" DATE NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'DRAFT',
    "notes" TEXT,
    "received_by" BIGINT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "receipts_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "receipt_lines" (
    "id" BIGSERIAL NOT NULL,
    "receipt_id" BIGINT NOT NULL,
    "purchase_line_id" BIGINT NOT NULL,
    "grade_id" BIGINT,
    "qty_received" DECIMAL(18,3) NOT NULL,
    "qty_accepted" DECIMAL(18,3) NOT NULL,
    "qty_rejected" DECIMAL(18,3) NOT NULL DEFAULT 0,
    "moisture_percent" DECIMAL(7,2),
    "unit_id" BIGINT NOT NULL,
    "unit_cost" DECIMAL(18,2) NOT NULL,
    "warehouse_id" BIGINT NOT NULL,
    "warehouse_location_id" BIGINT,
    "notes" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "receipt_lines_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "inventory_lots" (
    "id" BIGSERIAL NOT NULL,
    "lot_code" VARCHAR(100) NOT NULL,
    "parent_lot_id" BIGINT,
    "source_type" VARCHAR(30) NOT NULL,
    "source_ref_id" BIGINT,
    "purchase_id" BIGINT,
    "purchase_line_id" BIGINT,
    "receipt_id" BIGINT,
    "receipt_line_id" BIGINT,
    "grade_id" BIGINT,
    "warehouse_id" BIGINT NOT NULL,
    "warehouse_location_id" BIGINT,
    "original_qty" DECIMAL(18,3) NOT NULL,
    "available_qty" DECIMAL(18,3) NOT NULL,
    "reserved_qty" DECIMAL(18,3) NOT NULL DEFAULT 0,
    "damaged_qty" DECIMAL(18,3) NOT NULL DEFAULT 0,
    "shrinkage_qty" DECIMAL(18,3) NOT NULL DEFAULT 0,
    "final_moisture_percent" DECIMAL(7,2),
    "above_average_ratio_percent" DECIMAL(7,2),
    "unit_id" BIGINT NOT NULL,
    "unit_cost" DECIMAL(18,2) NOT NULL,
    "received_at" TIMESTAMP(3) NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "qr_code_value" VARCHAR(255),
    "barcode_value" VARCHAR(255),
    "notes" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "inventory_lots_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "consignments" (
    "id" BIGSERIAL NOT NULL,
    "consignment_no" VARCHAR(50) NOT NULL,
    "consignment_date" DATE NOT NULL,
    "sales_id" BIGINT NOT NULL,
    "buyer_id" BIGINT NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'OPEN',
    "notes" TEXT,
    "created_by" BIGINT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "consignments_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "consignment_lines" (
    "id" BIGSERIAL NOT NULL,
    "consignment_id" BIGINT NOT NULL,
    "lot_id" BIGINT NOT NULL,
    "qty_consigned" DECIMAL(18,3) NOT NULL,
    "available_qty_snapshot" DECIMAL(18,3) NOT NULL,
    "mal_unit_price_snapshot" DECIMAL(18,2),
    "agent_name_snapshot" VARCHAR(150),
    "agent_share_percent" DECIMAL(5,2),
    "status" VARCHAR(20) NOT NULL DEFAULT 'OPEN',
    "notes" TEXT,
    "close_date" DATE,
    "selling_price" DECIMAL(18,2),
    "qty_sold" DECIMAL(18,3) NOT NULL DEFAULT 0,
    "qty_returned" DECIMAL(18,3) NOT NULL DEFAULT 0,
    "qty_shrinkage" DECIMAL(18,3) NOT NULL DEFAULT 0,
    "sales_commission" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "agent_commission" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "consignment_lines_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "regular_sales" (
    "id" BIGSERIAL NOT NULL,
    "sale_no" VARCHAR(50) NOT NULL,
    "sale_date" DATE NOT NULL,
    "buyer_id" BIGINT NOT NULL,
    "buyer_currency_code" VARCHAR(10) NOT NULL,
    "company_currency_code" VARCHAR(10) NOT NULL,
    "exchange_rate" DECIMAL(18,6),
    "courier_id" BIGINT,
    "shipping_cost_buyer" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "shipping_cost_company" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "shipping_receipt_file_url" VARCHAR(255),
    "close_date" DATE,
    "total_nominal_buyer" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "total_nominal_company" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "total_agent_commission" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "status" VARCHAR(20) NOT NULL DEFAULT 'IN_PROGRESS',
    "notes" TEXT,
    "created_by" BIGINT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "regular_sales_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "regular_sale_lines" (
    "id" BIGSERIAL NOT NULL,
    "regular_sale_id" BIGINT NOT NULL,
    "lot_id" BIGINT NOT NULL,
    "available_qty_snapshot" DECIMAL(18,3) NOT NULL,
    "mal_unit_price_snapshot" DECIMAL(18,2),
    "agent_id" BIGINT,
    "agent_name_snapshot" VARCHAR(150),
    "agent_share_percent" DECIMAL(5,2),
    "qty_planned" DECIMAL(18,3) NOT NULL,
    "selling_price_planned" DECIMAL(18,2) NOT NULL,
    "qty_actual_sold" DECIMAL(18,3),
    "qty_returned" DECIMAL(18,3),
    "qty_shrinkage" DECIMAL(18,3),
    "selling_price_actual" DECIMAL(18,2),
    "notes" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "regular_sale_lines_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "jit_sales" (
    "id" BIGSERIAL NOT NULL,
    "sale_no" VARCHAR(50) NOT NULL,
    "sale_date" DATE NOT NULL,
    "buyer_id" BIGINT NOT NULL,
    "buyer_currency_code" VARCHAR(10) NOT NULL,
    "company_currency_code" VARCHAR(10) NOT NULL,
    "exchange_rate" DECIMAL(18,6),
    "courier_id" BIGINT,
    "shipping_cost_buyer" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "shipping_cost_company" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "shipping_receipt_file_url" VARCHAR(255),
    "close_date" DATE,
    "total_nominal_buyer" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "total_nominal_company" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "total_agent_commission" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "status" VARCHAR(20) NOT NULL DEFAULT 'OPEN',
    "notes" TEXT,
    "created_by" BIGINT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "jit_sales_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "jit_sale_lines" (
    "id" BIGSERIAL NOT NULL,
    "jit_sale_id" BIGINT NOT NULL,
    "grade_id" BIGINT NOT NULL,
    "qty_planned" DECIMAL(18,3) NOT NULL,
    "qty_actual_sold" DECIMAL(18,3),
    "mal_unit_price" DECIMAL(18,2) NOT NULL,
    "selling_price_planned" DECIMAL(18,2) NOT NULL,
    "selling_price_actual" DECIMAL(18,2),
    "agent_id" BIGINT,
    "agent_name_snapshot" VARCHAR(150),
    "profit_share_scheme_id" BIGINT,
    "profit_share_scheme_name" VARCHAR(150),
    "agent_share_percent" DECIMAL(5,2),
    "notes" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "jit_sale_lines_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "stock_adjustments" (
    "id" BIGSERIAL NOT NULL,
    "adjustment_no" VARCHAR(50) NOT NULL,
    "lot_id" BIGINT NOT NULL,
    "adjustment_reason_id" BIGINT NOT NULL,
    "adjustment_date" DATE NOT NULL,
    "qty_change" DECIMAL(18,3) NOT NULL,
    "available_qty_before" DECIMAL(18,3) NOT NULL,
    "available_qty_after" DECIMAL(18,3) NOT NULL,
    "notes" TEXT,
    "created_by" BIGINT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "stock_adjustments_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "lot_transformations" (
    "id" BIGSERIAL NOT NULL,
    "transformation_no" VARCHAR(50) NOT NULL,
    "transformation_type" VARCHAR(30) NOT NULL DEFAULT 'MIX',
    "transformation_date" DATE NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'FINALIZED',
    "remainder_mode" VARCHAR(30),
    "remainder_qty" DECIMAL(18,3),
    "processing_loss_mode" VARCHAR(30),
    "processing_loss_qty" DECIMAL(18,3),
    "notes" TEXT,
    "created_by" BIGINT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "lot_transformations_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "lot_transformation_inputs" (
    "id" BIGSERIAL NOT NULL,
    "transformation_id" BIGINT NOT NULL,
    "source_lot_id" BIGINT NOT NULL,
    "qty_used" DECIMAL(18,3) NOT NULL,
    "unit_cost_snapshot" DECIMAL(18,2) NOT NULL,
    "notes" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "lot_transformation_inputs_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "lot_transformation_outputs" (
    "id" BIGSERIAL NOT NULL,
    "transformation_id" BIGINT NOT NULL,
    "result_lot_id" BIGINT NOT NULL,
    "qty_produced" DECIMAL(18,3) NOT NULL,
    "notes" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "lot_transformation_outputs_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "purchase_analyses" (
    "id" BIGSERIAL NOT NULL,
    "purchase_id" BIGINT NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'DRAFT',
    "weight_buy" DECIMAL(18,3),
    "weight_received" DECIMAL(18,3),
    "weight_final" DECIMAL(18,3),
    "moisture_buy_percent" DECIMAL(7,2),
    "moisture_received_percent" DECIMAL(7,2),
    "moisture_final_percent" DECIMAL(7,2),
    "above_average_ratio_percent" DECIMAL(7,2),
    "average_price" DECIMAL(18,2),
    "modal_beli" DECIMAL(18,2),
    "modal_masuk" DECIMAL(18,2),
    "modal_jual" DECIMAL(18,2),
    "modal_barang" DECIMAL(18,2),
    "total_modal_beli" DECIMAL(18,2),
    "total_modal_mal" DECIMAL(18,2),
    "market_reference_price" DECIMAL(18,2),
    "market_valuation_total" DECIMAL(18,2),
    "agent_profit_share_total" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "notes" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "purchase_analyses_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "purchase_analysis_cost_entries" (
    "id" BIGSERIAL NOT NULL,
    "analysis_id" BIGINT NOT NULL,
    "cost_type" VARCHAR(50) NOT NULL,
    "description" VARCHAR(255),
    "amount" DECIMAL(18,2) NOT NULL,
    "proof_file_url" VARCHAR(255),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "purchase_analysis_cost_entries_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "lot_purchase_allocations" (
    "id" BIGSERIAL NOT NULL,
    "lot_id" BIGINT NOT NULL,
    "purchase_id" BIGINT NOT NULL,
    "purchase_line_id" BIGINT,
    "source_type" VARCHAR(30) NOT NULL,
    "source_ref_id" BIGINT,
    "agent_id_snapshot" BIGINT,
    "profit_share_scheme_id_snapshot" BIGINT,
    "qty_allocated" DECIMAL(18,3) NOT NULL,
    "cost_total_allocated" DECIMAL(18,2) NOT NULL,
    "unit_cost_snapshot" DECIMAL(18,2) NOT NULL,
    "notes" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "lot_purchase_allocations_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "purchase_realization_entries" (
    "id" BIGSERIAL NOT NULL,
    "purchase_id" BIGINT NOT NULL,
    "lot_id" BIGINT,
    "allocation_id" BIGINT,
    "event_type" VARCHAR(40) NOT NULL,
    "reference_type" VARCHAR(40) NOT NULL,
    "reference_id" BIGINT,
    "occurred_at" TIMESTAMP(3) NOT NULL,
    "qty_in" DECIMAL(18,3) NOT NULL DEFAULT 0,
    "qty_out" DECIMAL(18,3) NOT NULL DEFAULT 0,
    "qty_shrinkage" DECIMAL(18,3) NOT NULL DEFAULT 0,
    "amount_cost" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "amount_revenue" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "amount_expense" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "amount_profit" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "agent_share_percent_snapshot" DECIMAL(7,2),
    "agent_amount" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "notes" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "purchase_realization_entries_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "purchase_realization_summaries" (
    "id" BIGSERIAL NOT NULL,
    "purchase_id" BIGINT NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'OPEN',
    "qty_opening" DECIMAL(18,3) NOT NULL DEFAULT 0,
    "qty_remaining" DECIMAL(18,3) NOT NULL DEFAULT 0,
    "qty_sold" DECIMAL(18,3) NOT NULL DEFAULT 0,
    "qty_returned" DECIMAL(18,3) NOT NULL DEFAULT 0,
    "qty_shrinkage" DECIMAL(18,3) NOT NULL DEFAULT 0,
    "cost_opening_total" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "cost_additional_total" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "revenue_total" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "profit_loss_total" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "agent_share_percent" DECIMAL(7,2),
    "agent_profit_total" DECIMAL(18,2) NOT NULL DEFAULT 0,
    "closed_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "purchase_realization_summaries_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "lot_washings" (
    "id" BIGSERIAL NOT NULL,
    "washing_no" VARCHAR(50) NOT NULL,
    "lot_id" BIGINT NOT NULL,
    "washing_place_id" BIGINT NOT NULL,
    "washing_cost" DECIMAL(18,2) NOT NULL,
    "duration_hours" INTEGER NOT NULL,
    "receipt_file_url" VARCHAR(255),
    "status" VARCHAR(20) NOT NULL DEFAULT 'IN_PROGRESS',
    "started_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "expected_done_at" TIMESTAMP(3) NOT NULL,
    "completed_at" TIMESTAMP(3),
    "before_qty" DECIMAL(18,3) NOT NULL,
    "after_qty" DECIMAL(18,3),
    "shrinkage_qty" DECIMAL(18,3),
    "before_grade_name" VARCHAR(100),
    "after_grade_name" VARCHAR(100),
    "before_warehouse_name" VARCHAR(150) NOT NULL,
    "before_location_name" VARCHAR(150),
    "after_warehouse_name" VARCHAR(150),
    "after_location_name" VARCHAR(150),
    "created_by" BIGINT NOT NULL,
    "completed_by" BIGINT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "lot_washings_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "app_settings" (
    "id" BIGSERIAL NOT NULL,
    "singleton_key" VARCHAR(30) NOT NULL DEFAULT 'SYSTEM',
    "company_name" VARCHAR(150),
    "company_email" VARCHAR(150),
    "company_phone" VARCHAR(50),
    "company_bank_name" VARCHAR(150),
    "company_bank_account_number" VARCHAR(80),
    "company_address" TEXT,
    "company_timezone" VARCHAR(80) NOT NULL DEFAULT 'Asia/Jakarta',
    "smtp_host" VARCHAR(150),
    "smtp_port" INTEGER,
    "smtp_secure" BOOLEAN NOT NULL DEFAULT true,
    "smtp_user" VARCHAR(150),
    "smtp_password" VARCHAR(255),
    "smtp_from_name" VARCHAR(150),
    "smtp_from_email" VARCHAR(150),
    "purchase_prefix" VARCHAR(20) NOT NULL DEFAULT 'PO',
    "receipt_prefix" VARCHAR(20) NOT NULL DEFAULT 'RCV',
    "lot_prefix" VARCHAR(20) NOT NULL DEFAULT 'LOT',
    "adjustment_prefix" VARCHAR(20) NOT NULL DEFAULT 'STA',
    "transformation_prefix" VARCHAR(20) NOT NULL DEFAULT 'MIX',
    "fund_request_prefix" VARCHAR(20) NOT NULL DEFAULT 'FR',
    "washing_prefix" VARCHAR(20) NOT NULL DEFAULT 'WSH',
    "regular_sale_prefix" VARCHAR(20) NOT NULL DEFAULT 'SRG',
    "jit_sale_prefix" VARCHAR(20) NOT NULL DEFAULT 'SJT',
    "consignment_prefix" VARCHAR(20) NOT NULL DEFAULT 'TJT',
    "default_locale" VARCHAR(10) NOT NULL DEFAULT 'id',
    "currency_code" VARCHAR(10) NOT NULL DEFAULT 'IDR',
    "date_format" VARCHAR(30) NOT NULL DEFAULT 'DD/MM/YYYY',
    "password_min_length" INTEGER NOT NULL DEFAULT 8,
    "session_timeout_minutes" INTEGER NOT NULL DEFAULT 720,
    "require_email_verification" BOOLEAN NOT NULL DEFAULT true,
    "audit_retention_days" INTEGER NOT NULL DEFAULT 365,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "app_settings_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "company_bank_accounts" (
    "id" BIGSERIAL NOT NULL,
    "app_setting_id" BIGINT NOT NULL,
    "bank_id" BIGINT NOT NULL,
    "account_number" VARCHAR(100) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "company_bank_accounts_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "roles_code_key" ON "roles"("code");

-- CreateIndex
CREATE UNIQUE INDEX "users_username_key" ON "users"("username");

-- CreateIndex
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");

-- CreateIndex
CREATE INDEX "audit_trails_action_occurred_at_idx" ON "audit_trails"("action", "occurred_at");

-- CreateIndex
CREATE INDEX "audit_trails_entity_type_occurred_at_idx" ON "audit_trails"("entity_type", "occurred_at");

-- CreateIndex
CREATE INDEX "audit_trails_user_id_occurred_at_idx" ON "audit_trails"("user_id", "occurred_at");

-- CreateIndex
CREATE UNIQUE INDEX "password_reset_tokens_token_hash_key" ON "password_reset_tokens"("token_hash");

-- CreateIndex
CREATE UNIQUE INDEX "email_verification_tokens_token_hash_key" ON "email_verification_tokens"("token_hash");

-- CreateIndex
CREATE UNIQUE INDEX "buyers_code_key" ON "buyers"("code");

-- CreateIndex
CREATE UNIQUE INDEX "employees_code_key" ON "employees"("code");

-- CreateIndex
CREATE UNIQUE INDEX "couriers_code_key" ON "couriers"("code");

-- CreateIndex
CREATE UNIQUE INDEX "washing_places_code_key" ON "washing_places"("code");

-- CreateIndex
CREATE UNIQUE INDEX "grades_code_key" ON "grades"("code");

-- CreateIndex
CREATE UNIQUE INDEX "grades_legacy_code_key" ON "grades"("legacy_code");

-- CreateIndex
CREATE UNIQUE INDEX "warehouses_code_key" ON "warehouses"("code");

-- CreateIndex
CREATE UNIQUE INDEX "units_code_key" ON "units"("code");

-- CreateIndex
CREATE UNIQUE INDEX "currencies_code_key" ON "currencies"("code");

-- CreateIndex
CREATE UNIQUE INDEX "banks_code_key" ON "banks"("code");

-- CreateIndex
CREATE UNIQUE INDEX "banks_name_key" ON "banks"("name");

-- CreateIndex
CREATE UNIQUE INDEX "profit_share_schemes_code_key" ON "profit_share_schemes"("code");

-- CreateIndex
CREATE UNIQUE INDEX "agents_code_key" ON "agents"("code");

-- CreateIndex
CREATE UNIQUE INDEX "agents_identity_type_identity_number_key" ON "agents"("identity_type", "identity_number");

-- CreateIndex
CREATE UNIQUE INDEX "agent_bank_accounts_agent_id_bank_id_account_number_key" ON "agent_bank_accounts"("agent_id", "bank_id", "account_number");

-- CreateIndex
CREATE INDEX "agent_balance_mutations_agent_id_occurred_at_idx" ON "agent_balance_mutations"("agent_id", "occurred_at");

-- CreateIndex
CREATE INDEX "agent_balance_mutations_agent_id_balance_type_occurred_at_idx" ON "agent_balance_mutations"("agent_id", "balance_type", "occurred_at");

-- CreateIndex
CREATE UNIQUE INDEX "fund_requests_request_no_key" ON "fund_requests"("request_no");

-- CreateIndex
CREATE INDEX "fund_requests_agent_id_transferred_at_idx" ON "fund_requests"("agent_id", "transferred_at");

-- CreateIndex
CREATE INDEX "fund_requests_transfer_type_transferred_at_idx" ON "fund_requests"("transfer_type", "transferred_at");

-- CreateIndex
CREATE UNIQUE INDEX "sales_code_key" ON "sales"("code");

-- CreateIndex
CREATE UNIQUE INDEX "sales_identity_type_identity_number_key" ON "sales"("identity_type", "identity_number");

-- CreateIndex
CREATE INDEX "sales_commission_mutations_sales_id_occurred_at_idx" ON "sales_commission_mutations"("sales_id", "occurred_at");

-- CreateIndex
CREATE UNIQUE INDEX "sales_bank_accounts_sales_id_bank_id_account_number_key" ON "sales_bank_accounts"("sales_id", "bank_id", "account_number");

-- CreateIndex
CREATE UNIQUE INDEX "warehouse_locations_warehouse_id_code_key" ON "warehouse_locations"("warehouse_id", "code");

-- CreateIndex
CREATE UNIQUE INDEX "adjustment_reasons_code_key" ON "adjustment_reasons"("code");

-- CreateIndex
CREATE UNIQUE INDEX "purchases_purchase_no_key" ON "purchases"("purchase_no");

-- CreateIndex
CREATE UNIQUE INDEX "receipts_receipt_no_key" ON "receipts"("receipt_no");

-- CreateIndex
CREATE UNIQUE INDEX "inventory_lots_lot_code_key" ON "inventory_lots"("lot_code");

-- CreateIndex
CREATE UNIQUE INDEX "consignments_consignment_no_key" ON "consignments"("consignment_no");

-- CreateIndex
CREATE UNIQUE INDEX "regular_sales_sale_no_key" ON "regular_sales"("sale_no");

-- CreateIndex
CREATE UNIQUE INDEX "regular_sale_lines_regular_sale_id_lot_id_key" ON "regular_sale_lines"("regular_sale_id", "lot_id");

-- CreateIndex
CREATE UNIQUE INDEX "jit_sales_sale_no_key" ON "jit_sales"("sale_no");

-- CreateIndex
CREATE UNIQUE INDEX "stock_adjustments_adjustment_no_key" ON "stock_adjustments"("adjustment_no");

-- CreateIndex
CREATE UNIQUE INDEX "lot_transformations_transformation_no_key" ON "lot_transformations"("transformation_no");

-- CreateIndex
CREATE UNIQUE INDEX "lot_transformation_inputs_transformation_id_source_lot_id_key" ON "lot_transformation_inputs"("transformation_id", "source_lot_id");

-- CreateIndex
CREATE UNIQUE INDEX "lot_transformation_outputs_result_lot_id_key" ON "lot_transformation_outputs"("result_lot_id");

-- CreateIndex
CREATE UNIQUE INDEX "purchase_analyses_purchase_id_key" ON "purchase_analyses"("purchase_id");

-- CreateIndex
CREATE INDEX "lot_purchase_allocations_lot_id_idx" ON "lot_purchase_allocations"("lot_id");

-- CreateIndex
CREATE INDEX "lot_purchase_allocations_purchase_id_idx" ON "lot_purchase_allocations"("purchase_id");

-- CreateIndex
CREATE INDEX "lot_purchase_allocations_purchase_line_id_idx" ON "lot_purchase_allocations"("purchase_line_id");

-- CreateIndex
CREATE INDEX "lot_purchase_allocations_source_type_source_ref_id_idx" ON "lot_purchase_allocations"("source_type", "source_ref_id");

-- CreateIndex
CREATE INDEX "purchase_realization_entries_purchase_id_occurred_at_idx" ON "purchase_realization_entries"("purchase_id", "occurred_at");

-- CreateIndex
CREATE INDEX "purchase_realization_entries_lot_id_idx" ON "purchase_realization_entries"("lot_id");

-- CreateIndex
CREATE INDEX "purchase_realization_entries_allocation_id_idx" ON "purchase_realization_entries"("allocation_id");

-- CreateIndex
CREATE INDEX "purchase_realization_entries_reference_type_reference_id_idx" ON "purchase_realization_entries"("reference_type", "reference_id");

-- CreateIndex
CREATE INDEX "purchase_realization_entries_event_type_occurred_at_idx" ON "purchase_realization_entries"("event_type", "occurred_at");

-- CreateIndex
CREATE UNIQUE INDEX "purchase_realization_summaries_purchase_id_key" ON "purchase_realization_summaries"("purchase_id");

-- CreateIndex
CREATE INDEX "purchase_realization_summaries_status_idx" ON "purchase_realization_summaries"("status");

-- CreateIndex
CREATE INDEX "purchase_realization_summaries_closed_at_idx" ON "purchase_realization_summaries"("closed_at");

-- CreateIndex
CREATE UNIQUE INDEX "lot_washings_washing_no_key" ON "lot_washings"("washing_no");

-- CreateIndex
CREATE INDEX "lot_washings_lot_id_status_idx" ON "lot_washings"("lot_id", "status");

-- CreateIndex
CREATE UNIQUE INDEX "app_settings_singleton_key_key" ON "app_settings"("singleton_key");

-- CreateIndex
CREATE UNIQUE INDEX "company_bank_accounts_app_setting_id_bank_id_account_number_key" ON "company_bank_accounts"("app_setting_id", "bank_id", "account_number");

-- AddForeignKey
ALTER TABLE "users" ADD CONSTRAINT "users_role_id_fkey" FOREIGN KEY ("role_id") REFERENCES "roles"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "audit_trails" ADD CONSTRAINT "audit_trails_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "password_reset_tokens" ADD CONSTRAINT "password_reset_tokens_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "email_verification_tokens" ADD CONSTRAINT "email_verification_tokens_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "buyer_contact_people" ADD CONSTRAINT "buyer_contact_people_buyer_id_fkey" FOREIGN KEY ("buyer_id") REFERENCES "buyers"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "washing_place_contact_people" ADD CONSTRAINT "washing_place_contact_people_washing_place_id_fkey" FOREIGN KEY ("washing_place_id") REFERENCES "washing_places"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "grade_buy_price_standards" ADD CONSTRAINT "grade_buy_price_standards_grade_id_fkey" FOREIGN KEY ("grade_id") REFERENCES "grades"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "grade_sell_price_standards" ADD CONSTRAINT "grade_sell_price_standards_grade_id_fkey" FOREIGN KEY ("grade_id") REFERENCES "grades"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "agents" ADD CONSTRAINT "agents_profit_share_scheme_id_fkey" FOREIGN KEY ("profit_share_scheme_id") REFERENCES "profit_share_schemes"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "agent_bank_accounts" ADD CONSTRAINT "agent_bank_accounts_agent_id_fkey" FOREIGN KEY ("agent_id") REFERENCES "agents"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "agent_bank_accounts" ADD CONSTRAINT "agent_bank_accounts_bank_id_fkey" FOREIGN KEY ("bank_id") REFERENCES "banks"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "agent_balance_mutations" ADD CONSTRAINT "agent_balance_mutations_agent_id_fkey" FOREIGN KEY ("agent_id") REFERENCES "agents"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "fund_requests" ADD CONSTRAINT "fund_requests_agent_id_fkey" FOREIGN KEY ("agent_id") REFERENCES "agents"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "fund_requests" ADD CONSTRAINT "fund_requests_agent_bank_account_id_fkey" FOREIGN KEY ("agent_bank_account_id") REFERENCES "agent_bank_accounts"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "fund_requests" ADD CONSTRAINT "fund_requests_company_bank_account_id_fkey" FOREIGN KEY ("company_bank_account_id") REFERENCES "company_bank_accounts"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "fund_requests" ADD CONSTRAINT "fund_requests_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "sales_commission_mutations" ADD CONSTRAINT "sales_commission_mutations_sales_id_fkey" FOREIGN KEY ("sales_id") REFERENCES "sales"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "sales_bank_accounts" ADD CONSTRAINT "sales_bank_accounts_sales_id_fkey" FOREIGN KEY ("sales_id") REFERENCES "sales"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "sales_bank_accounts" ADD CONSTRAINT "sales_bank_accounts_bank_id_fkey" FOREIGN KEY ("bank_id") REFERENCES "banks"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "warehouse_locations" ADD CONSTRAINT "warehouse_locations_warehouse_id_fkey" FOREIGN KEY ("warehouse_id") REFERENCES "warehouses"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "purchases" ADD CONSTRAINT "purchases_agent_id_fkey" FOREIGN KEY ("agent_id") REFERENCES "agents"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "purchases" ADD CONSTRAINT "purchases_buyout_source_agent_id_fkey" FOREIGN KEY ("buyout_source_agent_id") REFERENCES "agents"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "purchases" ADD CONSTRAINT "purchases_profit_share_scheme_id_fkey" FOREIGN KEY ("profit_share_scheme_id") REFERENCES "profit_share_schemes"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "purchases" ADD CONSTRAINT "purchases_courier_id_fkey" FOREIGN KEY ("courier_id") REFERENCES "couriers"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "purchases" ADD CONSTRAINT "purchases_received_by_employee_id_fkey" FOREIGN KEY ("received_by_employee_id") REFERENCES "employees"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "purchases" ADD CONSTRAINT "purchases_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "purchase_lines" ADD CONSTRAINT "purchase_lines_purchase_id_fkey" FOREIGN KEY ("purchase_id") REFERENCES "purchases"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "purchase_lines" ADD CONSTRAINT "purchase_lines_grade_id_fkey" FOREIGN KEY ("grade_id") REFERENCES "grades"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "purchase_lines" ADD CONSTRAINT "purchase_lines_source_lot_id_fkey" FOREIGN KEY ("source_lot_id") REFERENCES "inventory_lots"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "purchase_lines" ADD CONSTRAINT "purchase_lines_unit_id_fkey" FOREIGN KEY ("unit_id") REFERENCES "units"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "purchase_lines" ADD CONSTRAINT "purchase_lines_warehouse_id_fkey" FOREIGN KEY ("warehouse_id") REFERENCES "warehouses"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "purchase_lines" ADD CONSTRAINT "purchase_lines_warehouse_location_id_fkey" FOREIGN KEY ("warehouse_location_id") REFERENCES "warehouse_locations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "receipts" ADD CONSTRAINT "receipts_purchase_id_fkey" FOREIGN KEY ("purchase_id") REFERENCES "purchases"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "receipts" ADD CONSTRAINT "receipts_received_by_fkey" FOREIGN KEY ("received_by") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "receipt_lines" ADD CONSTRAINT "receipt_lines_receipt_id_fkey" FOREIGN KEY ("receipt_id") REFERENCES "receipts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "receipt_lines" ADD CONSTRAINT "receipt_lines_purchase_line_id_fkey" FOREIGN KEY ("purchase_line_id") REFERENCES "purchase_lines"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "receipt_lines" ADD CONSTRAINT "receipt_lines_grade_id_fkey" FOREIGN KEY ("grade_id") REFERENCES "grades"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "receipt_lines" ADD CONSTRAINT "receipt_lines_unit_id_fkey" FOREIGN KEY ("unit_id") REFERENCES "units"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "receipt_lines" ADD CONSTRAINT "receipt_lines_warehouse_id_fkey" FOREIGN KEY ("warehouse_id") REFERENCES "warehouses"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "receipt_lines" ADD CONSTRAINT "receipt_lines_warehouse_location_id_fkey" FOREIGN KEY ("warehouse_location_id") REFERENCES "warehouse_locations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "inventory_lots" ADD CONSTRAINT "inventory_lots_parent_lot_id_fkey" FOREIGN KEY ("parent_lot_id") REFERENCES "inventory_lots"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "inventory_lots" ADD CONSTRAINT "inventory_lots_purchase_id_fkey" FOREIGN KEY ("purchase_id") REFERENCES "purchases"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "inventory_lots" ADD CONSTRAINT "inventory_lots_purchase_line_id_fkey" FOREIGN KEY ("purchase_line_id") REFERENCES "purchase_lines"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "inventory_lots" ADD CONSTRAINT "inventory_lots_receipt_id_fkey" FOREIGN KEY ("receipt_id") REFERENCES "receipts"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "inventory_lots" ADD CONSTRAINT "inventory_lots_receipt_line_id_fkey" FOREIGN KEY ("receipt_line_id") REFERENCES "receipt_lines"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "inventory_lots" ADD CONSTRAINT "inventory_lots_grade_id_fkey" FOREIGN KEY ("grade_id") REFERENCES "grades"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "inventory_lots" ADD CONSTRAINT "inventory_lots_warehouse_id_fkey" FOREIGN KEY ("warehouse_id") REFERENCES "warehouses"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "inventory_lots" ADD CONSTRAINT "inventory_lots_warehouse_location_id_fkey" FOREIGN KEY ("warehouse_location_id") REFERENCES "warehouse_locations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "inventory_lots" ADD CONSTRAINT "inventory_lots_unit_id_fkey" FOREIGN KEY ("unit_id") REFERENCES "units"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "consignments" ADD CONSTRAINT "consignments_sales_id_fkey" FOREIGN KEY ("sales_id") REFERENCES "sales"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "consignments" ADD CONSTRAINT "consignments_buyer_id_fkey" FOREIGN KEY ("buyer_id") REFERENCES "buyers"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "consignments" ADD CONSTRAINT "consignments_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "consignment_lines" ADD CONSTRAINT "consignment_lines_consignment_id_fkey" FOREIGN KEY ("consignment_id") REFERENCES "consignments"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "consignment_lines" ADD CONSTRAINT "consignment_lines_lot_id_fkey" FOREIGN KEY ("lot_id") REFERENCES "inventory_lots"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "regular_sales" ADD CONSTRAINT "regular_sales_buyer_id_fkey" FOREIGN KEY ("buyer_id") REFERENCES "buyers"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "regular_sales" ADD CONSTRAINT "regular_sales_courier_id_fkey" FOREIGN KEY ("courier_id") REFERENCES "couriers"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "regular_sales" ADD CONSTRAINT "regular_sales_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "regular_sale_lines" ADD CONSTRAINT "regular_sale_lines_regular_sale_id_fkey" FOREIGN KEY ("regular_sale_id") REFERENCES "regular_sales"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "regular_sale_lines" ADD CONSTRAINT "regular_sale_lines_lot_id_fkey" FOREIGN KEY ("lot_id") REFERENCES "inventory_lots"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "jit_sales" ADD CONSTRAINT "jit_sales_buyer_id_fkey" FOREIGN KEY ("buyer_id") REFERENCES "buyers"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "jit_sales" ADD CONSTRAINT "jit_sales_courier_id_fkey" FOREIGN KEY ("courier_id") REFERENCES "couriers"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "jit_sales" ADD CONSTRAINT "jit_sales_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "jit_sale_lines" ADD CONSTRAINT "jit_sale_lines_jit_sale_id_fkey" FOREIGN KEY ("jit_sale_id") REFERENCES "jit_sales"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "jit_sale_lines" ADD CONSTRAINT "jit_sale_lines_grade_id_fkey" FOREIGN KEY ("grade_id") REFERENCES "grades"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "jit_sale_lines" ADD CONSTRAINT "jit_sale_lines_agent_id_fkey" FOREIGN KEY ("agent_id") REFERENCES "agents"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "jit_sale_lines" ADD CONSTRAINT "jit_sale_lines_profit_share_scheme_id_fkey" FOREIGN KEY ("profit_share_scheme_id") REFERENCES "profit_share_schemes"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "stock_adjustments" ADD CONSTRAINT "stock_adjustments_lot_id_fkey" FOREIGN KEY ("lot_id") REFERENCES "inventory_lots"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "stock_adjustments" ADD CONSTRAINT "stock_adjustments_adjustment_reason_id_fkey" FOREIGN KEY ("adjustment_reason_id") REFERENCES "adjustment_reasons"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "stock_adjustments" ADD CONSTRAINT "stock_adjustments_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "lot_transformations" ADD CONSTRAINT "lot_transformations_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "lot_transformation_inputs" ADD CONSTRAINT "lot_transformation_inputs_transformation_id_fkey" FOREIGN KEY ("transformation_id") REFERENCES "lot_transformations"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "lot_transformation_inputs" ADD CONSTRAINT "lot_transformation_inputs_source_lot_id_fkey" FOREIGN KEY ("source_lot_id") REFERENCES "inventory_lots"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "lot_transformation_outputs" ADD CONSTRAINT "lot_transformation_outputs_transformation_id_fkey" FOREIGN KEY ("transformation_id") REFERENCES "lot_transformations"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "lot_transformation_outputs" ADD CONSTRAINT "lot_transformation_outputs_result_lot_id_fkey" FOREIGN KEY ("result_lot_id") REFERENCES "inventory_lots"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "purchase_analyses" ADD CONSTRAINT "purchase_analyses_purchase_id_fkey" FOREIGN KEY ("purchase_id") REFERENCES "purchases"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "purchase_analysis_cost_entries" ADD CONSTRAINT "purchase_analysis_cost_entries_analysis_id_fkey" FOREIGN KEY ("analysis_id") REFERENCES "purchase_analyses"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "lot_purchase_allocations" ADD CONSTRAINT "lot_purchase_allocations_lot_id_fkey" FOREIGN KEY ("lot_id") REFERENCES "inventory_lots"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "lot_purchase_allocations" ADD CONSTRAINT "lot_purchase_allocations_purchase_id_fkey" FOREIGN KEY ("purchase_id") REFERENCES "purchases"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "lot_purchase_allocations" ADD CONSTRAINT "lot_purchase_allocations_purchase_line_id_fkey" FOREIGN KEY ("purchase_line_id") REFERENCES "purchase_lines"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "purchase_realization_entries" ADD CONSTRAINT "purchase_realization_entries_purchase_id_fkey" FOREIGN KEY ("purchase_id") REFERENCES "purchases"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "purchase_realization_entries" ADD CONSTRAINT "purchase_realization_entries_lot_id_fkey" FOREIGN KEY ("lot_id") REFERENCES "inventory_lots"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "purchase_realization_entries" ADD CONSTRAINT "purchase_realization_entries_allocation_id_fkey" FOREIGN KEY ("allocation_id") REFERENCES "lot_purchase_allocations"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "purchase_realization_summaries" ADD CONSTRAINT "purchase_realization_summaries_purchase_id_fkey" FOREIGN KEY ("purchase_id") REFERENCES "purchases"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "lot_washings" ADD CONSTRAINT "lot_washings_lot_id_fkey" FOREIGN KEY ("lot_id") REFERENCES "inventory_lots"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "lot_washings" ADD CONSTRAINT "lot_washings_washing_place_id_fkey" FOREIGN KEY ("washing_place_id") REFERENCES "washing_places"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "lot_washings" ADD CONSTRAINT "lot_washings_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "lot_washings" ADD CONSTRAINT "lot_washings_completed_by_fkey" FOREIGN KEY ("completed_by") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "company_bank_accounts" ADD CONSTRAINT "company_bank_accounts_app_setting_id_fkey" FOREIGN KEY ("app_setting_id") REFERENCES "app_settings"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "company_bank_accounts" ADD CONSTRAINT "company_bank_accounts_bank_id_fkey" FOREIGN KEY ("bank_id") REFERENCES "banks"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

