Esempio n. 1
0
    def fix_period_start(cls):
        from tv.models import Fee
        from tv.models import FeeType
        from tv.models import TariffPlan

        ss = cls.objects.filter(start__gte='2006-02-01').filter(
            start__lt='2011-03-01')
        ft = FeeType.objects.all()
        ft = ft[0]
        ft2 = FeeType.objects.all()
        ft2 = ft2[4]
        tp = TariffPlan.objects.all()
        tp = tp[0]

        for s in ss:
            m1 = ft.get_sum(date=s.start)['fee']
            m2 = ft2.get_sum(date=s.start)['fee']
            m = m2 - m1
            try:
                bill = s.iac.bill
                card = s.iac.catv_card
                print "%s %s | %s | %s %s" % (card, bill, m, m1, m2)
                f = Fee(bill=bill,
                        card=card,
                        sum=-m,
                        tp=tp,
                        fee_type=ft,
                        inner_descr=u'перенос бази. возврат подключение',
                        timestamp=s.start)
                f.save()
                f.make()
            except:
                print "EXCEPTION CAUGHT!"
Esempio n. 2
0
    def fix_period_end(cls):
        from tv.models import Fee
        from tv.models import FeeType
        from tv.models import TariffPlan

        ss = cls.objects.filter(finish__lte='2011-03-01')
        ft = FeeType.objects.all()
        ft = ft[0]
        tp = TariffPlan.objects.all()
        tp = tp[0]

        for s in ss:
            if not s.finish:
                continue
            try:
                bill = s.iac.bill
                card = s.iac.catv_card
                m = ft.get_sum(date=s.finish)['ret']
                print "%s %s | %s |" % (card, bill, m)
                f = Fee(bill=bill,
                        card=card,
                        sum=-m,
                        tp=tp,
                        fee_type=ft,
                        inner_descr=u'перенос бази. возврат отключение',
                        timestamp=s.finish)
                f.save()
                f.make()
            except:
                print "EXCEPTION CAUGHT!"
