Inventory cost
unknown
php
14 days ago
3.7 kB
3
Indexable
Never
private function getLifetimeStats($livestock) { $purchasePrice = $livestock->purchase_price ?? 0; $estimatedSellingPrice = $livestock->est_value ?? 0; $totalInventoryCost = $this->calculateInventoryCostForLivestock($livestock); $totalFarmExpenses = $livestock->farm->expenses->sum('amount'); $activeLivestockCount = Livestock::where('farm_id', $livestock->farm_id) ->whereIn('status', ['ACTIVE', 'LACTATING', 'FOR_SALE', 'QUARANTINE', 'SICK']) ->count(); $monthsSinceAdded = $livestock->created_at->diffInMonths(now()) ?: 1; $averageCostPerMonth = ($totalInventoryCost + $totalFarmExpenses) / $activeLivestockCount / $monthsSinceAdded; $totalCost = $purchasePrice + ($averageCostPerMonth * $monthsSinceAdded); $marginAvailable = $estimatedSellingPrice - $totalCost; $targetToSell = $averageCostPerMonth != 0 ? $marginAvailable / $averageCostPerMonth : 0; $roi = $totalCost != 0 ? (($estimatedSellingPrice - $totalCost) / $totalCost) * 100 : 0; $dailyCost = $averageCostPerMonth / 30; // Assuming 30 days per month $daysToBreakEven = $dailyCost != 0 ? ($totalCost / $dailyCost) : 0; return [ 'purchase_price' => [ 'value' => round($purchasePrice, 2), 'description' => 'The initial cost of acquiring the livestock.', ], 'average_cost_per_month' => [ 'value' => round($averageCostPerMonth, 2), 'description' => 'The average monthly cost of maintaining the livestock, including feed, healthcare, and a portion of farm expenses.', ], 'total_cost' => [ 'value' => round($totalCost, 2), 'description' => 'The total amount spent on the livestock, including purchase price and accumulated monthly costs.', ], 'estimated_selling_price' => [ 'value' => round($estimatedSellingPrice, 2), 'description' => 'The projected market value of the livestock if sold today.', ], 'margin_available' => [ 'value' => round($marginAvailable, 2), 'description' => 'The potential profit if the livestock is sold at the estimated selling price.', ], 'target_to_sell' => [ 'value' => round($targetToSell, 2), 'description' => 'The number of months it would take for the margin to equal the total cost, indicating an optimal selling time.', ], 'roi' => [ 'value' => round($roi, 2), 'description' => 'Return on Investment: The percentage of profit or loss relative to the total cost, indicating the efficiency of the investment.', ], 'days_to_break_even' => [ 'value' => round($daysToBreakEven, 0), 'description' => 'The estimated number of days it will take for the livestock\'s value to equal its total cost.', ], ]; } private function calculateInventoryCostForLivestock($livestock) { $farmId = $livestock->farm_id; $startDate = $livestock->created_at; return ItemEntry::where(function ($query) use ($farmId) { $query->whereHas('item', function ($subQuery) use ($farmId) { $subQuery->where('farm_id', $farmId); }); }) ->where('type', 'OUT') ->where('posting_date', '>=', $startDate) ->sum(DB::raw('amount * balance')); } }
Leave a Comment