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')