238 lines
6.6 KiB
PHP
238 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
use App\Contracts\AdminActionLogsInterface;
|
|
use App\Contracts\TopUpResourceInterface;
|
|
//use App\Helpers\CurrentUserHelper;
|
|
use App\Helpers\CurrentUserHelper;
|
|
use App\Http\Requests\TopUpFormValidation;
|
|
use App\TopUp;
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Libraries\ParameterHelper;
|
|
use App\Helpers\HttpStatusCode;
|
|
use App\Http\Resources\TopUpResource;
|
|
use DateInterval;
|
|
|
|
class TopUpController extends Controller
|
|
{
|
|
|
|
public $top_up;
|
|
public $admin_logs;
|
|
|
|
protected $format;
|
|
const MODULE = 'TOPUP';
|
|
|
|
public function __construct(TopUpResourceInterface $top_up, HttpStatusCode $httpStatusCode, AdminActionLogsInterface $admin_logs)
|
|
{
|
|
$this->top_up = $top_up;
|
|
$this->admin_logs = $admin_logs;
|
|
$this->format = $httpStatusCode;
|
|
$this->module = "TopUp";
|
|
$this->model = "TopUp";
|
|
}
|
|
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$params = [
|
|
'search' => ($request->has('_search') ? $request->get('_search') : NULL),
|
|
'page_size' => ($request->has('page_size') ? $request->get('page_size') : 10),
|
|
'page' => ($request->has('page') ? $request->get('page') : 10),
|
|
'sorting' => ParameterHelper::prepareSortingParameter($request)
|
|
];
|
|
|
|
$list = $this->top_up->listing($params);
|
|
|
|
if(count($list)){
|
|
$additionals = $this->format->success("Success",[],false);
|
|
$data = TopUpResource::collection($list)->additional($additionals);
|
|
return $data->response()->setStatusCode(200);
|
|
}else{
|
|
return $this->format->success("No records found",[]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
//
|
|
return "create";
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(TopUpFormValidation $request)
|
|
{
|
|
$id = $this->top_up->store($request);
|
|
if($id){
|
|
$this->admin_logs->log($id,self::MODULE,'STORE');
|
|
return $this->format->created('TopUp has been added');
|
|
}else{
|
|
return $this->format->notFound();
|
|
}
|
|
}
|
|
|
|
// Count the total number of data in the top_up table
|
|
public function get_count(){
|
|
$listCount = TopUp::where('created_at', '>=' ,date('Y-m-d').' 00:00:00')->where('created_at', '<=' ,date('Y-m-d').' 23:59:59')->count();
|
|
if($listCount <= 5){
|
|
return $this->format->mobile_success("Success",$listCount,false);
|
|
}
|
|
return $this->format->unprocessableEntity("You have reached today's top up limit",$listCount);
|
|
}
|
|
|
|
public function generate_fee_code(Request $request)
|
|
{
|
|
$fee_code = "0001";
|
|
$details = TopUp::orderBy('created_at', 'desc')->first();
|
|
|
|
if($details){
|
|
$fee_code = ltrim($details->fee_code, '0') + 1;
|
|
}
|
|
|
|
$data = [
|
|
'fee_code' => str_pad($fee_code, 4, '0', STR_PAD_LEFT)
|
|
];
|
|
|
|
if ($data){
|
|
return $this->format->success("SUCCESS",$data);
|
|
}else{
|
|
return $this->format->notFound();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show($uuid)
|
|
{
|
|
$details = TopUp::where(['topup_uuid' => $uuid,'is_active' => 1])->first();
|
|
if($details)
|
|
{
|
|
$additionals = $this->format->success("Success",[],false);
|
|
$data = (new TopUpResource($details))->additional($additionals);
|
|
return $data->response()->setStatusCode(200);
|
|
}
|
|
else{
|
|
return $this->format->notFound('Top Up doesn\'t exists');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
//
|
|
return "edit";
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(TopUpFormValidation $request, $id)
|
|
{
|
|
$currentUser = CurrentUserHelper::get_currentAdmin();
|
|
|
|
if ($id == null && empty($id)) {
|
|
return $this->format->notFound();
|
|
}
|
|
|
|
if(!$this->top_up->check($id)){
|
|
return $this->format->notFound();
|
|
}
|
|
|
|
$request->topup_uuid = $id;
|
|
$request->update_by = $currentUser->admin_id;
|
|
$details = $this->top_up->update($request);
|
|
|
|
if (!$details){
|
|
return $this->format->notFound();
|
|
}
|
|
|
|
$additionals = $this->format->success("Success",[],false);
|
|
$data = (new TopUpResource($details))->additional($additionals);
|
|
|
|
return $data->response()->setStatusCode(200);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy($uuid)
|
|
{
|
|
$details = $this->top_up->getByField('topup_uuid',$uuid);
|
|
|
|
if(isset($details['0']))
|
|
{
|
|
$details = $details['0'];
|
|
if($this->top_up->delete($uuid))
|
|
{
|
|
$this->admin_logs->log($details['topup_id'],self::MODULE,'DELETE');
|
|
return $this->format->success("TopUp Successfully Deleted");
|
|
}
|
|
}
|
|
|
|
return $this->format->notFound();
|
|
}
|
|
|
|
public function batch_delete(Request $request)
|
|
{
|
|
$uuid = $request->has('topup_uuid') ? $request->get('topup_uuid') : null;
|
|
|
|
if($uuid)
|
|
{
|
|
$details = $this->top_up->getDetailsWhereIn('topup_uuid',$uuid);
|
|
|
|
if($this->top_up->delete($uuid))
|
|
{
|
|
foreach ($details as $key => $value)
|
|
{
|
|
$this->admin_logs->log($value['topup_id'],self::MODULE,'DELETE');
|
|
}
|
|
|
|
return $this->format->success("TopUp Successfully Deleted");
|
|
|
|
}
|
|
else
|
|
{
|
|
return $this->format->notFound();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
$data['topup_uuid'] = 'topup_uuid is required';
|
|
return $this->format->unprocessableEntity("Submit at least one item",$data);
|
|
}
|
|
}
|
|
}
|