-- https://www.phpmyadmin.net/
--

START TRANSACTION;



--
-- Database: `mdsoftware_stock`
--

-- --------------------------------------------------------

--
-- Table structure for table `company_settings`
--

CREATE TABLE `company_settings` (
  `id` int(11) NOT NULL,
  `company_name` varchar(200) NOT NULL DEFAULT 'My Company',
  `gstin` varchar(20) NOT NULL DEFAULT '',
  `state` varchar(60) NOT NULL DEFAULT '',
  `state_code` varchar(2) NOT NULL DEFAULT '09',
  `address` varchar(255) NOT NULL DEFAULT '',
  `default_tax_mode` enum('INCLUSIVE','EXCLUSIVE') NOT NULL DEFAULT 'EXCLUSIVE',
  `rounding_mode` enum('NEAREST_1','NEAREST_0_05','NEAREST_0_01') NOT NULL DEFAULT 'NEAREST_0_01',
  `enable_cst` tinyint(1) NOT NULL DEFAULT 0,
  `cst_percent_default` decimal(5,2) NOT NULL DEFAULT 0.00,
  `invoice_prefix` varchar(20) NOT NULL DEFAULT 'INV-',
  `invoice_padding` int(11) NOT NULL DEFAULT 6,
  `invoice_next` int(11) NOT NULL DEFAULT 1,
  `b2c_invoice_type` enum('TAX_INVOICE','BILL_OF_SUPPLY') NOT NULL DEFAULT 'TAX_INVOICE',
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `company_settings`
--

INSERT INTO `company_settings` (`id`, `company_name`, `gstin`, `state_code`, `address`, `default_tax_mode`, `rounding_mode`, `enable_cst`, `cst_percent_default`, `invoice_prefix`, `invoice_padding`, `invoice_next`, `b2c_invoice_type`, `created_at`, `updated_at`) VALUES
(1, 'My Company', '', '09', '', 'EXCLUSIVE', 'NEAREST_0_01', 0, 0.00, 'INV-', 6, 4, 'TAX_INVOICE', '2026-02-15 16:19:05', '2026-02-15 16:19:05');

-- --------------------------------------------------------

--
-- Table structure for table `credit_notes`
--

CREATE TABLE `credit_notes` (
  `id` int(11) NOT NULL,
  `credit_no` varchar(30) NOT NULL,
  `credit_date` date NOT NULL,
  `sales_id` int(11) NOT NULL,
  `customer_id` int(11) DEFAULT NULL,
  `taxable_total` decimal(14,2) NOT NULL DEFAULT 0.00,
  `tax_total` decimal(14,2) NOT NULL DEFAULT 0.00,
  `grand_total` decimal(14,2) NOT NULL DEFAULT 0.00,
  `note` varchar(255) DEFAULT NULL,
  `is_cancelled` tinyint(1) NOT NULL DEFAULT 0,
  `cancelled_at` datetime DEFAULT NULL,
  `created_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `credit_note_items`
--

CREATE TABLE `credit_note_items` (
  `id` int(11) NOT NULL,
  `credit_id` int(11) NOT NULL,
  `sales_item_id` int(11) NOT NULL,
  `item_id` int(11) NOT NULL,
  `qty` decimal(14,4) NOT NULL,
  `base_rate` decimal(14,6) NOT NULL,
  `gst_percent` decimal(5,2) NOT NULL,
  `cgst_amt` decimal(14,2) NOT NULL DEFAULT 0.00,
  `sgst_amt` decimal(14,2) NOT NULL DEFAULT 0.00,
  `igst_amt` decimal(14,2) NOT NULL DEFAULT 0.00,
  `cst_amt` decimal(14,2) NOT NULL DEFAULT 0.00,
  `taxable_amount` decimal(14,2) NOT NULL DEFAULT 0.00,
  `line_total` decimal(14,2) NOT NULL DEFAULT 0.00,
  `issue_rate` decimal(14,6) NOT NULL DEFAULT 0.000000,
  `cogs_reversal` decimal(14,2) NOT NULL DEFAULT 0.00
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `masters_items`
--

CREATE TABLE `masters_items` (
  `id` int(11) NOT NULL,
  `item_name` varchar(200) NOT NULL,
  `brand` varchar(100) DEFAULT NULL,
  `unit` varchar(20) NOT NULL DEFAULT 'pcs',
  `hsn_sac` varchar(30) DEFAULT NULL,
  `gst_percent` decimal(5,2) NOT NULL DEFAULT 0.00,
  `default_purchase_rate` decimal(12,4) NOT NULL DEFAULT 0.0000,
  `default_sale_rate` decimal(12,4) NOT NULL DEFAULT 0.0000,
  `mrp` decimal(12,4) NOT NULL DEFAULT 0.0000,
  `min_stock_qty` decimal(14,4) NOT NULL DEFAULT 0.0000,
  `stock_tracking_mode` enum('NONE','BATCH','EXPIRY','BATCH_EXPIRY') NOT NULL DEFAULT 'NONE',
  `is_active` tinyint(1) NOT NULL DEFAULT 1,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `masters_items`
--

INSERT INTO `masters_items` (`id`, `item_name`, `brand`, `unit`, `hsn_sac`, `gst_percent`, `default_purchase_rate`, `default_sale_rate`, `mrp`, `min_stock_qty`, `stock_tracking_mode`, `is_active`, `created_at`, `updated_at`) VALUES
(1, 'water bottel', 'pepsi', 'pcs', 'kjb', 5.00, 10.0000, 12.0000, 15.0000, 25.0000, 'NONE', 1, '2026-02-15 18:14:45', '2026-02-15 18:14:45');

-- --------------------------------------------------------

--
-- Table structure for table `masters_parties`
--

CREATE TABLE `masters_parties` (
  `id` int(11) NOT NULL,
  `party_type` enum('CUSTOMER','SUPPLIER','BOTH') NOT NULL DEFAULT 'CUSTOMER',
  `party_name` varchar(200) NOT NULL,
  `gstin` varchar(20) DEFAULT NULL,
  `state_code` varchar(2) NOT NULL,
  `address` varchar(255) DEFAULT NULL,
  `mobile` varchar(20) DEFAULT NULL,
  `email` varchar(120) DEFAULT NULL,
  `credit_limit` decimal(12,2) NOT NULL DEFAULT 0.00,
  `opening_balance` decimal(12,2) NOT NULL DEFAULT 0.00,
  `opening_balance_type` enum('DR','CR') NOT NULL DEFAULT 'DR',
  `is_active` tinyint(1) NOT NULL DEFAULT 1,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `masters_parties`
--

INSERT INTO `masters_parties` (`id`, `party_type`, `party_name`, `gstin`, `state_code`, `address`, `mobile`, `email`, `credit_limit`, `opening_balance`, `opening_balance_type`, `is_active`, `created_at`, `updated_at`) VALUES
(1, 'BOTH', 'test', 'hjkjkh', 'Ut', 'Ayodhy', '7007807386', 'dileepvermamds@gmail.com', 100.00, 0.00, 'DR', 1, '2026-02-15 18:13:30', '2026-02-15 18:13:30');

-- --------------------------------------------------------

--
-- Table structure for table `purchases`
--

CREATE TABLE `purchases` (
  `id` int(11) NOT NULL,
  `purchase_no` varchar(30) NOT NULL,
  `supplier_invoice_no` varchar(50) DEFAULT NULL,
  `supplier_id` int(11) NOT NULL,
  `invoice_date` date NOT NULL,
  `tax_type` enum('INTRA','INTER') NOT NULL,
  `tax_mode` enum('INCLUSIVE','EXCLUSIVE') NOT NULL,
  `freight_amount` decimal(14,2) NOT NULL DEFAULT 0.00,
  `freight_taxable` tinyint(1) NOT NULL DEFAULT 0,
  `other_charges` decimal(14,2) NOT NULL DEFAULT 0.00,
  `other_charges_taxable` tinyint(1) NOT NULL DEFAULT 0,
  `header_discount` decimal(14,2) NOT NULL DEFAULT 0.00,
  `taxable_total` decimal(14,2) NOT NULL DEFAULT 0.00,
  `tax_total` decimal(14,2) NOT NULL DEFAULT 0.00,
  `grand_total` decimal(14,2) NOT NULL DEFAULT 0.00,
  `created_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `purchases`
--

INSERT INTO `purchases` (`id`, `purchase_no`, `supplier_invoice_no`, `supplier_id`, `invoice_date`, `tax_type`, `tax_mode`, `freight_amount`, `freight_taxable`, `other_charges`, `other_charges_taxable`, `header_discount`, `taxable_total`, `tax_total`, `grand_total`, `created_at`) VALUES
(1, 'PUR-000001', '1212121', 1, '2026-02-15', 'INTER', 'EXCLUSIVE', 10.00, 1, 10.00, 1, 10.00, 303.00, 22.98, 325.98, '2026-02-15 18:35:32');

-- --------------------------------------------------------

--
-- Table structure for table `purchase_items`
--

CREATE TABLE `purchase_items` (
  `id` int(11) NOT NULL,
  `purchase_id` int(11) NOT NULL,
  `item_id` int(11) NOT NULL,
  `qty` decimal(14,4) NOT NULL,
  `discount_percent` decimal(5,2) NOT NULL DEFAULT 0.00,
  `discount_amount` decimal(14,2) NOT NULL DEFAULT 0.00,
  `base_rate` decimal(14,6) NOT NULL,
  `gst_percent` decimal(5,2) NOT NULL,
  `cgst_amt` decimal(14,2) NOT NULL DEFAULT 0.00,
  `sgst_amt` decimal(14,2) NOT NULL DEFAULT 0.00,
  `igst_amt` decimal(14,2) NOT NULL DEFAULT 0.00,
  `cst_amt` decimal(14,2) NOT NULL DEFAULT 0.00,
  `taxable_amount` decimal(14,2) NOT NULL DEFAULT 0.00,
  `line_total` decimal(14,2) NOT NULL DEFAULT 0.00
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `purchase_items`
--

INSERT INTO `purchase_items` (`id`, `purchase_id`, `item_id`, `qty`, `discount_percent`, `discount_amount`, `base_rate`, `gst_percent`, `cgst_amt`, `sgst_amt`, `igst_amt`, `cst_amt`, `taxable_amount`, `line_total`) VALUES
(1, 1, 1, 11.0000, 10.00, 0.00, 10.000000, 5.00, 0.00, 0.00, 4.95, 0.00, 99.00, 103.95),
(2, 1, 1, 12.0000, 20.00, 0.00, 10.000000, 5.00, 0.00, 0.00, 4.80, 0.00, 96.00, 100.80),
(3, 1, 1, 14.0000, 30.00, 0.00, 10.000000, 12.00, 0.00, 0.00, 11.76, 0.00, 98.00, 109.76);

-- --------------------------------------------------------

--
-- Table structure for table `purchase_payments`
--

CREATE TABLE `purchase_payments` (
  `id` int(11) NOT NULL,
  `purchase_id` int(11) NOT NULL,
  `pay_date` date NOT NULL,
  `mode` enum('CASH','UPI','BANK','CREDIT') NOT NULL DEFAULT 'CREDIT',
  `amount` decimal(14,2) NOT NULL DEFAULT 0.00,
  `reference_no` varchar(50) DEFAULT NULL,
  `note` varchar(255) DEFAULT NULL,
  `created_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `receipts`
--

CREATE TABLE `receipts` (
  `id` int(11) NOT NULL,
  `party_id` int(11) DEFAULT NULL,
  `receipt_date` date NOT NULL,
  `mode` enum('CASH','UPI','BANK') NOT NULL DEFAULT 'CASH',
  `amount` decimal(14,2) NOT NULL DEFAULT 0.00,
  `reference_no` varchar(50) DEFAULT NULL,
  `note` varchar(255) DEFAULT NULL,
  `is_cancelled` tinyint(1) NOT NULL DEFAULT 0,
  `cancelled_at` datetime DEFAULT NULL,
  `created_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `receipt_allocations`
--

CREATE TABLE `receipt_allocations` (
  `id` int(11) NOT NULL,
  `receipt_id` int(11) NOT NULL,
  `sales_id` int(11) NOT NULL,
  `amount` decimal(14,2) NOT NULL DEFAULT 0.00,
  `created_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `sales`
--

CREATE TABLE `sales` (
  `id` int(11) NOT NULL,
  `invoice_no` varchar(30) NOT NULL,
  `invoice_date` date NOT NULL,
  `invoice_type` enum('B2B','B2C') NOT NULL DEFAULT 'B2C',
  `customer_id` int(11) DEFAULT NULL,
  `gstin` varchar(20) DEFAULT NULL,
  `place_state_code` varchar(2) NOT NULL,
  `place_of_supply` varchar(80) DEFAULT NULL,
  `tax_type` enum('INTRA','INTER') NOT NULL,
  `tax_mode` enum('INCLUSIVE','EXCLUSIVE') NOT NULL,
  `freight_amount` decimal(14,2) NOT NULL DEFAULT 0.00,
  `freight_taxable` tinyint(1) NOT NULL DEFAULT 0,
  `other_charges` decimal(14,2) NOT NULL DEFAULT 0.00,
  `other_charges_taxable` tinyint(1) NOT NULL DEFAULT 0,
  `header_discount` decimal(14,2) NOT NULL DEFAULT 0.00,
  `taxable_total` decimal(14,2) NOT NULL DEFAULT 0.00,
  `tax_total` decimal(14,2) NOT NULL DEFAULT 0.00,
  `grand_total` decimal(14,2) NOT NULL DEFAULT 0.00,
  `created_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `sales`
--

INSERT INTO `sales` (`id`, `invoice_no`, `invoice_date`, `invoice_type`, `customer_id`, `gstin`, `place_state_code`, `place_of_supply`, `tax_type`, `tax_mode`, `freight_amount`, `freight_taxable`, `other_charges`, `other_charges_taxable`, `header_discount`, `taxable_total`, `tax_total`, `grand_total`, `created_at`) VALUES
(1, 'INV-000001', '2026-02-15', 'B2C', 1, '0', 'Ut', 'Fzdz', 'INTER', 'EXCLUSIVE', 0.00, 0, 0.00, 0, 0.00, 1200.00, 60.00, 1260.00, '2026-02-15 18:54:46'),
(2, 'INV-000002', '2026-02-15', 'B2C', NULL, '0', '09', '', 'INTRA', 'EXCLUSIVE', 0.00, 0, 0.00, 0, 0.00, 120.00, 6.00, 126.00, '2026-02-15 18:55:14'),
(3, 'INV-000003', '2026-02-15', 'B2C', NULL, '0', '09', '', 'INTRA', 'EXCLUSIVE', 0.00, 0, 0.00, 0, 0.00, 120.00, 6.00, 126.00, '2026-02-15 19:07:28');

-- --------------------------------------------------------

--
-- Table structure for table `sales_items`
--

CREATE TABLE `sales_items` (
  `id` int(11) NOT NULL,
  `sales_id` int(11) NOT NULL,
  `item_id` int(11) NOT NULL,
  `qty` decimal(14,4) NOT NULL,
  `discount_percent` decimal(5,2) NOT NULL DEFAULT 0.00,
  `discount_amount` decimal(14,2) NOT NULL DEFAULT 0.00,
  `base_rate` decimal(14,6) NOT NULL,
  `gst_percent` decimal(5,2) NOT NULL,
  `cgst_amt` decimal(14,2) NOT NULL DEFAULT 0.00,
  `sgst_amt` decimal(14,2) NOT NULL DEFAULT 0.00,
  `igst_amt` decimal(14,2) NOT NULL DEFAULT 0.00,
  `cst_amt` decimal(14,2) NOT NULL DEFAULT 0.00,
  `taxable_amount` decimal(14,2) NOT NULL DEFAULT 0.00,
  `line_total` decimal(14,2) NOT NULL DEFAULT 0.00,
  `issue_rate` decimal(14,6) NOT NULL DEFAULT 0.000000,
  `cogs_value` decimal(14,2) NOT NULL DEFAULT 0.00
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `sales_items`
--

INSERT INTO `sales_items` (`id`, `sales_id`, `item_id`, `qty`, `discount_percent`, `discount_amount`, `base_rate`, `gst_percent`, `cgst_amt`, `sgst_amt`, `igst_amt`, `cst_amt`, `taxable_amount`, `line_total`, `issue_rate`, `cogs_value`) VALUES
(1, 3, 1, 10.0000, 0.00, 0.00, 12.000000, 5.00, 3.00, 3.00, 0.00, 0.00, 120.00, 126.00, 10.000000, 100.00);

-- --------------------------------------------------------

--
-- Table structure for table `sales_payments`
--

CREATE TABLE `sales_payments` (
  `id` int(11) NOT NULL,
  `sales_id` int(11) NOT NULL,
  `pay_date` date NOT NULL,
  `mode` enum('CASH','UPI','BANK','CREDIT') NOT NULL DEFAULT 'CREDIT',
  `amount` decimal(14,2) NOT NULL DEFAULT 0.00,
  `reference_no` varchar(50) DEFAULT NULL,
  `note` varchar(255) DEFAULT NULL,
  `is_cancelled` tinyint(1) NOT NULL DEFAULT 0,
  `cancelled_at` datetime DEFAULT NULL,
  `created_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `sales_payments`
--

INSERT INTO `sales_payments` (`id`, `sales_id`, `pay_date`, `mode`, `amount`, `reference_no`, `note`, `is_cancelled`, `cancelled_at`, `created_at`) VALUES
(1, 1, '2026-02-15', 'CASH', 1000.00, '1121', NULL, 0, NULL, '2026-02-15 18:54:46');

-- --------------------------------------------------------

--
-- Table structure for table `stock_ledger`
--

CREATE TABLE `stock_ledger` (
  `id` bigint(20) NOT NULL,
  `txn_date` date NOT NULL,
  `ref_type` varchar(20) NOT NULL,
  `ref_id` int(11) NOT NULL,
  `ref_no` varchar(30) NOT NULL,
  `note` varchar(255) DEFAULT NULL,
  `item_id` int(11) NOT NULL,
  `in_qty` decimal(14,4) NOT NULL DEFAULT 0.0000,
  `out_qty` decimal(14,4) NOT NULL DEFAULT 0.0000,
  `rate_used` decimal(14,6) NOT NULL DEFAULT 0.000000,
  `value_in` decimal(14,2) NOT NULL DEFAULT 0.00,
  `value_out` decimal(14,2) NOT NULL DEFAULT 0.00,
  `balance_qty_after` decimal(14,4) NOT NULL DEFAULT 0.0000,
  `balance_value_after` decimal(14,2) NOT NULL DEFAULT 0.00,
  `avg_rate_after` decimal(14,6) NOT NULL DEFAULT 0.000000
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `stock_ledger`
--

INSERT INTO `stock_ledger` (`id`, `txn_date`, `ref_type`, `ref_id`, `ref_no`, `note`, `item_id`, `in_qty`, `out_qty`, `rate_used`, `value_in`, `value_out`, `balance_qty_after`, `balance_value_after`, `avg_rate_after`) VALUES
(1, '2026-02-15', 'PURCHASE', 1, 'PUR-1', '1212121', 1, 11.0000, 0.0000, 10.000000, 110.00, 0.00, 11.0000, 110.00, 10.000000),
(2, '2026-02-15', 'PURCHASE', 1, 'PUR-1', '1212121', 1, 12.0000, 0.0000, 10.000000, 120.00, 0.00, 23.0000, 230.00, 10.000000),
(3, '2026-02-15', 'PURCHASE', 1, 'PUR-1', '1212121', 1, 14.0000, 0.0000, 10.000000, 140.00, 0.00, 37.0000, 370.00, 10.000000),
(4, '2026-02-15', 'SALE', 2, 'INV-000002', '0', 1, 0.0000, 10.0000, 10.000000, 0.00, 100.00, 27.0000, 270.00, 10.000000),
(5, '2026-02-15', 'SALE', 3, 'INV-000003', '0', 1, 0.0000, 10.0000, 10.000000, 0.00, 100.00, 17.0000, 170.00, 10.000000);

-- --------------------------------------------------------

--
-- Table structure for table `stock_summary`
--

CREATE TABLE `stock_summary` (
  `item_id` int(11) NOT NULL,
  `qty_on_hand` decimal(14,4) NOT NULL DEFAULT 0.0000,
  `avg_rate` decimal(14,6) NOT NULL DEFAULT 0.000000,
  `stock_value` decimal(14,2) NOT NULL DEFAULT 0.00,
  `updated_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `stock_summary`
--

INSERT INTO `stock_summary` (`item_id`, `qty_on_hand`, `avg_rate`, `stock_value`, `updated_at`) VALUES
(1, 17.0000, 10.000000, 170.00, '2026-02-15 19:07:28');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `company_settings`
--
ALTER TABLE `company_settings`
  ADD PRIMARY KEY (`id`);

--
-- Indexes for table `credit_notes`
--
ALTER TABLE `credit_notes`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uniq_credit` (`credit_no`),
  ADD KEY `idx_cn_date` (`credit_date`),
  ADD KEY `fk_cn_sales` (`sales_id`),
  ADD KEY `fk_cn_customer` (`customer_id`);

--
-- Indexes for table `credit_note_items`
--
ALTER TABLE `credit_note_items`
  ADD PRIMARY KEY (`id`),
  ADD KEY `fk_cni_credit` (`credit_id`),
  ADD KEY `fk_cni_sales_item` (`sales_item_id`),
  ADD KEY `fk_cni_item` (`item_id`);

--
-- Indexes for table `masters_items`
--
ALTER TABLE `masters_items`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_item_name` (`item_name`);

--
-- Indexes for table `masters_parties`
--
ALTER TABLE `masters_parties`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_party_name` (`party_name`),
  ADD KEY `idx_party_type` (`party_type`),
  ADD KEY `idx_state_code` (`state_code`);

--
-- Indexes for table `purchases`
--
ALTER TABLE `purchases`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_purchase_date` (`invoice_date`),
  ADD KEY `fk_pur_supplier` (`supplier_id`);

--
-- Indexes for table `purchase_items`
--
ALTER TABLE `purchase_items`
  ADD PRIMARY KEY (`id`),
  ADD KEY `fk_pur_item_pur` (`purchase_id`),
  ADD KEY `fk_pur_item_item` (`item_id`);

--
-- Indexes for table `purchase_payments`
--
ALTER TABLE `purchase_payments`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_pp_purchase` (`purchase_id`),
  ADD KEY `idx_pp_date` (`pay_date`);

--
-- Indexes for table `receipts`
--
ALTER TABLE `receipts`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_rc_party_date` (`party_id`,`receipt_date`);

--
-- Indexes for table `receipt_allocations`
--
ALTER TABLE `receipt_allocations`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_ra_receipt` (`receipt_id`),
  ADD KEY `idx_ra_sales` (`sales_id`);

--
-- Indexes for table `sales`
--
ALTER TABLE `sales`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uniq_invoice` (`invoice_no`),
  ADD KEY `idx_sales_date` (`invoice_date`),
  ADD KEY `fk_sales_customer` (`customer_id`);

--
-- Indexes for table `sales_items`
--
ALTER TABLE `sales_items`
  ADD PRIMARY KEY (`id`),
  ADD KEY `fk_sales_item_sales` (`sales_id`),
  ADD KEY `fk_sales_item_item` (`item_id`);

--
-- Indexes for table `sales_payments`
--
ALTER TABLE `sales_payments`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_sp_sales` (`sales_id`),
  ADD KEY `idx_sp_date` (`pay_date`);

--
-- Indexes for table `stock_ledger`
--
ALTER TABLE `stock_ledger`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_led_item_date` (`item_id`,`txn_date`),
  ADD KEY `idx_led_ref` (`ref_type`,`ref_id`);

--
-- Indexes for table `stock_summary`
--
ALTER TABLE `stock_summary`
  ADD PRIMARY KEY (`item_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `credit_notes`
--
ALTER TABLE `credit_notes`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

--
-- AUTO_INCREMENT for table `credit_note_items`
--
ALTER TABLE `credit_note_items`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `masters_items`
--
ALTER TABLE `masters_items`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

--
-- AUTO_INCREMENT for table `masters_parties`
--
ALTER TABLE `masters_parties`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

--
-- AUTO_INCREMENT for table `purchases`
--
ALTER TABLE `purchases`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

--
-- AUTO_INCREMENT for table `purchase_items`
--
ALTER TABLE `purchase_items`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;

--
-- AUTO_INCREMENT for table `purchase_payments`
--
ALTER TABLE `purchase_payments`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `receipts`
--
ALTER TABLE `receipts`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `receipt_allocations`
--
ALTER TABLE `receipt_allocations`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `sales`
--
ALTER TABLE `sales`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;

--
-- AUTO_INCREMENT for table `sales_items`
--
ALTER TABLE `sales_items`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

--
-- AUTO_INCREMENT for table `sales_payments`
--
ALTER TABLE `sales_payments`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

--
-- AUTO_INCREMENT for table `stock_ledger`
--
ALTER TABLE `stock_ledger`
  MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;

--
-- Constraints for dumped tables
--

--
-- Constraints for table `credit_notes`
--
ALTER TABLE `credit_notes`
  ADD CONSTRAINT `fk_cn_customer` FOREIGN KEY (`customer_id`) REFERENCES `masters_parties` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `fk_cn_sales` FOREIGN KEY (`sales_id`) REFERENCES `sales` (`id`) ON DELETE CASCADE;

--
-- Constraints for table `credit_note_items`
--
ALTER TABLE `credit_note_items`
  ADD CONSTRAINT `fk_cni_credit` FOREIGN KEY (`credit_id`) REFERENCES `credit_notes` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `fk_cni_item` FOREIGN KEY (`item_id`) REFERENCES `masters_items` (`id`),
  ADD CONSTRAINT `fk_cni_sales_item` FOREIGN KEY (`sales_item_id`) REFERENCES `sales_items` (`id`);

--
-- Constraints for table `purchases`
--
ALTER TABLE `purchases`
  ADD CONSTRAINT `fk_pur_supplier` FOREIGN KEY (`supplier_id`) REFERENCES `masters_parties` (`id`);

--
-- Constraints for table `purchase_items`
--
ALTER TABLE `purchase_items`
  ADD CONSTRAINT `fk_pur_item_item` FOREIGN KEY (`item_id`) REFERENCES `masters_items` (`id`),
  ADD CONSTRAINT `fk_pur_item_pur` FOREIGN KEY (`purchase_id`) REFERENCES `purchases` (`id`) ON DELETE CASCADE;

--
-- Constraints for table `purchase_payments`
--
ALTER TABLE `purchase_payments`
  ADD CONSTRAINT `fk_pp_purchase` FOREIGN KEY (`purchase_id`) REFERENCES `purchases` (`id`) ON DELETE CASCADE;

--
-- Constraints for table `receipts`
--
ALTER TABLE `receipts`
  ADD CONSTRAINT `fk_rc_party` FOREIGN KEY (`party_id`) REFERENCES `masters_parties` (`id`) ON DELETE SET NULL;

--
-- Constraints for table `receipt_allocations`
--
ALTER TABLE `receipt_allocations`
  ADD CONSTRAINT `fk_ra_receipt` FOREIGN KEY (`receipt_id`) REFERENCES `receipts` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `fk_ra_sales` FOREIGN KEY (`sales_id`) REFERENCES `sales` (`id`) ON DELETE CASCADE;

--
-- Constraints for table `sales`
--
ALTER TABLE `sales`
  ADD CONSTRAINT `fk_sales_customer` FOREIGN KEY (`customer_id`) REFERENCES `masters_parties` (`id`) ON DELETE SET NULL;

--
-- Constraints for table `sales_items`
--
ALTER TABLE `sales_items`
  ADD CONSTRAINT `fk_sales_item_item` FOREIGN KEY (`item_id`) REFERENCES `masters_items` (`id`),
  ADD CONSTRAINT `fk_sales_item_sales` FOREIGN KEY (`sales_id`) REFERENCES `sales` (`id`) ON DELETE CASCADE;

--
-- Constraints for table `sales_payments`
--
ALTER TABLE `sales_payments`
  ADD CONSTRAINT `fk_sp_sales` FOREIGN KEY (`sales_id`) REFERENCES `sales` (`id`) ON DELETE CASCADE;

--
-- Constraints for table `stock_ledger`
--
ALTER TABLE `stock_ledger`
  ADD CONSTRAINT `fk_led_item` FOREIGN KEY (`item_id`) REFERENCES `masters_items` (`id`);

--
-- Constraints for table `stock_summary`
--
ALTER TABLE `stock_summary`
  ADD CONSTRAINT `fk_ss_item` FOREIGN KEY (`item_id`) REFERENCES `masters_items` (`id`);


-- --------------------------------------------------------
-- Feature upgrade v22: Barcode/SKU, Batch/Expiry, Credit Control, Users/Roles, Backup/Restore, GST Export
-- --------------------------------------------------------

-- Add barcode/sku and batch/expiry flags in item master
ALTER TABLE `masters_items`
  ADD COLUMN `sku` varchar(50) NOT NULL DEFAULT '' AFTER `item_name`,
  ADD COLUMN `barcode` varchar(60) NOT NULL DEFAULT '' AFTER `sku`,
  ADD COLUMN `enable_batch` tinyint(1) NOT NULL DEFAULT 0 AFTER `mrp`,
  ADD COLUMN `enable_expiry` tinyint(1) NOT NULL DEFAULT 0 AFTER `enable_batch`;

CREATE INDEX `idx_items_barcode` ON `masters_items` (`barcode`);
CREATE INDEX `idx_items_sku` ON `masters_items` (`sku`);

-- Add batch & expiry to purchase/sales item lines
ALTER TABLE `purchase_items`
  ADD COLUMN `batch_no` varchar(50) NOT NULL DEFAULT '' AFTER `gst_percent`,
  ADD COLUMN `expiry_date` date NULL AFTER `batch_no`;

ALTER TABLE `sales_items`
  ADD COLUMN `batch_no` varchar(50) NOT NULL DEFAULT '' AFTER `gst_percent`,
  ADD COLUMN `expiry_date` date NULL AFTER `batch_no`;

-- Stock ledger batch info
ALTER TABLE `stock_ledger`
  ADD COLUMN `batch_no` varchar(50) NOT NULL DEFAULT '' AFTER `item_id`,
  ADD COLUMN `expiry_date` date NULL AFTER `batch_no`;

-- Batch FIFO store
CREATE TABLE IF NOT EXISTS `stock_batches` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `item_id` int(11) NOT NULL,
  `batch_no` varchar(50) NOT NULL DEFAULT '',
  `expiry_date` date NULL,
  `purchase_id` int(11) NULL,
  `purchase_item_id` int(11) NULL,
  `created_at` datetime NOT NULL,
  `qty_in` decimal(12,3) NOT NULL DEFAULT 0,
  `qty_out` decimal(12,3) NOT NULL DEFAULT 0,
  `qty_balance` decimal(12,3) NOT NULL DEFAULT 0,
  `cost_rate` decimal(12,2) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  KEY `idx_batch_item` (`item_id`),
  KEY `idx_batch_fifo` (`item_id`,`expiry_date`,`created_at`,`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE `company_settings`
  ADD COLUMN `state` varchar(60) NOT NULL DEFAULT '' AFTER `gstin`;

-- Company settings: credit control & WhatsApp/SMS link templates
ALTER TABLE `company_settings`
  ADD COLUMN `credit_control_mode` enum('OFF','WARN','BLOCK') NOT NULL DEFAULT 'OFF' AFTER `cst_percent_default`,
  ADD COLUMN `whatsapp_share_template` varchar(255) NOT NULL DEFAULT 'https://wa.me/?text={MESSAGE}' AFTER `credit_control_mode`,
  ADD COLUMN `sms_share_template` varchar(255) NOT NULL DEFAULT '' AFTER `whatsapp_share_template`;


COMMIT;

