Fix payment entry

This commit is contained in:
Deon George 2022-06-13 15:46:38 +10:00
parent 9e889008bf
commit 49a4830d89
10 changed files with 42 additions and 25 deletions

View File

@ -6,7 +6,16 @@ use Illuminate\Http\Request;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use App\Models\{Account,Charge,InvoiceItem,Payment,PaymentItem,Service,SiteDetail,Supplier,SupplierDetail}; use App\Models\{Account,
Charge,
Invoice,
InvoiceItem,
Payment,
PaymentItem,
Service,
SiteDetail,
Supplier,
SupplierDetail};
/** /**
* The AdminController governs all routes that are prefixed with 'a/'. * The AdminController governs all routes that are prefixed with 'a/'.
@ -83,9 +92,10 @@ class AdminController extends Controller
public function pay_addedit(Request $request,Payment $o) public function pay_addedit(Request $request,Payment $o)
{ {
if ($request->post()) { if ($request->post()) {
$validation = $request->validate([ $validation = $request->validate([
'account_id' => 'required|exists:accounts,id', 'account_id' => 'required|exists:accounts,id',
'payment_date' => 'required|date', 'paid_at' => 'required|date',
'checkout_id' => 'required|exists:ab_checkout,id', 'checkout_id' => 'required|exists:ab_checkout,id',
'total_amt' => 'required|numeric|min:0.01', 'total_amt' => 'required|numeric|min:0.01',
'fees_amt' => 'nullable|numeric|lt:total_amt', 'fees_amt' => 'nullable|numeric|lt:total_amt',
@ -93,15 +103,18 @@ class AdminController extends Controller
'pending' => 'nullable|boolean', 'pending' => 'nullable|boolean',
'notes' => 'nullable|string', 'notes' => 'nullable|string',
'ip' => 'nullable|ip', 'ip' => 'nullable|ip',
'invoices' => ['nullable','array',function ($attribute,$value,$fail) use ($request) { 'invoices' => ['required','array',function ($attribute,$value,$fail) use ($request) {
if (collect($value)->sum() > $request->post('total_amt')) if (collect($value)->sum('id') > $request->post('total_amt'))
$fail('Allocation is greater than payment total.'); $fail('Allocation is greater than payment total.');
}], }],
'invoices.*.id' => 'nullable|exists:invoices,id', 'invoices.*.id' => ['required',function ($attribute,$value,$fail) {
if (! Invoice::exists(str_replace(str_replace($attribute,'invoice\.','',),'.id','')))
$fail('Invoice doesnt exist in DB');
}],
]); ]);
if (! $o->exists) { if (! $o->exists) {
$o->forceFill($request->only(['account_id','payment_date','checkout_id','checkout_id','total_amt','fees_amt','source_id','pending','notes','ip'])); $o->forceFill($request->only(['account_id','paid_at','checkout_id','checkout_id','total_amt','fees_amt','source_id','pending','notes','ip']));
$o->site_id = config('site')->site_id; $o->site_id = config('site')->site_id;
$o->save(); $o->save();
} }
@ -113,7 +126,7 @@ class AdminController extends Controller
if ($items->count() == 1) { if ($items->count() == 1) {
$oo = $items->pop(); $oo = $items->pop();
if (! $amount) { if (! $amount['id']) {
$oo->delete(); $oo->delete();
continue; continue;
} }
@ -123,7 +136,7 @@ class AdminController extends Controller
$oo->invoice_id = $id; $oo->invoice_id = $id;
} }
$oo->amount = ($oo->invoice->due >= 0) && ($oo->invoice->due-$amount >= 0) ? $amount : 0; $oo->amount = ($oo->invoice->due >= 0) && ($oo->invoice->due-$amount['id'] >= 0) ? $amount['id'] : 0;
$oo->site_id = config('site')->site_id; $oo->site_id = config('site')->site_id;
$o->items()->save($oo); $o->items()->save($oo);
} }

View File

@ -217,7 +217,7 @@ class PaypalController extends Controller
break; break;
} }
$po->payment_date = Carbon::parse($cap->create_time); $po->paid_at = Carbon::parse($cap->create_time);
$po->checkout_id = $this->o->id; $po->checkout_id = $this->o->id;
$po->checkout_data = $cap->id; $po->checkout_data = $cap->id;

View File

@ -57,7 +57,7 @@ final class PaymentsImport implements ShouldQueue
} }
// Find the last payment logged // Find the last payment logged
$last = Carbon::createFromTimestamp(Payment::whereIN('checkout_id',$cos)->where('account_id',$ao->id)->max('payment_date')); $last = Carbon::createFromTimestamp(Payment::whereIN('checkout_id',$cos)->where('account_id',$ao->id)->max('paid_at'));
$o = $this->o->getDebits([ $o = $this->o->getDebits([
'customerId'=>$c->Id, 'customerId'=>$c->Id,
@ -79,7 +79,7 @@ final class PaymentsImport implements ShouldQueue
$lp = $ao->payments->last(); $lp = $ao->payments->last();
if ($lp AND (($pd == $lp->payment_date) OR ($p->Id == $lp->checkout_data))) { if ($lp AND (($pd == $lp->paid_at) OR ($p->Id == $lp->checkout_data))) {
Log::alert(sprintf('%s:Payment Already Recorded: [%s] %s %s (%s)',self::LOGKEY,$pd->format('Y-m-d'),$ao->id,$p->Id,$p->Amount)); Log::alert(sprintf('%s:Payment Already Recorded: [%s] %s %s (%s)',self::LOGKEY,$pd->format('Y-m-d'),$ao->id,$p->Id,$p->Amount));
continue; continue;
} }
@ -87,7 +87,7 @@ final class PaymentsImport implements ShouldQueue
// New Payment // New Payment
$po = new Payment; $po = new Payment;
$po->site_id = 1; // @todo $po->site_id = 1; // @todo
$po->payment_date = $pd; $po->paid_at = $pd;
$po->checkout_id = '999'; // @todo $po->checkout_id = '999'; // @todo
$po->checkout_data = $p->Id; $po->checkout_data = $p->Id;
$po->total_amt = $p->Amount; $po->total_amt = $p->Amount;

View File

@ -281,7 +281,7 @@ class Invoice extends Model implements IDs
->filter(function($item) { return ! $item->pending_status; }) ->filter(function($item) { return ! $item->pending_status; })
->last(); ->last();
return $o?->payment_date; return $o?->paid_at;
} }
/** /**

View File

@ -15,7 +15,7 @@ use App\Traits\PushNew;
* *
* Attributes for payments: * Attributes for payments:
* + lid : Local ID for payment * + lid : Local ID for payment
* + payment_date : Date payment received * + paid_at : Date payment received
* + sid : System ID for payment * + sid : System ID for payment
* + total : Payment total * + total : Payment total
* + balance : Remaining credit on payment * + balance : Remaining credit on payment
@ -95,9 +95,9 @@ class Payment extends Model implements IDs
public function scopeUnapplied($query) public function scopeUnapplied($query)
{ {
return $query return $query
->select(['payments.id','payment_date','account_id','checkout_id','total_amt',DB::raw("SUM(amount) as allocated")]) ->select(['payments.id','paid_at','account_id','checkout_id','total_amt',DB::raw("SUM(amount) as allocated")])
->leftJoin('payment_items',['payment_items.payment_id'=>'payments.id']) ->leftJoin('payment_items',['payment_items.payment_id'=>'payments.id'])
->groupBy(['payments.id','payment_date','total_amt','account_id','checkout_id']) ->groupBy(['payments.id','paid_at','total_amt','account_id','checkout_id'])
->having(DB::raw('ROUND(total_amt-IFNULL(allocated,0),2)'),'>',self::threshold); ->having(DB::raw('ROUND(total_amt-IFNULL(allocated,0),2)'),'>',self::threshold);
} }

View File

@ -10,6 +10,8 @@ class PaymentItem extends Model
{ {
use PushNew; use PushNew;
public $timestamps = FALSE;
/* RELATIONS */ /* RELATIONS */
public function invoice() public function invoice()

