Billsfunctions Documentation

Bills Functions Controller Documentation

File: /controllers/billsfunctions.php

Purpose: Core billing system utilities for optical sales operations with payment processing

Last Updated: December 20, 2024

Total Functions: 25+

Lines of Code: ~1,978

---

๐Ÿ“‹ Overview

The Bills Functions Controller is the core utility library for the ERP's optical billing system. It provides comprehensive functionality for:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Bill Tables

Table NamePurposeKey Columns
**bills**Sales bill masterbillid, clientid, billno, finalnetbillvalue, waitvalue, kashfid, dailyentryid
**billsproducts**Bill line itemsid, billid, productid, productno, productprice, producttotalprice, storeid
**doctorkashf**Eye examination recordsid, customerid, kashfid, doctorid, date, sphere/cylinder/axis values
**kashf**Medical examination masterid, customerid, kashfvalue, kashftype, doctorid, paystatus
### Financial Tables

Table NamePurposeKey Columns
**clientdebtchange**Customer debt trackingclientdebtchangeid, clientid, clientdebtchangeamount, clientdebtchangetype, tablename
**dailyentry**Accounting journal entriesdailyentryid, dDateTime, entryComment, fromFlag
**dailyentrycreditor**Credit entriesid, dailyentryid, accountstreeid, value, pluginControllerName
**dailyentrydebtor**Debit entriesid, dailyentryid, accountstreeid, value, pluginControllerName
**storereport**Inventory movement logstorereportid, productid, storeid, productbefore, productafter, tablename
### Master Data Tables

Table NamePurposeKey Columns
**client**Customer masterclientid, clientname, clientdebt, clientarea, treeId
**product**Product catalogproductid, productName, productBuyPrice, lastbuyprice, meanbuyprice
**storedetail**Current inventorystoredetailid, productid, storeid, productquantity
**save**Cash registerssaveid, savename, savecurrentvalue, treeId
**insurancecompanies**Insurance providersid, name, discountpercent
**paymentnetworks**Payment networksid, name, discountpercent
---

๐Ÿ”‘ Key Functions

1. add() - Complete Bill Processing

Location: Line 247

Purpose: Main function for creating optical sales bills with full payment processing

Function Signature:

function add($offline) // $offline: 0=online, 1=offline
// Returns: [flag, billId, detailResult]

Process Flow:

1. Save bill details via saveBillDetails()

2. Save bill products via saveBillProducts()

3. Handle offline/online processing differences

4. Return processing results and bill ID

Features:

---

2. saveBillDetails() - Bill Master Record Creation

Location: Line 258

Purpose: Create main bill record with payment processing and client debt management

Function Signature:

function saveBillDetails($offline)
// Returns: Array with flag, note, billId

Complex Processing Logic:

1. Parameter Extraction: Extract and validate all POST parameters

2. Offline Bill Handling: Generate bill numbers, handle new clients

3. Online Bill Handling: Validate bill numbers, check duplicates

4. Medical Examination: Process internal/external kashf (eye exams)

5. Payment Processing: Handle cash, insurance, card payments

6. Client Debt: Update customer debt balances

7. Daily Entry: Create accounting journal entries

Payment Types Supported:

// Payment method processing
$paymentamethods = $_POST['paymentamethods'];
foreach ($paymentamethods as $method) {
    switch($method) {
        case 'insurance': saveInsurancePayment(); break;
        case 'card': saveCardPayment(); break;
        case 'cash': saveCashPayment(); break;
    }
}

---

3. saveInternalKashf() - Medical Examination Processing

Location: Line 434

Purpose: Handle internal eye examination (kashf) records

Function Signature:

function saveInternalKashf($clientId, $offline)
// Returns: Doctor kashf ID

Process Flow:

1. Check if offline kashf already exists

2. Create kashf master record if needed

3. Create doctor kashf detail record

4. Mark kashf as used to prevent duplicate discounts

5. Update kashf payment status

Medical Data Handling:

// Left and right eye measurements
$D_Lsphere = filter_input(INPUT_POST, 'D_Lsphere');  // Distance sphere
$D_Lcylinder = filter_input(INPUT_POST, 'D_Lcylinder'); // Distance cylinder
$D_Laxis = filter_input(INPUT_POST, 'D_Laxis');      // Distance axis
// ... similar for right eye and near vision

---

4. saveDailyEntry() - Financial Journal Integration

Location: Line 820

Purpose: Create comprehensive accounting entries for optical sales

Function Signature:

function saveDailyEntry($insurance, $card, $cash, $waitvalue, $billId, $offline)

Accounting Logic:

1. Credit Entries: Sales revenue to appropriate accounts

2. Debit Entries: Cash, receivables, insurance claims

3. Discount Handling: Insurance and payment network fees

4. Cost Center Integration: Project-based accounting

5. Multi-currency Support: Different payment methods

Account Structure:

// Main accounts used
$salesAccount = 16;        // Sales revenue
$cashAccount = $save->treeId;      // Cash register
$receivablesAccount = $client->treeId; // Customer receivables
$insuranceAccount = $insurance->treeId; // Insurance claims

---

5. saveBillProducts() - Product Processing & Inventory

Location: Line 1256

Purpose: Process bill line items with inventory management

Function Signature:

function saveBillProducts($billId, $offline, $affectNetworkNow = 0)
// Returns: Flag indicating inventory issues

Process Flow:

1. Product Validation: Check product IDs and quantities

2. Inventory Updates: Decrease stock quantities

3. Cost Tracking: Update last/mean buy prices

4. Store Reports: Log inventory movements

5. Service Handling: Skip inventory for service items

Inventory Logic:

if ($product->isService != 1) {
    $storeDetail = $storeDetailEX->getProductQuantity($productId, $storeId);
    $productbefore = $storeDetail->productquantity;
    $productafter = $productbefore - $productNum;
    $storeDetail->productquantity = $productafter;
    $storeDetailDAO->update($storeDetail);
    
    if ($storeDetail->productquantity < 0) {
        $flag = 1; // Negative inventory warning
    }
}

---

6. updateClientDebt() - Customer Account Management

Location: Line 104

Purpose: Update customer debt balances with proper tracking

Function Signature:

function updateClientDebt($clientid, $clientdebtAfter, $offline)

Process Flow:

1. Set appropriate user ID (online/offline)

2. Update client debt balance

3. Maintain audit trail

---

7. insertClientdebtchangeupdate() - Debt Change Tracking

Location: Line 125

Purpose: Log all customer debt changes for audit and reporting

Function Signature:

function insertClientdebtchangeupdate($clientid, $before, $amount, $type, 
    $processname, $modelid, $after, $tablename, $comment, $totalCost, $date, $offline)

Tracking Fields:

---

8. getBillDetails() - Bill Display Processing

Location: Line 1346

Purpose: Load and format bill data for display/printing

Function Signature:

function getBillDetails($billId)

Data Assembly:

1. Bill Master: Load main bill record

2. Client Data: Customer information

3. Insurance Data: Insurance company details if applicable

4. Payment Networks: Card payment details

5. Medical Data: Eye examination results

6. Products: Line items with categories

7. Branch Info: Store location details

---

๐Ÿ”„ Workflows

Workflow 1: Complete Optical Sales Bill Creation

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Create Optical Sales Bill
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Bill Initialization
- Parse POST parameters
- Validate client and date information
- Generate bill number (offline) or validate (online)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Medical Examination Processing
IF kashf required:
โ”‚
โ†’ External Kashf: Save location and date
โ”‚
โ†’ Internal Kashf:
โ”‚ โ”œโ”€ Create kashf master record
โ”‚ โ”œโ”€ Save eye examination details
โ”‚
โ”‚ โ”œโ”€ Record ADD values and notes
โ”‚ โ”‚ โ””โ”€ Mark kashf as used โ”‚
โ”‚
โ”‚ โ””โ”€โ†’ No Kashf: Skip medical processing โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Payment Method Processing
FOR EACH selected payment method:
โ”‚
โ†’ Cash Payment:
โ”‚ โ”‚ โ””โ”€ Record cash amount and register โ”‚
โ”‚
โ†’ Insurance Payment:
โ”‚ โ”œโ”€ Load insurance company details
โ”‚ โ”œโ”€ Calculate customer/company portions
โ”‚ โ”œโ”€ Apply insurance discount percentage
โ”‚ โ”‚ โ””โ”€ Upload insurance documents โ”‚
โ”‚
โ”‚ โ””โ”€โ†’ Card Payment: โ”‚
โ”œโ”€ Record payment network details
โ”œโ”€ Calculate network fees/discounts
โ”‚ โ””โ”€ Process card transaction data โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Product Processing
FOR EACH product on bill:
โ”‚
โ†’ Validate product and quantity
โ”‚
โ†’ Create billsproducts record
โ”‚
โ†’ Update inventory (if not service):
โ”‚ โ”œโ”€ Get current stock quantity
โ”‚ โ”œโ”€ Decrease by sold quantity
โ”‚ โ”œโ”€ Check for negative inventory
โ”‚ โ”‚ โ””โ”€ Log inventory movement โ”‚
โ”‚
โ”‚ โ””โ”€โ†’ Update product cost tracking โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Financial Processing
Customer Debt Management:
โ”œโ”€ Calculate debt change
โ”œโ”€ Update customer debt balance
โ”‚ โ””โ”€ Log debt change transaction โ”‚
Accounting Entries:
โ”œโ”€ Create daily entry header
โ”œโ”€ Credit sales account
โ”œโ”€ Debit payment accounts (cash/insurance/card)
โ”œโ”€ Handle discounts and fees
โ”‚ โ””โ”€ Link entry to bill record โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
6Finalization
- Commit database transaction
- Return bill ID and status
- Handle any processing errors
- Update bill with daily entry ID
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

