Billsfunctionsbuy Documentation

Bills Functions Buy Controller Documentation

File: /controllers/billsfunctionsbuy.php

Purpose: Purchase bill processing utilities for optical inventory procurement

Last Updated: December 20, 2024

Total Functions: 20+

Lines of Code: ~1,118

---

๐Ÿ“‹ Overview

The Bills Functions Buy Controller provides comprehensive purchase bill processing for optical inventory procurement. It handles:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Purchase Tables

Table NamePurposeKey Columns
**billsbuy**Purchase bill masterbillid, clientid, billno, finalnetbillvalue, waitvalue, dailyentryid
**billsproductsbuy**Purchase line itemsid, billid, productid, productno, productprice, producttotalprice, storeid
**supplier**Supplier master datasupplierid, suppliername, suppliercurrentDebt, treeId
**supplierdebtchange**Supplier debt trackingid, supplierid, supplierdebtchangeamount, supplierdebtchangetype, tablename
### Financial & Banking Tables

Table NamePurposeKey Columns
**bank**Bank master databankid, bankname
**bankaccount**Bank account detailsaccountid, bankid, accountname, treeId
**dailyentry**Accounting journal entriesdailyentryid, dDateTime, entryComment
**dailyentrycreditor**Credit entriesid, dailyentryid, accountstreeid, value
**dailyentrydebtor**Debit entriesid, dailyentryid, accountstreeid, value
### Inventory & Product Tables

Table NamePurposeKey Columns
**product**Product masterproductid, productName, productBuyPrice, lastbuyprice, meanbuyprice
**storedetail**Current inventorystoredetailid, productid, storeid, productquantity
**storereport**Inventory movement logid, productid, storeid, productbefore, productafter, tablename
---

๐Ÿ”‘ Key Functions

1. addBuy() - Complete Purchase Bill Processing

Location: Line 149

Purpose: Main function for creating purchase bills with full payment processing

Function Signature:

function addBuy()
// Returns: [flag, billId, detailResult]

Process Flow:

1. Save bill details via saveBillDetailsBuy()

2. Save bill products via saveBillProductsBuy()

3. Handle payment processing and accounting

4. Return processing results

Features:

---

2. saveBillDetailsBuy() - Purchase Bill Creation

Location: Line 160

Purpose: Create purchase bill master record with payment integration

Function Signature:

function saveBillDetailsBuy()
// Returns: Array with flag, note, billId

Key Processing Steps:

1. Bill Validation: Check bill number uniqueness

2. Supplier Management: Load supplier and debt information

3. Payment Processing: Handle multiple payment methods

4. Debt Tracking: Update supplier debt balances

5. Accounting: Create financial journal entries

Supplier Debt Logic:

$client = $supplierDAO->load($clientId);
$clientdebtBefore = $client->suppliercurrentDebt;
$clientdebtAfter = $clientdebtBefore + $waitvalue; // Increase supplier debt

---

3. savePaymentDetailsBuy() - Purchase Payment Processing

Location: Line 268

Purpose: Process multiple payment methods for purchase bills

Function Signature:

function savePaymentDetailsBuy($billsBuy, $waitvalue, $affectNetworkNow = 0)

Payment Methods Supported:

1. Cash Payment: Direct cash payment to supplier

2. Bank Payment: Bank transfer or check payment

3. Insurance Payment: Insurance company payment processing

Payment Processing Logic:

$paymentamethods = $_POST['paymentamethods'];
foreach ($paymentamethods as $method) {
    switch($method) {
        case 'cash': $billsBuy = saveCashPaymentBuy($billsBuy); break;
        case 'card': $billsBuy = saveCardPaymentBuy($billsBuy); break; // Bank
        case 'insurance': $billsBuy = saveInsurancePaymentBuy($billsBuy); break;
    }
}

---

4. saveDailyEntryBuy() - Purchase Accounting Integration

Location: Line 356

Purpose: Create comprehensive accounting entries for purchases

Function Signature:

function saveDailyEntryBuy($insurance, $card, $cash, $waitvalue, $billId, $discountAsMoney)

Accounting Structure:

1. Debit: Inventory/Purchases account (receives goods)

2. Credits:

- Cash account (if cash payment)

- Bank account (if bank payment)

- Supplier payable (if credit terms)

- Discount account (if discount given)

Account Mapping:

// Main accounts for purchases
$purchasesAccount = 12;        // Inventory/Purchases
$cashAccount = $save->treeId;  // Cash register
$supplierAccount = $supplier->treeId; // Supplier payable
$discountAccount = 52;         // Purchase discounts

---

5. saveBillProductsBuy() - Purchase Product Processing

Location: Line 575

Purpose: Process purchase line items with inventory and costing updates

Function Signature:

function saveBillProductsBuy($billId, $affectNetworkNow = 0)

Product Processing Steps:

1. Line Item Creation: Save purchase product records

2. Inventory Updates: Increase stock quantities

