1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +01:00
invoiceninja/app/Jobs/Invoice/MarkPaid.php

83 lines
2.0 KiB
PHP
Raw Normal View History

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
*/
namespace App\Jobs\Invoice;
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;
use Illuminate\Support\Facades\Log;
2019-05-13 08:18:46 +02:00
class MarkPaid implements ShouldQueue
{
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 */
$payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id);
2019-05-13 08:18:46 +02:00
$payment->amount = $this->invoice->balance;
2019-05-13 08:18:46 +02:00
$payment->status_id = Payment::STATUS_COMPLETED;
$payment->client_id = $this->invoice->client_id;
2019-05-13 08:18:46 +02:00
$payment->save();
/* Create a payment relationship to the invoice entity */
$payment->invoices()->save($this->invoice);
2019-05-13 08:18:46 +02:00
$data = [
'payment_id' => $payment->id,
'invoice_ids' => [
$this->invoice->id
]
];
event(new PaymentWasCreated($data));
/* Update Invoice balance */
2019-05-16 01:40:53 +02:00
$invoice = ApplyPaymentToInvoice::dispatchNow($payment, $this->invoice);
2019-05-13 08:18:46 +02:00
2019-05-16 01:40:53 +02:00
UpdateCompanyLedgerWithPayment::dispatchNow($payment, $payment->amount);
return $invoice;
2019-05-13 08:18:46 +02:00
}
}