Workflow 2: Medical Examination Integration

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Eye Examination Processing
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Examination Type Determination
kashftype values:
โ”‚
โ†’ -1: No examination required
โ†’ 0: Internal examination
โ”‚ โ””โ”€โ†’ 1: External examination โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Internal Examination Processing
Create doctorkashf record:
โ”‚
โ†’ Left Eye Distance (D):
โ”‚ โ”œโ”€ Sphere, Cylinder, Axis
โ”‚ โ”‚ โ””โ”€ Prism, Visual Acuity โ”‚
โ”‚
โ†’ Left Eye Near (N):
โ”‚ โ”œโ”€ Sphere, Cylinder, Axis
โ”‚ โ”‚ โ””โ”€ Prism, Visual Acuity โ”‚
โ”‚
โ†’ Right Eye Distance (D):
โ”‚ โ”œโ”€ Sphere, Cylinder, Axis
โ”‚ โ”‚ โ””โ”€ Prism, Visual Acuity โ”‚
โ”‚
โ†’ Right Eye Near (N):
โ”‚ โ”œโ”€ Sphere, Cylinder, Axis
โ”‚ โ”‚ โ””โ”€ Prism, Visual Acuity โ”‚
โ”‚
โ”‚ โ””โ”€โ†’ Additional Data: โ”‚
โ”œโ”€ ADD values (right/left)
โ”œโ”€ Bifocal measurements
โ”œโ”€ PD (Pupillary Distance)
โ”œโ”€ BVD (Back Vertex Distance)
โ”‚ โ””โ”€ Clinical notes โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3External Examination Handling
- Record examination location
- Store examination date
- Link to existing examination records
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Examination Integration
- Link examination to bill record
- Update examination payment status
- Apply any examination-based discounts
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐Ÿงฎ Complex Calculation Methods

Insurance Payment Calculation

function saveInsurancePayment($bills, $offline) {
    $bills->customerpercent = $_POST['clientPercent'];
    $bills->customermax = $_POST['clientMax'];
    $bills->customercarry = $_POST['clientCarry'];
    $bills->companyaccept = $_POST['insAccept'];
    $bills->companycarry = $_POST['insuranceValue'];
    
    // Apply insurance company discount
    $bills->insdiscountpercent = $insuranceCompanies->discountpercent;
    
    return $bills;
}

Payment Network Fee Calculation

function saveCardPayment($bills, $offline) {
    $bills->paymentnetworkid = $_POST['paymentNetworks'];
    $bills->cardvalue = $_POST['cardValue'];
    $bills->netdiscountpercent = $paymentNetworks->discountpercent;
    
    // Calculate network fee
    $cardValue = $_POST['cardValue'];
    $cardDiscount = $cardValue * ($bills->netdiscountpercent) / 100;
    $netAmount = $cardValue - $cardDiscount;
    
    return $bills;
}

Product Cost Tracking

function lastAndMeanBuyPrice_SellOptic($detailId, $productId) {
    $buyProduct = $productDAO->load($productId);
    
    // Use existing costs or default to buy price
    if ($buyProduct->meanbuyprice == NULL || $buyProduct->meanbuyprice == 0) {
        $buyProduct->meanbuyprice = $buyProduct->productBuyPrice;
    }
    if ($buyProduct->lastbuyprice == NULL || $buyProduct->lastbuyprice == 0) {
        $buyProduct->lastbuyprice = $buyProduct->productBuyPrice;
    }
    
    // Update sell record with cost information
    $billsProductsEX->updatePrices_f_optic($buyProduct->lastbuyprice, 
                                          $buyProduct->meanbuyprice, 
                                          $detailId);
}

---

๐Ÿ”’ Security & Permissions

Transaction Management

$mytransactions = new Transaction();
try {
    // All database operations
    $mytransactions->commit();
} catch (Exception $ex) {
    $mytransactions->rollback();
}

Input Sanitization

$clientId = filter_input(INPUT_POST, "client");
$billDate = filter_input(INPUT_POST, "billdate");
$productstotalprice = filter_input(INPUT_POST, "totalBillPrice");

Session Management

---

๐Ÿ“Š Performance Considerations

Database Optimization

1. Critical Indexes:

- bills(clientid, billdate, deleted)

- billsproducts(billid, productid)

- storedetail(productid, storeid)

- clientdebtchange(clientid, clientdebtchangedate)

2. Transaction Efficiency:

- Batch product processing

- Minimize database calls in loops

- Use prepared statements via DAO layer

3. Inventory Management:

- Real-time stock updates

- Negative inventory warnings

- Store movement logging

---

๐Ÿš€ Future Enhancement Opportunities

1. Advanced Medical Integration

2. Enhanced Payment Processing

3. Inventory Optimization

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When billing system enhancements are implemented