Skip to content

Commit

Permalink
Implement BF amaount
Browse files Browse the repository at this point in the history
  • Loading branch information
OniiCoder committed Mar 8, 2024
1 parent 6114692 commit ccdf709
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function up()
$table->string('contact_person_name')->nullable();
$table->string('contact_person_phone')->nullable();
$table->dateTime('contact_person_dob')->nullable();
$table->double('bf_amount', 16, 4)->default(0);
$table->double('bf_balance', 16, 4)->default(0);
$table->dateTime('bf_date')->nullable(now());
$table->json('meta')->nullable();
$table->dateTime('created_at')->nullable();
$table->dateTime('updated_at')->nullable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public function up()
Schema::create('pamtechoga_payment_splits', function (Blueprint $table) {
$table->increments('id');
$table->integer('payment_id');
$table->integer('order_id');
$table->integer('order_id')->nullable();
$table->integer('bf_organization_id')->nullable();
$table->double('amount', 16, 4);
$table->enum('status', ['SPLIT', 'REFUNDED'])->default('SPLIT');
$table->dateTime('created_at')->default(now());
Expand Down
1 change: 1 addition & 0 deletions resources/views/models/orders/form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
x-data
x-on:input="isNaN(parseFloat($event.target.value.replace(/,/g, ''))) ? $event.target.value = 0 : $event.target.value = parseFloat($event.target.value.replace(/,/g, '')).toLocaleString('en-US')"
/>

<x-fab::forms.input
wire:model="formattedUnitPrice"
label="Price per litre (NGN)"
Expand Down
23 changes: 20 additions & 3 deletions resources/views/models/organizationOrders/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,24 @@

<div class="overflow-hidden rounded-lg bg-white px-4 py-5 shadow sm:p-6">
<dt class="truncate text-sm font-medium text-gray-500">All Time Debt Owed</dt>
<dd class="mt-1 text-xl font-semibold tracking-tight text-gray-900">{{ number_format(\Vibraniuum\Pamtechoga\Models\OrderDebt::where('organization_id', $this->organization)->sum('balance')) }}</dd>
@php
$totalDebtOwed = \Vibraniuum\Pamtechoga\Models\OrderDebt::where('organization_id', $this->organization)->sum('balance');
$organization = \Vibraniuum\Pamtechoga\Models\Organization::where('id', $this->organization)->first();
$bfFromOrganizationRecord = $organization->bf_amount;
$startDate = $this->startDate;
$endDate = $this->endDate;
$splitPaymentForOrganizationBf = \Vibraniuum\Pamtechoga\Models\PaymentSplit::where('bf_organization_id', $organization->id)
->whereHas('payment', function ($query) use ($startDate, $endDate) {
$query->whereBetween('payment_date', [$startDate, $endDate]);
})
->sum('amount');
$totalDebtOwed = $totalDebtOwed + ($bfFromOrganizationRecord - $splitPaymentForOrganizationBf);
@endphp
<dd class="mt-1 text-xl font-semibold tracking-tight text-gray-900">{{ number_format($totalDebtOwed) }}</dd>
</div>

<div class="overflow-hidden rounded-lg bg-white px-4 py-5 shadow sm:p-6">
Expand Down Expand Up @@ -65,7 +82,7 @@
<div class="mt-8 text-xl font-semibold tracking-tight text-gray-900">{{ \Vibraniuum\Pamtechoga\Models\Organization::where('id', $this->organization)->first()->name }}'s Orders for: {{ \Illuminate\Support\Carbon::make($startDate)->toFormattedDateString() }} - {{ \Illuminate\Support\Carbon::make($endDate)->toFormattedDateString() }}</div>

<div class="mt-4 flex justify-end">
<x-fab::elements.button type="button" wire:click="exportAsCSV">Export data as CSV</x-fab::elements.button>
{{-- <x-fab::elements.button type="button" wire:click="exportAsCSV">Export data as CSV</x-fab::elements.button>--}}
</div>
</div>

Expand Down Expand Up @@ -312,7 +329,7 @@
{{ \Illuminate\Support\Carbon::make($data->payment_date)->toFormattedDateString() }}
</td>
<td class="px-6 py-4">
{{ $data->type }}
{{ $data->type === 'DEBT' ? 'DOWN PAYMENT' : $data->type }}
</td>
<th scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
@if($data->type === 'CREDIT')
Expand Down
34 changes: 34 additions & 0 deletions resources/views/models/organizations/form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,40 @@

</x-fab::layouts.panel>

<x-fab::layouts.panel title="Balance Brought Forward Record">
<x-fab::forms.input
wire:model="formattedBFAmount"
label="BF Amount"
help="This is the amount owed at the given date."
{{-- :disabled="$model->exists"--}}
x-data
x-on:input="isNaN(parseFloat($event.target.value.replace(/,/g, ''))) ? $event.target.value = 0 : $event.target.value = parseFloat($event.target.value.replace(/,/g, '')).toLocaleString('en-US')"
/>

<x-fab::forms.input
{{-- wire:model="formattedBFBalance"--}}
label="BF Balance"
value="{{ number_format($this->getBalance()) }}"
help="This is the balance remaining to be paid."
:disabled="true"
x-data
x-on:input="isNaN(parseFloat($event.target.value.replace(/,/g, ''))) ? $event.target.value = 0 : $event.target.value = parseFloat($event.target.value.replace(/,/g, '')).toLocaleString('en-US')"
/>

<x-fab::forms.date-picker
wire:model="model.bf_date"
label="Date of Balance Brought Forward"
:options="[
'dateFormat' => 'Y-m-d',
'altInput' => true,
'altFormat' => 'D, M J, Y',
'enableTime' => false,
'maxDate' => Carbon::now()->format('Y-m-d')
]"
/>

</x-fab::layouts.panel>

<x-fab::layouts.panel
title="Branches"
description="Below are the branches created and assigned to this organization."
Expand Down
7 changes: 6 additions & 1 deletion resources/views/models/payments/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<dl class="mt-5 grid grid-cols-1 gap-4 sm:grid-cols-3">
@php
$debt = \Vibraniuum\Pamtechoga\Models\OrderDebt::sum('balance');
$totalBfAmount = \Vibraniuum\Pamtechoga\Models\Organization::sum('bf_amount');
$splitPaymentForOrganizationBf = \Vibraniuum\Pamtechoga\Models\PaymentSplit::where('bf_organization_id', '<>', null)->sum('amount');
$debt = $debt + ($totalBfAmount - $splitPaymentForOrganizationBf);
@endphp
<div class="overflow-hidden rounded-lg bg-white px-4 py-5 shadow sm:p-6">
<dt class="truncate text-sm font-medium text-gray-500">Total Organizations Debts Amount</dt>
Expand Down Expand Up @@ -61,7 +66,7 @@

@if($this->shouldShowColumn('type'))
<x-fab::lists.table.column>
<a href="{{ route('lego.pamtechoga.payments.edit', $data) }}">{{ $data->type }}</a>
<a href="{{ route('lego.pamtechoga.payments.edit', $data) }}">{{ $data->type === 'DEBT' ? 'DOWN PAYMENT' : $data->type }}</a>
</x-fab::lists.table.column>
@endif

Expand Down
27 changes: 27 additions & 0 deletions src/Http/Livewire/OrganizationsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Helix\Lego\Models\User;
use Vibraniuum\Pamtechoga\Models\OrganizationUser;
use Vibraniuum\Pamtechoga\Models\Payment;
use Vibraniuum\Pamtechoga\Models\PaymentSplit;
use Vibraniuum\Pamtechoga\Models\Review;
use Vibraniuum\Pamtechoga\Models\SupportMessage;

Expand All @@ -26,6 +27,9 @@ class OrganizationsForm extends Form

public SupportCollection $branches;

public $formattedBFAmount = 0;
public $formattedBFBalance = 0;

protected $listeners = [
'updateBranchesOrder',
];
Expand All @@ -40,9 +44,28 @@ public function rules()
'model.contact_person_name' => 'nullable',
'model.contact_person_phone' => 'nullable',
'model.contact_person_dob' => 'nullable',
'model.bf_amount' => 'nullable',
// 'model.bf_balance' => 'nullable',
'model.bf_date' => 'nullable',
];
}

