Advertisement
vitareinforce

fungsi store

Nov 9th, 2023
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. public function store(Request $request) {
  2.  
  3. try {
  4. // refactor: gunakan request->only untuk mempersingkat assign data
  5. $data = $request->only([
  6. 'Type', 'Code', 'Name', 'Category',
  7. 'Quantity100', 'Quantity90', 'Quantity60', 'Quantity30',
  8. 'Price100', 'Price90', 'Price60', 'Price30',
  9. 'Nominal', 'Sisa', 'RefurbishQuantity', 'IsMaintenance',
  10. 'Picture1', 'Picture2', 'Picture3', 'Picture4',
  11. 'LegalEntityId', 'BranchId', 'SupplierId',
  12. 'PaymentType', 'PaymentVia',
  13. 'SNI1', 'SNI2', 'SNI3',
  14. 'ProductName', 'ItemMasterId', 'ApprovalCentral',
  15. 'ProcurementType', 'IsBundle', 'BundleId',
  16. 'LegalEntityId', 'BranchId' // ambil bendera cabang dr request
  17. ]);
  18.  
  19. // refactor: ubah cara hitung total
  20. $data['TotalQuantity'] = $data['Quantity100']
  21. + $data['Quantity90']
  22. + $data['Quantity60']
  23. + $data['Quantity30'];
  24.  
  25. // refactor: ubah cara hitung total
  26. $data['TotalPrice'] = ($data['Quantity100'] * $data['Price100'])
  27. + ($data['Quantity90'] * $data['Price90'])
  28. + ($data['Quantity60'] * $data['Price60'])
  29. + ($data['Quantity30'] * $data['Price30']);
  30.  
  31. // $data['Total'] = $data['TotalPrice'];
  32.  
  33. // konversi ke string
  34. $data['TotalPrice'] = (string)$data['TotalPrice'];
  35.  
  36. // Vitra-RC-263-290 tambah lookup ke item id untuk pengadaan pusat
  37. // query barang cabang ambil legal entity id dan branch id dr request
  38. $inventoryBranch = InventoryBranches::where('LegalEntityId', $data['LegalEntityId'])
  39. ->where('BranchId', $data['BranchId'])
  40. ->where('ItemMasterId', $data['ItemMasterId'])
  41. ->first();
  42. $data['ItemId'] = $inventoryBranch != null ? $inventoryBranch->Id : null;
  43.  
  44. $procurement = Procurements::create($data);
  45.  
  46. return response()->json(['message' => 'Successfully created']);
  47.  
  48. } catch (Exception $exception) {
  49. return response()->json(['message' => $exception->getMessage()]);
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement