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.
61 lines
2.0 KiB
61 lines
2.0 KiB
# -*- coding: utf-8 -*-
|
|
from django.db import models
|
|
from django.db.models import Sum
|
|
|
|
from django.conf import settings
|
|
from pytils.numeral import choose_plural
|
|
from docs.models.base_models import BaseInvoiceModel, BaseItemInvoiceModel
|
|
from docs.models.linked_docs_mixin import LinkedDocsMixin
|
|
from docs import consts
|
|
|
|
|
|
class Invoice(BaseInvoiceModel, LinkedDocsMixin):
|
|
"""Счет."""
|
|
UNPAID = 1
|
|
PARTLY_PAID = 2
|
|
PAID = 3
|
|
|
|
PAID_CHOICES = (
|
|
(UNPAID, 'Нет'),
|
|
(PARTLY_PAID, 'Частично'),
|
|
(PAID, 'Да'),
|
|
)
|
|
|
|
paid_status = models.PositiveSmallIntegerField(
|
|
'Оплачен?',
|
|
choices=PAID_CHOICES,
|
|
default=UNPAID
|
|
)
|
|
closed_status = models.BooleanField('Закрыт?', choices=consts.BOOL_CHOICES, default=False)
|
|
|
|
class Meta(BaseInvoiceModel.Meta):
|
|
verbose_name = 'Счёт'
|
|
verbose_name_plural = 'Счета'
|
|
|
|
def to_string_for_json(self):
|
|
# Счёт №Х от ХХ.ХХ.ХХ, Х позиций, на сумму ХХ.
|
|
# import ipdb;ipdb.set_trace()
|
|
date_format = settings.DATE_INPUT_FORMATS[0]
|
|
count = self.invoice_items.count()
|
|
str_date = self.doc_date.strftime(format=date_format)
|
|
|
|
return '{name} № {number} от {date}, {count} {count_name}, на сумму {sum}'.format(
|
|
name=self._meta.verbose_name or '',
|
|
number=self.doc_num,
|
|
date=str_date,
|
|
count=count or '',
|
|
count_name=choose_plural(count, (u"позиция", u"позиции", u"позиций")),
|
|
sum=self.invoice_items.aggregate(Sum('total_price'))['total_price__sum']
|
|
)
|
|
|
|
def __str__(self):
|
|
return self.to_string_for_json()
|
|
|
|
|
|
class InvoiceItem(BaseItemInvoiceModel):
|
|
"""Табличная часть счета."""
|
|
parent = models.ForeignKey(Invoice, related_name='invoice_items')
|
|
|
|
class Meta(BaseItemInvoiceModel.Meta):
|
|
verbose_name = 'Табл. часть счета'
|
|
verbose_name_plural = 'Табл. части счетов'
|
|
|