pos-gis/app/Models/tokoModel.php

415 lines
15 KiB
PHP
Raw Normal View History

2024-10-07 06:13:42 +00:00
<?php
namespace App\Models;
use Datatables, DB;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Yajra\DataTables\DataTables as DT;
use Illuminate\Support\Facades\Http;
use App\Http\Controllers\Controller;
use Auth;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Auth\User as Authenticatable;
class tokoModel extends Model
{
protected $primaryKey = 'outlet_id';
protected $table = 'outlet';
public $timestamps = false;
public function initData($request,$route)
{
// INIT DB
2024-10-08 06:24:47 +00:00
$data['title'] = 'Agent';
2024-10-07 06:13:42 +00:00
$data['actButton'] = ['edit'];
$data['tableHead'] =
array(
2024-10-08 06:24:47 +00:00
["Company Name","all","merchant_nm"],
["Nama Agent","all","outlet_nm"],
["Kode Agent","all","outlet_code"],
2024-10-07 06:13:42 +00:00
["Jenis","all","outlet_type_nm"],
["Alamat","all","address"],
["Kota","all","city_nm"],
["MID Trx","all","mid_trx"],
["EDC Device","all","sn_list"],
["Act","all","action"]
);
$data['db'] = $this->table;
$data['db_key'] = $this->primaryKey;
$data['route'] = $route;
$cGlobal = new Controller();
$dtable = \DB::table('outlet')
->select("outlet.*","reff_outlet_type.*","reff_city.*","merchant.merchant_nm", DB::raw("string_agg(DISTINCT device.sn,', ') as sn_list"))
->leftJoin("merchant", function($join){
$join->on("merchant.mid", "=", "outlet.mid");
})
->leftJoin("reff_city", function($join){
$join->on("reff_city.city_id", "=", "outlet.city_id");
})
->leftJoin("reff_outlet_type", function($join){
$join->on("reff_outlet_type.outlet_type_id", "=", "outlet.outlet_type_id");
})
->leftJoin("device", function($join){
$join->on("device.outlet_id", "=", "outlet.outlet_id");
})
->groupBy("outlet.outlet_id","reff_city.city_id","merchant.merchant_nm","reff_outlet_type.outlet_type_id")
->orderByDesc('outlet.outlet_id')
->when(Auth::user()->role == 3, function ($query) use ($request) {
$query->where('outlet.city_id', Auth::user()->id_kota);
})
->when(request('merchant') != null, function ($query) use ($request) {
if ($request->merchant != 'all') {
$query->where('outlet.mid', $request->merchant);
}
})
->get();
// LIST DATA TABLE
$data['data_table'] = $dtable;
// FORM FIELD FOR STORE
$data['set_field'] = [
'mid_trx' => $request->post('mid_trx'),
];
if ($request->post('id')) {
$data['get_data_edit']['outlet'] = collect(\DB::select("SELECT * FROM outlet where outlet_id = ?",[$request->post('id')]))->first();
$data['get_data_edit']['bank'] = DB::table('outlet_bank')->where('outlet_id',$request->post('id'))->get();
}
// GET DATA FOR EDIT
// if ($request->post('id')) {
// $data['get_data_edit'] = collect(\DB::select("SELECT * FROM outlet where outlet_id = ?",[$request->post('id')]))->first();
// }
foreach($data['tableHead'] as $v){
$arrHead[] = $v[2];
}
$data['head'] = implode(",",$arrHead);
return $data;
}
public function getDT($data,$init)
{
$dt = DT::of($data);
$dt->editColumn('sn_list',function($data) {
$list = '';
$dList = explode(",",$data->sn_list);
if($data->sn_list){
foreach ($dList as $key => $v) {
$list .= '<span class="label label-success label-inline mr-2 mb-2">'.$v.'</span>';
}
}
return $list;
});
return $dt;
}
public function storeCustom($act,Request $request)
{
DB::beginTransaction();
try{
$cGlobal = new Controller();
2024-10-08 06:24:47 +00:00
$titleTrail = 'Agent';
2024-10-07 06:13:42 +00:00
switch ($act) {
case 'storeToko':
$id = $request->input('get_id');
$oldData = null;
// VALIDATE
if ($id == '') {
$checkCode = DB::table('outlet')->where('outlet_code', $request->outlet_code)->count();
$checkNameOutlet = DB::table('outlet')->where('mid',$request->mid)->where('outlet_nm','ilike', '%' .$request->outlet_nm. '%')->count();
if ($checkCode > 0) {
return response()->json([
'rc' => 99,
2024-10-08 06:24:47 +00:00
'rm' => "Kode Agent ".$request->outlet_code." Sudah Terdaftar"
2024-10-07 06:13:42 +00:00
]);
}
if ($checkNameOutlet > 0) {
return response()->json([
'rc' => 99,
2024-10-08 06:24:47 +00:00
'rm' => "Nama Agent ".$request->outlet_nm." Sudah Terdaftar"
2024-10-07 06:13:42 +00:00
]);
}
}else{
$dataCheck = DB::table('outlet')->where('outlet_id',$id)->first();
if ($request->outlet_code == $dataCheck->outlet_code) {
}else{
$checkCode = DB::table('outlet')->where('outlet_code',$request->outlet_code)->count();
$validNMID = ($checkCode > 0) ? false : true;
if ($validNMID == false) {
return response()->json([
'rc' => 99,
2024-10-08 06:24:47 +00:00
'rm' => "Kode Agent ".$request->outlet_code." Sudah Terdaftar"
2024-10-07 06:13:42 +00:00
]);
}
}
if ($request->outlet_nm == $dataCheck->outlet_nm) {
}else{
$checkNameOutlet = DB::table('outlet')->where('mid',$request->mid)->where('outlet_nm','ilike', '%' .$request->outlet_nm. '%')->count();
$validName = ($checkNameOutlet > 0) ? false : true;
if ($validName == false) {
return response()->json([
'rc' => 99,
2024-10-08 06:24:47 +00:00
'rm' => "Nama Agent ".$request->outlet_nm." Sudah Terdaftar"
2024-10-07 06:13:42 +00:00
]);
}
}
}
if ($id == "") {
$event = "Tambah ".$titleTrail." ".$request->input('outlet_nm');
$getMaxId = \DB::table('outlet')->max('outlet_id');
$datas = new tokoModel();
$datas->outlet_id = $getMaxId+1;
$datas->is_active = 'f';
}else{
$event = "Ubah ".$titleTrail." ".$request->input('outlet_nm');
$oldData = \DB::table($this->table)->where('outlet_id',$id)->first();
$datas = tokoModel::find($id);
}
$datas->mid = $request->mid;
$datas->outlet_nm = $cGlobal->cleanString($request->outlet_nm);
$datas->address = $cGlobal->cleanString($request->post('address'));
$datas->outlet_code = $request->post('outlet_code');
$datas->outlet_type_id = $request->post('outlet_type_id');
if (Auth::user()->role == 3) {
$datas->city_id = Auth::user()->id_kota;
}else{
$datas->city_id = $request->post('city_id');
}
$datas->nmid = $request->post('nmid');
$datas->is_charge_service = $request->post('is_charge_service');
$datas->charge_service_prs = ($request->post('is_charge_service') == 't') ? $request->post('charge_service_prs'):0;
$datas->save();
$outletBank = $request->outlet_bank;
DB::table('outlet_bank')->where('outlet_id', $datas->outlet_id)->delete();
if ($outletBank) {
foreach ($outletBank as $k => $v) {
$getMaxIdM = \DB::table('outlet_bank')->max('outlet_bank_id');
$datasAdd = [
'outlet_bank_id' => $getMaxIdM+1,
'outlet_id' => $datas->outlet_id,
'bank_code' => $v
];
DB::table('outlet_bank')->insert($datasAdd);
}
}
$cGlobal->auditTrailValue($event,$titleTrail,$this->table,json_encode($datas),json_encode($oldData));
break;
case 'storeConfirm':
$id = $request->input('get_outlet_id');
$oldData = null;
if (Auth::user()->role == 3) {
$getOutlet = DB::table('outlet')->where('outlet_id',$id)->first();
if ($getOutlet->city_id != Auth::user()->id_kota){
return response()->json([
'rc' => 99,
'rm' => "Tidak Memiliki Akses"
]);
}
}
$event = "Confirm ".$titleTrail." ".$request->input('outlet_nm');
$oldData = \DB::table($this->table)->where('outlet_id',$id)->first();
$datas = tokoModel::find($id);
$datas->mid_trx = $request->post('mid_trx');
$datas->save();
$cGlobal->auditTrailValue($event,$titleTrail,$this->table,json_encode($datas),json_encode($oldData));
break;
case 'dataConfirm':
$dataList = DB::table('outlet')
->where('outlet_id',$request->id)
->first();
if (Auth::user()->role == 3) {
$getOutlet = DB::table('outlet')->where('outlet_id',$id)->first();
if ($getOutlet->city_id != Auth::user()->id_kota){
return response()->json([
'rc' => 99,
'rm' => "Tidak Memiliki Akses"
]);
}
}
return response()->json([
'rc' => 0,
'rm' => "sukses",
'data' => $dataList
]);
break;
case 'listEdc':
if (Auth::user()->role == 3) {
$getOutlet = DB::table('outlet')->where('outlet_id',$request->id)->first();
if ($getOutlet->city_id != Auth::user()->id_kota){
return response()->json([
'rc' => 99,
'rm' => "Tidak Memiliki Akses"
]);
}
}
$dataList = DB::table('device')
->leftJoin('reff_brand','reff_brand.id_brand','device.brand')
// ->where('mid',$request->id)
->where('outlet_id',$request->id)
->get();
return response()->json([
'rc' => 0,
'rm' => "sukses",
'list' => $dataList
]);
break;
case 'addEdc':
$getOutlet = DB::table('outlet')->where('outlet_id',$request->outlet_id)->first();
$getDevice = DB::table('device')->where('device_id',$request->device_id)->first();
if (Auth::user()->role == 3) {
if ($getOutlet->city_id != Auth::user()->id_kota){
return response()->json([
'rc' => 99,
'rm' => "Tidak Memiliki Akses"
]);
}
}
if($getDevice->outlet_id != null){
return response()->json([
'rc' => 99,
'rm' => "Tidak Memiliki Akses"
]);
}
$edc = deviceModel::find($request->device_id);
$edc->mid = $getOutlet->mid;
$edc->outlet_id = $request->outlet_id;
$edc->save();
DB::commit();
return response()->json([
'rc' => 0,
'rm' => "sukses",
'title' => $getOutlet->outlet_nm
]);
break;
case 'removeEdc':
if (Auth::user()->role == 3) {
$getOutlet = DB::table('device')
->join('outlet','device.outlet_id','outlet.outlet_id')
->where('device_id',$request->device_id)->first();
if ($getOutlet->city_id != Auth::user()->id_kota){
return response()->json([
'rc' => 99,
'rm' => "Tidak Memiliki Akses"
]);
}
}
$edc = deviceModel::find($request->device_id);
$edc->mid = null;
$edc->outlet_id = null;
$edc->save();
break;
}
DB::commit();
return response()->json([
'rc' => 0,
'rm' => "sukses"
]);
}
catch (QueryException $e){
if($e->getCode() == '23505'){
$response = "Terjadi Duplikasi Data, Data Gagal Disimpan !";
}else{
$response = "Terjadi Kesalahan, Data Tidak Sesuai !";
}
DB::rollback();
return response()->json([
'rc' => 99,
'rm' => $response,
// 'msg' => $e->getMessage()
]);
}
}
}