Rapport-Laravel/app/Http/Controllers/FormController.php

898 lines
48 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\LambdaRequest;
use App\Http\Requests\FormAdditional;
use DB;
use Log;
use Storage;
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);
//echo '<pre>'; print_r($formdata); 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->partition.'.'.$formdata->form_id)) {
return view('form.'.$formdata->partition.'.'.$formdata->form_id)->with($data);
} elseif (\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]);
//echo '<pre>'; print_r($finfo); exit;
// if($finfo->data->formdata->partition == 'rapport-dc') {
// echo 'Temporary down for maintanance, please check back after 10 min'; exit;
// }
Log::debug(print_r($finfo, true));
if( isset($finfo->data->users) && sizeof($finfo->data->users) > 0 ) {
$finfo->data->formdata->users = $finfo->data->users;
$finfo->data->formdata->manager_can_submit = true;
}
$request->session()->put('formdata', $finfo->data->formdata);
if($finfo->status == 'success' && $finfo->data->formdata->link_type == 'form' && isset($finfo->data->personalizations) && sizeof($finfo->data->personalizations) > 0 ){
return $this->deliverpersonalization($finfo->data->formdata, $finfo->data->questions, $finfo->data->personalizations);
} else 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, 'lastkey' => $finfo->data->lastkey ]);
} 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, 'lastkey' => $finfo->data->lastkey ]);
}
}
}
public function loadmore(Request $request) {
$finfo = $request->session()->get('finfo');
//echo $finfo; exit;
$lastkey = $request->input('lastkey', '');
//print_r(['finfo' => $finfo, 'lastkey' => $lastkey]);
$finfo = LambdaRequest::postrequest('form', 'formentity/getforminfo', ['finfo' => $finfo, 'lastkey' => $lastkey]);
$html = '';
foreach($finfo->data->submissions as $submission) {
$html .= '<tr>
<th>'.date("d M Y", strtotime(isset($submission->record_date) ? $submission->record_date : $submission->updatedAt)).'</th>
<th>
<img src="'.(isset($submission->user_info->avatar) ? $submission->user_info->avatar : 'https://ui-avatars.com/api/?background=random&name='.$submission->user_info->name).'" class="rounded-circle avatar-sm" />
</th>
<td>
<h5 class="m-0 fw-normal">'.$submission->user_info->name.'</h5>
<p class="mb-0 text-muted"><small>'.$submission->user_info->designation.'</small></p>
</td>';
foreach($finfo->data->questions as $code => $question) {
if($question->type == 'file'){
$html .= '<td><a target="_blank" href="'. (isset($submission->formdata->{$code}) ? (is_array($submission->formdata->{$code}) ? implode(', ', $submission->formdata->{$code}) : $submission->formdata->{$code}) : '' ).'" download="'.( isset($submission->formdata->{$code}) ? (is_array($submission->formdata->{$code}) ? implode(', ', $submission->formdata->{$code}) : $submission->formdata->{$code}) : '' ).'" class="btn btn-dark btn-xs"><i class="mdi mdi-cloud-download me-1"></i> Download</a></td>';
} else {
$html .= '<td>'.( isset($submission->formdata->{$code}) ? (is_array($submission->formdata->{$code}) ? implode(', ', $submission->formdata->{$code}) : $submission->formdata->{$code}) : '' ).'</td>';
}
}
$html .= '<th>';
if( in_array('edit', $submission->actions) ){
$html .= '<a href="/form/edit?id='.$submission->id.'" class="btn btn-primary btn-xs">Edit</button>';
}
$html .= '</th>
</tr>';
}
return response()->json(['html' => $html, 'lastkey' => $finfo->data->lastkey]);
}
public function show(Request $request) {
$formdata = $request->session()->get('formdata');
//Log::debug(print_r($formdata, true));
$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 ]);
//Log::debug(print_r($finfo, true));
//echo '<pre>'; print_r($formdata); print_r($finfo); exit;
$formdata->title = $finfo->data->formdata->title;
return $this->deliverform($formdata, $finfo->data->questions, $id);
}
public function submit(Request $request) {
$post = $request->except('_token');
$files = $request->file();
foreach($files as $fname => $file) {
$post[$fname] = $this->_uploadMedia($request, $fname, 'form/'.$post['form_id'].'/'.$fname);
}
if($post['id']=='') unset($post['id']);
$formdata = $request->session()->get('formdata');
if(isset($post['user_id']) && $post['user_id'] != '') {
$formdata->user_id = $post['user_id'];
}
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');
$files = $request->file();
//Log::debug(print_r($files, true));
// if($post['form_id'] == '1663069943-2d45ab0c-ccbe-4047-a77f-bce46b93257a') {
// exit;
// }
foreach($files as $fname => $file) {
$post[$fname] = $this->_uploadMedia($request, $fname, 'form/'.$post['form_id'].'/'.$fname);
}
if(isset($post['id']) && $post['id']=='') unset($post['id']);
$formdata = $request->session()->get('formdata');
if(isset($post['user_id']) && $post['user_id'] != '') {
$formdata->user_id = $post['user_id'];
}
if(isset($formdata) && !empty($formdata) && $formdata->user_id != '') {
$post = $this->processformdata($formdata->form_id, $post, $formdata->user_id, $formdata->partition);
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($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);
$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, $user_id = 0, $partition = '' ) {
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':
case '1660360290-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;
case '1655958637-c41fb1b6-4ddc-42d3-aa2a-563c1cf5e4c9':
Log::debug(print_r($post, true));
if(array_key_exists('chemist_name', $post) ) {
$chemistformdata = [];
$chemistformdata['form_id'] = '1654747635-9d7133e4-a641-4865-aeed-021bb34d8960';
$chemistformdata['record_date'] = $post['record_date'];
$chemistformdata['chemist_name'] = $post['chemist_name'];
$chemistformdata['dapasach_units'] = $post['dapasach_strips'];
$chemistformdata['dapasach_m_units'] = $post['dapasach_m_strips'];
$chemistformdata['dapasach_s_units'] = $post['dapasach_s_strips'];
Log::debug(print_r($chemistformdata, true));
$finfo = LambdaRequest::postrequest('form', 'formentity/submitmultipleform', ['formdata' => $chemistformdata, 'entity_id' => $chemistformdata['form_id'], 'user_id' => $user_id, 'partition' => $partition ]);
}
$rxformdata = [];
$rxformdata['form_id'] = '1657032612-8a539e63-fc5e-47c2-96f9-b4454cea7591';
$rxformdata['record_date'] = $post['record_date'];
$rxformdata['no_of_dapasach_rx'] = $post['no_of_dapasach_rx'];
$rxformdata['no_of_dapasach_m_rx'] = $post['no_of_dapasach_m_rx'];
$rxformdata['no_of_dapasach_s_rx'] = $post['no_of_dapasach_s_rx'];
$rxformdata['dapasach_units_made_available'] = $post['dapasach_units_made_available'];
$rxformdata['dapasach_m_units_made_available'] = $post['dapasach_m_units_made_available'];
$rxformdata['dapasach_s_units_made_available'] = $post['dapasach_s_units_made_available'];
Log::debug(print_r($rxformdata, true));
$finfo = LambdaRequest::postrequest('form', 'formentity/submitform', ['formdata' => $rxformdata, 'entity_id' => $rxformdata['form_id'], 'user_id' => $user_id, 'partition' => $partition ]);
unset($post['chemist_name']);
unset($post['dapasach_units']);
unset($post['dapasach_m_units']);
unset($post['dapasach_s_units']);
unset($post['no_of_dapasach_rx']);
unset($post['no_of_dapasach_m_rx']);
unset($post['no_of_dapasach_s_rx']);
unset($post['dapasach_units_made_available']);
unset($post['dapasach_m_units_made_available']);
Log::debug(print_r($post, true));
break;
case '1659513453-1f5e2584-fb76-4075-b040-6sg6e34a5423':
if($post['customer_type'] == 'Doctor') {
$post['doctor_count'] = 1;
$post['retailer_count'] = 0;
} else {
$post['doctor_count'] = 0;
$post['retailer_count'] = 1;
}
break;
case '1659513016-1f5e2584-fb76-4075-b040-a9a4e34a7458':
case '1659538834-fb8bd30a-a719-4285-8d74-063462930c1d':
if($post['customer_type'] == 'Doctor') {
$post['doctor_count'] = 1;
$post['retailer_count'] = 0;
} else {
$post['doctor_count'] = 0;
$post['retailer_count'] = 1;
}
$prefix = '';
if($post['brand_selected'] == 'DAPASACH FRANCHISE') {
$prefix = 'dapasach_';
if($post['customer_type'] == 'Retailer') {
$post[$prefix.'pob_dapasach'] = $post['pob_dapasach'];
$post[$prefix.'pob_dapasach_s'] = $post['pob_dapasach_s'];
$post[$prefix.'pob_dapasach_m'] = $post['pob_dapasach_m'];
}
} else if($post['brand_selected'] == 'METOLAR FRANCHISE') {
$prefix = 'metolar_';
if($post['customer_type'] == 'Retailer') {
$post[$prefix.'pob_metolar_trio'] = $post['pob_metolar_trio'];
$post[$prefix.'pob_metolar_3d'] = $post['pob_metolar_3d'];
}
}
$post[$prefix.'doctor_count'] = $post['doctor_count'];
$post[$prefix.'retailer_count'] = $post['retailer_count'];
break;
case '1663562771-bd3aeb6b-2495-4b82-9ce8-7ba159700a7a':
$post[strtolower($post['brand']).'_count'] = 1;
$post[strtolower($post['brand']).'_business_potential'] = $post['business_potential'];
break;
case '1663833303-3503e11a-ca91-4bb7-ac85-bc9bc36d9997':
if($post['doctor_activity'] == 'Yes' || $post['rx_generated'] == 'Yes') {
$rxformdata = [];
$rxformdata['form_id'] = '1665645874-53e8b736-a061-4231-b209-f229083a883c';
$rxformdata['record_date'] = empty($post['rx_date']) ? $post['activity_rx_date'] : $post['rx_date'];
$rxformdata['contact_id'] = $post['contact_id'];
$rxformdata['no_of_dapasach_rx'] = ($post['rx_generated'] == 'Yes') ? $post['no_of_dapasach_rx'] : 0;
$rxformdata['no_of_dapasach_m_rx'] = ($post['rx_generated'] == 'Yes') ? $post['no_of_dapasach_m_rx'] : 0;
$rxformdata['no_of_dapasach_s_rx'] = ($post['rx_generated'] == 'Yes') ? $post['no_of_dapasach_s_rx'] : 0;
$rxformdata['doctor_activity'] = $post['doctor_activity'];
$rxformdata['activity_name'] = isset($post['activity_name']) ? $post['activity_name'] : '';
Log::debug(print_r($rxformdata, true));
$finfo = LambdaRequest::postrequest('form', 'formentity/submitform', ['formdata' => $rxformdata, 'entity_id' => $rxformdata['form_id'], 'user_id' => $user_id, 'partition' => $partition ]);
}
if($post['converted_count'] == 1) {
$rxformdata = [];
$rxformdata['form_id'] = '1665653958-93e66eae-f0ad-4792-ae91-87f9ae787380';
$rxformdata['record_date'] = $post['conversion_date'];
$rxformdata['contact_id'] = $post['contact_id'];
$rxformdata['converted_count'] = $post['converted_count'];
Log::debug(print_r($rxformdata, true));
$finfo = LambdaRequest::postrequest('form', 'formentity/submitform', ['formdata' => $rxformdata, 'entity_id' => $rxformdata['form_id'], 'user_id' => $user_id, 'partition' => $partition ]);
}
break;
case '1669979078-e991fc27-e8c1-4120-9883-b42af09244d0':
$post[strtolower($post['brand_name'])] = $post['no_of_rx'];
break;
case '1669979081-e991fc27-e8c1-4120-9883-b42af09244d3':
$post[$post['activity_type']] = 1;
break;
case '1672032315-9b267137-4b81-425c-afb4-a4c0fa701372':
if($post['powerof5'] == 'Yes') {
$post['powerof5_duova_rx'] = $post['duova_rx'];
$post['powerof5_glycohale_fb_rx'] = $post['glycohale_fb_rx'];
$post['powerof5_maxiflo_rx'] = $post['maxiflo_rx'];
$post['powerof5_maxiflo_sb_rx'] = $post['maxiflo_sb_rx'];
$post['powerof5_nistami_rx'] = $post['nistami_rx'];
$post['powerof5_tiova_rx'] = $post['tiova_rx'];
$post['powerof5_tiova_sb_rx'] = $post['tiova_sb_rx'];
$post['powerof5_triohale_rx'] = $post['triohale_rx'];
}
break;
}
return $post;
}
public function history(Request $request) {
$formdata = $request->session()->get('formdata');
$form_id = $request->input('form_id', $formdata->form_id);
//echo '<pre>'; print_r($formdata); exit;
$history = LambdaRequest::postrequest('form', 'formentity/getformhistory', ['form_id' => $form_id, 'user_id' => $formdata->user_id, 'partition' => $formdata->partition ]);
//echo '<pre>'; print_r($history); print_r($formdata); exit;
$data = ['questions' => $history->data->questions, 'submissions' => $history->data->submissions, 'formname' => $history->data->formname, 'form_id' => $form_id ];
if (\View::exists('form.'.$formdata->partition.'.'.$formdata->form_id.'-history')) {
return view('form.'.$formdata->partition.'.'.$formdata->form_id.'-history')->with($data);
} elseif (\View::exists('form.'.$formdata->form_id.'-history')) {
return view('form.'.$formdata->form_id.'-history')->with($data);
} else {
return view('form.history')->with($data);
}
//return view('form.history')->with();
//echo '<pre>'; print_r($history); print_r($formdata); exit;
}
public function edit(Request $request) {
$formdata = $request->session()->get('formdata');
//Log::debug(print_r($formdata, true));
$id = $request->input('id', false);
//'user_id' => $formdata->user_id,
$finfo = LambdaRequest::postrequest('form', 'formentity/getformdefination', ['form_id' => $formdata->form_id, 'partition' => $formdata->partition, 'id' => $id ]);
$formdata->title = $finfo->data->formdata->title;
Log::debug(print_r(['form_id' => $formdata->form_id, 'user_id' => $formdata->user_id, 'partition' => $formdata->partition, 'id' => $id ], true));
if (\View::exists('form.'.$formdata->partition.'.'.$formdata->form_id)) {
return view('form.'.$formdata->partition.'.'.$formdata->form_id)->with(['questions' => $finfo->data->questions,'form_id' => $formdata->form_id, 'form' => $formdata, 'id' => $id ]);
} elseif (\View::exists('form.'.$formdata->form_id)) {
return view('form.'.$formdata->form_id)->with(['questions' => $finfo->data->questions,'form_id' => $formdata->form_id, 'form' => $formdata, 'id' => $id ]);
} else {
return view('form.common')->with(['questions' => $finfo->data->questions, 'form_id' => $formdata->form_id, '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;
$s3 = (isset($post['s3']) && $post['s3'] == 1) ? 1 : 0;
unset($post['download']);
unset($post['email']);
unset($post['s3']);
$response = LambdaRequest::postrequest('metric', 'metric_report/view', $post, $token);
//Log::debug(print_r($response, true));
//echo '<pre>'; print_r($response); print_r($post); exit;
$breadcrumbs = [];
if(isset($post['hid'])){
if($request->session()->has('leaderboard_breadcrumbs')) {
$breadcrumbs = $request->session()->get('leaderboard_breadcrumbs');
}
$breadcrumbs[$post['level']] = ['hid' => $post['hid'], 'username' => $post['username'], 'level' => $post['level'] ];
$request->session()->put('leaderboard_breadcrumbs', $breadcrumbs);
$breadcrumbs = array_filter($breadcrumbs, function($k) use($post) { return $k >= $post['level']; }, ARRAY_FILTER_USE_KEY);
krsort($breadcrumbs);
}
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;
case 'contacts':
if($response->data->graph->chart == 'contacts') {
//echo '<pre>'; print_r($response->data->graph->values); exit;
$headers = ['Doctor', 'MSL Code', 'User', 'Employee Code', 'Email', 'Designation', 'Manager 1 Name', 'Manager 1 Emp Code', 'Manager 1 Email', 'Manager 2 Name', 'Manager 2 Emp Code', 'Manager 2 Email', 'Zone', 'Region', "Area", "Territory"];
foreach($response->data->graph->values->metrics as $metric) {
$headers[] = $metric->metric;
}
$exceldata[] = $headers;
foreach($response->data->graph->values->contacts as $contact) {
$row = [];
$row[] = $contact->name;
$row[] = $contact->msl_code;
$user = $response->data->graph->values->users->{$contact->id};
$row[] = $user->name;
$row[] = $user->employee_code;
$row[] = $user->email;
$row[] = $user->designation;
$row[] = isset($user->manager1->name) ? $user->manager1->name : '-';
$row[] = isset($user->manager1->employee_code) ? $user->manager1->employee_code : '-';
$row[] = isset($user->manager1->email) ? $user->manager1->email : '-';
$row[] = isset($user->manager2->name) ? $user->manager2->name : '-';
$row[] = isset($user->manager2->employee_code) ? $user->manager2->employee_code : '-';
$row[] = isset($user->manager2->email) ? $user->manager2->email : '-';
$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->{$contact->id}) ? $metric->stats->{$contact->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));
if($s3) {
$filename = 'reports/'.strtotime("now").'-report.xlsx';
$res = Excel::store(new InvoicesExport($exceldata), 'public/'.$filename);
Log::debug(print_r($res, true));
Log::debug('/storage/'.$filename);
return response()->json(['sent' => 'yes', 'filename' => '/storage/'.$filename]);
} else {
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, 'breadcrumbs' => $breadcrumbs]);
}
}
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 ]);
}
}
protected function deliverpersonalization($formdata, $questions, $personalizations) {
$additional_info = $this->get_additional_info($formdata);
//echo '<pre>'; print_r($formdata); print_r($questions); print_r($personalizations); exit;
$data = ['questions' => $questions, 'form' => $formdata, 'form_id' => $formdata->form_id, 'personalizations' => $personalizations ];
if(sizeof($additional_info) > 0) {
$data['additional'] = $additional_info;
}
if (\View::exists('form.'.$formdata->partition.'.personalization-'.$formdata->form_id)) {
return view('form.'.$formdata->partition.'.personalization-'.$formdata->form_id)->with($data);
} elseif (\View::exists('form.personalization-'.$formdata->form_id)) {
return view('form.personalization-'.$formdata->form_id)->with($data);
} else {
return view('form.personalization')->with($data);
}
}
public function submitpersonalized(Request $request) {
$post = $request->except('_token');
$files = $request->file();
$images = [];
foreach($files as $fname => $file) {
$post[$fname] = $this->_uploadMediaLocal($request, $fname, 'public/personalized/'.$post['form_id'].'/'.$fname);
$images[] = $fname;
}
$post['images_fields'] = $images;
$post['form_info'] = json_decode($post['form_info']);
$request->session()->put('form_info', $post);
return response()->json($post);
}
public function submitpersonalizedimages(Request $request) {
$post = $request->except('_token');
$formdata = $request->session()->get('formdata');
$form_info = $request->session()->get('form_info');
foreach($form_info['images_fields'] as $im ) {
Log::debug(print_r($im, true));
$data = explode( ',', $post[$im] );
$image = base64_decode( $data[1] );
$filename = pathinfo($form_info[$im]);
$filename = $filename['basename'];
Storage::disk('public')->put('personalized/'.$post['form_id'].'/'.$im.'/cropped-'.$filename, $image);
$form_info['cropped-'.$im] = str_replace($filename, 'cropped-'.$filename, $form_info[$im]);
}
$request->session()->put('form_info', $form_info);
return redirect('/form/generatepersonalization');
}
public function generatepersonalization(Request $request) {
$post = $request->all();
$formdata = $request->session()->get('formdata');
$form_info = $request->session()->get('form_info');
$template = pathinfo($form_info['form_info']->template);
if(!file_exists(storage_path('app/public/personalized/'.$form_info['form_id'].'/template-'.$form_info['form_info']->id.'.'.$template['extension'] ))) {
//Log::debug(print_r($form_info['form_info'], true));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $form_info['form_info']->template);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // good edit, thanks!
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); // also, this seems wise considering output is image.
$data = curl_exec($ch);
curl_close($ch);
Storage::disk('public')->put('personalized/'.$form_info['form_id'].'/template-'.$form_info['form_info']->id.'.'.$template['extension'], $data);
}
$folder = storage_path('app/public/personalized/'.$form_info['form_id']);
$templatename = 'template-'.$form_info['form_info']->id.'.'.$template['extension'];
$outputImage = $this->retimage($folder.'/'.$templatename);
foreach($form_info['form_info']->positions as $pos) {
switch($pos->type) {
case 'image':
$image = storage_path( str_replace('/storage', 'app/public', $form_info['cropped-'.$pos->field_name] ) );
$photoimg = $this->retimage( $image );
$photosize = getimagesize( $image );
imagecopyresized($outputImage,$photoimg,$pos->x,$pos->y,0,0,$pos->width,$pos->height, $photosize[0], $photosize[1]);
break;
case 'text':
$font = storage_path('app').'/fonts/Poppins-Medium.ttf';
$white = imagecolorallocate($outputImage, 0, 0, 0);
imagettftext($outputImage, $pos->size, 0, $pos->x, $pos->y, $white, $font, $form_info[$pos->field_name] );
break;
}
}
$filename = 'generated-'.strtotime("now").'.png';
$form_info['genareted_image'] = $filename;
unset($form_info['form_info']);
imagepng($outputImage, $folder.'/'.$filename);
imagedestroy($outputImage);
if(isset($formdata) && !empty($formdata) && $formdata->user_id != '') {
$post = $this->processformdata($formdata->form_id, $post, $formdata->user_id, $formdata->partition);
$finfo = LambdaRequest::postrequest('form', 'formentity/submitform', ['formdata' => $form_info, 'entity_id' => $formdata->form_id, 'user_id' => $formdata->user_id, 'partition' => $formdata->partition ]);
}
$request->session()->put('form_info', $form_info);
return redirect('/form/downloadpersonalization');
}
public function downloadpersonalization(Request $request) {
$form_info = $request->session()->get('form_info');
//echo '<prE>';print_r($form_info); exit;
return view('form.download')->with('image_url', 'personalized/'.$form_info['form_id'].'/'.$form_info['genareted_image']);
}
public function downloaddata(Request $request) {
$formdata = $request->session()->get('formdata');
$form_id = $request->input('form_id', $formdata->form_id);
//echo '<pre>'; print_r($formdata); exit;
$history = LambdaRequest::postrequest('form', 'formentity/getformhistory', ['form_id' => $form_id, 'user_id' => $formdata->user_id, 'partition' => $formdata->partition, 'team_report' => 1 ]);
//echo '<pre>'; print_r($history); exit;
$exceldata = [];
$exceldata[] = [$formdata->title];
$exceldata[] = [''];
$headers = ['Submission Date', 'Submitted By'];
foreach($history->data->questions as $question) {
$headers[] = $question->label;
}
$exceldata[] = $headers;
foreach($history->data->submissions as $submission) {
$row = [];
$row[] = $submission->createdAt;
$row[] = $submission->user_info->name;
foreach($history->data->questions as $q => $v) {
$row[] = isset($submission->formdata->{$q}) ? $submission->formdata->{$q} : '-';
}
$exceldata[] = $row;
}
return Excel::download(new InvoicesExport($exceldata), $formdata->title.'.xlsx');
//echo $report;
// Excel::create($formdata->title, function($excel) {
// $excel->sheet('Form Data', function($sheet) {
// $sheet->fromArray($exceldata);
// });
// });
// Excel::create(new InvoicesExport($exceldata), 'public/'.$filename)->download('xlsx');
// $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));
// if($s3) {
// $filename = 'reports/'.strtotime("now").'-report.xlsx';
// $res = Excel::store(new InvoicesExport($exceldata), 'public/'.$filename);
// Log::debug(print_r($res, true));
// Log::debug('/storage/'.$filename);
// return response()->json(['sent' => 'yes', 'filename' => '/storage/'.$filename]);
// } else {
// Mail::to( $response->data->user->email )->send(new ReportExcel($data));
// return response()->json(['sent' => 'yes']);
// }
// echo '<pre>'; print_r($history); print_r($formdata); exit;
// $form_id = $request->input('form_id', '');
// echo $form_id; exit;
}
protected function retimage($image) {
$size = getimagesize($image);
switch ($size['mime']) {
case 'image/jpeg':
$photoimg = imagecreatefromjpeg($image);
break;
case 'image/png':
$photoimg = imagecreatefrompng($image);
break;
case 'image/jpeg':
$photoimg = imagecreatefrompng($image);
break;
default:
# code...
break;
}
return $photoimg;
}
public function getdoctorlist(Request $request) {
$response = LambdaRequest::getrequest('ums', 'contact/get_my_contacts', ['hierarchy' => $request->input('hierarchy', ''), 'partition' => $request->input('partition', '') ]);
$docs = [];
foreach($response->data->items as $item) {
$docs[] = ['value' => $item->id, 'display' => $item->name];
}
return response()->json($docs);
}
public function surveyhandle($finfo, Request $request) {
$request->session()->put('finfo', $finfo);
$finfo = LambdaRequest::postrequest('form', 'survey/getforminfo', ['finfo' => $finfo]);
// echo '<pre>'; print_r($finfo); exit;
$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);
$formdata = $finfo->data->formdata;
$questions = $finfo->data->questions;
$data = ['questions' => $questions, 'form' => $formdata, 'form_id' => $formdata->form_id, 'id' => false];
if (\View::exists('srvey.'.$formdata->partition.'.'.$formdata->form_id)) {
return view('srvey.'.$formdata->partition.'.'.$formdata->form_id)->with($data);
} elseif (\View::exists('form.'.$formdata->form_id)) {
return view('srvey.'.$formdata->form_id)->with($data);
} else {
// return $data;
return view('srvey.common')->with($data);
}
} elseif($finfo->status == 'success' && $finfo->data->formdata->link_type == 'list') {
//echo '<pre>'; print_r($finfo->data); exit;
if (\View::exists('survey.list-'.$finfo->data->formdata->form_id)) {
return view('srvey.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, 'lastkey' => $finfo->data->lastkey ]);
} else {
return view('srvey.list')->with(['questions' => $finfo->data->questions, 'submissions' => $finfo->data->submissions, 'formname' => $finfo->data->formdata->title, 'form_id' => $finfo->data->formdata->form_id, 'lastkey' => $finfo->data->lastkey ]);
}
}
}
public function ajaxsurveysubmit(Request $request) {
$post = $request->except('_token');
$files = $request->file();
foreach($files as $fname => $file) {
$post[$fname] = $this->_uploadMedia($request, $fname, 'form/'.$post['form_id'].'/'.$fname);
}
if(isset($post['id']) && $post['id']=='') unset($post['id']);
$formdata = $request->session()->get('formdata');
if(isset($post['user_id']) && $post['user_id'] != '') {
$formdata->user_id = $post['user_id'];
}
if(isset($formdata) && !empty($formdata) && $formdata->user_id != '') {
$post = $this->processformdata($formdata->form_id, $post, $formdata->user_id, $formdata->partition);
$finfo = LambdaRequest::postrequest('form', 'survey/submitform', ['formdata' => $post, 'entity_id' => $formdata->form_id, 'user_id' => $formdata->user_id, 'partition' => $formdata->partition ]);
if($finfo->status == 'success') {
return response()->json(['status' => 'success']);
} else {
return response()->json(['status' => 'failure']);
}
} else {
return response()->json(['status' => 'failure']);
}
}
}