# Fortnight System Setup Guide

## Overview
The new Fortnight system has been completely rebuilt to match the Excel design from your screenshots. All old code and database tables have been removed and replaced with a new Excel-like interface.

## Features Implemented

### ✅ Excel-like Design
- Exact replication of Excel spreadsheet layout
- Color-coded cells (blue for purchase, green for consumption, yellow for stock)
- Category sections with headers
- Total rows for each category
- Grand total row at the top
- Summary section on the right

### ✅ Month-wise Data Entry
- Date-based navigation (like Excel tabs)
- Each date creates a separate period
- Easy navigation between dates
- "+ New" button to create today's entry

### ✅ Role-Based Access Control
- **Admin/HR Admin**: Full access (view, edit, manage access)
- **Registered Employees**: Can access if admin grants permission
- Admin can grant/revoke access to any employee
- Admin can set view-only or edit permissions

### ✅ Real-time Calculations
- Opening Stock Value = Opening Stock Qty × Net Rate
- Purchase Value = Purchase Qty × Net Rate
- Consumption Value = Consumption Qty × Net Rate
- Closing Stock Qty = Opening Stock Qty + Purchase Qty - Consumption Qty
- Closing Stock Value = Closing Stock Qty × Net Rate
- All calculations update automatically as you type

### ✅ Month/Year Filtering (Admin Only)
- Admin can filter data by month and year
- View historical data easily
- Navigate to any date

### ✅ Data Saving
- All data is saved to database
- Period locking prevents accidental edits
- Supplier name and Bill/Challan No tracking

## Database Setup

### Step 1: Run Database Setup
Run the database setup script:

```bash
php database/run_fortnight_setup.php
```

Or manually run the SQL file:
```sql
source fortnight_new_setup.sql
```

This will:
- Drop all old Fortnight tables
- Create new tables with proper structure
- Insert default categories (Grocery, Dairy, Vegetable, LPG Gas, Non-Veg, Beverage, Cleaning, Cash Purchase)

### Step 2: Add Inventory Items
You need to add inventory items (materials) to each category. You can do this by:
1. Accessing the database directly
2. Or creating an admin interface for managing items (future enhancement)

Example SQL to add items:
```sql
INSERT INTO inventory_items (category_id, material_code, material_name, unit, net_rate, site_id, display_order) VALUES
(1, 'PR 1', 'AJWAIN', 'KG', 100.00, 0, 1),
(1, 'PR 2', 'SOYA SAUCE', 'BOTT', 70.00, 0, 2),
(2, 'DR 1', 'AMULTAZA', 'PKT', 50.00, 0, 1);
```

## Usage

### For Admin/HR Admin

1. **Access Fortnight Module**
   - Navigate to: `/modules/fortnight/list.php`
   - You'll see the Excel-like interface

2. **Grant Access to Employees**
   - Click "Manage Access" button in the header
   - Or navigate to: `/modules/fortnight/access.php`
   - Select an employee and click "Grant Access"
   - Choose "Allow data entry" for edit access, or leave unchecked for view-only

3. **Enter Data**
   - Select a date (or use today's date)
   - Enter quantities in:
     - Opening Stock Qty (yellow background)
     - Purchase Qty (blue background)
     - Consumption Qty (green background)
   - Enter Supplier Name and Bill/Challan No
   - Click "Save Data"

4. **Filter by Month/Year**
   - Use the filter dropdowns at the top (Admin only)
   - Select month and year to view historical data

### For Employees with Access

1. **Access Fortnight Module**
   - Navigate to: `/modules/fortnight/list.php`
   - You'll see the Excel-like interface

2. **Enter Data** (if edit permission granted)
   - Select a date
   - Enter quantities and supplier information
   - Click "Save Data"

3. **View Data** (if view-only permission)
   - Can view all data but cannot edit
   - All fields are read-only

## Database Structure

### Tables

1. **inventory_categories**
   - Stores material categories (Grocery, Dairy, etc.)

2. **inventory_items**
   - Stores individual materials/items
   - Links to categories
   - Contains material code, name, unit, net rate

3. **fortnight_periods**
   - Stores date-based periods
   - One record per date per site
   - Supports locking to prevent edits

4. **fortnight_inventory**
   - Main data entry table
   - Stores all quantities and values
   - Links period + item
   - Calculated values stored for reporting

5. **fortnight_access**
   - Manages employee access permissions
   - Tracks who granted access and when

## File Structure

```
modules/fortnight/
├── list.php          # Main Excel-like interface
├── access.php        # Access management (Admin only)
├── create.php        # (Old - can be removed)
├── export.php        # (Old - can be removed)
├── lock.php          # (Old - can be removed)
└── unlock.php        # (Old - can be removed)

database/
└── run_fortnight_setup.php  # Database setup script

fortnight_new_setup.sql      # SQL setup file
```

## Color Coding

- **Grey Headers**: #808080 - Table column headers
- **Orange**: #ffa500 - Category headers and total rows
- **Light Blue**: #cfe2f3 - Purchase quantity input fields
- **Light Green**: #d9ead3 - Net Rate, Consumption quantity input fields
- **Yellow**: #fff2cc - Opening Stock fields, summary table headers
- **Calculated Fields**: #f0f0f0 - Read-only calculated values

## Notes

- The system automatically creates periods when you select a date
- Calculations happen in real-time as you type
- Data is saved per date, allowing month-wise tracking
- Admin can filter and view any month/year combination
- Locked periods cannot be edited (future enhancement for period locking)

## Troubleshooting

### No items showing in the table
- Make sure inventory items are added to the database
- Check that items have `is_active = 1`
- Verify category_id matches inventory_categories

### Access denied error
- Admin/HR Admin have automatic access
- Employees need to be granted access by admin
- Check `fortnight_access` table

### Calculations not working
- Check browser console for JavaScript errors
- Ensure all input fields have correct data attributes
- Verify net_rate values are set for items

## Future Enhancements

- Period locking functionality
- Export to Excel
- Bulk item import
- Item management interface
- Reports and analytics
- Opening stock auto-calculation from previous period

