diff --git a/app/Services/Credit/MarkSent.php b/app/Services/Credit/MarkSent.php index fdd6e1d891bf..1591feb095cb 100644 --- a/app/Services/Credit/MarkSent.php +++ b/app/Services/Credit/MarkSent.php @@ -43,6 +43,7 @@ class MarkSent ->service() ->setStatus(Credit::STATUS_SENT) ->applyNumber() + ->adjustBalance($this->credit->amount) ->save(); diff --git a/app/Services/Invoice/MarkInvoiceDeleted.php b/app/Services/Invoice/MarkInvoiceDeleted.php index cc830756a21c..e7b55b669928 100644 --- a/app/Services/Invoice/MarkInvoiceDeleted.php +++ b/app/Services/Invoice/MarkInvoiceDeleted.php @@ -21,6 +21,8 @@ class MarkInvoiceDeleted extends AbstractService private $invoice; + private $adjustment_amount = 0; + public function __construct(Invoice $invoice) { $this->invoice = $invoice; @@ -28,7 +30,36 @@ class MarkInvoiceDeleted extends AbstractService public function run() { + if($this->invoice->is_deleted) + return $this->invoice; + + // if(in_array($this->invoice->status_id, ['currencies', 'industries', 'languages', 'countries', 'banks'])) + // return $this-> + + $this->cleanup() + ->setAdjustmentAmount() + ->deletePaymentables(); + } + + + private function setAdjustmentAmount() + { + + foreach ($this->invoice->payments as $payment) { + $this->adjustment_amount += $payment->paymentables + ->where('paymentable_type', '=', 'invoices') + ->where('paymentable_id', $this->invoice->id) + ->sum(DB::raw('amount')); + } + + return $this; + } + + private function cleanup() + { + $check = false; + $x=0; do { @@ -43,10 +74,10 @@ class MarkInvoiceDeleted extends AbstractService $this->invoice->tasks()->update(['invoice_id' => null]); $this->invoice->expenses()->update(['invoice_id' => null]); - return $this->invoice; + return $this; + } - private function calcNumber($x) { if ($x==0) { @@ -57,4 +88,19 @@ class MarkInvoiceDeleted extends AbstractService return $number; } + + + private function deletePaymentables() + { + + $this->invoice->payments->each(function ($payment){ + $payment->paymentables() + ->where('paymentable_type', '=', 'invoices') + ->where('paymentable_id', $this->invoice->id) + ->update(['deleted_at' => now()]); + }); + + + return $this; + } }