public function transferToSmallest($id_item, $id_unit, $quantity) { $itemData = Item::find($id_item); if (empty($itemData->smallest_id_unit)) { DB::rollback(); throw $this->response->error('unrecorded_smallest_item', 500); } $itemUnitData = ItemUnit::where('id_item', $id_item)->where('id_unit', $id_unit)->first(); if (!empty($itemUnitData)) { if (!empty($itemUnitData->quantity)) { return ['id_item' => $id_item, 'id_unit' => $itemData->smallest_id_unit, 'quantity' => $itemUnitData->quantity * $quantity]; } else { DB::rollback(); throw $this->response->error('transfer_table_still_empty', 500); } } else { DB::rollback(); throw $this->response->error('unrecorded_unit', 500); } }
public function updateJumlahKonversi($data) { if (Request::has('konversi')) { $id_item = $data['id']; $variable = Request::get('konversi'); $price_buy = Request::get('price_buy'); $price_sell = Request::get('price_sell'); $price_rent = Request::get('price_rent'); foreach ($variable as $key => $value) { $model = new ItemUnit(); $data = $model->where('id_item', $id_item)->where('id_unit', $key)->count(); if ($data <= 0) { continue; } $model = new ItemUnit(); $data = $model->where('id_item', $id_item)->where('id_unit', $key); // set params for update $params = ['quantity' => $key != Request::get('smallest_id_unit') ? $value : '1', 'price_buy' => $price_buy[$key], 'price_sell' => $price_sell[$key], 'price_rent' => $price_rent[$key]]; if (!$data->update($params)) { DB::rollback(); throw $this->response->error(trans('response.update_failed:konversi'), 404); } } } }