2019-05-13 08:18:46 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
2019-10-01 07:54:21 +02:00
|
|
|
namespace App\Jobs\Invoice;
|
2019-05-13 08:18:46 +02:00
|
|
|
|
2019-05-14 06:05:05 +02:00
|
|
|
use App\Events\Payment\PaymentWasCreated;
|
2019-05-13 08:18:46 +02:00
|
|
|
use App\Factory\PaymentFactory;
|
2019-05-16 01:40:53 +02:00
|
|
|
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
|
2019-05-13 08:18:46 +02:00
|
|
|
use App\Jobs\Invoice\ApplyPaymentToInvoice;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Repositories\InvoiceRepository;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
2019-05-16 08:00:27 +02:00
|
|
|
class MarkInvoicePaid implements ShouldQueue
|
2019-05-13 08:18:46 +02:00
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
public $invoice;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(Invoice $invoice)
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->invoice = $invoice;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
/* Create Payment */
|
2019-05-14 06:05:05 +02:00
|
|
|
$payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id);
|
2019-05-13 08:18:46 +02:00
|
|
|
|
2019-05-14 06:05:05 +02:00
|
|
|
$payment->amount = $this->invoice->balance;
|
2019-05-13 08:18:46 +02:00
|
|
|
$payment->status_id = Payment::STATUS_COMPLETED;
|
2019-05-14 06:05:05 +02:00
|
|
|
$payment->client_id = $this->invoice->client_id;
|
2019-10-03 13:50:50 +02:00
|
|
|
$payment->transaction_reference = ctrans('texts.manual_entry');
|
2019-05-13 08:18:46 +02:00
|
|
|
/* Create a payment relationship to the invoice entity */
|
2019-10-01 06:27:04 +02:00
|
|
|
$payment->save();
|
2019-05-13 08:18:46 +02:00
|
|
|
$payment->invoices()->save($this->invoice);
|
2019-10-01 06:27:04 +02:00
|
|
|
$payment->save();
|
2019-05-13 08:18:46 +02:00
|
|
|
|
|
|
|
/* Update Invoice balance */
|
2019-10-01 06:27:04 +02:00
|
|
|
event(new PaymentWasCreated($payment));
|
2019-05-13 08:18:46 +02:00
|
|
|
|
2019-10-03 13:50:50 +02:00
|
|
|
UpdateCompanyLedgerWithPayment::dispatchNow($payment, ($payment->amount*-1));
|
|
|
|
|
2019-10-01 07:54:21 +02:00
|
|
|
return $this->invoice;
|
2019-05-13 08:18:46 +02:00
|
|
|
}
|
|
|
|
}
|