Esempio n. 1
0
class StockEntry(models.Model):
    voucher_no = models.PositiveIntegerField(verbose_name=_('Voucher No.'))
    date = BSDateField(default=today, validators=[validate_in_fy], blank=True, null=True)
    description = models.CharField(max_length=255, blank=True, null=True)
    objects = FYManager()

    def save(self, *args, **kwargs):
        if self.__class__.objects.fiscal_year().filter(voucher_no=self.voucher_no).exclude(pk=self.pk):
            raise ValidationError(_('Voucher no. exists!'))
        super(StockEntry, self).save(*args, **kwargs)

    def __init__(self, *args, **kwargs):
        super(StockEntry, self).__init__(*args, **kwargs)

        if not self.pk and not self.voucher_no:
            self.voucher_no = get_next_voucher_no(StockEntry, 'voucher_no')

    def __str__(self):
        return str(self.voucher_no)

    class Meta:
        verbose_name_plural = 'Stock Entries'
Esempio n. 2
0
class Demand(models.Model):
    release_no = models.IntegerField()
    demandee = models.ForeignKey(User, related_name='demands')
    date = BSDateField(default=today, validators=[validate_in_fy])
    purpose = models.CharField(max_length=254)

    objects = FYManager()

    def get_voucher_no(self):
        return self.release_no

    def __init__(self, *args, **kwargs):
        super(Demand, self).__init__(*args, **kwargs)
        if not self.pk and not self.release_no:
            self.release_no = get_next_voucher_no_for_fy(self.__class__, 'release_no')

    def save(self, *args, **kwargs):
        if self.__class__.objects.fiscal_year().filter(release_no=self.release_no).exclude(pk=self.pk):
            raise ValidationError(_('Voucher no. exists!'))
        super(Demand, self).save(*args, **kwargs)

    def __str__(self):
        return unicode(self.release_no)

    @property
    def fiscal_year(self):
        return FiscalYear.from_date(self.date)

    @property
    def status(self):
        status_hash = {
            'Requested': 0,
            'Approved': 1,
            'Fulfilled': 2,
        }
        status_codes = []
        rows = self.rows.all()
        for row in rows:
            status_codes.append(status_hash[row.status])
        if not status_codes:
            return _('Empty')
        inv_hash = {v: k for k, v in status_hash.items()}
        status_text = _(inv_hash[min(status_codes)])
        if len(set(status_codes)) > 1:
            status_text = "%s %s" % (_('Partially'), _(inv_hash[max(status_codes)]))
        return status_text

    @property
    def status_code(self):
        status_hash = {
            'Requested': 0,
            'Approved': 1,
            'Fulfilled': 2,
        }
        status_codes = []
        rows = self.rows.all()
        for row in rows:
            status_codes.append(status_hash[row.status])
        if not status_codes:
            return None
        status_code = str(min(status_codes))
        if len(set(status_codes)) > 1:
            status_code = "%s%s" % ('0', str(max(status_codes)))
        return status_code