def reimburse(): ''' Start reimbursement process, entailing interaction with special numbers ''' counter = 0 network_name_map = dict([(v, k) for k, v in NAME_NETWORK_MAP.items()]) reimbursements = ReimbursementRecord.objects.exclude( status__in=(ReimbursementRecord.COMPLETED, ReimbursementRecord.ERROR) ) #import pdb;pdb.set_trace() for network, _ in Subscriber.NETWORKS: print 'network: %s'%network if reimbursements.filter( status__in=( ReimbursementRecord.IN_PROGRESS, ReimbursementRecord.QUEUED), subscriber__network=network): continue else: #there is no reimbursement in progress for "network" subscribers = Subscriber.objects.filter(balance__gt=0, network=network) min_time = settings.REIMBURSEMENT_MIN_TIME qualified = qualify(subscribers, min_time) if qualified: subscriber = qualified[0] else: continue network_name = network_name_map.get(network) _amount = max(subscriber.balance, settings.MINIMUM_TRANSFERS.get(network_name)) backend_name = subscriber.get_backend() backend, _ = Backend.objects.get_or_create(name=backend_name) text = subscriber.first_message % { 'number': '0%s'%subscriber.number[-10:], 'amount': _amount, 'pin': settings.NETWORK_PINS.get(network_name) } logging.info('message to send is %s'%text) to_number = REIMBURSEMENT_NUMBERS.get(network_name) if len(to_number) < 11:#If it is a short-code, prefix with 's' to_number = 's%s'%to_number connection, _ = Connection.objects.get_or_create( backend=backend, identity=to_number) msg = OutgoingMessage(connection=connection, template=text) try: msg.send() except: router = Router() #router.start() router.outgoing(msg) ReimbursementRecord.objects.create( subscriber=subscriber, amount=_amount, status=ReimbursementRecord.IN_PROGRESS) counter += 1 return counter
def handle(self, msg): #self.send_queued_messages() backend = msg.connection.backend #import pdb;pdb.set_trace() if backend.name in settings.REIMBURSED_BACKENDS: network_name = backend.name.split('-')[1] network_id = NAME_NETWORK_MAP.get(network_name) if msg.connection.identity == REIMBURSEMENT_NUMBERS.get(network_name): try: current = ReimbursementRecord.objects.get( status=ReimbursementRecord.IN_PROGRESS, subscriber__network=network_id) except ReimbursementRecord.DoesNotExist: pass else: status = getattr(self, 'handle_%s'%network_name)(msg, current) current.status = status current.add_message(msg.text) current.completed_on = datetime.now() if status == ReimbursementRecord.COMPLETED: bal = current.subscriber.balance - current.amount current.subscriber.balance = max(bal, 0) current.subscriber.save() #current.completed_on = datetime.now() ReimbursementLog.objects.create( phone='+234%s'%current.subscriber.number[-10:], amount=current.amount, reimbursed_on=datetime.now()) sub_no = '+234%s'%current.subscriber.number[-10:] try: conn = Connection.objects.get(identity=sub_no, backend=backend) notice = OutgoingMessage(connection=conn, template=settings.REIMBURSEMENT_NOTICE) self.router.outgoing(notice) except Exception, exc: self.error(str(exc)) current.save() return True #we always handle network credit transfer messages