200 lines
7.2 KiB
PHP
200 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace Modules\Crm\Http\Controllers;
|
|
|
|
use App\Category;
|
|
use App\Utils\ModuleUtil;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\Crm\Entities\CrmCompany;
|
|
use Modules\Crm\Entities\CrmCompanyBranch;
|
|
use Modules\Crm\Entities\CrmCompanyContact;
|
|
use Modules\Crm\Entities\CrmCompanyPhone;
|
|
use Modules\Crm\Entities\CrmCompanyRequirement;
|
|
use Modules\Crm\Services\CompanyProfileService;
|
|
use Modules\Crm\Utils\TelemarketingUtil;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class CompanyProfileController extends Controller
|
|
{
|
|
protected $moduleUtil;
|
|
|
|
protected $profileService;
|
|
|
|
public function __construct(ModuleUtil $moduleUtil, CompanyProfileService $profileService)
|
|
{
|
|
$this->moduleUtil = $moduleUtil;
|
|
$this->profileService = $profileService;
|
|
}
|
|
|
|
protected function authorizeAccess(): void
|
|
{
|
|
$business_id = request()->session()->get('user.business_id');
|
|
TelemarketingUtil::authorizeTelemarketing($this->moduleUtil, $business_id, 'crm.telemarketing.manage_companies');
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = request()->session()->get('user.business_id');
|
|
|
|
if (request()->ajax()) {
|
|
$query = CrmCompany::where('business_id', $business_id);
|
|
|
|
return Datatables::of($query)
|
|
->addColumn('action', function ($row) {
|
|
$url = action([self::class, 'show'], $row->id);
|
|
|
|
return '<a href="'.$url.'" class="btn btn-xs btn-info"><i class="fa fa-eye"></i></a>';
|
|
})
|
|
->editColumn('profile_completion', function ($row) {
|
|
return $row->profile_completion.'%';
|
|
})
|
|
->rawColumns(['action'])
|
|
->make(true);
|
|
}
|
|
|
|
return view('crm::telemarketing.companies.index');
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = request()->session()->get('user.business_id');
|
|
|
|
$company = CrmCompany::where('business_id', $business_id)
|
|
->with(['contacts', 'branches', 'phones', 'requirements', 'offers.catalog'])
|
|
->findOrFail($id);
|
|
|
|
$companyTypes = CrmCompany::companyTypes();
|
|
$contactRoles = CrmCompanyContact::roles();
|
|
$phoneTypes = CrmCompanyPhone::phoneTypes();
|
|
$requirementTypes = CrmCompanyRequirement::requirementTypes();
|
|
$industries = Category::forDropdown($business_id, 'product');
|
|
|
|
return view('crm::telemarketing.companies.show')->with(compact(
|
|
'company', 'companyTypes', 'contactRoles', 'phoneTypes', 'requirementTypes', 'industries'
|
|
));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = request()->session()->get('user.business_id');
|
|
|
|
$company = CrmCompany::create(array_merge($request->only([
|
|
'legal_name', 'brand_name', 'company_type', 'industry_category_id',
|
|
'employee_count_range', 'website', 'email', 'main_phone',
|
|
'registration_number', 'address', 'city', 'province', 'description',
|
|
]), [
|
|
'business_id' => $business_id,
|
|
'created_by' => auth()->id(),
|
|
]));
|
|
|
|
$this->profileService->calculateCompletion($company);
|
|
|
|
if ($request->ajax()) {
|
|
return response()->json(['success' => true, 'company' => $company]);
|
|
}
|
|
|
|
return redirect()->action([self::class, 'show'], $company->id);
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = request()->session()->get('user.business_id');
|
|
|
|
$company = CrmCompany::where('business_id', $business_id)->findOrFail($id);
|
|
$company->update($request->only([
|
|
'legal_name', 'brand_name', 'company_type', 'industry_category_id',
|
|
'employee_count_range', 'website', 'email', 'main_phone',
|
|
'registration_number', 'address', 'city', 'province', 'description',
|
|
]));
|
|
|
|
$this->profileService->calculateCompletion($company->fresh());
|
|
|
|
$output = ['success' => true, 'msg' => __('lang_v1.updated_success')];
|
|
|
|
if ($request->ajax()) {
|
|
return response()->json($output);
|
|
}
|
|
|
|
return redirect()->back()->with('status', $output);
|
|
}
|
|
|
|
public function storeContact(Request $request, $id)
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = request()->session()->get('user.business_id');
|
|
$company = CrmCompany::where('business_id', $business_id)->findOrFail($id);
|
|
|
|
$company->contacts()->create($request->only([
|
|
'role', 'name', 'mobile', 'phone', 'email', 'extension',
|
|
'is_decision_maker', 'is_potential_partner', 'partner_potential',
|
|
'partner_type', 'influence_level', 'notes',
|
|
]));
|
|
|
|
$this->profileService->calculateCompletion($company->fresh());
|
|
|
|
return redirect()->back()->with('status', ['success' => 1, 'msg' => __('lang_v1.added_success')]);
|
|
}
|
|
|
|
public function storeBranch(Request $request, $id)
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = request()->session()->get('user.business_id');
|
|
$company = CrmCompany::where('business_id', $business_id)->findOrFail($id);
|
|
|
|
$company->branches()->create($request->only([
|
|
'branch_name', 'city', 'address', 'phone', 'manager_name', 'is_headquarters',
|
|
]));
|
|
|
|
$this->profileService->calculateCompletion($company->fresh());
|
|
|
|
return redirect()->back()->with('status', ['success' => 1, 'msg' => __('lang_v1.added_success')]);
|
|
}
|
|
|
|
public function storePhone(Request $request, $id)
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = request()->session()->get('user.business_id');
|
|
$company = CrmCompany::where('business_id', $business_id)->findOrFail($id);
|
|
|
|
$company->phones()->create($request->only(['phone_type', 'number', 'extension', 'notes']));
|
|
|
|
$this->profileService->calculateCompletion($company->fresh());
|
|
|
|
return redirect()->back()->with('status', ['success' => 1, 'msg' => __('lang_v1.added_success')]);
|
|
}
|
|
|
|
public function storeRequirement(Request $request, $id)
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = request()->session()->get('user.business_id');
|
|
$company = CrmCompany::where('business_id', $business_id)->findOrFail($id);
|
|
|
|
$company->requirements()->create($request->only([
|
|
'requirement_type', 'item_name', 'product_id', 'quantity_estimate',
|
|
'frequency', 'current_supplier', 'notes',
|
|
]));
|
|
|
|
$this->profileService->calculateCompletion($company->fresh());
|
|
|
|
return redirect()->back()->with('status', ['success' => 1, 'msg' => __('lang_v1.added_success')]);
|
|
}
|
|
|
|
public function completion($id)
|
|
{
|
|
$this->authorizeAccess();
|
|
$business_id = request()->session()->get('user.business_id');
|
|
$company = CrmCompany::where('business_id', $business_id)->findOrFail($id);
|
|
|
|
return response()->json([
|
|
'completion' => $company->profile_completion,
|
|
'empty_fields' => $this->profileService->getEmptyFields($company),
|
|
'suggested' => $this->profileService->getSuggestedFieldsForCall($company),
|
|
]);
|
|
}
|
|
}
|