Skip to content

Commit

Permalink
Merge branch 'fix-issue-6.5' into 'master'
Browse files Browse the repository at this point in the history
Fix issue 6.5

See merge request mohit.panjvani/crater-web!1459
  • Loading branch information
mohitpanjwani committed Mar 3, 2022
2 parents adf4b3a + 25c43ab commit bb54edc
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Crater\Http\Controllers\V1\Admin\Settings;

use Crater\Http\Controllers\Controller;
use Crater\Models\Company;
use Illuminate\Http\Request;

class CompanyCurrencyCheckTransactionsController extends Controller
{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function __invoke(Request $request)
{
$company = Company::find($request->header('company'));

$this->authorize('manage company', $company);

if ($company->hasTransactions()) {
return response()->json([
'success' => false,
]);
}

return response()->json([
'success' => true,
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion app/Http/Controllers/V1/PDF/EstimatePdfController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Crater\Http\Controllers\Controller;
use Crater\Models\Estimate;
use Illuminate\Http\Request;

class EstimatePdfController extends Controller
{
Expand All @@ -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');
}
}
7 changes: 6 additions & 1 deletion app/Http/Controllers/V1/PDF/InvoicePdfController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Crater\Http\Controllers\Controller;
use Crater\Models\Invoice;
use Illuminate\Http\Request;

class InvoicePdfController extends Controller
{
Expand All @@ -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');
}
}
7 changes: 6 additions & 1 deletion app/Http/Controllers/V1/PDF/PaymentPdfController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Crater\Http\Controllers\Controller;
use Crater\Models\Payment;
use Illuminate\Http\Request;

class PaymentPdfController extends Controller
{
Expand All @@ -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');
}
}
17 changes: 17 additions & 0 deletions app/Models/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 4 additions & 0 deletions app/Models/Estimate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 4 additions & 0 deletions app/Models/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 4 additions & 0 deletions app/Models/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
8 changes: 7 additions & 1 deletion app/Models/RecurringInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Crater\Models\Invoice;
use Illuminate\Database\Migrations\Migration;

class ChangeOverDueStatusToSent extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$overdueInvoices = Invoice::where('status', 'OVERDUE')->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()
{
//
}
}
3 changes: 3 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -394,6 +395,8 @@

Route::post('/settings', UpdateSettingsController::class);

Route::get('/company/has-transactions', CompanyCurrencyCheckTransactionsController::class);


// Mails
//----------------------------------
Expand Down

0 comments on commit bb54edc

Please sign in to comment.