577 lines
22 KiB
PHP
577 lines
22 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Requests\LambdaRequest;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use App\Exports\InvoicesExport;
|
|
use Carbon\Carbon;
|
|
|
|
class NebMitraController extends Controller
|
|
{
|
|
protected $_base_url = '/nebmitra';
|
|
|
|
public function __construct()
|
|
{
|
|
$this->_viewname = 'nebmitra';
|
|
$this->_title = 'Neb Mitra';
|
|
}
|
|
|
|
/**
|
|
* Handle action routing - maps actions to methods
|
|
* Supports: doctors, doctorsajax, doctorsexport, stockist, stockistajax, stockistexport, abmmapping, abmmappingajax, abmmappingexport
|
|
*/
|
|
public function action($action, Request $request)
|
|
{
|
|
// Handle compound actions like 'doctorsajax' or 'doctorsexport'
|
|
if (preg_match('/^(\w+)(ajax|export)$/i', $action, $matches)) {
|
|
$mainAction = $matches[1];
|
|
$subAction = $matches[2];
|
|
$methodName = $mainAction . ucfirst(strtolower($subAction));
|
|
|
|
// Log for debugging
|
|
Log::debug('NebMitra action routing', [
|
|
'action' => $action,
|
|
'mainAction' => $mainAction,
|
|
'subAction' => $subAction,
|
|
'methodName' => $methodName,
|
|
'methodExists' => method_exists($this, $methodName)
|
|
]);
|
|
|
|
if (method_exists($this, $methodName)) {
|
|
return $this->{$methodName}($request);
|
|
}
|
|
}
|
|
|
|
// Default action handling
|
|
if (method_exists($this, $action)) {
|
|
return $this->{$action}($request);
|
|
}
|
|
|
|
abort(404, 'Action not found: ' . $action);
|
|
}
|
|
|
|
/**
|
|
* Show Doctors list page
|
|
*/
|
|
public function doctors()
|
|
{
|
|
return view('nebmitra.doctors')->with([
|
|
'title' => 'Neb Mitra - Doctors',
|
|
'base_url' => $this->_base_url . '/doctors',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* AJAX endpoint for Doctors DataTable
|
|
*/
|
|
public function doctorsAjax(Request $request)
|
|
{
|
|
$query = DB::table('team3_nebmitra_doctors');
|
|
$user = Auth::user();
|
|
$user_id = $user->id ?? null;
|
|
$all_data_access = $user->all_data_access ?? '0';
|
|
$roles = json_decode($user->roles ?? '[]', true);
|
|
$role = !empty($roles) ? $roles[0] : null;
|
|
|
|
$neb1 = '1655449174-a39a5017-8232-4d58-a253-90d06bc733d0';
|
|
$neb2 = '1655449175-a39a5017-8232-4d58-a253-90d06bc733d0';
|
|
$nebarise = '1655449176-a39a5017-8232-4d58-a253-90d06bc733d0';
|
|
$nebmitraabm = '1655449175-a39a5017-8232-4d58-NEBMITRAABM-90d06bc733d0';
|
|
|
|
$abm = collect();
|
|
if(isset($role) && ($role == $neb1 || $role == $neb2 || $role == $nebarise)){
|
|
$abm = DB::table('team3_nebmitra_to_abm_mapping')
|
|
->select('mitra_user_id', 'mitra_hierarchy_id')
|
|
->where('abm_user_id', $user_id)
|
|
->get();
|
|
}
|
|
|
|
if($all_data_access != '1' && $user_id) {
|
|
$hierarchy = LambdaRequest::getrequest('ums', 'hierarchy/hierarchy_below_me', ['partition' => 'rapport-team3', 'user_id' => $user_id]);
|
|
|
|
$user_ids = [];
|
|
$hierarchy_ids = [];
|
|
|
|
if (isset($hierarchy->data->hierarchies) && is_array($hierarchy->data->hierarchies)) {
|
|
if($role == $nebmitraabm){
|
|
foreach ($hierarchy->data->hierarchies as $hie) {
|
|
if (isset($hie->owner) && !empty($hie->owner)) {
|
|
$user_ids[] = $hie->owner;
|
|
}
|
|
if (isset($hie->id) && !empty($hie->id)) {
|
|
$hierarchy_ids[] = $hie->id;
|
|
}
|
|
}
|
|
} else {
|
|
foreach($abm as $row){
|
|
if (isset($row->mitra_user_id) && !empty($row->mitra_user_id)) {
|
|
$user_ids[] = $row->mitra_user_id;
|
|
}
|
|
if (isset($row->mitra_hierarchy_id) && !empty($row->mitra_hierarchy_id)) {
|
|
$hierarchy_ids[] = $row->mitra_hierarchy_id;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($user_ids) || !empty($hierarchy_ids)) {
|
|
$query->where(function($q) use ($user_ids, $hierarchy_ids) {
|
|
if (!empty($user_ids)) {
|
|
$q->orWhereIn('user_id', $user_ids);
|
|
}
|
|
if (!empty($hierarchy_ids)) {
|
|
$q->orWhereIn('hierarchy_id', $hierarchy_ids);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
// Search
|
|
$search = $request->input('search.value');
|
|
if ($search) {
|
|
$query->where(function($q) use ($search) {
|
|
$q->where('doctor_name', 'like', "%$search%")
|
|
->orWhere('mobile_number', 'like', "%$search%")
|
|
->orWhere('location', 'like', "%$search%")
|
|
->orWhere('state', 'like', "%$search%")
|
|
->orWhere('legacy_code', 'like', "%$search%")
|
|
->orWhere('type', 'like', "%$search%");
|
|
});
|
|
}
|
|
|
|
$total = $query->count();
|
|
|
|
// Paging
|
|
$start = $request->input('start', 0);
|
|
$length = $request->input('length', 10);
|
|
$query->skip($start)->take($length);
|
|
|
|
// Ordering
|
|
if ($request->has('order')) {
|
|
$columns = $request->input('columns');
|
|
foreach ($request->input('order') as $order) {
|
|
$colIdx = $order['column'];
|
|
$dir = $order['dir'];
|
|
$colName = $columns[$colIdx]['data'];
|
|
if($colName != null) {
|
|
$query->orderBy($colName, $dir);
|
|
}
|
|
}
|
|
} else {
|
|
$query->orderBy('id', 'desc');
|
|
}
|
|
|
|
$data = $query->get();
|
|
|
|
return response()->json([
|
|
'draw' => intval($request->input('draw')),
|
|
'recordsTotal' => $total,
|
|
'recordsFiltered' => $total,
|
|
'data' => $data,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Export Doctors to Excel
|
|
*/
|
|
public function doctorsExport(Request $request)
|
|
{
|
|
try {
|
|
$query = DB::table('team3_nebmitra_doctors');
|
|
$user = Auth::user();
|
|
$user_id = $user->id ?? null;
|
|
$all_data_access = $user->all_data_access ?? '0';
|
|
$roles = json_decode($user->roles ?? '[]', true);
|
|
$role = !empty($roles) ? $roles[0] : null;
|
|
|
|
// Apply same filters as ajaxlist
|
|
$neb1 = '1655449174-a39a5017-8232-4d58-a253-90d06bc733d0';
|
|
$neb2 = '1655449175-a39a5017-8232-4d58-a253-90d06bc733d0';
|
|
$nebarise = '1655449176-a39a5017-8232-4d58-a253-90d06bc733d0';
|
|
$nebmitraabm = '1655449175-a39a5017-8232-4d58-NEBMITRAABM-90d06bc733d0';
|
|
|
|
$abm = collect();
|
|
if(isset($role) && ($role == $neb1 || $role == $neb2 || $role == $nebarise)){
|
|
$abm = DB::table('team3_nebmitra_to_abm_mapping')
|
|
->select('mitra_user_id', 'mitra_hierarchy_id')
|
|
->where('abm_user_id', $user_id)
|
|
->get();
|
|
}
|
|
|
|
if($all_data_access != '1' && $user_id) {
|
|
$hierarchy = LambdaRequest::getrequest('ums', 'hierarchy/hierarchy_below_me', ['partition' => 'rapport-team3', 'user_id' => $user_id]);
|
|
|
|
$user_ids = [];
|
|
$hierarchy_ids = [];
|
|
|
|
if (isset($hierarchy->data->hierarchies) && is_array($hierarchy->data->hierarchies)) {
|
|
if($role == $nebmitraabm){
|
|
foreach ($hierarchy->data->hierarchies as $hie) {
|
|
if (isset($hie->owner) && !empty($hie->owner)) {
|
|
$user_ids[] = $hie->owner;
|
|
}
|
|
if (isset($hie->id) && !empty($hie->id)) {
|
|
$hierarchy_ids[] = $hie->id;
|
|
}
|
|
}
|
|
} else {
|
|
foreach($abm as $row){
|
|
if (isset($row->mitra_user_id) && !empty($row->mitra_user_id)) {
|
|
$user_ids[] = $row->mitra_user_id;
|
|
}
|
|
if (isset($row->mitra_hierarchy_id) && !empty($row->mitra_hierarchy_id)) {
|
|
$hierarchy_ids[] = $row->mitra_hierarchy_id;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($user_ids) || !empty($hierarchy_ids)) {
|
|
$query->where(function($q) use ($user_ids, $hierarchy_ids) {
|
|
if (!empty($user_ids)) {
|
|
$q->orWhereIn('user_id', $user_ids);
|
|
}
|
|
if (!empty($hierarchy_ids)) {
|
|
$q->orWhereIn('hierarchy_id', $hierarchy_ids);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
$data = $query->orderBy('id', 'desc')->get();
|
|
|
|
$headers = [
|
|
'ID', 'Legacy Code', 'Doctor Name', 'Mobile Number', 'Location', 'State', 'Type', 'Date Added'
|
|
];
|
|
|
|
$exceldata = [];
|
|
$exceldata[] = $headers;
|
|
|
|
foreach($data as $row) {
|
|
$exceldata[] = [
|
|
$row->id ?? '',
|
|
$row->legacy_code ?? '',
|
|
$row->doctor_name ?? '',
|
|
$row->mobile_number ?? '',
|
|
$row->location ?? '',
|
|
$row->state ?? '',
|
|
$row->type ?? '',
|
|
$row->date_added ? Carbon::parse($row->date_added)->format('d-m-Y') : '',
|
|
];
|
|
}
|
|
|
|
$filename = 'nebmitra_doctors_' . date('Ymd_His') . '.xlsx';
|
|
return Excel::download(new InvoicesExport($exceldata), $filename);
|
|
} catch (\Exception $e) {
|
|
Log::error('NebMitra doctors export error', [
|
|
'message' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString()
|
|
]);
|
|
abort(500, 'Export failed: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show Stockist list page
|
|
*/
|
|
public function stockist()
|
|
{
|
|
return view('nebmitra.stockist')->with([
|
|
'title' => 'Neb Mitra - Stockist',
|
|
'base_url' => $this->_base_url . '/stockist',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* AJAX endpoint for Stockist DataTable
|
|
*/
|
|
public function stockistAjax(Request $request)
|
|
{
|
|
$query = DB::table('team3_nebmitra_stockist');
|
|
$user = Auth::user();
|
|
$user_id = $user->id ?? null;
|
|
$all_data_access = $user->all_data_access ?? '0';
|
|
$roles = json_decode($user->roles ?? '[]', true);
|
|
$role = !empty($roles) ? $roles[0] : null;
|
|
|
|
$neb1 = '1655449174-a39a5017-8232-4d58-a253-90d06bc733d0';
|
|
$neb2 = '1655449175-a39a5017-8232-4d58-a253-90d06bc733d0';
|
|
$nebarise = '1655449176-a39a5017-8232-4d58-a253-90d06bc733d0';
|
|
$nebmitraabm = '1655449175-a39a5017-8232-4d58-NEBMITRAABM-90d06bc733d0';
|
|
|
|
$abm = collect();
|
|
if(isset($role) && ($role == $neb1 || $role == $neb2 || $role == $nebarise)){
|
|
$abm = DB::table('team3_nebmitra_to_abm_mapping')
|
|
->select('mitra_user_id', 'mitra_hierarchy_id')
|
|
->where('abm_user_id', $user_id)
|
|
->get();
|
|
}
|
|
|
|
if($all_data_access != '1' && $user_id) {
|
|
$hierarchy = LambdaRequest::getrequest('ums', 'hierarchy/hierarchy_below_me', ['partition' => 'rapport-team3', 'user_id' => $user_id]);
|
|
|
|
$user_ids = [];
|
|
$hierarchy_ids = [];
|
|
|
|
if (isset($hierarchy->data->hierarchies) && is_array($hierarchy->data->hierarchies)) {
|
|
if($role == $nebmitraabm){
|
|
foreach ($hierarchy->data->hierarchies as $hie) {
|
|
if (isset($hie->owner) && !empty($hie->owner)) {
|
|
$user_ids[] = $hie->owner;
|
|
}
|
|
if (isset($hie->id) && !empty($hie->id)) {
|
|
$hierarchy_ids[] = $hie->id;
|
|
}
|
|
}
|
|
} else {
|
|
foreach($abm as $row){
|
|
if (isset($row->mitra_user_id) && !empty($row->mitra_user_id)) {
|
|
$user_ids[] = $row->mitra_user_id;
|
|
}
|
|
if (isset($row->mitra_hierarchy_id) && !empty($row->mitra_hierarchy_id)) {
|
|
$hierarchy_ids[] = $row->mitra_hierarchy_id;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($user_ids) || !empty($hierarchy_ids)) {
|
|
$query->where(function($q) use ($user_ids, $hierarchy_ids) {
|
|
if (!empty($user_ids)) {
|
|
$q->orWhereIn('user_id', $user_ids);
|
|
}
|
|
if (!empty($hierarchy_ids)) {
|
|
$q->orWhereIn('hierarchy_id', $hierarchy_ids);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
// Search
|
|
$search = $request->input('search.value');
|
|
if ($search) {
|
|
$query->where(function($q) use ($search) {
|
|
$q->where('stockist_name', 'like', "%$search%")
|
|
->orWhere('mobile_number', 'like', "%$search%")
|
|
->orWhere('email_id', 'like', "%$search%")
|
|
->orWhere('location', 'like', "%$search%")
|
|
->orWhere('district', 'like', "%$search%")
|
|
->orWhere('state', 'like', "%$search%")
|
|
->orWhere('pincode', 'like', "%$search%")
|
|
->orWhere('hq', 'like', "%$search%")
|
|
->orWhere('legacy_code', 'like', "%$search%");
|
|
});
|
|
}
|
|
|
|
$total = $query->count();
|
|
|
|
// Paging
|
|
$start = $request->input('start', 0);
|
|
$length = $request->input('length', 10);
|
|
$query->skip($start)->take($length);
|
|
|
|
// Ordering
|
|
if ($request->has('order')) {
|
|
$columns = $request->input('columns');
|
|
foreach ($request->input('order') as $order) {
|
|
$colIdx = $order['column'];
|
|
$dir = $order['dir'];
|
|
$colName = $columns[$colIdx]['data'];
|
|
if($colName != null) {
|
|
$query->orderBy($colName, $dir);
|
|
}
|
|
}
|
|
} else {
|
|
$query->orderBy('id', 'desc');
|
|
}
|
|
|
|
$data = $query->get();
|
|
|
|
return response()->json([
|
|
'draw' => intval($request->input('draw')),
|
|
'recordsTotal' => $total,
|
|
'recordsFiltered' => $total,
|
|
'data' => $data,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Export Stockist to Excel
|
|
*/
|
|
public function stockistExport(Request $request)
|
|
{
|
|
$query = DB::table('team3_nebmitra_stockist');
|
|
$user = Auth::user();
|
|
$user_id = $user->id ?? null;
|
|
$all_data_access = $user->all_data_access ?? '0';
|
|
$roles = json_decode($user->roles ?? '[]', true);
|
|
$role = !empty($roles) ? $roles[0] : null;
|
|
|
|
// Apply same filters as ajaxlist
|
|
$neb1 = '1655449174-a39a5017-8232-4d58-a253-90d06bc733d0';
|
|
$neb2 = '1655449175-a39a5017-8232-4d58-a253-90d06bc733d0';
|
|
$nebarise = '1655449176-a39a5017-8232-4d58-a253-90d06bc733d0';
|
|
$nebmitraabm = '1655449175-a39a5017-8232-4d58-NEBMITRAABM-90d06bc733d0';
|
|
|
|
$abm = collect();
|
|
if(isset($role) && ($role == $neb1 || $role == $neb2 || $role == $nebarise)){
|
|
$abm = DB::table('team3_nebmitra_to_abm_mapping')
|
|
->select('mitra_user_id', 'mitra_hierarchy_id')
|
|
->where('abm_user_id', $user_id)
|
|
->get();
|
|
}
|
|
|
|
if($all_data_access != '1' && $user_id) {
|
|
$hierarchy = LambdaRequest::getrequest('ums', 'hierarchy/hierarchy_below_me', ['partition' => 'rapport-team3', 'user_id' => $user_id]);
|
|
|
|
$user_ids = [];
|
|
$hierarchy_ids = [];
|
|
|
|
if (isset($hierarchy->data->hierarchies) && is_array($hierarchy->data->hierarchies)) {
|
|
if($role == $nebmitraabm){
|
|
foreach ($hierarchy->data->hierarchies as $hie) {
|
|
if (isset($hie->owner) && !empty($hie->owner)) {
|
|
$user_ids[] = $hie->owner;
|
|
}
|
|
if (isset($hie->id) && !empty($hie->id)) {
|
|
$hierarchy_ids[] = $hie->id;
|
|
}
|
|
}
|
|
} else {
|
|
foreach($abm as $row){
|
|
if (isset($row->mitra_user_id) && !empty($row->mitra_user_id)) {
|
|
$user_ids[] = $row->mitra_user_id;
|
|
}
|
|
if (isset($row->mitra_hierarchy_id) && !empty($row->mitra_hierarchy_id)) {
|
|
$hierarchy_ids[] = $row->mitra_hierarchy_id;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($user_ids) || !empty($hierarchy_ids)) {
|
|
$query->where(function($q) use ($user_ids, $hierarchy_ids) {
|
|
if (!empty($user_ids)) {
|
|
$q->orWhereIn('user_id', $user_ids);
|
|
}
|
|
if (!empty($hierarchy_ids)) {
|
|
$q->orWhereIn('hierarchy_id', $hierarchy_ids);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
$data = $query->orderBy('id', 'desc')->get();
|
|
|
|
// Get all columns from the table
|
|
$firstRow = $data->first();
|
|
$headers = $firstRow ? array_keys((array)$firstRow) : [];
|
|
$headers = array_map('ucfirst', array_map(function($h) {
|
|
return str_replace('_', ' ', $h);
|
|
}, $headers));
|
|
|
|
$exceldata = [];
|
|
$exceldata[] = $headers;
|
|
|
|
foreach($data as $row) {
|
|
$exceldata[] = array_values((array)$row);
|
|
}
|
|
|
|
$filename = 'nebmitra_stockist_' . date('Ymd_His') . '.xlsx';
|
|
return Excel::download(new InvoicesExport($exceldata), $filename);
|
|
}
|
|
|
|
/**
|
|
* Show NebMitra to ABM Mapping list page
|
|
*/
|
|
public function abmmapping()
|
|
{
|
|
return view('nebmitra.abm_mapping')->with([
|
|
'title' => 'Neb Mitra to ABM Mapping',
|
|
'base_url' => $this->_base_url . '/abmmapping',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* AJAX endpoint for ABM Mapping DataTable
|
|
*/
|
|
public function abmmappingAjax(Request $request)
|
|
{
|
|
$query = DB::table('team3_nebmitra_to_abm_mapping');
|
|
|
|
// Search
|
|
$search = $request->input('search.value');
|
|
if ($search) {
|
|
$query->where(function($q) use ($search) {
|
|
$q->where('abm_user_id', 'like', "%$search%")
|
|
->orWhere('mitra_user_id', 'like', "%$search%")
|
|
->orWhere('mitra_hierarchy_id', 'like', "%$search%")
|
|
->orWhere('abm_name', 'like', "%$search%")
|
|
->orWhere('mitra_name', 'like', "%$search%");
|
|
});
|
|
}
|
|
|
|
$total = $query->count();
|
|
|
|
// Paging
|
|
$start = $request->input('start', 0);
|
|
$length = $request->input('length', 10);
|
|
$query->skip($start)->take($length);
|
|
|
|
// Ordering
|
|
if ($request->has('order')) {
|
|
$columns = $request->input('columns');
|
|
foreach ($request->input('order') as $order) {
|
|
$colIdx = $order['column'];
|
|
$dir = $order['dir'];
|
|
$colName = $columns[$colIdx]['data'];
|
|
if($colName != null) {
|
|
$query->orderBy($colName, $dir);
|
|
}
|
|
}
|
|
} else {
|
|
$query->orderBy('id', 'desc');
|
|
}
|
|
|
|
$data = $query->get();
|
|
|
|
return response()->json([
|
|
'draw' => intval($request->input('draw')),
|
|
'recordsTotal' => $total,
|
|
'recordsFiltered' => $total,
|
|
'data' => $data,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Export ABM Mapping to Excel
|
|
*/
|
|
public function abmmappingExport(Request $request)
|
|
{
|
|
$data = DB::table('team3_nebmitra_to_abm_mapping')->orderBy('id', 'desc')->get();
|
|
|
|
// Get all columns from the table
|
|
$firstRow = $data->first();
|
|
$headers = $firstRow ? array_keys((array)$firstRow) : [];
|
|
$headers = array_map('ucfirst', array_map(function($h) {
|
|
return str_replace('_', ' ', $h);
|
|
}, $headers));
|
|
|
|
$exceldata = [];
|
|
$exceldata[] = $headers;
|
|
|
|
foreach($data as $row) {
|
|
$exceldata[] = array_values((array)$row);
|
|
}
|
|
|
|
$filename = 'nebmitra_abm_mapping_' . date('Ymd_His') . '.xlsx';
|
|
return Excel::download(new InvoicesExport($exceldata), $filename);
|
|
}
|
|
}
|
|
|