Exemple #1
0
    def test_bill_update(self):
        """
        Test a Bill update (new Bill record)
        """

        _start_call = Record()
        _start_call.timestamp = datetime.datetime.strptime(
            "2018-08-26T15:07:10+0000", "%Y-%m-%dT%H:%M:%S%z")
        _start_call.source = '51992657100'
        _start_call.destination = '5133877079'
        _start_call.call_type = 'S'
        _start_call.call_id = '1'
        _start_call.save()

        _end_call = Record()
        _end_call.timestamp = datetime.datetime.strptime(
            "2018-08-26T15:17:10+0000", "%Y-%m-%dT%H:%M:%S%z")
        _end_call.call_type = 'E'
        _end_call.call_id = '1'
        _end_call.save()

        _start_period = _start_call.timestamp
        _end_period = _end_call.timestamp

        _bill = Bill()
        _bill.subscriber = '51992657100'
        _bill.period = '072018'
        _bill.save()

        _bill_record = BillRecord()
        _bill_record.bill_origin = _bill
        _bill_record.start_call = _start_call
        _bill_record.end_call = _end_call
        _bill_record.call_price = _bill.calculate_charge(
            _start_period, _end_period)
        _bill_record.save()

        self.assertEquals(
            BillRecord.objects.filter(bill_origin=_bill).count(), 1)
Exemple #2
0
    def setUp(self):
        self.client = APIClient()

        # Create a Bill of last month for tests
        _start_call = Record()
        _start_call.timestamp = datetime.datetime.strptime(
                                  "2018-08-26T15:07:10+0000",
                                  "%Y-%m-%dT%H:%M:%S%z")
        _start_call.source = '51992657100'
        _start_call.destination = '5133877079'
        _start_call.call_type = 'S'
        _start_call.call_id = '1'
        _start_call.save()

        _end_call = Record()
        _end_call.timestamp = datetime.datetime.strptime(
                                  "2018-08-26T15:17:10+0000",
                                  "%Y-%m-%dT%H:%M:%S%z")
        _end_call.call_type = 'E'
        _end_call.call_id = '1'
        _end_call.save()

        _start_period = _start_call.timestamp
        _end_period = _end_call.timestamp

        _bill = Bill()
        _bill.subscriber = '51992657100'
        _bill.period = last_month_string()
        _bill.save()

        _bill_record = BillRecord()
        _bill_record.bill_origin = _bill
        _bill_record.start_call = _start_call
        _bill_record.end_call = _end_call
        _bill_record.call_price = _bill.calculate_charge(
                                    _start_period, _end_period
                                  )
        _bill_record.save()        
    def save(self, *args, **kwargs):
        """Business rule for save a new Start or End call record

        Override the Record Save Method for implement business rule
        Verify Call Record consistency:
            - Only valid Call Record types are allowed:
                (START_CALL_TYPE or END_CALL_TYPE)
            - Only unique call_id per call type is allowed
            - A E type Call record only may exist if a origin S type
                Call record (same call_id) already exists
            - The end call date/time MAY be higher than
                the start call date/time
        """

        # Verify Call Type
        _valid_type = False
        for choise in self.TYPE_CALL_CHOICES:
            if self.call_type == choise[0]:
                _valid_type = True

        if not _valid_type:
            raise ValidationError({'detail': 'Invalid call record Type'})

        # Verify a call with same type and call_id already created
        if Record.objects.filter(call_id=self.call_id,
                                 call_type=self.call_type).exists():
            raise ValidationError(
                {'detail': 'This record call_id is already created'})

        # If a End Call record, generate a charge
        if self.call_type == self.END_CALL_TYPE:

            # Get the origin Start Call
            _origin_start_call = Record.objects.filter(
                call_id=self.call_id, call_type=self.START_CALL_TYPE)

            if not _origin_start_call.exists():
                raise NotFound('Origin start call record not Found')

            # Verify the Start and End date/time consistency
            if _origin_start_call.get().timestamp > self.timestamp:
                raise ValidationError({
                    'detail':
                    "Date/time from origin "
                    "start call is higher then end call"
                })

            # Get the period string
            # The end call date determine his bill period
            _end_call_period = self.timestamp.strftime("%m%Y")

            self.source = _origin_start_call.get().source
            self.destination = _origin_start_call.get().destination

            # Get a already created bill for the subscriber on period
            _already_created_bill = Bill.objects.filter(
                period=_end_call_period, subscriber=self.source)
            if _already_created_bill.exists():
                self.bill = _already_created_bill.get()

            # If not found a already created Bill, create a new one
            else:
                _new_created_bill = Bill()
                _new_created_bill.subscriber = self.source
                _new_created_bill.period = _end_call_period
                _new_created_bill.save(self)

                self.bill = _new_created_bill

            # save the information of bill on the origin
            # start call record
            _origin_start_call.update(bill=self.bill)

            # save the current end call record
            super(Record, self).save(*args, **kwargs)

            # update the bill
            Bill().update_bill_record(_origin_start_call.get(), self)

        else:
            # Verify a start call with same source and destination number
            if (self.source == self.destination):
                raise ValidationError(
                    {'detail': 'Same source and destination numbers'})

            super(Record, self).save(*args, **kwargs)