dashboard/billing/management/commands/generate_invoice.py

46 lines
2.0 KiB
Python

# Copyright © 2022 Aravinth Manivannan <realaravinth@batsense.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.core.management.base import BaseCommand
from django.core.exceptions import ValidationError
from django.conf import settings
from django.contrib.auth import get_user_model
from oauth2_provider.models import get_application_model
from oauth2_provider.generators import generate_client_id, generate_client_secret
from accounts.utils import gen_secret
from dash.models import Instance
from infrastructure.models import InstanceCreated
from billing.utils import generate_invoice, payment_fullfilled
Application = get_application_model()
class Command(BaseCommand):
help = "Generate invoices, should be run from cronjob scheduled for daily execution"
def handle(self, *args, **options):
instances = Instance.objects.all()
if instances:
for paid_instance in InstanceCreated.objects.all():
self.stdout.write(f"Found instance: {instance}")
if not payment_fullfilled(instance=paid_instance.instance):
self.stdout.write(
f"Payment not fulfilled for instance: {paid_instance.instance}"
)
payment = generate_invoice(instance=paid_instance.instance)
else:
self.stdout.write("No instances available")