public function updatedFormattedBFAmount()
{
$amount = (float) str_replace(',', '', $this->formattedBFAmount);
$this->model->bf_amount = $amount;
}

public function getBalance()
{
if ($this->model->id) {
$splitsTotal = PaymentSplit::where('bf_organization_id', $this->model->id)->sum('amount');
return $this->model->bf_amount - $splitsTotal;
}

return 0;
}

public function mount($organization = null)
{
$this->setModel($organization);
Expand All @@ -53,6 +76,10 @@ public function mount($organization = null)
}

$this->branches = collect($this->model->branches ?? []);

if ($this->model->exists) {
$this->formattedBFAmount = number_format($this->model->bf_amount);
}
//
// $this->selectedProducts = $this->model->products;
// $this->selectedProductsIds = $this->selectedProducts->map(fn ($product) => $product->id)->toArray();
Expand Down
41 changes: 35 additions & 6 deletions src/Services/ConfirmPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@
class ConfirmPayment
{
// algorithm
// check if organization's split payments total is less than organization bf_amount
// if true
// if payment amount > (bf_amount - split payments total) AKA bf_balance
// payment amount = payment amount - bf_balance
// create split record with bf_balance
// else
// create split record with payment amount
// payment amount = 0

// orders = get all incomplete orders
// if payment <= 0 : return
// if payment amount <= 0 : return
// for each order
// if order debt balance <= payment
// create split record
Expand All @@ -22,11 +31,11 @@ class ConfirmPayment
// set order debt balance to 0
// else
// create split record
// order debt balance -= payment
// payment = 0
// order debt balance -= payment amount
// payment amount = 0
// exit loop
// end for
// if payment > 0
// if payment amount > 0
// update organization credit balance
// log credit balance

Expand All @@ -35,6 +44,28 @@ public function run(Payment $payment)
$paymentAmount = $payment->amount;
$organizationId = $payment->organization_id;

$splitsTotal = PaymentSplit::where('bf_organization_id', $organizationId)->sum('amount');
$organization = Organization::where('id', $organizationId)->first();
$bfBalance = $organization->bf_amount - $splitsTotal;

if($bfBalance > 0) {
if($paymentAmount > $bfBalance) {
$paymentAmount = $paymentAmount - $bfBalance;
PaymentSplit::create([
'payment_id' => $payment->id,
'bf_organization_id' => $organizationId,
'amount' => $bfBalance,
]);
} else {
PaymentSplit::create([
'payment_id' => $payment->id,
'bf_organization_id' => $organizationId,
'amount' => $paymentAmount,
]);
$paymentAmount = 0;
}
}

// get only incomplete orders
$orders = Order::where('organization_id', $organizationId)
->where('payment_is_complete', false)
Expand Down Expand Up @@ -83,8 +114,6 @@ public function run(Payment $payment)
}

