Skip to content

Commit

Permalink
[IMP] event_additinal_material: Pasar todo el proceso de evento a reg…
Browse files Browse the repository at this point in the history
…istro de evento.
  • Loading branch information
alfredoavanzosc committed Jul 30, 2021
1 parent e8fa549 commit 4d3686a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 43 deletions.
37 changes: 0 additions & 37 deletions event_additional_material/models/event_event.py
Expand Up @@ -13,40 +13,3 @@ class EventEvent(models.Model):
additional_material_ids = fields.One2many(
string='Additional materials',
comodel_name='event.additional.material', inverse_name='event_id')

def put_in_sale_order_additional_material(self, sale):
for event in self.filtered(lambda x: x.additional_material_ids):
for additional_material in event.additional_material_ids:
line = sale.order_line.filtered(
lambda a: a.product_id == additional_material.product_id)
if not line:
event.create_sale_line_with_additional_material(
sale, additional_material)
else:
event.update_sale_line_from_additional_material(
line, additional_material)

def create_sale_line_with_additional_material(self, sale, material):
product_uom_qty = self.calculate_additional_material_qty(material)
vals = {
'order_id': sale.id,
'product_id': material.product_id.id,
'name': material.product_id.name,
'product_uom': material.product_id.uom_id.id,
'product_uom_qty': product_uom_qty,
'price_unit': 1}
line = self.env['sale.order.line'].create(vals)
line.product_id_change()
line.price_unit = material.price_unit

def update_sale_line_from_additional_material(self, sale_line, material):
product_uom_qty = self.calculate_additional_material_qty(material)
vals = {
'product_uom_qty': product_uom_qty,
'price_unit': material.price_unit}
sale_line.write(vals)

def calculate_additional_material_qty(self, material):
num_registrations = len(self.registration_ids.filtered(
lambda x: x.state == 'open'))
return material.product_uom_qty * num_registrations
45 changes: 42 additions & 3 deletions event_additional_material/models/event_registration.py
Expand Up @@ -8,9 +8,48 @@ class EventRegistration(models.Model):

def action_confirm(self):
result = super(EventRegistration, self).action_confirm()
self.put_in_sale_order_additional_material()
return result

def put_in_sale_order_additional_material(self):
for registration in self.filtered(
lambda x: x.event_id.add_mat_automatically and
x.sale_order_id):
registration.event_id.put_in_sale_order_additional_material(
registration.sale_order_id)
return result
event = registration.event_id
for additional_material in event.additional_material_ids:
line = registration.sale_order_id.order_line.filtered(
lambda a: a.product_id == additional_material.product_id)
if not line:
registration.create_sale_line_with_additional_material(
registration.sale_order_id,
additional_material)
else:
registration.update_sale_line_from_additional_material(
line, additional_material)

def create_sale_line_with_additional_material(self, sale, material):
product_uom_qty = self.calculate_additional_material_qty(material)
vals = {
'order_id': sale.id,
'product_id': material.product_id.id,
'name': material.product_id.name,
'product_uom': material.product_id.uom_id.id,
'product_uom_qty': product_uom_qty,
'price_unit': 1}
line = self.env['sale.order.line'].create(vals)
line.product_id_change()
line.price_unit = material.price_unit

def update_sale_line_from_additional_material(self, sale_line, material):
product_uom_qty = self.calculate_additional_material_qty(material)
vals = {
'product_uom_qty': product_uom_qty,
'price_unit': material.price_unit}
sale_line.write(vals)

def calculate_additional_material_qty(self, material):
num_registrations = len(self.event_id.registration_ids.filtered(
lambda x: x.state == 'open' and x.sale_order_id
and x.sale_order_id == self.sale_order_id))
return (material.product_uom_qty * num_registrations
if num_registrations else 0)
Expand Up @@ -13,6 +13,4 @@ def action_put_material_from_registration(self):
self.ensure_one()
registrations = self.env['event.registration'].browse(
self.env.context.get('active_ids'))
for registration in registrations.filtered(lambda x: x.sale_order_id):
registration.event_id.put_in_sale_order_additional_material(
registration.sale_order_id)
registrations.put_in_sale_order_additional_material()

0 comments on commit 4d3686a

Please sign in to comment.