[MERGE] forward port branch saas-11.3 up to a9f3cf76b93

This commit is contained in:
Christophe Simonis
2018-11-16 14:27:04 +01:00
4 changed files with 161 additions and 15 deletions
+8 -4
View File
@@ -229,10 +229,9 @@ class SaleCouponProgram(models.Model):
valid_products = program._get_valid_products(products)
ordered_rule_products_qty = sum(products_qties[product] for product in valid_products)
# Avoid program if 1 ordered foo on a program '1 foo, 1 free foo'
if program._is_valid_product(program.reward_product_id) and program.reward_type == 'product':
line = order.order_line.filtered(lambda line: line.product_id == program.reward_product_id)
if program.promo_applicability == 'on_current_order' and \
program._is_valid_product(program.reward_product_id) and program.reward_type == 'product':
ordered_rule_products_qty -= program.reward_product_quantity
# needed_quantity = program.rule_min_quantity if self.
if ordered_rule_products_qty >= program.rule_min_quantity:
valid_programs |= program
return valid_programs
@@ -267,7 +266,12 @@ class SaleCouponProgram(models.Model):
# Product requirement should not be checked if the coupon got generated by a promotion program (the requirement should have only be checked to generate the coupon)
if not next_order:
programs = programs and programs._filter_programs_on_products(order)
programs = programs and programs._filter_not_ordered_reward_programs(order)
programs_curr_order = programs.filtered(lambda p: p.promo_applicability == 'on_current_order')
programs = programs.filtered(lambda p: p.promo_applicability == 'on_next_order')
if programs_curr_order:
# Checking if rewards are in the SO should not be performed for rewards on_next_order
programs += programs_curr_order._filter_not_ordered_reward_programs(order)
return programs
def _is_valid_partner(self, partner):
+43 -11
View File
@@ -217,13 +217,24 @@ class SaleOrder(models.Model):
self.write({'order_line': [(0, False, value) for value in self._get_reward_line_values(program)]})
def _create_reward_coupon(self, program):
coupon = self.env['sale.coupon'].create({
'program_id': program.id,
'state': 'reserved',
'partner_id': self.partner_id.id,
'order_id': self.id,
'discount_line_product_id': program.discount_line_product_id.id
})
# if there is already a coupon that was set as expired, reactivate that one instead of creating a new one
coupon = self.env['sale.coupon'].search([
('program_id', '=', program.id),
('state', '=', 'expired'),
('partner_id', '=', self.partner_id.id),
('order_id', '=', self.id),
('discount_line_product_id', '=', program.discount_line_product_id.id),
], limit=1)
if coupon:
coupon.write({'state': 'reserved'})
else:
coupon = self.env['sale.coupon'].create({
'program_id': program.id,
'state': 'reserved',
'partner_id': self.partner_id.id,
'order_id': self.id,
'discount_line_product_id': program.discount_line_product_id.id
})
self.generated_coupon_ids |= coupon
return coupon
@@ -258,8 +269,17 @@ class SaleOrder(models.Model):
return programs
def _get_applied_coupon_program_coming_from_another_so(self):
# TODO: Remove me in master as no more used
pass
def _get_valid_applied_coupon_program(self):
self.ensure_one()
programs = self.applied_coupon_ids.mapped('program_id')._filter_programs_from_common_rules(self, True)
# applied_coupon_ids's coupons might be coming from:
# * a coupon generated from a previous order that benefited from a promotion_program that rewarded the next sale order.
# In that case requirements to benefit from the program (Quantity and price) should not be checked anymore
# * a coupon_program, in that case the promo_applicability is always for the current order and everything should be checked (filtered)
programs = self.applied_coupon_ids.mapped('program_id').filtered(lambda p: p.promo_applicability == 'on_next_order')._filter_programs_from_common_rules(self, True)
programs += self.applied_coupon_ids.mapped('program_id').filtered(lambda p: p.promo_applicability == 'on_current_order')._filter_programs_from_common_rules(self)
return programs
def _create_new_no_code_promo_reward_lines(self):
@@ -334,18 +354,26 @@ class SaleOrder(models.Model):
self.ensure_one()
order = self
applicable_programs = order._get_applicable_no_code_promo_program() + order._get_applicable_programs() + order._get_applied_coupon_program_coming_from_another_so()
applied_programs = order._get_applied_programs_with_rewards_on_current_order()
applicable_programs = order._get_applicable_no_code_promo_program() + order._get_applicable_programs() + order._get_valid_applied_coupon_program()
applied_programs = order._get_applied_programs_with_rewards_on_current_order() + order._get_applied_programs_with_rewards_on_next_order()
programs_to_remove = applied_programs - applicable_programs
products_to_remove = programs_to_remove.mapped('discount_line_product_id')
# delete reward line coming from an archived coupon (it will never be updated/removed when recomputing the order)
invalid_lines = order.order_line.filtered(lambda line: line.is_reward_line and line.product_id.id not in (applied_programs).mapped('discount_line_product_id').ids)
# Invalid generated coupon for which we are not eligible anymore ('expired' since it is specific to this SO and we may again met the requirements)
self.generated_coupon_ids.filtered(lambda coupon: coupon.program_id.discount_line_product_id.id in products_to_remove.ids).write({'state': 'expired'})
# Reset applied coupons for which we are not eligible anymore ('valid' so it can be use on another )
coupons_to_remove = order.applied_coupon_ids.filtered(lambda coupon: coupon.program_id in programs_to_remove)
coupons_to_remove.write({'state': 'new'})
# Unbind promotion and coupon programs which requirements are not met anymore
order.no_code_promo_program_ids -= programs_to_remove
order.code_promo_program_id -= programs_to_remove
order.applied_coupon_ids -= order.applied_coupon_ids.filtered(lambda coupon: coupon.program_id in programs_to_remove)
order.applied_coupon_ids -= coupons_to_remove
# Remove their reward lines
invalid_lines |= order.order_line.filtered(lambda line: line.product_id.id in products_to_remove.ids)
invalid_lines.unlink()
@@ -358,6 +386,10 @@ class SaleOrder(models.Model):
self.applied_coupon_ids.mapped('program_id') + \
self.code_promo_program_id.filtered(lambda p: p.promo_applicability == 'on_current_order')
def _get_applied_programs_with_rewards_on_next_order(self):
return self.no_code_promo_program_ids.filtered(lambda p: p.promo_applicability == 'on_next_order') + \
self.code_promo_program_id.filtered(lambda p: p.promo_applicability == 'on_next_order')
class SaleOrderLine(models.Model):
_inherit = "sale.order.line"