示例#1
0
 def validate_cancel(self, status, cancel_reason):
     """Validate decline is correct."""
     if status == "CANCELLED" and not cancel_reason:
         raise ILLError(
             "You have to provide a cancel reason when cancelling a request"
         )
     if cancel_reason and not status == "CANCELLED":
         raise ILLError("If you select a cancel reason you need to select"
                        ' "Cancelled" in the state')
示例#2
0
    def create(
        self,
        start_date,
        end_date,
        transaction_location_pid=None,
        transaction_user_pid=None,
    ):
        """Create a new loan for the patron out of this borrowing request.

        The newly created loan PID is added to the borrowing request and the
        status is changed to "ON_LOAN".
        It is the responsibility of the caller to ensure that the current
        borrowing request does not already have a loan attached and the initial
        status is valid.

        :param start_date: the start date of the loan.
        :param end_date: the end date of the loan.
        :param transaction_location_pid: the location pid of the transaction
            that will be stored in the loan. If not passed, the current one
            will be used.
        :param transaction_user_pid: the user pid of the transaction
            that will be stored in the loan. If not passed, the current logged
            in user id will be used.
        :return: the PID and record loan
        """
        rec = self.record

        tloc_pid = transaction_location_pid
        if not tloc_pid:
            pid_value, _ = current_app_ils.get_default_location_pid
            tloc_pid = pid_value

        transaction_user_pid = transaction_user_pid or str(current_user.id)
        if arrow.get(end_date) < arrow.now():
            raise ILLError("The loan end date cannot be in the past.")

        item_pid = dict(type=rec._pid_type, value=rec["pid"])
        pid, loan = checkout_loan(
            start_date=start_date,
            end_date=end_date,
            document_pid=rec["document_pid"],
            item_pid=item_pid,
            patron_pid=rec["patron_pid"],
            transaction_location_pid=tloc_pid,
            transaction_user_pid=str(transaction_user_pid),
        )
        rec.setdefault("patron_loan", {})
        rec["patron_loan"]["pid"] = pid.pid_value
        rec["status"] = "ON_LOAN"
        return pid, loan
示例#3
0
    def create_loan(
        self,
        start_date=arrow.utcnow(),
        transaction_location_pid=None,
        transaction_user_pid=None,
    ):
        """Create a new loan out of this borrowing request.

        The newly created loan PID is added to the borrowing request and the
        status is changed to "ON_LOAN".
        It is the responsibility of the caller to ensure that the current
        borrowing request does not already have a loan attached and the initial
        status is valid.

        :param start_date: the start date of the loan. Defaults to now.
        :param transaction_location_pid: the location pid of the transaction
            that will be stored in the loan. If not passed, the current one
            will be used.
        :param transaction_user_pid: the user pid of the transaction
            that will be stored in the loan. If not passed, the current logged
            in user id will be used.
        :return: the PID and record loan
        """
        tloc_pid = (transaction_location_pid
                    or current_app.config["ILS_DEFAULT_LOCATION_PID"])
        transaction_user_pid = transaction_user_pid or current_user.id
        if arrow.get(self["loan_end_date"]) < arrow.now():
            raise ILLError("The loan end date cannot be in the past.")

        item_pid = dict(type=self._pid_type, value=self["pid"])
        pid, loan = checkout_loan(
            start_date=start_date,
            end_date=self["loan_end_date"],
            document_pid=self["document_pid"],
            item_pid=item_pid,
            patron_pid=self["patron_pid"],
            transaction_location_pid=tloc_pid,
            transaction_user_pid=str(transaction_user_pid),
        )
        self["loan_pid"] = pid.pid_value
        self["status"] = "ON_LOAN"
        return pid, loan
示例#4
0
 def get(self):
     """Return the loan for this borrowing request."""
     loan_pid = self.record.get("patron_loan", {}).get("pid")
     if not loan_pid:
         raise ILLError("There is no patron loan attached to the request.")
     return Loan.get_record_by_pid(loan_pid)