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;
|
|
|
|
|
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-14 06:05:05 +02:00
|
|
|
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 */
|
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-05-13 08:18:46 +02:00
|
|
|
|
|
|
|
$payment->save();
|
|
|
|
|
|
|
|
/* Create a payment relationship to the invoice entity */
|
|
|
|
$payment->invoices()->save($this->invoice);
|
2019-05-14 12:27:47 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|