Skip to content

Commit

Permalink
Merge pull request #364 from liberu-accounting/sweep/Implement-Invent…
Browse files Browse the repository at this point in the history
…ory-Management-System-for-Accounting-Application

Implement Inventory Management System for Accounting Application
  • Loading branch information
curtisdelicata authored Dec 24, 2024
2 parents 3e617d3 + a02f98a commit 9dfbcd2
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 3 deletions.
65 changes: 65 additions & 0 deletions app/Models/InventoryItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class InventoryItem extends Model
{
use HasFactory;

protected $primaryKey = 'inventory_item_id';

protected $fillable = [
'name',
'sku',
'description',
'unit_price',
'current_quantity',
'reorder_point',
'account_id',
'category_id',
'is_active'
];

protected $casts = [
'unit_price' => 'decimal:2',
'current_quantity' => 'integer',
'reorder_point' => 'integer',
'is_active' => 'boolean'
];

public function account()
{
return $this->belongsTo(Account::class);
}

public function category()
{
return $this->belongsTo(Category::class);
}

public function transactions()
{
return $this->hasMany(InventoryTransaction::class);
}

public function adjustments()
{
return $this->hasMany(InventoryAdjustment::class);
}

public function updateQuantity($change)
{
$this->current_quantity += $change;
$this->save();
}

public function needsReorder()
{
return $this->current_quantity <= $this->reorder_point;
}
}
39 changes: 39 additions & 0 deletions app/Models/InventoryTransaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class InventoryTransaction extends Model
{
use HasFactory;

protected $primaryKey = 'inventory_transaction_id';

protected $fillable = [
'inventory_item_id',
'transaction_id',
'quantity',
'unit_price',
'transaction_type',
'notes'
];

protected $casts = [
'quantity' => 'integer',
'unit_price' => 'decimal:2'
];

public function inventoryItem()
{
return $this->belongsTo(InventoryItem::class);
}

public function transaction()
{
return $this->belongsTo(Transaction::class);
}
}
17 changes: 14 additions & 3 deletions app/Models/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Transaction extends Model
'reconciled',
'discrepancy_notes',
'exchange_rate',
'purchase_order_id',
'transaction_type',
];

protected $casts = [
Expand Down Expand Up @@ -49,9 +49,9 @@ public function categories()
return $this->belongsToMany(Category::class);
}

public function purchaseOrder()
public function inventoryTransactions()
{
return $this->belongsTo(PurchaseOrder::class, 'purchase_order_id');
return $this->hasMany(InventoryTransaction::class);
}

public function getAmountInDefaultCurrency()
Expand All @@ -62,4 +62,15 @@ public function getAmountInDefaultCurrency()
}
return $this->amount * $this->exchange_rate;
}

public function updateInventory()
{
foreach ($this->inventoryTransactions as $inventoryTx) {
$quantity = $inventoryTx->quantity;
if ($this->transaction_type === 'sale') {
$quantity *= -1;
}
$inventoryTx->inventoryItem->updateQuantity($quantity);
}
}
}
46 changes: 46 additions & 0 deletions app/Services/InventoryService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@


<?php

namespace App\Services;

use App\Models\InventoryItem;
use App\Models\Transaction;
use App\Models\InventoryTransaction;

class InventoryService
{
public function createInventoryTransaction(
Transaction $transaction,
InventoryItem $item,
int $quantity,
float $unitPrice,
string $type
) {
$inventoryTx = InventoryTransaction::create([
'inventory_item_id' => $item->id,
'transaction_id' => $transaction->id,
'quantity' => $quantity,
'unit_price' => $unitPrice,
'transaction_type' => $type
]);

$change = $type === 'sale' ? -$quantity : $quantity;
$item->updateQuantity($change);

return $inventoryTx;
}

public function checkLowStock()
{
return InventoryItem::where('is_active', true)
->where('current_quantity', '<=', 'reorder_point')
->get();
}

public function getInventoryValue()
{
return InventoryItem::where('is_active', true)
->sum(\DB::raw('unit_price * current_quantity'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@


<?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_items', function (Blueprint $table) {
$table->id('inventory_item_id');
$table->string('name');
$table->string('sku')->unique();
$table->text('description')->nullable();
$table->decimal('unit_price', 15, 2);
$table->integer('current_quantity')->default(0);
$table->integer('reorder_point')->default(0);
$table->foreignId('account_id')->constrained('accounts');
$table->foreignId('category_id')->nullable()->constrained('categories');
$table->boolean('is_active')->default(true);
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('inventory_items');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@


<?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_transactions', function (Blueprint $table) {
$table->id('inventory_transaction_id');
$table->foreignId('inventory_item_id')->constrained('inventory_items');
$table->foreignId('transaction_id')->constrained('transactions');
$table->integer('quantity');
$table->decimal('unit_price', 15, 2);
$table->enum('transaction_type', ['purchase', 'sale', 'adjustment']);
$table->text('notes')->nullable();
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('inventory_transactions');
}
};

0 comments on commit 9dfbcd2

Please sign in to comment.