def add_total_value_to_document(self, billing_date, invoice=None,
                                    proforma=None):

        ONE_DAY = datetime.timedelta(days=1)

        if self.is_billed_first_time:
            if not self.trial_end:  # has no trial
                if billing_date.month in [self.start_date.month,
                                          next_month(self.start_date)]:
                    self._log_value_state('first time, without trial')

                    # The same month or the next one as the start_date

                    # Generate first invoice, when the subscription starts
                    # => add the prorated value of the plan for the current month
                    end_date = self._get_interval_end_date(date=self.start_date)
                    self._add_plan_value(start_date=self.start_date,
                                         end_date=end_date,
                                         invoice=invoice, proforma=proforma)

                    if (self.state == self.STATES.CANCELED and
                        self.cancel_date.month == self.start_date.month and
                        self.cancel_date == billing_date.month
                    ):
                        # It was canceled the same month it started, with `now`
                        # option and since it got here (by permissions of
                        # Subscription.should_be_billed) => the customer does
                        # not have consolidated billing => add the mfs as this
                        # is the last subscription of the customer and (s)he
                        # is probably leaving the service.
                        self._add_mfs(start_date=self.start_date,
                                      end_date=end_date,
                                      invoice=invoice, proforma=proforma)

                    if billing_date.month == next_month(self.start_date):
                        # If it gets here => a new subscription, with no trial
                        # and the customer has other subscriptions => it's
                        # an old customer.
                        # Add consumed mfs from last month
                        self._add_mfs(start_date=self.start_date,
                                      end_date=end_date,
                                      invoice=invoice, proforma=proforma)

                        if self.state == self.STATES.ACTIVE:
                            # If the subscription is still active, add the
                            # plan's value for the month ahead
                            bsd = self.bucket_start_date(
                                reference_date=billing_date)
                            bed = self.bucket_end_date(
                                reference_date=billing_date)
                            self._add_plan_value(
                                start_date=bsd, end_date=bed,
                                invoice=invoice, proforma=proforma)
            elif self.on_trial(billing_date):
                self._log_value_state('first time, on trial')
                # Next month after the subscription has started with trial
                # spanning over >=2 months
                end_date = self._get_interval_end_date(date=self.start_date)
                self._add_trial_value(start_date=self.start_date,
                                      end_date=end_date,
                                      invoice=invoice, proforma=proforma)
            else:
                self._log_value_state('first time, after trial')
                # Billed first time, right after trial end

                # Is billed right after trial in the same month as start_date
                # => add the trial value and the prorated value for the rest
                # of the month

                # Add the value of the plan + the value of the consumed mfs
                # during the trial
                end_date = self._get_interval_end_date(date=self.start_date)
                self._add_trial_value(start_date=self.start_date,
                                      end_date=end_date,
                                      invoice=invoice, proforma=proforma)

                first_day_after_trial = self.trial_end + ONE_DAY
                # If it got here (by permission of `should_be_billed()`
                # => it has to be either ACTIVE or CANCELED
                end_date = None
                if self.state == self.STATES.ACTIVE:
                    # Add the value for the rest of the month if the
                    # subscription is still active
                    end_date = self.bucket_end_date(
                        reference_date=first_day_after_trial)
                elif self.state == self.STATES.CANCELED:
                    # The subscription was canceled after the trial, during
                    # the prorated period of the month => add the value
                    # only between trial_end -> cancel_date
                    if self.cancel_date >= first_day_after_trial:
                        end_date = self.cancel_date

                if first_day_after_trial.month == self.trial_end.month:
                    # Add the prorated value of the plan between
                    # trial_end -> end_of_month only if the first day after
                    # trial belongs to the same month as the trial end.
                    # Exception e.g.: trial_end = 31.08.2015.
                    # trial_end + ONE_DAY = 1.09.2015 => don't add any prorated
                    # value.

                    if (self.state == self.STATES.ACTIVE or
                        (self.state == self.STATES.CANCELED and
                         self.cancel_date >= self.trial_end)
                    ):
                        # Add the prorated plan value for trial_end -> end_of_month
                        # if the subscription is active or if it was canceled
                        # between trial_end -> end_of_month.
                        # note: it could have been canceled before trial_end
                        # and in that case the value does not have to be added
                        self._add_plan_value(start_date=first_day_after_trial,
                                            end_date=end_date,
                                            invoice=invoice, proforma=proforma)

                        if billing_date.month == next_month(self.start_date):
                            # It's the next month after the subscription start
                            # and there was a prorated period between trial_end ->
                            # end_of_month => add the consumed metered features
                            # from that period.
                            self._add_mfs(start_date=first_day_after_trial,
                                          end_date=end_date,
                                          invoice=invoice, proforma=proforma)

                if (self.state == self.STATES.ACTIVE and
                    billing_date.month == next_month(self.start_date)
                ):
                    # It's billed next month after the start date and it is
                    # still active => add the prorated value for the next month
                    bsd = self.bucket_start_date(reference_date=billing_date)
                    bed = self.bucket_end_date(reference_date=billing_date)
                    self._add_plan_value(start_date=bsd, end_date=bed,
                                         invoice=invoice, proforma=proforma)
        else:
            last_billing_date = self.last_billing_date
            if (self.trial_end and
                (self.trial_end.month == billing_date.month or
                 self.trial_end.month == prev_month(billing_date)) and
                last_billing_date < self.trial_end
            ):
                self._log_value_state('billed before, with trial')
                # It has/had a trial which ends this month or it ended last
                # month and it has been billed before
                # => expect the following items:
                #   * the remaining trial value since last billing
                #   * the value of the prorated subscription
                #   * the value of the subscription ahead
                # note: it will get here only after the trial has ended
                # We test if trial_end was this month or the previous one.

                # Add trial value
                # The end_date will be either the normal end_date of the
                # bucket or the cancel_date (if the subscription is canceled)
                bsd = self.bucket_start_date(reference_date=last_billing_date)
                bed = self.bucket_end_date(reference_date=last_billing_date)
                if self.state == self.STATES.CANCELED:
                    if bsd <= self.cancel_date <= bed:
                        # Was scheduled for canceling sometimes during the
                        # trial period
                        bed = self.cancel_date

                self._add_trial_value(start_date=bsd, end_date=bed,
                                      invoice=invoice, proforma=proforma)

                # Add the plan's value for the period after the trial
                # The end_date will be either the normal end_date of the
                # bucket or the cancel_date (if the subscription is canceled)
                first_day_after_trial = self.trial_end + ONE_DAY

                if first_day_after_trial.month == self.trial_end.month:
                    # The same month as the trial end => add prorated value of the
                    # subscription between first_day_after_trial and end of the
                    # month
                    bsd = self.bucket_start_date(
                        reference_date=first_day_after_trial)
                    bed = self.bucket_end_date(
                        reference_date=first_day_after_trial)
                    if self.state == self.STATES.CANCELED:
                        if bsd <= self.cancel_date <= bed:
                            bed = self.cancel_date

                    if (self.state == self.STATES.ACTIVE or
                        self.cancel_date >= self.trial_end
                    ):
                        # Add the prorated value only if the subscription is still
                        # active or it was canceled during the period right after
                        # the trial. If it was canceled during the trial, skip
                        # adding the mfs, as there was no active period after
                        # trial_end
                        self._add_plan_value(start_date=bsd, end_date=bed,
                                             invoice=invoice, proforma=proforma)

                        if billing_date.month == next_month(first_day_after_trial):
                            # If there was a period of paid subscription between
                            # trial_end -> last_month_end=> add the consumed mfs
                            # for that period.
                            # Note: the bsd and bed that were previously
                            # computed are being used
                            self._add_mfs(start_date=bsd, end_date=bed,
                                          invoice=invoice, proforma=proforma)

                # The subscription was not canceled and we bill it the
                # next month after the trial end => add the value of the
                # subscription for the next month
                if (self.state == self.STATES.ACTIVE and
                    billing_date.month == next_month(self.trial_end)
                ):
                    # It's the next month after the trial end => add the value
                    # for the month ahead
                    bsd = self.bucket_start_date(reference_date=billing_date)
                    bed = self.bucket_end_date(reference_date=billing_date)
                    self._add_plan_value(start_date=bsd, end_date=bed,
                                        invoice=invoice, proforma=proforma)

            else:
                self._log_value_state('billed before, normal')
                # This is the normal case of a subscription which was billed
                # sometimes at the beginning of last month or it's the next
                # month after the trial_end of a subscription whose customer
                # does not have consolidated billing => the subscription was
                # billed after trial_end.

                # Add mfs for the last month or part of month
                bsd = self.bucket_start_date(reference_date=last_billing_date)
                bed = self.bucket_end_date(reference_date=last_billing_date)
                self._add_mfs(start_date=bsd, end_date=bed,
                              invoice=invoice, proforma=proforma)

                if self.state == self.STATES.ACTIVE:
                    # Add the plan's value for the month ahead
                    bsd = self.bucket_start_date(reference_date=billing_date)
                    bed = self.bucket_end_date(reference_date=billing_date)

                    self._add_plan_value(start_date=bsd, end_date=bed,
                                         invoice=invoice, proforma=proforma)

        BillingLog.objects.create(subscription=self, invoice=invoice,
                                  proforma=proforma, billing_date=billing_date)
Resets the state tracker to prepare for processing a new page.
dataset/ETHPy150Open PressLabs/silver/silver/models.py/Subscription.add_total_value_to_document--crossover with--dataset/ETHPy150Open google/closure-linter/closure_linter/statetracker.py/StateTracker.Reset