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

161 lines
3.7 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: root
* Date: 10/3/18
* Time: 3:36 PM
*/
namespace App\Services;
use App\Helpers\CurrentUserHelper;
use App\Libraries\UuidHelper;
use App\TopUp;
use Response;
use Schema;
use Hash;
use App\Contracts\TopUpResourceInterface;
use App\Libraries\ListHelper;
class TopUpService implements TopUpResourceInterface
{
public $top_up;
public $admin;
public function listing($params)
{
$pagination = ListHelper::validatePagination($params['page_size'],$params['page']);
$list = TopUp::when($params['search'], function ($query) use ($params) {
return $query->where('name', 'like', '%'.$params['search'].'%', 'and')
->where('fee_code', 'like', '%'.$params['search'].'%', 'or' );
});
$list = $list->where('is_active','=',1);
$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)
{
$currentUser = CurrentUserHelper::get_currentAdmin();
$this->top_up = new TopUp;
$uuid = new UuidHelper;
$this->top_up->topup_uuid = $uuid->generate_uuid1();
$this->top_up->fee_code = $request->get('fee_code');
$this->top_up->name = $request->get('name');
$this->top_up->amount = $request->get('amount');
$this->top_up->type = $request->get('type');
if ($this->top_up->save())
{
return $this->top_up->topup_id;
}
else
{
return false;
}
}
public function update($request)
{
$this->top_up = TopUp::where([
['topup_uuid',$request->topup_uuid]
])->first();
$this->top_up->amount = $request->amount;
$this->top_up->type = $request->type;
$this->top_up->name = $request->name;
if ($this->top_up->save())
{
return $this->top_up;
}
else
{
return false;
}
}
public function check($id)
{
$this->top_up = TopUp::where([
['topup_uuid',$id]
])->first();
return $this->top_up ? $this->top_up : false;
}
public function getByField($field,$value)
{
$this->top_up = TopUp::where($field,$value);
return $this->top_up->get();
}
public function delete($uuid)
{
$currentUser = CurrentUserHelper::get_currentAdmin();
if(is_array($uuid))
{
$this->top_up = TopUp::whereIn('topup_uuid',$uuid)
->update([
'is_active' => 0,
'updated_by' => $currentUser->admin_id
]);
if($this->top_up)
return true;
else
return false;
}
else
{
$this->top_up = TopUp::where('topup_uuid',$uuid)->first();
$this->top_up->updated_by = $currentUser->admin_id;
$this->top_up->is_active = 0;
if ($this->top_up->save())
return true;
else
return false;
}
}
public function getDetailsWhereIn($field,$value,$relationship = null)
{
if($relationship)
{
$details = TopUp::with($relationship)->whereIn($field,$value)->get();
}
else
{
$details = TopUp::whereIn($field,$value)->get();
}
return $details->toArray();
}
}