# Purchase Module Setup - Complete Guide

## ✅ Completed Tasks

### 1. Database Tables Created
All purchase-related database tables have been successfully created in `uzrs-inventorydb`:

- **`tblpurchases`** - Main purchase invoices table
- **`tblpurchases_details`** - Purchase line items
- **`tblpurchases_gst_details`** - GST breakdown per product
- **`tblpurchase_type_config`** - Purchase types (GST/NON-GST/IMPORT)
- **`tblhold_purchase_bills`** - Held/draft purchase bills
- **`tblsupplier_accounts`** - Supplier ledger/accounts
- **`tblpurchase_expenses`** - Purchase-related expenses
- **`vw_purchases_with_discounts`** - Purchase view with calculations

### 2. Files Renamed (Main Folder)
- `add-customer-modal.php` → `add-supplier-modal.php`
- `customer-ledger-report.php` → `supplier-ledger-report.php`
- `print-customer-ledger.php` → `print-supplier-ledger.php`
- `loyalty-points-modal.php` → `supplier-info-modal.php`

### 3. API Files Renamed
- `add-customer.php` → `add-supplier.php`
- `get-customer-address.php` → `get-supplier-address.php`
- `get-customer-ledger.php` → `get-supplier-ledger.php`
- `delete-sale.php` → `delete-purchase.php`
- `save-sale.php` → `save-purchase.php`
- `get-sales-types.php` → `get-purchase-types.php`
- `get-loyalty-points.php` → `get-supplier-info.php`
- `get-loyalty-settings.php` → `get-purchase-settings.php`
- `manage-loyalty-points.php` → `manage-supplier-credits.php`
- `update-loyalty-settings.php` → `update-purchase-settings.php`
- `get-outstanding-invoices.php` → `get-outstanding-purchases.php`

### 4. Code Updates Started
- Updated `index.php` header section with purchase terminology
- Updated `save-purchase.php` to use purchase tables
- Database queries updated to use `tblpurchases`, `tblsuppliers`, etc.

---

## 🔄 Remaining Tasks - Find & Replace Required

The following find/replace operations need to be applied across ALL files in the Purchase folder:

### Database Tables
| Find | Replace |
|------|---------|
| `tblsales` | `tblpurchases` |
| `tblsales_details` | `tblpurchases_details` |
| `tblsales_gst_details` | `tblpurchases_gst_details` |
| `tblsales_type_config` | `tblpurchase_type_config` |
| `tblhold_bills` | `tblhold_purchase_bills` |
| `tblcustomers` | `tblsuppliers` |
| `tblcustomer_accounts` | `tblsupplier_accounts` |
| `tblcustomer_loyalty_points` | (Remove - not needed for purchases) |

### Variables & Fields
| Find | Replace |
|------|---------|
| `customerId` | `supplierId` |
| `CustomerID` | `SupplierID` |
| `CustomerName` | `SupplierName` |
| `CustomerCode` | `SupplierCode` |
| `customerSelect` | `supplierSelect` |
| `customerSearch` | `supplierSearch` |
| `customerSuggestions` | `supplierSuggestions` |
| `saleId` | `purchaseId` |
| `SaleID` | `PurchaseID` |
| `saleDate` | `purchaseDate` |
| `SaleDate` | `PurchaseDate` |
| `salesType` | `purchaseType` |
| `SalesType` | `PurchaseType` |
| `cooliAmount` | `freightAmount` |
| `CooliAmount` | `FreightAmount` |
| `Mobile` | `PhoneNumber` |
| `CurrentBalance` | (Remove or adapt for supplier credits) |
| `LoyaltyPoints` | (Remove - not applicable) |

### UI Text & Labels
| Find | Replace |
|------|---------|
| `"Select Customer"` | `"Select Supplier"` |
| `"Customer"` | `"Supplier"` |
| `"Sales"` | `"Purchase"` |
| `"Sale"` | `"Purchase"` |
| `"New Customer"` | `"New Supplier"` |
| `"Customer Payments"` | `"Supplier Payments"` |
| `"Loyalty Points"` | `"Supplier Info"` |
| `"Invoice"` | `"Purchase Invoice"` |