View File

@ -16,7 +16,6 @@
@section('main-content') @section('main-content')
<div class="row"> <div class="row">
<div class="col-6"> <div class="col-6">
<div class="card card-dark"> <div class="card card-dark">
<div class="card-header"> <div class="card-header">
<h1 class="card-title">Record Payment</h1> <h1 class="card-title">Record Payment</h1>
@ -33,14 +32,14 @@
<!-- DATE RECEIVED --> <!-- DATE RECEIVED -->
<div class="col-4"> <div class="col-4">
<div class="form-group has-validation"> <div class="form-group has-validation">
<label for="payment_date">Date Received</label> <label for="paid_at">Date Received</label>
<div class="input-group"> <div class="input-group">
<div class="input-group-prepend"> <div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-fw fa-calendar"></i></span> <span class="input-group-text"><i class="fas fa-fw fa-calendar"></i></span>
</div> </div>
<input type="date" class="form-control @error('payment_date') is-invalid @enderror" id="payment_date" name="payment_date" value="{{ old('payment_date',($o->exists ? $o->payment_date : \Carbon\Carbon::now())->format('Y-m-d')) }}" required> <input type="date" class="form-control @error('paid_at') is-invalid @enderror" id="paid_at" name="paid_at" value="{{ old('paid_at',($o->exists ? $o->paid_at : \Carbon\Carbon::now())->format('Y-m-d')) }}" required>
<span class="invalid-feedback" role="alert"> <span class="invalid-feedback" role="alert">
@error('payment_date') @error('paid_at')
{{ $message }} {{ $message }}
@else @else
Payment Date is required. Payment Date is required.

