Skip to Content
Menu
This question has been flagged
1 Reply
3101 Views

I have the following code where I send by means of onchange and I send 
that the value entered in limit is automatically saved in limit_id which
 is in class hr. and I want to do the following before arriving the value entered in limit
to limit_id a subtraction limited minus the amount that contains the
limit_id field of each worker at that moment and show the remaining
amount since each worker can have a value different in limit_id The way I'm doing it is bad because it only subtracts the first worker and
 that result applies to all workers and as I mentioned above I want you to
 subtract each worker and show the remaining value depending on the amount
 you have in limit_d someone could help me please

class DiningRoomLimit (models.Model):     _name = 'dining.room.limit'     _order = 'sequence'     name = fields.Char (readonly = True, create = "false", store = True)     limit = fields.Float (string = 'Credit Limit', required = True, digits = (64, 2))     sequence = fields.Integer (required = True, default = 1,)     employee = fields.Many2one ('hr.employee', 'invoice',)     currency_id = fields.Many2one ('res.currency', 'Currency',         default = lambda self: self.env.user.company_id.currency_id.id, required = True)     @ api.onchange ('limit')     def _onchange_limit (self):         for r in self:             result = self.env ['hr.employee']. search ([])             h = r.limit             for record in result:                 j = h - record.limit_id                          result.write ({                 'limit_id': j,                 }) hr class class DiningRoomIndexCord (models.Model):     _inherit = ['hr.employee']     limit_id = fields.Float (string = 'Credit Limit', digits = (64, 2))

Avatar
Discard
Best Answer

Hi,

result = self.env ['hr.employee']. search ([])

--> result means all employees. And then:

result.write ({
                'limit_id': j,
                })

means "Update limit_id for all employees".

You need to update each record:

for record in result:
           j = h - record.limit_id 
           record.write ({
                'limit_id': j,
                })

'write(...)' is in for loop.

 

Avatar
Discard
Author

hello Thanh Loyal I did it as you said and does the tour but only updates the value of the last employee

Please make sure that the two lines: 'j = ...' and 'record.wirte(...)' have same line indent. In your code, I see that 'result.write(...)' is aligned with 'for' instead of 'j = ...'

Author

thank you

Author

a question brother

I'm trying this

I wish to do the following

I have a class invoice, I have an inheritance to hr.employee and I have another class called discount

I do the following recorded several invoices to ones employees with credit status

now from the discunt class I want to take a tour and krow which invoices each employee must then make a subtraction to the amount that will be deducted from all the employees minus each of the structures the invoice you owe, if the invoice is the amount of an invoice is equal to the discount pass the payment status and leave the other invoices in credit status, if the amount of the invoice is greater than reacts the subtraction and shows the remaining total, if the amount of the invoice is less than the send a paid status and the remaining total of the discount is subtracted to another contract.

this is my class invoice

class DiningRoom(models.Model):

_name = 'dining.room.invoice'

_order = 'create_date desc'

_rec_name = 'number_invoice'

number_invoice = fields.Char(default=lambda self: _('New'), required=True, string='Nro. de Factura')

amount_untaxed = fields.Float(string='Base imponible', readonly=True, track_visibility='always', compute='_amount_all', store=True, digits=(64, 2))

amount_total = fields.Float(string='Total', readonly=True, track_visibility='always', compute='_amount_all', store=True, digits=(64, 2))

reference = fields.Char(string='Referencia Bancaria:',)

worker = fields.Many2one('hr.employee', 'Ficha del Trabajador:')

client = fields.Many2one('dining.room.client', 'Cedula del Cliente:')

currency_id = fields.Many2one('res.currency', 'Currency',

default=lambda self: self.env.user.company_id.currency_id.id, required=True)

product_id = fields.Many2one('dining.products', related='product_ids.product_id', string='Comida')

product_ids = fields.One2many('dining.room.order', 'order_id', string='Productos', required=True, copy=True, store=True)

state = fields.Selection([

('draft', 'Borrador'),

('credit', 'Credito'),

('transfer', 'Tranferencia'),

('cash', 'Efectivo'),

('cancel', 'Anular'),

('paymet', 'Pago'),

], string=_('Status'), readonly=True, copy=False, index=True, default='draft',)

model_room = fields.Selection([

('credit', "Credito"),

('transfer', "Transferencia"),

('cash', "Efectivo"),

],string="Pago", required=True, default='credit')

date = fields.Datetime(string='Fecha', default=fields.Datetime.now)

date2 = fields.Date(string='Fecha')

option = fields.Selection([

('worker','trabajador'),

('client','cliente')

],string="opcion", required=True, default='worker')

prueba = fields.Float(string='Base imponible', digits=(64, 2))

this is my class discount

class DiningRoomDiscount(models.Model):

_name = 'dining.room.discount'

bond_id = fields.Many2one('dining.room.configuration.bond', 'bono',)

currency_id = fields.Many2one('res.currency', 'Currency',

default=lambda self: self.env.user.company_id.currency_id.id, required=True)

@api.multi

def discount_method(self):

for r in self:

bond = r.bond_id.vat

worker = r.worker.name

result = self.env['dining.room.invoice'].search([('state','ilike','credit'),('amount_total', '>=', bond)])

return result.write({

'state': 'paymet',

})

you have knowledge about that and forgive the inconvenience