0

I want to in every time I created a data on One2many field, at the same time I want it to save as data on my maintenance.equipment. I try to find the solution on other module in addons but i didn't yet find the answer.

The scenario is, before validating the shipment in my product, I need to input serial number on it. Each serial number I created to that product at the same time serves as my equipment name.

this is the picture for the scenario

and that serial number which is sample1010, I need it to become my equipment name in module maintenance.equipment. I expected it to display here in my equipment.

my equipment module

All i taught is only i need to do is creating Many2one and One2many fields like this

class StockPackOperation(models.Model):
    _inherit = 'stock.pack.operation'

    lines_ids = fields.One2many('maintenance.equipment', 'lines_id')
    sample = fields.Char(string="Sample")

class MaintenanceEquipment(models.Model):
    _inherit = 'maintenance.equipment'

    lines_id = fields.Many2one('stock.pack.operation')

but nothings happen. Any help or suggestion or advice please. I need to do this. Thanks in advice masters. Anw I am new in odoo.

law rence
  • 169
  • 1
  • 18
  • You need to override def create in class maintenance.equipment.In that create method you need to generate a serial key based on the sequence you require(ir.sequence) and write the same sequence in the serial number field by passing the generated sequence in the vals that calls the super method of create and set the serial number in _rec_name. That way your serial number will be generated on every create in one2many and would generate a serial number along with the data as in your picture. – Omi Harjani Mar 05 '18 at 01:37
  • I need to clarify that, what want is, when you assign a assign a serial number, you want to create maintenance.equipment line with the same name as that of serial number ,right? – Asmita Chavan Mar 05 '18 at 06:20
  • yes @chavan_asmita. You are right. I still working on it base on omi harjani told me to do – law rence Mar 05 '18 at 06:51
  • do you have any suggestion @chavan_asmita?? please help me. Thanks – law rence Mar 05 '18 at 08:24
  • what omi harjani has told you to do will work in this way: When you will create maintenance.equipment record, it will create serial lot number with the same name. do you want me to help you in that, or the other way ? – Asmita Chavan Mar 06 '18 at 08:37

1 Answers1

0

This can be achieved by inheriting stock.pack.operation.lot class, as entered serial numbers are stored in this class with lot_name (in case of incoming shipment) and with lot_id (in case of outgoing shipment).

You will not need to care about outgoing shipment, as in outgoing shipment we select already existing serial numbers. class StockPackOperationLot(models.Model): _inherit = 'stock.pack.operation.lot'

@api.model
def create(self, vals):
    res = super(StockPackOperationLot, self).create(vals)
    if vals.get('lot_name'):
        self.env['maintenance.equipment'].create({'name': vals.get('lot_name')})
    return res

@api.multi
def write(self, vals):
    if vals.get('lot_name'):
        lot_name = self.lot_name
        equipment_id = self.env['maintenance.equipment'].search([('name', '=', lot_name)])
        res = super(StockPackOperationLot, self).write(vals)
        equipment_id.name = vals.get('lot_name')
        return res
    else:
        return super(StockPackOperationLot, self).write(vals)

For this functionality to work properly, you will need to make sure equipment name is unique, else you will need to store related equipment id in each stock.pack.operation.lot record, so that when user edits serial number, equipment is also updated, when there is no unique constraint on name of equipment.

hope this helps you...

Asmita Chavan
  • 171
  • 3
  • 7