Change approach for update client balance logic

This commit is contained in:
David Bomba 2025-09-05 13:53:30 +10:00
parent f0af52c017
commit f02c6bb277
2 changed files with 61 additions and 58 deletions

View File

@ -45,28 +45,31 @@ class ClientService
public function calculateBalance(?Invoice $invoice = null)
{
$balance = Invoice::withTrashed()
->where('client_id', $this->client->id)
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
->where('is_deleted', false)
->sum('balance');
$pre_client_balance = $this->client->balance;
// $pre_client_balance = $this->client->balance;
try {
$balance = Invoice::withTrashed()
->where('client_id', $this->client->id)
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
->where('is_deleted', false)
->sum('balance');
DB::connection(config('database.default'))->transaction(function () use ($balance) {
$this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first();
$this->client->balance = $balance;
$this->client->saveQuietly();
}, 2);
} catch (\Throwable $throwable) {
nlog("DB ERROR " . $throwable->getMessage());
}
if ($invoice && floatval($this->client->balance) != floatval($pre_client_balance)) {
$diff = $this->client->balance - $pre_client_balance;
$invoice->ledger()->insertInvoiceBalance($diff, $this->client->balance, "Update Adjustment Invoice # {$invoice->number} => {$diff}");
}
// if ($invoice && floatval($this->client->balance) != floatval($pre_client_balance)) {
// $diff = $this->client->balance - $pre_client_balance;
// $invoice->ledger()->insertInvoiceBalance($diff, $this->client->balance, "Update Adjustment Invoice # {$invoice->number} => {$diff}");
// }
return $this;
}

94
composer.lock generated
View File

