def export(output, transactions): """ Export a set of Transaction in ledger format. """ for transaction in transactions: dest = ("\t\t%(dest_organization)s:%(dest_account)s" % {'dest_organization': transaction.dest_organization, 'dest_account': transaction.dest_account}) dest_amount = as_money(transaction.dest_amount, transaction.dest_unit).rjust(60 - len(dest)) orig = ("\t\t%(orig_organization)s:%(orig_account)s" % {'orig_organization': transaction.orig_organization, 'orig_account': transaction.orig_account}) if transaction.dest_unit != transaction.orig_unit: orig_amount = as_money(transaction.orig_amount, transaction.orig_unit).rjust(60 - len(orig)) else: orig_amount = '' output.write(""" %(date)s #%(reference)s - %(description)s %(dest)s%(dest_amount)s %(orig)s%(orig_amount)s """ % {'date': datetime.datetime.strftime( transaction.created_at, '%Y/%m/%d %H:%M:%S'), 'reference': '%s' % transaction.event_id, 'description': transaction.descr, 'dest': dest, 'dest_amount': dest_amount, 'orig': orig, 'orig_amount': orig_amount})
def to_representation(self, obj): ret = super(TransactionSerializer, self).to_representation(obj) is_debit = self._is_debit(obj) if is_debit: amount = as_money(obj.orig_amount, '-%s' % obj.orig_unit) else: amount = as_money(obj.dest_amount, obj.dest_unit) ret.update({ 'description': as_html_description(obj), 'is_debit': is_debit, 'amount': amount}) return ret
def to_representation(self, obj): ret = super(TransactionSerializer, self).to_representation(obj) is_debit = self._is_debit(obj) if is_debit: amount = as_money(obj.orig_amount, '-%s' % obj.orig_unit) else: amount = as_money(obj.dest_amount, obj.dest_unit) ret.update({ 'description': as_html_description(obj, provider=get_current_provider()), 'is_debit': is_debit, 'amount': amount }) return ret
def to_representation(self, obj): ret = super(TransactionSerializer, self).to_representation(obj) ret.update({ 'description': as_html_description(obj), 'is_debit': self._is_debit(obj), 'amount': as_money(obj.dest_amount, '-%s' % obj.dest_unit if self._is_debit(obj) else obj.dest_unit)}) return ret
def export(output, transactions): """ Export a set of Transaction in ledger format. """ for transaction in transactions: dest = ("\t\t%(dest_organization)s:%(dest_account)s" % { 'dest_organization': transaction.dest_organization, 'dest_account': transaction.dest_account }) dest_amount = as_money(transaction.dest_amount, transaction.dest_unit).rjust(60 - len(dest)) orig = ("\t\t%(orig_organization)s:%(orig_account)s" % { 'orig_organization': transaction.orig_organization, 'orig_account': transaction.orig_account }) if transaction.dest_unit != transaction.orig_unit: orig_amount = as_money(transaction.orig_amount, transaction.orig_unit).rjust(60 - len(orig)) else: orig_amount = '' output.write( """ %(date)s #%(reference)s - %(description)s %(dest)s%(dest_amount)s %(orig)s%(orig_amount)s """ % { 'date': datetime.datetime.strftime(transaction.created_at, '%Y/%m/%d %H:%M:%S'), 'reference': '%s' % transaction.event_id, 'description': transaction.descr, 'dest': dest, 'dest_amount': dest_amount, 'orig': orig, 'orig_amount': orig_amount })
def to_representation(self, obj): ret = super(TransactionSerializer, self).to_representation(obj) ret.update({ 'description': as_html_description(obj), 'is_debit': self._is_debit(obj), 'amount': as_money( obj.dest_amount, '-%s' % obj.dest_unit if self._is_debit(obj) else obj.dest_unit) }) return ret
def htmlize_money(value, currency='usd'): text = as_money(value, currency) look = re.match(r'(\$|€|£)?(\d(\d|,)*)(\.\d+)?(.*)', text) if look: unit_prefix = '<span class="unit-prefix">%s</span>' % look.group(1) int_amount = look.group(2) frac_amount = look.group(4) unit_suffx = '<span class="unit-suffix">%s</span>' % look.group(5) if frac_amount == '.00': frac_amount = ( '<span class="frac-digits zero-frac-digits">%s</span>' % frac_amount) else: frac_amount = ('<span class="frac-digits">%s</span>' % frac_amount) html = unit_prefix + int_amount + frac_amount + unit_suffx return mark_safe(html) return text
def get_invoicable_options(subscription, created_at=None, prorate_to=None, coupon=None): """ Return a set of lines that must charged Today and a set of choices based on current subscriptions that the user might be willing to charge Today. """ #pylint: disable=too-many-locals created_at = datetime_or_now(created_at) option_items = [] plan = subscription.plan # XXX Not charging setup fee, it complicates the design too much # at this point. # Pro-rated to billing cycle prorated_amount = 0 if prorate_to: prorated_amount = plan.prorate_period(created_at, prorate_to) discount_percent = 0 descr_suffix = None if coupon: discount_percent = coupon.percent descr_suffix = '(code: %s)' % coupon.code if plan.period_amount == 0: # We are having a freemium business models, no discounts. option_items += [Transaction.objects.new_subscription_order( subscription, 1, prorated_amount, created_at, descr_suffix="free")] elif plan.unlock_event: # Locked plans are free until an event. option_items += [Transaction.objects.new_subscription_order( subscription, 1, plan.period_amount, created_at, DESCRIBE_UNLOCK_NOW % { 'plan': plan, 'unlock_event': plan.unlock_event}, discount_percent=discount_percent, descr_suffix=descr_suffix)] option_items += [Transaction.objects.new_subscription_order( subscription, 1, 0, created_at, DESCRIBE_UNLOCK_LATER % { 'amount': as_money(plan.period_amount, plan.unit), 'plan': plan, 'unlock_event': plan.unlock_event})] else: natural_periods = [1] if plan.advance_discount > 0: # Give a chance for discount when paying periods in advance if plan.interval == Plan.MONTHLY: natural_periods = [1, 3, 6, 12] elif plan.interval == Plan.YEARLY: natural_periods = [1, 2, 3] for nb_periods in natural_periods: option_items += [Transaction.objects.new_subscription_order( subscription, nb_periods, prorated_amount, created_at, discount_percent=discount_percent, descr_suffix=descr_suffix)] discount_percent += plan.advance_discount if discount_percent >= 100: break # never allow to be completely free here. return option_items
def humanize_money(value, currency='usd'): return as_money(value, currency)
def get_invoicable_options(subscription, created_at=None, prorate_to=None, coupon=None): """ Return a set of lines that must charged Today and a set of choices based on current subscriptions that the user might be willing to charge Today. """ #pylint: disable=too-many-locals created_at = datetime_or_now(created_at) option_items = [] plan = subscription.plan # XXX Not charging setup fee, it complicates the design too much # at this point. # Pro-rated to billing cycle prorated_amount = 0 if prorate_to: prorated_amount = plan.prorate_period(created_at, prorate_to) discount_percent = 0 if coupon: discount_percent = coupon.percent if plan.period_amount == 0: # We are having a freemium business models, no discounts. option_items += [subscription.create_order(1, prorated_amount, created_at, "free")] elif plan.unlock_event: # Locked plans are free until an event. option_items += [subscription.create_order(1, plan.period_amount, created_at, DESCRIBE_UNLOCK_NOW % { 'plan': plan, 'unlock_event': plan.unlock_event}, discount_percent=discount_percent)] option_items += [subscription.create_order(1, 0, created_at, DESCRIBE_UNLOCK_LATER % { 'amount': as_money(plan.period_amount, plan.unit), 'plan': plan, 'unlock_event': plan.unlock_event})] elif plan.interval == Plan.MONTHLY: # Give a change for discount when paying periods in advance for nb_periods in [1, 3, 6, 12]: option_items += [subscription.create_order( nb_periods, prorated_amount, created_at, discount_percent=discount_percent)] discount_percent += 10 if discount_percent >= 100: discount_percent = 100 elif plan.interval == Plan.YEARLY: # Give a change for discount when paying periods in advance for nb_periods in [1]: # XXX disabled discount until configurable. option_items += [subscription.create_order( nb_periods, prorated_amount, created_at, discount_percent=discount_percent)] discount_percent += 10 if discount_percent >= 100: discount_percent = 100 else: raise IntegrityError(#pylint: disable=nonstandard-exception "Cannot use interval specified for plan '%s'" % plan) return option_items
def get_invoicable_options(subscription, created_at=None, prorate_to=None, coupon=None): """ Return a set of lines that must charged Today and a set of choices based on current subscriptions that the user might be willing to charge Today. """ #pylint: disable=too-many-locals created_at = datetime_or_now(created_at) option_items = [] plan = subscription.plan # XXX Not charging setup fee, it complicates the design too much # at this point. # Pro-rated to billing cycle prorated_amount = 0 if prorate_to: prorated_amount = plan.prorate_period(created_at, prorate_to) discount_percent = 0 descr_suffix = None if coupon: discount_percent = coupon.percent descr_suffix = '(code: %s)' % coupon.code if plan.period_amount == 0: # We are having a freemium business models, no discounts. option_items += [ Transaction.objects.new_subscription_order(subscription, 1, prorated_amount, created_at, descr_suffix="free") ] elif plan.unlock_event: # Locked plans are free until an event. option_items += [ Transaction.objects.new_subscription_order( subscription, 1, plan.period_amount, created_at, DESCRIBE_UNLOCK_NOW % { 'plan': plan, 'unlock_event': plan.unlock_event }, discount_percent=discount_percent, descr_suffix=descr_suffix) ] option_items += [ Transaction.objects.new_subscription_order( subscription, 1, 0, created_at, DESCRIBE_UNLOCK_LATER % { 'amount': as_money(plan.period_amount, plan.unit), 'plan': plan, 'unlock_event': plan.unlock_event }) ] else: natural_periods = [1] if plan.advance_discount > 0: # Give a chance for discount when paying periods in advance if plan.interval == Plan.MONTHLY: natural_periods = [1, 3, 6, 12] elif plan.interval == Plan.YEARLY: natural_periods = [1, 2, 3] for nb_periods in natural_periods: option_items += [ Transaction.objects.new_subscription_order( subscription, nb_periods, prorated_amount, created_at, discount_percent=discount_percent, descr_suffix=descr_suffix) ] discount_percent += plan.advance_discount if discount_percent >= 100: break # never allow to be completely free here. return option_items