2020-02-03 11:33:07 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-02-03 11:33:07 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-02-03 11:33:07 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-02-03 11:33:07 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Invoice;
|
|
|
|
|
|
|
|
use App\Models\Invoice;
|
2020-02-17 10:37:44 +01:00
|
|
|
use App\Services\AbstractService;
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
class UpdateBalance extends AbstractService
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
2021-02-07 14:03:59 +01:00
|
|
|
public $invoice;
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2021-02-07 14:03:59 +01:00
|
|
|
public $balance_adjustment;
|
2020-02-17 10:37:44 +01:00
|
|
|
|
2021-10-25 02:49:11 +02:00
|
|
|
private $is_draft;
|
|
|
|
|
|
|
|
public function __construct($invoice, $balance_adjustment, bool $is_draft)
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
|
|
|
$this->invoice = $invoice;
|
2020-02-17 10:37:44 +01:00
|
|
|
$this->balance_adjustment = $balance_adjustment;
|
2021-10-25 02:49:11 +02:00
|
|
|
$this->is_draft = $is_draft;
|
2020-02-03 11:33:07 +01:00
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function run()
|
|
|
|
{
|
2020-02-03 11:33:07 +01:00
|
|
|
if ($this->invoice->is_deleted) {
|
2021-02-07 14:03:59 +01:00
|
|
|
return $this->invoice;
|
2020-02-03 11:33:07 +01:00
|
|
|
}
|
2022-03-09 05:45:13 +01:00
|
|
|
|
|
|
|
nlog("invoice id = {$this->invoice->id}");
|
|
|
|
nlog("invoice balance = {$this->invoice->balance}");
|
|
|
|
nlog("invoice adjustment = {$this->balance_adjustment}");
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2022-03-09 05:45:13 +01:00
|
|
|
// $this->invoice->balance += floatval($this->balance_adjustment);
|
2021-01-24 07:44:14 +01:00
|
|
|
|
2022-03-09 05:45:13 +01:00
|
|
|
$this->invoice->increment('balance', floatval($this->balance_adjustment));
|
|
|
|
|
2021-10-25 02:49:11 +02:00
|
|
|
if ($this->invoice->balance == 0 && !$this->is_draft) {
|
2020-02-17 10:37:44 +01:00
|
|
|
$this->invoice->status_id = Invoice::STATUS_PAID;
|
2020-02-03 11:33:07 +01:00
|
|
|
}
|
|
|
|
|
2022-03-09 05:45:13 +01:00
|
|
|
nlog("final balance = {$this->invoice->balance}");
|
2021-11-14 04:59:15 +01:00
|
|
|
|
2020-02-03 11:33:07 +01:00
|
|
|
return $this->invoice;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-02-14 04:32:22 +01:00
|
|
|
}
|