Search code examples
pythonodoo

counter method does not count well


I have a method that tells the times that a worker makes a sale, but it does not work because it only marks me that he has made 1 but has actually done 5. Next I leave an image and the code used to guide me.

**class Worker (models.Model):**

    _name = 'project_rc.worker'

    sales_counter = fields.Integer (string = "Sales made", compute = "get_sales_made")
    document_ids = fields.One2many (comodel_name = 'project_rc.document', 
    inverse_name = 'worker_id', string = 'Invoice')

def get_sales_made (self):

      count = self.env ['project_rc.type_movement']. search_count ([('type_movement', '=', 'sale')])

      self.counter_sale = count


**class Document (models.Model):**

    type_movement_id = fields.Many2one (comodel_name = 'project_rc.type_movement', string = "Movement type")

    worker_id = fields.Many2one (asdel_name = 'project_rc.worker', string = "Worker")

**class Type_Movement (models.Model):**

    type_movement = fields.Selection ([('purchase', 'Purchase'), ('sale', 'Sale'), ('merma', 'Merma')], string = "Movement type")

    document_ids = fields.One2many (comodel_name = 'project_rc.document', inverse_name = 'type_movimiento_id', string = 'Document')

Sample picture: https://ibb.co/vs0dw5K


Solution

  • You are searching in the wrong table it should be project_rc.document

    self.env['project_rc.document'].search_count([('type_movement_id.type_movement', '=', 'sale')
                                                  ('worker_id', '=', rec.id)
                                                 ])
    

    Or you can simply filter document_ids to count sales.