unioil-loyalty-app/app/Http/Controllers/API/ProductDetailsController.php

386 lines
13 KiB
PHP

<?php
namespace App\Http\Controllers\API;
use App\Contracts\AdminResourceInterface;
use App\Contracts\ProductDetailsResourceInterface;
use App\Http\Requests\SystemPreferenceFormValidation;
use App\Libraries\StratuscastLibrary;
use App\Libraries\RNRPHLibrary;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Helpers\HttpStatusCode;
use Illuminate\Routing\UrlGenerator;
class ProductDetailsController extends Controller
{
public $product_details;
public $product_category;
protected $format;
protected $module;
protected $model;
public $admin;
public function __construct(ProductDetailsResourceInterface $product_details,HttpStatusCode $httpStatusCode, UrlGenerator $url, AdminResourceInterface $admin)
{
$this->product_details = $product_details;
$this->format = $httpStatusCode;
$this->module = "Products";
$this->model = "Products";
$this->url = $url;
$this->admin = $admin;
}
public function getFuelList(){
$rnrph = new RNRPHLibrary();
$rnrph->getAllFuels();
if (count($rnrph->response['data']) > 0) {
return $this->format->success("Success", $rnrph->response['data']);
}else{
return $this->format->success("Something went wrong",[]);
}
}
public function sync()
{
$this->product_details->inActiveProductDetails();
$cyware = new StratuscastLibrary();
$cyware->getProducts();
if ($cyware->response['status_code'] == "200"){
foreach ($cyware->response['data'] as $key => $value){
$product_category = $this->product_details->getProductCategoryByField( "type", $value->product_category);
$product_category_id = '';
$product_category_data['type'] = $value->product_category;
$product_category_data['description'] = $value->product_category;
if (count($product_category) == 0){
$product_category_id = $this->product_details->storeProductCategory( $product_category_data );
}else{
$product_category_data['pc_id'] = $product_category[0]->pc_id;
$product_category_id = $product_category_data['pc_id'];
$this->product_details->updateProductCategory( $product_category_data );
}
//product_details
$product_details = $this->product_details->getProductDetailsByField( "name", $value->product_name);
$data['pc_id'] = $product_category_id;
$data['name'] = $value->product_name;
$data['code'] = '';
$data['image'] = $value->image;
$data['description'] = $value->product_description;
$data['details'] = $value->product_specification;
$data['is_active'] = 1;
if (count($product_details) == 0){
$this->product_details->storeProductDetails( $data );
}else{
$data['pd_id'] = $product_details[0]->pd_id;
$this->product_details->updateProductDetails( $data );
}
}
return $this->format->success("Success",[]);
}else{
return $this->format->badRequest("Something went wrong",[]);
}
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*storeProductDetails
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(SystemPreferenceFormValidation $request)
{
}
/**
* Display the specified resource.
*d
* @param int $id
* products@return \Illuminate\Http\Response
*/
public function show($id)
{
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$data = [];
$product_category = $this->product_details->getAllProductCategory( );
$sort1 = [];
$sort2 = [];
$sort3 = [];
foreach ($product_category as $key => $value){
$datas["category_name"] = $value->type;
$datas["category_uuid"] = $value->pc_uuid;
$datas["products"] = [];
foreach ($this->product_details->getProductsByCategory($value->pc_id) as $key => $values){
if ($value['type'] == 'Lubricants'){
$returnValues['code'] = $values->code ;
$returnValues['name'] = $values->name ;
$returnValues['image'] = $values->image ;
$returnValues['description'] = $values->description ;
$returnValues['details'] = json_decode(json_encode($this->parseLubricant($values->details,$values->description ))) ;
array_push($datas["products"], $returnValues);
}else{
if ($values->name == 'Euro 5 Gasoline 95' || $values->name == 'Euro 5 Gasoline 91' || $values->name == 'Euro 5 Gasoline 97'){
$returnValues['code'] = $values->code ;
$returnValues['name'] = $values->name ;
$returnValues['image'] = $values->image ;
$returnValues['description'] = $values->description ;
$returnValues['details'] = json_decode(json_encode($this->parseFuel($values->details))) ;
array_push($datas["products"], $returnValues);
}
if ($values->name != 'Euro5 Gasoline' && $values->name != 'Euro 5 Gasoline 95' && $values->name != 'Euro 5 Gasoline 91' && $values->name != 'Euro 5 Gasoline 97' ){
$returnValues['code'] = $values->code ;
$returnValues['name'] = $values->name ;
$returnValues['image'] = $values->image ;
$returnValues['description'] = $values->description ;
$returnValues['details'] = json_decode(json_encode($this->parseDetails($values->details))) ;
array_push($datas["products"], $returnValues);
}
}
}
// $datas["products"] = [];
if($datas["category_name"] == 'Lubricants')
$sort1 = $datas;
if($datas["category_name"] == 'Fuels')
$sort2 = $datas;
if($datas["category_name"] == 'Asphalt')
$sort3 = $datas;
// array_push($data, $datas);
}
array_push($data, $sort1);
array_push($data, $sort2);
array_push($data, $sort3);
return $this->format->mobile_success('Success',$data);
}
private function removeNBSP($details)
{
// $details = htmlentities($details, null, 'utf-8');
$details = str_replace("&nbsp;", "", $details);
$details = str_replace("nbsp", "", $details);
$details = str_replace("&amp;amp;", "", $details);
$details = str_replace("&amp;;", "", $details);
$details = str_replace("&amp;", "", $details);
$details = str_replace(" ", " ", $details);
return $details;
}
public function products()
{
$cyware = new StratuscastLibrary();
$cyware->getProducts();
if ($cyware->response['status_code'] == "200"){
return $this->format->success("curl success",$cyware->response['data']);
}
}
private function parseDetails($details)
{
$detailsReturn = [];
$detailsArray = array_filter(explode("&nbsp;", $details));
$title = '';
$detail = '';
foreach ($detailsArray as $key => $value){
if ($key == 0){
$title = $value;
}else{
$details = $this->checkDetails($value);
$detail .= empty($details['next-title']) ? $details." " : $details['details'];
if (!empty($details['next-title'])){
$detailArray = ["level"=>1, "details"=> $this->removeNBSP(str_replace("#39;","'",str_replace("&#39;","'",$details['details'])) )];
$detailarray = ['level'=>0,'title' => $title, 'details' => [$detailArray]];
array_push($detailsReturn , $detailarray);
$title = $details['next-title'];
$detail = null;
}
}
$arraydetails = [];
$detailArray = ["level"=>1, "details"=>$this->removeNBSP(str_replace("&#39;","'",str_replace("#39;","'",$details)))];
array_push($arraydetails , ['level'=>0,'title' => "Product description", 'details' => [$detailArray]]);
}
return count($detailsReturn) == 0 ? $arraydetails : $detailsReturn;
}
private function checkDetails($details)
{
$detailsArray = array_filter(explode(".", $details));
if (count($detailsArray) == 1){
return $details;
}else{
$detailsHolder = '';
$count = count($detailsArray) - 1;
foreach ($detailsArray as $key => $value) {
if ($count == $key ){
$detail['details'] = $detailsHolder;
$detail['next-title'] = $value;
return $detail;
}else{
$detailsHolder .= $value.".";
}
}
}
return $details;
}
private function parseLubricant($details, $description)
{
$detailsReturn = [];
$product_description = $this->delimitbyColon("Product description:".$this->get_string_between($details, 'Product description:', "Application:" ));
$application = $this->delimitbyColon("Application:".$this->get_string_between($details, 'Application:', "Benefits:" ));
$benefits = $this->delimitbyColon("Benefits:".$this->get_string_between($details, 'Benefits:', FALSE ));
array_push($detailsReturn , $product_description);
array_push($detailsReturn , $application);
array_push($detailsReturn , $benefits);
return $detailsReturn;
}
private function parseFuel($details)
{
$details2 = ["level"=>1, "details"=>$details];
return [['level'=>0 ,'title'=> "Product description", "details"=> [$this->removeNBSP(str_replace("&#39;","'",str_replace("#39;","'",$details2)))]]];
}
private function delimitbyColon($details)
{
$details = explode(":", $details);
$details2 = [["level"=>1, "details"=>str_replace("&nbsp",'',$details[1])]];
return ['level'=>0 ,'title'=> $details[0], "details"=> $details2];
}
private function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
if ($end == false) {
$len = strlen($string);
}
return substr($string, $ini, $len);
}
public function get_fuels_from_products()
{
$fuel_category = $this->product_details->getProductCategoryByField('type','Fuels');
if(count($fuel_category) > 0)
{
$products = $this->product_details->getProductsByCategory($fuel_category[0]->pc_id);
if(count($products) > 0)
{
$return = [];
foreach ($products as $key => $value) {
if($value['image'] != "") $return[] = $value['name'];
// $return[] = $value['name'];
}
return $this->format->mobile_success('Success', array_reverse($return));
}
}
return $this->format->mobile_success('No Records Found');
}
}