def set_amount(self, ar, amount): self.voucher.fill_defaults() # rule = self.get_vat_rule() # if rule is None: # rate = ZERO # else: # rate = rule.rate 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 total_base_changed(self, ar): """Called when user has edited the `total_base` field. If total_base has been set to blank, then Lino fills it using :meth:`reset_totals`. If user has entered a value, compute :attr:`total_vat` and :attr:`total_incl` from this value using the vat rate. If there is no VatRule, `total_incl` and `total_vat` are set to None. If there are rounding differences, `total_vat` will get them. """ # 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() # logger.info("20150128 %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 reset_totals(self, ar): super(QtyVatItemBase, self).reset_totals(ar) # if not self.voucher.auto_compute_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 and self.qty is not None: self.set_amount(ar, myround(self.unit_price * self.qty))
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_incl_changed(self, ar): """Called when user has edited the `total_incl` field. If total_incl has been set to blank, then Lino fills it using :meth:`reset_totals`. If user enters a value, compute :attr:`total_base` and :attr:`total_vat` from this value using the vat rate. If there is no VatRule, `total_incl` should be disabled, so this method will never be called. If there are rounding differences, `total_vat` will get them. """ 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() 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 load_vnl(self, row, **kw): jnl, year, number = row2jnl(row) if jnl is None: return if year < START_YEAR: return doc = self.VENDICT.get((jnl, year, number)) if doc is None: msg = "VNL {0} without document".format([jnl.ref, year, number]) dblogger.warning(msg) return # raise Exception(msg) # dblogger.info("20131116 %s %s",row.idjnl,row.iddoc) # doc = jnl.get_document(year,number) # try: # doc = jnl.get_document(year,number) # except Exception,e: # dblogger.warning(str(e)) # return # kw.update(document=doc) kw.update(seqno=int(row.line.strip())) idart = row.idart.strip() if isinstance(doc, sales.VatProductInvoice): if row.code in ('A', 'F'): kw.update(product=products.Product.get_by_ref(idart)) elif row.code == 'G': a = self.vnlg2product(row) if a is not None: kw.update(product=a) kw.update(unit_price=mton(row.prixu)) kw.update(qty=qton(row.qte)) elif isinstance(doc, vat.VatAccountInvoice): if row.code == 'G': kw.update(account=idart) kw.update(title=row.desig.strip()) vc = tax2vat(row.idtax) kw.update(vat_class=vc) mb = mton(row.cmont) mv = mton(row.montt) kw.update(total_base=mb) kw.update(total_vat=mv) if mb is not None and mv is not None: kw.update(total_incl=mb + mv) # kw.update(qty=row.idtax.strip()) # kw.update(qty=row.montt.strip()) # kw.update(qty=row.attrib.strip()) # kw.update(date=row.date) # check whether we need a vat rule if mv and mb: vatrule = dict(vat_class=vc, vat_regime=doc.vat_regime) vatrule.update(country=doc.partner.country or dd.plugins.countries.get_my_country()) try: VatRule.objects.get(**vatrule) except VatRule.DoesNotExist: vatrule.update(rate=myround(mv / mb)) yield VatRule(**vatrule) try: yield doc.add_voucher_item(**kw) except Exception as e: dblogger.warning("Failed to load VNL line %s from %s : %s", row, kw, e)
def load_vnl(self, row, **kw): jnl, year, number = row2jnl(row) if jnl is None: return if year < START_YEAR: return doc = self.VENDICT.get((jnl, year, number)) if doc is None: msg = "VNL {0} without document".format( [jnl.ref, year, number]) dblogger.warning(msg) return # raise Exception(msg) # dblogger.info("20131116 %s %s",row.idjnl,row.iddoc) # doc = jnl.get_document(year,number) # try: # doc = jnl.get_document(year,number) # except Exception,e: # dblogger.warning(str(e)) # return # kw.update(document=doc) kw.update(seqno=int(row.line.strip())) idart = row.idart.strip() if isinstance(doc, sales.VatProductInvoice): if row.code in ('A', 'F'): kw.update(product=products.Product.get_by_ref(idart)) elif row.code == 'G': a = self.vnlg2product(row) if a is not None: kw.update(product=a) kw.update(unit_price=mton(row.prixu)) kw.update(qty=qton(row.qte)) elif isinstance(doc, vat.VatAccountInvoice): if row.code == 'G': kw.update(account=idart) kw.update(title=row.desig.strip()) vc = tax2vat(row.idtax) kw.update(vat_class=vc) mb = mton(row.cmont) mv = mton(row.montt) kw.update(total_base=mb) kw.update(total_vat=mv) if mb is not None and mv is not None: kw.update(total_incl=mb+mv) # kw.update(qty=row.idtax.strip()) # kw.update(qty=row.montt.strip()) # kw.update(qty=row.attrib.strip()) # kw.update(date=row.date) # check whether we need a vat rule if mv and mb: vatrule = dict(vat_class=vc, vat_regime=doc.vat_regime) vatrule.update( country=doc.partner.country or dd.plugins.countries.get_my_country()) try: VatRule.objects.get(**vatrule) except VatRule.DoesNotExist: vatrule.update(rate=myround(mv / mb)) yield VatRule(**vatrule) try: yield doc.add_voucher_item(**kw) except Exception as e: dblogger.warning("Failed to load VNL line %s from %s : %s", row, kw, e)