Dashboard sipadu mbip
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

InventoryController.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace App\Http\Controllers\Main;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use Illuminate\Support\Facades\Auth;
  6. use LynX39\LaraPdfMerger\Facades\PdfMerger;
  7. use Carbon\Carbon;
  8. use PDF;
  9. use App\SiteSetting;
  10. use App\Model\Staff;
  11. use App\Model\StaffDetail;
  12. use App\Model\User;
  13. use App\Model\UserDetail;
  14. use App\Model\Module\Roles;
  15. use App\Model\Module\Compound;
  16. use App\Model\Module\ConfidentialFile;
  17. use App\Model\Module\ItemInventory;
  18. use App\Model\Module\Barcode;
  19. use App\Model\Module\Attachment;
  20. use App\Model\Module\History;
  21. use App\Model\Module\SubHistory;
  22. use App\Jobs\GenerateBarcode;
  23. class InventoryController extends Controller
  24. {
  25. /**
  26. * Create Inventory ID controller.
  27. *
  28. * @return json
  29. */
  30. public function generateBR($uniq_id,$range){
  31. $allowedNumbers = range(0, 9);
  32. shuffle($allowedNumbers);
  33. $digits = array_rand($allowedNumbers, $range);
  34. $number = '';
  35. foreach($digits as $d){
  36. $number .= $allowedNumbers[$d];
  37. }
  38. return $uniq_id.$number;
  39. }
  40. /**
  41. * Create Inventory list controller.
  42. *
  43. * @return json
  44. */
  45. public function addItem(Request $request){
  46. $id = Auth::guard('sadmin')->id();
  47. $user = Staff::with('StaffDetail')->find($id);
  48. $compound = Compound::with('ConfidentialFile')->where('kpd', $request->kpd)->first();
  49. if(!empty($compound)){
  50. $item = [
  51. 'kategori' => $request->get('kategori'),
  52. 'jenis' => $request->get('jenis'),
  53. 'bilangan' => $request->get('bilangan'),
  54. 'harga' => $request->get('harga'),
  55. 'lokasi_gudang' => $request->get('lokasi'),
  56. 'status' => 'simpan'
  57. ];
  58. $itemI = ItemInventory::create($item);
  59. $compound->ConfidentialFile->iteminventory()->save($itemI);
  60. $l = 0;
  61. while($l != $request->get('bilangan')) {
  62. $name = '';
  63. do {
  64. $name = $this->generateBR('BR', 5);
  65. } while (Barcode::where("barcode_id", "=", $name)->first() instanceof Barcode);
  66. $this->dispatch(new GenerateBarcode($request->kpd, $name, $itemI));
  67. $l++;
  68. }
  69. $now = Carbon::now();
  70. $gDate = $now->format('F Y');
  71. $historyData = [
  72. 'tarikh_kumpulan' => $gDate,
  73. ];
  74. $subHistory = [
  75. 'no_siri' => $compound->ConfidentialFile->no_siri,
  76. 'tajuk' => "Ada penambahan item inventori dari 'dashboard'",
  77. 'huraian' => "Item inventori telah ditambah oleh ".$user->StaffDetail->roles_access." <a href='".url('/main/staff')."/".$user->_id."/profile'>".$user->StaffDetail->full_name."</a>",
  78. ];
  79. $groupByDate = History::where('tarikh_kumpulan', $gDate)->first();
  80. if(!empty($groupByDate)){
  81. $groupByDate->subhistory()->create($subHistory);
  82. $compound->ConfidentialFile->history()->attach($groupByDate);
  83. }else{
  84. $history = History::create($historyData);
  85. $history->subhistory()->create($subHistory);
  86. $compound->ConfidentialFile->history()->attach($history);
  87. }
  88. return redirect()->back()->with('success_msg', '<strong>Berjaya!</strong> Rekod item inventori berjaya ditambah');
  89. }else{
  90. return redirect()->back()->with('error_msg', '<strong>Tidak Berjaya!</strong> Rekod kertas kompaun tidak ditemui');
  91. }
  92. }
  93. public function editItem(Request $request){
  94. $id = Auth::guard('sadmin')->id();
  95. $user = Staff::with('StaffDetail')->find($id);
  96. $data_barcode_empty = false;
  97. $compound = Compound::with('ConfidentialFile')->where('kpd', $request->kpd)->first();
  98. $itemI = ItemInventory::with('Attachment','Barcode')->where('_id',$request->_id)->first();
  99. if(!empty($itemI)){
  100. if($request->has('lokasi')){
  101. $itemI->jenis = $request->get('jenis');
  102. $itemI->bilangan = $request->get('bilangan');
  103. $itemI->harga = $request->get('harga');
  104. $itemI->lokasi_gudang = $request->get('lokasi');
  105. $itemI->save();
  106. }else{
  107. $itemI->jenis = $request->get('jenis');
  108. $itemI->bilangan = $request->get('bilangan');
  109. $itemI->harga = $request->get('harga');
  110. $itemI->save();
  111. }
  112. if(!empty($itemI->Barcode)){
  113. foreach ($itemI->Barcode as $b) {
  114. $b->forceDelete();
  115. }
  116. $data_barcode_empty = true;
  117. }
  118. if($data_barcode_empty == true){
  119. $l = 0;
  120. while($l != $request->get('bilangan')) {
  121. $name = $this->generateBR('BR', 5);
  122. $this->dispatch(new GenerateBarcode($request->kpd, $name, $itemI));
  123. $l++;
  124. }
  125. }
  126. $now = Carbon::now();
  127. $gDate = $now->format('F Y');
  128. $historyData = [
  129. 'tarikh_kumpulan' => $gDate,
  130. ];
  131. $subHistory = [
  132. 'no_siri' => $compound->ConfidentialFile->no_siri,
  133. 'tajuk' => "Item inventori dikemaskini",
  134. 'huraian' => "Item inventori diekmaskini oleh ".$user->StaffDetail->roles_access." <a href='".url('/main/staff')."/".$user->_id."/profile'>".$user->StaffDetail->full_name."</a>",
  135. ];
  136. $groupByDate = History::where('tarikh_kumpulan', $gDate)->first();
  137. if(!empty($groupByDate)){
  138. $groupByDate->subhistory()->create($subHistory);
  139. $compound->ConfidentialFile->history()->attach($groupByDate);
  140. }else{
  141. $history = History::create($historyData);
  142. $history->subhistory()->create($subHistory);
  143. $compound->ConfidentialFile->history()->attach($history);
  144. }
  145. return redirect()->back()->with('success_msg', '<strong>Berjaya!</strong> Kemaskini Rekod item inventori');
  146. }else{
  147. return redirect()->back()->with('error_msg', '<strong>Tidak Berjaya!</strong> Rekod item tidak ditemui');
  148. }
  149. }
  150. public function requestViewInventoryPdf($kpd) {
  151. $compound = Compound::with('ConfidentialFile')->where('kpd', $kpd)->first();
  152. $file = ConfidentialFile::with(['ItemInventory' => function($q){
  153. $q->with('Attachment','Barcode')->orderBy('created_at','DESC');
  154. }])->where('no_siri',$compound->ConfidentialFile->no_siri)->first();
  155. $now = Carbon::now()->format('d/m/Y h:i:s A');
  156. $site = SiteSetting::first();
  157. $pdf = PDF::loadView('pdf.inventory', compact('compound','file','now','site'));
  158. $pdf->setPaper('A4', 'potrait');
  159. return $pdf->stream();
  160. }
  161. }