public static function findLoanItem($id) { $loanItem = LoanItem::find($id); // $loanItem->loan = $loanItem->loan()->first(); // unset($loanItem->loan_id); // // $loanItem->loan->user = $loanItem->loan->user()->first(); // unset($loanItem->loan->user_id); // // $loanItem->copy = $loanItem->copy()->first(); // unset($loanItem->copy_id); // // $loanItem->copy->work = $loanItem->copy->work()->first(); // unset($loanItem->copy->work_id); return $loanItem; }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $now = new DateTime(); //new DateTime((new DateTime())->format('Y-m-d 00:00:00')); if ($request->user() != null) { if ($request->user()->hasRole(array('admin', 'librarian'))) { $loanItem = LoanItem::findLoanItem($request->get('loan_item_id')); // if (is_null($loanItem->returned_at) || empty($loanItem->returned_at) || $loanItem->returned_at == 'null' || $loanItem->returned_at == 'NULL') { // $dateDiff = $now->diff($now); // } else { // $dt2 = new DateTime(substr($loanItem->returned_at, 0, 10) . ' 00:00:00'); // $dateDiff = $now->diff(new DateTime($dt2->format('Y-m-d H:i:s'))); // } // return array($dateDiff->days, $dateDiff->invert); // if ($dateDiff->invert == 0 && $dateDiff->days > 0) { $loanItem->returned_at = $now->format('Y-m-d H:i:s'); if ($loanItem->save()) { $copy = Copy::find($loanItem->copy_id); $copy->status = Copy::AVAILABLE; if ($copy->save()) { return array($loanItem); } else { return array('erro ao devolver o exemplar'); } } else { return array('error ao salvar o loan item'); } // } else { // return array('erro, data returned_at de loan item esta invalida'); // } } else { return array('usuario nao possui permissao'); } } else { return array('usuario nao logado'); } }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $daysToAdd = 10; if ($request->user() != null) { if ($request->user()->hasRole(array('admin', 'librarian'))) { $loanItem = LoanItem::findLoanItem($request->get('loan_item_id')); $loanItem->return_prevision = date('Y-m-d H:i:s', strtotime($loanItem->return_prevision . ' + ' . $daysToAdd . ' days')); if ($loanItem->save()) { $renewLoanItem = (new RenewLoanItem())->store(array('loan_item_id' => $loanItem->id, 'return_prevision' => $loanItem->return_prevision)); if ($renewLoanItem->save()) { return $renewLoanItem; } else { return array('erro ao salvar o renew loan item'); } } else { return array('erro ao salvar o return prevision de loan item'); } } else { return array('usuario nao possui permissao'); } } else { return array('usuario nao logado'); } }