diff --git a/app/Console/Commands/DemoMode.php b/app/Console/Commands/DemoMode.php index 8a5864a6440a..936734e8efe3 100644 --- a/app/Console/Commands/DemoMode.php +++ b/app/Console/Commands/DemoMode.php @@ -132,7 +132,8 @@ class DemoMode extends Command 'enabled_modules' => 32767, 'company_key' => 'KEY', 'enable_shop_api' => true, - 'markdown_email_enabled' => false, + 'markdown_email_enabled' => true, + 'markdown_enabled' => false, ]); $settings = $company->settings; diff --git a/app/Factory/CompanyFactory.php b/app/Factory/CompanyFactory.php index bfb04daf1bc5..c4a4a3466691 100644 --- a/app/Factory/CompanyFactory.php +++ b/app/Factory/CompanyFactory.php @@ -46,7 +46,7 @@ class CompanyFactory $company->enabled_modules = config('ninja.enabled_modules'); //32767;//8191; //4095 $company->default_password_timeout = 1800000; - $company->markdown_email_enabled = false; + $company->markdown_email_enabled = true; $company->markdown_enabled = false; return $company; diff --git a/app/Jobs/Company/CreateCompany.php b/app/Jobs/Company/CreateCompany.php index 6c5c1db6f689..bf176abd1627 100644 --- a/app/Jobs/Company/CreateCompany.php +++ b/app/Jobs/Company/CreateCompany.php @@ -64,7 +64,8 @@ class CreateCompany $company->custom_fields = new \stdClass; $company->default_password_timeout = 1800000; $company->client_registration_fields = ClientRegistrationFields::generate(); - $company->markdown_email_enabled = false; + $company->markdown_email_enabled = true; + $company->markdown_enabled = false; if (Ninja::isHosted()) { $company->subdomain = MultiDB::randomSubdomainGenerator(); diff --git a/app/Services/Client/ClientService.php b/app/Services/Client/ClientService.php index 9fef778216c6..9e9c5fbf9579 100644 --- a/app/Services/Client/ClientService.php +++ b/app/Services/Client/ClientService.php @@ -12,6 +12,7 @@ namespace App\Services\Client; use App\Models\Client; +use App\Models\Credit; use App\Services\Client\Merge; use App\Services\Client\PaymentMethod; use App\Utils\Number; @@ -44,8 +45,9 @@ class ClientService public function updateBalanceAndPaidToDate(float $balance, float $paid_to_date) { // $this->client->balance += $amount; + // $this->client->paid_to_date += $amount; - \DB::connection(config('database.default'))->transaction(function () use($amount) { + \DB::connection(config('database.default'))->transaction(function () use($balance, $paid_to_date) { $this->client = Client::where('id', $this->client->id)->lockForUpdate()->first(); $this->client->balance += $balance; @@ -81,7 +83,7 @@ class ClientService public function getCreditBalance() :float { - $credits = $this->client->credits() + $credits = Credit::where('client_id', $this->client->id) ->where('is_deleted', false) ->where('balance', '>', 0) ->where(function ($query) { @@ -95,7 +97,7 @@ class ClientService public function getCredits() { - return $this->client->credits() + return Credit::where('client_id', $this->client->id) ->where('is_deleted', false) ->where('balance', '>', 0) ->where(function ($query) { diff --git a/app/Services/Invoice/MarkPaid.php b/app/Services/Invoice/MarkPaid.php index 1cfe1b5bdd48..bec391591a4c 100644 --- a/app/Services/Invoice/MarkPaid.php +++ b/app/Services/Invoice/MarkPaid.php @@ -34,6 +34,8 @@ class MarkPaid extends AbstractService private $invoice; + private $payable_balance; + public function __construct(Invoice $invoice) { $this->invoice = $invoice; @@ -51,17 +53,17 @@ class MarkPaid extends AbstractService $this->invoice->service()->markSent()->save(); } - $payable_balance = $this->invoice->balance; - - \DB::connection(config('database.default'))->transaction(function () use($payable_balance) { + \DB::connection(config('database.default'))->transaction(function () { $this->invoice = Invoice::where('id', $this->invoice->id)->lockForUpdate()->first(); + $this->payable_balance = $this->invoice->balance; + $this->invoice ->service() ->setExchangeRate() - ->updateBalance($payable_balance * -1) - ->updatePaidToDate($payable_balance) + ->updateBalance($this->payable_balance * -1) + ->updatePaidToDate($this->payable_balance) ->setStatus(Invoice::STATUS_PAID) ->save(); @@ -70,8 +72,8 @@ class MarkPaid extends AbstractService /* Create Payment */ $payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id); - $payment->amount = $payable_balance; - $payment->applied = $payable_balance; + $payment->amount = $this->payable_balance; + $payment->applied = $this->payable_balance; $payment->status_id = Payment::STATUS_COMPLETED; $payment->client_id = $this->invoice->client_id; $payment->transaction_reference = ctrans('texts.manual_entry'); @@ -99,21 +101,13 @@ class MarkPaid extends AbstractService /* Create a payment relationship to the invoice entity */ $payment->invoices()->attach($this->invoice->id, [ - 'amount' => $payable_balance, + 'amount' => $this->payable_balance, ]); event('eloquent.created: App\Models\Payment', $payment); $this->invoice->next_send_date = null; - // $this->invoice - // ->service() - // ->setExchangeRate() - // ->updateBalance($payment->amount * -1) - // ->updatePaidToDate($payment->amount) - // ->setStatus(Invoice::STATUS_PAID) - // ->save(); - $this->invoice ->service() ->applyNumber() @@ -121,16 +115,18 @@ class MarkPaid extends AbstractService ->save(); $payment->ledger() - ->updatePaymentBalance($payable_balance * -1); + ->updatePaymentBalance($this->payable_balance * -1); - \DB::connection(config('database.default'))->transaction(function () use ($payment) { + // \DB::connection(config('database.default'))->transaction(function () use ($payment) { - /* Get the last record for the client and set the current balance*/ - $client = Client::withTrashed()->where('id', $this->invoice->client_id)->lockForUpdate()->first(); - $client->paid_to_date += $payment->amount; - $client->balance -= $payment->amount; - $client->save(); - }, 1); + // /* Get the last record for the client and set the current balance*/ + // $client = Client::withTrashed()->where('id', $this->invoice->client_id)->lockForUpdate()->first(); + // $client->paid_to_date += $payment->amount; + // $client->balance -= $payment->amount; + // $client->save(); + // }, 1); + + $this->invoice->client->service()->updateBalanceAndPaidToDate($payment->amount, $payment->amount*-1)->save(); $this->invoice = $this->invoice ->service() diff --git a/app/Services/Invoice/MarkSent.php b/app/Services/Invoice/MarkSent.php index d95d7365bd6c..fae67eb6540c 100644 --- a/app/Services/Invoice/MarkSent.php +++ b/app/Services/Invoice/MarkSent.php @@ -63,14 +63,16 @@ class MarkSent extends AbstractService /*Adjust client balance*/ - \DB::connection(config('database.default'))->transaction(function () use ($adjustment) { + // \DB::connection(config('database.default'))->transaction(function () use ($adjustment) { - /* Get the last record for the client and set the current balance*/ - $client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first(); - $client->balance += $adjustment; - $client->save(); + // /* Get the last record for the client and set the current balance*/ + // $client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first(); + // $client->balance += $adjustment; + // $client->save(); - }, 1); + // }, 1); + + $this->invoice->client->service()->updateBalance($adjustment)->save(); $this->invoice->markInvitationsSent(); diff --git a/app/Services/Payment/DeletePayment.php b/app/Services/Payment/DeletePayment.php index 6ce1065e0b7d..a04aa04fe9a9 100644 --- a/app/Services/Payment/DeletePayment.php +++ b/app/Services/Payment/DeletePayment.php @@ -90,9 +90,12 @@ class DeletePayment ->updateInvoiceBalance($net_deletable, "Adjusting invoice {$paymentable_invoice->number} due to deletion of Payment {$this->payment->number}") ->save(); - $client = $client->service() - ->updateBalance($net_deletable) - ->save(); + $client = $this->payment + ->client + ->fresh() + ->service() + ->updateBalance($net_deletable) + ->save(); if ($paymentable_invoice->balance == $paymentable_invoice->amount) { $paymentable_invoice->service()->setStatus(Invoice::STATUS_SENT)->save(); diff --git a/tests/Integration/MarkInvoicePaidTest.php b/tests/Integration/MarkInvoicePaidTest.php index c0921c0b2832..873a87340c78 100644 --- a/tests/Integration/MarkInvoicePaidTest.php +++ b/tests/Integration/MarkInvoicePaidTest.php @@ -57,6 +57,8 @@ class MarkInvoicePaidTest extends TestCase } //events are not firing which makes this impossible to control. +nlog($client_balance); +nlog($invoice_balance); $this->assertEquals(0.00, $invoice->balance); $this->assertEquals(($client_balance - $invoice_balance), $client->balance);