578 lines
21 KiB
PHP
Executable File
578 lines
21 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Models;
|
|
use Session;
|
|
use Illuminate\Support\Arr;
|
|
use App\Jobs\SendNewUssdClientEmail;
|
|
use App\Jobs\SendUssdClientActiveEmail;
|
|
use App\Jobs\SendNewNotesEmailAlert;
|
|
|
|
|
|
class ClientsController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index()
|
|
{
|
|
|
|
$client_arr = new Models\Client;
|
|
$client_arr = $client_arr->with('auth_user_info','country_info', 'created_by_info')->orderBy('name', 'ASC')->paginate(10);
|
|
|
|
$data = [
|
|
'page_title' => 'Clients',
|
|
'client_arr' => $client_arr,
|
|
'current_user' => session('current_user')
|
|
];
|
|
return view('client.index-tabulator', $data);
|
|
}
|
|
public function indexBak(){
|
|
$client_arr = new Models\Client;
|
|
$client_arr = $client_arr->with('auth_user_info','country_info', 'created_by_info')->orderBy('name', 'ASC')->paginate(10);
|
|
$data = [
|
|
'page_title' => 'Clients',
|
|
'current_user' => session('current_user')
|
|
];
|
|
return view('client.index-rawjs', $data);
|
|
}
|
|
public function getClientJson(Request $request)
|
|
{
|
|
/*
|
|
$client_arr = new Models\Client;
|
|
$client_arr = $client_arr->with('auth_user_info','country_info', 'created_by_info', 'modified_by_info')->orderBy('name', 'ASC')->paginate(20);
|
|
|
|
dump($request->all());
|
|
$client_arr = $client_arr->with('auth_user_info','country_info', 'created_by_info', 'modified_by_info')
|
|
->orderBy('name', 'ASC')->paginate(20);
|
|
}
|
|
*/
|
|
$client_arr = \DB::table('clients')
|
|
->join('auth_users AS aumngr', 'aumngr.id', '=', 'clients.auth_user_id')
|
|
->join('auth_users AS aumodify', 'aumodify.id', '=', 'clients.last_modified_by')
|
|
->select('clients.id', 'clients.name', 'clients.status', 'clients.country', 'aumngr.name As accountMgr', 'aumodify.name AS modifiedBy')
|
|
->paginate(15);
|
|
|
|
if($request->has('keyword')){ // != ''
|
|
$keyword = $request->keyword;
|
|
$client_arr = \DB::table('clients')
|
|
->join('auth_users AS aumngr', 'aumngr.id', '=', 'clients.auth_user_id')
|
|
->join('auth_users AS aumodify', 'aumodify.id', '=', 'clients.last_modified_by')
|
|
->select('clients.id','clients.name', 'clients.status', 'clients.country', 'aumngr.name As accountMgr', 'aumodify.name AS modifiedBy')
|
|
->whereRaw("clients.name like '%$keyword%' or clients.status like '%$keyword%' OR clients.country like '%$keyword%' OR aumngr.name like '%$keyword%' OR aumodify.name like '%$keyword%'")
|
|
->paginate(15);
|
|
}
|
|
|
|
return response()->json($client_arr);
|
|
}
|
|
public function getClientJsonRawJs(Request $request){
|
|
#$client_arr = new Models\Client;
|
|
#$client_arr = $client_arr->with('auth_user_info','country_info', 'created_by_info', 'modified_by_info')->orderBy('name', 'ASC')->paginate(20);
|
|
|
|
$client_arr = \DB::table('clients')
|
|
->join('auth_users AS aumngr', 'aumngr.id', '=', 'clients.auth_user_id')
|
|
->join('auth_users AS aumodify', 'aumodify.id', '=', 'clients.last_modified_by')
|
|
->select('clients.id', 'clients.name', 'clients.status', 'clients.country', 'aumngr.name As accountMgr', 'aumodify.name AS modifiedBy')
|
|
->paginate(10);
|
|
|
|
if($request->has('keyword')){ // != ''
|
|
$keyword = $request->keyword;
|
|
$client_arr = \DB::table('clients')
|
|
->join('auth_users AS aumngr', 'aumngr.id', '=', 'clients.auth_user_id')
|
|
->join('auth_users AS aumodify', 'aumodify.id', '=', 'clients.last_modified_by')
|
|
->select('clients.id','clients.name', 'clients.status', 'clients.country', 'aumngr.name As accountMgr', 'aumodify.name AS modifiedBy')
|
|
->whereRaw("clients.name like '%$keyword%' or clients.status like '%$keyword%' OR clients.country like '%$keyword%' OR aumngr.name like '%$keyword%' OR aumodify.name like '%$keyword%'")
|
|
->paginate(10);
|
|
}
|
|
return response()->json($client_arr);
|
|
}
|
|
public function indexOriginal()
|
|
{
|
|
|
|
$client_arr = new Models\Client;
|
|
$table_columns = \DB::select(\DB::raw("show full columns from clients"));
|
|
$exclude_arr = [
|
|
'updated_at', 'id', 'auth_user_id', 'pay_mode', 'type', 'created_at', 'currency'
|
|
];
|
|
$columns = [];
|
|
$queries = [];
|
|
foreach ($table_columns as $key) {
|
|
$columns[$key->Field] = $key->Field;
|
|
}
|
|
foreach ($columns as $col) {
|
|
if (request('filter') == $col) {
|
|
$filter = request('filter');
|
|
$keyword = request('keyword');
|
|
$table_arr = ['staff_id', 'pay_mode'];
|
|
// dump($col);
|
|
// dump(in_array($col, $table_arr));
|
|
if (in_array($col, $table_arr)) {
|
|
$key = $this->get_filter_ids($filter, $keyword);
|
|
if (!empty($key)) {
|
|
$keyword = $key;
|
|
}
|
|
}
|
|
$client_arr = $client_arr->where($col, 'like', '%'.$keyword.'%');
|
|
$queries[$col] = request('keyword');
|
|
}
|
|
}
|
|
|
|
$client_arr = $client_arr->with('payment_type_info', 'service_info', 'auth_user_info', 'created_by_info')->orderBy('name', 'ASC')->paginate(10)->appends($queries);
|
|
$columns = Arr::except($columns, $exclude_arr);
|
|
$columns['pay_mode'] = 'Payment Mode';
|
|
$data = [
|
|
'page_title' => 'Clients',
|
|
'columns' => $columns,
|
|
'client_arr' => $client_arr,
|
|
'current_user' => session('current_user')
|
|
];
|
|
return view('client.index', $data);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
$countries = Models\Country::pluck('en_short_name','alpha_2_code');
|
|
$service_type = Models\Service::pluck('name', 'id');
|
|
$payment_type = Models\PaymentType::pluck('name', 'id');
|
|
$auth_users = Models\Account::pluck('name', 'id');
|
|
$status = ['Live' => 'Live', 'inactive' => 'Inactive', 'Prospective' => 'Prospective'];
|
|
$currency = Models\Currency::pluck('name','name');
|
|
|
|
$data = [
|
|
'page_title' => 'Create Client',
|
|
'countries' => $countries,
|
|
'service_type' => $service_type,
|
|
'status' => $status,
|
|
'currency' => $currency,
|
|
'auth_users' => $auth_users,
|
|
'payment_type' => $payment_type
|
|
];
|
|
|
|
return view('client.create', $data);
|
|
}
|
|
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'name' => 'required|unique:clients,name',
|
|
'email' => 'required|email',
|
|
'services' => 'required',
|
|
'country' => 'required',
|
|
'status' => 'required',
|
|
'payment_mode' => 'required',
|
|
'currency' => 'required',
|
|
'auth_user_id' => 'required', // account manager
|
|
]);
|
|
|
|
|
|
// dd($request->all());
|
|
//'email' => 'unique:users,email_address'
|
|
$client_arr = [
|
|
'name' => $request->name,
|
|
'email' => $request->email,
|
|
'country' => $request->country,
|
|
'status' => $request->status,
|
|
'pay_mode' => $request->payment_mode,
|
|
'currency' => $request->currency,
|
|
'auth_user_id' => $request->auth_user_id, // account manager
|
|
'created_by' => session('current_user.id'),
|
|
'last_modified_by' => session('current_user.id')
|
|
];
|
|
if ($request->notes) {
|
|
$client_arr['notes'] = $request->notes;
|
|
}
|
|
if ($request->skype_name) {
|
|
$client_arr['skype_name'] = $request->skype_name;
|
|
}
|
|
if ($request->linkedin_name) {
|
|
$client_arr['linkedin_name'] = $request->linkedin_name;
|
|
}
|
|
|
|
$result = Models\Client::create($client_arr);
|
|
|
|
if (in_array('3', $request->services)) {
|
|
\Log::info('ussd client detected');
|
|
$ussd_client_payment_arr = [
|
|
'client_id' => $result->id,
|
|
'last_modified_by_id' => session('current_user.id')
|
|
];
|
|
$retval = Models\UssdClientPayment::create($ussd_client_payment_arr);
|
|
//TODO send an email to jim/priscilla about new USSD client,
|
|
#dispatch(new SendNewUssdClientEmail($result));
|
|
}
|
|
$retval = $this->store_services($request->services, $result->id);
|
|
|
|
Session::flash('success_message', 'Client successfully added');
|
|
return redirect(url('clients'));
|
|
}
|
|
public function notesStore(Request $request)
|
|
{
|
|
$request->validate([
|
|
'client_id' => 'required',
|
|
'notes_body' => 'required'
|
|
]);
|
|
$auth_user = session('current_user');
|
|
|
|
// dd($request->all());
|
|
//'email' => 'unique:users,email_address'
|
|
$notes_arr = [
|
|
'notes_body' => $request->notes_body,
|
|
'services' => implode(',', $request->services),
|
|
'auth_user_id' => $auth_user['id'],
|
|
'client_id' => $request->client_id
|
|
];
|
|
if ($request->has('highlight')) {
|
|
$notes_arr['highlight'] = 'YES';
|
|
}
|
|
|
|
|
|
//dd($notes_arr);
|
|
$result = Models\ClientNote::create($notes_arr);
|
|
|
|
$notes = Models\ClientNote::with('client_info', 'created_by_info')->find($result->id);
|
|
//todo : send emails
|
|
//dispatch(new SendNewNotesEmailAlert($notes));
|
|
|
|
if ($result) {
|
|
$data = ['code' => 1, 'msg' => 'Notes successfully added'];
|
|
}
|
|
else{
|
|
$data = ['code' => 3, 'msg' => 'Your request could not be handled at this time'];
|
|
}
|
|
return response()->json($data, 200);
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show($id)
|
|
{
|
|
|
|
$showclient = Models\Client::with('service_info', 'country_info', 'auth_user_info')->find($id);
|
|
$service_type = Models\Service::pluck('name', 'id');
|
|
$service_type_names = Models\Service::pluck('name', 'name');
|
|
$show_services = Models\ClientCategory::where('client_id', $id)->get();
|
|
//->where('highlight', 'NO')
|
|
$show_notes = Models\ClientNote::with('created_by_info', 'client_info')->where('client_id', $id)->orderBy('id', 'DESC')->get()->take(20);
|
|
|
|
|
|
if ($showclient->status == 'Live') {
|
|
$status_bg = "info";
|
|
}
|
|
elseif ($showclient->status == 'Prospective') {
|
|
$status_bg = "warning";
|
|
}
|
|
else{
|
|
$status_bg = "danger";
|
|
}
|
|
|
|
//dd($showclient->sender_ids);
|
|
$data = [
|
|
'page_title' => 'Client Profile',
|
|
'showclient' => $showclient,
|
|
'show_services' => $show_services,
|
|
'service_type' => $service_type,
|
|
'service_type_names' => $service_type_names,
|
|
'show_notes' => $show_notes,
|
|
'status_bg' => $status_bg
|
|
];
|
|
|
|
return view('client.show', $data);
|
|
}
|
|
public function showservices($id)
|
|
{
|
|
$show_services = Models\ClientCategory::where('client_id', $id)->get();
|
|
dd($show_services);
|
|
$data = [
|
|
'page_title' => 'Show Services',
|
|
'show_services' => $show_services
|
|
];
|
|
|
|
return view('client.services', $data);
|
|
}
|
|
public function editservice($id)
|
|
{
|
|
$service = Models\ClientCategory::find($id);
|
|
dd($service);
|
|
$data = [
|
|
'page_title' => 'Show Services',
|
|
'service' => $service
|
|
];
|
|
return view('client.service-edit', $data);
|
|
}
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$client = Models\Client::find($id);
|
|
|
|
$service_type = Models\Service::pluck('name', 'id');
|
|
$countries = Models\Country::pluck('en_short_name','en_short_name');
|
|
|
|
$payment_type = ['Prepaid' => 'Prepaid', 'Postpaid' => 'Postpaid']; // Models\PaymentType::pluck('name', 'id')->toArray();
|
|
$status = ['Live' => 'Live', 'inactive' => 'Inactive', 'Prospective' => 'Prospective'];
|
|
$currency = Models\Currency::pluck('name', 'name'); //
|
|
|
|
$company_types = ['A2P Supplier' => 'A2P Supplier', 'A2P Consumer' => 'A2P Consumer', 'Hybrid' => 'Hybrid'];
|
|
$auth_users = Models\Account::pluck('name', 'id');
|
|
// dd($client->sender_ids);
|
|
if ($client->sender_ids) {
|
|
$sender_ids = json_decode($client->sender_ids, true);
|
|
$sender_ids = array_combine($sender_ids, $sender_ids);
|
|
$old_sender_ids = json_decode($client->sender_ids, true);
|
|
}
|
|
else{
|
|
$sender_ids = ['click' => 'click'];
|
|
$old_sender_ids = [];
|
|
}
|
|
|
|
|
|
if ($client->status == 'Live') {
|
|
$status_bg = "info";
|
|
}
|
|
elseif ($client->status == 'Prospective') {
|
|
$status_bg = "warning";
|
|
}
|
|
else{
|
|
$status_bg = "danger";
|
|
}
|
|
|
|
$contract_types = ['bilateral' => 'bilateral', 'unilateral' => 'unilateral'];
|
|
$connections = ['SMPP' => 'SMPP', 'HTTP' => 'HTTP'];
|
|
|
|
$data = [
|
|
'client' => $client,
|
|
'countries' => $countries,
|
|
'service_type' => $service_type->toArray(),
|
|
'payment_type' => $payment_type,
|
|
'status' => $status,
|
|
'auth_users' => $auth_users,
|
|
'currency' => $currency,
|
|
'page_title' => 'Update Client Details',
|
|
'connections_arr' => $connections,
|
|
'status_bg' => $status_bg,
|
|
'sender_ids' => $sender_ids,
|
|
'company_types' => $company_types,
|
|
'old_sender_ids' => $old_sender_ids,
|
|
'contract_types' => $contract_types
|
|
];
|
|
// dd($service_type->toArray());
|
|
return view('client.edit', $data);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
//Todo : work on the services and the sender IDs
|
|
$request->validate([
|
|
'name' => 'required',
|
|
'email' => 'required|email',
|
|
'country' => 'required',
|
|
'status' => 'required',
|
|
'currency' => 'required',
|
|
'auth_user_id' => 'required'
|
|
]);
|
|
$client_update = Models\Client::find($id);
|
|
|
|
//dump($request->status);
|
|
//dump(($request->sender_ids) ? json_encode($request->sender_ids) : "");
|
|
//dd($request->all());
|
|
|
|
$client_update->name = $request->name;
|
|
$client_update->email = $request->email;
|
|
$client_update->phone = $request->phone ?? "";
|
|
$client_update->contact_person = $request->contact_person;
|
|
$client_update->status = $request->status;
|
|
$client_update->pay_mode = $request->payment_mode;
|
|
$client_update->country = $request->country;
|
|
$client_update->currency = $request->currency;
|
|
$client_update->notes = $request->notes;
|
|
$client_update->skype_name = $request->skype_name;
|
|
$client_update->linkedin_name = $request->linkedin_name;
|
|
|
|
$client_update->company_type = $request->company_type ?? "";
|
|
$client_update->auth_user_id = $request->auth_user_id ?? "";
|
|
$client_update->contract_type = $request->contract_type ?? "";
|
|
$client_update->contract_validity = $request->contract_validity ?? "";
|
|
$client_update->smpp_details = $request->smpp_details ?? "";
|
|
$client_update->sender_ids = ($request->sender_ids) ? json_encode($request->sender_ids) : "";
|
|
$client_update->connections = ($request->connections) ? json_encode($request->connections) : "";
|
|
|
|
$client_update->created_by = session('current_user.id');
|
|
$client_update->last_modified_by = session('current_user.id');
|
|
|
|
$result = $client_update->save();
|
|
|
|
$client = Models\Client::find($id);
|
|
/*
|
|
if (in_array('3', $request->services)) {
|
|
if ($request->status == 'active' && $client_update->status !== 'active') {
|
|
\log::info('ussd client active detected');
|
|
#dispatch(new SendUssdClientActiveEmail($client));
|
|
}
|
|
}
|
|
|
|
if (in_array('3', $request->services)) {
|
|
\Log::info('ussd client detected');
|
|
$ussd_client_payment_arr = [
|
|
'client_id' => $client_update->id,
|
|
'last_modified_by_id' => session('current_user.id')
|
|
];
|
|
$retval = Models\UssdClientPayment::create($ussd_client_payment_arr);
|
|
//TODO send an email to jim/priscilla about new USSD client,
|
|
#dispatch(new SendNewUssdClientEmail($client_update));
|
|
}
|
|
$retval = $this->update_services($request->services, $client_update->id);
|
|
*/
|
|
|
|
|
|
|
|
Session::flash('success_message', 'Client successfully Updated');
|
|
return redirect(url('clients', $id));
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$result = Models\Client::destroy($id);
|
|
if (request()->ajax()) {
|
|
$result_arr = ['code' => 1];
|
|
return response()->json($result_arr);
|
|
}
|
|
Session::flash('success_message', 'Client successfully deleted!');
|
|
return redirect(route('client.index'));
|
|
}
|
|
|
|
public function store_services($services_arr, $client_id){
|
|
foreach ($services_arr as $id) {
|
|
$client_arr = [
|
|
'client_id' => $client_id,
|
|
'category_id' => $id
|
|
];
|
|
$result = Models\ClientCategory::create($client_arr);
|
|
}
|
|
return true;
|
|
}
|
|
public function update_services($services_arr, $client_id){
|
|
// remove and add will think of a better approach later
|
|
$result = Models\ClientCategory::where('client_id', $client_id)->delete();
|
|
|
|
foreach ($services_arr as $id) {
|
|
$client_arr = [
|
|
'client_id' => $client_id,
|
|
'category_id' => $id
|
|
];
|
|
$result = Models\ClientCategory::create($client_arr);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function get_filter_ids($filter, $keyword)
|
|
{
|
|
switch ($filter) {
|
|
case 'status':
|
|
$id = Models\Client::where('status', 'like', "%$keyword%")->get(['id']);
|
|
if ($id->isEmpty()) {
|
|
return '';
|
|
}
|
|
$step = json_decode($id);
|
|
$the_id = $step[0]->id;
|
|
return (count($step) > 0 ) ? $step[0]->id : "";
|
|
break;
|
|
case 'type':
|
|
$id = Models\Client::where('type', 'like', "%$keyword%")->get(['id']);
|
|
if ($id->isEmpty()) {
|
|
return '';
|
|
}
|
|
$step = json_decode($id);
|
|
$the_id = $step[0]->id;
|
|
return (count($step) > 0 ) ? $step[0]->id : "";
|
|
break;
|
|
case 'country':
|
|
$id = Models\Country::where('alpha_2_code', 'like', "%$keyword%")->orWhere('alpha_3_code', 'like', "%$keyword%")->orWhere('en_short_name', 'like', "%$keyword%")->get(['id']);
|
|
if ($id->isEmpty()) {
|
|
return '';
|
|
}
|
|
$step = json_decode($id);
|
|
$the_id = $step[0]->id;
|
|
return (count($step) > 0 ) ? $step[0]->id : "";
|
|
break;
|
|
case 'currency':
|
|
$id = Models\Client::where('currency', 'like', "%$keyword%")->get(['id']);
|
|
if ($id->isEmpty()) {
|
|
return '';
|
|
}
|
|
$step = json_decode($id);
|
|
$the_id = $step[0]->id;
|
|
return (count($step) > 0 ) ? $step[0]->id : "";
|
|
break;
|
|
case 'auth_user_id':
|
|
$id = Models\SystemUser::where('name', 'like', "%$keyword%")->get(['id']);
|
|
if ($id->isEmpty()) {
|
|
return '';
|
|
}
|
|
$step = json_decode($id);
|
|
$the_id = $step[0]->id;
|
|
return (count($step) > 0 ) ? $step[0]->id : "";
|
|
break;
|
|
case 'name':
|
|
// $this->log_query();
|
|
$id = Models\Client::where('name', 'like', "%$keyword%")->get(['id']);
|
|
if ($id->isEmpty()) {
|
|
return '';
|
|
}
|
|
$step = json_decode($id);
|
|
return (count($step) > 0 ) ? $step[0]->id : "";
|
|
break;
|
|
case 'pay_mode':
|
|
// $this->log_query();
|
|
$id = Models\PaymentType::where('name', 'like', "%$keyword%")->get(['id']);
|
|
if ($id->isEmpty()) {
|
|
return '';
|
|
}
|
|
$step = json_decode($id);
|
|
return (count($step) > 0 ) ? $step[0]->id : "";
|
|
break;
|
|
default:
|
|
return '';
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
}
|