if ($paymentAmount > 0) {
$organization = Organization::where('id', $organizationId)->first();

// log credit balance
CreditLog::create([
'organization_id' => $organizationId,
Expand Down
17 changes: 17 additions & 0 deletions src/Traits/DateFilterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Vibraniuum\Pamtechoga\Models\Order;
use Vibraniuum\Pamtechoga\Models\DepotOrder;
use Vibraniuum\Pamtechoga\Models\DepotPickup;
use Vibraniuum\Pamtechoga\Models\PaymentSplit;
use Vibraniuum\Pamtechoga\Models\Product;
use Vibraniuum\Pamtechoga\Models\Organization;
use Vibraniuum\Pamtechoga\Models\Driver;
Expand Down Expand Up @@ -88,6 +89,22 @@ public function applyFilterExtension()

$this->bfDebt = max($sumOfOrdersAmountBeforeStartDateForBF?->total - $sumOfPaymentsBeforeStartDateForBF, 0);

///////////////////////

$bfFromOrganizationRecord = $organization->bf_amount;

$startDate = $this->startDate;
$endDate = $this->endDate;
$splitPaymentForOrganizationBf = PaymentSplit::where('bf_organization_id', $organization->id)
->whereHas('payment', function ($query) use ($startDate, $endDate) {
$query->whereBetween('payment_date', [$startDate, $endDate]);
})
->sum('amount');

$this->bfDebt = $this->bfDebt + ($bfFromOrganizationRecord - $splitPaymentForOrganizationBf);

////////////////////

$this->totalPaymentsWithinRange = Payment::where('organization_id', $organization->id)
->whereBetween('payment_date', [$this->startDate, $this->endDate])
->where('status', $status)
Expand Down

0 comments on commit ccdf709

Please sign in to comment.