Dashboard sipadu mbip
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

InventoryController.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. namespace App\Http\Controllers\api;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Api\BaseController;
  5. use Config;
  6. use File;
  7. use Carbon\Carbon;
  8. use App\Model\Staff;
  9. use App\Model\StaffDetail;
  10. use App\Model\Module\Roles;
  11. use App\Model\Module\Compound;
  12. use App\Model\Module\ConfidentialFile;
  13. use App\Model\Module\ItemInventory;
  14. use App\Model\Module\Barcode;
  15. use App\Model\Module\Attachment;
  16. use App\Model\Module\History;
  17. use App\Model\Module\SubHistory;
  18. use App\Jobs\GenerateBarcode;
  19. class InventoryController extends BaseController
  20. {
  21. /**
  22. * Create generate kpd number controller.
  23. *
  24. * @return value
  25. */
  26. public function iventoryList($no_siri){
  27. $nested_data = array();
  28. $file = ConfidentialFile::with(['ItemInventory' => function($q){
  29. $q->with('Attachment','Barcode')->orderBy('created_at','DESC');
  30. }])->where('no_siri',$no_siri)->first();
  31. $curr = Carbon::now()->getTimestamp();
  32. $i = 1;
  33. foreach($file->ItemInventory as $i)
  34. {
  35. $n1 = '';
  36. $reg_time = $i->updated_at;
  37. $expiry_date = $reg_time->addDays(3);
  38. $expiry_date = $expiry_date->getTimestamp();
  39. if($curr < $expiry_date) {
  40. $n1 = "Baru/";
  41. }else{
  42. $n1 = "";
  43. }
  44. $lokasi = '-';
  45. if($i->lokasi_gudang != '-'){
  46. $lokasi = $i->lokasi_gudang;
  47. }
  48. array_push($nested_data, array(
  49. 'tarikh' => $i->created_at->format('d/m/Y'),
  50. 'masa' => $i->created_at->format('h:i a'),
  51. 'jenis' => $i->jenis,
  52. 'bilangan' => $i->bilangan,
  53. 'harga' => $i->harga,
  54. 'lokasi' => $lokasi,
  55. 'status' => $i->status,
  56. 'tindakan' => $i->_id
  57. ));
  58. }
  59. return \DataTables::of($nested_data)->make(true);
  60. }
  61. public function getDetail(Request $request){
  62. $empty_attach = false; $empty_barcode = false;
  63. $itemI = ItemInventory::with('Attachment','Barcode')->where('_id',$request->id)->first();
  64. if(!empty($itemI)){
  65. $data = [
  66. '_id' => $itemI->_id,
  67. 'kategori' => $itemI->kategori,
  68. 'jenis' => $itemI->jenis,
  69. 'bilangan' => $itemI->bilangan,
  70. 'harga' => $itemI->harga,
  71. 'lokasi' => $itemI->lokasi_gudang,
  72. 'status' => $itemI->status
  73. ];
  74. return response()->json(['status' => 'true', 'desc' => $data]);
  75. }else{
  76. return response()->json(['status' => 'false', 'desc' => 'Rekod item ( '.$itemI->jenis.' ) tidak ditemui']);
  77. }
  78. }
  79. public function deleteItem(Request $request){
  80. $empty_attach = false; $empty_barcode = false;
  81. $itemI = ItemInventory::with('Attachment','Barcode')->where('_id',$request->id)->first();
  82. if(!empty($itemI)){
  83. $user = Staff::with('StaffDetail')->where('_id', $request->_id)->first();
  84. $compound = Compound::with('ConfidentialFile')->where('kpd', $request->kpd)->first();
  85. if(!empty($itemI->Attachment)){
  86. foreach($itemI->Attachment as $a){
  87. $a->delete();
  88. }
  89. $empty_attach = true;
  90. }else{
  91. $empty_attach = true;
  92. }
  93. if(!empty($itemI->Barcode)){
  94. foreach($itemI->Barcode as $b){
  95. $b->delete();
  96. }
  97. $empty_barcode = true;
  98. }else{
  99. $empty_barcode = true;
  100. }
  101. if($empty_attach == true && $empty_barcode == true){
  102. $itemI->delete();
  103. $now = Carbon::now();
  104. $gDate = $now->format('F Y');
  105. $historyData = [
  106. 'tarikh_kumpulan' => $gDate,
  107. ];
  108. $subHistory = [
  109. 'no_siri' => $compound->ConfidentialFile->no_siri,
  110. 'tajuk' => "Pembuangan item inventori",
  111. 'huraian' => "Rekod Item inventori telah dibuang oleh ".$user->StaffDetail->roles_access." <a href='".url('/main/staff')."/".$user->_id."/profile'>".$user->StaffDetail->full_name."</a>",
  112. ];
  113. $groupByDate = History::where('tarikh_kumpulan', $gDate)->first();
  114. if(!empty($groupByDate)){
  115. $groupByDate->subhistory()->create($subHistory);
  116. $compound->ConfidentialFile->history()->attach($groupByDate);
  117. }else{
  118. $history = History::create($historyData);
  119. $history->subhistory()->create($subHistory);
  120. $compound->ConfidentialFile->history()->attach($history);
  121. }
  122. return response()->json(['status' => 'true', 'desc' => 'Berjaya buang rekod item ( '.$itemI->jenis.' )']);
  123. }else{
  124. return response()->json(['status' => 'false', 'desc' => 'Tidak berjaya buang rekod item ( '.$itemI->jenis.' )']);
  125. }
  126. }else{
  127. return response()->json(['status' => 'false', 'desc' => 'Rekod item tidak ditemui']);
  128. }
  129. }
  130. /**
  131. * Create generate kpd number controller.
  132. *
  133. * @return value
  134. */
  135. public function iventoryListMobile(Request $request){
  136. $nested_data = array();
  137. $staff = Staff::with('StaffDetail')->where('api_token', $request->api_token)->first();
  138. if(!empty($staff)){
  139. $file = ConfidentialFile::with(['ItemInventory' => function($q){
  140. $q->with('Attachment','Barcode')->orderBy('created_at','DESC');
  141. }])->where('no_siri',$request->no_siri)->first();
  142. $curr = Carbon::now()->getTimestamp();
  143. $i = 1;
  144. foreach($file->ItemInventory as $i) {
  145. $n1 = '';
  146. $reg_time = $i->updated_at;
  147. $expiry_date = $reg_time->addDays(3);
  148. $expiry_date = $expiry_date->getTimestamp();
  149. if($curr < $expiry_date) {
  150. $n1 = "Baru/";
  151. }else{
  152. $n1 = "";
  153. }
  154. $lokasi = '-';
  155. if($i->lokasi_gudang != '-'){
  156. $lokasi = $i->lokasi_gudang;
  157. }
  158. array_push($nested_data, array(
  159. 'tarikh' => $i->created_at->format('d/m/Y'),
  160. 'masa' => $i->created_at->format('h:i a'),
  161. 'jenis' => $i->jenis,
  162. 'bilangan' => $i->bilangan,
  163. 'harga' => $i->harga,
  164. 'lokasi' => $lokasi,
  165. 'status' => $i->status,
  166. 'tindakan' => $i->_id
  167. ));
  168. }
  169. return $this->sendResponse($nested_data, 'Berjaya dapatkan senarai inventori');
  170. }else{
  171. return $this->sendError('', 'Tidak ada rekod ditemui');
  172. }
  173. }
  174. public function addItemMobile(Request $request){
  175. $data = array();
  176. $staff = Staff::with('StaffDetail')->where('api_token', $request->api_token)->first();
  177. if(!empty($staff)){
  178. $compound = Compound::with('ConfidentialFile')->where('kpd', $request->kpd)->first();
  179. if(!empty($compound)){
  180. $item = [
  181. 'kategori' => 'alih',
  182. 'jenis' => $request->get('jenis'),
  183. 'bilangan' => $request->get('bilangan'),
  184. 'harga' => $request->get('harga'),
  185. 'lokasi_gudang' => $request->get('lokasi'),
  186. 'status' => 'simpan'
  187. ];
  188. $itemI = ItemInventory::create($item);
  189. array_push($data, $itemI);
  190. $compound->ConfidentialFile->iteminventory()->save($itemI);
  191. if($request->has('barcode')) {
  192. $temp_barcode = explode(',', $request->barcode);
  193. foreach ($temp_barcode as $key => $b) {
  194. $this->dispatch(new GenerateBarcode($request->kpd, $b, $itemI));
  195. }
  196. }
  197. $now = Carbon::now();
  198. $gDate = $now->format('F Y');
  199. $historyData = [
  200. 'tarikh_kumpulan' => $gDate,
  201. ];
  202. $subHistory = [
  203. 'no_siri' => $compound->ConfidentialFile->no_siri,
  204. 'tajuk' => "Ada penambahan item inventori dari penguatkuasa",
  205. 'huraian' => "Item inventori telah ditambah oleh ".$staff->StaffDetail->roles_access." <a href='".url('/main/staff')."/".$staff->_id."/profile'>".$staff->StaffDetail->full_name."</a>",
  206. ];
  207. $groupByDate = History::where('tarikh_kumpulan', $gDate)->first();
  208. if(!empty($groupByDate)){
  209. $groupByDate->subhistory()->create($subHistory);
  210. $compound->ConfidentialFile->history()->attach($groupByDate);
  211. }else{
  212. $history = History::create($historyData);
  213. $history->subhistory()->create($subHistory);
  214. $compound->ConfidentialFile->history()->attach($history);
  215. }
  216. return $this->sendResponse($data, 'Berjaya simpan item');
  217. }else{
  218. return $this->sendError('', 'Tiada rekod ditemui');
  219. }
  220. }
  221. else{
  222. return $this->sendError('', 'Kakitangan tidak ditemui');
  223. }
  224. }
  225. public function addItemPictureMobile(Request $request){
  226. $staff = Staff::with('StaffDetail')->where('api_token', $request->api_token)->first();
  227. if(!empty($staff)){
  228. $itemI = ItemInventory::where('_id', $request->_id)->first();
  229. if(!empty($itemI)){
  230. $compound = Compound::with('ConfidentialFile')->where('kpd', $request->kpd)->first();
  231. if(!empty($compound)){
  232. $success_upload = 0; $count_upload = 0;
  233. if($request->hasFile('file')) {
  234. $i = 1;
  235. $destinationGambarItem = 'document/'.$compound->ConfidentialFile->no_siri.'/item';
  236. $uploaded = public_path().'/'.$destinationGambarItem;
  237. if(!File::isDirectory($destinationGambarItem)){
  238. File::makeDirectory($destinationGambarItem, 0777, true, true);
  239. }
  240. $allowedfileExtension = ['jpg','jpeg'];
  241. $destinationPathPicture = array();
  242. $count_upload = count($request->file('file'));
  243. foreach($request->file('file') as $f){
  244. $extension = $f->getClientOriginalExtension();
  245. $check=in_array($extension,$allowedfileExtension);
  246. if($check) {
  247. $name = 'Item-'.$i++.'.'.$f->getClientOriginalExtension();
  248. $destinationPathPicture[] = '/'.$destinationGambarItem.'/'.$name;
  249. $f->move($destinationGambarItem, $name);
  250. $attach = new Attachment();
  251. $attach->path = '/'.$destinationGambarItem.'/'.$name;
  252. $itemI->attachment()->save($attach);
  253. $i++;
  254. }
  255. }
  256. return $this->sendResponse($destinationPathPicture, $success_upload.'/'.$count_upload.' picture compound successfully been uploaded');
  257. }else{
  258. return $this->sendError('', 'Tidak ada fail dimuatnaik');
  259. }
  260. }else{
  261. return $this->sendError('', 'Tidak ada rekod ditemui');
  262. }
  263. }else{
  264. return $this->sendError('', 'Tidak ada rekod ditemui');
  265. }
  266. }
  267. else{
  268. return $this->sendError('', 'Tidak ada rekod ditemui');
  269. }
  270. }
  271. public function deleteItemMobile(Request $request){
  272. $empty_attach = false; $empty_barcode = false;
  273. $staff = Staff::with('StaffDetail')->where('api_token', $request->api_token)->first();
  274. if(!empty($staff)){
  275. $compound = Compound::with('ConfidentialFile')->where('kpd', $request->kpd)->first();
  276. $itemI = ItemInventory::with('Attachment','Barcode')->where('_id',$request->id)->first();
  277. if(!empty($itemI)){
  278. if(!empty($itemI->Attachment)){
  279. foreach($itemI->Attachment as $a){
  280. $a->delete();
  281. }
  282. $empty_attach = true;
  283. }else{
  284. $empty_attach = true;
  285. }
  286. if(!empty($itemI->Barcode)){
  287. foreach($itemI->Barcode as $b){
  288. $b->delete();
  289. }
  290. $empty_barcode = true;
  291. }else{
  292. $empty_barcode = true;
  293. }
  294. if($empty_attach == true && $empty_barcode == true){
  295. $itemI->delete();
  296. $now = Carbon::now();
  297. $gDate = $now->format('F Y');
  298. $historyData = [
  299. 'tarikh_kumpulan' => $gDate,
  300. ];
  301. $subHistory = [
  302. 'no_siri' => $compound->ConfidentialFile->no_siri,
  303. 'tajuk' => "Pembuangan item inventori",
  304. 'huraian' => "Rekod Item inventori telah dibuang oleh ".$staff->StaffDetail->roles_access." <a href='".url('/main/staff')."/".$staff->_id."/profile'>".$staff->StaffDetail->full_name."</a>",
  305. ];
  306. $groupByDate = History::where('tarikh_kumpulan', $gDate)->first();
  307. if(!empty($groupByDate)){
  308. $groupByDate->subhistory()->create($subHistory);
  309. $compound->ConfidentialFile->history()->attach($groupByDate);
  310. }else{
  311. $history = History::create($historyData);
  312. $history->subhistory()->create($subHistory);
  313. $compound->ConfidentialFile->history()->attach($history);
  314. }
  315. return $this->sendResponse('','Buang rekod item ( '.$itemI->jenis.' )');
  316. }else{
  317. return $this->sendError('','Buang rekod item ( '.$itemI->jenis.' )');
  318. }
  319. }else{
  320. return $this->sendError('','Rekod item tidak ditemui');
  321. }
  322. }
  323. }
  324. public function getDetailMobile(Request $request){
  325. $data = array();
  326. $staff = Staff::with('StaffDetail')->where('api_token', $request->api_token)->first();
  327. if(!empty($staff)){
  328. $barcode = Barcode::where('barcode_id',$request->barcode)->first();
  329. if(!empty($barcode)){
  330. $itemI = ItemInventory::with('Attachment')->where('_id',$barcode->item_inventory_id)->first();
  331. if(!empty($itemI)){
  332. array_push($data, $itemI);
  333. return $this->sendResponse($data, 'Rekod item ditemui');
  334. }else{
  335. return $this->sendError('','Rekod item ditemui');
  336. }
  337. }else{
  338. return $this->sendError('','Barcode tidak ditemui');
  339. }
  340. }else{
  341. return $this->sendError('','Kakitangan tidak ditemui');
  342. }
  343. }
  344. }