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

329 lines
11 KiB
PHP
Executable File

<?php
namespace App\Services;
use Response;
use Schema;
use Hash;
use App\Libraries\ListHelper;
use Illuminate\Http\Request;
use Illuminate\Contracts\Filesystem\Filesystem;
use App\Contracts\PromotionResourceInterface;
use App\Promotions;
use App\PhotoSlider;
use App\Station;
use App\Helpers\HttpStatusCode;
use App\Helpers\CurrentUserHelper;
use App\Libraries\UuidHelper;
use App\Libraries\S3;
class PromotionService implements PromotionResourceInterface
{
public $promotion;
public $promotion_id;
public function listing($params)
{
$pagination = ListHelper::validatePagination($params['page_size'],$params['page']);
$list = Promotions::with('promotionStations.station')
->where('is_active','=',1);
if($params['search'] != null)
{
$list->where(function($query) use ($params){
$query->where('title', '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->promotion = new Promotions;
$s3 = new S3;
$filePath = $s3->upload($request->file('image'),'promotions');
$uuid = new UuidHelper;
$this->promotion->promotion_uuid = $uuid->generate_uuid1();
$this->promotion->title = $request->get('title');
$this->promotion->description = $request->get('description');
$this->promotion->date_start = $request->get('date_start');
$this->promotion->date_end = $request->get('date_end');
$this->promotion->is_toppromotion = $request->get('is_toppromotion');
$this->promotion->promo_type = $request->get('promo_type');
$this->promotion->is_gps = $request->get('is_gps');
$this->promotion->created_by = $currentUser->admin_id;
$this->promotion->image = $filePath;
if ($this->promotion->save())
{
return $this->promotion->promotion_id;
}
return false;
}
public function getByField($field,$value, $relationship = null)
{
if($relationship)
{
$this->admin = Promotions::with($relationship)
->where($field,$value);
}
else
$this->admin = Promotions::where($field,$value);
return $this->admin->get();
}
public function update($request,$uuid)
{
$currentUser = CurrentUserHelper::get_currentAdmin();
$this->promotion = Promotions::where('promotion_uuid',$uuid)->first();
if($this->promotion)
{
$this->promotion->title = $request->get('title');
$this->promotion->description = $request->get('description');
$this->promotion->date_start = date('Y-m-d H:i:s',strtotime($request->get('date_start')));
$this->promotion->date_end = date('Y-m-d H:i:s',strtotime($request->get('date_end')));
$this->promotion->is_toppromotion = $request->get('is_toppromotion');
$this->promotion->promo_type = $request->get('promo_type');
$this->promotion->is_gps = $request->get('is_gps');
$this->promotion->updated_by = $currentUser->admin_id;
if($request->has('image'))
{
$s3 = new S3;
$path = $s3->upload($request->file('image'),'promotions');
$this->promotion->image = $path;
}
if ($this->promotion->save())
{
return $this->promotion;
}
}
return false;
}
public function delete($uuid)
{
$currentUser = CurrentUserHelper::get_currentAdmin();
if(is_array($uuid))
{
$this->promotion = Promotions::whereIn('promotion_uuid',$uuid)
->update([
'is_active' => 0,
'updated_by' => $currentUser->admin_id
]);
if($this->promotion)
{
$this->promotion = Promotions::whereIn('promotion_uuid',$uuid)->get();
$promotion_id = [];
foreach ($this->promotion as $key => $value) {
$promotion_id[] = $value->promotion_id;
}
PhotoSlider::whereIn('promotion_id',$promotion_id)
->update(['promotion_id' => 0]);
return true;
}
else
return false;
}
else
{
$this->promotion = Promotions::where('promotion_uuid',$uuid)->first();
$this->promotion->is_active = 0;
$this->promotion->updated_by = $currentUser->admin_id;
PhotoSlider::where('promotion_id',$this->promotion->promotion_id)
->update(['promotion_id' => 0]);
if ($this->promotion->save())
return true;
else
return false;
}
}
public function getDetailsWhereIn($field,$value,$relationship = null)
{
if($relationship)
{
$details = Promotions::with($relationship)
->whereIn($field,$value)->get();
}
else
{
$details = Promotions::whereIn($field,$value)->get();
}
return $details->toArray();
}
public function get_promotion($include_promotion = null)
{
$list = Promotions::where('is_active',1)
->where('date_end', '>=', date('Y-m-d H:i:s'))
->whereNotIn('promotion_id',function($query) use ($include_promotion) {
$query->select('promotion_id')
->from('photo_slider')
->where('is_active',1);
if($include_promotion != null)
{
$query->where('promotion_uuid','<>',$include_promotion);
}
});
return $list->get();
}
public function getTopTwoPromotion($type)
{
$date_now = date('Y-m-d H:i:s');
$this->promotion = Promotions::where([
['is_toppromotion',1],
['is_active',1]
])
->where('promo_type',$type)
->where('date_start', '<=', $date_now)
->where('date_end', '>=', $date_now);
return $this->promotion->get();
}
public function get_promo_by_date_type($date,$type)
{
$list = Promotions::where('is_active',1)
->where('promo_type',$type)
->where('date_start', '<=', $date)
->where('date_end', '>=', $date);
return $list->get();
}
public function get_promo_by_date_birthdate_type($date,$birthdate,$type)
{
$birthdate = date('Y').'-'.date('m-d',strtotime($birthdate));
$list = Promotions::where('is_active',1)
->where('promo_type',$type)
->where('date_start', '<=', $date)
->where('date_end', '>=', $date)
->where('date_start', '<=', $birthdate)
->where('date_end', '>=', $birthdate);
return $list->get();
}
public function get_promo_by_date_station_gps($station_ids, $date, $type)
{
$list = Promotions::with('promotionStations.station')
->where('is_active',1)
->where('is_gps',1)
->where('promo_type',$type)
->where('date_start', '<=', $date)
->where('date_end', '>=', $date)
->whereHas('promotionStations', function ($query) use ($station_ids) {
$query->whereIn('station_id', $station_ids);
})
->orderBy('created_at','desc');
return $list->get();
}
public function get_promo_by_birthdate_station_gps($station_ids,$date,$birthdate,$type)
{
$birthdate = date('Y').'-'.date('m-d',strtotime($birthdate));
$list = Promotions::with('promotionStations.station')
->where('is_active',1)
->where('is_gps',1)
->where('promo_type',$type)
->where('date_start', '<=', $date)
->where('date_end', '>=', $date)
->where('date_start', '<=', $birthdate)
->where('date_end', '>=', $birthdate)
->whereHas('promotionStations', function ($query) use ($station_ids) {
$query->whereIn('station_id', $station_ids);
})
->orderBy('created_at','desc');
return $list->get();
}
public function expire_top_two()
{
$this->promotion = Promotions::where('is_active',1)
->where('date_end', '<=', date('Y-m-d H:i:s'))
->update(['is_toppromotion' => 0]);
}
public function get_all_branch_promo($date, $type)
{
$list = Promotions::where('is_active',1)
->where('is_gps',1)
->where('promo_type',$type)
->where('date_start', '<=', $date)
->where('date_end', '>=', $date)
->whereDoesntHave('promotionStations', function ($query){});
return $list->get();
}
public function get_all_branch_promo_by_birthdate($date,$birthdate,$type)
{
$birthdate = date('Y').'-'.date('m-d',strtotime($birthdate));
$list = Promotions::where('is_active',1)
->where('is_gps',1)
->where('promo_type',$type)
->where('date_start', '<=', $date)
->where('date_end', '>=', $date)
->where('date_start', '<=', $birthdate)
->where('date_end', '>=', $birthdate)
->whereDoesntHave('promotionStations', function ($query){});
return $list->get();
}
}