Hello,
I have membership model that have consumed discount ids having consumed times and remaining times and discount in POS when add the product, and it has discount the discount applied based in the remain times however I update the remaining times when I create the payment not when I add line because if I add lines remove I can not retrieve it back I want to another logic to achieve this here's my function
class Partner(models.Model):
_inherit = 'res.partner'
customer_membership_ids = fields.One2many('customer.membership', 'partner_id', string='Customer Memberships')
def get_free_service_discount_for_product(self, product_id, categ_id, line_qty, pricelist_id,order_lines,appointment_id=None,):
product = self.env['product.product'].browse(int(product_id))
categ = self.env['product.category'].browse(int(categ_id))
pricelist = self.env['product.pricelist'].browse(int(pricelist_id))
order_lines = self.env['pos.order.line'].search([('product_id', '=', int(product_id))])
if not order_lines:
print(f"No order lines found for product {product_id}")
return False
order = order_lines[0].order_id
if not order:
print("Error: No order found for the provided product.")
return False
active_memberships = self.customer_membership_ids.filtered(lambda mem : mem.status=='active')
active_membership = False
pos_order_return = self.env.context.get('pos_order_return', False)
payment_created = self.env.context.get('payment_created', False)
discount = self.env.context.get('discount', False)
print(discount)
print(f"Context: {self.env.context}")
print(f"Received pos_order_return: {pos_order_return}, payment_created: {payment_created}")
print(f"Received appointment_id: {appointment_id}")
appointment=None
if appointment_id:
appointment = self.env['business.appointment'].browse(appointment_id)
if appointment:
membership= appointment.membership_id
print("mem",membership)
free_service_products = []
free_discount_products = []
free_discount_product_categs = []
options = {}
if appointment:
active_membership= appointment.membership_id
elif active_membership and active_memberships:
active_membership = active_memberships[0]
for membership in active_memberships:
free_discount_lines = membership.consumed_discount_ids.filtered(
lambda discount: (discount.times and discount.remaining_times) or (
not discount.times and not discount.remaining_times)
)
if free_discount_lines:
active_membership = membership
break
if appointment:
print("i got an appiotment")
active_membership= appointment.membership_id
elif active_membership and active_memberships:
print("no appiotment")
for membership in active_memberships:
valid_discount = membership.consumed_discount_ids.filtered(lambda discount: discount.remaining_times > 0)
if valid_discount:
active_membership = membership
break
else:
active_membership = active_memberships[0]
if active_membership:
free_service_lines = active_membership.consumed_service_ids.filtered(lambda service : (service.times and service.remaining_times) or (not service.times and not service.remaining_times))
free_service_products = free_service_lines.mapped('product_id')
free_discount_lines = active_membership.consumed_discount_ids.filtered(lambda discount : (discount.times and discount.remaining_times) or (not discount.times and not discount.remaining_times))
free_discount_products = free_discount_lines.mapped('product_id')
free_discount_product_categs = free_discount_lines.mapped('product_categ_id')
options['customer_membership_id'] = {'id': active_membership.id, 'name':active_membership.name, 'package_name':active_membership.membership_package_id.name}
for discount_line in free_discount_lines:
discount_line.remaining_times
if product in free_service_products:
options['price'] = 0
options['lst_price'] = 0
ser_prod_line = free_service_lines.filtered(lambda ser:ser.product_id == product)
options['consumed_service_id'] = ser_prod_line[0].id
elif product in free_discount_products:
dis_prod_line = free_discount_lines.filtered(lambda dis:dis.product_id == product)
options['discount'] = dis_prod_line[0].discount
options['consumed_discount_id'] = dis_prod_line[0].id
for order_line in order_lines:
print("discount",options['discount'])
print(
f"Discount found on product {product_id} in order {order.id}. Discount: {order_line.discount}%")
# dis_prod_line[0].consumed_times += 1
# dis_prod_line[0]._get_remaining()
if self.env.context.get('pos_order_state', 'draft') == 'draft':
if dis_prod_line[0].remaining_times > 0:
print("producuuuuuuuuuuuuuuuuuuuuuuuuct")
# dis_prod_line[0].consumed_times += 1
# dis_prod_line[0]._get_remaining()
update_applied = True
elif categ in free_discount_product_categs:
dis_categ_line = free_discount_lines.filtered(lambda dis:dis.product_categ_id == categ)
options['discount'] = dis_categ_line[0].discount
options['consumed_discount_id'] = dis_categ_line[0].id
else:
options['discount'] = 0
options['price'] = pricelist.sudo()._get_product_price(product, int(line_qty))
options['lst_price'] = pricelist.sudo()._get_product_price(product, int(line_qty))
options['customer_membership_id'] = False
options['consumed_service_id'] = False
options['consumed_discount_id'] = False