delete_pending_purchases.py 1.62 KB
Newer Older
1 2 3 4
# -*- coding: utf-8 -*-
from django.utils import timezone

import datetime
Patrick's avatar
Patrick committed
5
from django.core.management.base import BaseCommand
6 7

from repanier.const import PERMANENCE_OPENED, DECIMAL_ZERO
Patrick's avatar
Patrick committed
8
from repanier.models import Permanence, CustomerInvoice
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
from django.conf import settings
from django.utils import translation


class Command(BaseCommand):
    args = '<none>'
    help = 'Open pre opened orders'

    def handle(self, *args, **options):
        from repanier.apps import REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS
        if REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS:
            now_less_one_hour = timezone.now() - datetime.timedelta(hours=1)
            translation.activate(settings.LANGUAGE_CODE)

            for permanence in Permanence.objects.filter(
                    status=PERMANENCE_OPENED
            ).order_by('?'):
                recently_updated_customer_invoice_qs = CustomerInvoice.objects.filter(
                    permanence_id=permanence.id,
                    is_order_confirm_send=False,
                    total_price_with_tax__gt=DECIMAL_ZERO,
                    purchase__is_updated_on__gte=now_less_one_hour
                ).distinct()
                customer_invoice_qs = CustomerInvoice.objects.filter(
                    permanence_id=permanence.id,
                    is_order_confirm_send=False,
                    total_price_with_tax__gt=DECIMAL_ZERO,
                ).exclude(
                    id__in=recently_updated_customer_invoice_qs
                )
                for customer_invoice in customer_invoice_qs:
Patrick's avatar
Patrick committed
40 41 42
                    customer_invoice.delete_if_unconfirmed(permanence)


43 44