Pnb.php 3.82 KB
Newer Older
Ghislain Loas's avatar
Ghislain Loas committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/**
 * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
 *
 * BOKEH is free software; you can redistribute it and/or modify
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
 * BOKEH is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
 * along with BOKEH; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 */

class Class_Loan_PnbLoader extends Storm_Model_Loader {
23
  public function findAllOngoingOfUser($user) {
Laurent's avatar
Laurent committed
24
    $loans =
25 26 27 28
      array_unique(
                   array_merge(
                               $this->findAllOngoingBy(['subscriber_id' => $user->getIdabon()]),
                               $this->findAllOngoingBy(['user_id' => $user->getId()])));
29

30
    (new Class_WebService_BibNumerique_Dilicom_Hub())->updateLoansReturnDate($loans);
31

32 33
    $filter = function($loan) {
      return strtotime($loan->getExpectedReturnDate()) > Class_Loan_Pnb::getTimeSource()->time();
34 35
    };

36
    return array_filter($loans, $filter);
37 38 39
  }


40 41 42 43 44
  public function findAllOngoingOfItem($item) {
    return $this->findAllOngoingBy(['order_line_id' => $item->getOrderLineId()]);
  }


45
  public function findAllOngoingBy($params) {
46
    $closure = function($params) { return Class_Loan_Pnb::findAllBy($params); };
47
    return $this->_applyOngoingAndDo($params, $closure);
48 49 50 51 52
  }


  public function findFirstOngoingBy($params) {
    $closure = function($params) { return Class_Loan_Pnb::findFirstBy($params); };
53
    return $this->_applyOngoingAndDo($params, $closure);
54
  }
55 56


57 58 59 60 61 62 63 64 65 66 67
  public function countByOrderLineId($id) {
    return Class_Loan_Pnb::countBy(['order_line_id' => $id]);
  }


  public function countByOngoingOrderLineId($id) {
    $closure = function($params) { return Class_Loan_Pnb::countBy($params); };
    return $this->_applyOngoingAndDo(['order_line_id' => $id], $closure);
  }


68 69 70 71 72
  protected function _applyOngoingAndDo($params, $closure) {
    $prefix = isset($params['where']) ? '(' . $params['where'] . ') and ' : '';
    $params['where'] = $prefix . 'expected_return_date > "' . $this->_getDate() . '"';

    return $closure($params);
73 74 75
  }


76
  protected function _getDate() {
77
    return Class_Loan_Pnb::getTimeSource()->dateFormat('Y-m-d H:i:s');
Patrick Barroca's avatar
Patrick Barroca committed
78
  }
Ghislain Loas's avatar
Ghislain Loas committed
79 80 81
}


82

Ghislain Loas's avatar
Ghislain Loas committed
83
class Class_Loan_Pnb extends Storm_Model_Abstract {
84
  use Trait_TimeSource, Trait_AlbumDelegator;
85

Patrick Barroca's avatar
Patrick Barroca committed
86 87 88
  protected
    $_table_name = 'loan_pnb',
    $_loader_class = 'Class_Loan_PnbLoader';
89

90

Patrick Barroca's avatar
Patrick Barroca committed
91 92 93
  public function getAlbum() {
    return Class_Album::findFirstBy(['id_origine' => $this->getRecordOriginId()]);
  }
94 95


Patrick Barroca's avatar
Patrick Barroca committed
96 97 98
  public function setAlbum($album) {
    return $this->setRecordOriginId($album->getIdOrigine());
  }
99

100

Patrick Barroca's avatar
Patrick Barroca committed
101 102 103
  public function isLate() {
    return false;
  }
104

105

Patrick Barroca's avatar
Patrick Barroca committed
106 107 108
  public function isPNB() {
    return true;
  }
109 110


111 112 113 114
  public function getOrderDate() {
    return ($item = $this->getItem())
      ? $item->getOrderDate()
      : '';
Patrick Barroca's avatar
Patrick Barroca committed
115
  }
116 117


118 119 120 121 122
  public function getItem() {
    foreach(Class_Album_Item::findAllBy(['album_id' => $this->getAlbum()->getId()])
            as $item)
      if ($item->getOrderLineId() == $this->getOrderLineId())
        return $item;
Patrick Barroca's avatar
Patrick Barroca committed
123
  }
124

125

Patrick Barroca's avatar
Patrick Barroca committed
126 127 128
  public function getBibliotheque() {
    return '';
  }
129 130


Patrick Barroca's avatar
Patrick Barroca committed
131 132 133
  public function getDateRetour() {
    return $this->getExpectedReturnDate();
  }
134 135


Patrick Barroca's avatar
Patrick Barroca committed
136 137 138
  public function isRenewable() {
    return false;
  }
139 140


Patrick Barroca's avatar
Patrick Barroca committed
141 142 143
  public function getType() {
    return '';
  }
144 145 146 147 148


  public function getIssueDate() {
    return $this->getLoanDate();
  }
Ghislain Loas's avatar
Ghislain Loas committed
149
}