Bills Documentation

Bills Controller Documentation

File: /controllers/bills.php

Purpose: Comprehensive billing system for sales and purchase operations with workshop management

Last Updated: December 20, 2024

Total Functions: 25+

Lines of Code: ~1,979

---

๐Ÿ“‹ Overview

The Bills Controller is the main billing system hub that handles both sales (bills) and purchase (buy bills) operations. It provides:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Bill Tables

Table NamePurposeKey Columns
**bills**Sales bills masterid, billno, clientid, finalnetbillvalue, billdate, insurance, approved
**billsproduct**Sales bill line itemsid, billid, productid, productno, productprice
**billsbuy**Purchase bills masterid, billno, clientid, finalnetbillvalue, billdate
**billsproductsbuy**Purchase bill line itemsid, billid, productid, productno, productprice
### Workshop Management Tables

Table NamePurposeKey Columns
**toworkshoporder**Workshop delivery ordersid, orderNo, driverId, workshopId, orderStatus, deliverWorkshopDate
**toworkshoporderbill**Bills in workshop ordersid, billid, toworkshoporderid
**workshop**Workshop definitionsid, workshopname
### Client & Supplier Tables

Table NamePurposeKey Columns
**client**Customer masterclientid, clientname, clientdebt
**supplier**Supplier mastersupplierid, suppliername
**clientarea**Customer areas/regionsid, name
### Payment & Insurance Tables

Table NamePurposeKey Columns
**insurancecompanie**Insurance companiesid, name
**paymentnetwork**Payment networksid, networkname
**bankaccount**Bank accountsid, accountname, bankid
**save**Cash registerssaveid, savename
### Product & Inventory Tables

Table NamePurposeKey Columns
**product**Product masterproductId, productName, productCatId
**storedetail**Inventory quantitiesstoredetailid, storeid, productid, productquantity
**productrequest**Product requestsid, productid, quantity, status
### System Tables

Table NamePurposeKey Columns
**branch**System branchesbranchId, branchName
**user**System usersuserid, username, employeename
**usergroup**User groupsusergroupid, directSaveBills, percentageDiscount
**programsettings**System settingsprogramsettingsid, settingkey, settingvalue
---

๐Ÿ”‘ Key Functions

1. Default Action / addview2 - Bill Creation Form

Location: Line 267

Purpose: Display bill creation form with initialization

Function Signature:

// Triggered when: do=empty, do=addview2
if (!isset($_GET['do']) || $_GET['do'] == "empty" || $_GET['do'] == "addview2")

Process Flow:

1. Generate bill number based on branch and year

2. Load dropdown data (clients, stores, insurance, etc.)

3. Handle product requests if coming from request system

4. Set direct save permissions based on user group

5. Display appropriate template (add.html or add2.html)

Bill Number Generation:

$branchId = $_SESSION['branchId'];
$today = date('y');
$billNo = $branchId . $today;
$bills = $billsEX->searchInBillNo($billNo);
if (count($bills) > 0) {
    $billNo = $bills->billno + 1;
} else {
    $billNo .= '00001';
}

---

2. add() - Sales Bill Creation

Location: Line 441

Purpose: Process sales bill creation with validation

Function Signature:

elseif ($_GET['do'] == "add")

Process Flow:

1. Call add(0) function from included billsfunctions.php

2. Process result for inventory warnings

3. Handle product request completion

4. Generate appropriate redirect based on success/failure

5. Update kashf (medical examination) usage if applicable

Error Handling:

if ($detailResult['flag'] == 99) {
    $note = "ุฑู‚ู… ุงู„ูุงุชูˆุฑุฉ ู…ูˆุฌูˆุฏ ู…ู† ู‚ุจู„";
    $smarty->assign('errorNote', $note);
    $smarty->display("error.html");
}

---

3. edit() - Bill Editing Interface

Location: Line 556

Purpose: Load bill for editing with all related data

Function Signature:

elseif ($_GET['do'] == "edit")

Process Flow:

1. Get bill ID from request

2. Load complete bill details via getBillDetails()

3. Load all dropdown data (products, clients, stores)

4. Display edit template with populated data

---

4. print / printbuy - Bill Printing

Location: Line 376 / 408

Purpose: Generate printable bill format

Print Types:

Process Flow:

1. Load complete bill details

2. Determine print format based on type parameter

3. Display appropriate print template

4. Handle lens-specific formatting if requested

---

5. show() - Bills Listing

Location: Line 705

Purpose: Display bills with filtering and summary information

Function Signature:

elseif ($do == "show")

Process Flow:

1. Load user group permissions

2. Generate bill number lists for filtering tabs:

- All bills

- Pending insurance bills

- Approved insurance bills

- Rejected insurance bills

3. Query today's bills with totals

4. Calculate daily balance summary

5. Display tabbed interface

Daily Summary Calculation:

$totalBalance = 0;
foreach ($bills as $bill) {
    if ($bill->deleted == 0) {
        $totalBalance = $totalBalance + $bill->finalnetbillvalue;
    }
}

---

6. billtransport - Workshop Transportation

Location: Line 757

Purpose: Manage bill transportation to workshops and branches

Process Flow:

1. Load new bills ready for transport

2. Load drivers, workshops, and branches data

3. Process bill product details and categories

4. Handle delivery orders with filtering options

