Beispiel #1
0
def processSubscriptions():
  with transaction.atomic():
    startOfMonth, endOfMonth = monthRange()
    lineItems = {}
    for planCls in find_api_classes('models', SubscriptionPlan):
      plans = planCls.objects.all()
      for plan in plans:
        for subscription in plan.subscriptions.filter(active=True):
          if subscription.identity not in lineItems:
            lineItems[subscription.identity] = {'subscriptions': [], 'lineItems': []}

          items = plan.process(subscription)
          funcLog().info("Processed subscription %s", subscription)

          if len(items) > 0 and subscription not in lineItems[subscription.identity]['subscriptions']:
            lineItems[subscription.identity]['subscriptions'].append(subscription)
            lineItems[subscription.identity]['lineItems'] += items
    invoices = []
    for identity, data in lineItems.iteritems():
      invoice = Invoice.bundleLineItems(identity, endOfMonth, data['lineItems'])
      if invoice:
        funcLog().info("Created invoice %s", invoice)
        invoices.append(invoice)
      for subscription in data['subscriptions']:
        subscription.save()
    for invoice in invoices:
      invoice.draft = False
      invoice.save()
Beispiel #2
0
    def createLineItems(self, subscription, processDate):
      targetIdentity = self.identity
      if targetIdentity is None:
        targetIdentity = subscription.identity
      planOwner = subscription.identity
      startOfMonth, endOfMonth = monthRange(processDate)

      funcLog().info("Processing subscription of %s dues for %s, billing to %s",
          self.rank, self.identity, planOwner)
      endOfMonth += datetime.timedelta(days=1)

      #FIXME: membershipRanges was removed
      #for range in targetIdentity.membershipRanges:
      #  if range['start'] <= startOfMonth and range['end'] >= endOfMonth:
      #    return []
      #  if RankLineItem.objects.filter(rank=self.rank, identity=targetIdentitiy,
      #      activeFromDate=startOfMonth, activeToDate=endOfMonth).exists():
      #    return []

      return [RankLineItem(
        rank = self.rank,
        identity = targetIdentity,
        activeFromDate = startOfMonth,
        activeToDate = endOfMonth
      ),]
Beispiel #3
0
 def getMembershipLineItemsForMonth(self, date=None):
   monthStart, monthEnd = monthRange(date)
   billedMonths = self.rankLineItems.filter(
     activeFromDate__gte=monthStart,
     activeToDate__lte=monthEnd
   )
   return billedMonths
Beispiel #4
0
 def generateMonthlyInvoice(self):
   if not self.billedForMonth():
     if self.highestRank is not None and self.highestRank.monthlyDues > 0:
       startOfMonth, endOfMonth = monthRange()
       invoice = Invoice.objects.create(
         user=self.user,
         dueDate=endOfMonth,
       )
       for group in self.user.groups.all():
         if group.rank.monthlyDues > 0:
           lineItem = RankLineItem.objects.create(
             rank = group.rank,
             member = self,
             activeFromDate=startOfMonth,
             activeToDate=endOfMonth,
             invoice=invoice
           )
       invoice.draft = False
       invoice.open = True
       invoice.save()
       return invoice
   return None
Beispiel #5
0
    def createLineItems(self, subscription, processDate):
      targetMember = self.member
      if targetMember is None:
        targetMember = subscription.user.member
      planOwner = subscription.user
      startOfMonth, endOfMonth = monthRange(processDate)

      funcLog().info("Processing subscription of %s dues for %s, billing to %s", self.rank, self.member, planOwner)
      endOfMonth += datetime.timedelta(days=1)

      for range in targetMember.membershipRanges:
        if range['start'] <= startOfMonth and range['end'] >= endOfMonth:
          return []
        if RankLineItem.objects.filter(rank=self.rank, member=targetMember,
            activeFromDate=startOfMonth, activeToDate=endOfMonth).exists():
          return []

      return [RankLineItem(
        rank = self.rank,
        member = targetMember,
        activeFromDate = startOfMonth,
        activeToDate = endOfMonth
      ),]