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

267 lines
11 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\LambdaRequest;
use DB;
use League\Csv\Reader;
use Log;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\InvoicesExport;
class ProductController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->_service = 'catalog';
$this->_module = 'product';
$this->_viewname = 'product';
$this->_title = 'Product';
$this->_base_url = '/product';
$this->_listtype = 'list';
}
public function list(Request $request) {
$fields = $this->get_list_fields();
//echo $this->_listtype; exit;
$response = LambdaRequest::signedrequest('get', $this->_service, $this->_module.'/get_all');
//echo '<pre>'; print_r($response); exit;
return $this->view($this->_listtype)->with([
'title' => $this->_title,
'fields' => $fields,
'base_url' => $this->_base_url,
'items' => $response->data->items,
'buttons' => $this->get_list_buttons()
]);
}
protected function get_list_buttons() {
return [ ['title' => 'Add New', 'link' => $this->_base_url.'/addnew' ], ['title' => 'Import', 'link' => $this->_base_url.'/import'], ['title' => 'Export', 'link' => $this->_base_url.'/export'] ];
}
protected function get_import_fields($records){
$response = LambdaRequest::signedrequest('get', 'catalog', 'productattribute/get_all');
//echo '<pre>'; print_r($response); exit;
$hierar = $this->getlambda('get', 'ums', 'hierarchy', 'dropdown');
$hierar_dd = [];
foreach($hierar->data->items as $hier){
$hierar_dd[$hier->code] = $hier->name . ' (' . $hier->code . ')';
}
//echo '<pre>'; print_r($response); exit;
$fields = [
'name' => ['title' => 'Name', 'type' => 'text', 'required' => true],
'sku' => ['title' => 'SKU', 'type' => 'text', 'required' => true],
'division' => ['title' => 'Division', 'type' => 'text', 'required' => true],
'company' => ['title' => 'Company', 'type' => 'text'],
'brand' => ['title' => 'Brand', 'type' => 'select', 'required' => true, 'values' => $hierar_dd ],
'status' => ['title' => 'Status', 'type' => 'select', 'required' => true, 'values' => ['active' => 'Active', 'disabled' => 'Disabled'] ],
];
foreach ($response->data->items as $item) {
$values = [];
if(in_array($item->type, ['select', 'radio', 'checkbox']) ) {
foreach($item->options as $op) {
$values[$op->value] = $op->display;
}
}
$fields[$item->code] = ['title' => $item->attribute, 'type' => $item->type, 'required' => $item->required=='yes'?true:false, 'values' => $values];
}
return $fields;
}
// public function list(Request $request) {
// $response = LambdaRequest::signedrequest('get', $this->_service, $this->_module.'/get_all');
// //echo '<pre>'; print_r($items); exit;
// $items = [];
// foreach ($response->data->items as $item) {
// $item->position = $item->hierarchy_info->code;
// unset($item->hierarchy_info);
// $items[] = $item;
// }
// $fields = $this->get_list_fields();
// return $this->view('list')->with([
// 'title' => $this->_title,
// 'fields' => $fields,
// 'base_url' => $this->_base_url,
// 'items' => $items,
// 'buttons' => $this->get_list_buttons()
// ]);
// }
protected function get_list_fields(){
return [
'actions' => ['title' => 'Actions', 'buttons' => [ 'edit', 'delete' ] ],
'name' => ['title' => 'Name', 'type' => 'text'],
'sku' => ['title' => 'SKU', 'type' => 'text'],
'division' => ['title' => 'Division', 'type' => 'text'],
'company' => ['title' => 'Company', 'type' => 'text'],
'status' => ['title' => 'Status', 'type' => 'select'],
];
}
protected function get_form_fields($type, $values){
$response = LambdaRequest::signedrequest('get', 'catalog', 'productattribute/get_all');
$brandr = $this->getlambda('get', 'catalog', 'brand', 'dropdown');
$brandr_dd = [];
foreach($brandr->data->items as $bran){
$brandr_dd[$bran->id] = $bran->name;
}
$fields = [
'name' => ['title' => 'Name', 'type' => 'text', 'required' => true],
'sku' => ['title' => 'SKU', 'type' => 'text', 'required' => true],
'division' => ['title' => 'Division', 'type' => 'text', 'required' => true],
'company' => ['title' => 'Company', 'type' => 'text'],
'brand' => ['title' => 'Brand', 'type' => 'select', 'required' => true, 'values' => $brandr_dd ],
'status' => ['title' => 'Status', 'type' => 'select', 'required' => true, 'values' => ['active' => 'Active', 'disabled' => 'Disabled'] ],
];
foreach ($response->data->items as $item) {
$values = [];
if(in_array($item->type, ['select', 'radio', 'checkbox']) ) {
foreach($item->options as $op) {
$values[$op->value] = $op->display;
}
}
$fields[$item->code] = ['title' => $item->attribute, 'type' => $item->type, 'required' => $item->required=='yes'?true:false, 'values' => $values];
}
return $fields;
}
protected function _beforesave($type, $request) {
$data = $request->except('_token');
$options = [];
if(isset($data['option_value'])) {
foreach($data['option_value'] as $k => $ov) {
$options[] = ['value' => $ov, 'display' => $data['option_name'][$k] ];
}
$data['options'] = $options;
unset($data['option_value']);
unset($data['option_name']);
}
//echo '<pre>'; print_r($data); exit;
return $data;
}
public function editall(Request $request) {
$response = LambdaRequest::signedrequest('get', $this->_service, $this->_module.'/get_my_contacts');
//echo '<pre>'; print_r($response); exit;
$fields = $this->get_list_fields();
return $this->view('editall')->with([
'title' => $this->_title,
'fields' => $response->data->attributes,
'base_url' => $this->_base_url,
'items' => $response->data->items
]);
}
public function postsavelist(Request $request) {
Log::debug(print_r($request->except('_token'), true));
$response = LambdaRequest::signedrequest('post', $this->_service, $this->_module.'/savelist', $request->except('_token'));
// //echo '<pre>'; print_r($response); exit;
// $fields = $this->get_list_fields();
// return $this->view('editall')->with([
// 'title' => $this->_title,
// 'fields' => $response->data->attributes,
// 'base_url' => $this->_base_url,
// 'items' => $response->data->items
// ]);
}
public function export(Request $request) {
$attributes = LambdaRequest::signedrequest('get', $this->_service, 'contactattribute/get_all', []);
$fields = [];
foreach($attributes->data->items as $attri) {
$fields[] = $attri->code;
}
$fields[] = 'user_info';
$fields[] = 'msl_code';
$fields[] = 'position';
//echo '<pre>'; print_r($response); exit;
$response = LambdaRequest::signedrequest('get', $this->_service, $this->_module.'/get_all', ['source' => 'dynamodb', 'belowme' => 'yes', 'search' => json_encode([]), 'keyword' => "", 'fields' => json_encode($fields) ]);
//echo '<pre>'; print_r($response); exit;
$headers = ['Name', 'Email', 'Phone', 'MSL Code', 'Position', 'Rep Name', 'Rep Employee Code', 'Position Name', 'Position Code', 'Status'];
foreach($attributes->data->items as $attri) {
$headers[] = $attri->attribute;
}
$exceldata[] = $headers;
foreach($response->data->items as $user) {
$row = [];
$row[] = isset($user->name) ? $user->name : '-';
$row[] = isset($user->email) ? $user->email : '-';
$row[] = isset($user->phone) ? $user->phone : '-';
$row[] = isset($user->msl_code) ? $user->msl_code : '-';
$row[] = isset($user->position) ? $user->position : '-';
$row[] = isset($user->user_info->name) ? $user->user_info->name : '-';
$row[] = isset($user->user_info->employee_code) ? $user->user_info->employee_code : '-';
$row[] = isset($user->hierarchy_info->name) ? $user->hierarchy_info->name : '-';
$row[] = isset($user->hierarchy_info->code) ? $user->hierarchy_info->code : '-';
$row[] = isset($user->status) ? $user->status : '-';
foreach($attributes->data->items as $attri) {
$row[] = isset($user->{$attri->code}) ? $user->{$attri->code} : '-';
}
$exceldata[] = $row;
}
return Excel::download(new InvoicesExport($exceldata), 'contacts.xlsx');
}
// public function postimport(Request $request) {
// if( $request->file('importfile') ) {
// $path = $request->file('importfile')->store('user_import');
// $request->session()->put('user_import_path', $path);
// $csv = Reader::createFromPath(storage_path('app') .'/' . $path, 'r');
// $csv->setHeaderOffset(0);
// $header = $csv->getHeader(); //returns the CSV header record
// $records = $csv->getRecords(); //returns all the CSV records as an Iterator
// $contacts = [];
// echo '<pre>';
// foreach($records as $record) {
// $contacts[] = (array)$record;
// }
// $created = 0;
// $updated = 0;
// $count = 0;
// $total = sizeof($contacts);
// while($count < $total) {
// $tempdata = array_slice($contacts, $count, 20);
// Log::debug(print_r($tempdata, true));
// $response = LambdaRequest::signedrequest('post', $this->_service, $this->_module.'/import', $tempdata);
// Log::debug(print_r($response, true));
// $count = $count + 20;
// $created = $created + $response->data->created_count;
// $updated = $updated + (isset($response->data->updated_count) ? $response->data->updated_count : 0);
// Log::debug(print_r(['$count' => $count, '$created' => $created, '$updated' => $updated], true));
// }
// //echo '<pre>'; print_r($response); exit;
// if($response->statusCode == 200 && $response->status == "success") {
// return redirect($this->_base_url.'s')->withMessage('Successfully Imported ' .$created. ' records and Updated ' .$updated. ' records out of ' .$total. ' records!!!');
// } else {
// return \Redirect::back()->withMessage('Some Error occured!');
// }
// exit;
// return redirect($this->_base_url.'/importmap')->withMessage('Successfully Uploaded !!!');
// } else {
// return \Redirect::back()->withMessage('Some Error occured!');
// }
// }
}