5. Track order status and delays

6. Manage returned bills from workshops

Bill Status Updates:

---

7. toworkshop() - Send to Workshop

Location: Line 1572

Purpose: Create workshop delivery order

Function Signature:

function toworkshop()

Process Flow:

1. Generate unique order number

2. Create workshop order record

3. Link bills to order

4. Update bill status to "at workshop"

5. Track driver and delivery timeouts

6. Return order ID for printing

Order Number Generation:

do {
    $unique = uniqid("0");
} while (isUnique($unique));

---

8. addBuy / updateBuy - Purchase Bill Operations

Location: Line 1125 / 1286

Purpose: Handle purchase bill creation and updates

Process Flow:

1. Generate buy bill number

2. Load supplier and bank data

3. Process purchase bill details

4. Handle inventory updates

5. Generate accounting entries

6. Manage product requests

---

9. saveDailyEntryDelete() - Accounting Integration

Location: Line 1337

Purpose: Generate reversal accounting entries for deleted bills

Accounting Structure:

// For sales bill deletion:
// Credit: Customer account (waitvalue)
// Credit: Insurance company (if applicable) 
// Credit: Cash register (if cash/card)
// Debit: Sales revenue account

---

๐Ÿ”„ Workflows

Workflow 1: Sales Bill Creation

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Create Sales Bill
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Initialize Bill Form
- Generate bill number
- Load customer dropdown
- Load store and product data
- Set user permissions
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2User Fills Bill Details
- Select customer
- Add products and quantities
- Set payment terms
- Configure insurance if applicable
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Validate and Process Bill
- Check product availability
- Validate bill number uniqueness
- Calculate totals and taxes
- Process payment allocations
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Update Inventory and Accounts
- Decrease product quantities
- Update customer debt
- Generate daily accounting entries
- Process insurance claims
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Complete and Output
- Save bill to database
- Mark product requests as fulfilled
- Generate print output or redirect
- Send for workshop processing if needed
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Workflow 2: Workshop Transportation

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Workshop Transportation
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Prepare Bills for Workshop
- Select bills with status "new"
- Group by product categories
- Choose driver and workshop
- Set delivery timeouts
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Create Delivery Order
- Generate unique order number
- Link selected bills to order
- Update bill status to "at workshop"
- Set delivery dates and timeouts
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Track Workshop Processing
- Monitor delivery timeouts
- Track workshop completion
- Handle delays and exceptions
- Prepare for return delivery
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Manage Return Process
- Assign return driver
- Update bill status to "returned"
- Track return delivery times
- Complete order fulfillment
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty)Default formBill creation form (normal)
`do=addview2`Category formBill creation form (with categories)
`do=add``add()`Process sales bill creation
`do=edit`Edit interfaceLoad bill for editing
`do=update``update()`Process bill updates
`do=delete`Delete processMark bill as deleted
`do=print`Print interfaceGenerate bill printout
`do=show`Bills listingDisplay bills with filters
`do=billtransport`Workshop transportManage workshop deliveries
`do=toworkshop``toworkshop()`Create workshop order
`do=buybill`Purchase formPurchase bill creation
`do=addbuybill``addBuy()`Process purchase bill
`do=showbuybill`Purchase listingDisplay purchase bills
---

๐Ÿงฎ Calculation Methods

Bill Number Generation

$branchId = $_SESSION['branchId'];
$today = date('y');
$billNo = $branchId . $today . '00001'; // Format: [Branch][Year][Sequence]

Payment Processing

// Cash and card payments to save account
if ($bills->cash == 1 || $bills->card == 1) {
    $saveValue = $bills->cashvalue + $cardRest;
}

// Insurance processing with discount
$insValue = $bills->companycarry;
$insDiscount = $insValue * ($bills->insdiscountpercent) / 100;
$insRest = $insValue - $insDiscount;

Workshop Timeout Calculations

$hours = dateTimeDiff($order->recieveBranchDate, $order->deliverBranchDate);
if ($hours > $order->drivertimeoutback) {
    $order->driverdelayback = $hours;
}

---

๐Ÿ”’ Security & Permissions

User Group Permissions

$usergroup = $UsergroupDAO->load($_SESSION['usergroupid']);
if ($userdata->directSaveBills == 1 && $usergroup->directSaveBills == 1) {
    $directSave = 1; // Allow direct saving without confirmation
}

Bill Access Control

---

๐Ÿ“Š Performance Considerations

Database Optimization

1. Indexes Required:

- bills(billdate, deleted)

- billsproduct(billid, productid)

- toworkshoporder(orderStatus, orderNo)

2. Query Optimization:

- Date-based filtering for daily operations

- Status-based queries for workshop management

- Batch operations for multiple bill processing

---

๐Ÿ› Common Issues & Troubleshooting

1. Duplicate Bill Numbers

Issue: Bill number already exists error

Cause: Race condition in bill number generation

Debug:

SELECT MAX(billno) FROM bills WHERE billno LIKE '[Branch][Year]%';

2. Workshop Order Tracking

Issue: Orders show wrong status

Cause: Status updates not properly synchronized

Fix: Verify status update transactions are atomic

3. Insurance Processing Errors

Issue: Insurance calculations incorrect

Cause: Percentage vs fixed amount confusion

Debug: Check discounttype field values

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When major changes occur