diff --git a/app/controllers/LoansController.php b/app/controllers/LoansController.php index 06da1bc..96cb25f 100644 --- a/app/controllers/LoansController.php +++ b/app/controllers/LoansController.php @@ -302,7 +302,7 @@ public function getSync() $due = array(); header("Content-type: text/html; charset=utf-8"); echo "Starter sync...
\n"; - $ncip = new NcipClient(); + $ncip = App::make('NcipClient'); $guest_ltid = Config::get('app.guest_ltid'); foreach (Loan::with('document','user')->get() as $loan) { @@ -337,6 +337,31 @@ public function getSync() echo "
\n"; } } + echo "
\n"; + + $users = array(); + foreach (User::with('loans.document.thing')->get() as $user) { + foreach ($user->loans as $loan) { + if ($loan->as_guest && $loan->document->thing->id == 1) { + $ltid = $user->ltid; + if (!empty($ltid)) { + echo "Checking loan $loan->id for $ltid : "; + if (!isset($users[$ltid])) { + $response = $ncip->lookupUser($ltid); + $users[$ltid] = $response; + } + if ($users[$ltid]->exists) { + echo " user has been imported, attempt to transfer loan "; + $loan->transfer(); + } else { + echo " nope, not yet imported"; + } + echo "
\n"; + } + } + } + } + exit(); } diff --git a/app/controllers/UsersController.php b/app/controllers/UsersController.php index bdcbe2f..07b15da 100644 --- a/app/controllers/UsersController.php +++ b/app/controllers/UsersController.php @@ -80,7 +80,6 @@ public function getNcipLookup($id) return Response::json(array('exists' => false)); } $data = $user->ncipLookup(); - $data['exists'] = true; return Response::json($data); } diff --git a/app/models/Loan.php b/app/models/Loan.php index 2aa543e..2d48adb 100644 --- a/app/models/Loan.php +++ b/app/models/Loan.php @@ -164,4 +164,23 @@ public function restore() return true; } -} \ No newline at end of file + public function transfer() + { + if ($this->as_guest) { + $dokid = $this->document->dokid; + $ltid = $this->user->ltid; + Log::info('Attempting to transfer ' . $dokid . ' to ' . $ltid); + $ncip = App::make('NcipClient'); + $ncip->checkInItem($dokid); + $response = $ncip->checkOutItem($ltid, $dokid); + if ($response->success) { + $this->as_guest = false; + $this->save(); + } else { + die($response->error); + } + } + return true; + } + +}