# Path: invoicing/schedules.py
# def recurring_invoice_scheduler(instance=None):
# def generate_child_invoice(**kwargs):
# def generate_child_invoice_hook(task):


from django.dispatch import receiver
from django.db.models.signals import post_save, pre_save
from invoicing.models import *
from haystack.management.commands import update_index
from django.utils import timezone
from invoicing import schedules
from django.forms import ValidationError
from django.utils.translation import ugettext_lazy as _
import invoicing.views as invoicing

@receiver(post_save)  # don't specify sender, so it is fired on all model saves
def update_search_index(sender, instance, created, **kwargs):
    """
    receiver to update the search index whenever a model is saved
    """
    watched_models = [Invoice, Account]
    # if sender in watched but do NOT trigger when saving the model when saving PDF (no point)
    if sender in watched_models and not getattr(instance, 'save_pdf', False):
        update_index.Command().handle(interactive=False)


def recurring(instance=None):
    """
    Function to handle the creation of child invoices
    """
    if instance.invoice_number:
        # ensure child recurring invoice does not produce its own child
        if int(getattr(instance, 'recurring', False)) not in dict(Invoice.RECURRING[:1]) and getattr(instance,
                                                                                                'parent_invoice',
                                                                                                False):
            raise ValidationError(_('A child invoice cannot itself be recurring'))
        # ensure invoice is not its own parent
        try:
            if getattr(instance, 'invoice_number', False) == getattr(instance, 'parent_invoice', False).invoice_number:
                raise ValidationError(_('An invoice cannot be it\'s own parent ... stop messing around!'))
        except AttributeError:
            pass  # thrown if no invoice_number for parent, so can discard as if no parent, no worries!
        # if status >= ISSUED, call the scheduler to start/stop recurring
        if getattr(instance, 'invoice_status', False) in dict(Invoice.INVOICE_STATUS[1:]):

# Path: invoicing/schedules.py
# def recurring_invoice_scheduler(instance=None):
# def generate_child_invoice(**kwargs):
# def generate_child_invoice_hook(task):


# Path: invoicing/signals.py
from django.dispatch import receiver
from django.db.models.signals import post_save, pre_save
from invoicing.models import *
from haystack.management.commands import update_index
from django.utils import timezone
from invoicing import schedules
from django.forms import ValidationError
from django.utils.translation import ugettext_lazy as _
import invoicing.views as invoicing


@receiver(post_save)  # don't specify sender, so it is fired on all model saves
def update_search_index(sender, instance, created, **kwargs):
    """
    receiver to update the search index whenever a model is saved
    """
    watched_models = [Invoice, Account]
    # if sender in watched but do NOT trigger when saving the model when saving PDF (no point)
    if sender in watched_models and not getattr(instance, 'save_pdf', False):
        update_index.Command().handle(interactive=False)


def recurring(instance=None):
    """
    Function to handle the creation of child invoices
    """
    if instance.invoice_number:
        # ensure child recurring invoice does not produce its own child
        if int(getattr(instance, 'recurring', False)) not in dict(Invoice.RECURRING[:1]) and getattr(instance,
                                                                                                'parent_invoice',
                                                                                                False):
            raise ValidationError(_('A child invoice cannot itself be recurring'))
        # ensure invoice is not its own parent
        try:
            if getattr(instance, 'invoice_number', False) == getattr(instance, 'parent_invoice', False).invoice_number:
                raise ValidationError(_('An invoice cannot be it\'s own parent ... stop messing around!'))
        except AttributeError:
            pass  # thrown if no invoice_number for parent, so can discard as if no parent, no worries!
        # if status >= ISSUED, call the scheduler to start/stop recurring
        if getattr(instance, 'invoice_status', False) in dict(Invoice.INVOICE_STATUS[1:]):

            schedules.recurring_invoice_scheduler(instance=instance)