level == 'DRAFTER' || Auth::user()->id == 11 || Auth::user()->level == 'QUANTITY SURVEYOR') {
// $clients = Client::where('id_user', Auth::user()->id)->get();
// $backup_volumes = BackupVolume::where('id_user', Auth::user()->id)->whereDoesntHave('rab')->get();
// } elseif (Auth::user()->level == 'ARSITEK') {
// $clients = Client::where('id_arsitek', Auth::user()->id)->get();
// $backup_volumes = BackupVolume::where('id_user', Auth::user()->id)->whereDoesntHave('rab')->get();
// } elseif (Auth::user()->level == 'VENDOR') {
// $clients = Client::where('id_vendor', Auth::user()->id)->get();
// $backup_volumes = BackupVolume::where('id_user', Auth::user()->id)->whereDoesntHave('rab')->get();
// } else {
// $clients = Client::all();
// $backup_volumes = BackupVolume::whereDoesntHave('rab')->get();
// }
return view('staff.new.rab-create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
DB::statement(' SET FOREIGN_KEY_CHECKS=0');
if ($request->act == 'duplicate') {
$rab = Rab::findOrFail($request->id);
$no_surat = null;
$bulan_romawi = $this->_bulantoRomawi(date('m'));
if ($rab->user->level == 'VENDOR') {
$cek_null = Rab::where('no_surat', '!=', null)->count();
if ($cek_null == 0) {
$no_surat = '001/Bintoro Steel/SPH/' . $bulan_romawi . '/' . date('Y');
} else {
$cek_last = Rab::where('no_surat', '!=', null)->orderBy('id', 'desc')->first()->no_surat;
$last_no = explode('/', $cek_last);
if ($last_no[4] == date('Y')) {
if ($last_no[3] == $bulan_romawi) {
$no_awal = sprintf("%03s", ($last_no[0] + 1));
$no_surat = $no_awal . '/Bintoro Steel/SPH/' . $bulan_romawi . '/' . date('Y');
} else {
$no_surat = '001/Bintoro Steel/SPH/' . $bulan_romawi . '/' . date('Y');
}
} else {
$no_surat = '001/Bintoro Steel/SPH/' . $bulan_romawi . '/' . date('Y');
}
}
}
$newR = Rab::create([
'id_client' => $rab->id_client,
'no_surat' => $no_surat,
'id_user' => Auth::user()->id,
'status' => 'non-approve',
'ppn' => $rab->ppn,
'nama_rab' => $rab->nama_rab,
'id_backup_volume' => $rab->id_backup_volume
]);
if ($rab->rabDetails) {
foreach ($rab->rabDetails as $row) {
if ($row->ahs) {
DB::statement(' SET FOREIGN_KEY_CHECKS=0');
$rabD = RabDetail::create([
'id_rab' => $newR->id,
'id_ahs' => $row->id_ahs,
'deskripsi' => $row->deskripsi,
'volume' => $row->volume,
'harga' => $row->harga,
'subtotal' => $row->subtotal,
]);
if ($row->materials) {
foreach ($row->materials as $row2) {
RabDetailMaterial::create([
'id_rab_detail' => $rabD->id,
'id_material' => $row2->id_material,
'qty' => $row2->qty,
'harga' => $row2->harga
]);
}
}
if ($row->upahs) {
foreach ($row->upahs as $row2) {
RabDetailUpah::create([
'id_rab_detail' => $rabD->id,
'id_upah' => $row2->id_upah,
'qty' => $row2->qty,
'harga' => $row2->harga
]);
}
}
} elseif ($row->ahs_vendor) {
DB::statement(' SET FOREIGN_KEY_CHECKS=0');
$rabD = RabDetail::create([
'id_rab' => $newR->id,
'id_ahs_vendor' => $row->id_ahs_vendor,
'deskripsi' => $row->deskripsi,
'volume' => $row->volume,
'harga' => $row->harga,
'subtotal' => $row->subtotal,
]);
}
}
}
return redirect(route('rab.index'))->with('msg', 'data berhasil diduplicate');
} else {
set_time_limit(0);
$no_surat = null;
$bulan_romawi = $this->_bulantoRomawi(date('m'));
if (Auth::user()->level == 'VENDOR') {
$cek_null = Rab::where('no_surat', '!=', null)->count();
if ($cek_null == 0) {
$no_surat = '001/Bintoro Steel/SPH/' . $bulan_romawi . '/' . date('Y');
} else {
$cek_last = Rab::where('no_surat', '!=', null)->orderBy('id', 'desc')->first()->no_surat;
$last_no = explode('/', $cek_last);
if ($last_no[4] == date('Y')) {
if ($last_no[3] == $bulan_romawi) {
$no_awal = sprintf("%03s", ($last_no[0] + 1));
$no_surat = $no_awal . '/Bintoro Steel/SPH/' . $bulan_romawi . '/' . date('Y');
} else {
$no_surat = '001/Bintoro Steel/SPH/' . $bulan_romawi . '/' . date('Y');
}
} else {
$no_surat = '001/Bintoro Steel/SPH/' . $bulan_romawi . '/' . date('Y');
}
}
}
$rab = Rab::create([
'id_client' => $request->client,
'no_surat' => $no_surat,
'status' => 'non-approve',
'id_user' => Auth::user()->id,
'ppn' => $request->ppn,
'nama_rab' => $request->nama_rab,
'id_backup_volume' => $request->id_backup_volume,
]);
if ($request->id_ahs) {
foreach ($request->id_ahs as $key => $value) {
DB::statement(' SET FOREIGN_KEY_CHECKS=0');
$rabD = RabDetail::create([
'id_rab' => $rab->id,
'id_ahs' => $value,
'deskripsi' => preg_replace('/[^A-Za-z0-9\s\.,-]/', ' ', $request->deskripsi[$key]),
'volume' => str_replace(',', '.', $request->volume[$key]),
'harga' => str_replace('.', '', $request->harga[$key]),
'subtotal' => 0,
]);
if ($rabD) {
$rabD->subtotal = $rabD->volume * $rabD->harga;
$rabD->save();
}
if ($rabD->ahs->ahsMaterialDetails) {
foreach ($rabD->ahs->ahsMaterialDetails as $row) {
if ($row->material->id) {
$rdm = RabDetailMaterial::create([
'id_rab_detail' => $rabD->id,
'id_material' => $row->material->id,
'qty' => $row->koefisien*$rabD->volume,
'harga' => $row->material->harga
]);
}
}
}
if ($rabD->ahs->ahsUpahDetails) {
foreach ($rabD->ahs->ahsUpahDetails as $row) {
if ($row->upah->id) {
RabDetailUpah::create([
'id_rab_detail' => $rabD->id,
'id_upah' => $row->upah->id,
'qty' => $row->koefisien*$rabD->volume,
'harga' => $row->upah->upah
]);
}
}
}
}
}
if ($request->id_ahs_vendor) {
foreach ($request->id_ahs_vendor as $key => $value) {
DB::statement(' SET FOREIGN_KEY_CHECKS=0');
$rabD = RabDetail::create([
'id_rab' => $rab->id,
'id_ahs_vendor' => $value,
'deskripsi' => preg_replace('/[^A-Za-z0-9\s\.,-]/', ' ', $request->deskripsi_v[$key]),
'volume' => str_replace(',', '.', $request->volume_v[$key]),
'harga' => str_replace('.', '', $request->harga_v[$key]),
'subtotal' => 0,
]);
if ($rabD) {
$rabD->subtotal = $rabD->volume * $rabD->harga;
$rabD->save();
}
}
}
return redirect(route('rab.index'))->with('msg', 'data berhasil ditambahkan');
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show(Request $request, $id)
{
if (\Request::ajax()) {
if (isset($_GET['act'])) {
if ($_GET['act'] == 'loadProject') {
$project = Project::whereDoesntHave('rab')->where('id_owner', $id)->get();
return response()->json([
'project' => $project,
]);
} elseif ($_GET['act'] == 'loadRAP') {
$modal = RapController::getModal($id);
return response()->json([
'modal' => $modal,
]);
} elseif ($_GET['act'] == 'rabData') {
$columns = array(
0 => 'id',
2 => 'status',
);
if (Auth::user()->level == 'ARSITEK' || Auth::user()->level == 'DRAFTER' || Auth::user()->level == 'QUANTITY SURVEYOR' || Auth::user()->level == 'SUPERVISI - ALUMNI' || Auth::user()->level == 'VENDOR') {
$totalData = Rab::where('id_user', Auth::user()->id)->count();
} else {
$totalData = Rab::whereHas('user', function ($q) {
if (isset($_GET['ub'])) {
if ($_GET['ub'] == 'steel') {
$q->where('level', 'VENDOR');
} elseif ($_GET['ub'] == 'build') {
$q->where('level', '!=', 'VENDOR');
}
}
})
->count();
}
$totalFiltered = $totalData;
$limit = $request->input('length');
$start = $request->input('start');
$order = $columns[$request->input('order.0.column')];
$dir = $request->input('order.0.dir');
if (empty($request->input('search.value'))) {
if (Auth::user()->level == 'ARSITEK' || Auth::user()->level == 'DRAFTER' || Auth::user()->level == 'QUANTITY SURVEYOR' || Auth::user()->level == 'SUPERVISI - ALUMNI' || Auth::user()->level == 'VENDOR') {
$rabs = Rab::where('id_user', Auth::user()->id)
->offset($start)
->limit($limit)
->orderBy($order, $dir)
->get();
} else {
$rabs = Rab::whereHas('user', function ($q) {
if (isset($_GET['ub'])) {
if ($_GET['ub'] == 'steel') {
$q->where('level', 'VENDOR');
} elseif ($_GET['ub'] == 'build') {
$q->where('level', '!=', 'VENDOR');
}
}
})
->offset($start)
->limit($limit)
->orderBy($order, $dir)
->get();
}
} else {
$search = $request->input('search.value');
if (Auth::user()->level == 'ARSITEK' || Auth::user()->level == 'DRAFTER' || Auth::user()->level == 'QUANTITY SURVEYOR' || Auth::user()->level == 'SUPERVISI - ALUMNI' || Auth::user()->level == 'VENDOR') {
$rabs = Rab::whereHas('client', function ($q) use ($search) {
$q->where([['clients.fullname','LIKE', "%{$search}%"],['rab.id_user', Auth::user()->id]])
->orWhere([['rab.nama_rab', 'LIKE', "%{$search}%"], ['rab.id_user', Auth::user()->id]]);
})
->offset($start)
->limit($limit)
->orderBy($order, $dir)
->get();
$totalFiltered = Rab::whereHas('client', function ($q) use ($search) {
$q->where([['clients.fullname','LIKE', "%{$search}%"],['rab.id_user', Auth::user()->id]])
->orWhere([['rab.nama_rab', 'LIKE', "%{$search}%"],['rab.id_user', Auth::user()->id]]);
})
->count();
} else {
$rabs = Rab::whereHas('user', function ($q) {
if (isset($_GET['ub'])) {
if ($_GET['ub'] == 'steel') {
$q->where('level', 'VENDOR');
} elseif ($_GET['ub'] == 'build') {
$q->where('level', '!=', 'VENDOR');
}
}
})
->whereHas('client', function ($q) use ($search) {
$q->where('clients.fullname', 'LIKE', "%{$search}%")
->orWhere('rab.nama_rab', 'LIKE', "%{$search}%");
})
->offset($start)
->limit($limit)
->orderBy($order, $dir)
->get();
$totalFiltered = Rab::whereHas('user', function ($q) {
if (isset($_GET['ub'])) {
if ($_GET['ub'] == 'steel') {
$q->where('level', 'VENDOR');
} elseif ($_GET['ub'] == 'build') {
$q->where('level', '!=', 'VENDOR');
}
}
})->whereHas('client', function ($q) use ($search) {
$q->where('clients.fullname', 'LIKE', "%{$search}%")
->orWhere('rab.nama_rab', 'LIKE', "%{$search}%");
})
->count();
}
if (Auth::user()->level == 'ARSITEK' || Auth::user()->level == 'DRAFTER' || Auth::user()->level == 'QUANTITY SURVEYOR' || Auth::user()->level == 'SUPERVISI - ALUMNI' || Auth::user()->level == 'VENDOR') {
$totalFiltered = Rab::whereHas('client', function ($q) use ($search) {
$q->where([['clients.fullname','LIKE', "%{$search}%"],['clients.id_user', Auth::user()->id]])
->orWhere([['rab.nama_rab', 'LIKE', "%{$search}%"],['clients.id_user', Auth::user()->id]]);
})
->count();
} else {
$totalFiltered = Rab::whereHas('user', function ($q) {
if (isset($_GET['ub'])) {
if ($_GET['ub'] == 'steel') {
$q->where('level', 'VENDOR');
} elseif ($_GET['ub'] == 'build') {
$q->where('level', '!=', 'VENDOR');
}
}
})->whereHas('client', function ($q) use ($search) {
$q->where('clients.fullname', 'LIKE', "%{$search}%")
->orWhere('rab.nama_rab', 'LIKE', "%{$search}%");
})
->count();
}
}
$data = array();
if (!empty($rabs) && $rabs->count() > 0) {
foreach ($rabs as $row) {
$nestedData['nama_rab'] = $row->nama_rab;
$nestedData['client'] = $row->client->fullname ?? "";
$nestedData['created_by'] = $row->user->fullname ?? "";
$nestedData['status'] = $row->status;
if (RoleController::access('rab', 'edit') > 0 || RoleController::access('rab', 'delete') > 0) {
$nestedData['opsi'] = "
";
if ($row->status == 'non-approve') {
if (RoleController::access('rab', 'edit') > 0) {
$nestedData['opsi'] .= "
";
}
if (RoleController::access('rab', 'create') > 0) {
$nestedData['opsi'] .= "
";
}
}
if (Auth::user()->level == 'SUPERADMIN' && $row->status == 'approve') {
$nestedData['opsi'] .= "
";
}
if ($row->status == 'approve' && $row->id_project != null) {
if (RoleController::access('rab', 'view') > 0) {
$nestedData['opsi'] .= "
Rekap
";
}
}
if (RoleController::access('rab', 'view') > 0) {
$nestedData['opsi'] .= '
share
';
}
if (RoleController::access('rab', 'view') > 0) {
$nestedData['opsi'] .= "
detail
";
}
if ($row->status == 'non-approve' || Auth::user()->level == 'SUPERADMIN') {
if (RoleController::access('rab', 'delete') > 0) {
$nestedData['opsi'] .= "
";
}
}
} else {
$nestedData['opsi'] = "";
}
$data[] = $nestedData;
}
}
$json_data = array(
"draw" => intval($request->input('draw')),
"recordsTotal" => intval($totalData),
"recordsFiltered" => intval($totalFiltered),
"data" => $data,
);
echo json_encode($json_data);
} elseif ($_GET['act'] == 'load_backup_volume') {
$backup_volume = BackupVolume::findOrFail($id);
$ahsS = AhsKategori::with(['subkategoris' => function ($q) use ($id) {
$q->with(['ahsS' => function ($q2) use ($id) {
$q2->where('status', 'approve')
->with(['backupVolumeDetail' => function ($q3) use ($id) {
$q3->where('id_backup_volume', $id);
}]);
}])->with(['vendors' => function ($q2) use ($id) {
$q2->where('status', 'approve')
->with(['backupVolumeDetail' => function ($q3) use ($id) {
$q3->where('id_backup_volume', $id);
}]);
}]);
}])->where('hidden', 0)->orderBy('urutan', 'asc')->get();
$view = view('staff.rab-client-create-data', compact('backup_volume', 'ahsS', 'backup_volume_details'))->render();
return response()->json(['view' => $view,'backup_volume' => $backup_volume]);
} elseif ($_GET['act'] == 'profit-manual') {
$harga = 0;
if ($request->from === "ahs") {
$ahs = Ahs::find($id);
$harga = $ahs->harga_modal ?? 0;
} else if ($request->from === "ahsv") {
$ahs = AhsVendor::find($id);
$harga = $ahs->vendor->harga_modal ?? 0;
}
$percent_profit = $request->percent_profit / 100;
$profit = $harga * $percent_profit;
return number_format($profit, 0, "", ".");
}
} else {
abort('404');
}
} else {
if (isset($_GET['act'])) {
if ($_GET['act'] == 'print-rab_rekap') {
$rab = Rab::findOrFail($id);
$company = Company::all()->first();
if ($rab->status == 'approve') {
$pdf = PDF::loadView('staff.rab-client-rekap_print', compact('rab', 'company'))->setPaper('a4')->output('rekap-rab-'.$rab->project->name.'.pdf');
return Response::make($pdf, 200, [
'Content-Disposition' => 'inline; filename=rekap-rab-'.$rab->project->name.'.pdf',
'Content-Type' => 'application/pdf',
]);
} else {
abort('404');
}
} elseif ($_GET['act'] == 'print-rab_data') {
$rab = Rab::findOrFail($id);
$company = Company::all()->first();
$rabDetails = AhsKategori::with(['subkategoris.ahsS' => function ($q) use ($id) {
$q->whereHas('rabDetail', function ($q2) use ($id) {
$q2->where('id_rab', $id);
})->with(['rabDetail' => function ($q2) use ($id) {
$q2->where('id_rab', $id);
}]);
}])->with(['subkategoris.vendors' => function ($q) use ($id) {
$q->whereHas('rabDetail', function ($q2) use ($id) {
$q2->where('id_rab', $id);
})->with(['rabDetail' => function ($q2) use ($id) {
$q2->where('id_rab', $id);
}]);
}])->orderBy('urutan', 'asc')->get();
$withPrice = isset($_GET['without_price'])?false:true;
if ($rab->user->level == 'VENDOR') {
$pdf = PDF::loadView('staff.rab-client-data-vendor_print', compact('rab', 'rabDetails', 'withPrice', 'company'))->setPaper('a4')->stream($rab->nama_rab.'.pdf');
} else {
$pdf = PDF::loadView('staff.rab-client-data_print', compact('rab', 'rabDetails', 'withPrice', 'company'))->setPaper('a4')->stream($rab->nama_rab.'.pdf');
}
return Response::make($pdf, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename='.$rab->nama_rab.'.pdf',
]);
} elseif ($_GET['act'] == 'share-rab') {
$rab = Rab::findOrFail($id);
$company = Company::all()->first();
$rabDetails = AhsKategori::with(['subkategoris.ahsS' => function ($q) use ($id) {
$q->whereHas('rabDetail', function ($q2) use ($id) {
$q2->where('id_rab', $id);
})->with(['rabDetail' => function ($q2) use ($id) {
$q2->where('id_rab', $id);
}]);
}])->with(['subkategoris.vendors' => function ($q) use ($id) {
$q->whereHas('rabDetail', function ($q2) use ($id) {
$q2->where('id_rab', $id);
})->with(['rabDetail' => function ($q2) use ($id) {
$q2->where('id_rab', $id);
}]);
}])->orderBy('urutan', 'asc')->get();
$withPrice = isset($_GET['without_price'])?false:true;
$file_name = date('YmdHis');
if ($rab->user->level == 'VENDOR') {
PDF::loadView('staff.rab-client-data-vendor_print', compact('rab', 'rabDetails', 'withPrice', 'company'))->setPaper('a4')->save(public_path() . '/rab-print' . '/' . $file_name . '.pdf');
} else {
PDF::loadView('staff.rab-client-data_print', compact('rab', 'rabDetails', 'withPrice', 'company'))->setPaper('a4')->save(public_path() . '/rab-print' . '/' . $file_name . '.pdf');
}
if (substr($rab->client->phone, 0, 2) == '62') {
return redirect('https://wa.me/'.$rab->client->phone.'?text='. asset('rab-print/' . $file_name . '.pdf'));
} else {
return redirect('https://wa.me/62'.$rab->client->phone.'?text='. asset('rab-print/' . $file_name . '.pdf'));
}
} else {
abort('404');
}
} else {
$rab = Rab::findOrFail($id);
$ahsS = AhsKategori::with(['subkategoris.ahsS' => function ($q) use ($id,$rab) {
$q->where('status', 'approve')
->with(['backupVolumeDetail' => function ($q2) use ($id,$rab) {
$q2->where('id_backup_volume', $rab->id_backup_volume);
}])->whereDoesntHave('rabDetail', function ($q2) use ($id) {
$q2->where('id_rab', $id);
});
}])->with(['subkategoris.vendors' => function ($q) use ($id,$rab) {
$q->where('status', 'approve')
->with(['backupVolumeDetail' => function ($q2) use ($id,$rab) {
$q2->where('id_backup_volume', $rab->id_backup_volume);
}])->whereDoesntHave('rabDetail', function ($q2) use ($id) {
$q2->where('id_rab', $id);
});
}])->where('hidden', 0)->orderBy('urutan', 'asc')->get();
$rabDetails = AhsKategori::with(['subkategoris.ahsS' => function ($q) use ($id,$rab) {
$q->whereHas('rabDetail', function ($q2) use ($id) {
$q2->where('id_rab', $id);
})->with(['rabDetail' => function ($q2) use ($id) {
$q2->where('id_rab', $id);
}])->with(['backupVolumeDetail' => function ($q) use ($rab) {
$q->where('id_backup_volume', $rab->id_backup_volume);
}]);
}])->with(['subkategoris.vendors' => function ($q) use ($id,$rab) {
$q->whereHas('rabDetail', function ($q2) use ($id) {
$q2->where('id_rab', $id);
})->with(['rabDetail' => function ($q2) use ($id) {
$q2->where('id_rab', $id);
}])->with(['backupVolumeDetail' => function ($q) use ($rab) {
$q->where('id_backup_volume', $rab->id_backup_volume);
}]);
}])->orderBy('urutan', 'asc')->get();
return view('staff.rab-client-edit', compact('rab', 'rabDetails', 'ahsS'));
}
}
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
DB::unprepared(' SET FOREIGN_KEY_CHECKS=0');
if ($request->act == 'approve-rab') {
$rab = Rab::findOrFail($id);
$totalharga = RabDetail::selectRaw('sum(subtotal) as totalharga')->where('id_rab', $id)->first()->totalharga;
$rab->status = 'approve';
$rab->harga_deal = $totalharga-$rab->harga_diskon;
$rab->save();
foreach ($rab->rabDetails as $rabD) {
if ($rabD->ahs) {
if ($rabD->ahs->ahsMaterialDetails) {
foreach ($rabD->ahs->ahsMaterialDetails as $row) {
$cek_rab_materials = RabDetailMaterial::where([['id_rab_detail',$rabD->id],['id_material',$row->material->id]])->first();
if (!$cek_rab_materials) {
RabDetailMaterial::create([
'id_rab_detail' => $rabD->id,
'id_material' => $row->material->id,
'qty' => $row->koefisien*$rabD->volume,
'harga' => $row->material->harga
]);
} else {
$cek_rab_materials->qty = $row->koefisien*$rabD->volume;
$cek_rab_materials->harga = $row->material->harga;
$cek_rab_materials->save();
}
if ($row->material->id) {
$crap = Crap::where([['id_rab',$rab->id],['id_material',$row->material->id]])->first();
if ($crap) {
$crap->qty_anggaran = $crap->qty_anggaran + ($row->koefisien*$rabD->volume);
$crap->save();
} else {
Crap::create([
'id_rab' => $rab->id,
'id_material' => $row->material->id,
'qty_anggaran' => $row->koefisien*$rabD->volume,
]);
}
}
}
}
if ($rabD->ahs->ahsUpahDetails) {
foreach ($rabD->ahs->ahsUpahDetails as $row) {
$cek_rab_upahs = RabDetailUpah::where([['id_rab_detail',$rabD->id],['id_upah',$row->upah->id]])->first();
if (!$cek_rab_upahs) {
RabDetailUpah::create([
'id_rab_detail' => $rabD->id,
'id_upah' => $row->upah->id,
'qty' => $row->koefisien*$rabD->volume,
'harga' => $row->upah->upah
]);
} else {
$cek_rab_upahs->qty = $row->koefisien*$rabD->volume;
$cek_rab_upahs->harga = $row->upah->upah;
$cek_rab_upahs->save();
}
$clo = Clo::where([['id_rab',$rab->id],['id_upah',$row->upah->id]])->first();
if ($clo) {
$clo->qty_anggaran = $clo->qty_anggaran + ($row->koefisien*$rabD->volume);
$clo->save();
} else {
Clo::create([
'id_rab' => $rab->id,
'id_upah' => $row->upah->id,
'qty_anggaran' => $row->koefisien*$rabD->volume,
]);
}
}
}
}
}
$rabD = RabDetail::where([['id_rab',$rab->id],['id_ahs_vendor','<>',null]])->get();
if ($rabD) {
# code...
foreach ($rabD as $row) {
$crap = Crap::where([['id_rab',$rab->id],['id_vendor',$row->ahs_vendor->vendor->id]])->first();
if ($crap) {
$crap->qty_anggaran = $crap->qty_anggaran + $row->volume;
$crap->save();
} else {
Crap::create([
'id_rab' => $rab->id,
'id_vendor' => $row->ahs_vendor->vendor->id,
'qty_anggaran' => $row->volume,
]);
}
}
}
$rab_details = RabDetail::where('id_rab', $rab->id)->get();
return redirect(route('rab.index'))->with('msg', 'data berhasil diapprove');
} elseif ($request->act == 'unsubmit-rab') {
$rab = Rab::findOrFail($id);
$rab->status = 'non-approve';
$id_project = $rab->id_project;
$rab->id_project = null;
$rab->save();
Project::destroy($id_project);
Crap::where('id_rab', $id)->delete();
Clo::where('id_rab', $id)->delete();
return redirect(route('rab.index'))->with('msg', 'data berhasil diunsubmit');
} elseif ($request->act == 'item-update') {
$rD = RabDetail::findOrFail($id);
$rD->deskripsi = preg_replace('/[^A-Za-z0-9\s\.,-]/', ' ', $request->deskripsi);
$rD->volume = str_replace(',', '.', $request->volume);
$rD->harga = str_replace('.', '', $request->harga);
$rD->subtotal = str_replace(',', '.', $request->volume) * str_replace('.', '', $request->harga);
$rD->save();
if ($rD->ahs) {
foreach ($rD->ahs->ahsMaterialDetails as $row) {
if ($row->material->id) {
$m = RabDetailMaterial::where([['id_rab_detail',$rD->id],['id_material',$row->material->id]])->first();
if ($m) {
$m->qty = $row->koefisien * $rD->volume;
$m->harga = $row->material->harga;
$m->save();
} else {
RabDetailMaterial::create([
'id_rab_detail' => $rD->id,
'id_material' => $row->material->id,
'qty' => $row->koefisien*$rD->volume,
'harga' => $row->material->harga
]);
}
}
}
foreach ($rD->ahs->ahsUpahDetails as $row) {
if ($row->upah->id) {
$u = RabDetailUpah::where([['id_rab_detail',$rD->id],['id_upah',$row->upah->id]])->first();
if ($u) {
$u->qty = $row->koefisien * $rD->volume;
$u->harga = $row->upah->upah;
$u->save();
} else {
RabDetailUpah::create([
'id_rab_detail' => $rD->id,
'id_upah' => $row->upah->id,
'qty' => $row->koefisien*$rD->volume,
'harga' => $row->upah->upah
]);
}
}
}
}
return redirect()->route('rab.show', $rD->id_rab)->with('msg', 'data berhasil dirubah');
} elseif ($request->act == "set-diskon") {
$rab = Rab::findOrFail($id);
$rab->harga_diskon = $request->harga_diskon?str_replace('.', '', $request->harga_diskon):0;
$rab->diskon = $request->diskon_persen?(float)str_replace(',', '.', $request->diskon_persen):0;
$rab->save();
return redirect()->route('rab.show', $id)->with('msg', 'data berhasil diperbarui');
} elseif ($request->act == 'profit-manual') {
$rab = Rab::findOrFail($id);
$rab->profit_manual = $request->profit_manual;
$rab->save();
$rabDetail = RabDetail::with(['ahs' => function ($q) use ($rab) {
$q->with(['backupVolumeDetail' => function ($q2) use ($rab) {
$q2->where('id_backup_volume', $rab->id_backup_volume);
}]);
}])->with(['ahs_vendor' => function ($q) use ($rab) {
$q->with(['backupVolumeDetail' => function ($q2) use ($rab) {
$q2->where('id_backup_volume', $rab->id_backup_volume);
}]);
}])->where('id_rab', $id)->get();
$percent_profit = $request->profit_manual / 100;
foreach ($rabDetail as $row) {
if ($row->ahs) {
$volume = $row->ahs->backupVolumeDetail?$row->ahs->backupVolumeDetail->volume:$row->ahs->rabDetail->volume;
} elseif ($row->ahs_vendor) {
$volume = $row->ahs_vendor->backupVolumeDetail?$row->ahs_vendor->backupVolumeDetail->volume:$row->ahs_vendor->rabDetail->volume;
}
$harga = 0;
if ($row->id_ahs) {
$harga = $row->ahs->harga_modal;
} else if ($row->id_ahs_vendor) {
$harga = $row->ahs_vendor->vendor->harga_modal;
}
$harga = $harga * $percent_profit;
// $harga = (int)$row->harga * $percent_profit;
RabDetail::where('id', $row->id)->update([
'harga' => $harga,
'subtotal' => $harga * $volume
]);
}
return redirect()->back()->with('msg', 'profit berhasil dirubah');
} else {
if ($request->id_ahs) {
foreach ($request->id_ahs as $key => $value) {
if ($value != null) {
$cek = RabDetail::where([['id_ahs', $value], ['id_rab', $id]])->first();
if (!$cek) {
DB::statement(' SET FOREIGN_KEY_CHECKS=0');
$rabD = RabDetail::create([
'id_rab' => $id,
'id_ahs' => $value,
'deskripsi' => preg_replace('/[^A-Za-z0-9\s\.,-]/', ' ', $request->deskripsi[$key]),
'volume' => str_replace(',', '.', $request->volume[$key]),
'harga' => str_replace('.', '', $request->harga[$key]),
'subtotal' => 0,
]);
if ($rabD) {
$rabD->subtotal = $rabD->volume * $rabD->harga;
$rabD->save();
}
if ($rabD->ahs) {
foreach ($rabD->ahs->ahsMaterialDetails as $row) {
if ($row->material->id) {
$rdm = RabDetailMaterial::create([
'id_rab_detail' => $rabD->id,
'id_material' => $row->material->id,
'qty' => $row->koefisien*$rabD->volume,
'harga' => $row->material->harga
]);
}
}
foreach ($rabD->ahs->ahsUpahDetails as $row) {
if ($row->upah->id) {
RabDetailUpah::create([
'id_rab_detail' => $rabD->id,
'id_upah' => $row->upah->id,
'qty' => $row->koefisien*$rabD->volume,
'harga' => $row->upah->upah
]);
}
}
}
} else {
$cek->volume = str_replace(',', '.', $request->volume[$key]);
$cek->harga = str_replace(',', '.', $request->harga[$key]);
$subtotal = (float)str_replace(',', '.', $request->volume[$key]) * (float)str_replace('.', '', $request->harga[$key]);
$cek->subtotal = $subtotal;
foreach ($cek->ahs->ahsMaterialDetails as $row) {
$cekm = RabDetailMaterial::where([['id_rab_detail',$cek->id],['id_material',$row->id]])->first();
if (!$cekm) {
if ($row->material->id) {
$rdm = RabDetailMaterial::create([
'id_rab_detail' => $id,
'id_material' => $row->material->id,
'qty' => $row->koefisien*$cek->volume,
'harga' => $row->material->harga
]);
}
} else {
$cekm->qty = $row->koefisien*$cek->volume;
$cekm->harga = $row->material->harga;
$cekm->save();
}
}
foreach ($cek->ahs->ahsUpahDetails as $row) {
$ceku = RabDetailUpah::where([['id_rab_detail',$cek->id],['id_upah',$row->id]])->first();
if (!$ceku) {
if ($row->upah->id) {
RabDetailUpah::create([
'id_rab_detail' => $cek->id,
'id_upah' => $row->upah->id,
'qty' => $row->koefisien*$cek->volume,
'harga' => $row->upah->upah
]);
}
} else {
$ceku->qty = $row->koefisien*$cek->volume;
$ceku->harga = $row->upah->upah;
$ceku->save();
}
}
$cek->save();
}
}
}
}
if ($request->id_ahs_vendor) {
foreach ($request->id_ahs_vendor as $key => $value) {
DB::statement(' SET FOREIGN_KEY_CHECKS=0');
$rabD = RabDetail::create([
'id_rab' => $id,
'id_ahs_vendor' => $value,
'deskripsi' => preg_replace('/[^A-Za-z0-9\s\.,-]/', ' ', $request->deskripsi_v[$key]),
'volume' => str_replace(',', '.', $request->volume_v[$key]),
'harga' => str_replace('.', '', $request->harga_v[$key]),
'subtotal' => 0,
]);
if ($rabD) {
$rabD->subtotal = $rabD->volume * $rabD->harga;
$rabD->save();
}
}
}
if ($request->act == 'add-item') {
return redirect(route('rab.show', $id))->with('msg', 'data berhasil ditambahkan');
} else {
return redirect(route('rab.index'))->with('msg', 'data berhasil ditambahkan');
}
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Request $request, $id)
{
if ($request->act == 'deleteRab') {
Rab::destroy($id);
return redirect()->route('rab.index')->with('msg', 'rab berhasil dihapus');
} elseif ($request->act == 'itemDelete') {
$r = RabDetail::findOrFail($id);
$id = $r->id_rab;
$r->delete();
return redirect(route('rab.show', $id))->with('msg', 'item berhasil dihapus');
} else {
abort('404');
}
}
private function _bulantoRomawi($num)
{
if ($num <= 12) {
$ro = $arrayName = array(
1 => 'I',
2 => 'II',
3 => 'III',
4 => 'IV',
5 => 'V',
6 => 'VI',
7 => 'VII',
8 => 'VIII',
9 => 'IX',
10 => 'X',
11 => 'XI',
12 => 'XII');
return $ro[(int) $num];
} else {
dd('jumlah bulan hanya ada 12');
}
}
}