706 lines
30 KiB
PHP
706 lines
30 KiB
PHP
<?php
|
|
|
|
namespace Modules\Crm\Http\Controllers;
|
|
|
|
use App\Category;
|
|
use App\Contact;
|
|
use App\User;
|
|
use App\Utils\ModuleUtil;
|
|
use App\Utils\Util;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\Crm\Entities\CrmContact;
|
|
use Modules\Crm\Utils\CrmUtil;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class LeadController extends Controller
|
|
{
|
|
protected $commonUtil;
|
|
|
|
protected $moduleUtil;
|
|
|
|
protected $crmUtil;
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @param Util $commonUtil
|
|
* @return void
|
|
*/
|
|
public function __construct(Util $commonUtil, ModuleUtil $moduleUtil, CrmUtil $crmUtil)
|
|
{
|
|
$this->commonUtil = $commonUtil;
|
|
$this->moduleUtil = $moduleUtil;
|
|
$this->crmUtil = $crmUtil;
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return Response
|
|
*/
|
|
public function index()
|
|
{
|
|
$business_id = request()->session()->get('user.business_id');
|
|
$can_access_all_leads = auth()->user()->can('crm.access_all_leads');
|
|
$can_access_own_leads = auth()->user()->can('crm.access_own_leads');
|
|
|
|
if (! (auth()->user()->can('superadmin') || $this->moduleUtil->hasThePermissionInSubscription($business_id, 'crm_module')) || ! ($can_access_all_leads || $can_access_own_leads)) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
|
|
$life_stages = Category::forDropdown($business_id, 'life_stage');
|
|
|
|
if (is_null(request()->get('lead_view'))) {
|
|
$lead_view = 'list_view';
|
|
} else {
|
|
$lead_view = request()->get('lead_view');
|
|
}
|
|
|
|
if (request()->ajax()) {
|
|
$leads = $this->crmUtil->getLeadsListQuery($business_id);
|
|
|
|
if (! $can_access_all_leads && $can_access_own_leads) {
|
|
$leads->OnlyOwnLeads();
|
|
}
|
|
|
|
if (! empty(request()->get('source'))) {
|
|
$leads->where('crm_source', request()->get('source'));
|
|
}
|
|
|
|
if (! empty(request()->get('life_stage'))) {
|
|
$leads->where('crm_life_stage', request()->get('life_stage'));
|
|
}
|
|
|
|
if (! empty(request()->get('user_id'))) {
|
|
$user_id = request()->get('user_id');
|
|
$leads->where(function ($query) use ($user_id) {
|
|
$query->whereHas('leadUsers', function ($q) use ($user_id) {
|
|
$q->where('user_id', $user_id);
|
|
});
|
|
});
|
|
}
|
|
|
|
$lead_state = request()->get('lead_state', 'active');
|
|
if ($lead_state === 'active') {
|
|
$leads->where('contacts.crm_is_archived', 0);
|
|
} elseif ($lead_state === 'archived') {
|
|
$leads->where('contacts.crm_is_archived', 1);
|
|
}
|
|
|
|
if (request()->filled('value_min')) {
|
|
$leads->where('contacts.crm_lead_value', '>=', $this->commonUtil->num_uf(request()->get('value_min')));
|
|
}
|
|
|
|
if (request()->filled('value_max')) {
|
|
$leads->where('contacts.crm_lead_value', '<=', $this->commonUtil->num_uf(request()->get('value_max')));
|
|
}
|
|
|
|
if (request()->filled('created_from')) {
|
|
$leads->whereDate('contacts.created_at', '>=', $this->commonUtil->uf_date(request()->get('created_from')));
|
|
}
|
|
|
|
if (request()->filled('created_to')) {
|
|
$leads->whereDate('contacts.created_at', '<=', $this->commonUtil->uf_date(request()->get('created_to')));
|
|
}
|
|
|
|
if ($lead_view == 'list_view') {
|
|
return Datatables::of($leads)
|
|
->addColumn('action', function ($row) {
|
|
$html = '<div class="btn-group">
|
|
<button class="btn btn-info dropdown-toggle btn-xs" type="button" data-toggle="dropdown" aria-expanded="false">
|
|
'.__('messages.action').'
|
|
<span class="caret"></span>
|
|
<span class="sr-only">'
|
|
.__('messages.action').'
|
|
</span>
|
|
</button>
|
|
<ul class="dropdown-menu dropdown-menu-left" role="menu">
|
|
<li>
|
|
<a href="'.action([\Modules\Crm\Http\Controllers\LeadController::class, 'show'], ['lead' => $row->id]).'" class="cursor-pointer view_lead">
|
|
<i class="fa fa-eye"></i>
|
|
'.__('messages.view').'
|
|
</a>
|
|
</li>
|
|
<li>
|
|
<a data-href="'.action([\Modules\Crm\Http\Controllers\LeadController::class, 'edit'], ['lead' => $row->id]).'"class="cursor-pointer edit_lead">
|
|
<i class="fa fa-edit"></i>
|
|
'.__('messages.edit').'
|
|
</a>
|
|
</li>
|
|
<li>
|
|
<a data-href="'.action([\Modules\Crm\Http\Controllers\LeadController::class, 'showConvertForm'], ['id' => $row->id]).'" class="cursor-pointer convert_to_customer">
|
|
<i class="fas fa-redo"></i>
|
|
'.__('crm::lang.convert_to_customer').'
|
|
</a>
|
|
</li>';
|
|
|
|
if (empty($row->crm_is_archived)) {
|
|
$html .= '<li>
|
|
<a data-href="'.action([\Modules\Crm\Http\Controllers\LeadController::class, 'archive'], ['id' => $row->id]).'" class="cursor-pointer archive_lead">
|
|
<i class="fas fa-archive"></i>
|
|
'.__('crm::lang.archive').'
|
|
</a>
|
|
</li>';
|
|
} else {
|
|
$html .= '<li>
|
|
<a data-href="'.action([\Modules\Crm\Http\Controllers\LeadController::class, 'activate'], ['id' => $row->id]).'" class="cursor-pointer activate_lead">
|
|
<i class="fas fa-undo"></i>
|
|
'.__('crm::lang.restore').'
|
|
</a>
|
|
</li>';
|
|
}
|
|
|
|
$html .= '<li>
|
|
<a data-href="'.action([\Modules\Crm\Http\Controllers\LeadController::class, 'destroy'], ['lead' => $row->id]).'" class="cursor-pointer delete_a_lead">
|
|
<i class="fas fa-trash"></i>
|
|
'.__('messages.delete').'
|
|
</a>
|
|
</li>';
|
|
|
|
$html .= '</ul>
|
|
</div>';
|
|
|
|
return $html;
|
|
})
|
|
->addColumn('lead_title', function ($row) {
|
|
$title = $row->display_lead_title;
|
|
$view_url = action([\Modules\Crm\Http\Controllers\LeadController::class, 'show'], ['lead' => $row->id]);
|
|
|
|
return '<a href="'.$view_url.'" class="view_lead">'.$title.'</a>';
|
|
})
|
|
->editColumn('created_at', '@format_date($created_at)')
|
|
->editColumn('crm_source', function ($row) {
|
|
return $row->Source?->name;
|
|
})
|
|
->editColumn('crm_life_stage', function ($row) {
|
|
$label = $row->lifeStage?->name ?? '';
|
|
$archived = ! empty($row->crm_is_archived) ? ' <span class="label label-default"><i class="fas fa-archive"></i> '.__('crm::lang.archived').'</span>' : '';
|
|
|
|
return $label ? '<span class="label label-info">'.$label.'</span>'.$archived : $archived;
|
|
})
|
|
->editColumn('crm_lead_value', function ($row) {
|
|
if ($row->crm_lead_value === null || $row->crm_lead_value === '') {
|
|
return '';
|
|
}
|
|
|
|
return $this->commonUtil->num_f($row->crm_lead_value, true);
|
|
})
|
|
->editColumn('name', '@if(!empty($supplier_business_name)) {{$supplier_business_name}},<br>@endif {{$name}}')
|
|
->editColumn('leadUsers', function ($row) {
|
|
$html = ' ';
|
|
foreach ($row->leadUsers as $leadUser) {
|
|
if (isset($leadUser->media->display_url)) {
|
|
$html .= '<img class="user_avatar" src="'.$leadUser->media->display_url.'" data-toggle="tooltip" title="'.$leadUser->user_full_name.'">';
|
|
} else {
|
|
$html .= '<img class="user_avatar" src="https://ui-avatars.com/api/?name='.$leadUser->first_name.'" data-toggle="tooltip" title="'.$leadUser->user_full_name.'">';
|
|
}
|
|
}
|
|
|
|
return $html;
|
|
})
|
|
->removeColumn('id')
|
|
->rawColumns(['action', 'crm_source', 'crm_life_stage', 'leadUsers', 'created_at', 'name', 'lead_title'])
|
|
->make(true);
|
|
} elseif ($lead_view == 'kanban') {
|
|
$leads = $leads->get()->groupBy('crm_life_stage');
|
|
//sort leads based on life stage
|
|
$crm_leads = [];
|
|
$board_draggable_to = [];
|
|
foreach ($life_stages as $key => $value) {
|
|
$board_draggable_to[] = strval($key);
|
|
if (! isset($leads[$key])) {
|
|
$crm_leads[strval($key)] = [];
|
|
} else {
|
|
$crm_leads[strval($key)] = $leads[$key];
|
|
}
|
|
}
|
|
|
|
$leads_html = [];
|
|
foreach ($crm_leads as $key => $leads) {
|
|
//get all the leads for particular board(life stage)
|
|
$cards = [];
|
|
foreach ($leads as $lead) {
|
|
$edit = action([\Modules\Crm\Http\Controllers\LeadController::class, 'edit'], ['lead' => $lead->id]);
|
|
|
|
$delete = action([\Modules\Crm\Http\Controllers\LeadController::class, 'destroy'], ['lead' => $lead->id]);
|
|
|
|
$view = action([\Modules\Crm\Http\Controllers\LeadController::class, 'show'], ['lead' => $lead->id]);
|
|
$convert = action([\Modules\Crm\Http\Controllers\LeadController::class, 'showConvertForm'], ['id' => $lead->id]);
|
|
|
|
//if member then get their avatar
|
|
if ($lead->leadUsers->count() > 0) {
|
|
$assigned_to = [];
|
|
foreach ($lead->leadUsers as $member) {
|
|
if (isset($member->media->display_url)) {
|
|
$assigned_to[$member->user_full_name] = $member->media->display_url;
|
|
} else {
|
|
$assigned_to[$member->user_full_name] = 'https://ui-avatars.com/api/?name='.$member->first_name;
|
|
}
|
|
}
|
|
}
|
|
|
|
$cards[] = [
|
|
'id' => $lead->id,
|
|
'title' => $lead->display_lead_title,
|
|
'viewUrl' => $view,
|
|
'editUrl' => $edit,
|
|
'editUrlClass' => 'edit_lead',
|
|
'deleteUrl' => $delete,
|
|
'deleteUrlClass' => 'delete_a_lead',
|
|
'convertUrl' => $convert,
|
|
'convertUrlClass' => 'convert_to_customer',
|
|
'assigned_to' => $assigned_to,
|
|
'hasDescription' => ! empty($lead->crm_lead_description),
|
|
'tags' => array_filter([
|
|
$lead->Source->name ?? null,
|
|
! empty($lead->crm_lead_value) ? $this->commonUtil->num_f($lead->crm_lead_value, true) : null,
|
|
]),
|
|
'dragTo' => $board_draggable_to,
|
|
];
|
|
}
|
|
|
|
//get all the card & board title for particular board(life stage)
|
|
$leads_html[] = [
|
|
'id' => strval($key),
|
|
'title' => $life_stages[$key],
|
|
'cards' => $cards,
|
|
];
|
|
}
|
|
|
|
$output = [
|
|
'success' => true,
|
|
'leads_html' => $leads_html,
|
|
'msg' => __('lang_v1.success'),
|
|
];
|
|
|
|
return $output;
|
|
}
|
|
}
|
|
|
|
$sources = Category::forDropdown($business_id, 'source');
|
|
|
|
$users = User::forDropdown($business_id, false, false, false, true);
|
|
|
|
return view('crm::lead.index')
|
|
->with(compact('sources', 'life_stages', 'lead_view', 'users'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return Response
|
|
*/
|
|
public function create()
|
|
{
|
|
$business_id = request()->session()->get('user.business_id');
|
|
if (! (auth()->user()->can('superadmin') || $this->moduleUtil->hasThePermissionInSubscription($business_id, 'crm_module'))) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
|
|
$users = User::forDropdown($business_id, false, false, false, true);
|
|
$sources = Category::forDropdown($business_id, 'source');
|
|
$life_stages = Category::forDropdown($business_id, 'life_stage');
|
|
|
|
$types['lead'] = __('crm::lang.lead');
|
|
$store_action = action([\Modules\Crm\Http\Controllers\LeadController::class, 'store']);
|
|
|
|
$module_form_parts = $this->moduleUtil->getModuleData('contact_form_part');
|
|
|
|
return view('contact.create')
|
|
->with(compact('types', 'store_action', 'sources', 'life_stages', 'users', 'module_form_parts'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param Request $request
|
|
* @return Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$business_id = request()->session()->get('user.business_id');
|
|
if (! (auth()->user()->can('superadmin') || $this->moduleUtil->hasThePermissionInSubscription($business_id, 'crm_module'))) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
|
|
try {
|
|
$input = $request->only(['type', 'prefix', 'first_name', 'middle_name', 'last_name', 'tax_number', 'mobile', 'landline', 'alternate_number', 'city', 'state', 'country', 'landmark', 'contact_id', 'manual_id', 'custom_field1', 'custom_field2', 'custom_field3', 'custom_field4', 'custom_field5', 'custom_field6', 'custom_field7', 'custom_field8', 'custom_field9', 'custom_field10', 'email', 'crm_source', 'crm_life_stage', 'crm_lead_title', 'crm_lead_description', 'dob', 'address_line_1', 'address_line_2', 'zip_code', 'supplier_business_name', 'shipping_custom_field_details']);
|
|
|
|
if ($request->filled('crm_lead_value')) {
|
|
$input['crm_lead_value'] = $this->commonUtil->num_uf($request->input('crm_lead_value'));
|
|
}
|
|
|
|
$input['name'] = implode(' ', [$input['prefix'], $input['first_name'], $input['middle_name'], $input['last_name']]);
|
|
|
|
if (! empty($request->input('is_export'))) {
|
|
$input['is_export'] = true;
|
|
$input['export_custom_field_1'] = $request->input('export_custom_field_1');
|
|
$input['export_custom_field_2'] = $request->input('export_custom_field_2');
|
|
$input['export_custom_field_3'] = $request->input('export_custom_field_3');
|
|
$input['export_custom_field_4'] = $request->input('export_custom_field_4');
|
|
$input['export_custom_field_5'] = $request->input('export_custom_field_5');
|
|
$input['export_custom_field_6'] = $request->input('export_custom_field_6');
|
|
}
|
|
|
|
if (! empty($input['dob'])) {
|
|
$input['dob'] = $this->commonUtil->uf_date($input['dob']);
|
|
}
|
|
|
|
$input['business_id'] = $business_id;
|
|
$input['created_by'] = $request->session()->get('user.id');
|
|
|
|
$assigned_to = $request->input('user_id');
|
|
|
|
$contact = CrmContact::createNewLead($input, $assigned_to);
|
|
|
|
if (! empty($contact)) {
|
|
$this->moduleUtil->getModuleData('after_contact_saved', ['contact' => $contact, 'input' => $request->input()]);
|
|
}
|
|
|
|
$output = ['success' => true,
|
|
'msg' => __('contact.added_success'),
|
|
];
|
|
} catch (\Exception $e) {
|
|
\Log::emergency('File:'.$e->getFile().'Line:'.$e->getLine().'Message:'.$e->getMessage());
|
|
|
|
$output = ['success' => false,
|
|
'msg' => __('messages.something_went_wrong'),
|
|
];
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
|
|
/**
|
|
* Show the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return Response
|
|
*/
|
|
public function show($id)
|
|
{
|
|
$business_id = request()->session()->get('user.business_id');
|
|
$can_access_all_leads = auth()->user()->can('crm.access_all_leads');
|
|
$can_access_own_leads = auth()->user()->can('crm.access_own_leads');
|
|
|
|
if (! (auth()->user()->can('superadmin') || $this->moduleUtil->hasThePermissionInSubscription($business_id, 'crm_module')) || ! ($can_access_all_leads || $can_access_own_leads)) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
|
|
$query = CrmContact::with('leadUsers', 'Source', 'lifeStage')
|
|
->where('business_id', $business_id);
|
|
|
|
if (! $can_access_all_leads && $can_access_own_leads) {
|
|
$query->OnlyOwnLeads();
|
|
}
|
|
$contact = $query->findOrFail($id);
|
|
|
|
$leads = CrmContact::leadsDropdown($business_id, false);
|
|
|
|
$contact_view_tabs = $this->moduleUtil->getModuleData('get_contact_view_tabs');
|
|
|
|
return view('crm::lead.show')
|
|
->with(compact('contact', 'leads', 'contact_view_tabs'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return Response
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$business_id = request()->session()->get('user.business_id');
|
|
$can_access_all_leads = auth()->user()->can('crm.access_all_leads');
|
|
$can_access_own_leads = auth()->user()->can('crm.access_own_leads');
|
|
|
|
if (! (auth()->user()->can('superadmin') || $this->moduleUtil->hasThePermissionInSubscription($business_id, 'crm_module')) || ! ($can_access_all_leads || $can_access_own_leads)) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
|
|
$query = CrmContact::with('leadUsers')
|
|
->where('business_id', $business_id);
|
|
|
|
if (! $can_access_all_leads && $can_access_own_leads) {
|
|
$query->OnlyOwnLeads();
|
|
}
|
|
$contact = $query->findOrFail($id);
|
|
|
|
$users = User::forDropdown($business_id, false);
|
|
$sources = Category::forDropdown($business_id, 'source');
|
|
$life_stages = Category::forDropdown($business_id, 'life_stage');
|
|
|
|
$types['lead'] = __('crm::lang.lead');
|
|
$update_action = action([\Modules\Crm\Http\Controllers\LeadController::class, 'update'], ['lead' => $id]);
|
|
|
|
return view('contact.edit')
|
|
->with(compact('contact', 'types', 'update_action', 'sources', 'life_stages', 'users'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param Request $request
|
|
* @param int $id
|
|
* @return Response
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
$business_id = request()->session()->get('user.business_id');
|
|
$can_access_all_leads = auth()->user()->can('crm.access_all_leads');
|
|
$can_access_own_leads = auth()->user()->can('crm.access_own_leads');
|
|
|
|
if (! (auth()->user()->can('superadmin') || $this->moduleUtil->hasThePermissionInSubscription($business_id, 'crm_module')) || ! ($can_access_all_leads || $can_access_own_leads)) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
|
|
try {
|
|
$input = $request->only(['type', 'prefix', 'first_name', 'middle_name', 'last_name', 'tax_number', 'mobile', 'landline', 'alternate_number', 'city', 'state', 'country', 'landmark', 'contact_id', 'custom_field1', 'custom_field2', 'custom_field3', 'custom_field4', 'custom_field5', 'custom_field6', 'custom_field7', 'custom_field8', 'custom_field9', 'custom_field10', 'email', 'crm_source', 'crm_life_stage', 'crm_lead_title', 'crm_lead_description', 'dob', 'address_line_1', 'address_line_2', 'zip_code', 'supplier_business_name', 'shipping_custom_field_details', 'export_custom_field_1', 'export_custom_field_2', 'export_custom_field_3', 'export_custom_field_4', 'export_custom_field_5', 'export_custom_field_6']);
|
|
|
|
if ($request->filled('crm_lead_value')) {
|
|
$input['crm_lead_value'] = $this->commonUtil->num_uf($request->input('crm_lead_value'));
|
|
} else {
|
|
$input['crm_lead_value'] = null;
|
|
}
|
|
|
|
$input['name'] = implode(' ', [$input['prefix'], $input['first_name'], $input['middle_name'], $input['last_name']]);
|
|
|
|
$input['is_export'] = ! empty($request->input('is_export')) ? 1 : 0;
|
|
|
|
if (! $input['is_export']) {
|
|
unset($input['export_custom_field_1'], $input['export_custom_field_2'], $input['export_custom_field_3'], $input['export_custom_field_4'], $input['export_custom_field_5'], $input['export_custom_field_6']);
|
|
}
|
|
|
|
if (! empty($input['dob'])) {
|
|
$input['dob'] = $this->commonUtil->uf_date($input['dob']);
|
|
}
|
|
|
|
$assigned_to = $request->input('user_id');
|
|
|
|
$contact = CrmContact::updateLead($id, $input, $assigned_to);
|
|
|
|
$output = [
|
|
'success' => true,
|
|
'msg' => __('lang_v1.success'),
|
|
];
|
|
} catch (\Exception $e) {
|
|
\Log::emergency('File:'.$e->getFile().'Line:'.$e->getLine().'Message:'.$e->getMessage());
|
|
|
|
$output = ['success' => false,
|
|
'msg' => __('messages.something_went_wrong'),
|
|
];
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return Response
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$business_id = request()->session()->get('user.business_id');
|
|
$can_access_all_leads = auth()->user()->can('crm.access_all_leads');
|
|
$can_access_own_leads = auth()->user()->can('crm.access_own_leads');
|
|
|
|
if (! (auth()->user()->can('superadmin') || $this->moduleUtil->hasThePermissionInSubscription($business_id, 'crm_module')) || ! ($can_access_all_leads || $can_access_own_leads)) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
|
|
if (request()->ajax()) {
|
|
try {
|
|
$query = CrmContact::where('business_id', $business_id);
|
|
|
|
if (! $can_access_all_leads && $can_access_own_leads) {
|
|
$query->OnlyOwnLeads();
|
|
}
|
|
$contact = $query->findOrFail($id);
|
|
|
|
$contact->delete();
|
|
|
|
$output = [
|
|
'success' => true,
|
|
'msg' => __('lang_v1.success'),
|
|
];
|
|
} catch (\Exception $e) {
|
|
\Log::emergency('File:'.$e->getFile().'Line:'.$e->getLine().'Message:'.$e->getMessage());
|
|
|
|
$output = ['success' => false,
|
|
'msg' => __('messages.something_went_wrong'),
|
|
];
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
}
|
|
|
|
public function showConvertForm($id)
|
|
{
|
|
$business_id = request()->session()->get('user.business_id');
|
|
$can_access_all_leads = auth()->user()->can('crm.access_all_leads');
|
|
$can_access_own_leads = auth()->user()->can('crm.access_own_leads');
|
|
|
|
if (! (auth()->user()->can('superadmin') || $this->moduleUtil->hasThePermissionInSubscription($business_id, 'crm_module')) || ! ($can_access_all_leads || $can_access_own_leads)) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
|
|
$query = CrmContact::where('business_id', $business_id)->where('type', 'lead');
|
|
|
|
if (! $can_access_all_leads && $can_access_own_leads) {
|
|
$query->OnlyOwnLeads();
|
|
}
|
|
|
|
$contact = $query->findOrFail($id);
|
|
|
|
return view('crm::lead.convert_modal')->with(compact('contact'));
|
|
}
|
|
|
|
public function convertToCustomer(Request $request, $id)
|
|
{
|
|
$business_id = request()->session()->get('user.business_id');
|
|
$can_access_all_leads = auth()->user()->can('crm.access_all_leads');
|
|
$can_access_own_leads = auth()->user()->can('crm.access_own_leads');
|
|
|
|
if (! (auth()->user()->can('superadmin') || $this->moduleUtil->hasThePermissionInSubscription($business_id, 'crm_module')) || ! ($can_access_all_leads || $can_access_own_leads)) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
|
|
try {
|
|
$request->validate([
|
|
'first_name' => 'required|string|max:191',
|
|
'last_name' => 'required|string|max:191',
|
|
'supplier_business_name' => 'required|string|max:191',
|
|
'email' => 'required|email|max:191',
|
|
'mobile' => 'nullable|string|max:191',
|
|
'address_line_1' => 'nullable|string|max:191',
|
|
'city' => 'nullable|string|max:191',
|
|
'state' => 'nullable|string|max:191',
|
|
'zip_code' => 'nullable|string|max:191',
|
|
'country' => 'nullable|string|max:191',
|
|
'tax_number' => 'nullable|string|max:191',
|
|
]);
|
|
|
|
$query = CrmContact::where('business_id', $business_id)->where('type', 'lead');
|
|
|
|
if (! $can_access_all_leads && $can_access_own_leads) {
|
|
$query->OnlyOwnLeads();
|
|
}
|
|
|
|
$contact = $query->findOrFail($id);
|
|
|
|
$input = $request->only([
|
|
'first_name', 'last_name', 'supplier_business_name', 'email', 'mobile',
|
|
'address_line_1', 'city', 'state', 'zip_code', 'country', 'tax_number',
|
|
]);
|
|
|
|
$input['name'] = trim(implode(' ', array_filter([
|
|
$contact->prefix,
|
|
$input['first_name'],
|
|
$contact->middle_name,
|
|
$input['last_name'],
|
|
])));
|
|
|
|
$input['type'] = 'customer';
|
|
$input['converted_by'] = auth()->user()->id;
|
|
$input['converted_on'] = \Carbon::now();
|
|
|
|
$contact->update($input);
|
|
|
|
$this->commonUtil->activityLog($contact, 'converted', null, ['update_note' => __('crm::lang.converted_from_leads')]);
|
|
|
|
$output = [
|
|
'success' => true,
|
|
'msg' => __('crm::lang.lead_converted_successfully'),
|
|
'redirect_url' => action([\App\Http\Controllers\ContactController::class, 'show'], [$contact->id]),
|
|
];
|
|
} catch (\Illuminate\Validation\ValidationException $e) {
|
|
throw $e;
|
|
} catch (\Exception $e) {
|
|
\Log::emergency('File:'.$e->getFile().'Line:'.$e->getLine().'Message:'.$e->getMessage());
|
|
|
|
$output = [
|
|
'success' => false,
|
|
'msg' => __('messages.something_went_wrong'),
|
|
];
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
|
|
public function postLifeStage($id)
|
|
{
|
|
$business_id = request()->session()->get('user.business_id');
|
|
if (! (auth()->user()->can('superadmin') || $this->moduleUtil->hasThePermissionInSubscription($business_id, 'crm_module'))) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
|
|
if (request()->ajax()) {
|
|
try {
|
|
$contact = CrmContact::where('business_id', $business_id)->findOrFail($id);
|
|
|
|
$contact->crm_life_stage = request()->input('crm_life_stage');
|
|
$contact->save();
|
|
|
|
$output = [
|
|
'success' => true,
|
|
'msg' => __('lang_v1.success'),
|
|
];
|
|
} catch (\Exception $e) {
|
|
\Log::emergency('File:'.$e->getFile().'Line:'.$e->getLine().'Message:'.$e->getMessage());
|
|
|
|
$output = ['success' => false,
|
|
'msg' => __('messages.something_went_wrong'),
|
|
];
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
}
|
|
|
|
public function archive($id)
|
|
{
|
|
return $this->toggleLeadArchiveState($id, true);
|
|
}
|
|
|
|
public function activate($id)
|
|
{
|
|
return $this->toggleLeadArchiveState($id, false);
|
|
}
|
|
|
|
protected function toggleLeadArchiveState($id, $archived)
|
|
{
|
|
$business_id = request()->session()->get('user.business_id');
|
|
if (! (auth()->user()->can('superadmin') || $this->moduleUtil->hasThePermissionInSubscription($business_id, 'crm_module'))) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
|
|
if (request()->ajax()) {
|
|
try {
|
|
$contact = CrmContact::where('business_id', $business_id)->findOrFail($id);
|
|
$contact->crm_is_archived = $archived ? 1 : 0;
|
|
$contact->save();
|
|
|
|
$output = [
|
|
'success' => true,
|
|
'msg' => __('lang_v1.success'),
|
|
];
|
|
} catch (\Exception $e) {
|
|
\Log::emergency('File:'.$e->getFile().'Line:'.$e->getLine().'Message:'.$e->getMessage());
|
|
|
|
$output = [
|
|
'success' => false,
|
|
'msg' => __('messages.something_went_wrong'),
|
|
];
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
}
|
|
}
|