1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php

75 lines
1.8 KiB
PHP
Raw Normal View History

2019-05-15 06:47:07 +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\Company;
use App\Factory\CompanyLedgerFactory;
use App\Models\CompanyLedger;
use App\Models\Invoice;
2019-05-15 07:03:18 +02:00
use App\Models\Payment;
2019-05-15 06:47:07 +02:00
use Illuminate\Foundation\Bus\Dispatchable;
2019-05-15 07:03:18 +02:00
/**
* Class for update company ledger with payment.
*/
2019-05-15 06:47:07 +02:00
class UpdateCompanyLedgerWithPayment
{
use Dispatchable;
public $adjustment;
2019-05-16 01:40:53 +02:00
public $payment;
2019-05-15 07:03:18 +02:00
2019-05-15 06:47:07 +02:00
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Payment $payment, float $adjustment)
{
$this->payment = $payment;
$this->adjustment = $adjustment;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$balance = 0;
2019-05-16 07:36:53 +02:00
2019-05-15 07:03:18 +02:00
/* Get the last record for the client and set the current balance*/
2019-05-15 06:47:07 +02:00
$ledger = CompanyLedger::whereClientId($this->payment->client_id)
->whereCompanyId($this->payment->company_id)
->orderBy('id', 'DESC')
->first();
if($ledger)
$balance = $ledger->balance;
2019-05-16 01:40:53 +02:00
$company_ledger = CompanyLedgerFactory::create($this->payment->company_id, $this->payment->user_id);
2019-05-15 06:47:07 +02:00
$company_ledger->client_id = $this->payment->client_id;
2019-05-16 01:40:53 +02:00
$company_ledger->adjustment = $this->adjustment;
2019-05-15 06:47:07 +02:00
$company_ledger->balance = $balance + $this->adjustment;
2019-05-15 07:03:18 +02:00
$company_ledger->save();
2019-05-15 06:47:07 +02:00
$this->payment->company_ledger()->save($company_ledger); //todo add model directive here
}
}