diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index 84a77c49580b..85b296026d58 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -84,23 +84,23 @@ class PaymentRepository extends BaseRepository { $data['amount'] = array_sum(array_column($data['invoices'], 'amount')); } - // $client->service()->updatePaidToDate($data['amount'])->save(); - $client->paid_to_date += $data['amount']; + $client->service()->updatePaidToDate($data['amount'])->save(); + // $client->paid_to_date += $data['amount']; $client->save(); } else{ //this fixes an edge case with unapplied payments - // $client->service()->updatePaidToDate($data['amount'])->save(); - $client->paid_to_date += $data['amount']; + $client->service()->updatePaidToDate($data['amount'])->save(); + // $client->paid_to_date += $data['amount']; $client->save(); } if (array_key_exists('credits', $data) && is_array($data['credits']) && count($data['credits']) > 0) { $_credit_totals = array_sum(array_column($data['credits'], 'amount')); - // $client->service()->updatePaidToDate($_credit_totals)->save(); - $client->paid_to_date += $_credit_totals; + $client->service()->updatePaidToDate($_credit_totals)->save(); + // $client->paid_to_date += $_credit_totals; $client->save(); } diff --git a/app/Services/Client/ClientService.php b/app/Services/Client/ClientService.php index a5dd781f7d51..9fef778216c6 100644 --- a/app/Services/Client/ClientService.php +++ b/app/Services/Client/ClientService.php @@ -28,14 +28,46 @@ class ClientService public function updateBalance(float $amount) { - $this->client->balance += $amount; + // $this->client->balance += $amount; + + \DB::connection(config('database.default'))->transaction(function () use($amount) { + + $this->client = Client::where('id', $this->client->id)->lockForUpdate()->first(); + $this->client->balance += $amount; + $this->client->save(); + + }, 2); + + return $this; + } + + public function updateBalanceAndPaidToDate(float $balance, float $paid_to_date) + { + // $this->client->balance += $amount; + + \DB::connection(config('database.default'))->transaction(function () use($amount) { + + $this->client = Client::where('id', $this->client->id)->lockForUpdate()->first(); + $this->client->balance += $balance; + $this->client->paid_to_date += $paid_to_date; + $this->client->save(); + + }, 2); return $this; } public function updatePaidToDate(float $amount) { - $this->client->paid_to_date += $amount; + // $this->client->paid_to_date += $amount; + + \DB::connection(config('database.default'))->transaction(function () use($amount) { + + $this->client = Client::where('id', $this->client->id)->lockForUpdate()->first(); + $this->client->paid_to_date += $amount; + $this->client->save(); + + }, 2); return $this; }