Files
api_arrahn/app/Http/Controllers/CustomerController.php
T
2020-11-17 10:47:35 +08:00

149 lines
4.7 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Customer;
use App\Gadai;
use App\Lelong;
use App\Cawangan;
use App\Poskod;
use Illuminate\Http\Request;
class CustomerController extends Controller
{
public function showAllCustomers()
{
return response()->json(Customer::on('mysql2')->get());
}
public function showOneCustomer($id)
{
return response()->json(Customer::on('mysql2')->find($id));
}
public function showOneCustomerByIC($ic){
$cawangan_all = Cawangan::on('mysql7')->get();
$customer_all = [];
foreach($cawangan_all as $index=>$cawangan){
$customer = Customer::on($cawangan['mysql'])
->where('kplama','=',$ic)
->orWhere('kpbaru','=',$ic)
->first();
if($customer != null){
array_push($customer_all,$customer);
}
}
return response()->json($customer_all);
}
public function showOneCustomerGadai($ic,$status){
if($status == 'belum_tebus'){
$gadai_status = '';
}elseif($status == 'sudah_tebus'){
$gadai_status = 'T';
}elseif($status == 'lelong'){
$gadai_status = 'L';
}
$cawangan_all = Cawangan::on('mysql7')->get();
$gadai_all = [];
foreach($cawangan_all as $index=>$cawangan){
if($status == 'belum_tebus' || $status == 'sudah_tebus'){
$gadai = Gadai::on($cawangan['mysql'])
->where([['sttebus','=',$gadai_status],['nokp','=',$ic]])
->get();
if(sizeof($gadai) != 0){
array_push($gadai_all,$gadai);
}
}elseif($status == 'lelong'){
$gadai = Gadai::on($cawangan['mysql'])
->where([['sttebus','=',$gadai_status],['nokp','=',$ic]])
->rightJoin('lelong', 'gadai.norujukan', '=', 'lelong.norujukan')
->get();
if(sizeof($gadai) != 0){
array_push($gadai_all,$gadai);
}
}
}
return response()->json($gadai_all);
}
public function showOneCustomerGadaiByCawangan($ic,$status,$kodcaw){
if($status == 'belum_tebus'){
$gadai_status = '';
}elseif($status == 'sudah_tebus'){
$gadai_status = 'T';
}elseif($status == 'lelong'){
$gadai_status = 'L';
}
$cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
if($status == 'belum_tebus' || $status == 'sudah_tebus'){
$gadai = Gadai::on($cawangan['mysql'])
->where([['sttebus','=',$gadai_status],['nokp','=',$ic]])
->get();
}elseif($status == 'lelong'){
$gadai = Gadai::on($cawangan['mysql'])
->where([['sttebus','=',$gadai_status],['nokp','=',$ic]])
->rightJoin('lelong', 'gadai.norujukan', '=', 'lelong.norujukan')
->get();
}
return response()->json($gadai);
}
public function create(Request $request)
{
$this->validate($request, [
]);
$customer = Customer::create($request->all());
return response()->json($customer, 201);
}
public function update($id, Request $request)
{
$customer = Customer::findOrFail($id);
$customer->update($request->all());
return response()->json($customer, 200);
}
public function updateOnline($nokp,Request $request){
$cawangan_all = Cawangan::on('mysql7')->get();
foreach($cawangan_all as $index=>$cawangan){
$customer = Customer::on($cawangan['mysql'])
->where('kplama','=',$nokp)
->orWhere('kpbaru','=',$nokp)
->first();
if($customer != null){
$poskod = Poskod::on('mysql7')->where('poskod','=',$request->poskod)->first();
$customer_update = Customer::on($cawangan['mysql'])
->where('kplama','=',$nokp)
->orWhere('kpbaru','=',$nokp)
->update(array(
'alamat1' => $request->alamat,
'poskod' => $request->poskod,
'koddaerah' => $poskod['koddaerah'],
'kodnegeri' => $poskod['kodnegeri'],
'telefon' => $request->telefon1,
'telefon2' => $request->telefon2
));
}
}
return response()->json($customer_update, 200);
}
public function delete($id)
{
Customer::findOrFail($id)->delete();
return response('Deleted Successfully', 200);
}
}