3. Cost Calculations: Update product cost information

4. Movement Logging: Record inventory transactions

Inventory Increase Logic:

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

---

6. lastAndMeanBuyPrice_SellOpticBuy() - Advanced Cost Tracking

Location: Line 1069

Purpose: Calculate and update product cost averages using weighted average method

Function Signature:

function lastAndMeanBuyPrice_SellOpticBuy($productquantityBefore, $productChangeAmount, 
                                         $productquantityAfter, $lastBuyPriceOnePiece, 
                                         $detailId, $productId)

Weighted Average Calculation:

// Calculate new weighted average cost
$Bast = ($productquantityBefore * $buyProduct->meanbuyprice) + 
        ($productChangeAmount * $lastBuyPriceOnePiece);
$makam = $productquantityAfter;
$meanBuyPrice = round(($Bast / $makam), 3);

// Update product costs
$buyProduct->meanbuyprice = $meanBuyPrice;    // Weighted average
$buyProduct->lastbuyprice = $lastBuyPriceOnePiece; // Most recent
$productDAO->update($buyProduct);

---

7. updateSupplierDebt() - Supplier Account Management

Location: Line 103

Purpose: Update supplier debt balances with proper tracking

Function Signature:

function updateSupplierDebt($supplierId, $supplierdebtAfter)

---

8. insertSupplierDebtChangeupdate() - Supplier Debt Tracking

Location: Line 113

Purpose: Log all supplier debt changes for audit and reporting

Function Signature:

function insertSupplierDebtChangeupdate($supplierId, $before, $amount, $type, 
                                       $processname, $modelid, $after, $tablename, 
                                       $comment, $date)

---

9. getBillDetailsBuy() - Purchase Bill Display

Location: Line 654

Purpose: Load and format purchase bill data for display/printing

Function Signature:

function getBillDetailsBuy($billId)

Data Assembly:

1. Bill Master: Purchase bill details

2. Supplier Data: Supplier information

3. Bank Details: Payment account information

4. Products: Line items with specifications

5. Branch Info: Store location details

---

๐Ÿ”„ Workflows

Workflow 1: Complete Purchase Bill Processing

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Create Purchase Bill
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Bill Initialization
- Validate supplier information
- Generate/validate bill number
- Parse product and payment data
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Payment Method Processing
FOR EACH selected payment method:
โ”‚
โ†’ Cash Payment:
โ”‚ โ”‚ โ””โ”€ Record cash amount paid โ”‚
โ”‚
โ†’ Bank Payment:
โ”‚ โ”œโ”€ Select bank and account
โ”‚ โ”œโ”€ Record transfer amount
โ”‚ โ”‚ โ””โ”€ Link to bank account record โ”‚
โ”‚
โ”‚ โ””โ”€โ†’ Insurance Payment: โ”‚
โ”œโ”€ Process insurance claim
โ”œโ”€ Calculate coverage amounts
โ”‚ โ””โ”€ Upload supporting documents โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Product Receiving
FOR EACH product purchased:
โ”‚
โ†’ Create purchase line item
โ”‚
โ†’ Update inventory (if physical product):
โ”‚ โ”œโ”€ Get current stock quantity
โ”‚ โ”œโ”€ Add received quantity
โ”‚ โ”‚ โ””โ”€ Log inventory movement โ”‚
โ”‚
โ”‚ โ””โ”€โ†’ Update product costing: โ”‚
โ”œโ”€ Calculate new weighted average cost
โ”œโ”€ Update last purchase price
โ”‚ โ””โ”€ Save cost history โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Supplier Account Management
- Calculate supplier debt change
- Update supplier balance
- Log debt change transaction
- Handle credit terms if applicable
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Financial Accounting
Create journal entries:
โ”‚
โ†’ Debit: Inventory/Purchases Account
โ”‚ (Total bill amount)
โ”‚
โ†’ Credit: Cash Account
โ”‚ (If cash payment made)
โ”‚
โ†’ Credit: Bank Account
โ”‚ (If bank payment made)
โ”‚
โ†’ Credit: Supplier Payable
โ”‚ (If credit terms)
โ”‚
โ”‚ โ””โ”€โ†’ Credit: Purchase Discount โ”‚
(If discount received)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
6Finalization
- Commit all database transactions
- Generate purchase order confirmation
- Update supplier payment status
- Return processing results
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

Workflow 2: Product Cost Calculation (Weighted Average)

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Update Product Costs
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Load Current Product Data
- Get current inventory quantity
- Get current weighted average cost
- Get last purchase price
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Calculate Weighted Average Cost
Formula:
โ”‚
โ†’ Current Value = Qty_Before ร— Current_Avg_Cost
โ”‚
โ†’ Purchase Value = Qty_Purchased ร— Purchase_Price
โ”‚
โ†’ Total Value = Current_Value + Purchase_Value
โ”‚
โ†’ Total Quantity = Qty_Before + Qty_Purchased
โ”‚
โ”‚ โ””โ”€โ†’ New_Avg_Cost = Total_Value รท Total_Quantity โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Update Product Cost Records
- Save new weighted average cost
- Save last purchase price
- Update purchase line item costs
- Log cost change transaction
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐Ÿงฎ Purchase-Specific Calculations

