Resetdiscount Documentation

Reset Discount Controller Documentation

File: /controllers/resetdiscount.php

Purpose: Utility controller for fixing restaurant discount calculations on specific dates

Last Updated: December 20, 2024

Total Functions: 2

Lines of Code: ~60

---

๐Ÿ“‹ Overview

The Reset Discount Controller is a specialized utility for correcting restaurant discount calculations. It handles:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables (Indirect Operations)

Table NamePurposeKey Columns
**sellbill**Sales bills with discount datasellbillid, sellbilldiscount, sellbilldiscounttype, sellbilldate, sellbilltotalbill
**youtubelink**Tutorial video linksyoutubelinkid, title, url, category
### Referenced Tables (via Extended DAO)

Table NamePurposeKey Columns
**sellbilldetail**Bill line itemssellbilldetailid, sellbillid, sellbilldetailprice, sellbilldetaildiscount
**programsettings**System configurationprogramsettingsid, settingkey, settingvalue
**user**System usersuserid, username, usergroupid
**usergroup**User permissionsusergroupid, groupname, permissions
---

๐Ÿ”‘ Key Functions

1. Default Action (empty $do) - Discount Fix Interface

Location: Line 39

Purpose: Display interface for fixing restaurant discounts with date selection

Process Flow:

1. Check user authentication and permissions

2. Process discount fix if date provided

3. Load YouTube tutorial links for assistance

4. Display fix interface template

5. Show results if operation performed

Function Logic:

$billdate = filter_input(INPUT_POST, "billdate");
if (isset($billdate) && !empty($billdate)) {
    $rowsAffected = $sellbillEX->fixDiscountRestaurantFatma($billdate);
    $smarty->assign("rowsAffected", $rowsAffected);
}

Features:

---

2. fixDiscountRestaurantFatma() - Core Fix Logic

Location: Called from SellbillMySqlExtDAO

Purpose: Execute restaurant-specific discount corrections

Process Flow:

1. Query sales bills for specified date

2. Identify discount calculation errors

3. Apply corrected discount formulas

4. Update affected records

5. Return count of rows modified

Expected Corrections:

---

๐Ÿ”„ Workflows

Workflow 1: Discount Correction Process

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Access Discount Fix Tool
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1User Authentication
- Verify user login
- Check administrative permissions
- Load user session data
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Display Fix Interface
- Show date selection form
- Display YouTube tutorial links
- Present previous operation results
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3User Selects Date
- Choose specific bill date
- Submit form for processing
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Execute Discount Fix
- Call fixDiscountRestaurantFatma()
- Process all bills for selected date
- Apply discount corrections
- Count affected records
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Display Results
- Show number of rows affected
- Confirm completion status
- Provide success feedback
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty)Default actionShow discount fix interface
### Form Parameters

POST Data:

---

๐Ÿ”’ Security & Permissions

Access Control

include_once("../public/authentication.php");

Security Features:

Permission Requirements

---

๐Ÿงฎ Discount Fix Logic

Restaurant-Specific Corrections

The fixDiscountRestaurantFatma() function likely corrects:

1. Discount Type Mismatches:

   -- Fix percentage discounts calculated as fixed amounts
   UPDATE sellbill SET 
   sellbillaftertotalbill = sellbilltotalbill - (sellbilltotalbill * sellbilldiscount / 100)
   WHERE sellbilldiscounttype = 0 AND sellbilldate = ?
   ```

2. **Fixed Amount Corrections**:
   ```sql
   -- Fix fixed discounts
   UPDATE sellbill SET 
   sellbillaftertotalbill = sellbilltotalbill - sellbilldiscount
   WHERE sellbilldiscounttype = 1 AND sellbilldate = ?
   ```

3. **Tax Recalculations**:
   ```sql
   -- Recalculate tax on corrected amounts
   UPDATE sellbill SET 
   tax_amount = (sellbillaftertotalbill * tax_rate / 100)
   WHERE sellbilldate = ?
   ```

---

## ๐Ÿ“Š Performance Considerations

### Batch Operation Impact
- Processes all bills for specified date
- May affect hundreds or thousands of records
- Database locks during updates
- Potential timeout for large date ranges

### Optimization Strategies
1. **Index Usage**:
   - Ensure index on `sellbill(sellbilldate)`
   - Consider composite index for discount operations

2. **Batch Size Management**:
   - Process in smaller batches if needed
   - Add progress feedback for large operations

---

## ๐Ÿ› Common Issues & Troubleshooting

### 1. **No Rows Affected**
**Issue**: Fix operation returns 0 rows affected  
**Possible Causes**:
- No bills exist for selected date
- Bills already have correct discount calculations
- Date format incorrect

**Debug Steps**:
sql

-- Check if bills exist for date

SELECT COUNT(*) FROM sellbill WHERE DATE(sellbilldate) = 'YYYY-MM-DD';

-- Check discount types and values

SELECT sellbillid, sellbilldiscount, sellbilldiscounttype,

sellbilltotalbill, sellbillaftertotalbill

FROM sellbill WHERE DATE(sellbilldate) = 'YYYY-MM-DD';

### 2. **Timeout Errors**
**Issue**: Operation times out on large datasets  
**Solution**: Process smaller date ranges or implement batch processing

### 3. **Permission Denied**
**Issue**: User cannot access fix function  
**Solution**: Verify user has administrative permissions

---

## ๐Ÿงช Testing Scenarios

### Test Case 1: Basic Discount Fix

1. Create test bills with incorrect discounts

2. Run fix for specific date

3. Verify discount calculations corrected

4. Check row count matches expected

### Test Case 2: Edge Cases

1. Test with date having no bills

2. Test with already correct discounts

3. Test with invalid date formats

4. Verify appropriate responses

### Test Case 3: Performance Testing

1. Create large number of bills for test date

2. Monitor execution time

3. Verify all records processed

4. Check database performance impact

---

## ๐Ÿ’ก Tutorial Integration

### YouTube Links Feature
php

$youtubes = $youtubeLinkDAO->queryAll();

$smarty->assign("youtubes", $youtubes);

```

Purpose:

Display Options:

---

๐Ÿ“š Business Context

When to Use This Tool

1. After System Updates: When discount calculation logic changes

2. Data Migration: After importing bills with incorrect discounts

3. Error Correction: When bugs in discount calculation are discovered

4. Audit Compliance: To ensure consistent discount application

5. Restaurant Specific Issues: Address unique restaurant discount scenarios

Typical Scenarios

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When major changes occur