diff --git a/app/Http/Controllers/SetupController.php b/app/Http/Controllers/SetupController.php index 2d39f48908cc..87dd6e22398a 100644 --- a/app/Http/Controllers/SetupController.php +++ b/app/Http/Controllers/SetupController.php @@ -16,6 +16,7 @@ use App\Http\Requests\Setup\CheckDatabaseRequest; use App\Http\Requests\Setup\CheckMailRequest; use App\Http\Requests\Setup\StoreSetupRequest; use App\Jobs\Account\CreateAccount; +use App\Jobs\Util\SchedulerCheck; use App\Jobs\Util\VersionCheck; use App\Models\Account; use App\Utils\CurlUtils; @@ -279,10 +280,7 @@ class SetupController extends Controller public function update() { - // if(Ninja::isHosted()) - // return redirect('/'); - - // if( Ninja::isNinja() || !request()->has('secret') || (request()->input('secret') != config('ninja.update_secret')) ) + if(!request()->has('secret') || (request()->input('secret') != config('ninja.update_secret')) ) return redirect('/'); @@ -311,6 +309,8 @@ class SetupController extends Controller $this->buildCache(true); + SchedulerCheck::dispatchNow(); + return redirect('/'); } diff --git a/app/Models/Account.php b/app/Models/Account.php index 8b0c07dbfd0b..54766d341fa9 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -134,6 +134,9 @@ class Account extends BaseModel public function getPlan() { + if(Carbon::parse($this->plan_expires)->lt(now())) + return ''; + return $this->plan ?: ''; } diff --git a/app/Services/Invoice/ApplyPaymentAmount.php b/app/Services/Invoice/ApplyPaymentAmount.php new file mode 100644 index 000000000000..2671b6209977 --- /dev/null +++ b/app/Services/Invoice/ApplyPaymentAmount.php @@ -0,0 +1,128 @@ +invoice = $invoice; + $this->amount = $amount; + } + + public function run() + { + if ($this->invoice->status_id == Invoice::STATUS_DRAFT) { + $this->invoice->service()->markSent(); + } + + /*Don't double pay*/ + if ($this->invoice->statud_id == Invoice::STATUS_PAID) { + return $this->invoice; + } + + if($this->amount == 0) + return $this->invoice; + + /* Create Payment */ + $payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id); + + $payment->amount = $this->amount; + $payment->applied = min($this->amount, $this->invoice->balance); + $payment->number = $this->getNextPaymentNumber($this->invoice->client); + $payment->status_id = Payment::STATUS_COMPLETED; + $payment->client_id = $this->invoice->client_id; + $payment->transaction_reference = ctrans('texts.manual_entry'); + $payment->currency_id = $this->invoice->client->getSetting('currency_id'); + $payment->is_manual = true; + /* Create a payment relationship to the invoice entity */ + $payment->save(); + + $this->setExchangeRate($payment); + + $payment->invoices()->attach($this->invoice->id, [ + 'amount' => $payment->amount, + ]); + + $this->invoice->next_send_date = null; + + $this->invoice->service() + ->setExchangeRate() + ->updateBalance($payment->amount * -1) + ->updatePaidToDate($payment->amount) + ->setCalculatedStatus() + ->applyNumber() + ->deletePdf() + ->save(); + + if ($this->invoice->client->getSetting('client_manual_payment_notification')) + $payment->service()->sendEmail(); + + /* Update Invoice balance */ + event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); + event(new InvoiceWasPaid($this->invoice, $payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); + + $payment->ledger() + ->updatePaymentBalance($payment->amount * -1); + + $this->invoice + ->client + ->service() + ->updateBalance($payment->amount * -1) + ->updatePaidToDate($payment->amount) + ->save(); + + $this->invoice->service()->workFlow()->save(); + + return $this->invoice; + } + + private function setExchangeRate(Payment $payment) + { + + $client_currency = $payment->client->getSetting('currency_id'); + $company_currency = $payment->client->company->settings->currency_id; + + if ($company_currency != $client_currency) { + + $exchange_rate = new CurrencyApi(); + + $payment->exchange_rate = $exchange_rate->exchangeRate($client_currency, $company_currency, Carbon::parse($payment->date)); + //$payment->exchange_currency_id = $client_currency; // 23/06/2021 + $payment->exchange_currency_id = $company_currency; + + $payment->save(); + + } + + } +} diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index 9f7f7bdf6b74..f6df31475ca2 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -22,6 +22,7 @@ use App\Models\Payment; use App\Models\Task; use App\Repositories\BaseRepository; use App\Services\Client\ClientService; +use App\Services\Invoice\ApplyPaymentAmount; use App\Services\Invoice\UpdateReminder; use App\Utils\Ninja; use App\Utils\Traits\MakesHash; @@ -51,6 +52,13 @@ class InvoiceService return $this; } + public function applyPaymentAmount($amount) + { + $this->invoice = (new ApplyPaymentAmount($this->invoice, $amount))->run(); + + return $this; + } + /** * Applies the invoice number. * @return $this InvoiceService object diff --git a/app/Services/Invoice/TriggeredActions.php b/app/Services/Invoice/TriggeredActions.php index 02b13eecfad0..9dca9a50f733 100644 --- a/app/Services/Invoice/TriggeredActions.php +++ b/app/Services/Invoice/TriggeredActions.php @@ -44,6 +44,10 @@ class TriggeredActions extends AbstractService $this->invoice = $this->invoice->service()->markPaid()->save(); } + if ($this->request->has('amount_paid') && is_numeric($this->request->input('amount_paid')) ) { + $this->invoice = $this->invoice->service()->applyPaymentAmount($this->request->input('amount_paid'))->save(); + } + if ($this->request->has('send_email') && $this->request->input('send_email') == 'true') { $this->sendEmail(); } @@ -52,6 +56,7 @@ class TriggeredActions extends AbstractService $this->invoice = $this->invoice->service()->markSent()->save(); } + return $this->invoice; } diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index 583cf4b8147a..70dda9a6edea 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -168,7 +168,12 @@ class HtmlEngine $data['$invoice.discount'] = ['value' => Number::formatMoney($this->entity_calc->getTotalDiscount(), $this->client) ?: ' ', 'label' => ctrans('texts.discount')]; $data['$discount'] = &$data['$invoice.discount']; $data['$subtotal'] = ['value' => Number::formatMoney($this->entity_calc->getSubTotal(), $this->client) ?: ' ', 'label' => ctrans('texts.subtotal')]; - $data['$net_subtotal'] = ['value' => Number::formatMoney(($this->entity_calc->getSubTotal() - $this->entity->total_taxes), $this->client) ?: ' ', 'label' => ctrans('texts.net_subtotal')]; + + if($this->entity->uses_inclusive_taxes) + $data['$net_subtotal'] = ['value' => Number::formatMoney(($this->entity_calc->getSubTotal() - $this->entity->total_taxes), $this->client) ?: ' ', 'label' => ctrans('texts.net_subtotal')]; + else + $data['$net_subtotal'] = ['value' => Number::formatMoney($this->entity_calc->getSubTotal(), $this->client) ?: ' ', 'label' => ctrans('texts.net_subtotal')]; + $data['$invoice.subtotal'] = &$data['$subtotal']; if ($this->entity->partial > 0) { diff --git a/resources/views/index/index.blade.php b/resources/views/index/index.blade.php index 05f6d2559a84..fcaf04fb8eaa 100644 --- a/resources/views/index/index.blade.php +++ b/resources/views/index/index.blade.php @@ -1,27 +1,26 @@
- - + + +