Esempio n. 1
0
class CartItem(models.Model):
    id = fields.AutoField(
        verbose_name=_('ID'),
        primary_key=True,
    )
    cart = models.ForeignKey(
        Cart,
        related_name='items',
        on_delete=models.CASCADE,
        null=False,
        blank=False,
    )
    product = models.ForeignKey(
        Product,
        related_name='items',
        on_delete=models.CASCADE,
        null=False,
        blank=False,
    )
    term = fields.PositiveSmallIntegerField(
        verbose_name=_('Term'),
        null=False,
        blank=False,
        choices=Product.Payment_Terms,
    )
    unit_price = fields.DecimalField(
        verbose_name=_('Unit Price'),
        max_digits=10,
        decimal_places=4,
        null=False,
        blank=False,
    )
    quantity = fields.PositiveSmallIntegerField(
        verbose_name=_('Quantity'),
        null=False,
        blank=False,
        default=1,
    )
    item_price = fields.DecimalField(
        verbose_name=_('Item Price'),
        max_digits=10,
        decimal_places=4,
        null=False,
        blank=False,
    )
    records = models.Manager()

    class Meta:
        db_table = 'billing_cart_items'
        default_related_name = 'items'
        verbose_name = _('Cart Item')
        verbose_name_plural = _('Cart Items')

    def __unicode__(self):
        return str(self.id)
Esempio n. 2
0
class DecimalFieldDefinition(FieldDefinition):
    max_digits = fields.PositiveSmallIntegerField(
        _('max digits'), help_text=max_digits_help_text)
    decimal_places = fields.PositiveSmallIntegerField(
        _('decimal_places'), help_text=decimal_places_help_text)

    objects = FieldDefinitionManager()

    class Meta(_NumericMeta):
        app_label = 'numeric'
        defined_field_class = fields.DecimalField
        defined_field_options = (
            'max_digits',
            'decimal_places',
        )
