I need to make a method which only counts when selecting the attribute written in the selection "sale", I have always done this function with the attribute itself but never with selection, can someone please help me?
To count in the way that it used to be through the following code, but it does not work because it tells me all the movements, then I require that I count the sales made by a worker and not buy them.
def get_sales_made (self):
for rec in self:
sales_counter = len (rec.document_ids)
I have 3 tables called "type of movement", document "and" worker ", so that I can only count the sales I have to ring" type of movement "with" document "since the latter is a foreign key. Below is the table with the attributes.
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):
for rec in self:
sales_counter = len (rec.document_ids.type_movement_id)
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')
I used another method to count but it didn't work:
def get_sales_made (self):
count = self.env ['project_rc.type_movement']. search_count ([('type_movement', '=', 'sale')])
self.counter_sale = count
view cron
<record forcecreate = "True" id = "vendor_monthly_counter" model = "ir.cron">
<field name = "name"> Monthly worker accountant </field>
<field eval = "True" name = "active" />
<field name = "user_id" ref = "base.user_root" />
<field name = "interval_number"> 1 </field>
<field name = "interval_type"> months </field>
<field name = "numbercall"> - 1 </field>
<field ref = "model_proyecto_rc_worker" name = "model_id" />
<field name = "state"> code </field>
<field name = "code"> model.get_realized_ sales () </field>
<field eval = "False" name = "doall" />
<field name = "function"> True </field>
</record>
</data>
Be carreful using your function get_sales_made
count = self.env ['project_rc.type_movement']. search_count ([('type_movement', '=', 'sale')])
// You're counting the number of TYPES, not the number of DOCUMENTS
self.counter_sale = count
What I suppose you want to realise is :
@api.depends('document_ids')
def get_sales_made(self):
for rec in self:
document = rec.document_ids.filtered(lambda r: r.type_movement_id and r.type_movement_id.type_movement == 'sale')
rec.sales_counter = len(document)
The @api.depends tells Odoo that your field depends on the field document_ids, and will be calculated every time this one is changed. The filtered funciton catch every document of type 'sale'