forked from uncloud/uncloud
Add initial generate-bills and charge-negative-balance uncloud-pay
commands
This commit is contained in:
parent
ef5e7e8035
commit
059791e2f2
10 changed files with 275 additions and 12 deletions
|
|
@ -0,0 +1,23 @@
|
|||
from django.core.management.base import BaseCommand
|
||||
from uncloud_auth.models import User
|
||||
from uncloud_pay.models import Order, Bill
|
||||
from uncloud_pay.helpers import get_balance_for
|
||||
|
||||
from datetime import timedelta
|
||||
from django.utils import timezone
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Generate bills and charge customers if necessary.'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
pass
|
||||
|
||||
def handle(self, *args, **options):
|
||||
users = User.objects.all()
|
||||
print("Processing {} users.".format(users.count()))
|
||||
for user in users:
|
||||
balance = get_balance_for(user)
|
||||
if balance < 0:
|
||||
print("User {} has negative balance ({}), charging.".format(user.username, balance))
|
||||
# TODO: charge
|
||||
print("=> Done.")
|
||||
48
uncloud/uncloud_pay/management/commands/generate-bills.py
Normal file
48
uncloud/uncloud_pay/management/commands/generate-bills.py
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
from django.core.management.base import BaseCommand
|
||||
from uncloud_auth.models import User
|
||||
from uncloud_pay.models import Order, Bill
|
||||
|
||||
from datetime import timedelta
|
||||
from django.utils import timezone
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Generate bills and charge customers if necessary.'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
pass
|
||||
|
||||
# TODO: check for existing bills
|
||||
def handle(self, *args, **options):
|
||||
customers = User.objects.all()
|
||||
print("Processing {} users.".format(customers.count()))
|
||||
for customer in customers:
|
||||
orders = Order.objects.filter(owner=customer)
|
||||
|
||||
# Pay all non-billed usage untill now.
|
||||
bill_starting_date = timezone.now()
|
||||
bill_ending_date = timezone.now()
|
||||
|
||||
billed_orders = []
|
||||
for order in orders:
|
||||
print(order)
|
||||
if True: # FIXME
|
||||
billed_orders.append(order)
|
||||
|
||||
# Update starting date if need be.
|
||||
if order.starting_date < bill_starting_date:
|
||||
bill_starting_date = order.starting_date
|
||||
|
||||
if len(billed_orders) > 0:
|
||||
bill = Bill(owner=customer,
|
||||
starting_date=bill_starting_date,
|
||||
ending_date=bill_starting_date,
|
||||
due_date=timezone.now() + timedelta(days=10))
|
||||
bill.save()
|
||||
|
||||
for order in billed_orders:
|
||||
print(order)
|
||||
order.bill.add(bill)
|
||||
|
||||
print("Created bill {} for user {}".format(bill.uuid, customer.username))
|
||||
|
||||
print("=> Done.")
|
||||
Loading…
Add table
Add a link
Reference in a new issue