Esempio n. 3
0
class DebtLoan(models.Model):
    DEBT = 0
    LOAN = 1
    CATEGORY_CHOICES = (
        (DEBT, 'debt'),
        (LOAN, 'loan'),
    )

    with_who = fields.CharField(max_length=255)
    title = fields.CharField(max_length=255, null=True, blank=True)
    amount = fields.DecimalField(max_digits=10, decimal_places=2)
    category = fields.PositiveSmallIntegerField(choices=CATEGORY_CHOICES)
    created = fields.DateTimeField(default=timezone.now, editable=False)
    modified = fields.DateTimeField(default=timezone.now)
    active = fields.BooleanField(default=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        if self.title:
            return "{}: {}".format(self.with_who, self.title)
        else:
            return "{}".format(self.with_who)

    def deactivate(self):
        if self.active:
            self.active = False
            self.save()
Esempio n. 4
0
class PaymentDetails(_Model):
    from django.db.models.fields import related as _related
    from django.db.models import deletion as _deletion
    from django.db.models import fields as _field
    from datetime import datetime as _dt

    company = _related.ForeignKey(
        'holding.Company',
        on_delete=_deletion.CASCADE,
        null=False,
        blank=False,
    )

    rate = _related.ForeignKey(
        'pay.Rate',
        on_delete=_deletion.CASCADE,
        verbose_name="Выбранный тариф",
        null=False,
        blank=False,
    )

    discount = _related.ForeignKey(
        'pay.Discount',
        on_delete=_deletion.CASCADE,
        null=True,
        blank=False,
    )

    start = _field.DateTimeField(
        "Дата начала действия тарифа",
        default=_dt.now,
    )

    discount_percent = _field.PositiveSmallIntegerField(
        "Процент скидки на момент платежа",
        default=None,
        null=True,
        blank=True,
    )

    def save(self,
             force_insert=False,
             force_update=False,
             using=None,
             update_fields=None):
        if not self.discount:
            self.discount = self.company.discount

        if self.discount:
            self.discount_percent = self.discount.percent

        super().save(force_insert=force_insert,
                     force_update=force_update,
                     using=using,
                     update_fields=update_fields)

    class Meta:
        verbose_name = "Дополнительные детали"
        verbose_name_plural = "Детали оплат"
Esempio n. 5
0
class ArrayFieldDefinition(FieldDefinition):
    base_field = DjangoField(_('base field'))
    size = fields.PositiveSmallIntegerField(_('size'), null=True, blank=True)

    class Meta(_PostgresMeta):
        app_label = 'postgres'
        defined_field_class = pg_fields.ArrayField
        defined_field_options = ('base_field', 'size')
Esempio n. 6
0
class CharFieldDefinition(FieldDefinition):
    max_length = fields.PositiveSmallIntegerField(
        _('max length'), blank=True, null=True
    )

    objects = FieldDefinitionManager()

    class Meta:
        app_label = 'text'
        defined_field_class = fields.CharField
        defined_field_options = ('max_length',)
        defined_field_description = _('String')
        defined_field_category = _('Text')
Esempio n. 7
0
class Discount(_Model):
    from django.db.models import fields as _field

    name = _field.CharField(
        verbose_name="Название скидки",
        max_length=200,
        null=False,
        blank=False,
    )

    percent = _field.PositiveSmallIntegerField(
        verbose_name="Процент",
        default=0,
    )

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "Скидка"
        verbose_name_plural = "Скидки"
Esempio n. 8
0
class Product(models.Model):
    Payment_Types = (
        (0, _('Free')),
        (1, _('One-Time')),
        (2, _('Recurring')),
    )
    Payment_Terms = (
        (0, _('One-Time')),
        (1, _('Hourly')),
        (24, _('Daily')),
    )
    id = fields.AutoField(
        verbose_name=_('ID'),
        primary_key=True,
    )
    store = models.ForeignKey(
        Store,
        related_name='products',
        null=False,
        blank=False,
    )
    main_category = models.ForeignKey(
        Category,
        verbose_name=_('Main Category'),
        related_name='products',
        null=False,
        blank=False,
    )
    quantity = fields.PositiveSmallIntegerField(
        verbose_name=_('Quantity'),
        null=False,
        blank=False,
        default=1,
    )
    enable_stock_control = fields.BooleanField(
        verbose_name=_('Enable Stock Control'),
        null=False,
        blank=False,
        default=False,
    )
    payment_types = fields.PositiveSmallIntegerField(
        verbose_name=_('Payment Type'),
        choices=Payment_Types,
        null=False,
        blank=False,
        default=2,
    )
    is_active = fields.BooleanField(
        verbose_name=_('Is Active'),
        null=False,
        blank=False,
        default=True,
        db_index=True,
    )
    create_date = fields.DateTimeField(
        verbose_name=_('Create Date'),
        null=False,
        blank=True,
        auto_now_add=True,
    )
    update_date = fields.DateTimeField(
        verbose_name=_('Update Date'),
        null=True,
        blank=True,
        auto_now=True,
    )
    category = models.ManyToManyField(
        'Category',
        verbose_name=_('Categories'),
        related_name='categories',
        db_table='billing_product_categories',
    )
    records = models.Manager()

    class Meta:
        db_table = 'billing_product'
        default_related_name = 'products'
        verbose_name = _('Product')
        verbose_name_plural = _('Products')

    def __unicode__(self):
        if self.store.default_language is not None:
            return ProductLang.records.get(
                product=self,
                language=self.store.default_language,
            ).title
        else:
            return str(self.id)
Esempio n. 9
0
class Logbook(_Model):
    from django.db.models.fields import related as _related
    from django.db.models import deletion as _deletion
    from django.db.models import fields as _field
    from app.fields import timezone as _timezone

    employee = _related.ForeignKey(
        'holding.Employee',
        on_delete=_deletion.SET_NULL,
        null=True,
        blank=False,  # For admin panel?
        related_name="logbooks",
    )

    start = _field.DateTimeField("Начало отрезка")
    end = _field.DateTimeField("Конец отрезка")

    activity = _field.PositiveSmallIntegerField(
        verbose_name="Активность",
        default=0,
    )

    mood = _field.PositiveSmallIntegerField(
        verbose_name="Настроение",
        default=0,
    )

    fatigue = _field.PositiveSmallIntegerField(
        verbose_name="Усталость",
        default=0,
    )

    timezone = _timezone.TimeZoneField(
        verbose_name="Временная зона",
        null=True,
        blank=True,
    )

    def save(self,
             force_insert=False,
             force_update=False,
             using=None,
             update_fields=None):
        if self.timezone is None:
            self.timezone = self.company.timezone

        return super(Logbook, self).save(force_insert, force_update, using,
                                         update_fields)

    @property
    def employee_fullname(self):
        return "%s %s" % (self.employee.first_name, self.employee.last_name)

    employee_fullname.fget.short_description = "Сотрудник"

    @property
    def company(self):
        return self.employee.company

    company.fget.short_description = "Компания"

    @property
    def company_name(self):
        return self.company.name

    company_name.fget.short_description = "Компания"

    def __str__(self):
        return "%s %s [%s]:(%s - %s)" % (
            self.employee.first_name,
            self.employee.last_name,
            self.start.date(),
            self.start.time(),
            self.end.time(),
        )

    class Meta:
        verbose_name = "Показатель сотрудника"
        verbose_name_plural = "Показатели сотрудников"
Esempio n. 10
0
class TransactionCheck(models.Model):
    """
    Represents an in-progress or completed check of a transaction for
    correctness.

    The ``TransactionCheck`` gets created once the check starts and has a flag
    to track completeness.
    """

    transaction = models.ForeignKey(
        Transaction,
        on_delete=models.CASCADE,
        related_name="checks",
    )

    completed = fields.BooleanField(default=False)
    """True if all of the checks expected to be carried out against the models
    in this transaction have recorded any result."""

    successful = fields.BooleanField(null=True)
    """
    True if all of the checks carried out against the models in this
    transaction returned a positive result.
    
    This value will be null until ``completed`` is `True`.
    """

    head_transaction_id: int
    head_transaction = models.ForeignKey(
        Transaction,
        on_delete=models.CASCADE,
    )
    """
    The latest transaction in the stream of approved transactions (i.e. in the
    REVISION partition) at the moment this check was carried out.

    Once new transactions are commited and the head transaction is no longer the
    latest, this check will no longer be an accurate signal of correctness
    because the new transactions could include new data which would invalidate
    the checks. (Unless the checked transaction < head transaction, in which
    case it will always be correct.)
    """

    tracked_model_count = fields.PositiveSmallIntegerField()
    """
    The number of tracked models in the transaction at the moment this check was
    carried out.

    If something is removed from the transaction later, the number of tracked
    models will no longer match. This is used to detect if the check is now
    stale.
    """

    latest_tracked_model = models.ForeignKey(
        TrackedModel,
        on_delete=models.CASCADE,
        null=True,
    )
    """
    The latest tracked model in the transaction at the moment this check was
    carried out.
    
    If some models are removed and subsequent ones added to the transaction, the
    count may be the same but the latest transaction will have a new primary
    key. This is used to detect if the check is now stale.
    """

    model_checks: models.QuerySet["TrackedModelCheck"]

    objects: TransactionCheckQueryset = models.Manager.from_queryset(
        TransactionCheckQueryset, )()

    def save(self, *args, **kwargs):
        """Computes the metadata we will need later to detect if the check is
        current and fresh."""
        if not self.head_transaction_id:
            self.head_transaction = Transaction.approved.last()

        self.tracked_model_count = self.transaction.tracked_models.count()
        self.latest_tracked_model = self.transaction.tracked_models.order_by(
            "pk", ).last()

        return super().save(*args, **kwargs)

    class Meta:
        ordering = (
            "transaction__partition",
            "transaction__order",
            "head_transaction__partition",
            "head_transaction__order",
        )

        constraints = (models.CheckConstraint(
            check=(models.Q(completed=False, successful__isnull=True)
                   | models.Q(completed=True, successful__isnull=False)),
            name="completed_checks_include_successfulness",
        ), )
Esempio n. 11
0
class ArticleCategory(models.Model):
    name = fields.CharField(max_length=50, null=False, blank=False)
    slug = fields.SlugField(auto_created=True)
    ordering = fields.PositiveSmallIntegerField(db_index=True)