pos-gis/app/Models/merchantModel.php

297 lines
10 KiB
PHP
Raw Permalink 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\Models\merchantParkModel;
use App\Models\deviceModel;
use App\Models\tokofeeModel;
use Illuminate\Database\QueryException;
use App\Http\Controllers\Controller;
use Auth;
use Illuminate\Foundation\Auth\User as Authenticatable;
class merchantModel extends Model
{
protected $primaryKey = 'mid';
protected $table = 'merchant';
public $timestamps = false;
public function initData($request,$route)
{
// INIT DB
2024-10-08 06:24:47 +00:00
$data['title'] = 'Company';
2024-10-07 06:13:42 +00:00
$data['actButton'] = ['edit'];
$data['tableHead'] =
array(
// ["Logo","all","logo"],
2024-10-08 06:24:47 +00:00
["Company Code","all","mid"],
["Company Name","all","merchant_nm"],
2024-10-07 06:13:42 +00:00
["Address","all","address"],
["Phone","all","phone_no"],
["Tax","all","tax_list"],
2024-10-08 06:24:47 +00:00
// ["Hotel","all","is_hotel"],
// ["Restoran","all","is_resto"],
2024-10-07 06:13:42 +00:00
// ["Act","all","action"]
);
if (Auth::user()->role != 3) {
array_push($data['tableHead'],["Act","all","action"]);
}
$data['db'] = $this->table;
$data['db_key'] = $this->primaryKey;
$data['route'] = $route;
$dtable = \DB::table($this->table)
->select("merchant.*", DB::raw("string_agg(DISTINCT reff_tax.tax_nm || ' (' || reff_tax.tax_prs || '%)' ,', ') as tax_list"))
->leftJoin("device", function($join){
$join->on("device.mid", "=", "merchant.mid");
})
->leftJoin("merchant_tax", function($join){
$join->on("merchant_tax.mid", "=", "merchant.mid");
})
->leftJoin("reff_tax", function($join){
$join->on("reff_tax.tax_id", "=", "merchant_tax.tax_id");
})
->groupBy("merchant.mid")
->orderBy('merchant_nm')
->when(Auth::user()->role == 3, function ($query) use ($request) {
$query->where('city_id', Auth::user()->id_kota);
})
->get();
// LIST DATA TABLE
$data['data_table'] = $dtable;
// FORM FIELD FOR STORE
$data['set_field'] = [
// 'is_active' => $request->post('is_active')
];
// GET DATA FOR EDIT
if ($request->post('id')) {
if (Auth::user()->role == 3) {
return response()->json([
'rc' => 99,
'rm' => "No Access"
]);
}
$data['get_data_edit']['merchant'] = \DB::selectOne("SELECT * from merchant m where m.mid = ?",[$request->post('id')]);
}
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('is_hotel',function($data) {
return ($data->is_hotel == true) ? '<span class="label label-success label-rounded mr-2"><i class="las la-check"></i></span>':'<span class="label label-danger label-rounded mr-2" style="background-color: #d1d1d1 !important;"><i class="las la-times"></i></span>';
});
$dt->editColumn('is_resto',function($data) {
return ($data->is_resto == true) ? '<span class="label label-success label-rounded mr-2"><i class="las la-check"></i></span>':'<span class="label label-danger label-rounded mr-2" style="background-color: #d1d1d1 !important;"><i class="las la-times"></i></span>';
});
$dt->editColumn('tax_list',function($data) {
$list = '';
$dList = explode(",",$data->tax_list);
if($data->tax_list){
foreach ($dList as $key => $v) {
$list .= '<span class="label label-danger label-inline mr-2">'.$v.'</span>';
}
}
return $list;
});
$dt->editColumn('logo',function($data) {
return '<img style="width:50px;" src="'.asset('gallery').'/'.$data->logo.'" alt="">';
});
return $dt;
}
public function storeCustom($act,Request $request)
{
DB::beginTransaction();
try{
$cGlobal = new Controller();
2024-10-08 06:24:47 +00:00
$titleTrail = 'Company';
2024-10-07 06:13:42 +00:00
switch ($act) {
case 'storeMerchant':
if (Auth::user()->role == 3) {
return response()->json([
'rc' => 99,
'rm' => "No Access"
]);
}
$id = $request->input('get_id');
$oldData = null;
// VALIDATE
if ($id == '') {
$checkMID = DB::table('merchant')->where('mid', $request->mid)->count();
// $checkNMID = DB::table('merchant')->where('nmid', $request->nmid)->count();
$checkPHONE = DB::table('merchant')->where('phone_no', $request->phone_no)->count();
if ($checkMID > 0) {
return response()->json([
'rc' => 99,
'rm' => "MID ".$request->mid." Sudah Terdaftar"
]);
}
if ($checkPHONE > 0) {
return response()->json([
'rc' => 99,
'rm' => "Phone No ".$request->phone_no." Sudah Terdaftar"
]);
}
}else{
$dataCheck = DB::table('merchant')->where('mid',$id)->first();
if ($request->phone_no == $dataCheck->phone_no) {
}else{
$checkPHONE = DB::table('merchant')->where('phone_no',$request->phone_no)->count();
$validPHONE = ($checkPHONE > 0) ? false : true;
if ($validPHONE == false) {
return response()->json([
'rc' => 99,
'rm' => "Phone No ".$request->phone_no." Sudah Terdaftar"
]);
}
}
}
if ($id == "") {
$event = "Tambah ".$titleTrail." ".$request->input('merchant_nm');
$datas = new merchantModel();
$datas->mid = $cGlobal->cleanString($request->mid);
}else{
$event = "Ubah ".$titleTrail." ".$request->input('merchant_nm');
$oldData = \DB::table($this->table)->where('mid',$id)->first();
$datas = merchantModel::find($id);
}
$datas->merchant_nm = $cGlobal->cleanString($request->merchant_nm);
$datas->address = $request->address;
$datas->phone_no = $request->phone_no;
$datas->nmid = $cGlobal->cleanString($request->nmid);
$datas->is_hotel = $request->is_hotel;
$datas->is_resto = $request->is_resto;
// $datas->email = $request->email;
$datas->city_id = $request->kota;
$datas->save();
$cGlobal->auditTrailValue($event,$titleTrail,$this->table,json_encode($datas),json_encode($oldData));
break;
case 'listTax':
if (Auth::user()->role == 3) {
return response()->json([
'rc' => 99,
'rm' => "No Access"
]);
}
$dataList = DB::table('merchant_tax')->where('mid',$request->id)->get();
return response()->json([
'rc' => 0,
'rm' => "sukses",
'list' => $dataList
]);
break;
case 'addTax':
if (Auth::user()->role == 3) {
return response()->json([
'rc' => 99,
'rm' => "No Access"
]);
}
$listAdd = $request->tax_select;
DB::table('merchant_tax')->where('mid', $request->get_mid_tax)->delete();
if ($listAdd) {
foreach ($listAdd as $k => $v) {
$datas = [
'mid' => $request->get_mid_tax,
'tax_id' => $v
];
DB::table('merchant_tax')->insert($datas);
2024-10-08 06:24:47 +00:00
$cGlobal->auditTrailValue("Company Tax",$titleTrail,'merchant_tax',json_encode($datas),'');
2024-10-07 06:13:42 +00:00
}
}
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()
]);
}
}
}