def insert_reserve_from_form(dbo, username, post): """ Inserts a movement from the workflow reserve an animal screen. Returns the new movement id """ # Validate that we have a date before doing anthing l = dbo.locale if None is post.date("reservationdate"): raise utils.ASMValidationError(i18n._("Reservations must have a valid reservation date.", l)) # Do the movement itself first move_dict = { "person" : post["person"], "animal" : post["animal"], "reservationdate" : post["reservationdate"], "reservationstatus" : post["reservationstatus"], "adoptionno" : post["movementnumber"], "movementdate" : "", "type" : str(NO_MOVEMENT), "donation" : post["amount"], "returncategory" : configuration.default_return_reason(dbo) } movementid = insert_movement_from_form(dbo, username, utils.PostedData(move_dict, l)) # Create any payments financial.insert_donations_from_form(dbo, username, post, post["reservationdate"], False, post["person"], post["animal"], movementid) return movementid
def insert_retailer_from_form(dbo, username, post): """ Inserts a retailer from the workflow move to retailer screen. Returns the new movement id """ # Validate that we have a movement date before doing anthing l = dbo.locale if None is post.date("retailerdate"): raise utils.ASMValidationError(i18n._("Retailer movements must have a valid movement date.", l)) # Is this animal already at a foster? If so, return that foster first fm = get_animal_movements(dbo, post.integer("animal")) for m in fm: if m.MOVEMENTTYPE == FOSTER and m.RETURNDATE is None: return_movement(dbo, m.ID, post.integer("animal"), post.date("retailerdate")) # Create the retailer movement move_dict = { "person" : post["person"], "animal" : post["animal"], "movementdate" : post["retailerdate"], "adoptionno" : post["movementnumber"], "type" : str(RETAILER), "donation" : post["amount"], "returncategory" : configuration.default_return_reason(dbo) } movementid = insert_movement_from_form(dbo, username, utils.PostedData(move_dict, l)) return movementid
def insert_transfer_from_form(dbo, username, post): """ Inserts a movement from the workflow transfer an animal screen. Returns the new movement id """ # Validate that we have a movement date before doing anthing l = dbo.locale if None == post.date("transferdate"): raise utils.ASMValidationError(i18n._("Transfers must have a valid transfer date.", l)) # Is this animal already on foster? If so, return that foster first fm = get_animal_movements(dbo, post.integer("animal")) for m in fm: if m["MOVEMENTTYPE"] == FOSTER and m["RETURNDATE"] is None: return_movement(dbo, m["ID"], post.integer("animal"), post.date("transferdate")) # Create the transfer movement move_dict = { "person" : post["person"], "animal" : post["animal"], "adoptionno" : post["movementnumber"], "movementdate" : post["transferdate"], "type" : str(TRANSFER), "donation" : post["amount"], "returncategory" : configuration.default_return_reason(dbo) } movementid = insert_movement_from_form(dbo, username, utils.PostedData(move_dict, l)) return movementid
def insert_reserve_for_animal_name(dbo, username, personid, animalname): """ Creates a reservation for the animal with animalname to personid. animalname can either be just the name of a shelter animal, or it can be in the form name::code. If a code is present, that will be used to locate the animal. If the person is banned from adopting animals, an exception is raised. """ l = dbo.locale if animalname.find("::") != -1: animalcode = animalname.split("::")[1] aid = dbo.query_int("SELECT ID FROM animal WHERE ShelterCode = ? ORDER BY ID DESC", [animalcode]) else: aid = dbo.query_int("SELECT ID FROM animal WHERE LOWER(AnimalName) LIKE ? ORDER BY ID DESC", [animalname.lower()]) if 1 == dbo.query_int("SELECT IsBanned FROM owner WHERE ID=?", [personid]): raise utils.ASMValidationError("owner %s is banned from adopting animals - not creating reserve") if aid == 0: raise utils.ASMValidationError("could not find an animal for '%s' - not creating reserve" % animalname) move_dict = { "person" : str(personid), "animal" : str(aid), "reservationdate" : i18n.python2display(l, dbo.today()), "reservationstatus" : configuration.default_reservation_status(dbo), "movementdate" : "", "type" : str(NO_MOVEMENT), "returncategory" : configuration.default_return_reason(dbo) } return insert_movement_from_form(dbo, username, utils.PostedData(move_dict, l))
def insert_transfer_from_form(dbo, username, post): """ Inserts a movement from the workflow transfer an animal screen. Returns the new movement id """ # Validate that we have a movement date before doing anthing l = dbo.locale if None == post.date("transferdate"): raise utils.ASMValidationError( i18n._("Transfers must have a valid transfer date.", l)) # Is this animal already on foster? If so, return that foster first fm = get_animal_movements(dbo, post.integer("animal")) for m in fm: if m["MOVEMENTTYPE"] == FOSTER and m["RETURNDATE"] is None: return_movement(dbo, m["ID"], post.integer("animal"), post.date("transferdate")) # Create the transfer movement move_dict = { "person": post["person"], "animal": post["animal"], "adoptionno": post["movementnumber"], "movementdate": post["transferdate"], "type": str(TRANSFER), "donation": post["amount"], "returncategory": configuration.default_return_reason(dbo) } movementid = insert_movement_from_form(dbo, username, utils.PostedData(move_dict, l)) return movementid
def insert_reserve_from_form(dbo, username, post): """ Inserts a movement from the workflow reserve an animal screen. Returns the new movement id """ # Validate that we have a date before doing anthing l = dbo.locale if None == post.date("reservationdate"): raise utils.ASMValidationError(i18n._("Reservations must have a valid reservation date.", l)) # Do the movement itself first move_dict = { "person" : post["person"], "animal" : post["animal"], "reservationdate" : post["reservationdate"], "reservationstatus" : post["reservationstatus"], "adoptionno" : post["movementnumber"], "movementdate" : "", "type" : str(NO_MOVEMENT), "donation" : post["amount"], "returncategory" : configuration.default_return_reason(dbo) } movementid = insert_movement_from_form(dbo, username, utils.PostedData(move_dict, l)) # Create any payments financial.insert_donations_from_form(dbo, username, post, post["reservationdate"], False, post["person"], post["animal"], movementid) return movementid
def insert_reserve_for_animal_name(dbo, username, personid, animalname): """ Creates a reservation for the animal with animalname to personid. animalname can either be just the name of a shelter animal, or it can be in the form name::code. If a code is present, that will be used to locate the animal. """ l = dbo.locale if animalname.find("::") != -1: animalcode = animalname.split("::")[1] aid = db.query_int(dbo, "SELECT ID FROM animal WHERE ShelterCode = %s ORDER BY ID DESC" % db.ds(animalcode)) else: aid = db.query_int(dbo, "SELECT ID FROM animal WHERE LOWER(AnimalName) LIKE '%s' ORDER BY ID DESC" % animalname.lower()) # Bail out if we couldn't find a matching animal if aid == 0: return move_dict = { "person" : str(personid), "animal" : str(aid), "reservationdate" : i18n.python2display(l, i18n.now(dbo.timezone)), "reservationstatus" : configuration.default_reservation_status(dbo), "movementdate" : "", "type" : str(NO_MOVEMENT), "returncategory" : configuration.default_return_reason(dbo) } return insert_movement_from_form(dbo, username, utils.PostedData(move_dict, l))
def insert_retailer_from_form(dbo, username, data): """ Inserts a retailer from the workflow move to retailer screen. Returns the new movement id """ # Validate that we have a movement date before doing anthing l = dbo.locale if None == utils.df_kd(data, "retailerdate", l): raise utils.ASMValidationError(i18n._("Retailer movements must have a valid movement date.", l)) # Is this animal already at a foster? If so, return that foster first fm = get_animal_movements(dbo, utils.df_ki(data, "animal")) for m in fm: if m["MOVEMENTTYPE"] == FOSTER and m["RETURNDATE"] is None: return_movement(dbo, m["ID"], utils.df_ki(data, "animal"), utils.df_kd(data, "retailerdate", l)) # Create the retailer movement move_dict = { "person" : utils.df_ks(data, "person"), "animal" : utils.df_ks(data, "animal"), "movementdate" : utils.df_ks(data, "retailerdate"), "adoptionno" : utils.df_ks(data, "movementnumber"), "type" : str(RETAILER), "donation" : utils.df_ks(data, "amount"), "returncategory" : configuration.default_return_reason(dbo) } movementid = insert_movement_from_form(dbo, username, move_dict) return movementid
def insert_retailer_from_form(dbo, username, data): """ Inserts a retailer from the workflow move to retailer screen. Returns the new movement id """ # Validate that we have a movement date before doing anthing l = dbo.locale if None == utils.df_kd(data, "retailerdate", l): raise utils.ASMValidationError( i18n._("Retailer movements must have a valid movement date.", l)) # Is this animal already at a foster? If so, return that foster first fm = get_animal_movements(dbo, utils.df_ki(data, "animal")) for m in fm: if m["MOVEMENTTYPE"] == FOSTER and m["RETURNDATE"] is None: return_movement(dbo, m["ID"], utils.df_ki(data, "animal"), utils.df_kd(data, "retailerdate", l)) # Create the retailer movement move_dict = { "person": utils.df_ks(data, "person"), "animal": utils.df_ks(data, "animal"), "movementdate": utils.df_ks(data, "retailerdate"), "adoptionno": utils.df_ks(data, "movementnumber"), "type": str(RETAILER), "donation": utils.df_ks(data, "amount"), "returncategory": configuration.default_return_reason(dbo) } movementid = insert_movement_from_form(dbo, username, move_dict) return movementid
def insert_reserve_for_animal_name(dbo, username, personid, animalname): """ Creates a reservation for the animal with animalname to personid """ l = dbo.locale aid = db.query_int( dbo, "SELECT ID FROM animal WHERE LOWER(AnimalName) LIKE '%s' ORDER BY ID DESC" % animalname.lower()) # Bail out if an invalid animal name was given - we can't create the reservation if aid == 0: return move_dict = { "person": str(personid), "animal": str(aid), "reservationdate": i18n.python2display(l, i18n.now(dbo.timezone)), "movementdate": "", "type": str(NO_MOVEMENT), "returncategory": configuration.default_return_reason(dbo) } return insert_movement_from_form(dbo, username, utils.PostedData(move_dict, l))
def insert_reserve_from_form(dbo, username, data): """ Inserts a movement from the workflow reserve an animal screen. Returns the new movement id """ # Validate that we have a date before doing anthing l = dbo.locale if None == utils.df_kd(data, "reservationdate", l): raise utils.ASMValidationError(i18n._("Reservations must have a valid reservation date.", l)) # Do the movement itself first move_dict = { "person" : utils.df_ks(data, "person"), "animal" : utils.df_ks(data, "animal"), "reservationdate" : utils.df_ks(data, "reservationdate"), "adoptionno" : utils.df_ks(data, "movementnumber"), "movementdate" : "", "type" : str(NO_MOVEMENT), "donation" : utils.df_ks(data, "amount"), "returncategory" : configuration.default_return_reason(dbo) } movementid = insert_movement_from_form(dbo, username, move_dict) # Then the donation if we have one donation_amount = int(utils.df_m(data, "amount", l)) if donation_amount > 0: due = "" received = utils.df_ks(data, "reservationdate") if configuration.movement_donations_default_due(dbo): due = utils.df_ks(data, "reservationdate") received = "" don_dict = { "person" : utils.df_ks(data, "person"), "animal" : utils.df_ks(data, "animal"), "movement" : str(movementid), "type" : utils.df_ks(data, "donationtype"), "payment" : utils.df_ks(data, "payment"), "frequency" : "0", "amount" : utils.df_ks(data, "amount"), "due" : due, "received" : received, "giftaid" : utils.df_ks(data, "giftaid") } financial.insert_donation_from_form(dbo, username, don_dict) # And a second donation if there is one donation_amount = int(utils.df_m(data, "amount2", l)) if donation_amount > 0: due = "" received = utils.df_ks(data, "movementdate") if configuration.movement_donations_default_due(dbo): due = utils.df_ks(data, "movementdate") received = "" don_dict = { "person" : utils.df_ks(data, "person"), "animal" : utils.df_ks(data, "animal"), "movement" : str(movementid), "type" : utils.df_ks(data, "donationtype2"), "payment" : utils.df_ks(data, "payment2"), "frequency" : "0", "amount" : utils.df_ks(data, "amount2"), "due" : due, "received" : received, "giftaid" : utils.df_ks(data, "giftaid") } financial.insert_donation_from_form(dbo, username, don_dict) return movementid
def insert_adoption_from_form(dbo, username, data, creating = []): """ Inserts a movement from the workflow adopt an animal screen. Returns the new movement id creating is an ongoing list of animals we're already going to create adoptions for. It prevents a never ending recursive loop of animal1 being bonded to animal2 that's bonded to animal1, etc. """ l = dbo.locale # Validate that we have a movement date before doing anthing if None == utils.df_kd(data, "movementdate", l): raise utils.ASMValidationError(i18n._("Adoption movements must have a valid adoption date.", l)) # Get the animal record for this adoption a = animal.get_animal(dbo, utils.df_ki(data, "animal")) if a is None: raise utils.ASMValidationError("Adoption POST has an invalid animal ID: %d" % utils.df_ki(data, "animal")) al.debug("Creating adoption for %d (%s - %s)" % (a["ID"], a["SHELTERCODE"], a["ANIMALNAME"]), "movement.insert_adoption_from_form", dbo) creating.append(a["ID"]) # If the animal is bonded to other animals, we call this function # again with a copy of the data and the bonded animal substituted # so we can create their adoption records too. if a["BONDEDANIMALID"] is not None and a["BONDEDANIMALID"] != 0 and a["BONDEDANIMALID"] not in creating: al.debug("Found bond to animal %d, creating adoption..." % a["BONDEDANIMALID"], "movement.insert_adoption_from_form", dbo) newdata = dict(data) newdata["animal"] = str(a["BONDEDANIMALID"]) insert_adoption_from_form(dbo, username, newdata, creating) if a["BONDEDANIMAL2ID"] is not None and a["BONDEDANIMAL2ID"] != 0 and a["BONDEDANIMAL2ID"] not in creating: al.debug("Found bond to animal %d, creating adoption..." % a["BONDEDANIMAL2ID"], "movement.insert_adoption_from_form", dbo) newdata = dict(data) newdata["animal"] = str(a["BONDEDANIMAL2ID"]) insert_adoption_from_form(dbo, username, newdata, creating) cancel_reserves = configuration.cancel_reserves_on_adoption(dbo) # Prepare a dictionary of data for the movement table via insert_movement_from_form move_dict = { "person" : utils.df_ks(data, "person"), "animal" : utils.df_ks(data, "animal"), "adoptionno" : utils.df_ks(data, "movementnumber"), "movementdate" : utils.df_ks(data, "movementdate"), "type" : str(ADOPTION), "donation" : utils.df_ks(data, "amount"), "insurance" : utils.df_ks(data, "insurance"), "returncategory" : configuration.default_return_reason(dbo), "trial" : utils.df_ks(data, "trial"), "trialenddate" : utils.df_ks(data, "trialenddate") } # Is this animal currently on foster? If so, return the foster fm = get_animal_movements(dbo, utils.df_ki(data, "animal")) for m in fm: if m["MOVEMENTTYPE"] == FOSTER and m["RETURNDATE"] is None: return_movement(dbo, m["ID"], utils.df_ki(data, "animal"), utils.df_kd(data, "movementdate", l)) # Is this animal current at a retailer? If so, return it from the # retailer and set the originalretailermovement and retailerid fields # on our new adoption movement so it can be linked back for m in fm: if m["MOVEMENTTYPE"] == RETAILER and m["RETURNDATE"] is None: return_movement(dbo, m["ID"], utils.df_ki(data, "animal"), utils.df_kd(data, "movementdate", l)) move_dict["originalretailermovement"] = str(m["ID"]) move_dict["retailer"] = str(m["OWNERID"]) # Did we say we'd like to flag the owner as homechecked? if utils.df_kc(data, "homechecked") == 1: db.execute(dbo, "UPDATE owner SET IDCheck = 1, DateLastHomeChecked = %s WHERE ID = %d" % \ ( db.dd(i18n.now(dbo.timezone)), utils.df_ki(data, "person"))) # If the animal was flagged as not available for adoption, then it # shouldn't be since we've just adopted it. db.execute(dbo, "UPDATE animal SET IsNotAvailableForAdoption = 0 WHERE ID = %s" % utils.df_ks(data, "animal")) # Is the animal reserved to the person adopting? movementid = 0 for m in fm: if m["MOVEMENTTYPE"] == NO_MOVEMENT and m["RESERVATIONDATE"] is not None \ and m["RESERVATIONCANCELLEDDATE"] is None and m["ANIMALID"] == utils.df_ki(data, "animal") \ and m["OWNERID"] == utils.df_ki(data, "person"): # yes - update the existing movement movementid = m["ID"] move_dict["movementid"] = str(movementid) move_dict["adoptionno"] = utils.padleft(movementid, 6) move_dict["reservationdate"] = str(i18n.python2display(l, m["RESERVATIONDATE"])) move_dict["comments"] = utils.nulltostr(m["COMMENTS"]) break elif cancel_reserves and m["MOVEMENTTYPE"] == NO_MOVEMENT and m["RESERVATIONDATE"] is not None \ and m["RESERVATIONCANCELLEDDATE"] is None: # no, but it's reserved to someone else and we're cancelling # reserves on adoption db.execute(dbo, "UPDATE adoption SET ReservationCancelledDate = %s WHERE ID = %d" % \ ( utils.df_d(data, "movementdate", l), m["ID"] )) if movementid != 0: update_movement_from_form(dbo, username, move_dict) else: movementid = insert_movement_from_form(dbo, username, move_dict) # Create the donation if there is one donation_amount = int(utils.df_m(data, "amount", l)) if donation_amount > 0: due = "" received = utils.df_ks(data, "movementdate") if configuration.movement_donations_default_due(dbo): due = utils.df_ks(data, "movementdate") received = "" don_dict = { "person" : utils.df_ks(data, "person"), "animal" : utils.df_ks(data, "animal"), "movement" : str(movementid), "type" : utils.df_ks(data, "donationtype"), "payment" : utils.df_ks(data, "payment"), "frequency" : "0", "amount" : utils.df_ks(data, "amount"), "due" : due, "received" : received, "giftaid" : utils.df_ks(data, "giftaid") } financial.insert_donation_from_form(dbo, username, don_dict) # And a second donation if there is one donation_amount = int(utils.df_m(data, "amount2", l)) if donation_amount > 0: due = "" received = utils.df_ks(data, "movementdate") if configuration.movement_donations_default_due(dbo): due = utils.df_ks(data, "movementdate") received = "" don_dict = { "person" : utils.df_ks(data, "person"), "animal" : utils.df_ks(data, "animal"), "movement" : str(movementid), "type" : utils.df_ks(data, "donationtype2"), "payment" : utils.df_ks(data, "payment2"), "frequency" : "0", "amount" : utils.df_ks(data, "amount2"), "due" : due, "received" : received, "giftaid" : utils.df_ks(data, "giftaid") } financial.insert_donation_from_form(dbo, username, don_dict) # Then any boarding cost record cost_amount = int(utils.df_m(data, "costamount", l)) cost_type = utils.df_ks(data, "costtype") cost_create = utils.df_ki(data, "costcreate") if cost_amount > 0 and cost_type != "" and cost_create == 1: boc_dict = { "animalid" : utils.df_ks(data, "animal"), "type" : cost_type, "costdate" : utils.df_ks(data, "movementdate"), "cost" : utils.df_ks(data, "costamount") } animal.insert_cost_from_form(dbo, username, boc_dict) return movementid
def insert_reserve_from_form(dbo, username, data): """ Inserts a movement from the workflow reserve an animal screen. Returns the new movement id """ # Validate that we have a date before doing anthing l = dbo.locale if None == utils.df_kd(data, "reservationdate", l): raise utils.ASMValidationError( i18n._("Reservations must have a valid reservation date.", l)) # Do the movement itself first move_dict = { "person": utils.df_ks(data, "person"), "animal": utils.df_ks(data, "animal"), "reservationdate": utils.df_ks(data, "reservationdate"), "adoptionno": utils.df_ks(data, "movementnumber"), "movementdate": "", "type": str(NO_MOVEMENT), "donation": utils.df_ks(data, "amount"), "returncategory": configuration.default_return_reason(dbo) } movementid = insert_movement_from_form(dbo, username, move_dict) # Then the donation if we have one donation_amount = int(utils.df_m(data, "amount", l)) if donation_amount > 0: due = "" received = utils.df_ks(data, "reservationdate") if configuration.movement_donations_default_due(dbo): due = utils.df_ks(data, "reservationdate") received = "" don_dict = { "person": utils.df_ks(data, "person"), "animal": utils.df_ks(data, "animal"), "movement": str(movementid), "type": utils.df_ks(data, "donationtype"), "payment": utils.df_ks(data, "payment"), "frequency": "0", "amount": utils.df_ks(data, "amount"), "due": due, "received": received, "giftaid": utils.df_ks(data, "giftaid") } financial.insert_donation_from_form(dbo, username, don_dict) # And a second donation if there is one donation_amount = int(utils.df_m(data, "amount2", l)) if donation_amount > 0: due = "" received = utils.df_ks(data, "movementdate") if configuration.movement_donations_default_due(dbo): due = utils.df_ks(data, "movementdate") received = "" don_dict = { "person": utils.df_ks(data, "person"), "animal": utils.df_ks(data, "animal"), "movement": str(movementid), "type": utils.df_ks(data, "donationtype2"), "payment": utils.df_ks(data, "payment2"), "frequency": "0", "amount": utils.df_ks(data, "amount2"), "due": due, "received": received, "giftaid": utils.df_ks(data, "giftaid") } financial.insert_donation_from_form(dbo, username, don_dict) return movementid
def insert_reserve_from_form(dbo, username, post): """ Inserts a movement from the workflow reserve an animal screen. Returns the new movement id """ # Validate that we have a date before doing anthing l = dbo.locale if None == post.date("reservationdate"): raise utils.ASMValidationError( i18n._("Reservations must have a valid reservation date.", l)) # Do the movement itself first move_dict = { "person": post["person"], "animal": post["animal"], "reservationdate": post["reservationdate"], "adoptionno": post["movementnumber"], "movementdate": "", "type": str(NO_MOVEMENT), "donation": post["amount"], "returncategory": configuration.default_return_reason(dbo) } movementid = insert_movement_from_form(dbo, username, utils.PostedData(move_dict, l)) # Then the donation if we have one donation_amount = post.integer("amount") if donation_amount > 0: due = "" received = post["reservationdate"] if configuration.movement_donations_default_due(dbo): due = post["reservationdate"] received = "" don_dict = { "person": post["person"], "animal": post["animal"], "movement": str(movementid), "type": post["donationtype"], "payment": post["payment"], "destaccount": post["destaccount"], "frequency": "0", "amount": post["amount"], "due": due, "received": received, "giftaid": post["giftaid"] } financial.insert_donation_from_form(dbo, username, utils.PostedData(don_dict, l)) # And a second donation if there is one donation_amount = post.integer("amount2") if donation_amount > 0: due = "" received = post["movementdate"] if configuration.movement_donations_default_due(dbo): due = post["movementdate"] received = "" don_dict = { "person": post["person"], "animal": post["animal"], "movement": str(movementid), "type": post["donationtype2"], "payment": post["payment2"], "destaccount": post["destaccount2"], "frequency": "0", "amount": post["amount2"], "due": due, "received": received, "giftaid": post["giftaid"] } financial.insert_donation_from_form(dbo, username, utils.PostedData(don_dict, l)) return movementid
def insert_reclaim_from_form(dbo, username, post): """ Inserts a movement from the workflow adopt an animal screen. Returns the new movement id """ l = dbo.locale # Validate that we have a movement date before doing anthing if None == post.date("movementdate"): raise utils.ASMValidationError( i18n._("Reclaim movements must have a valid reclaim date.", l)) # Get the animal record for this reclaim a = animal.get_animal(dbo, post.integer("animal")) if a is None: raise utils.ASMValidationError( "Reclaim POST has an invalid animal ID: %d" % post.integer("animal")) al.debug( "Creating reclaim for %d (%s - %s)" % (a["ID"], a["SHELTERCODE"], a["ANIMALNAME"]), "movement.insert_reclaim_from_form", dbo) # Prepare a dictionary of data for the movement table via insert_movement_from_form move_dict = { "person": post["person"], "animal": post["animal"], "adoptionno": post["movementnumber"], "movementdate": post["movementdate"], "type": str(RECLAIMED), "donation": post["amount"], "returncategory": configuration.default_return_reason(dbo) } # Is this animal currently on foster? If so, return the foster fm = get_animal_movements(dbo, post.integer("animal")) for m in fm: if m["MOVEMENTTYPE"] == FOSTER and m["RETURNDATE"] is None: return_movement(dbo, m["ID"], post.integer("animal"), post.date("movementdate")) # Is this animal current at a retailer? If so, return it from the # retailer and set the originalretailermovement and retailerid fields # on our new adoption movement so it can be linked back for m in fm: if m["MOVEMENTTYPE"] == RETAILER and m["RETURNDATE"] is None: return_movement(dbo, m["ID"], post.integer("animal"), post.date("movementdate")) move_dict["originalretailermovement"] = str(m["ID"]) move_dict["retailer"] = str(m["OWNERID"]) # If the animal was flagged as not available for adoption, then it # shouldn't be since we've just reclaimed it. db.execute( dbo, "UPDATE animal SET IsNotAvailableForAdoption = 0 WHERE ID = %s" % post["animal"]) # Is the animal reserved? Should clear it if so cancel_reserves = configuration.cancel_reserves_on_adoption(dbo) for m in fm: if cancel_reserves and m["MOVEMENTTYPE"] == NO_MOVEMENT and m["RESERVATIONDATE"] is not None \ and m["RESERVATIONCANCELLEDDATE"] is None: db.execute(dbo, "UPDATE adoption SET ReservationCancelledDate = %s WHERE ID = %d" % \ ( post.db_date("movementdate"), m["ID"] )) movementid = insert_movement_from_form(dbo, username, utils.PostedData(move_dict, l)) # Create the donation if there is one donation_amount = post.integer("amount") if donation_amount > 0: due = "" received = post["movementdate"] if configuration.movement_donations_default_due(dbo): due = post["movementdate"] received = "" don_dict = { "person": post["person"], "animal": post["animal"], "movement": str(movementid), "type": post["donationtype"], "payment": post["payment"], "destaccount": post["destaccount"], "frequency": "0", "amount": post["amount"], "due": due, "received": received, "giftaid": post["giftaid"] } financial.insert_donation_from_form(dbo, username, utils.PostedData(don_dict, l)) # Then any boarding cost record cost_amount = post.integer("costamount") cost_type = post["costtype"] cost_create = post.integer("costcreate") if cost_amount > 0 and cost_type != "" and cost_create == 1: boc_dict = { "animalid": post["animal"], "type": cost_type, "costdate": post["movementdate"], "costpaid": post["movementdate"], "cost": post["costamount"] } animal.insert_cost_from_form(dbo, username, utils.PostedData(boc_dict, l)) return movementid
def insert_adoption_from_form(dbo, username, data, creating=[]): """ Inserts a movement from the workflow adopt an animal screen. Returns the new movement id creating is an ongoing list of animals we're already going to create adoptions for. It prevents a never ending recursive loop of animal1 being bonded to animal2 that's bonded to animal1, etc. """ l = dbo.locale # Validate that we have a movement date before doing anthing if None == utils.df_kd(data, "movementdate", l): raise utils.ASMValidationError( i18n._("Adoption movements must have a valid adoption date.", l)) # Get the animal record for this adoption a = animal.get_animal(dbo, utils.df_ki(data, "animal")) if a is None: raise utils.ASMValidationError( "Adoption POST has an invalid animal ID: %d" % utils.df_ki(data, "animal")) al.debug( "Creating adoption for %d (%s - %s)" % (a["ID"], a["SHELTERCODE"], a["ANIMALNAME"]), "movement.insert_adoption_from_form", dbo) creating.append(a["ID"]) # If the animal is bonded to other animals, we call this function # again with a copy of the data and the bonded animal substituted # so we can create their adoption records too. if a["BONDEDANIMALID"] is not None and a["BONDEDANIMALID"] != 0 and a[ "BONDEDANIMALID"] not in creating: al.debug( "Found bond to animal %d, creating adoption..." % a["BONDEDANIMALID"], "movement.insert_adoption_from_form", dbo) newdata = dict(data) newdata["animal"] = str(a["BONDEDANIMALID"]) insert_adoption_from_form(dbo, username, newdata, creating) if a["BONDEDANIMAL2ID"] is not None and a["BONDEDANIMAL2ID"] != 0 and a[ "BONDEDANIMAL2ID"] not in creating: al.debug( "Found bond to animal %d, creating adoption..." % a["BONDEDANIMAL2ID"], "movement.insert_adoption_from_form", dbo) newdata = dict(data) newdata["animal"] = str(a["BONDEDANIMAL2ID"]) insert_adoption_from_form(dbo, username, newdata, creating) cancel_reserves = configuration.cancel_reserves_on_adoption(dbo) # Prepare a dictionary of data for the movement table via insert_movement_from_form move_dict = { "person": utils.df_ks(data, "person"), "animal": utils.df_ks(data, "animal"), "adoptionno": utils.df_ks(data, "movementnumber"), "movementdate": utils.df_ks(data, "movementdate"), "type": str(ADOPTION), "donation": utils.df_ks(data, "amount"), "insurance": utils.df_ks(data, "insurance"), "returncategory": configuration.default_return_reason(dbo), "trial": utils.df_ks(data, "trial"), "trialenddate": utils.df_ks(data, "trialenddate") } # Is this animal currently on foster? If so, return the foster fm = get_animal_movements(dbo, utils.df_ki(data, "animal")) for m in fm: if m["MOVEMENTTYPE"] == FOSTER and m["RETURNDATE"] is None: return_movement(dbo, m["ID"], utils.df_ki(data, "animal"), utils.df_kd(data, "movementdate", l)) # Is this animal current at a retailer? If so, return it from the # retailer and set the originalretailermovement and retailerid fields # on our new adoption movement so it can be linked back for m in fm: if m["MOVEMENTTYPE"] == RETAILER and m["RETURNDATE"] is None: return_movement(dbo, m["ID"], utils.df_ki(data, "animal"), utils.df_kd(data, "movementdate", l)) move_dict["originalretailermovement"] = str(m["ID"]) move_dict["retailer"] = str(m["OWNERID"]) # Did we say we'd like to flag the owner as homechecked? if utils.df_kc(data, "homechecked") == 1: db.execute(dbo, "UPDATE owner SET IDCheck = 1, DateLastHomeChecked = %s WHERE ID = %d" % \ ( db.dd(i18n.now(dbo.timezone)), utils.df_ki(data, "person"))) # If the animal was flagged as not available for adoption, then it # shouldn't be since we've just adopted it. db.execute( dbo, "UPDATE animal SET IsNotAvailableForAdoption = 0 WHERE ID = %s" % utils.df_ks(data, "animal")) # Is the animal reserved to the person adopting? movementid = 0 for m in fm: if m["MOVEMENTTYPE"] == NO_MOVEMENT and m["RESERVATIONDATE"] is not None \ and m["RESERVATIONCANCELLEDDATE"] is None and m["ANIMALID"] == utils.df_ki(data, "animal") \ and m["OWNERID"] == utils.df_ki(data, "person"): # yes - update the existing movement movementid = m["ID"] move_dict["movementid"] = str(movementid) move_dict["adoptionno"] = utils.padleft(movementid, 6) move_dict["reservationdate"] = str( i18n.python2display(l, m["RESERVATIONDATE"])) move_dict["comments"] = utils.nulltostr(m["COMMENTS"]) break elif cancel_reserves and m["MOVEMENTTYPE"] == NO_MOVEMENT and m["RESERVATIONDATE"] is not None \ and m["RESERVATIONCANCELLEDDATE"] is None: # no, but it's reserved to someone else and we're cancelling # reserves on adoption db.execute(dbo, "UPDATE adoption SET ReservationCancelledDate = %s WHERE ID = %d" % \ ( utils.df_d(data, "movementdate", l), m["ID"] )) if movementid != 0: update_movement_from_form(dbo, username, move_dict) else: movementid = insert_movement_from_form(dbo, username, move_dict) # Create the donation if there is one donation_amount = int(utils.df_m(data, "amount", l)) if donation_amount > 0: due = "" received = utils.df_ks(data, "movementdate") if configuration.movement_donations_default_due(dbo): due = utils.df_ks(data, "movementdate") received = "" don_dict = { "person": utils.df_ks(data, "person"), "animal": utils.df_ks(data, "animal"), "movement": str(movementid), "type": utils.df_ks(data, "donationtype"), "payment": utils.df_ks(data, "payment"), "frequency": "0", "amount": utils.df_ks(data, "amount"), "due": due, "received": received, "giftaid": utils.df_ks(data, "giftaid") } financial.insert_donation_from_form(dbo, username, don_dict) # And a second donation if there is one donation_amount = int(utils.df_m(data, "amount2", l)) if donation_amount > 0: due = "" received = utils.df_ks(data, "movementdate") if configuration.movement_donations_default_due(dbo): due = utils.df_ks(data, "movementdate") received = "" don_dict = { "person": utils.df_ks(data, "person"), "animal": utils.df_ks(data, "animal"), "movement": str(movementid), "type": utils.df_ks(data, "donationtype2"), "payment": utils.df_ks(data, "payment2"), "frequency": "0", "amount": utils.df_ks(data, "amount2"), "due": due, "received": received, "giftaid": utils.df_ks(data, "giftaid") } financial.insert_donation_from_form(dbo, username, don_dict) # Then any boarding cost record cost_amount = int(utils.df_m(data, "costamount", l)) cost_type = utils.df_ks(data, "costtype") cost_create = utils.df_ki(data, "costcreate") if cost_amount > 0 and cost_type != "" and cost_create == 1: boc_dict = { "animalid": utils.df_ks(data, "animal"), "type": cost_type, "costdate": utils.df_ks(data, "movementdate"), "cost": utils.df_ks(data, "costamount") } animal.insert_cost_from_form(dbo, username, boc_dict) return movementid
def insert_adoption_from_form(dbo, username, post, creating = [], create_payments = True): """ Inserts a movement from the workflow adopt an animal screen. Returns the new movement id creating is an ongoing list of animals we're already going to create adoptions for. It prevents a never ending recursive loop of animal1 being bonded to animal2 that's bonded to animal1, etc. create_payments is True if we should create payments - don't do this for bonded animals or we'll double up all the payments. """ l = dbo.locale # Validate that we have a movement date before doing anthing if None is post.date("movementdate"): raise utils.ASMValidationError(i18n._("Adoption movements must have a valid adoption date.", l)) # Get the animal record for this adoption a = animal.get_animal(dbo, post.integer("animal")) if a is None: raise utils.ASMValidationError("Adoption POST has an invalid animal ID: %d" % post.integer("animal")) al.debug("Creating adoption for %d (%s - %s)" % (a["ID"], a["SHELTERCODE"], a["ANIMALNAME"]), "movement.insert_adoption_from_form", dbo) creating.append(a.ID) # If the animal is bonded to other animals, we call this function # again with a copy of the data and the bonded animal substituted # so we can create their adoption records too. We only do this if # the other animals are still on shelter (therefore alive). if a.BONDEDANIMALID is not None and a.BONDEDANIMALID != 0 and a.BONDEDANIMAL1ARCHIVED == 0 and a.BONDEDANIMALID not in creating: al.debug("Found bond to animal %d, creating adoption..." % a.BONDEDANIMALID, "movement.insert_adoption_from_form", dbo) newdata = dict(post.data) newdata["animal"] = str(a.BONDEDANIMALID) insert_adoption_from_form(dbo, username, utils.PostedData(newdata, dbo.locale), creating, create_payments = False) if a.BONDEDANIMAL2ID is not None and a.BONDEDANIMAL2ID != 0 and a.BONDEDANIMAL2ARCHIVED == 0 and a.BONDEDANIMAL2ID not in creating: al.debug("Found bond to animal %d, creating adoption..." % a.BONDEDANIMAL2ID, "movement.insert_adoption_from_form", dbo) newdata = dict(post.data) newdata["animal"] = str(a.BONDEDANIMAL2ID) insert_adoption_from_form(dbo, username, utils.PostedData(newdata, dbo.locale), creating, create_payments = False) cancel_reserves = configuration.cancel_reserves_on_adoption(dbo) # Prepare a dictionary of data for the movement table via insert_movement_from_form move_dict = { "person" : post["person"], "animal" : post["animal"], "adoptionno" : post["movementnumber"], "movementdate" : post["movementdate"], "type" : str(ADOPTION), "donation" : post["amount"], "insurance" : post["insurance"], "returncategory" : configuration.default_return_reason(dbo), "trial" : post["trial"], "trialenddate" : post["trialenddate"] } # Is this animal currently on foster? If so, return the foster fm = get_animal_movements(dbo, post.integer("animal")) for m in fm: if m.MOVEMENTTYPE == FOSTER and m.RETURNDATE is None: return_movement(dbo, m["ID"], post.integer("animal"), post.date("movementdate")) # Is this animal current at a retailer? If so, return it from the # retailer and set the originalretailermovement and retailerid fields # on our new adoption movement so it can be linked back for m in fm: if m.MOVEMENTTYPE == RETAILER and m.RETURNDATE is None: return_movement(dbo, m.ID, post.integer("animal"), post.date("movementdate")) move_dict["originalretailermovement"] = str(m.ID) move_dict["retailer"] = str(m["OWNERID"]) # Did we say we'd like to flag the owner as homechecked? if post.boolean("homechecked") == 1: dbo.update("owner", post.integer("person"), { "IDCheck": 1, "DateLastHomeChecked": dbo.today() }, username) # If the animal was flagged as not available for adoption, then it # shouldn't be since we've just adopted it. dbo.update("animal", a.ID, { "IsNotAvailableForAdoption": 0 }) # Is the animal reserved to the person adopting? movementid = 0 for m in fm: if m.MOVEMENTTYPE == NO_MOVEMENT and m.RESERVATIONDATE is not None \ and m.RESERVATIONCANCELLEDDATE is None and m.ANIMALID == post.integer("animal") \ and m.OWNERID == post.integer("person"): # yes - update the existing movement movementid = m.ID move_dict["movementid"] = str(movementid) move_dict["adoptionno"] = utils.padleft(movementid, 6) move_dict["reservationdate"] = str(i18n.python2display(l, m.RESERVATIONDATE)) move_dict["comments"] = utils.nulltostr(m.COMMENTS) break elif cancel_reserves and m.MOVEMENTTYPE == NO_MOVEMENT and m.RESERVATIONDATE is not None \ and m.RESERVATIONCANCELLEDDATE is None: # no, but it's reserved to someone else and we're cancelling # reserves on adoption dbo.update("adoption", m.ID, { "ReservationCancelledDate": post.date("movementdate") }, username) if movementid != 0: update_movement_from_form(dbo, username, utils.PostedData(move_dict, l)) else: movementid = insert_movement_from_form(dbo, username, utils.PostedData(move_dict, l)) # Create any payments if create_payments: financial.insert_donations_from_form(dbo, username, post, post["movementdate"], False, post["person"], post["animal"], movementid) # Then any boarding cost record cost_amount = post.integer("costamount") cost_type = post["costtype"] cost_create = post.integer("costcreate") if cost_amount > 0 and cost_type != "" and cost_create == 1: boc_dict = { "animalid" : post["animal"], "type" : cost_type, "costdate" : post["movementdate"], "costpaid" : post["movementdate"], "cost" : post["costamount"] } animal.insert_cost_from_form(dbo, username, utils.PostedData(boc_dict, l)) return movementid
def insert_reclaim_from_form(dbo, username, post): """f Inserts a movement from the workflow adopt an animal screen. Returns the new movement id """ l = dbo.locale # Validate that we have a movement date before doing anthing if None is post.date("movementdate"): raise utils.ASMValidationError(i18n._("Reclaim movements must have a valid reclaim date.", l)) # Get the animal record for this reclaim a = animal.get_animal(dbo, post.integer("animal")) if a is None: raise utils.ASMValidationError("Reclaim POST has an invalid animal ID: %d" % post.integer("animal")) al.debug("Creating reclaim for %d (%s - %s)" % (a.ID, a.SHELTERCODE, a.ANIMALNAME), "movement.insert_reclaim_from_form", dbo) # Prepare a dictionary of data for the movement table via insert_movement_from_form move_dict = { "person" : post["person"], "animal" : post["animal"], "adoptionno" : post["movementnumber"], "movementdate" : post["movementdate"], "type" : str(RECLAIMED), "donation" : post["amount"], "returncategory" : configuration.default_return_reason(dbo) } # Is this animal currently on foster? If so, return the foster fm = get_animal_movements(dbo, post.integer("animal")) for m in fm: if m.MOVEMENTTYPE == FOSTER and m.RETURNDATE is None: return_movement(dbo, m.ID, post.integer("animal"), post.date("movementdate")) # Is this animal current at a retailer? If so, return it from the # retailer and set the originalretailermovement and retailerid fields # on our new adoption movement so it can be linked back for m in fm: if m.MOVEMENTTYPE == RETAILER and m.RETURNDATE is None: return_movement(dbo, m["ID"], post.integer("animal"), post.date("movementdate")) move_dict["originalretailermovement"] = str(m.ID) move_dict["retailer"] = str(m.OWNERID) # If the animal was flagged as not available for adoption, then it # shouldn't be since we've just reclaimed it. dbo.update("animal", post.integer("animal"), { "IsNotAvailableForAdoption": 0 }) # Is the animal reserved? Should clear it if so cancel_reserves = configuration.cancel_reserves_on_adoption(dbo) for m in fm: if cancel_reserves and m.MOVEMENTTYPE == NO_MOVEMENT and m.RESERVATIONDATE is not None \ and m.RESERVATIONCANCELLEDDATE is None: dbo.update("adoption", m.ID, { "ReservationCancelledDate": post.date("movementdate") }, username) movementid = insert_movement_from_form(dbo, username, utils.PostedData(move_dict, l)) # Create any payments financial.insert_donations_from_form(dbo, username, post, post["movementdate"], False, post["person"], post["animal"], movementid) # Then any boarding cost record cost_amount = post.integer("costamount") cost_type = post["costtype"] cost_create = post.integer("costcreate") if cost_amount > 0 and cost_type != "" and cost_create == 1: boc_dict = { "animalid" : post["animal"], "type" : cost_type, "costdate" : post["movementdate"], "costpaid" : post["movementdate"], "cost" : post["costamount"] } animal.insert_cost_from_form(dbo, username, utils.PostedData(boc_dict, l)) return movementid
def insert_reclaim_from_form(dbo, username, post): """f Inserts a movement from the workflow adopt an animal screen. Returns the new movement id """ l = dbo.locale # Validate that we have a movement date before doing anthing if None == post.date("movementdate"): raise utils.ASMValidationError(i18n._("Reclaim movements must have a valid reclaim date.", l)) # Get the animal record for this reclaim a = animal.get_animal(dbo, post.integer("animal")) if a is None: raise utils.ASMValidationError("Reclaim POST has an invalid animal ID: %d" % post.integer("animal")) al.debug("Creating reclaim for %d (%s - %s)" % (a["ID"], a["SHELTERCODE"], a["ANIMALNAME"]), "movement.insert_reclaim_from_form", dbo) # Prepare a dictionary of data for the movement table via insert_movement_from_form move_dict = { "person" : post["person"], "animal" : post["animal"], "adoptionno" : post["movementnumber"], "movementdate" : post["movementdate"], "type" : str(RECLAIMED), "donation" : post["amount"], "returncategory" : configuration.default_return_reason(dbo) } # Is this animal currently on foster? If so, return the foster fm = get_animal_movements(dbo, post.integer("animal")) for m in fm: if m["MOVEMENTTYPE"] == FOSTER and m["RETURNDATE"] is None: return_movement(dbo, m["ID"], post.integer("animal"), post.date("movementdate")) # Is this animal current at a retailer? If so, return it from the # retailer and set the originalretailermovement and retailerid fields # on our new adoption movement so it can be linked back for m in fm: if m["MOVEMENTTYPE"] == RETAILER and m["RETURNDATE"] is None: return_movement(dbo, m["ID"], post.integer("animal"), post.date("movementdate")) move_dict["originalretailermovement"] = str(m["ID"]) move_dict["retailer"] = str(m["OWNERID"]) # If the animal was flagged as not available for adoption, then it # shouldn't be since we've just reclaimed it. db.execute(dbo, "UPDATE animal SET IsNotAvailableForAdoption = 0 WHERE ID = %s" % post["animal"]) # Is the animal reserved? Should clear it if so cancel_reserves = configuration.cancel_reserves_on_adoption(dbo) for m in fm: if cancel_reserves and m["MOVEMENTTYPE"] == NO_MOVEMENT and m["RESERVATIONDATE"] is not None \ and m["RESERVATIONCANCELLEDDATE"] is None: db.execute(dbo, "UPDATE adoption SET ReservationCancelledDate = %s WHERE ID = %d" % \ ( post.db_date("movementdate"), m["ID"] )) movementid = insert_movement_from_form(dbo, username, utils.PostedData(move_dict, l)) # Create any payments financial.insert_donations_from_form(dbo, username, post, post["movementdate"], False, post["person"], post["animal"], movementid) # Then any boarding cost record cost_amount = post.integer("costamount") cost_type = post["costtype"] cost_create = post.integer("costcreate") if cost_amount > 0 and cost_type != "" and cost_create == 1: boc_dict = { "animalid" : post["animal"], "type" : cost_type, "costdate" : post["movementdate"], "costpaid" : post["movementdate"], "cost" : post["costamount"] } animal.insert_cost_from_form(dbo, username, utils.PostedData(boc_dict, l)) return movementid