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

172 lines
4.4 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: root
* Date: 10/3/18
* Time: 3:36 PM
*/
namespace App\Services;
use App\CodeCity;
use App\CodeProvince;
use App\Contracts\CityAndProvinceResourceInterface;
use App\Libraries\UuidHelper;
use Response;
use Schema;
use Hash;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\SystemPreference;
use App\Helpers\HttpStatusCode;
use Ramsey\Uuid\Uuid;
use App\Helpers\CurrentUserHelper;
class CityAndProvinceService implements CityAndProvinceResourceInterface
{
public $code_city;
public $code_province;
public function __construct(Request $request)
{
}
public function getAll()
{
$this->code_city = CodeCity::all();
$this->code_province = CodeProvince::all();
return $this->code_province;
}
public function getCityByField($field,$value)
{
$this->code_city = CodeCity::where($field,$value);
return $this->code_city->get();
}
public function getProvinceByField($field,$value)
{
$this->code_province = CodeProvince::where($field,$value);
return $this->code_province->get();
}
public function storeCity($value)
{
$this->code_city = new CodeCity;
$uuid = new UuidHelper;
$this->code_city->city_uuid = $uuid->generate_uuid1();
$this->code_city->code = $value['code'];
$this->code_city->name = $value['name'];
// $this->code_city->province_id = $value['province_id'];
$this->code_city->is_active = 1;
$this->code_city->created_by = 0;
if ($this->code_city->save())
{
return $this->code_city->province_id;
}
else
{
return false;
}
}
public function storeProvince($value)
{
$this->code_province = new CodeProvince;
$uuid = new UuidHelper;
$this->code_province->province_uuid = $uuid->generate_uuid1();
$this->code_province->code = $value['code'];
$this->code_province->name = $value['name'];
$this->code_province->is_active = 1;
$this->code_province->created_by = 0;
if ($this->code_province->save())
{
return $this->code_province->province_id;
}
else
{
return false;
}
}
public function updateProvince($value)
{
$this->code_province = CodeProvince::where([
['province_id',$value['province_id']]
])->first();
$this->code_province->code = $value['code'];
$this->code_province->name = $value['name'];
if ($this->code_province->save())
{
return $this->code_province;
}
else
{
return false;
}
}
public function updateCity($value)
{
$this->code_city = CodeCity::where([
['city_id',$value['city_id']]
])->first();
$this->code_city->code = $value['code'];
$this->code_city->name = $value['name'];
$this->code_city->is_active = 1;
// $this->code_city->province_id = $value['province_id'];
if ($this->code_city->save())
{
return $this->code_city;
}
else
{
return false;
}
}
public function getAllRelationship()
{
$this->code_province = CodeProvince::select(DB::raw('code_province.*,code_city.name as city_name, city_uuid'))
->leftJoin('code_city', 'code_province.province_id', '=','code_city.province_id')
->orderBy('code_province.name','asc')
->orderBy('code_city.name','asc')
->get();
return $this->code_province;
}
public function getAllCities()
{
$this->code_cities = CodeCity::select(DB::raw('*'))
->where('is_active',1)
->orderBy('name','asc')
->get();
return $this->code_cities;
}
public function disbale_cities($ids){
$toUpdate = [];
$toUpdate['is_active'] = 0;
CodeCity::whereIn('city_id', $ids)
->update($toUpdate);
}
}