invoiceninja/app/Jobs/Invoice/MarkInvoicePaid.php

81 lines
2.2 KiB
PHP
Raw Normal View History

2019-05-13 16:18:46 +10: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 15:54:21 +10:00
namespace App\Jobs\Invoice;
2019-05-13 16:18:46 +10:00
use App\Events\Payment\PaymentWasCreated;
2019-05-13 16:18:46 +10:00
use App\Factory\PaymentFactory;
use App\Jobs\Client\UpdateClientBalance;
use App\Jobs\Client\UpdateClientPaidToDate;
2019-05-16 09:40:53 +10:00
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
2019-05-13 16:18:46 +10: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 16:00:27 +10:00
class MarkInvoicePaid implements ShouldQueue
2019-05-13 16:18:46 +10: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 */
$payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id);
2019-05-13 16:18:46 +10:00
$payment->amount = $this->invoice->balance;
2019-05-13 16:18:46 +10:00
$payment->status_id = Payment::STATUS_COMPLETED;
$payment->client_id = $this->invoice->client_id;
2019-10-03 21:50:50 +10:00
$payment->transaction_reference = ctrans('texts.manual_entry');
2019-05-13 16:18:46 +10:00
/* Create a payment relationship to the invoice entity */
$payment->save();
$payment->invoices()->attach($this->invoice->id,[
'amount' => $payment->amount
]);
2019-05-13 16:18:46 +10:00
$this->invoice->updateBalance($payment->amount*-1);
2019-05-13 16:18:46 +10:00
/* Update Invoice balance */
event(new PaymentWasCreated($payment));
2019-05-13 16:18:46 +10:00
2019-10-03 21:50:50 +10:00
UpdateCompanyLedgerWithPayment::dispatchNow($payment, ($payment->amount*-1));
UpdateClientBalance::dispatchNow($payment->client, $payment->amount*-1);
UpdateClientPaidToDate::dispatchNow($payment->client, $payment->amount);
2019-10-03 21:50:50 +10:00
2019-10-01 15:54:21 +10:00
return $this->invoice;
2019-05-13 16:18:46 +10:00
}
}