From 49a4830d89805792ed181392af5ab8f1ae2cffcc Mon Sep 17 00:00:00 2001 From: Deon George Date: Mon, 13 Jun 2022 15:46:38 +1000 Subject: [PATCH] Fix payment entry --- app/Http/Controllers/AdminController.php | 29 ++++++++++++++----- app/Http/Controllers/PaypalController.php | 2 +- app/Jobs/PaymentsImport.php | 6 ++-- app/Models/Invoice.php | 2 +- app/Models/Payment.php | 6 ++-- app/Models/PaymentItem.php | 2 ++ .../adminlte/a/payment/addedit.blade.php | 7 ++--- .../adminlte/a/payment/unapplied.blade.php | 2 +- .../a/payment/widgets/invoices.blade.php | 6 ++-- routes/api.php | 5 +++- 10 files changed, 42 insertions(+), 25 deletions(-) diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php index b9b43a6..814f174 100644 --- a/app/Http/Controllers/AdminController.php +++ b/app/Http/Controllers/AdminController.php @@ -6,7 +6,16 @@ use Illuminate\Http\Request; use Illuminate\Support\Arr; 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/'. @@ -83,9 +92,10 @@ class AdminController extends Controller public function pay_addedit(Request $request,Payment $o) { if ($request->post()) { + $validation = $request->validate([ 'account_id' => 'required|exists:accounts,id', - 'payment_date' => 'required|date', + 'paid_at' => 'required|date', 'checkout_id' => 'required|exists:ab_checkout,id', 'total_amt' => 'required|numeric|min:0.01', 'fees_amt' => 'nullable|numeric|lt:total_amt', @@ -93,15 +103,18 @@ class AdminController extends Controller 'pending' => 'nullable|boolean', 'notes' => 'nullable|string', 'ip' => 'nullable|ip', - 'invoices' => ['nullable','array',function ($attribute,$value,$fail) use ($request) { - if (collect($value)->sum() > $request->post('total_amt')) + 'invoices' => ['required','array',function ($attribute,$value,$fail) use ($request) { + if (collect($value)->sum('id') > $request->post('total_amt')) $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) { - $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->save(); } @@ -113,7 +126,7 @@ class AdminController extends Controller if ($items->count() == 1) { $oo = $items->pop(); - if (! $amount) { + if (! $amount['id']) { $oo->delete(); continue; } @@ -123,7 +136,7 @@ class AdminController extends Controller $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; $o->items()->save($oo); } diff --git a/app/Http/Controllers/PaypalController.php b/app/Http/Controllers/PaypalController.php index 25b65b1..e134904 100644 --- a/app/Http/Controllers/PaypalController.php +++ b/app/Http/Controllers/PaypalController.php @@ -217,7 +217,7 @@ class PaypalController extends Controller 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_data = $cap->id; diff --git a/app/Jobs/PaymentsImport.php b/app/Jobs/PaymentsImport.php index 4c38070..0caab18 100644 --- a/app/Jobs/PaymentsImport.php +++ b/app/Jobs/PaymentsImport.php @@ -57,7 +57,7 @@ final class PaymentsImport implements ShouldQueue } // 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([ 'customerId'=>$c->Id, @@ -79,7 +79,7 @@ final class PaymentsImport implements ShouldQueue $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)); continue; } @@ -87,7 +87,7 @@ final class PaymentsImport implements ShouldQueue // New Payment $po = new Payment; $po->site_id = 1; // @todo - $po->payment_date = $pd; + $po->paid_at = $pd; $po->checkout_id = '999'; // @todo $po->checkout_data = $p->Id; $po->total_amt = $p->Amount; diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 4a5fb2b..8d23184 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -281,7 +281,7 @@ class Invoice extends Model implements IDs ->filter(function($item) { return ! $item->pending_status; }) ->last(); - return $o?->payment_date; + return $o?->paid_at; } /** diff --git a/app/Models/Payment.php b/app/Models/Payment.php index 79902d4..5284e55 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -15,7 +15,7 @@ use App\Traits\PushNew; * * Attributes for payments: * + lid : Local ID for payment - * + payment_date : Date payment received + * + paid_at : Date payment received * + sid : System ID for payment * + total : Payment total * + balance : Remaining credit on payment @@ -95,9 +95,9 @@ class Payment extends Model implements IDs public function scopeUnapplied($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']) - ->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); } diff --git a/app/Models/PaymentItem.php b/app/Models/PaymentItem.php index e0b41d5..500fca0 100644 --- a/app/Models/PaymentItem.php +++ b/app/Models/PaymentItem.php @@ -10,6 +10,8 @@ class PaymentItem extends Model { use PushNew; + public $timestamps = FALSE; + /* RELATIONS */ public function invoice() diff --git a/resources/views/theme/backend/adminlte/a/payment/addedit.blade.php b/resources/views/theme/backend/adminlte/a/payment/addedit.blade.php index 600cc60..d25c70a 100644 --- a/resources/views/theme/backend/adminlte/a/payment/addedit.blade.php +++ b/resources/views/theme/backend/adminlte/a/payment/addedit.blade.php @@ -16,7 +16,6 @@ @section('main-content')
-

Record Payment

@@ -33,14 +32,14 @@
- +
- + - @error('payment_date') + @error('paid_at') {{ $message }} @else Payment Date is required. diff --git a/resources/views/theme/backend/adminlte/a/payment/unapplied.blade.php b/resources/views/theme/backend/adminlte/a/payment/unapplied.blade.php index 45885ae..5d1d302 100644 --- a/resources/views/theme/backend/adminlte/a/payment/unapplied.blade.php +++ b/resources/views/theme/backend/adminlte/a/payment/unapplied.blade.php @@ -36,7 +36,7 @@ @if (! $o->balance) @continue @endif {{ $o->id }} - {{ $o->payment_date->format('Y-m-d') }} + {{ $o->paid_at->format('Y-m-d') }} {{ $o->account->name }} {{ $o->checkout->name }} {{ number_format($o->total_amt,2) }} diff --git a/resources/views/theme/backend/adminlte/a/payment/widgets/invoices.blade.php b/resources/views/theme/backend/adminlte/a/payment/widgets/invoices.blade.php index a1d8eb1..00cbb09 100644 --- a/resources/views/theme/backend/adminlte/a/payment/widgets/invoices.blade.php +++ b/resources/views/theme/backend/adminlte/a/payment/widgets/invoices.blade.php @@ -2,7 +2,7 @@ @if(($x=$o->invoices() ->where('active',TRUE) - ->orderBy('due_date') + ->orderBy('due_at') ->with(['items.taxes','paymentitems.payment','account']) ->get() ->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 @@ {{ $io->sid }} {{ $io->invoice_date->format('Y-m-d') }} - {{ $io->due_date->format('Y-m-d') }} + {{ $io->due_at->format('Y-m-d') }} {{ number_format($io->total,2) }} {{ number_format($io->due,2) }} - + @endforeach diff --git a/routes/api.php b/routes/api.php index 0f7f854..0d62f40 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,6 +1,6 @@ ['auth:api','role:wholesaler']], function() { Route::group(['middleware'=>['auth:api','role:reseller']], function() { Route::get('/r/services/{o}',[ResellerServicesController::class,'services']) ->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() {