View File

@ -36,7 +36,7 @@
@if (! $o->balance) @continue @endif @if (! $o->balance) @continue @endif
<tr> <tr>
<td><a href="{{ url('a/payment/addedit',$o->id) }}">{{ $o->id }}</td> <td><a href="{{ url('a/payment/addedit',$o->id) }}">{{ $o->id }}</td>
<td>{{ $o->payment_date->format('Y-m-d') }}</td> <td>{{ $o->paid_at->format('Y-m-d') }}</td>
<td>{{ $o->account->name }}</td> <td>{{ $o->account->name }}</td>
<td>{{ $o->checkout->name }}</td> <td>{{ $o->checkout->name }}</td>
<td class="text-right">{{ number_format($o->total_amt,2) }}</td> <td class="text-right">{{ number_format($o->total_amt,2) }}</td>

View File

@ -2,7 +2,7 @@
<label for="checkout_id">Invoices Due</label> <label for="checkout_id">Invoices Due</label>
@if(($x=$o->invoices() @if(($x=$o->invoices()
->where('active',TRUE) ->where('active',TRUE)
->orderBy('due_date') ->orderBy('due_at')
->with(['items.taxes','paymentitems.payment','account']) ->with(['items.taxes','paymentitems.payment','account'])
->get() ->get()
->filter(function($item) use ($pid) { return $item->due > 0 || $item->payments->search(function($item) use ($pid) { return $item->id == $pid; }) !== FALSE; }))->count()) ->filter(function($item) use ($pid) { return $item->due > 0 || $item->payments->search(function($item) use ($pid) { return $item->id == $pid; }) !== FALSE; }))->count())
@ -23,11 +23,11 @@
<tr> <tr>
<td><a href="{{ url('u/invoice',[$io->id]) }}">{{ $io->sid }}</a></td> <td><a href="{{ url('u/invoice',[$io->id]) }}">{{ $io->sid }}</a></td>
<td>{{ $io->invoice_date->format('Y-m-d') }}</td> <td>{{ $io->invoice_date->format('Y-m-d') }}</td>
<td>{{ $io->due_date->format('Y-m-d') }}</td> <td>{{ $io->due_at->format('Y-m-d') }}</td>
<td>{{ number_format($io->total,2) }}</td> <td>{{ number_format($io->total,2) }}</td>
<td>{{ number_format($io->due,2) }}</td> <td>{{ number_format($io->due,2) }}</td>
<td class="text-right"> <td class="text-right">
<input type="text" class="text-right invoice" name="invoices[{{ $io->id }}]" value="{{ number_format(($x=$io->paymentitems->filter(function($item) use ($pid) { return $item->payment_id == $pid; })) ? $x->sum('amount') : 0,2) }}"> <input type="text" class="text-right invoice" name="invoices[{{ $io->id }}][id]" value="{{ number_format(($x=$io->paymentitems->filter(function($item) use ($pid) { return $item->payment_id == $pid; })) ? $x->sum('amount') : 0,2) }}">
</td> </td>
</tr> </tr>
@endforeach @endforeach

View File

@ -1,6 +1,6 @@
<?php <?php
use App\Http\Controllers\{CheckoutController,ProductController,ResellerServicesController}; use App\Http\Controllers\{AdminController,CheckoutController,ProductController,ResellerServicesController};
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@ -22,6 +22,9 @@ Route::group(['middleware'=>['auth:api','role:wholesaler']], function() {
Route::group(['middleware'=>['auth:api','role:reseller']], function() { Route::group(['middleware'=>['auth:api','role:reseller']], function() {
Route::get('/r/services/{o}',[ResellerServicesController::class,'services']) Route::get('/r/services/{o}',[ResellerServicesController::class,'services'])
->where('o','[0-9]+'); ->where('o','[0-9]+');
Route::post('r/invoices/{o}',[AdminController::class,'pay_invoices'])
->where('o','[0-9]+')
->middleware(['theme:adminlte-be','role:wholesaler']);
}); });
Route::group(['middleware'=>'auth:api'], function() { Route::group(['middleware'=>'auth:api'], function() {