diff --git a/app/Http/Controllers/V1/Admin/Settings/CompanyCurrencyCheckTransactionsController.php b/app/Http/Controllers/V1/Admin/Settings/CompanyCurrencyCheckTransactionsController.php new file mode 100644 index 000000000..3f7979d57 --- /dev/null +++ b/app/Http/Controllers/V1/Admin/Settings/CompanyCurrencyCheckTransactionsController.php @@ -0,0 +1,33 @@ +header('company')); + + $this->authorize('manage company', $company); + + if ($company->hasTransactions()) { + return response()->json([ + 'success' => false, + ]); + } + + return response()->json([ + 'success' => true, + ]); + } +} diff --git a/app/Http/Controllers/V1/Admin/Settings/UpdateCompanySettingsController.php b/app/Http/Controllers/V1/Admin/Settings/UpdateCompanySettingsController.php index 264b0af70..f2c879c6b 100644 --- a/app/Http/Controllers/V1/Admin/Settings/UpdateCompanySettingsController.php +++ b/app/Http/Controllers/V1/Admin/Settings/UpdateCompanySettingsController.php @@ -17,9 +17,20 @@ class UpdateCompanySettingsController extends Controller */ public function __invoke(UpdateSettingsRequest $request) { - $this->authorize('manage company', Company::find($request->header('company'))); + $company = Company::find($request->header('company')); + $this->authorize('manage company', $company); - CompanySetting::setSettings($request->settings, $request->header('company')); + $companyCurrency = CompanySetting::getSetting('currency', $request->header('company')); + $data = $request->settings; + + if ($companyCurrency !== $data['currency'] && $company->hasTransactions()) { + return response()->json([ + 'success' => false, + 'message' => 'You cannot change currency once transaction is created.' + ]); + } + + CompanySetting::setSettings($data, $request->header('company')); return response()->json([ 'success' => true, diff --git a/app/Http/Controllers/V1/PDF/EstimatePdfController.php b/app/Http/Controllers/V1/PDF/EstimatePdfController.php index 80e96915d..64f65cce5 100644 --- a/app/Http/Controllers/V1/PDF/EstimatePdfController.php +++ b/app/Http/Controllers/V1/PDF/EstimatePdfController.php @@ -4,6 +4,7 @@ use Crater\Http\Controllers\Controller; use Crater\Models\Estimate; +use Illuminate\Http\Request; class EstimatePdfController extends Controller { @@ -13,8 +14,13 @@ class EstimatePdfController extends Controller * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ - public function __invoke(Estimate $estimate) + public function __invoke(Request $request, Estimate $estimate) { + if ($request->has('preview')) { + return $estimate->getPDFData(); + } + + return $estimate->getGeneratedPDFOrStream('estimate'); } } diff --git a/app/Http/Controllers/V1/PDF/InvoicePdfController.php b/app/Http/Controllers/V1/PDF/InvoicePdfController.php index 07a78f45a..6f3c47c98 100644 --- a/app/Http/Controllers/V1/PDF/InvoicePdfController.php +++ b/app/Http/Controllers/V1/PDF/InvoicePdfController.php @@ -4,6 +4,7 @@ use Crater\Http\Controllers\Controller; use Crater\Models\Invoice; +use Illuminate\Http\Request; class InvoicePdfController extends Controller { @@ -13,8 +14,12 @@ class InvoicePdfController extends Controller * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ - public function __invoke(Invoice $invoice) + public function __invoke(Request $request, Invoice $invoice) { + if ($request->has('preview')) { + return $invoice->getPDFData(); + } + return $invoice->getGeneratedPDFOrStream('invoice'); } } diff --git a/app/Http/Controllers/V1/PDF/PaymentPdfController.php b/app/Http/Controllers/V1/PDF/PaymentPdfController.php index c091e7243..fec5fa4bb 100644 --- a/app/Http/Controllers/V1/PDF/PaymentPdfController.php +++ b/app/Http/Controllers/V1/PDF/PaymentPdfController.php @@ -4,6 +4,7 @@ use Crater\Http\Controllers\Controller; use Crater\Models\Payment; +use Illuminate\Http\Request; class PaymentPdfController extends Controller { @@ -13,8 +14,12 @@ class PaymentPdfController extends Controller * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ - public function __invoke(Payment $payment) + public function __invoke(Request $request, Payment $payment) { + if ($request->has('preview')) { + return view('app.pdf.payment.payment'); + } + return $payment->getGeneratedPDFOrStream('payment'); } } diff --git a/app/Models/Company.php b/app/Models/Company.php index 2a3b29084..a0d9fddbb 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -380,4 +380,21 @@ public function checkModelData($model) $model->taxes()->delete(); } } + + public function hasTransactions() + { + if ( + $this->customers()->exists() || + $this->items()->exists() || + $this->invoices()->exists() || + $this->estimates()->exists() || + $this->expenses()->exists() || + $this->payments()->exists() || + $this->recurringInvoices()->exists() + ) { + return true; + } + + return false; + } } diff --git a/app/Models/Estimate.php b/app/Models/Estimate.php index f9aa6eb37..5b7c3b8fd 100644 --- a/app/Models/Estimate.php +++ b/app/Models/Estimate.php @@ -412,6 +412,10 @@ public function getPDFData() 'taxes' => $taxes, ]); + if (request()->has('preview')) { + return view('app.pdf.estimate.'.$estimateTemplate); + } + return PDF::loadView('app.pdf.estimate.'.$estimateTemplate); } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 396fc1166..464390af4 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -575,6 +575,10 @@ public function getPDFData() 'taxes' => $taxes, ]); + if (request()->has('preview')) { + return view('app.pdf.invoice.'.$invoiceTemplate); + } + return PDF::loadView('app.pdf.invoice.'.$invoiceTemplate); } diff --git a/app/Models/Payment.php b/app/Models/Payment.php index f0afd2c69..83a17441d 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -375,6 +375,10 @@ public function getPDFData() 'logo' => $logo ?? null, ]); + if (request()->has('preview')) { + return view('app.pdf.payment.payment'); + } + return PDF::loadView('app.pdf.payment.payment'); } diff --git a/app/Models/RecurringInvoice.php b/app/Models/RecurringInvoice.php index 6666ea5d7..aa3ecabbc 100644 --- a/app/Models/RecurringInvoice.php +++ b/app/Models/RecurringInvoice.php @@ -305,9 +305,15 @@ public function createInvoice() ->setCustomer($this->customer_id) ->setNextNumbers(); + $days = CompanySetting::getSetting('invoice_due_date_days', $this->company_id); + + if (! $days || $days == "null") { + $days = 7; + } + $newInvoice['creator_id'] = $this->creator_id; $newInvoice['invoice_date'] = Carbon::today()->format('Y-m-d'); - $newInvoice['due_date'] = Carbon::today()->addDays(7)->format('Y-m-d'); + $newInvoice['due_date'] = Carbon::today()->addDays($days)->format('Y-m-d'); $newInvoice['status'] = Invoice::STATUS_DRAFT; $newInvoice['company_id'] = $this->company_id; $newInvoice['paid_status'] = Invoice::STATUS_UNPAID; diff --git a/database/migrations/2022_03_03_063237_change_over_due_status_to_sent.php b/database/migrations/2022_03_03_063237_change_over_due_status_to_sent.php new file mode 100644 index 000000000..a87a7351a --- /dev/null +++ b/database/migrations/2022_03_03_063237_change_over_due_status_to_sent.php @@ -0,0 +1,35 @@ +get(); + + if ($overdueInvoices) { + $overdueInvoices->map(function ($overdueInvoice) { + $overdueInvoice->status = Invoice::STATUS_SENT; + $overdueInvoice->overdue = true; + $overdueInvoice->save(); + }); + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/routes/api.php b/routes/api.php index 784760eab..b81cbd79b 100644 --- a/routes/api.php +++ b/routes/api.php @@ -67,6 +67,7 @@ use Crater\Http\Controllers\V1\Admin\Role\AbilitiesController; use Crater\Http\Controllers\V1\Admin\Role\RolesController; use Crater\Http\Controllers\V1\Admin\Settings\CompanyController; +use Crater\Http\Controllers\V1\Admin\Settings\CompanyCurrencyCheckTransactionsController; use Crater\Http\Controllers\V1\Admin\Settings\DiskController; use Crater\Http\Controllers\V1\Admin\Settings\GetCompanyMailConfigurationController; use Crater\Http\Controllers\V1\Admin\Settings\GetCompanySettingsController; @@ -394,6 +395,8 @@ Route::post('/settings', UpdateSettingsController::class); + Route::get('/company/has-transactions', CompanyCurrencyCheckTransactionsController::class); + // Mails //----------------------------------