Supplier Debt Management

// Purchase increases supplier debt (amount we owe)
$supplierDebtBefore = $supplier->suppliercurrentDebt;
$supplierDebtAfter = $supplierDebtBefore + $waitvalue;

// Log debt change
insertSupplierDebtChangeupdate($supplierId, $supplierDebtBefore, $waitvalue, 
                              0, "Purchase Bill", $billId, $supplierDebtAfter, 
                              "billsbuy.php", "Purchase transaction", $billDate);

Discount Processing

function calculatePurchaseDiscount($billsBuy) {
    $discountAsMoney = 0;
    if ($billsBuy->discounttype == 1) {
        // Fixed amount discount
        $discountAsMoney = $billsBuy->discountvalue;
    } else {
        // Percentage discount
        $discountAsMoney = $billsBuy->netbillvalue * $billsBuy->discountvalue / 100;
    }
    return $discountAsMoney;
}

Inventory Costing (FIFO/Weighted Average)

function updateProductCosts($productId, $purchaseQty, $purchasePrice) {
    $product = $productDAO->load($productId);
    $currentQty = $storeDetail->productquantity;
    $currentAvgCost = $product->meanbuyprice;
    
    // Weighted average calculation
    $totalValue = ($currentQty * $currentAvgCost) + ($purchaseQty * $purchasePrice);
    $totalQty = $currentQty + $purchaseQty;
    $newAvgCost = round($totalValue / $totalQty, 3);
    
    // Update product costs
    $product->meanbuyprice = $newAvgCost;
    $product->lastbuyprice = $purchasePrice;
    $productDAO->update($product);
}

---

๐ŸŒ Bank Account Integration

Bank Payment Processing

function saveCardPaymentBuy($billsBuy) {
    // In purchase context, "card" represents bank payments
    $billsBuy->paymentnetworkid = $_POST['paymentNetworks']; // Bank ID
    $billsBuy->cardvalue = $_POST['cardValue'];              // Amount
    $billsBuy->netdiscountpercent = $_POST['accountid'];     // Account ID
    return $billsBuy;
}

Bank Account Linking

// Link payment to specific bank account
$accountid = filter_input(INPUT_POST, "accountid");
$accountData = $accountDAO->load($accountid);

if ($accountData->treeId > 0) {
    $treeId = $accountData->treeId;
} else {
    // Create chart of accounts entry for bank account
    $bankData = $bankDAO->load($billsBuy->paymentnetworkid);
    $treeId = addTreeElement("$accountData->accountname / $bankData->bankname", 
                            38, 3, 0, 1, '', 0, 0);
}

---

๐Ÿ”’ Security & Permissions

Transaction Integrity

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

Input Validation

$clientId = filter_input(INPUT_POST, "client");
$billNo = filter_input(INPUT_POST, "billno");
$netbillvalue = filter_input(INPUT_POST, "netBillValue");

User Context

---

๐Ÿ“Š Performance Considerations

Database Optimization

1. Critical Indexes:

- billsbuy(clientid, billdate, deleted)

- billsproductsbuy(billid, productid)

- supplier(supplierid)

- supplierdebtchange(supplierid, supplierdebtchangedate)

2. Inventory Performance:

- Efficient stock quantity updates

- Batch product cost calculations

- Optimized movement logging

3. Financial Calculations:

- Minimize decimal precision issues

- Efficient weighted average computations

- Fast account tree lookups

---

๐Ÿ› Common Issues & Troubleshooting

1. Cost Calculation Errors

Issue: Incorrect weighted average costs

Cause: Division by zero or precision issues

Debug:

// Check for valid quantities before calculation
if ($totalQty > 0) {
    $newAvgCost = round($totalValue / $totalQty, 3);
} else {
    // Handle zero quantity case
    $newAvgCost = $purchasePrice;
}

2. Supplier Debt Discrepancies

Issue: Supplier balances don't match purchase totals

Cause: Missing debt change records

Debug:

-- Verify supplier debt changes
SELECT 
    SUM(CASE WHEN supplierdebtchangetype = 0 THEN supplierdebtchangeamount ELSE 0 END) as purchases,
    SUM(CASE WHEN supplierdebtchangetype = 1 THEN supplierdebtchangeamount ELSE 0 END) as payments
FROM supplierdebtchange 
WHERE supplierid = [SUPPLIER_ID];

3. Bank Account Linking Issues

Issue: Payments not properly linked to bank accounts

Cause: Missing account setup or tree ID issues

Solution:

---

๐Ÿš€ Future Enhancement Opportunities

1. Advanced Procurement

2. Enhanced Costing

3. Bank Integration

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When procurement features are enhanced