unioil-mobile-api/app/Services/LoyaltyCardService.php

491 lines
16 KiB
PHP

<?php
namespace App\Services;
use Response;
use Schema;
use Hash;
use App\Libraries\ListHelper;
use Illuminate\Http\Request;
use App\Contracts\LoyaltyCardResourceInterface;
use App\Contracts\CardTypeResourceInterface;
use App\Helpers\HttpStatusCode;
use App\Helpers\CurrentUserHelper;
use App\Libraries\StaticContents;
use App\Libraries\UuidHelper;
use App\LoyaltyCard;
use App\CodeCivilStatus;
use App\CodeVehicleOwn;
use App\CodeFuelType;
use App\CodeGender;
class LoyaltyCardService implements LoyaltyCardResourceInterface
{
public $loyaltyCard;
public $lcard_id;
public $cardType;
public function __construct(CardTypeResourceInterface $cardType)
{
$this->cardType = $cardType;
}
public function listing($params)
{
$pagination = ListHelper::validatePagination($params['page_size'],$params['page']);
$list = LoyaltyCard::with(['personalDetails','codeVehicleOwn','codeCivilStatus','codeGender','codeCardType']);
if($params['search'] != null)
{
// $list = $list->whereHas('personalDetails', function ($query) use ($params) {
// $query->where('firstname', 'LIKE', '%'.$params['search'].'%');
// $query->orWhere('lastname', 'LIKE', '%'.$params['search'].'%');
// })
// ->orWhere(function($query) use ($params){
// $query->orWhere('card_number', 'LIKE', '%'.$params['search'].'%');
// });
$list = $list->where(function($list_or) use ($params){
$list_or->whereHas('personalDetails', function ($query) use ($params) {
$query->where('firstname', 'LIKE', '%'.$params['search'].'%');
$query->orWhere('lastname', 'LIKE', '%'.$params['search'].'%');
})
->orWhere(function($query) use ($params){
$query->orWhere('card_number', 'LIKE', '%'.$params['search'].'%');
});
});
}
if($params['filter'])
{
$list->where(function($query) use ($params){
foreach($params['filter'] as $field => $value)
{
if($value != null)
{
if(is_array($value))
{
foreach ($value as $v) {
$v = $field == 'is_validated' ? StaticContents::member_status($v, true) : $v;
$query->orWhere($field, $v);
}
}
else
{
$value = $field == 'is_validated' ? StaticContents::member_status($value, true) : $value;
$query->orWhere($field, $value);
}
}
}
});
}
// query works if you put it in last
$list = $list->where('loyalty_card.is_active','=',1)
->where('loyalty_card.is_locked', $params['locked'])
->where('cyware_synced',1);
$sorting = $params['sorting'];
if(count($sorting) > 0)
{
$list = $list->sort($sorting['field'],$sorting['sort_order']);
}
if($pagination == true){
return $list->paginate($params['page_size']);
}else
return $list->get();
}
public function getAll()
{
$this->loyaltyCard = LoyaltyCard::all();
return $this->loyaltyCard;
}
public function getByField($data, $relationship = null)
{
if($relationship)
{
$this->loyaltyCard = LoyaltyCard::with($relationship);
}
else
$this->loyaltyCard = new LoyaltyCard;
if(count($data))
{
foreach ($data as $field => $value) {
$this->loyaltyCard = $this->loyaltyCard->where($field,$value);
}
}
return $this->loyaltyCard->get();
}
public function store($pd_id, $data, $deviceUUID = null, $cyware_synced = null)
{
$this->loyaltyCard = new LoyaltyCard;
$uuid = new UuidHelper;
$this->loyaltyCard->lcard_uuid = $uuid->generate_uuid1();
$this->loyaltyCard->pd_id = $pd_id;
$this->loyaltyCard->card_number = $data->card_number;
$this->loyaltyCard->pin = $data->pin;
$this->loyaltyCard->birthdate = $data->birthdate;
$this->loyaltyCard->mobile = strlen($data->mobile) == 12 ? $data->mobile : '63'.$data->mobile;
$this->loyaltyCard->email = $data->email;
// for card activation
if(isset($data->deviceUUID) && $data->deviceUUID != null)
$this->loyaltyCard->deviceUUID = $data->deviceUUID;
// for card enrollment
if($cyware_synced == 1)
{
// get card type id
$cardtype = $this->cardType->getByField([
'code' => $data->card_type_code,
'is_active' => 1
]);
$card_id = count($cardtype) ? $cardtype[0]['cardtype_id'] : 0;
$this->loyaltyCard->cardtype_id = $card_id;
$this->loyaltyCard->expiry_date = $data->expiry_date;
$this->loyaltyCard->last_synchronized = date('Y-m-d H:i:s');
// $this->loyaltyCard->is_validated = 1;
// $this->loyaltyCard->validation_dt = date('Y-m-d H:i:s');
if($deviceUUID != null)
$this->loyaltyCard->deviceUUID = $deviceUUID;
if($cyware_synced != null)
$this->loyaltyCard->cyware_synced = $cyware_synced;
}
if ($this->loyaltyCard->save())
{
return [
'lcard_uuid' => $this->loyaltyCard->lcard_uuid,
'lcard_id' => $this->loyaltyCard->lcard_id,
];
}
else
{
return false;
}
}
public function getDetailsWhereIn($field,$value,$relationship = null)
{
if($relationship)
{
$details = LoyaltyCard::with($relationship)
->whereIn($field,$value)->get();
}
else
{
$details = LoyaltyCard::whereIn($field,$value)->get();
}
return $details->toArray();
}
public function delete($uuid)
{
$currentUser = CurrentUserHelper::get_currentAdmin();
if(is_array($uuid))
{
$this->loyaltyCard = LoyaltyCard::whereIn('lcard_uuid',$uuid)
->update([
'is_active' => 0,
'updated_by' => $currentUser->admin_id
]);
if($this->loyaltyCard)
return true;
else
return false;
}
else
{
$this->loyaltyCard = LoyaltyCard::where('lcard_uuid',$uuid)->first();
$this->loyaltyCard->is_active = 0;
$this->loyaltyCard->updated_by = $currentUser->id;
if ($this->loyaltyCard->save())
return true;
else
return false;
}
}
public function show($uuid,$relationship = null)
{
if($relationship)
{
$this->loyaltyCard = LoyaltyCard::with($relationship)->whereLcardUuid($uuid)->first();
}
else
$this->loyaltyCard = LoyaltyCard::whereLcardUuid($uuid)->first();
return $this->loyaltyCard;
}
public function activate_locked($uuid)
{
$this->loyaltyCard = LoyaltyCard::where('lcard_uuid',$uuid)->first();
$this->loyaltyCard->is_locked = 0;
// $this->loyaltyCard->lock_code = '';
// $this->loyaltyCard->lock_dt = null;
if ($this->loyaltyCard->save())
{
return true;
}
else
{
return false;
}
}
public function numberOfOTPAttempts($lcard_uuid)
{
$this->loyaltyCard = LoyaltyCard::with('otpAttempts')
->where('lcard_uuid',$lcard_uuid)
->numberOfOTPAttempts()
->first();
return $this->loyaltyCard;
}
public function lockMember($id,$lock_code)
{
$this->loyaltyCard = LoyaltyCard::where('lcard_id',$id)->first();
$this->loyaltyCard->is_locked = 1;
$this->loyaltyCard->lock_code = $lock_code;
$this->loyaltyCard->lock_dt = date('Y-m-d H:i:s');
if ($this->loyaltyCard->save())
{
return true;
}
else
{
return false;
}
}
public function validateMember($id)
{
$this->loyaltyCard = LoyaltyCard::where('lcard_id',$id)->first();
$this->loyaltyCard->is_validated = 1;
$this->loyaltyCard->validation_dt = date('Y-m-d H:i:s');
if ($this->loyaltyCard->save())
{
return true;
}
else
{
return false;
}
}
public function update($request, $id)
{
$civil_status = $request->has('civilstatus_code') ? CodeCivilStatus::code($request->civilstatus_code)->first() : null;
$vehicle_own = $request->has('vo_code') ? CodeVehicleOwn::code($request->vo_code)->first() : null;
$fuel_type = $request->has('fueltype_code') ? CodeFuelType::code($request->fueltype_code)->first() : null;
$gender = $request->has('gender_code') ? CodeGender::code($request->gender_code)->first() : null;
$this->loyaltyCard = LoyaltyCard::where('lcard_id',$id)->first();
$this->loyaltyCard->mobile = strlen($request->mobile) == 12 ? $request->mobile : '63'.$request->mobile;
$this->loyaltyCard->email = $request->email;
// $this->loyaltyCard->city_id = $request->city_id;
$this->loyaltyCard->city_name = $request->city;
$this->loyaltyCard->civilstatus_id = $civil_status ? $civil_status->civilstatus_id : 0;
$this->loyaltyCard->vo_id = $vehicle_own ? $vehicle_own->vo_id : 0;
$this->loyaltyCard->fueltype_id = $fuel_type ? $fuel_type->fueltype_id : 0;
$this->loyaltyCard->gender_id = $gender ? $gender->gender_id : 0;
$this->loyaltyCard->updated_by = $id;
if ($this->loyaltyCard->save())
{
return true;
}
else
{
return false;
}
}
public function setInactive($id)
{
$this->loyaltyCard = LoyaltyCard::where('lcard_id',$id)->first();
$this->loyaltyCard->is_validated = 0;
return $this->loyaltyCard->save() ? true : false;
}
public function remove_deviceuuid($card_number, $deactivated = 0)
{
$this->loyaltyCard = LoyaltyCard::where('card_number',$card_number)
->where('is_active',1)
->first();
$this->loyaltyCard->deviceUUID = "";
$this->loyaltyCard->cyware_deactivated = $deactivated;
return $this->loyaltyCard->save() ? true : false;
}
public function update_deviceuuid($lcard_id,$deviceUUID)
{
$this->loyaltyCard = LoyaltyCard::where('lcard_id',$lcard_id)
->first();
$this->loyaltyCard->deviceUUID = $deviceUUID;
return $this->loyaltyCard->save() ? true : false;
}
public function sync_cyware($data)
{
// get card type id
$cardtype = $this->cardType->getByField([
'code' => $data->card_type_code,
'is_active' => 1
]);
$card_id = count($cardtype) ? $cardtype[0]['cardtype_id'] : 0;
$this->loyaltyCard = LoyaltyCard::where('card_number',$data->card_number)->first();
$this->loyaltyCard->card_number = $data->card_number;
$this->loyaltyCard->pin = $data->pin;
$this->loyaltyCard->cardtype_id = $card_id;
$this->loyaltyCard->expiry_date = $data->expiry_date;
$this->loyaltyCard->birthdate = $data->birthdate;
$this->loyaltyCard->mobile = strlen($data->mobile) == 12 ? $data->mobile : '63'.$data->mobile;
$this->loyaltyCard->email = $data->email;
$this->loyaltyCard->total_pts_earn = $data->total_pts_earn;
$this->loyaltyCard->total_pts_redeem = $data->total_pts_redeem;
$this->loyaltyCard->total_pts_bal = $data->total_pts_bal;
$this->loyaltyCard->last_synchronized = date('Y-m-d H:i:s');
if ($this->loyaltyCard->save())
{
return $this->loyaltyCard->pd_id;
}
else
{
return false;
}
}
public function cyware_activate($lcard_id, $data)
{
// get card type id
$cardtype = $this->cardType->getByField([
'code' => $data->card_type_code,
'is_active' => 1
]);
$card_id = count($cardtype) ? $cardtype[0]['cardtype_id'] : 0;
$this->loyaltyCard = LoyaltyCard::where('lcard_id',$lcard_id)->first();
$this->loyaltyCard->cardtype_id = $card_id;
$this->loyaltyCard->expiry_date = $data->expiry_date;
$this->loyaltyCard->last_synchronized = date('Y-m-d H:i:s');
$this->loyaltyCard->cyware_synced = 1;
$this->loyaltyCard->is_locked = 0;
$this->loyaltyCard->lock_code = '';
$this->loyaltyCard->lock_dt = null;
if ($this->loyaltyCard->save())
{
return true;
}
else
{
return false;
}
}
public function update_non_cyware_synced($lcard_id, $data)
{
$this->loyaltyCard = LoyaltyCard::where('lcard_id',$lcard_id)->first();
$this->loyaltyCard->card_number = $data->card_number;
$this->loyaltyCard->pin = $data->pin;
$this->loyaltyCard->birthdate = $data->birthdate;
$this->loyaltyCard->mobile = strlen($data->mobile) == 12 ? $data->mobile : '63'.$data->mobile;
$this->loyaltyCard->email = $data->email;
$this->loyaltyCard->last_synchronized = date('Y-m-d H:i:s');
if($data->deviceUUID != null)
$this->loyaltyCard->deviceUUID = $data->deviceUUID;
if ($this->loyaltyCard->save())
{
return true;
}
else
{
return false;
}
}
public function store_signup($pd_id, $data)
{
$this->loyaltyCard = new LoyaltyCard;
$uuid = new UuidHelper;
$this->loyaltyCard->lcard_uuid = $uuid->generate_uuid1();
$this->loyaltyCard->pd_id = $pd_id;
$this->loyaltyCard->card_number = $data->card_number;
$this->loyaltyCard->pin = $data->pin;
$this->loyaltyCard->birthdate = $data->birthdate;
$this->loyaltyCard->mobile = $data->mobile;
$this->loyaltyCard->email = $data->email;
$this->loyaltyCard->cardtype_id = $data->card_id;
// $this->loyaltyCard->expiry_date = $data->expiry_date;
$this->loyaltyCard->is_validated = 1;
$this->loyaltyCard->validation_dt = date('Y-m-d H:i:s');
$this->loyaltyCard->cyware_synced = 1;
$this->loyaltyCard->last_synchronized = date('Y-m-d H:i:s');
if ($this->loyaltyCard->save())
{
return [
'lcard_uuid' => $this->loyaltyCard->lcard_uuid,
'lcard_id' => $this->loyaltyCard->lcard_id,
];
}
else
{
return false;
}
}
}