def renew_loan(request, item_id): item = WFItem.get(item_id) # TODO: get days from config days = 7 start = item.loan.end end = get_business_day(start + datetime.timedelta(days=days)) actual_days = (end - start).days if item.reserves and item.reserves[0].start < end: return jsonfy(item=item_id, error="Se encuentra reservado") old_end = item.loan.end item.loan.end = end if not item.loan.renew_count: item.loan.renew_count = 0 item.loan.renew_count += 1 item.save() CirculationLog(type='renew', date=old_end, length=actual_days, item_id=item_id, item_type=item.item_type, loan_type=item.loan.type, user_id=item.loan.user_id).save() return jsonfy(item=item_id, status='loan renewed')
def loan(request, what, user_id, item_id, loan_type=None): """ Make a loan/reserve """ # If date it's a reserve else it's a loan date = request.GET.get("date", None) user = User.get(user_id) if not user: return jsonfy(error='user not found') # TODO: get days from config days = 7 if loan_type == 'room': days = 1 if date: date = format_date(date, '%d/%m/%Y') start = get_business_day(date) start = get_business_day(datetime.date.today()) end = get_business_day(start + datetime.timedelta(days=days)) actual_days = (end - start).days requested_item = WFItem.get(item_id) # TODO: re do everything to use one item #urn = requested_item.urn #if not urn: # return jsonfy(error='invalid urn') #items = WFItem.view('couchflow/by_urn', # include_docs=True, startkey=urn, endkey=urn).all() items = [requested_item] loanable_items = [] for item in items: if not item.inventory_nbr: print "inventory_nbr is None" continue # it's a reserve and item have a reserve # and it only must support 1 reserve per item if date and item.reserves: print 'No se puede reservar un item reservado' continue # it's a loan and item have a loan if not date and item.loan.start: print 'No se puede prestar un item prestado' continue # if nave no reserve and no loan it can # be given to anyone, so empty the list # and make it the only option if not item.reserves and not item.loan.start: loanable_items = [item] print "ideal, se puede prestar o reservar" break # if it's a loan, and item has not a loan and if (date and not item.reserves): loanable_items.append(item) # if it's a loan, and item has not a loan and # no fit in start and reserve if (not date and not item.loan.start): item_days = (item.reserves[0].start, item.reserves[0].end) new_start, new_end = get_hole([item_days], start, days) if new_start != start and new_end != end: loanable_items = [item] break # TODO: return json error # can't loan a item that is loaned if not loanable_items: all_dates = [] for item in items: if item.loan.start and not item.reserves: all_dates.append(item.loan.end) elif item.reserves: all_dates.append(item.reserves[0].end) if not all_dates: return jsonfy(item=item_id, error="no se encuentra disponible") max_date = get_business_day(min(all_dates) + datetime.timedelta(1)) max_date = max_date.strftime("%d/%m/%Y") error = 'No se pudo encontrar disponibilidad, '\ 'se calcula disponibilidad para el "%s"' % max_date return jsonfy(item=item_id, error=error) # if its a loan and there is only one # nothing else needed, dates and item is set if not date and len(loanable_items): item = loanable_items[0] # reserve an just one item elif date and len(loanable_items) == 1: item = loanable_items[0] # if there's a loan get the closest date if item.loan.start: item_days = (item.loan.start, item.loan.end) start, end = get_hole([item_days], start, days) # else you can make the reserve when asked else: print item.loan print item.reserves print 'IDEAL!' # there are more than one posible reserve, get the best else: loanable_dates = [] for loanable in loanable_items: item_days = (loanable.loan.start, loanable.loan.end) new_start, new_end = get_hole([item_days], start, days) loanable_dates.append((new_start, new_end, loanable)) loanable_dates.sort(key=lambda i: i[0]) start, end, item = loanable_dates[0] if not date: loan = Loan(start=start, end=end, user_id=user_id, type=loan_type) item.loan = loan CirculationLog(type='loan', date=start, length=actual_days, item_type=item.item_type, loan_type=loan_type, item_id=item._id, user_id=user_id).save() else: reserve = Reserve(start=start, end=end, user_id=user_id) item.reserves.append(reserve) item.save() return jsonfy(item=item._id, status='loaned')