### JavaScript Functions
| Find | Replace |
|------|---------|
| `saveSale()` | `savePurchase()` |
| `deleteSale()` | `deletePurchase()` |
| `getSaleDetails()` | `getPurchaseDetails()` |
| `loadCustomers()` | `loadSuppliers()` |
| `selectCustomer()` | `selectSupplier()` |
| `addCustomer()` | `addSupplier()` |

### API Endpoints (in JavaScript files)
| Find | Replace |
|------|---------|
| `api/save-sale.php` | `api/save-purchase.php` |
| `api/delete-sale.php` | `api/delete-purchase.php` |
| `api/get-customer` | `api/get-supplier` |
| `api/add-customer.php` | `api/add-supplier.php` |
| `api/get-sales-types.php` | `api/get-purchase-types.php` |
| `api/get-loyalty` | `api/get-supplier` |

---

## 📝 Files Requiring Manual Review

These files may need special attention beyond find/replace:

1. **index.php** - Main purchase interface
   - Update all customer dropdown logic to supplier
   - Remove loyalty points section
   - Update payment modal logic
   - Update all JavaScript event handlers

2. **products.php** - Product selection
   - May need purchase-specific pricing logic
   - Update to show purchase rates instead of sales rates

3. **api/save-purchase.php** - Save purchase logic
   - Update stock increase logic (purchase increases stock)
   - Remove loyalty points calculations
   - Update supplier account ledger entries
   - Reverse debit/credit for supplier accounts

4. **api/delete-purchase.php** - Delete purchase
   - Update stock decrease logic (deleting purchase decreases stock)
   - Update supplier account reversal

5. **api/get-purchase-types.php** - Get purchase types
   - Query `tblpurchase_type_config` table

6. **js/sales.js** - Rename to `js/purchase.js`
   - Update all functions and variables
   - Update all API endpoints
   - Remove loyalty points logic

7. **print-invoice.php** - Rename to `print-purchase-invoice.php`
   - Update to show supplier details instead of customer
   - Update terminology throughout

8. **gst-details-modal.php**
   - Update to use purchase GST details

---

## 🔍 Special Considerations

### Stock Management
**IMPORTANT**: Purchase and Sales have OPPOSITE effects on inventory:
- **Sales**: Decrease stock (`CurrentStock = CurrentStock - Quantity`)
- **Purchase**: Increase stock (`CurrentStock = CurrentStock + Quantity`)

### Supplier Accounts vs Customer Accounts
- **Customer**: When they buy, it's a DEBIT (they owe us)
- **Supplier**: When we buy, it's a CREDIT (we owe them)
- Payment direction is reversed

### Removed Features for Purchase
- Loyalty Points (customer-only feature)
- Shipping Details (may not be needed for purchase)
- Some payment modes might differ

---

## 🛠️ Quick Fix Script

You can use VS Code's "Find in Files" (Ctrl+Shift+H) to do batch replacements:

1. Search location: `Purchase` folder
2. Use "Replace in Files"
3. Apply each find/replace pair from the tables above
4. Review changes before saving

---

## ✨ Testing Checklist

After completing all changes:

- [ ] Can load Purchase module without errors
- [ ] Can select supplier from dropdown
- [ ] Can add products to purchase cart
- [ ] Can save purchase successfully
- [ ] Stock increases correctly after purchase
- [ ] Supplier account is updated (credit increases)
- [ ] Can print purchase invoice
- [ ] Can hold and retrieve purchase bills
- [ ] GST calculations work correctly
- [ ] Can edit existing purchase
- [ ] Can delete purchase (reverses stock and supplier account)

---

## 📊 Database Verification

Run these queries to verify tables:

```sql
-- Check purchase tables
SHOW TABLES LIKE 'tblpurchase%';

-- Check supplier tables
SHOW TABLES LIKE '%supplier%';

-- Verify purchase type config data
SELECT * FROM tblpurchase_type_config;
```

---

## 🎯 Quick Start

1. All database tables are ready ✅
2. File structure is set up ✅
3. Apply find/replace operations from this guide
4. Test each major function
5. Fix any errors in console or PHP logs

---

**Created**: November 4, 2025  
**Database**: uzrs-inventorydb  
**Module**: Purchase Management System
