Compare commits
2 Commits
b54fabd416
...
59d186e3b5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
59d186e3b5 | ||
|
|
a7a55a92a1 |
@@ -11,6 +11,7 @@
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Modules\Webstatement\Models\Account;
|
||||
use Modules\Webstatement\Models\AccountBalance;
|
||||
|
||||
class ProcessAccountDataJob implements ShouldQueue
|
||||
{
|
||||
@@ -92,6 +93,27 @@
|
||||
$account = Account::firstOrNew(['account_number' => $data['account_number']]);
|
||||
$account->fill($data);
|
||||
$account->save();
|
||||
|
||||
// Store the opening balances in the AccountBalance model for this period
|
||||
if (isset($data['open_actual_bal']) || isset($data['open_cleared_bal'])) {
|
||||
$accountBalance = AccountBalance::firstOrNew([
|
||||
'account_number' => $data['account_number'],
|
||||
'period' => $period
|
||||
]);
|
||||
|
||||
// Set the balances
|
||||
if (isset($data['open_actual_bal'])) {
|
||||
$accountBalance->actual_balance = $data['open_actual_bal'];
|
||||
}
|
||||
|
||||
if (isset($data['open_cleared_bal'])) {
|
||||
$accountBalance->cleared_balance = $data['open_cleared_bal'];
|
||||
}
|
||||
|
||||
$accountBalance->save();
|
||||
Log::info("Saved balance for account {$data['account_number']} for period $period");
|
||||
}
|
||||
|
||||
$processedCount++;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
|
||||
@@ -34,4 +34,23 @@ class Account extends Model
|
||||
{
|
||||
return $this->belongsTo(Customer::class, 'customer_code', 'customer_code');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all balances for this account.
|
||||
*/
|
||||
public function balances()
|
||||
{
|
||||
return $this->hasMany(AccountBalance::class, 'account_number', 'account_number');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get balance for a specific period.
|
||||
*
|
||||
* @param string $period Format: YYYY-MM
|
||||
* @return AccountBalance|null
|
||||
*/
|
||||
public function getBalanceForPeriod($period)
|
||||
{
|
||||
return $this->balances()->where('period', $period)->first();
|
||||
}
|
||||
}
|
||||
|
||||
59
app/Models/AccountBalance.php
Normal file
59
app/Models/AccountBalance.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Webstatement\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
|
||||
class AccountBalance extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected $fillable = [
|
||||
'account_number',
|
||||
'period',
|
||||
'actual_balance',
|
||||
'cleared_balance',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the account that owns the balance.
|
||||
*/
|
||||
public function account()
|
||||
{
|
||||
return $this->belongsTo(Account::class, 'account_number', 'account_number');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope a query to filter by account number.
|
||||
*/
|
||||
public function scopeForAccount($query, $accountNumber)
|
||||
{
|
||||
return $query->where('account_number', $accountNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope a query to filter by period.
|
||||
*/
|
||||
public function scopeForPeriod($query, $period)
|
||||
{
|
||||
return $query->where('period', $period);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get balance for a specific account and period.
|
||||
*
|
||||
* @param string $accountNumber
|
||||
* @param string $period Format: YYYY-MM
|
||||
* @return AccountBalance|null
|
||||
*/
|
||||
public static function getBalance($accountNumber, $period)
|
||||
{
|
||||
return self::where('account_number', $accountNumber)
|
||||
->where('period', $period)
|
||||
->first();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('account_balances', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('account_number');
|
||||
$table->string('period'); // Format: YYYY-MM
|
||||
$table->decimal('actual_balance', 20, 2)->default(0);
|
||||
$table->decimal('cleared_balance', 20, 2)->default(0);
|
||||
$table->timestamps();
|
||||
|
||||
// Create a unique constraint to ensure one record per account per period
|
||||
$table->unique(['account_number', 'period']);
|
||||
|
||||
// Add indexes for faster queries
|
||||
$table->index('account_number');
|
||||
$table->index('period');
|
||||
$table->index('created_at');
|
||||
|
||||
// Add foreign key if needed
|
||||
// $table->foreign('account_number')->references('account_number')->on('accounts');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('account_balances');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user