def ajax_cc_retry_and_trans_restart(request):
    charge_id = request.GET.get('id')
    action = 'retry charging'
    if charge_id:
        old_cc = Charge.objects.get(id=charge_id)
        try:
            if old_cc.payment_getaway in [Charge.AUTHORIZE] and old_cc.atransaction:
                old_cc.status = Charge.SUCCESS
                old_cc.save()
                old_cc.add_charge_step(action, Charge.SUCCESS, 'User "%s" started retry.Previous charge was successfully. Restart canceled' % request.user)
            else:
                old_cc = old_cc.check_getaway()
                old_cc.make_charge(retry=True)
                old_cc.add_charge_step(action, Charge.SUCCESS, 'User "%s" started retry. Charge retry ended successfully' % request.user)
                if old_cc.customer.email_success_charge:
                    successful_precharge_restart_customer_notification(old_cc)

            transaction = old_cc.get_last_transaction
            transaction.retry_count = 1
            transaction.state = Transaction.RETRY
            transaction.adv_status = 'Transaction restarted by user %s' % request.user
            transaction.add_transaction_step('Restarted',
                                             'button',
                                             Transaction.SUCCESS,
                                             'User %s restarted transaction.' % request.user)
            transaction.save()
            from ppars.apps.core.tasks import queue_refill
            transaction.was_retried_on_step = False
            queue_refill.delay(transaction.id)
        except Exception, e:
            logger.error("Exception: %s. Trace: %s." % (e, traceback.format_exc(limit=10)))
            old_cc.add_charge_step(action, Charge.ERROR, 'User "%s" started retry. Charge retry ended with error: "%s"' % (request.user, e))
        finally:
 def handle(self, *args, **options):
     print "start core"
     today = datetime.now(pytz.timezone('US/Eastern')).date()
     start_date = datetime.combine(today - timedelta(days=6), time.min)
     for transaction in Transaction.objects.filter(state=Transaction.INTERMEDIATE, started__gt=start_date):
         print 'run %s' % transaction
         queue_refill.delay(transaction.id)
     print "done"
 def handle(self, *args, **options):
     print "start core"
     to_refill = []
     not_to_refill = []
     today = datetime.now(timezone('US/Eastern')).date()
     start_date = datetime.combine(today - timedelta(days=7), time.min)
     for autorefill in AutoRefill.objects.filter(enabled=True, trigger=AutoRefill.TRIGGER_SC,
                                                 renewal_date__range=(datetime.now().date() - timedelta(days=64),
                                                                      datetime.now().date()))\
             .exclude(renewal_date=None).exclude(schedule='').exclude(schedule=None):
         if autorefill.renewal_date < datetime.now(timezone('US/Eastern')).date():
             result = get_mdn_status(autorefill.phone_number, autorefill.company)
             if not result['login_exception'] and result['status_find'] and\
                     result['renewal_date'] and result['renewal_date'] > datetime.now(timezone('US/Eastern')).date():
                 to_refill.append(autorefill.get_full_url())
                 if Transaction.objects.filter(autorefill=autorefill, started__gt=start_date):
                     transaction = Transaction.objects.filter(autorefill=autorefill, started__gt=start_date)[0]
                 else:
                     if not autorefill.check_renewal_end_date(today=today):
                         continue
                     transaction = Transaction.objects.create(user=autorefill.user,
                                                              autorefill=autorefill,
                                                              state="Q",
                                                              company=autorefill.company,
                                                              triggered_by='System')
                 if transaction.completed:
                     continue
                 if autorefill.need_buy_pins and transaction.pin:
                     transaction.state = Transaction.COMPLETED
                     transaction.save()
                     continue
                 transaction.state = Transaction.PROCESS
                 transaction.save()
                 queue_refill.delay(transaction.id)
                 autorefill.renewal_date = result['renewal_date']
                 autorefill.save()
             else:
                 not_to_refill.append(autorefill.get_full_url())
     print 'Was refiled: ', to_refill
     print 'Wasn\'t refiled: ', not_to_refill
                                           state=Transaction.INTERMEDIATE):
 if transaction.autorefill:
     try:
         result = get_mdn_status(transaction.autorefill.phone_number, transaction.autorefill.company)
     except Exception as e:
         not_refilled.append('Not refilled: ' + transaction.get_full_url())
         continue
     if not result['login_exception']:
         if 'renewal_date' in result and (not result['renewal_date'] or
                                             result['renewal_date'] == datetime.date(2015, 11, 8)):
             refilled.append('Refilled: ' + transaction.get_full_url())
             autorefill = transaction.autorefill
             autorefill.renewal_date = datetime.date(2015, 11, 8)
             autorefill.save()
             autorefill.set_renewal_date_to_next(today=autorefill.renewal_date)
             queue_refill.delay(transaction.id)
             send_with = transaction.autorefill.customer.send_status
             if send_with == 'NO':
                 send_with = 'EM'
             notification = Notification.objects.create(
                 company=CompanyProfile.objects.get(superuser_profile=True),
                 customer=transaction.autorefill.customer,
                 email='*****@*****.**',
                 phone_number='',
                 subject='Apology for delay in the service',
                 body='Hi, %s. Our system had a server issues and your reifll didn\'t run on time. It\'s'
                      ' fixed and it should not repeat, we apologize for any inconvenience'
                      ' this has caused you.' % transaction.autorefill.customer.first_name,
                 send_with=send_with)
             notification.send_notification()
         elif 'renewal_date' in result and result['renewal_date'] > datetime.date(2015, 11, 8):
 def handle(self, *args, **options):
     transaction = Transaction.objects.filter(id=6754102092517661).get()
     queue_refill.delay(transaction.id)