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
|
|
|
|
2019-05-14 14:05:05 +10:00
|
|
|
use App\Events\Payment\PaymentWasCreated;
|
2019-05-13 16:18:46 +10:00
|
|
|
use App\Factory\PaymentFactory;
|
2019-11-20 08:06:48 +11:00
|
|
|
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 */
|
2019-05-14 14:05:05 +10:00
|
|
|
$payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id);
|
2019-05-13 16:18:46 +10:00
|
|
|
|
2019-05-14 14:05:05 +10:00
|
|
|
$payment->amount = $this->invoice->balance;
|
2019-05-13 16:18:46 +10:00
|
|
|
$payment->status_id = Payment::STATUS_COMPLETED;
|
2019-05-14 14:05:05 +10:00
|
|
|
$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 */
|
2019-10-01 14:27:04 +10:00
|
|
|
$payment->save();
|
2019-11-19 21:23:56 +11:00
|
|
|
|
|
|
|
|
$payment->invoices()->attach($this->invoice->id,[
|
|
|
|
|
'amount' => $payment->amount
|
|
|
|
|
]);
|
2019-05-13 16:18:46 +10:00
|
|
|
|
2019-10-20 17:23:35 +11:00
|
|
|
$this->invoice->updateBalance($payment->amount*-1);
|
|
|
|
|
|
2019-05-13 16:18:46 +10:00
|
|
|
/* Update Invoice balance */
|
2019-10-01 14:27:04 +10:00
|
|
|
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));
|
2019-11-20 16:41:49 +11:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|