-
-
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 #364 from liberu-accounting/sweep/Implement-Invent…
…ory-Management-System-for-Accounting-Application Implement Inventory Management System for Accounting Application
- Loading branch information
Showing
6 changed files
with
225 additions
and
3 deletions.
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
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; | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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')); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
database/migrations/2024_02_14_000001_create_inventory_items_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,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'); | ||
} | ||
}; |
29 changes: 29 additions & 0 deletions
29
database/migrations/2024_02_14_000002_create_inventory_transactions_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,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'); | ||
} | ||
}; |