165 lines
8.1 KiB
PHP
165 lines
8.1 KiB
PHP
<?php
|
|
|
|
namespace Modules\Business\App\Http\Controllers;
|
|
|
|
use App\Models\Option;
|
|
use App\Models\Business;
|
|
use App\Helpers\HasUploader;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\BusinessCategory;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class SettingController extends Controller
|
|
{
|
|
use HasUploader;
|
|
|
|
public function index()
|
|
{
|
|
$setting = Option::where('key', 'business-settings')
|
|
->where('value', 'LIKE', '%"business_id":%' . auth()->user()->business_id . '%')
|
|
->get()
|
|
->firstWhere('value.business_id', auth()->user()->business_id);
|
|
|
|
$business_categories = BusinessCategory::whereStatus(1)->latest()->get();
|
|
$business = Business::findOrFail(auth()->user()->business_id);
|
|
|
|
return view('business::settings.general', compact('setting', 'business_categories', 'business'));
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$request->validate([
|
|
'address' => 'nullable|max:250',
|
|
'email' => 'nullable|email|max:255',
|
|
'companyName' => 'required|max:250',
|
|
'business_category_id' => 'required|exists:business_categories,id',
|
|
'phoneNumber' => 'nullable|min:5|max:15',
|
|
'vat_name' => 'nullable|max:250',
|
|
'vat_no' => 'nullable|max:250|required_with:vat_name',
|
|
'a4_invoice_logo' => 'nullable|image',
|
|
'thermal_invoice_logo' => 'nullable|image',
|
|
'invoice_scanner_logo' => 'nullable|image',
|
|
'sale_rounding_option' => 'nullable|in:none,round_up,nearest_whole_number,nearest_0.05,nearest_0.1,nearest_0.5',
|
|
'product_profit_option' => 'nullable|in:markup,margin',
|
|
'note' => 'nullable|string|max:250',
|
|
'note_label' => 'nullable|string|max:250',
|
|
'gratitude_message' => 'nullable|string|max:250',
|
|
'show_company_name' => 'nullable|boolean',
|
|
'show_phone_number' => 'nullable|boolean',
|
|
'show_address' => 'nullable|boolean',
|
|
'show_email' => 'nullable|boolean',
|
|
'show_vat' => 'nullable|boolean',
|
|
'show_note' => 'nullable|boolean',
|
|
'show_gratitude_msg' => 'nullable|boolean',
|
|
'show_invoice_scanner_logo' => 'nullable|boolean',
|
|
'show_a4_invoice_logo' => 'nullable|boolean',
|
|
'show_thermal_invoice_logo' => 'nullable|boolean',
|
|
]);
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
$business = Business::findOrFail(auth()->user()->business_id);
|
|
|
|
$business->update([
|
|
'address' => $request->address,
|
|
'companyName' => $request->companyName,
|
|
'business_category_id' => $request->business_category_id,
|
|
'phoneNumber' => $request->phoneNumber,
|
|
'email' => $request->email,
|
|
'vat_name' => $request->vat_name,
|
|
'vat_no' => $request->vat_no,
|
|
]);
|
|
|
|
$moduleKeys = [
|
|
'show_company_name',
|
|
'show_phone_number',
|
|
'show_address',
|
|
'show_email',
|
|
'show_vat',
|
|
];
|
|
|
|
$modules = [];
|
|
foreach ($moduleKeys as $key) {
|
|
$modules[$key] = $request->has($key) ? 1 : 0;
|
|
}
|
|
|
|
$business->update([
|
|
'meta' => $modules
|
|
]);
|
|
|
|
$data = $request->except('_token', '_method', 'logo', 'favicon', 'a4_invoice_logo', 'thermal_invoice_logo', 'invoice_scanner_logo', 'address', 'companyName', 'business_category_id', 'phoneNumber');
|
|
|
|
$setting = Option::find($id);
|
|
|
|
if ($setting) {
|
|
$setting->update($request->except($data) + [
|
|
'value' => $request->except('_token', '_method', 'a4_invoice_logo', 'thermal_invoice_logo', 'invoice_scanner_logo', 'address', 'companyName', 'business_category_id', 'phoneNumber', 'email', 'show_company_name', 'show_phone_number', 'show_address', 'show_email', 'show_vat') + [
|
|
'business_id' => $business->id,
|
|
'a4_invoice_logo' => $request->a4_invoice_logo ? $this->upload($request, 'a4_invoice_logo', $setting->value['a4_invoice_logo'] ?? null) : ($setting->value['a4_invoice_logo'] ?? null),
|
|
'thermal_invoice_logo' => $request->thermal_invoice_logo ? $this->upload($request, 'thermal_invoice_logo', $setting->value['thermal_invoice_logo'] ?? null) : ($setting->value['thermal_invoice_logo'] ?? null),
|
|
'invoice_scanner_logo' => $request->invoice_scanner_logo ? $this->upload($request, 'invoice_scanner_logo', $setting->value['invoice_scanner_logo'] ?? null) : ($setting->value['invoice_scanner_logo'] ?? null),
|
|
'sale_rounding_option' => $request->sale_rounding_option ?? 'none',
|
|
'product_profit_option' => $request->product_profit_option,
|
|
'note' => $request->note,
|
|
'note_label' => $request->note_label,
|
|
'gratitude_message' => $request->gratitude_message,
|
|
'vat_name' => $request->vat_name,
|
|
'vat_no' => $request->vat_no,
|
|
'show_note' => $request->has('show_note') ? 1 : 0,
|
|
'show_gratitude_msg' => $request->has('show_gratitude_msg') ? 1 : 0,
|
|
'show_invoice_scanner_logo' => $request->has('show_invoice_scanner_logo') ? 1 : 0,
|
|
'show_a4_invoice_logo' => $request->has('show_a4_invoice_logo') ? 1 : 0,
|
|
'show_thermal_invoice_logo' => $request->has('show_thermal_invoice_logo') ? 1 : 0,
|
|
'warranty_void_label' => $request->warranty_void_label,
|
|
'show_warranty' => $request->has('show_warranty') ? 1 : 0,
|
|
'warranty_void' => $request->warranty_void,
|
|
],
|
|
]);
|
|
} else {
|
|
Option::insert([
|
|
'key' => 'business-settings',
|
|
'value' => json_encode([
|
|
'business_id' => $business->id,
|
|
'a4_invoice_logo' => $request->a4_invoice_logo ? $this->upload($request, 'a4_invoice_logo') : null,
|
|
'thermal_invoice_logo' => $request->thermal_invoice_logo ? $this->upload($request, 'thermal_invoice_logo') : null,
|
|
'invoice_scanner_logo' => $request->invoice_scanner_logo ? $this->upload($request, 'invoice_scanner_logo') : null,
|
|
'sale_rounding_option' => $request->sale_rounding_option ?? 'none',
|
|
'product_profit_option' => $request->product_profit_option,
|
|
'note' => $request->note,
|
|
'note_label' => $request->note_label,
|
|
'gratitude_message' => $request->gratitude_message,
|
|
'vat_name' => $request->vat_name,
|
|
'vat_no' => $request->vat_no,
|
|
'warranty_void_label' => $request->warranty_void_label,
|
|
'warranty_void' => $request->warranty_void,
|
|
'show_note' => 1,
|
|
'show_gratitude_msg' => 1,
|
|
'show_invoice_scanner_logo' => 1,
|
|
'show_a4_invoice_logo' => 1,
|
|
'show_thermal_invoice_logo' => 1,
|
|
'show_warranty' => 1,
|
|
]),
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
}
|
|
|
|
Cache::forget("business_setting_{$business->id}");
|
|
Cache::forget("business_sale_rounding_{$business->id}");
|
|
|
|
DB::commit();
|
|
|
|
return response()->json([
|
|
'message' => __('Business General Setting updated successfully'),
|
|
'redirect' => route('business.settings.index'),
|
|
]);
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return response()->json(__('Something went wrong.'), 400);
|
|
}
|
|
}
|
|
}
|