diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index 26a360722a98..7130c1a9a1ab 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -22,6 +22,7 @@ use App\Models\Credit; use App\Models\Invitation; use App\Models\Invoice; use App\Models\InvoiceInvitation; +use App\Models\Payment; use App\Utils\Ninja; use Carbon; use DB; @@ -325,8 +326,11 @@ class CheckData extends Command $total_invoice_payments = 0; foreach ($client->invoices->where('is_deleted', false)->where('status_id', '>', 1) as $invoice) { - $total_amount = $invoice->payments->whereNull('deleted_at')->sum('pivot.amount'); - $total_refund = $invoice->payments->whereNull('deleted_at')->sum('pivot.refunded'); + // $total_amount = $invoice->payments->whereNull('deleted_at')->sum('pivot.amount'); + // $total_refund = $invoice->payments->whereNull('deleted_at')->sum('pivot.refunded'); + + $total_amount = $invoice->payments->where('is_deleted', false)->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED])->sum('pivot.amount'); + $total_refund = $invoice->payments->where('is_deleted', false)->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED])->sum('pivot.refunded'); $total_invoice_payments += ($total_amount - $total_refund); } @@ -360,8 +364,8 @@ class CheckData extends Command Client::cursor()->each(function ($client) use ($wrong_balances) { $client->invoices->where('is_deleted', false)->whereIn('status_id', '!=', Invoice::STATUS_DRAFT)->each(function ($invoice) use ($wrong_balances, $client) { - $total_amount = $invoice->payments->sum('pivot.amount'); - $total_refund = $invoice->payments->sum('pivot.refunded'); + $total_amount = $invoice->payments->whereIn('status_id', [Payment::STATUS_PAID, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED])->sum('pivot.amount'); + $total_refund = $invoice->payments->whereIn('status_id', [Payment::STATUS_PAID, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED])->sum('pivot.refunded'); $total_credit = $invoice->credits->sum('amount'); $total_paid = $total_amount - $total_refund; diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index d850444409f3..50c35ef9c217 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -186,7 +186,7 @@ class Invoice extends BaseModel public function payments() { - return $this->morphToMany(Payment::class, 'paymentable')->withPivot('amount', 'refunded')->withTimestamps()->withTrashed(); + return $this->morphToMany(Payment::class, 'paymentable')->withTrashed()->withPivot('amount', 'refunded')->withTimestamps(); } public function company_ledger() diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index 673d701b96b2..bdf05cfc6745 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -12,6 +12,7 @@ namespace App\Repositories; use App\Events\Payment\PaymentWasCreated; +use App\Events\Payment\PaymentWasDeleted; use App\Factory\CreditFactory; use App\Jobs\Credit\ApplyCreditPayment; use App\Libraries\Currency\Conversion\CurrencyApi; @@ -146,7 +147,7 @@ class PaymentRepository extends BaseRepository //todo optimize into a single query foreach ($data['credits'] as $paid_credit) { - $credit = Credit::find($this->decodePrimaryKey($paid_credit['credit_id']))->withTrashed(); + $credit = Credit::withTrashed()->find($this->decodePrimaryKey($paid_credit['credit_id'])); if ($credit) { ApplyCreditPayment::dispatchNow($credit, $payment, $paid_credit['amount'], $credit->company); @@ -198,9 +199,12 @@ class PaymentRepository extends BaseRepository return; } - $payment->service()->deletePayment(); + $payment = $payment->service()->deletePayment(); - return parent::delete($payment); + event(new PaymentWasDeleted($payment, $payment->company, Ninja::eventVars())); + + return $payment; + //return parent::delete($payment); } public function restore($payment)