-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #375 from liberu-accounting/sweep/Implement-Advanc…
…ed-Inventory-Valuation-Methods-and-Cost-Tracking Implement Advanced Inventory Valuation Methods and Cost Tracking
- Loading branch information
Showing
4 changed files
with
175 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
|
||
|
||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use App\Models\InventoryItem; | ||
use App\Models\InventoryTransaction; | ||
use App\Models\InventoryCostLayer; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class InventoryValuationService | ||
{ | ||
public function calculateCostOfGoodsSold(InventoryTransaction $transaction) | ||
{ | ||
$item = $transaction->inventoryItem; | ||
|
||
switch ($item->valuation_method) { | ||
case 'fifo': | ||
return $this->calculateFIFOCost($transaction); | ||
case 'lifo': | ||
return $this->calculateLIFOCost($transaction); | ||
case 'average': | ||
return $this->calculateAverageCost($transaction); | ||
} | ||
} | ||
|
||
private function calculateFIFOCost(InventoryTransaction $transaction) | ||
{ | ||
$remainingQty = $transaction->quantity; | ||
$totalCost = 0; | ||
|
||
$costLayers = InventoryCostLayer::where('inventory_item_id', $transaction->inventory_item_id) | ||
->where('quantity', '>', 0) | ||
->orderBy('purchase_date') | ||
->get(); | ||
|
||
foreach ($costLayers as $layer) { | ||
$qtyFromLayer = min($remainingQty, $layer->quantity); | ||
$totalCost += $qtyFromLayer * $layer->unit_cost; | ||
$layer->quantity -= $qtyFromLayer; | ||
$layer->save(); | ||
|
||
$remainingQty -= $qtyFromLayer; | ||
if ($remainingQty <= 0) break; | ||
} | ||
|
||
return $totalCost; | ||
} | ||
|
||
private function calculateLIFOCost(InventoryTransaction $transaction) | ||
{ | ||
$remainingQty = $transaction->quantity; | ||
$totalCost = 0; | ||
|
||
$costLayers = InventoryCostLayer::where('inventory_item_id', $transaction->inventory_item_id) | ||
->where('quantity', '>', 0) | ||
->orderBy('purchase_date', 'desc') | ||
->get(); | ||
|
||
foreach ($costLayers as $layer) { | ||
$qtyFromLayer = min($remainingQty, $layer->quantity); | ||
$totalCost += $qtyFromLayer * $layer->unit_cost; | ||
$layer->quantity -= $qtyFromLayer; | ||
$layer->save(); | ||
|
||
$remainingQty -= $qtyFromLayer; | ||
if ($remainingQty <= 0) break; | ||
} | ||
|
||
return $totalCost; | ||
} | ||
|
||
private function calculateAverageCost(InventoryTransaction $transaction) | ||
{ | ||
$item = $transaction->inventoryItem; | ||
return $transaction->quantity * $item->average_cost; | ||
} | ||
|
||
public function updateAverageCost(InventoryItem $item, $purchaseQty, $purchaseCost) | ||
{ | ||
$totalValue = ($item->current_quantity * $item->average_cost) + | ||
($purchaseQty * $purchaseCost); | ||
$totalQty = $item->current_quantity + $purchaseQty; | ||
|
||
$item->average_cost = $totalValue / $totalQty; | ||
$item->save(); | ||
} | ||
|
||
public function getInventoryValuation(InventoryItem $item) | ||
{ | ||
switch ($item->valuation_method) { | ||
case 'fifo': | ||
case 'lifo': | ||
return InventoryCostLayer::where('inventory_item_id', $item->id) | ||
->where('quantity', '>', 0) | ||
->sum(DB::raw('quantity * unit_cost')); | ||
case 'average': | ||
return $item->current_quantity * $item->average_cost; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
database/migrations/2024_02_14_000003_create_inventory_cost_layers_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
|
||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up() | ||
{ | ||
Schema::create('inventory_cost_layers', function (Blueprint $table) { | ||
$table->id(); | ||
$table->foreignId('inventory_item_id')->constrained('inventory_items'); | ||
$table->integer('quantity'); | ||
$table->decimal('unit_cost', 15, 2); | ||
$table->date('purchase_date'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::dropIfExists('inventory_cost_layers'); | ||
} | ||
}; |