id();
$user = Staff::with('StaffDetail')->find($id);
$compound = Compound::with('ConfidentialFile')->where('kpd',$request->kpd)->first();
$file = ConfidentialFile::where('no_siri',$compound->ConfidentialFile->no_siri)->first();
$jbkod = $compound->jbkod;
$now = Carbon::now();
$gDate = $now->format('F Y');
if($request->dashboard == "true"){
$roles = Roles::where('kod', $request->kategori_modul)->first();
if($compound->modul == $request->kategori_modul){
return redirect()->back()->withInput()->with('error_msg','Tidak Berjaya! Kompaun ini sudah berada di dalam kategori modul '.$roles->name);
}else{
$reg_id = array();
if(!empty($roles->staff_detail_ids)){
foreach($roles->staff_detail_ids as $s){
$staff = Staff::with(['StaffDetail' => function($q) use($jbkod){
$q->where('jbkod', $jbkod);
}])->where('_id', $s)->first();
if(!empty($staff->StaffDetail)){
if($staff->token_firebase != ''){
$reg_id[] = $staff->token_firebase;
}
}
}
}
if(count($reg_id) > 0) {
$client = new \GuzzleHttp\Client();
$result = $client->request('POST', url('api/push/notification'), [
'verify' => false,
'form_params' => [
'title' => $roles->name.' '.$compound->kpd,
'msg' => 'Kompaun ini telah di pindahkan ke modul '.$roles->name,
'register_id' => $reg_id,
]
]);
}
$compound->modul = $request->kategori_modul;
$compound->save();
$historyData = [
'tarikh_kumpulan' => $gDate,
];
$subHistory = [
'no_siri' => $compound->ConfidentialFile->no_siri,
'tajuk' => "Serahan Modul",
'huraian' => $compound->kpd. " / Fail Kulit (".$compound->ConfidentialFile->no_siri.") telah diserahkan ke modul ".$roles->name,
];
$groupByDate = History::where('tarikh_kumpulan', $gDate)->first();
if(!empty($groupByDate)){
$groupByDate->subhistory()->create($subHistory);
$compound->ConfidentialFile->history()->attach($groupByDate);
}else{
$history = History::create($historyData);
$history->subhistory()->create($subHistory);
$compound->ConfidentialFile->history()->attach($history);
}
return redirect()->back()->with('success_msg', 'Berjaya Kemaskini! Kompaun ini berjaya diserahkan ke kategori modul '.$roles->name);
}
}else if($request->dashboard == "false"){
if($compound->penguatkuasa == $request->penguatkuasa){
$staff = StaffDetail::where('_id',$request->penguatkuasa)->first();
return redirect()->back()->withInput()->with('error_msg','Tidak Berjaya! Kompaun ini sudah diserahkan kepada penguatkuasa ['.$staff->no_badan.'] '.$staff->full_name);
}else{
$staff = Staff::with('StaffDetail')->where('_id',$request->penguatkuasa)->where(function($q){
$q->where('roles_access','PenguatKuasa')->orWhere('roles_access','PPenguatKuasa');
})->first();
$role = Roles::where('kod', $compound->modul)->first();
if(!empty($staff)){
if($staff->token_firebase != '') {
$client = new \GuzzleHttp\Client();
$result = $client->request('POST', url('api/push/notification'), [
'verify' => false,
'form_params' => [
'title' => $role->name.' '.$compound->kpd,
'msg' => 'Kompaun ini berada di ruangan '.$role->name,
'register_id[]' => $staff->token_firebase,
]
]);
}
$compound->penguatkuasa = $request->penguatkuasa;
$compound->save();
$historyData = [
'tarikh_kumpulan' => $gDate,
];
$subHistory = [
'no_siri' => $compound->ConfidentialFile->no_siri,
'tajuk' => "Menugaskan Penguatkuasa ".$staff->StaffDetail->full_name,
'huraian' => $compound->kpd. " / Fail Kulit (".$compound->ConfidentialFile->no_siri.") telah ditugaskan kepada penguatkuasa _id."/profile'>".$staff->StaffDetail->full_name.".",
];
$groupByDate = History::where('tarikh_kumpulan', $gDate)->first();
if(!empty($groupByDate)){
$groupByDate->subhistory()->create($subHistory);
$compound->ConfidentialFile->history()->attach($groupByDate);
}else{
$history = History::create($historyData);
$history->subhistory()->create($subHistory);
$compound->ConfidentialFile->history()->attach($history);
}
return redirect()->back()->with('success_msg', 'Berjaya Kemaskini! Kompaun ini berjaya ditugaskan kepada penguatkuasa ['.$staff->no_badan.'] '.$staff->StaffDetail->full_name);
}else{
return redirect()->back()->withInput()->with('error_msg','Tidak Berjaya! Data penguatkuasa tidak ditemui');
}
}
}
}
public function requestViewAllPdf($kpd) {
$ready_view_pdf = false;
$availablePDF = array();
$now = Carbon::now()->format('d/m/Y h:i:s A');
$site = SiteSetting::first();
$compound = Compound::with('ConfidentialFile','Attachment','CompoundInvestigation')->where('kpd', $kpd)->first();
$file = ConfidentialFile::with(['Memo' => function($q){
$q->orderBy('updated_at','ASC');
}],['Investigation' => function($q){
$q->with(['Attachment' => function($w){
$w->first();
}])->orderBy('updated_at','ASC');
}],['ItemInventory' => function($w){
$w->with('Barcode');
}])->where('_id',$compound->ConfidentialFile->_id)->first();
/**
** Compound
**/
$tawaran = '';
if($compound->jumlah_kemaskini_kompaun == ''){
$tawaran = $compound->jumlah_asal_kompaun;
}else{
$tawaran = $compound->jumlah_kemaskini_kompaun;
}
$staff = Staff::with('StaffDetail')->where('_id', $compound->dikeluarkan)->first();
$faulty = Faulty::with('DeedLaw')->where('_id',$compound->seksyen_kesalahan)->first();
$department = Department::where('_id', $compound->jbkod)->first();
$upload [] = [ 'name' => 'compound', 'contents' => $compound ];
$upload [] = [ 'name' => 'faulty', 'contents' => $faulty ];
$upload [] = [ 'name' => 'staff', 'contents' => $staff ];
$upload [] = [ 'name' => 'department', 'contents' => $department ];
$upload [] = [ 'name' => 'tawaran', 'contents' => $tawaran ];
$upload [] = [ 'name' => 'site', 'contents' => $site ];
$upload [] = [ 'name' => 'type', 'contents' => 'mbip' ];
$upload [] = [ 'name' => 'file', 'contents' => $file ];
$upload [] = [ 'name' => 'now', 'contents' => $now ];
$client = new \GuzzleHttp\Client();
$result = $client->request('POST', 'https://files.sipadu.my/api/compound/pdf', [
'multipart' => $upload
]);
$response = json_decode($result->getBody()->getContents());
if($response->success == true){
$compound->pdf_path = $response->data;
$compound->save();
}
/**
** Investigation
**/
$upload [] = [ 'name' => 'file', 'contents' => $file ]; $temp = [];
if(!empty($file->Investigation)){
foreach($file->Investigation as $i => $in){
$attach = Investigation::with('Attachment')->where('_id',$in->_id)->first();
foreach ($attach->Attachment as $key => $p) {
$temp[] = $p->path;
}
}
$upload [] = [ 'name' => 'fileInvest', 'contents' => json_encode($temp)];
}
/**
** All Memo in file
**/
if(!empty($file->Memo)){
$memoData = array();
foreach($file->Memo as $i => $m){
$memo = Memo::with('Attachment')->where('_id',$m->_id)->first();
$staffD = Staff::with('StaffDetail')->where('_id',$memo->dikeluarkan)->first();
$staffS = Staff::with('StaffDetail')->where('_id',$memo->disahkan)->first();
$kesalahan = Faulty::where('itkod', $memo->itkod)->first();
$dikeluarkan = ''; $disahkan = ''; $faulty = ''; $modul = '';
if(!empty($staffD)){
$dikeluarkan = $staffD->StaffDetail->full_name;
}
if(!empty($staffS)){
$disahkan = $staffD->StaffDetail->full_name;
}
if(!empty($kesalahan)){
$faulty ='['.$kesalahan->skter.'] '.$kesalahan->nama;
}
if($memo->modul != '-'){
$r = Roles::where('kod',$memo->modul)->first();
if(!empty($r)){
$modul = $r->name;
}
}
$update = Carbon::parse($memo->updated)->toDateTimeString();
$mula = date('d/m/Y h:i a', strtotime($memo->tarikh_mula));
$akhir = date('d/m/Y', strtotime($memo->tarikh_akhir));
if($memo->jenis_data == "manual"){
$dataM = [
'memo' => $memo,
'dikeluarkan' => $dikeluarkan,
'disahkan' => $disahkan,
'update' => $update,
'mula' => $mula,
'akhir' => $akhir,
'faulty' => $faulty,
'modul' => $modul,
'attachment' => [],
];
$memoData[] = $dataM;
}else if($memo->jenis_data == "pdf"){
foreach($m->Attachment as $key => $a){
$pdf[] = $a->path;
}
$dataM = [
'memo' => $memo,
'dikeluarkan' => $dikeluarkan,
'disahkan' => $disahkan,
'update' => $update,
'mula' => $mula,
'akhir' => $akhir,
'faulty' => $faulty,
'modul' => $modul,
'attachment' => $pdf,
];
$memoData[] = $dataM;
}
}
$upload [] = [ 'name' => 'memo', 'contents' => json_encode($memoData)];
}
/**
** Inventory
**/
if(count($file->ItemInventory) > 0){
$dataItem = array();
foreach ($file->ItemInventory as $key => $b) {
$barcode = '';
foreach ($b->Barcode as $key => $br) {
$barcode .= ', '.$br->barcode_id;
$data = [
'item' => $b,
'barcode' => $barcode,
];
}
$dataItem[] = $data;
}
$upload [] = [ 'name' => 'inventori', 'contents' => json_encode($dataItem)];
}
$client = new \GuzzleHttp\Client();
$result = $client->request('POST', 'https://files.sipadu.my/api/store/compound/pdf', [
'multipart' => $upload
]);
$response = json_decode($result->getBody()->getContents());
if($response->success == true){
return redirect($response->data);
}else if($response->success == false){
return response()->json($response->message);
}
}
}