380 lines
19 KiB
PHP
380 lines
19 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Requests\LambdaRequest;
|
|
use DB;
|
|
use Log;
|
|
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use App\Exports\InvoicesExport;
|
|
use Auth;
|
|
use App\Mail\ReportExcel;
|
|
use Mail;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class FormController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->_service = 'form';
|
|
$this->_module = 'formaction';
|
|
$this->_viewname = 'formaction';
|
|
$this->_title = 'Quick Actions';
|
|
$this->_base_url = '/formaction';
|
|
}
|
|
|
|
protected function deliverform($formdata, $questions, $id = false) {
|
|
$additional_info = $this->get_additional_info($formdata);
|
|
if($formdata->form_id == '1653655221-724ec5b4-fccb-4a88-bf25-cf894fa27a77'){
|
|
//echo '<pre>'; print_r($questions); exit;
|
|
}
|
|
//echo '<pre>'; print_r($questions); exit;
|
|
$data = ['questions' => $questions, 'form' => $formdata, 'form_id' => $formdata->form_id, 'id' => false, 'id' => $id ];
|
|
if(sizeof($additional_info) > 0) {
|
|
$data['additional'] = $additional_info;
|
|
}
|
|
if (\View::exists('form.'.$formdata->form_id)) {
|
|
return view('form.'.$formdata->form_id)->with($data);
|
|
} else {
|
|
return view('form.common')->with($data);
|
|
}
|
|
}
|
|
|
|
protected function muscle_questions($formdata) {
|
|
switch($formdata->form_id) {
|
|
case '63c2ff17-a69c-4b47-af33-1d9909c608b6':
|
|
case 'eaef2abf-f16b-414c-b6a5-df3fd2282b2e':
|
|
$hierarchy = LambdaRequest::getrequest('ums', 'hierarchy/hierarchy_below_me', ['partition' => $formdata->partition, 'user_id' => $formdata->user_id ]);
|
|
$team = [];
|
|
foreach($hierarchy->data->hierarchies as $hie) {
|
|
if(isset($hie->owner_info->id)) {
|
|
$team[$hie->owner_info->id] = ['name' => $hie->owner_info->name, 'code' => $hie->owner_info->employee_code ];
|
|
}
|
|
}
|
|
return ['team' => $team];
|
|
break;
|
|
|
|
default:
|
|
return [];
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function handle($finfo, Request $request) {
|
|
$request->session()->put('finfo', $finfo);
|
|
$finfo = Cache::remember($finfo, 3600, function () use ($finfo) {
|
|
return LambdaRequest::postrequest('form', 'formentity/getforminfo', ['finfo' => $finfo]);
|
|
});
|
|
// $finfo = LambdaRequest::postrequest('form', 'formentity/getforminfo', ['finfo' => $finfo]);
|
|
// Log::debug(print_r($finfo, true));
|
|
$request->session()->put('formdata', $finfo->data->formdata);
|
|
if($finfo->status == 'success' && $finfo->data->formdata->link_type == 'form') {
|
|
return $this->deliverform($finfo->data->formdata, $finfo->data->questions);
|
|
} elseif($finfo->status == 'success' && $finfo->data->formdata->link_type == 'list') {
|
|
//echo '<pre>'; print_r($finfo->data); exit;
|
|
if (\View::exists('form.list-'.$finfo->data->formdata->form_id)) {
|
|
return view('form.list-'.$finfo->data->formdata->form_id)->with(['questions' => $finfo->data->questions, 'submissions' => $finfo->data->submissions, 'formname' => $finfo->data->formdata->title, 'form_id' => $finfo->data->formdata->form_id ]);
|
|
} else {
|
|
return view('form.list')->with(['questions' => $finfo->data->questions, 'submissions' => $finfo->data->submissions, 'formname' => $finfo->data->formdata->title, 'form_id' => $finfo->data->formdata->form_id ]);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function show(Request $request) {
|
|
$formdata = $request->session()->get('formdata');
|
|
$id = $request->input('id', false);
|
|
$finfo = LambdaRequest::postrequest('form', 'formentity/getformdefination', ['form_id' => $formdata->form_id, 'user_id' => $formdata->user_id, 'partition' => $formdata->partition, 'id' => $id ]);
|
|
$formdata->title = $finfo->data->formdata->title;
|
|
return $this->deliverform($formdata, $finfo->data->questions, $id);
|
|
}
|
|
|
|
public function submit(Request $request) {
|
|
$post = $request->except('_token');
|
|
if($post['id']=='') unset($post['id']);
|
|
$formdata = $request->session()->get('formdata');
|
|
if(isset($formdata) && !empty($formdata) && $formdata->user_id != '') {
|
|
$post = $this->processformdata($formdata->form_id, $post);
|
|
//Log::debug(print_r($post, true));
|
|
$finfo = LambdaRequest::postrequest('form', 'formentity/submitform', ['formdata' => $post, 'entity_id' => $formdata->form_id, 'user_id' => $formdata->user_id, 'partition' => $formdata->partition ]);
|
|
if($formdata->link_type == 'list') {
|
|
return redirect('/form/'.$request->session()->get('finfo') )->withMessage('Successfully Saved !!!');
|
|
} else {
|
|
return redirect('/form/history')->withMessage('Successfully Saved !!!');
|
|
}
|
|
} else {
|
|
echo '<h1>Something looks wrong. Try again!!!</h1>'; exit;
|
|
}
|
|
}
|
|
|
|
public function ajaxsubmit(Request $request) {
|
|
$post = $request->except('_token');
|
|
if(isset($post['id']) && $post['id']=='') unset($post['id']);
|
|
$formdata = $request->session()->get('formdata');
|
|
if(isset($formdata) && !empty($formdata) && $formdata->user_id != '') {
|
|
$post = $this->processformdata($formdata->form_id, $post);
|
|
//Log::debug(print_r($post, true));
|
|
$finfo = LambdaRequest::postrequest('form', 'formentity/submitform', ['formdata' => $post, 'entity_id' => $post['form_id'], 'user_id' => $formdata->user_id, 'partition' => $formdata->partition ]);
|
|
//Log::debug(print_r($finfo, true));
|
|
if($finfo->status == 'success') {
|
|
return response()->json(['status' => 'success']);
|
|
} else {
|
|
return response()->json(['status' => 'failure']);
|
|
}
|
|
} else {
|
|
return response()->json(['status' => 'failure']);
|
|
}
|
|
}
|
|
|
|
public function ajaxsubmitmultiple(Request $request) {
|
|
$post = $request->except('_token');
|
|
$formdata = $request->session()->get('formdata');
|
|
|
|
if(isset($formdata) && !empty($formdata) && $formdata->user_id != '') {
|
|
$post = $this->processformdata($formdata->form_id, $post);
|
|
//echo '<pre>'; print_r($post); exit;
|
|
//Log::debug(print_r($post, true));
|
|
//echo json_encode(['formdata' => $post, 'entity_id' => $post['form_id'], 'user_id' => $formdata->user_id, 'partition' => $formdata->partition ]);
|
|
$finfo = LambdaRequest::postrequest('form', 'formentity/submitmultipleform', ['formdata' => $post, 'entity_id' => $post['form_id'], 'user_id' => $formdata->user_id, 'partition' => $formdata->partition ]);
|
|
//Log::debug(print_r($finfo, true));
|
|
if($finfo->status == 'success') {
|
|
return response()->json(['status' => 'success']);
|
|
} else {
|
|
return response()->json(['status' => 'failure']);
|
|
}
|
|
} else {
|
|
return response()->json(['status' => 'failure']);
|
|
}
|
|
}
|
|
|
|
protected function processformdata($form, $post) {
|
|
switch($form) {
|
|
case '1654748343-81b065ee-e05c-431c-a44d-706afc1c16f4':
|
|
if($post['contact_id'] == 'other') {
|
|
$post['contact_id'] = '';
|
|
}
|
|
break;
|
|
case '1650365556-f06dde85-6138-4dae-9c23-5067e318c522':
|
|
$post[ $post['brand_name'].'-current' ] = $post['current_business_value'];
|
|
$post[ $post['brand_name'].'-potential' ] = $post['potential_business_value'];
|
|
break;
|
|
case '1653655221-724ec5b4-fccb-4a88-bf25-cf894fa27a77':
|
|
$brands = ['Hinex DM' => 'hinex-dm', 'Hinex HP' => 'hinex-hp', 'Hinex ST' => 'hinex-st', 'Hinex ST +' => 'hinex-st+'];
|
|
foreach($post['brand'] as $i => $brand) {
|
|
foreach($brands as $bname => $bkey) {
|
|
if($bname == $brand)
|
|
$post[$bkey][$i] = $post['units'][$i];
|
|
else
|
|
$post[$bkey][$i] = 0;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
|
|
return $post;
|
|
}
|
|
|
|
public function history(Request $request) {
|
|
$formdata = $request->session()->get('formdata');
|
|
//echo '<pre>'; print_r($formdata); exit;
|
|
$history = LambdaRequest::postrequest('form', 'formentity/getformhistory', ['form_id' => $formdata->form_id, 'user_id' => $formdata->user_id, 'partition' => $formdata->partition ]);
|
|
//echo '<pre>'; print_r($history); print_r($formdata); exit;
|
|
return view('form.history')->with(['questions' => $history->data->questions, 'submissions' => $history->data->submissions, 'formname' => $history->data->formname, 'form_id' => $formdata->form_id ]);
|
|
//echo '<pre>'; print_r($history); print_r($formdata); exit;
|
|
}
|
|
|
|
public function edit(Request $request) {
|
|
$formdata = $request->session()->get('formdata');
|
|
$id = $request->input('id', false);
|
|
$finfo = LambdaRequest::postrequest('form', 'formentity/getformdefination', ['form_id' => $formdata->form_id, 'user_id' => $formdata->user_id, 'partition' => $formdata->partition, 'id' => $id ]);
|
|
$formdata->title = $finfo->data->formdata->title;
|
|
if (\View::exists('form.'.$formdata->form_id)) {
|
|
return view('form.'.$formdata->form_id)->with(['questions' => $finfo->data->questions, 'form' => $formdata, 'id' => $id ]);
|
|
} else {
|
|
return view('form.common')->with(['questions' => $finfo->data->questions, 'form' => $formdata, 'id' => $id ]);
|
|
}
|
|
}
|
|
|
|
protected function get_additional_info($formdata) {
|
|
switch($formdata->form_id) {
|
|
case '63c2ff17-a69c-4b47-af33-1d9909c608b6':
|
|
case 'eaef2abf-f16b-414c-b6a5-df3fd2282b2e':
|
|
$hierarchy = LambdaRequest::getrequest('ums', 'hierarchy/hierarchy_below_me', ['partition' => $formdata->partition, 'user_id' => $formdata->user_id ]);
|
|
$team = [];
|
|
foreach($hierarchy->data->hierarchies as $hie) {
|
|
if(isset($hie->owner_info->id)) {
|
|
$team[$hie->owner_info->id] = ['name' => $hie->owner_info->name, 'code' => $hie->owner_info->employee_code ];
|
|
}
|
|
}
|
|
return ['team' => $team];
|
|
break;
|
|
case '1653655221-724ec5b4-fccb-4a88-bf25-cf894fa27a77':
|
|
|
|
return [];
|
|
break;
|
|
|
|
default:
|
|
return [];
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function getformfielddata(Request $request) {
|
|
$post = $request->except('_token');
|
|
$history = LambdaRequest::postrequest('form', 'formentity/getformhistory', ['form_id' => $post['form_id'], 'user_id' => $post['user_id'], 'partition' => $post['partition'] ]);
|
|
$submission = [];
|
|
foreach($history->data->submissions as $sub) {
|
|
if($sub->formdata->record_date == strtotime($post['record_date']) ) {
|
|
$submission = (array)$sub->formdata;
|
|
$submission['submission_id'] = $sub->id;
|
|
}
|
|
}
|
|
|
|
return response()->json(['submission' => $submission, 'submissions' => $history->data->submissions, 'record_date' => strtotime($post['record_date']) ]);
|
|
}
|
|
|
|
public function metricreport(Request $request) {
|
|
$post = $request->input();
|
|
$token = $request->header('Authorization');
|
|
if($token != '') {
|
|
$request->session()->put('metric_access_key', $token);
|
|
} else {
|
|
$token = $request->session()->get('metric_access_key');
|
|
}
|
|
$download = (isset($post['download']) && $post['download'] == 1) ? 1 : 0;
|
|
$email = (isset($post['email']) && $post['email'] == 1) ? 1 : 0;
|
|
unset($post['download']);
|
|
unset($post['email']);
|
|
|
|
$response = LambdaRequest::postrequest('metric', 'metric_report/view', $post, $token);
|
|
//Log::debug(print_r($response, true));
|
|
//echo '<pre>'; print_r($response); print_r($post); exit;
|
|
|
|
if($download) {
|
|
$exceldata = [];
|
|
$exceldata[] = [$response->data->graph->name];
|
|
$exceldata[] = ['Report: '.ucwords($response->data->graph->agg_type).' Report'];
|
|
$exceldata[] = ['Type: '.ucwords($response->data->graph->type).' Metric'];
|
|
foreach($response->data->periods as $v) {
|
|
if($post['period'] == $v->value) {
|
|
$exceldata[] = ['Period: '.ucwords($v->label)];
|
|
}
|
|
}
|
|
$exceldata[] = [''];
|
|
switch($response->data->post->type) {
|
|
case 'leaderboard':
|
|
$exceldata[] = ["Rank", "User", "Designation", "Units", "Percent"];
|
|
foreach($response->data->graph->values as $val) {
|
|
$exceldata[] = [(string)$val->rank, (string)$val->user->name, $val->user->designation, (string)round($val->score, 2), (string)$response->data->graph->values[0]->rank != 0 ? ($response->data->graph->values[0]->rank == 1 ? ($response->data->graph->values[0]->score ? ceil(($val->score/$response->data->graph->values[0]->score)*100) : 0) : ($response->data->graph->values[1]->score ? ceil(($val->score/$response->data->graph->values[1]->score)*100) : 0 ) ) : 0 ];
|
|
}
|
|
break;
|
|
case 'trend':
|
|
$headings = ['Periods'];
|
|
if($response->data->graph->type == 'grouped') {
|
|
foreach($response->data->graph->values as $met) {
|
|
$headings[] = $met->metric;
|
|
}
|
|
$exceldata[] = $headings;
|
|
foreach($response->data->graph->values[0]->stats as $i => $stat) {
|
|
$row = [$stat->x];
|
|
foreach($response->data->graph->values as $met) {
|
|
$row[] = isset($met->stats[$i])?$met->stats[$i]->y:0;
|
|
}
|
|
$exceldata[] = $row;
|
|
}
|
|
} else {
|
|
$headings[] = $response->data->graph->label;
|
|
$exceldata[] = $headings;
|
|
foreach($response->data->graph->values as $i => $val) {
|
|
$exceldata[] = [$val->x, $val->y];
|
|
}
|
|
}
|
|
break;
|
|
case 'graphical':
|
|
if($response->data->graph->type == 'grouped') {
|
|
$exceldata[] = ['Metric Name', 'Units', 'Percent'];
|
|
$total = 0;
|
|
foreach($response->data->graph->values as $val) {
|
|
$total = $total + $val->value;
|
|
}
|
|
foreach($response->data->graph->values as $val) {
|
|
$exceldata[] = [$val->label, $val->value, (string)round(($val->value/$total)*100, 2)];
|
|
}
|
|
}
|
|
break;
|
|
case 'details':
|
|
if($response->data->graph->chart == 'tabular') {
|
|
$headers = ['User', 'Employee Code', 'Designation', 'Zone', 'Region', "Area", "Territory"];
|
|
foreach($response->data->graph->values->metrics as $metric) {
|
|
$headers[] = $metric->metric;
|
|
}
|
|
$exceldata[] = $headers;
|
|
foreach($response->data->graph->values->users as $user) {
|
|
$row = [];
|
|
$row[] = $user->name;
|
|
$row[] = $user->employee_code;
|
|
$row[] = $user->designation;
|
|
$row[] = isset($user->positions->Zone) ? $user->positions->Zone : '-';
|
|
$row[] = isset($user->positions->Region) ? $user->positions->Region : '-';
|
|
$row[] = isset($user->positions->Area) ? $user->positions->Area : '-';
|
|
$row[] = isset($user->positions->Territory) ? $user->positions->Territory : '-';
|
|
foreach($response->data->graph->values->metrics as $metric) {
|
|
$row[] = isset($metric->stats->{$user->id}) ? $metric->stats->{$user->id} : '-';
|
|
}
|
|
$exceldata[] = $row;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
//echo '<pre>'; print_r($exceldata); exit;
|
|
$data = new \StdClass;
|
|
$data->sender_name = 'Rapport Support';
|
|
$data->sender_email = 'support@stratawiz.com';
|
|
$data->subject = 'Attached Report';
|
|
$data->body = '';
|
|
|
|
$report = Excel::raw(new InvoicesExport($exceldata), 'Xlsx');
|
|
$data->attachment = $report;
|
|
$data->attachment_name = 'report.xlsx';
|
|
|
|
//Mail::to( str_replace(Auth::user()->domain.'-','', Auth::user()->email) )->send(new ReportExcel($data));
|
|
//Mail::to( 'ravigulhane1983@gmail.com' )->send(new ReportExcel($data));
|
|
Mail::to( $response->data->user->email )->send(new ReportExcel($data));
|
|
return response()->json(['sent' => 'yes']);
|
|
} else {
|
|
$colors = ['FECD00', '01C14B', 'FF8050', '4A5CD0', '707070'];
|
|
return view('metricreport.appreport')->with(['periods' => $response->data->periods, 'charts' => $response->data->available_charts, 'graph' => $response->data->graph, 'post' => (array)$response->data->post, 'colors' => $colors]);
|
|
}
|
|
}
|
|
|
|
public function metriclist(Request $request) {
|
|
$post = $request->input();
|
|
$token = $request->header('Authorization');
|
|
if($token != '') {
|
|
$request->session()->put('metric_access_key', $token);
|
|
} else {
|
|
$token = $request->session()->get('metric_access_key');
|
|
}
|
|
$reports = LambdaRequest::getrequest('metric', 'metric_report/list', $post, $token);
|
|
//echo '<pre>'; print_r($reports); exit;
|
|
//$reports = $this->getlambda('get', 'metric', 'metric_report', 'list');
|
|
return view('metricreport.appreportlist')->with(['metrics' => $reports->data->metrics]);
|
|
|
|
}
|
|
|
|
public function preview(Request $request) {
|
|
$data = $request->input('data');
|
|
$finfo = LambdaRequest::postrequest('form', 'formentity/preview', ['data' => $data]);
|
|
//echo '<pre>'; print_r($finfo); exit;
|
|
$formdata = $finfo->data->formdata;
|
|
$formdata->link_type = 'form';
|
|
$formdata->is_approval = 'yes';
|
|
$formdata->title = $finfo->data->formdata->form_name;
|
|
if (\View::exists('form.'.$formdata->form_id)) {
|
|
return view('form.'.$formdata->form_id)->with(['questions' => $finfo->data->questions, 'form' => $formdata, 'id' => $finfo->data->formdata->submission_id ]);
|
|
} else {
|
|
return view('form.common')->with(['questions' => $finfo->data->questions, 'form' => $formdata, 'id' => $finfo->data->formdata->submission_id ]);
|
|
}
|
|
}
|
|
}
|