2020-02-03 11:33:07 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Invoice;
|
|
|
|
|
|
|
|
use App\Events\Payment\PaymentWasCreated;
|
|
|
|
use App\Factory\PaymentFactory;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
2020-02-17 10:37:44 +01:00
|
|
|
use App\Services\AbstractService;
|
2020-02-03 11:33:07 +01:00
|
|
|
use App\Services\Client\ClientService;
|
|
|
|
use App\Services\Payment\PaymentService;
|
2020-02-26 11:06:08 +01:00
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
class MarkPaid extends AbstractService
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
2020-02-26 11:06:08 +01:00
|
|
|
use GeneratesCounter;
|
|
|
|
|
2020-02-03 11:33:07 +01:00
|
|
|
private $client_service;
|
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
private $invoice;
|
|
|
|
|
|
|
|
public function __construct(ClientService $client_service, Invoice $invoice)
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
|
|
|
$this->client_service = $client_service;
|
2020-02-17 10:37:44 +01:00
|
|
|
|
|
|
|
$this->invoice = $invoice;
|
2020-02-03 11:33:07 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
public function run()
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
if($this->invoice->status_id == Invoice::STATUS_DRAFT)
|
|
|
|
$this->invoice->service()->markSent();
|
2020-02-03 11:33:07 +01:00
|
|
|
|
|
|
|
/* Create Payment */
|
2020-02-17 10:37:44 +01:00
|
|
|
$payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id);
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
$payment->amount = $this->invoice->balance;
|
2020-02-26 11:46:35 +01:00
|
|
|
$payment->number = $this->getNextPaymentNumber($this->invoice->client);
|
2020-02-03 11:33:07 +01:00
|
|
|
$payment->status_id = Payment::STATUS_COMPLETED;
|
2020-02-17 10:37:44 +01:00
|
|
|
$payment->client_id = $this->invoice->client_id;
|
2020-02-03 11:33:07 +01:00
|
|
|
$payment->transaction_reference = ctrans('texts.manual_entry');
|
|
|
|
/* Create a payment relationship to the invoice entity */
|
|
|
|
$payment->save();
|
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
$payment->invoices()->attach($this->invoice->id, [
|
2020-02-03 11:33:07 +01:00
|
|
|
'amount' => $payment->amount
|
|
|
|
]);
|
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
$this->invoice->service()
|
2020-02-04 08:51:44 +01:00
|
|
|
->updateBalance($payment->amount*-1)
|
|
|
|
->setStatus(Invoice::STATUS_PAID)
|
|
|
|
->save();
|
2020-02-03 11:33:07 +01:00
|
|
|
|
|
|
|
/* Update Invoice balance */
|
|
|
|
event(new PaymentWasCreated($payment, $payment->company));
|
|
|
|
|
2020-02-20 22:05:01 +01:00
|
|
|
$payment->ledger()
|
|
|
|
->updatePaymentBalance($payment->amount*-1);
|
2020-02-14 04:32:22 +01:00
|
|
|
|
2020-02-03 11:33:07 +01:00
|
|
|
$this->client_service
|
|
|
|
->updateBalance($payment->amount*-1)
|
|
|
|
->updatePaidToDate($payment->amount)
|
|
|
|
->save();
|
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
return $this->invoice;
|
2020-02-03 11:33:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|