@ -1119,25 +1119,25 @@
},
{
"name": "brick/math",
"version": "0.13.1",
"version": "0.14.0",
"source": {
"type": "git",
"url": "https://github.com/brick/math.git",
"reference": "fc7ed316430118cc7836bf45faff18d5dfc8de04"
"reference": "113a8ee2656b882d4c3164fa31aa6e12cbb7aaa2"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/brick/math/zipball/fc7ed316430118cc7836bf45faff18d5dfc8de04",
"reference": "fc7ed316430118cc7836bf45faff18d5dfc8de04",
"url": "https://api.github.com/repos/brick/math/zipball/113a8ee2656b882d4c3164fa31aa6e12cbb7aaa2",
"reference": "113a8ee2656b882d4c3164fa31aa6e12cbb7aaa2",
"shasum": ""
},
"require": {
"php": "^8.1"
"php": "^8.2"
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.2",
"phpunit/phpunit": "^10.1",
"vimeo/psalm": "6.8.8"
"phpstan/phpstan": "2.1.22",
"phpunit/phpunit": "^11.5"
},
"type": "library",
"autoload": {
@ -1167,7 +1167,7 @@
],
"support": {
"issues": "https://github.com/brick/math/issues",
"source": "https://github.com/brick/math/tree/0.13.1"
"source": "https://github.com/brick/math/tree/0.14.0"
},
"funding": [
{
@ -1175,7 +1175,7 @@
"type": "github"
}
],
"time": "2025-03-29T13:50:30+00:00"
"time": "2025-08-29T12:40:03+00:00"
},
{
"name": "btcpayserver/btcpayserver-greenfield-php",
@ -8518,16 +8518,16 @@
},
{
"name": "open-telemetry/api",
"version": "1.4.0",
"version": "1.5.0",
"source": {
"type": "git",
"url": "https://github.com/opentelemetry-php/api.git",
"reference": "b3a9286f9c1c8247c83493c5b1fa475cd0cec7f7"
"reference": "7692075f486c14d8cfd37fba98a08a5667f089e5"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/opentelemetry-php/api/zipball/b3a9286f9c1c8247c83493c5b1fa475cd0cec7f7",
"reference": "b3a9286f9c1c8247c83493c5b1fa475cd0cec7f7",
"url": "https://api.github.com/repos/opentelemetry-php/api/zipball/7692075f486c14d8cfd37fba98a08a5667f089e5",
"reference": "7692075f486c14d8cfd37fba98a08a5667f089e5",
"shasum": ""
},
"require": {
@ -8584,7 +8584,7 @@
"issues": "https://github.com/open-telemetry/opentelemetry-php/issues",
"source": "https://github.com/open-telemetry/opentelemetry-php"
},
"time": "2025-06-19T23:36:51+00:00"
"time": "2025-08-07T23:07:38+00:00"
},
{
"name": "open-telemetry/context",
@ -9521,16 +9521,16 @@
},
{
"name": "phpoffice/phpspreadsheet",
"version": "2.4.0",
"version": "2.4.1",
"source": {
"type": "git",
"url": "https://github.com/PHPOffice/PhpSpreadsheet.git",
"reference": "3a3cad86101a77019eb2fc693aab1a8c11b18b94"
"reference": "096ae6faf94b49b2cf53e92a0073133c941e1f57"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHPOffice/PhpSpreadsheet/zipball/3a3cad86101a77019eb2fc693aab1a8c11b18b94",
"reference": "3a3cad86101a77019eb2fc693aab1a8c11b18b94",
"url": "https://api.github.com/repos/PHPOffice/PhpSpreadsheet/zipball/096ae6faf94b49b2cf53e92a0073133c941e1f57",
"reference": "096ae6faf94b49b2cf53e92a0073133c941e1f57",
"shasum": ""
},
"require": {
@ -9620,9 +9620,9 @@
],
"support": {
"issues": "https://github.com/PHPOffice/PhpSpreadsheet/issues",
"source": "https://github.com/PHPOffice/PhpSpreadsheet/tree/2.4.0"
"source": "https://github.com/PHPOffice/PhpSpreadsheet/tree/2.4.1"
},
"time": "2025-08-10T06:45:13+00:00"
"time": "2025-09-01T18:41:37+00:00"
},
{
"name": "phpoption/phpoption",
@ -10752,20 +10752,20 @@
},
{
"name": "ramsey/uuid",
"version": "4.9.0",
"version": "4.9.1",
"source": {
"type": "git",
"url": "https://github.com/ramsey/uuid.git",
"reference": "4e0e23cc785f0724a0e838279a9eb03f28b092a0"
"reference": "81f941f6f729b1e3ceea61d9d014f8b6c6800440"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/ramsey/uuid/zipball/4e0e23cc785f0724a0e838279a9eb03f28b092a0",
"reference": "4e0e23cc785f0724a0e838279a9eb03f28b092a0",
"url": "https://api.github.com/repos/ramsey/uuid/zipball/81f941f6f729b1e3ceea61d9d014f8b6c6800440",
"reference": "81f941f6f729b1e3ceea61d9d014f8b6c6800440",
"shasum": ""
},
"require": {
"brick/math": "^0.8.8 || ^0.9 || ^0.10 || ^0.11 || ^0.12 || ^0.13",
"brick/math": "^0.8.8 || ^0.9 || ^0.10 || ^0.11 || ^0.12 || ^0.13 || ^0.14",
"php": "^8.0",
"ramsey/collection": "^1.2 || ^2.0"
},
@ -10824,9 +10824,9 @@
],
"support": {
"issues": "https://github.com/ramsey/uuid/issues",
"source": "https://github.com/ramsey/uuid/tree/4.9.0"
"source": "https://github.com/ramsey/uuid/tree/4.9.1"
},
"time": "2025-06-25T14:20:11+00:00"
"time": "2025-09-04T20:59:21+00:00"
},
{
"name": "razorpay/razorpay",
@ -11266,23 +11266,23 @@
},
{
"name": "sentry/sentry-laravel",
"version": "4.15.1",
"version": "4.15.3",
"source": {
"type": "git",
"url": "https://github.com/getsentry/sentry-laravel.git",
"reference": "7e0675e8e06d1ec5cb623792892920000a3aedb5"
"reference": "c3f71a83e8b3a1451e811199d145e864519cecc1"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/getsentry/sentry-laravel/zipball/7e0675e8e06d1ec5cb623792892920000a3aedb5",
"reference": "7e0675e8e06d1ec5cb623792892920000a3aedb5",
"url": "https://api.github.com/repos/getsentry/sentry-laravel/zipball/c3f71a83e8b3a1451e811199d145e864519cecc1",
"reference": "c3f71a83e8b3a1451e811199d145e864519cecc1",
"shasum": ""
},
"require": {
"illuminate/support": "^6.0 | ^7.0 | ^8.0 | ^9.0 | ^10.0 | ^11.0 | ^12.0",
"nyholm/psr7": "^1.0",
"php": "^7.2 | ^8.0",
"sentry/sentry": "^4.14.1",
"sentry/sentry": "^4.15.2",
"symfony/psr-http-message-bridge": "^1.0 | ^2.0 | ^6.0 | ^7.0"
},
"require-dev": {
@ -11339,7 +11339,7 @@
],
"support": {
"issues": "https://github.com/getsentry/sentry-laravel/issues",
"source": "https://github.com/getsentry/sentry-laravel/tree/4.15.1"
"source": "https://github.com/getsentry/sentry-laravel/tree/4.15.3"
},
"funding": [
{
@ -11351,7 +11351,7 @@
"type": "custom"
}
],
"time": "2025-06-24T12:39:03+00:00"
"time": "2025-09-04T14:37:41+00:00"
},
{
"name": "setasign/fpdf",
@ -18084,16 +18084,16 @@
},
{
"name": "friendsofphp/php-cs-fixer",
"version": "v3.87.0",
"version": "v3.87.1",
"source": {
"type": "git",
"url": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer.git",
"reference": "50a13c4c5f25d2c6894e30e92c051474cf0e115a"
"reference": "2f5170365e2a422d0c5421f9c8818b2c078105f6"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHP-CS-Fixer/PHP-CS-Fixer/zipball/50a13c4c5f25d2c6894e30e92c051474cf0e115a",
"reference": "50a13c4c5f25d2c6894e30e92c051474cf0e115a",
"url": "https://api.github.com/repos/PHP-CS-Fixer/PHP-CS-Fixer/zipball/2f5170365e2a422d0c5421f9c8818b2c078105f6",
"reference": "2f5170365e2a422d0c5421f9c8818b2c078105f6",
"shasum": ""
},
"require": {
@ -18126,7 +18126,7 @@
"require-dev": {
"facile-it/paraunit": "^1.3.1 || ^2.7",
"infection/infection": "^0.29.14",
"justinrainbow/json-schema": "^6.4",
"justinrainbow/json-schema": "^6.5",
"keradus/cli-executor": "^2.2",
"mikey179/vfsstream": "^1.6.12",
"php-coveralls/php-coveralls": "^2.8",
@ -18176,7 +18176,7 @@
],
"support": {
"issues": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues",
"source": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/tree/v97773{PHP_CS_FIXER_VERSION}"
"source": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/tree/v3.87.1"
},
"funding": [
{
@ -18184,7 +18184,7 @@
"type": "github"
}
],
"time": "2025-09-02T10:58:35+00:00"
"time": "2025-09-02T15:27:36+00:00"
},
{
"name": "hamcrest/hamcrest-php",
@ -19194,16 +19194,16 @@
},
{
"name": "phpunit/phpunit",
"version": "11.5.35",
"version": "11.5.36",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/phpunit.git",
"reference": "d341ee94ee5007b286fc7907b383aae6b5b3cc91"
"reference": "264a87c7ef68b1ab9af7172357740dc266df5957"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/d341ee94ee5007b286fc7907b383aae6b5b3cc91",
"reference": "d341ee94ee5007b286fc7907b383aae6b5b3cc91",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/264a87c7ef68b1ab9af7172357740dc266df5957",
"reference": "264a87c7ef68b1ab9af7172357740dc266df5957",
"shasum": ""
},
"require": {
@ -19275,7 +19275,7 @@
"support": {
"issues": "https://github.com/sebastianbergmann/phpunit/issues",
"security": "https://github.com/sebastianbergmann/phpunit/security/policy",
"source": "https://github.com/sebastianbergmann/phpunit/tree/11.5.35"
"source": "https://github.com/sebastianbergmann/phpunit/tree/11.5.36"
},
"funding": [
{
@ -19299,7 +19299,7 @@
"type": "tidelift"
}
],
"time": "2025-08-28T05:13:54+00:00"
"time": "2025-09-03T06:24:17+00:00"
},
{
"name": "react/cache",