Esempio n. 3
0
    def launch_hamster(self, countdown=True, debug=True):
        from lib.functions import date_formatter, add_months
        from tv.models import FeeType, Fee, Payment, TariffPlan
        from django.db.models import Max
        from time import sleep
        import gc

        gc.enable()

        if debug:
            print "Abonent %s" % self
            if countdown:
                print "    hamster ready to be launched."
                print "    all finance log will be recalculated."
                print "    use only when neccecary."
                print "    -------------------------------------"
                print "    you have 5 sec to cancel... (Ctrl+C)"
                try:
                    sleep(1)
                    print "    4..."
                    sleep(1)
                    print "    3..."
                    sleep(1)
                    print "    2..."
                    sleep(1)
                    print "    1..."
                    sleep(1)
                    print "    hamster launched..."
                except KeyboardInterrupt:
                    print "    hamster launching cancelled..."
                    print "    Bye..."
                    return False

            print "        resetting all finance log..."

        self.bill.balance = 0
        self.bill.save()
        Fee.objects.filter(bill=self.bill).delete()
        self.catv_card.service_log.all().delete()

        pp = Payment.objects.filter(bill=self.bill)
        for p in pp:
            p.maked = False
            if not p.bank_date:
                p.bank_date = p.timestamp.date()
            p.save()
        if debug:
            print "            Done..."

        catv = FeeType.objects.get(pk=5)
        catv_part = FeeType.objects.get(pk=1)
        tp = TariffPlan.objects.all()
        tp = tp[0]

        thismonth = date_formatter(date.today())['month'].date()
        nextmonth = date_formatter(add_months(thismonth, 1))['month'].date()

        new = True
        prev_closing_fee = 0
        prev_closing_month = date(1970, 1, 1)

        for i in self.intervals.all():
            if debug:
                print "        processing interval %s-%s" % (i.start, i.finish)
            if not i.finish:
                i.finish = thismonth
            d = i.start

            for service in self.catv_card.services.all():
                service.active = True
                service.save(sdate=d, descr="%s/%s" % (i.s1, i.s2))
                self.catv_card.active = True
                self.catv_card.save()
                self.disabled = False
                self.save()

            dd = date_formatter(add_months(d, 1))['month'].date()

            if debug:
                print "            starting date %s" % d
            pp = Payment.objects.filter(bill=self.bill,
                                        maked=False,
                                        bank_date__lte=d)
            for p in pp:
                p.save()
                p.make()
            if d > date(2006, 2, 1) or not new:
                full = catv.get_sum(date=i.start)['fee']
                sum = catv_part.get_sum(date=i.start)['fee']
                if debug:
                    print "                full fee: %s" % full
                    print "                current fee: %s" % sum
                    print "                closing fee: %s" % prev_closing_fee
                    print "                closing month: %s" % prev_closing_month
                    print "                currnet month: %s" % date_formatter(
                        d)['month'].date()
                    print sum + prev_closing_fee > full
                    print date_formatter(
                        d)['month'].date() == prev_closing_month
                if sum + prev_closing_fee > full and date_formatter(
                        d)['month'].date() == prev_closing_month:
                    print "                overpowered fee catched! fixed..."
                    f = Fee(bill=self.bill,
                            card=self.catv_card,
                            sum=full - prev_closing_fee,
                            tp=tp,
                            fee_type=catv_part,
                            timestamp=d,
                            inner_descr=u'Кабельное ТВ | подключение (!)')
                else:
                    f = Fee(bill=self.bill,
                            card=self.catv_card,
                            sum=sum,
                            tp=tp,
                            fee_type=catv_part,
                            timestamp=d,
                            inner_descr=u'Кабельное ТВ | подключение')
                f.save()
                maxid = Fee.objects.aggregate(Max('id'))['id__max']
                f = Fee.objects.get(pk=maxid)
                f.make()
            else:
                if debug:
                    print "                ignored because before 2006-02-01"
                f = Fee(bill=self.bill,
                        card=self.catv_card,
                        sum=0,
                        tp=tp,
                        fee_type=catv_part,
                        timestamp=d,
                        inner_descr=
                        u'Кабельное ТВ | подключение (оплачено на месте)')
                f.save()
                maxid = Fee.objects.aggregate(Max('id'))['id__max']
                f = Fee.objects.get(pk=maxid)
                f.make()
            new = False
            d = dd
            dd = date_formatter(add_months(d, 1))['month'].date()
            pp = Payment.objects.filter(bill=self.bill,
                                        maked=False,
                                        bank_date__lte=d)
            for p in pp:
                p.save()
                p.make()

            while dd < i.finish or dd == nextmonth or dd == thismonth:
                if debug:
                    print "            processing date %s" % d
                sum = catv.get_sum(date=d)['fee']
                f = Fee(bill=self.bill,
                        card=self.catv_card,
                        sum=sum,
                        tp=tp,
                        fee_type=catv,
                        timestamp=d,
                        inner_descr=u'Кабельное ТВ | абонплата')
                f.save()
                maxid = Fee.objects.aggregate(Max('id'))['id__max']
                f = Fee.objects.get(pk=maxid)
                f.make()
                d = dd
                dd = date_formatter(add_months(d, 1))['month'].date()
                pp = Payment.objects.filter(bill=self.bill,
                                            maked=False,
                                            bank_date__lte=d)
                for p in pp:
                    p.save()
                    p.make()

            if d < thismonth:
                if debug:
                    print "            closing date %s" % d
                full = catv.get_sum(date=i.finish)['fee']
                sum = full - catv_part.get_sum(date=i.finish)['ret']
                prev_closing_fee = sum
                prev_closing_month = date_formatter(i.finish)['month'].date()
                f = Fee(bill=self.bill,
                        card=self.catv_card,
                        sum=sum,
                        tp=tp,
                        fee_type=catv,
                        timestamp=i.finish,
                        inner_descr=u'Кабельное ТВ | отключение')
                f.save()
                maxid = Fee.objects.aggregate(Max('id'))['id__max']
                f = Fee.objects.get(pk=maxid)
                f.make()

                for service in self.catv_card.services.all():
                    service.active = False
                    service.save(sdate=d, descr="")
                    self.catv_card.active = False
                    self.catv_card.save()
                    self.disabled = True
                    self.save()

            pp = Payment.objects.filter(bill=self.bill,
                                        maked=False,
                                        bank_date__lte=d)
            for p in pp:
                p.save()
                p.make()

        pp = Payment.objects.filter(bill=self.bill, maked=False)
        for p in pp:
            p.save()
            p.make()

        if debug:
            print "    hamster finished his work and stopped"
            print "    dont forget donate to WWF ;)"
            print "    Bye..."

        gc.collect()
        return True