You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.7 KiB
75 lines
2.7 KiB
from django.db.models.signals import post_save
|
|
from django.dispatch import receiver
|
|
from django.core.mail import send_mail, EmailMultiAlternatives
|
|
from django.template.loader import get_template, render_to_string
|
|
|
|
from .models import WithDraw, InvoiceHistory, Transaction
|
|
from projects.models import Stage
|
|
|
|
|
|
@receiver(post_save, sender=WithDraw)
|
|
def send_for_accountant(sender, instance, created, **kwargs):
|
|
|
|
if created:
|
|
ctx_dict = {
|
|
'username': instance.user.username,
|
|
}
|
|
subject, from_email, to = 'Заявка на распечатку', 'mukhtar@mukhtar', 'muhtarzubanchi05@gmail.com'
|
|
text_content = render_to_string('send_for_accountant.txt', ctx_dict)
|
|
html_content = get_template('send_for_accountant.html').render(ctx_dict)
|
|
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
|
|
msg.send()
|
|
|
|
|
|
@receiver(post_save, sender=Transaction)
|
|
def add_invoice_history(sender, instance, created, **kwargs):
|
|
if 'add' in instance.type and instance.complete:
|
|
inv_history = InvoiceHistory()
|
|
inv_history.comment = 'Пополнение счета'
|
|
inv_history.sum = instance.sum
|
|
inv_history.user = instance.customer
|
|
inv_history.type = "score"
|
|
inv_history.save()
|
|
|
|
|
|
@receiver(post_save, sender=Stage)
|
|
def add_contractor_inv_from_stage(sender, instance, created, **kwargs):
|
|
if instance.close_contractor and instance.close_customer and instance.is_paid:
|
|
inv = InvoiceHistory()
|
|
inv.comment = 'Сумма за завершение этапа ' + instance.name + ' заказа ' + str(instance.order)
|
|
inv.sum = instance.cost
|
|
inv.type = "score"
|
|
if instance.order.contractor:
|
|
inv.user = instance.order.contractor
|
|
else:
|
|
inv.user = instance.order.team.owner
|
|
inv.save()
|
|
|
|
|
|
@receiver(post_save, sender=Transaction)
|
|
def reserve_stages(sender, instance, created, **kwargs):
|
|
if 'reservation' in instance.type and instance.complete:
|
|
order = None
|
|
stages_names = []
|
|
stages_ids_raw = instance.stages_id
|
|
if stages_ids_raw:
|
|
stages_ids = [s for s in stages_ids_raw.split(';') if s]
|
|
for pk in stages_ids:
|
|
stage = Stage.objects.get(pk=pk)
|
|
stages_names.append(stage.name)
|
|
stage.is_paid = True
|
|
order = stage.order
|
|
stage.save()
|
|
|
|
inv_history = InvoiceHistory()
|
|
inv_history.comment = 'Резервирование средств за этапы ' + ' , '.join(stages_names) + ' заказа' + str(order)
|
|
inv_history.sum = instance.sum
|
|
inv_history.user = instance.customer
|
|
inv_history.type = "history"
|
|
inv_history.save()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|