123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- <?php
-
- namespace App\Http\Controllers\Officer;
-
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\Auth;
- use LynX39\LaraPdfMerger\Facades\PdfMerger;
-
- use Carbon\Carbon;
- use PDF;
-
- use App\SiteSetting;
- use App\Model\Staff;
- use App\Model\StaffDetail;
- use App\Model\User;
- use App\Model\UserDetail;
- use App\Model\Module\Roles;
- use App\Model\Module\Compound;
- use App\Model\Module\ConfidentialFile;
- use App\Model\Module\ItemInventory;
- use App\Model\Module\Barcode;
- use App\Model\Module\Attachment;
- use App\Model\Module\History;
- use App\Model\Module\SubHistory;
-
- use App\Jobs\GenerateBarcode;
-
- class InventoryController extends Controller
- {
- /**
- * Create Inventory ID controller.
- *
- * @return json
- */
- public function generateBR($uniq_id,$range){
- $allowedNumbers = range(0, 9);
- shuffle($allowedNumbers);
- $digits = array_rand($allowedNumbers, $range);
-
- $number = '';
- foreach($digits as $d){
- $number .= $allowedNumbers[$d];
- }
-
- return $uniq_id.$number;
- }
-
- /**
- * Create Inventory list controller.
- *
- * @return json
- */
- public function addItem(Request $request){
-
- $id = Auth::guard('ofr')->id();
- $user = Staff::with('StaffDetail')->find($id);
-
- $compound = Compound::with('ConfidentialFile')->where('kpd', $request->kpd)->first();
- if(!empty($compound)){
-
- $item = [
- 'kategori' => $request->get('kategori'),
- 'jenis' => $request->get('jenis'),
- 'bilangan' => $request->get('bilangan'),
- 'harga' => $request->get('harga'),
- 'lokasi_gudang' => $request->get('lokasi'),
- 'status' => 'simpan'
- ];
-
- $itemI = ItemInventory::create($item);
- $compound->ConfidentialFile->iteminventory()->save($itemI);
-
- $l = 0;
- while($l != $request->get('bilangan')) {
- $name = '';
- do {
- $name = $this->generateBR('BR', 5);
- } while (Barcode::where("barcode_id", "=", $name)->first() instanceof Barcode);
-
- $this->dispatch(new GenerateBarcode($request->kpd, $name, $itemI));
- $l++;
- }
-
- $now = Carbon::now();
- $gDate = $now->format('F Y');
-
- $historyData = [
- 'tarikh_kumpulan' => $gDate,
- ];
- $subHistory = [
- 'no_siri' => $compound->ConfidentialFile->no_siri,
- 'tajuk' => "Ada penambahan item inventori dari 'dashboard'",
- 'huraian' => "Item inventori telah ditambah oleh ".$user->StaffDetail->roles_access." <a href='".url('/main/staff')."/".$user->_id."/profile'>".$user->StaffDetail->full_name."</a>",
- ];
-
- $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', '<strong>Berjaya!</strong> Rekod item inventori berjaya ditambah');
- }else{
- return redirect()->back()->with('error_msg', '<strong>Tidak Berjaya!</strong> Rekod kertas kompaun tidak ditemui');
- }
- }
-
- public function editItem(Request $request){
-
- $id = Auth::guard('ofr')->id();
- $user = Staff::with('StaffDetail')->find($id);
-
- $data_barcode_empty = false;
- $compound = Compound::with('ConfidentialFile')->where('kpd', $request->kpd)->first();
- $itemI = ItemInventory::with('Attachment','Barcode')->where('_id',$request->_id)->first();
- if(!empty($itemI)){
-
- if($request->has('lokasi')){
- $itemI->jenis = $request->get('jenis');
- $itemI->bilangan = $request->get('bilangan');
- $itemI->harga = $request->get('harga');
- $itemI->lokasi_gudang = $request->get('lokasi');
- $itemI->save();
- }else{
- $itemI->jenis = $request->get('jenis');
- $itemI->bilangan = $request->get('bilangan');
- $itemI->harga = $request->get('harga');
- $itemI->save();
- }
-
- if(!empty($itemI->Barcode)){
- foreach ($itemI->Barcode as $b) {
- $b->forceDelete();
- }
-
- $data_barcode_empty = true;
- }
-
- if($data_barcode_empty == true){
- $l = 0;
- while($l != $request->get('bilangan')) {
- $name = $this->generateBR('BR', 5);
- $this->dispatch(new GenerateBarcode($request->kpd, $name, $itemI));
- $l++;
- }
- }
-
- $now = Carbon::now();
- $gDate = $now->format('F Y');
-
- $historyData = [
- 'tarikh_kumpulan' => $gDate,
- ];
- $subHistory = [
- 'no_siri' => $compound->ConfidentialFile->no_siri,
- 'tajuk' => "Item inventori dikemaskini",
- 'huraian' => "Item inventori diekmaskini oleh ".$user->StaffDetail->roles_access." <a href='".url('/main/staff')."/".$user->_id."/profile'>".$user->StaffDetail->full_name."</a>",
- ];
-
- $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', '<strong>Berjaya!</strong> Kemaskini Rekod item inventori');
-
- }else{
- return redirect()->back()->with('error_msg', '<strong>Tidak Berjaya!</strong> Rekod item tidak ditemui');
- }
- }
-
- public function requestViewInventoryPdf($kpd) {
-
- $compound = Compound::with('ConfidentialFile')->where('kpd', $kpd)->first();
- $file = ConfidentialFile::with(['ItemInventory' => function($q){
- $q->with('Attachment','Barcode')->orderBy('created_at','DESC');
- }])->where('no_siri',$compound->ConfidentialFile->no_siri)->first();
-
- $site = SiteSetting::first();
- $now = Carbon::now()->format('d/m/Y h:i:s A');
-
- $pdf = PDF::loadView('pdf.inventory', compact('compound','file','now','site'));
- $pdf->setPaper('A4', 'potrait');
- return $pdf->stream();
- }
- }
|