2014-02-27 14:10 GMT+01:00 Iasmini - Gmail <[email protected]>:

> Em 27/02/2014 08:50, Cédric Krier escreveu:
>
>  On 27 Feb 08:43, Iasmini - Gmail wrote:
>>
>>> Hi,
>>>
>>> I need to do something just before saving. I did this:
>>>
>>> class Invoice:
>>>      __name__ = 'account.invoice'
>>> ...
>>>      def save(self):
>>>          if self.rps_number == '':
>>>              if not self.create_rps_number():
>>>                  self.raise_user_error(u'xxxx!')
>>>
>>>          super(Invoice, self).save()
>>>
>>> But this function is not called. Why?
>>>
>> Because save is a recent method added for syntactic sugar and so it is
>> not always (almost never) called.
>> I think what you are looking for is the validate method:
>>
>>      http://doc.tryton.org/3.0/trytond/doc/ref/models/models.
>> html?highlight=validate#trytond.model.ModelStorage.validate
>>
>>  I did on validate(). I'm setting the value of the field numero_rps with
> the created numero_rps by method gera_numero_rps() called on validate().
> But on invoice this field is getting null after saving. Why? Save method is
> called after validate(), right?
>
> I tried with self and with invoice (putting @staticmethod before function).
>
> Here is the code:
>
> class Invoice:
> ...
>     @classmethod
>     def validate(cls, invoices):
>         super(Invoice, cls).validate(invoices)
>
>         # gera número do rps antes de salvar
>         for invoice in invoices:
>             if invoice.numero_rps == '' or invoice.numero_rps == None:
>                 if not invoice.gera_numero_rps():
>                     invoice.raise_user_error(u'xxx!')
>

this raise_user_error (and the 'if') is unnecessary because
gera_numbero_rps() never returns False. Read below.


    def gera_numero_rps(self):
>         go_on = True
>
>         try:
>             NumeracaoRPS = Pool().get('nfse_br.numeracao_rps')
>
>             numeracao_rps = NumeracaoRPS.search([
>                 ('tipo_rps', '=', self.tipo_rps),
>                 ('serie_rps', '=', self.serie),
>                 ('company', '=', self.company)])
>
>             if numeracao_rps:
>                 numeracao_rps[0].ultimo_numero_rps +=
> numeracao_rps[0].ultimo_numero_rps
>                 NumeracaoRPS.write(numeracao_rps, {
>                     'ultimo_numero_rps': numeracao_rps[0].ultimo_
> numero_rps})
>             else:
>                 go_on = False
>                 self.raise_user_error(u'Deve ser criada a numeração do RPS
> de acordo com o tipo do RPS, série e'
>                                       u' emitente da NFS-e antes de
> processar!')
>                 return
>

this return isn't never called because you are raising an exception before.

            self.numero_rps = str(numeracao_rps[0].ultimo_numero_rps)
>         except:
>             go_on = False
>             raise
>
>         return go_on
>

as you are raising and exception for all invalid cases (go_on = False), the
return is unnecessary: if the method doesn't raise exception is because
always has done OK.


Here
>
> self.numero_rps = str(numeracao_rps[0].ultimo_numero_rps)
>
> I'm setting invoice.numero_rps, but in database is null. I traced and it's
> > 0.


in gera_numero_rps() you are modifying the instance (changing values). I
think you can't modify nothing in validate, only check and raise exception
if something is wrong.

So, *you shoud call gera_numero_rps() in create() and write() functions*.

-- 
Guillem Barba
http://www.guillem.alcarrer.net

Reply via email to