private function generateCode() { $spendings = Spending::where('project_id', '=', Auth::user()->curr_project_id)->where('location_id', '=', Auth::user()->location_id)->where('spendable_type', '=', 'Classification')->count(); $spendings += 1; $code = Auth::user()->curr_project->code . Auth::user()->location->code . '02' . $spendings; return $code; }
public function create($data) { $currentUser = parent::authenticateUser(); $spending = new Spending(); if (isset($data->quantity) && isset($data->name) && isset($data->date)) { $spending->setDate($data->date); $spending->setQuantity($data->quantity); $spending->setName($data->name); $spending->setOwner($currentUser->getLogin()); try { $idSpending = $this->spendingDAO->save($spending); foreach ($data->types as $type_loop) { $type = $this->typeDAO->findById($type_loop->idType); if ($type == NULL) { $this->spendingDAO->delete($idSpending); header($this->server->getServerProtocol() . ' 400 Bad request'); echo "Type with id " . $type_loop->idType . " not found"; return; } $typeSpending = new TypeSpending(); $typeSpending->setSpending($idSpending); $typeSpending->setType($type); $this->typeSpendingDAO->save($typeSpending); } header($this->server->getServerProtocol() . ' 201 Created'); header('Location: ' . $this->server->getRequestUri() . "/" . $idSpending); header('Content-Type: application/json'); } catch (ValidationException $e) { header($this->server->getServerProtocol() . ' 400 Bad request'); echo json_encode($e->getErrors()); } } }