def set_amount(self, ar, amount): self.voucher.fill_defaults() if self.voucher.vat_regime.item_vat: # unit_price_includes_vat self.total_incl = myround(amount) self.total_incl_changed(ar) else: self.total_base = myround(amount) self.total_base_changed(ar)
def create_entry(self, report, obj, **kwargs): kwargs.update(report=report) kwargs[self.fkname] = obj old = Balance(obj.old_d, obj.old_c) kwargs.update(old_d = old.d) kwargs.update(old_c = old.c) kwargs.update(during_d = myround(obj.during_d or ZERO)) #In Django2, does not respect the decimal_places for the output_field in the aggregation functions. kwargs.update(during_c = myround(obj.during_c or ZERO)) return self.entry_model(**kwargs)
def compute_totals(self): if self.pk is None or not self.state.editable: return base = Decimal() vat = Decimal() for i in self.items.all(): if i.total_base is not None: base += i.total_base if i.total_vat is not None: vat += i.total_vat self.total_base = myround(base) self.total_vat = myround(vat) self.total_incl = myround(vat + base)
def total_incl_changed(self, ar): if self.total_incl is None: self.reset_totals(ar) if self.total_incl is None: return # assert not isinstance(self.total_incl,basestring) rule = self.get_vat_rule(self.get_trade_type()) if rule is None: self.total_base = None self.total_vat = None else: self.total_base = myround(self.total_incl / (ONE + rule.rate)) self.total_vat = myround(self.total_incl - self.total_base)
def reset_totals(self, ar=None): super(QtyVatItemBase, self).reset_totals(ar) # if self.voucher.edit_totals: # if self.qty: # if self.voucher.item_vat: # self.unit_price = self.total_incl / self.qty # else: # self.unit_price = self.total_base / self.qty if self.unit_price is not None: if self.qty is None: self.set_amount(ar, myround(self.unit_price)) else: self.set_amount(ar, myround(self.unit_price * self.qty))
def guess_amount(self): if self.amount: return self.amount = ZERO if self.voucher.auto_compute_amount: q = self.voucher.items.exclude(id=self.id).annotate( models.Sum('amount')) # print("20210108", q[0].amount__sum) self.amount = -myround(q[0].amount__sum)
def mton(s, default=None): # PriceField s = s.strip() if not s: return default if s != "GRATIS": # TIM accepted an (erroneous) amount '36535..23' as 36535 # (omitting the part after the duplicated ".") i = s.find('..') if i != -1: s = s[:i] return myround(Decimal(s)) return Decimal()
def total_base_changed(self, ar): # dd.logger.info("20150128 total_base_changed %r", self.total_base) if self.total_base is None: self.reset_totals(ar) if self.total_base is None: return rule = self.get_vat_rule(self.get_trade_type()) # dd.logger.info("20180813 %r", rule) if rule is None: self.total_incl = None self.total_vat = None else: self.total_incl = myround(self.total_base * (ONE + rule.rate)) self.total_vat = self.total_incl - self.total_base
def compute_sums(self, report): collect = [] k = self.fkname + '__ref__startswith' for obj in self.outer_model.get_heading_objects(): qs = self.entry_model.objects.filter(**{k: obj.ref}) kw = dict() kw.update(old_d=models.Sum('old_d')) kw.update(old_c=models.Sum('old_c')) kw.update(during_d=models.Sum('during_d')) kw.update(during_c=models.Sum('during_c')) d = qs.aggregate(**kw) if d['old_d'] or d['old_c'] or d['during_d'] or d['during_c']: if d['during_d']: d['during_d'] = myround(d['during_d']) if d['during_c']: d['during_c'] = myround(d['during_c']) d['report'] = report d[self.fkname] = obj collect.append(self.entry_model(**d)) # print(20180905, d) for obj in collect: obj.full_clean() obj.save()
def compute_sums(self, report): collect = [] k = self.fkname + '__ref__startswith' for obj in self.outer_model.get_header_objects(): qs = self.entry_model.objects.filter(**{k: obj.ref}) kw = dict() kw.update(old_d=models.Sum('old_d')) kw.update(old_c=models.Sum('old_c')) kw.update(during_d=models.Sum('during_d')) kw.update(during_c=models.Sum('during_c')) d = qs.aggregate(**kw) if d['old_d'] or d['old_c'] or d['during_d'] or d['during_c']: if d['during_d']: d['during_d'] = myround(d['during_d']) if d['during_c']: d['during_c'] = myround(d['during_c']) d['report'] = report d[self.fkname] = obj collect.append(self.entry_model(**d)) # print(20180905, d) for obj in collect: obj.full_clean() obj.save()
def update_missing_rate(self, ar): Guest = rt.models.cal.Guest Event = rt.models.cal.Event # flt = Event.objects.filter( # gfk2lookup(Event.owner, self.course)) flt = {'event__'+k: v for k, v in gfk2lookup(Event.owner, self.course).items()} flt.update(partner=self.pupil) total = Guest.objects.filter(**flt).count() if total: missing = Guest.objects.filter( state__in=(GuestStates.missing, GuestStates.excused), **flt).count() self.missing_rate = myround(Decimal(missing*100) / total) else: self.missing_rate = ZERO self.full_clean() self.save() ar.success(refresh=True)