'Given a Team instance, and amount as str, return a dict.
We INSERT instead of UPDATE, so that we have history to explore. The
COALESCE function returns the first of its arguments that is not NULL.
The effect here is to stamp all payment instructions with the timestamp
of the first instruction from this ~user to that Team. I believe this
is used to determine the order of payments during payday.
The dict returned represents the row inserted in the payment_instructions
table.'

def set_payment_instruction(self, team, amount, update_self=True, update_team=True, cursor=None):

    assert self.is_claimed
    amount = Decimal(amount)
    if ((amount < MIN_PAYMENT) or (amount > MAX_PAYMENT)):
        raise BadAmount
    NEW_PAYMENT_INSTRUCTION = u'\n                                                INSERT    INTO    payment_instructions\n                                                                                                (ctime,    participant_id,    team_id,    amount)\n                                                                    VALUES    (    COALESCE    ((    SELECT    ctime\n                                                                                                                                                                FROM    payment_instructions\n                                                                                                                                                            WHERE    (            participant_id=%(participant_id)s\n                                                                                                                                                                                    AND    team_id=%(team_id)s\n                                                                                                                                                                                        )\n                                                                                                                                                            LIMIT    1\n                                                                                                                                                        ),    CURRENT_TIMESTAMP)\n                                                                                                ,    %(participant_id)s,    %(team_id)s,    %(amount)s\n                                                                                                    )\n                                                        RETURNING    *\n\n                                '
    args = dict(participant_id=self.id, team_id=team.id, amount=amount)
    t = (cursor or self.db).one(NEW_PAYMENT_INSTRUCTION, args)
    t_dict = t._asdict()
    if (amount > 0):
        self._update_due(t_dict[u'team_id'], t_dict[u'id'], cursor)
    else:
        self._reset_due(t_dict[u'team_id'], cursor=cursor)
    if update_self:
        self.update_giving(cursor)
    if update_team:
        team.update_receiving(cursor)
    if (team.slug == u'Gratipay'):
        self.update_is_free_rider((None if (amount == 0) else False), cursor)
    return t._asdict()
