Coding a PHP Inventory Tracker for Egg and Poultry Sales
Author: Marcial Rey (In Between Bamboos Farm)
Raising poultry is only half the battle; managing the business logistics is the other. Tracking how many trays of eggs you collect daily versus how many you sell is crucial for calculating profit margins. Instead of paying a monthly fee for commercial inventory software, I built a lightweight PHP tracker tailored specifically to the homestead.
The Math Behind the Margins
The logic is straightforward: Input the daily feed costs, input the daily egg trays collected, and log the daily sales in Philippine Pesos (PHP). The database does the rest.
The Code: The Daily Sales Processor
Here is the PHP snippet that processes the end-of-day ledger. It calculates the net profit dynamically before saving it to the database.
<?php
// process-daily-ledger.php
// 1. Hardcoded variables (Usually pulled from a secure web form)
$trays_collected = 15;
$trays_sold = 12;
$price_per_tray = 210.00; // In PHP (Pesos)
$daily_feed_cost = 850.00;
// 2. The Business Logic
$gross_revenue = $trays_sold * $price_per_tray;
$net_profit = $gross_revenue - $daily_feed_cost;
$unsold_inventory = $trays_collected - $trays_sold;
$ledger_date = date("Y-m-d");
// 3. Connect to the Database
$conn = new mysqli('localhost', 'root', '', 'agritech_db');
// 4. Save the financial record
$sql = "INSERT INTO poultry_ledger (log_date, trays_collected, revenue, profit, left_over)
VALUES ('$ledger_date', '$trays_collected', '$gross_revenue', '$net_profit', '$unsold_inventory')";
if ($conn->query($sql) === TRUE) {
echo "Success: End of day ledger logged. Net Profit: ₱" . number_format($net_profit, 2);
}
$conn->close();
?>