diff --git a/app/Http/Controllers/CreditController.php b/app/Http/Controllers/CreditController.php index 88a48009878a..067543775276 100644 --- a/app/Http/Controllers/CreditController.php +++ b/app/Http/Controllers/CreditController.php @@ -524,6 +524,11 @@ class CreditController extends BaseController { /*If we are using bulk actions, we don't want to return anything */ switch ($action) { + case 'mark_paid': + $credit->service()->markPaid()->save(); + return $this->itemResponse($credit); + break; + case 'clone_to_credit': $credit = CloneCreditFactory::create($credit, auth()->user()->id); diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index c7ae018be8f0..0307d177d132 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -191,7 +191,7 @@ class PaymentRepository extends BaseRepository { if(array_key_exists('exchange_rate', $data) && isset($data['exchange_rate'])) return $payment; - $client = Client::find($data['client_id']); + $client = Client::withTrashed()->find($data['client_id']); $client_currency = $client->getSetting('currency_id'); $company_currency = $client->company->settings->currency_id; diff --git a/app/Services/Credit/CreditService.php b/app/Services/Credit/CreditService.php index c6b41fb3eef3..9a867284afa3 100644 --- a/app/Services/Credit/CreditService.php +++ b/app/Services/Credit/CreditService.php @@ -11,8 +11,12 @@ namespace App\Services\Credit; +use App\Factory\PaymentFactory; use App\Jobs\Util\UnlinkFile; use App\Models\Credit; +use App\Models\Payment; +use App\Models\PaymentType; +use App\Repositories\PaymentRepository; use App\Services\Credit\CreateInvitations; use App\Services\Credit\TriggeredActions; use App\Utils\Traits\MakesHash; @@ -79,6 +83,60 @@ class CreditService return $this; } + /* + For euro users - we mark a credit as paid when + we need to document a refund of sorts. + + Criteria: Credit must be a negative value + A negative payment for the balance will be generated + This amount will be reduced from the clients paid to date. + + */ + public function markPaid() + { + if($this->credit->balance > 0) + return $this; + + $payment_repo = new PaymentRepository(); + + //set credit balance to zero + $adjustment = $this->credit->balance; + + $this->updateBalance($adjustment) + ->updatePaidToDate($adjustment) + ->save(); + + //create a negative payment of total $this->credit->balance + $payment = PaymentFactory::create($this->credit->company_id, $this->credit->user_id); + $payment->client_id = $this->credit->client_id; + $payment->amount = $adjustment; + $payment->applied = $adjustment; + $payment->refunded = 0; + $payment->status_id = Payment::STATUS_COMPLETED; + $payment->type_id = PaymentType::CREDIT; + $payment->is_manual = true; + $payment->date = now(); + + $payment->saveQuietly(); + $payment->number = $payment->client->getNextPaymentNumber($payment->client, $payment); + $payment = $payment_repo->processExchangeRates(['client_id' => $this->credit->client_id], $payment); + $payment->saveQuietly(); + + $payment + ->credits() + ->attach($this->credit->id, ['amount' => $adjustment]); + + //reduce client paid_to_date by $this->credit->balance amount + $this->credit + ->client + ->updatePaidToDate($adjustment) + ->save(); + + event('eloquent.created: App\Models\Payment', $payment); + + return $this; + } + public function markSent() { $this->credit = (new MarkSent($this->credit->client, $this->credit))->run();