| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Scriptotek\Alma\Bibs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Scriptotek\Alma\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Scriptotek\Alma\Conf\Library; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Scriptotek\Alma\Model\LazyResource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Scriptotek\Alma\Users\Loan; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Scriptotek\Alma\Users\Requests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Scriptotek\Alma\Users\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class Item extends LazyResource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** @var Bib */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public $bib; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** @var Holding */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public $holding; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** @var Requests */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public $requests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public $item_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * Item constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param Client  $client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param Bib     $bib | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param Holding $holding | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param $item_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function __construct(Client $client, Bib $bib, Holding $holding, $item_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         parent::__construct($client); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->bib = $bib; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->holding = $holding; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->item_id = $item_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->requests = Requests::make($this->client, $this->url('/requests')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * Generate the base URL for this resource. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     protected function urlBase() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return "/bibs/{$this->bib->mms_id}/holdings/{$this->holding->holding_id}/items/{$this->item_id}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * Check if we have the full representation of our data object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param \stdClass $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     protected function isInitialized($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         return isset($data->item_data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * Called when data is available to be processed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param mixed $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     protected function onData($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         if (isset($this->bib_data)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $this->bib->init($this->bib_data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if (isset($this->holding_data)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $this->holding->init($this->holding_data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * Create a new loan. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param User    $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param Library $library | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param string  $circ_desk | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @throws \Scriptotek\Alma\Exception\RequestFailed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @return Loan | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public function checkOut(User $user, Library $library, $circ_desk = 'DEFAULT_CIRC_DESK') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $postData = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             'library'   => ['value' => $library->code], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             'circ_desk' => ['value' => $circ_desk], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $data = $this->client->postJSON( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             $this->url('/loans', ['user_id' => $user->id]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $postData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         return Loan::make($this->client, $user, $data->loan_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             ->init($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * Perform scan-in on item. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @param Library $library | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @param string  $circ_desk | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @param array   $params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @throws \Scriptotek\Alma\Exception\RequestFailed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @return ScanInResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     public function scanIn(Library $library, $circ_desk = 'DEFAULT_CIRC_DESK', $params = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $params['op'] = 'scan'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $params['library'] = $library->code; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $params['circ_desk'] = $circ_desk; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $data = $this->client->postJSON($this->url('', $params)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         return ScanInResponse::make($this->client, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * Get the current loan as a Loan object, or null if the item is not loaned out. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @returns Loan|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     public function getLoan() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $data = $this->client->getJSON($this->url('/loans')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         if ($data->total_record_count == 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             return Loan::make( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                 $this->client, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 User::make($this->client, $data->item_loan[0]->user_id), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                 $data->item_loan[0]->loan_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             )->init($data->item_loan[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |     public function __get($key) | 
            
                                                        
            
                                    
            
            
                | 148 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 149 |  |  |         if ($key == 'loan') { | 
            
                                                        
            
                                    
            
            
                | 150 |  |  |             return $this->getLoan(); | 
            
                                                        
            
                                    
            
            
                | 151 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 153 |  |  |         $this->init(); | 
            
                                                        
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 155 |  |  |         if (isset($this->data->item_data->{$key})) { | 
            
                                                        
            
                                    
            
            
                | 156 |  |  |             return $this->data->item_data->{$key}; | 
            
                                                        
            
                                    
            
            
                | 157 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 158 |  |  |         if (isset($this->data->holding_data->{$key})) { | 
            
                                                        
            
                                    
            
            
                | 159 |  |  |             return $this->data->holding_data->{$key}; | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 161 |  |  |         if (isset($this->data->bib_data->{$key})) { | 
            
                                                        
            
                                    
            
            
                | 162 |  |  |             return $this->data->bib_data->{$key}; | 
            
                                                        
            
                                    
            
            
                | 163 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 165 |  |  |         return parent::__get($key); | 
            
                                                        
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 167 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 168 |  |  |  |