unioil-loyalty-app/app/Services/CardTypeService.php

199 lines
6.4 KiB
PHP

<?php
namespace App\Services;
use Response;
use Schema;
use Hash;
use App\Libraries\ListHelper;
use Illuminate\Http\Request;
use App\Contracts\CardTypeResourceInterface;
use App\CodeCardType;
use App\Helpers\HttpStatusCode;
use App\Helpers\CurrentUserHelper;
use App\Libraries\UuidHelper;
use App\Libraries\S3;
class CardTypeService implements CardTypeResourceInterface
{
public $cardType;
public $cardtype_id;
public function listing($params)
{
$pagination = ListHelper::validatePagination($params['page_size'],$params['page']);
$list = CodeCardType::where('is_active','=',1);
if($params['search'] != null)
{
$list->where(function($query) use ($params){
$query->where('code', 'LIKE', '%'.$params['search'].'%');
$query->orWhere('description', 'LIKE', '%'.$params['search'].'%');
$query->orWhere('name', 'LIKE', '%'.$params['search'].'%');
});
}
$sorting = $params['sorting'];
if(count($sorting) > 0)
{
$list = $list->orderBy($sorting['field'],$sorting['sort_order']);
}
if($pagination == true)
return $list->paginate($params['page_size']);
else
return $list->get();
}
public function store(Request $request)
{
$currentUser = CurrentUserHelper::get_currentAdmin();
$this->cardType = new CodeCardType;
$s3 = new S3;
$filePath = $s3->upload($request->file('image'),'cardTypes');
$filePath_bgimage = $s3->upload($request->file('bg_image'),'cardTypes_bg_image');
$uuid = new UuidHelper;
$this->cardType->cardtype_uuid = $uuid->generate_uuid1();
$this->cardType->code = $request->get('code');
$this->cardType->name = $request->get('type');
$this->cardType->description = $request->get('description');
$this->cardType->terms_and_conditions = $request->get('terms_and_conditions');
$this->cardType->faqs = $request->get('faqs');
$this->cardType->created_by = $currentUser->admin_id;
$this->cardType->image = $filePath;
$this->cardType->bg_image = $filePath_bgimage;
$this->cardType->id_number = $request->get('id_number');
$this->cardType->id_number_description = $request->has('id_number_description') ? $request->get('id_number_description') : "";
$this->cardType->is_black = $request->get('virtual_card_font_color');
if ($this->cardType->save())
{
return $this->cardType->cardtype_id;
}
else
{
return false;
}
}
public function getByField($data, $relationship = null)
{
if($relationship)
{
$this->cardType = CodeCardType::with($relationship);
}
else
$this->cardType = new CodeCardType;
if(count($data))
{
foreach ($data as $field => $value) {
$this->cardType = $this->cardType->where($field,$value);
}
}
return $this->cardType->get();
}
public function update($request,$uuid)
{
$currentUser = CurrentUserHelper::get_currentAdmin();
$this->cardType = CodeCardType::where('cardtype_uuid',$uuid)->first();
if($this->cardType)
{
$this->cardType->code = $request->get('code');
$this->cardType->name = $request->get('type');
$this->cardType->description = $request->get('description');
$this->cardType->terms_and_conditions = $request->get('terms_and_conditions');
$this->cardType->faqs = $request->get('faqs');
$this->cardType->updated_by = $currentUser->admin_id;
$this->cardType->id_number = $request->get('id_number');
$this->cardType->id_number_description = $request->has('id_number_description') ? $request->get('id_number_description') : "";
$this->cardType->is_black = $request->get('virtual_card_font_color');
if($request->has('image'))
{
$s3 = new S3;
$filePath = $s3->upload($request->file('image'),'cardTypes');
$this->cardType->image = $filePath;
}
if($request->has('bg_image'))
{
$s3 = new S3;
$filePath_bgimage = $s3->upload($request->file('bg_image'),'cardTypes_bg_image');
$this->cardType->bg_image = $filePath_bgimage;
}
if ($this->cardType->save())
{
return $this->cardType;
}
}
return false;
}
public function delete($uuid)
{
$currentUser = CurrentUserHelper::get_currentAdmin();
if(is_array($uuid))
{
$this->cardType = CodeCardType::whereIn('cardtype_uuid',$uuid)
->update([
'is_active' => 0,
'updated_by' => $currentUser->admin_id
]);
if($this->cardType)
return true;
else
return false;
}
else
{
$this->cardType = CodeCardType::where('cardtype_uuid',$uuid)->first();
$this->cardType->is_active = 0;
$this->cardType->updated_by = $currentUser->admin_id;
if ($this->cardType->save())
return true;
else
return false;
}
}
public function getDetailsWhereIn($field,$value,$relationship = null)
{
if($relationship)
{
$details = CodeCardType::with($relationship)
->whereIn($field,$value)->get();
}
else
{
$details = CodeCardType::whereIn($field,$value)->get();
}
return $details->toArray();
}
public function getAllCardType()
{
$data = CodeCardType::where('is_active',1)->get();
return $data;
}
}