diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php index fa2a1d0..5a410d8 100644 --- a/app/Http/Controllers/AdminController.php +++ b/app/Http/Controllers/AdminController.php @@ -123,7 +123,7 @@ class AdminController extends Controller $oo->invoice_id = $id; } - $oo->alloc_amt = ($oo->invoice->due >= 0) && ($oo->invoice->due-$amount >= 0) ? $amount : 0; + $oo->amount = ($oo->invoice->due >= 0) && ($oo->invoice->due-$amount >= 0) ? $amount : 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 8c0fbcf..25b65b1 100644 --- a/app/Http/Controllers/PaypalController.php +++ b/app/Http/Controllers/PaypalController.php @@ -229,7 +229,7 @@ class PaypalController extends Controller $pio = new PaymentItem; $pio->site_id = 1; // @todo To implement $pio->invoice_id = $cap->invoice_id; - $pio->alloc_amt = $cap->amount->value-$po->fees_amt; + $pio->amount = $cap->amount->value-$po->fees_amt; $po->items->push($pio); diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 3ff2033..4a5fb2b 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -263,7 +263,7 @@ class Invoice extends Model implements IDs { return $this->paymentitems ->filter(function($item) { return ! $item->payment->pending_status; }) - ->sum('alloc_amt'); + ->sum('amount'); } /** @@ -293,7 +293,7 @@ class Invoice extends Model implements IDs { return $this->paymentitems ->filter(function($item) { return $item->payment->pending_status; }) - ->sum('alloc_amt'); + ->sum('amount'); } /** diff --git a/app/Models/Payment.php b/app/Models/Payment.php index ad3eba2..18373d7 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -26,11 +26,9 @@ class Payment extends Model implements IDs { use PushNew; - const CREATED_AT = 'date_orig'; - const UPDATED_AT = 'date_last'; - - protected $dates = ['payment_date']; - protected $dateFormat = 'U'; + protected $dates = [ + 'paid_at', + ]; // Array of items that can be updated with PushNew protected $pushable = ['items']; @@ -75,7 +73,7 @@ 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(alloc_amt) as allocated")]) + ->select(['payments.id','payment_date','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']) ->having(DB::raw('ROUND(total_amt-IFNULL(allocated,0),2)'),'>',self::threshold); @@ -85,7 +83,7 @@ class Payment extends Model implements IDs public function getBalanceAttribute(): float { - $balance = $this->getTotalAttribute()-$this->items->sum('alloc_amt'); + $balance = $this->getTotalAttribute()-$this->items->sum('amount'); return ($balance < self::threshold) ? 0 : $balance; } diff --git a/app/Models/PaymentItem.php b/app/Models/PaymentItem.php index 2c25d18..e0b41d5 100644 --- a/app/Models/PaymentItem.php +++ b/app/Models/PaymentItem.php @@ -4,16 +4,11 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; -use App\Traits\{NextKey,PushNew}; +use App\Traits\PushNew; class PaymentItem extends Model { use PushNew; - const RECORD_ID = 'payment_item'; - - protected $dateFormat = 'U'; - const CREATED_AT = 'date_orig'; - const UPDATED_AT = 'date_last'; /* RELATIONS */ diff --git a/app/Models/User.php b/app/Models/User.php index accb285..0028941 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -357,9 +357,9 @@ class User extends Authenticatable implements IDs ->select([ 'payment_id', 'invoice_id', - DB::raw('SUM(alloc_amt) AS allocate'), + DB::raw('SUM(amount) AS allocate'), ]) - ->where('alloc_amt','>',0) + ->where('amount','>',0) ->groupBy(['invoice_id','payment_id']); } diff --git a/database/migrations/2022_04_22_122640_rename_invoice_tables.php b/database/migrations/2022_04_22_122640_rename_invoice_tables.php index 1ef1eaa..8f0ab53 100644 --- a/database/migrations/2022_04_22_122640_rename_invoice_tables.php +++ b/database/migrations/2022_04_22_122640_rename_invoice_tables.php @@ -35,8 +35,8 @@ return new class extends Migration $o->created_at = \Carbon\Carbon::createFromTimestamp($o->date_orig); if ($o->date_last) $o->updated_at = \Carbon\Carbon::createFromTimestamp($o->date_last); - if ($o->due_date) - $o->due_at = \Carbon\Carbon::createFromTimestamp($o->due_date); + if ($o->getRawOriginal('due_date')) + $o->due_at = \Carbon\Carbon::createFromTimestamp($o->getRawOriginal('due_date')); if ($o->reminders) { try { $reminders = unserialize($o->reminders); diff --git a/database/migrations/2022_04_22_144438_optimise_payments.php b/database/migrations/2022_04_22_144438_optimise_payments.php new file mode 100644 index 0000000..9b7a8c9 --- /dev/null +++ b/database/migrations/2022_04_22_144438_optimise_payments.php @@ -0,0 +1,71 @@ +dropForeign(['site_id']); + $table->datetime('created_at')->nullable()->after('id'); + $table->datetime('updated_at')->nullable()->after('created_at'); + $table->boolean('active')->nullable()->after('updated_at'); + $table->date('paid_at')->nullable()->after('payment_date'); + + $table->foreign(['account_id','site_id'])->references(['id','site_id'])->on('accounts'); + $table->foreign(['source_id','site_id'])->references(['id','site_id'])->on('users'); + }); + + // Convert out dates + foreach (\App\Models\Payment::withoutGlobalScope(\App\Models\Scopes\SiteScope::class)->cursor() as $o) { + if ($o->date_orig) + $o->created_at = \Carbon\Carbon::createFromTimestamp($o->date_orig); + if ($o->date_last) + $o->updated_at = \Carbon\Carbon::createFromTimestamp($o->date_last); + if ($o->payment_date) + $o->paid_at = \Carbon\Carbon::createFromTimestamp($o->payment_date); + $o->save(); + } + + Schema::table('payments', function (Blueprint $table) { + $table->dropColumn(['date_orig','date_last','payment_date']); + }); + + DB::statement('ALTER TABLE payment_items MODIFY payment_id int unsigned NOT NULL'); + DB::statement('ALTER TABLE payment_items MODIFY invoice_id int unsigned DEFAULT NULL'); + DB::statement('ALTER TABLE payment_items RENAME COLUMN alloc_amt TO amount'); + + Schema::table('payment_items', function (Blueprint $table) { + $table->dropForeign(['site_id']); + $table->boolean('active')->nullable()->after('site_id'); + $table->dropColumn(['date_orig','date_last']); + + $table->foreign(['payment_id','site_id'])->references(['id','site_id'])->on('payments'); + }); + + DB::statement('UPDATE payment_items SET active=1'); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + abort(500,'Cant go back'); + } +}; 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 a3e4485..a1d8eb1 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 @@ -27,7 +27,7 @@
{{ $oo->account->name }} | {{ $oo->sid }} | -{{ $oo->payment_date->format('Y-m-d') }} | +{{ $oo->paid_at->format('Y-m-d') }} | ${{ number_format($oo->total,2) }} | {{--${{ number_format($oo->balance,2) }} | --}}