Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function store(Request $request) {
- try {
- // refactor: gunakan request->only untuk mempersingkat assign data
- $data = $request->only([
- 'Type', 'Code', 'Name', 'Category',
- 'Quantity100', 'Quantity90', 'Quantity60', 'Quantity30',
- 'Price100', 'Price90', 'Price60', 'Price30',
- 'Nominal', 'Sisa', 'RefurbishQuantity', 'IsMaintenance',
- 'Picture1', 'Picture2', 'Picture3', 'Picture4',
- 'LegalEntityId', 'BranchId', 'SupplierId',
- 'PaymentType', 'PaymentVia',
- 'SNI1', 'SNI2', 'SNI3',
- 'ProductName', 'ItemMasterId', 'ApprovalCentral',
- 'ProcurementType', 'IsBundle', 'BundleId',
- 'LegalEntityId', 'BranchId' // ambil bendera cabang dr request
- ]);
- // refactor: ubah cara hitung total
- $data['TotalQuantity'] = $data['Quantity100']
- + $data['Quantity90']
- + $data['Quantity60']
- + $data['Quantity30'];
- // refactor: ubah cara hitung total
- $data['TotalPrice'] = ($data['Quantity100'] * $data['Price100'])
- + ($data['Quantity90'] * $data['Price90'])
- + ($data['Quantity60'] * $data['Price60'])
- + ($data['Quantity30'] * $data['Price30']);
- // $data['Total'] = $data['TotalPrice'];
- // konversi ke string
- $data['TotalPrice'] = (string)$data['TotalPrice'];
- // Vitra-RC-263-290 tambah lookup ke item id untuk pengadaan pusat
- // query barang cabang ambil legal entity id dan branch id dr request
- $inventoryBranch = InventoryBranches::where('LegalEntityId', $data['LegalEntityId'])
- ->where('BranchId', $data['BranchId'])
- ->where('ItemMasterId', $data['ItemMasterId'])
- ->first();
- $data['ItemId'] = $inventoryBranch != null ? $inventoryBranch->Id : null;
- $procurement = Procurements::create($data);
- return response()->json(['message' => 'Successfully created']);
- } catch (Exception $exception) {
- return response()->json(['message' => $exception->getMessage()]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement