| 1 |
|
| 2 |
|
| 3 |
from odoo import Command |
| 4 |
from odoo.tests.common import tagged |
| 5 |
|
| 6 |
from odoo.addons.sale_loyalty.tests.common import TestSaleCouponCommon |
| 7 |
|
| 8 |
|
| 9 |
@tagged('-at_install', 'post_install') |
| 10 |
class TestBuyGiftCard(TestSaleCouponCommon): |
| 11 |
|
| 12 |
def test_buying_gift_card(self): |
| 13 |
order = self.empty_order |
| 14 |
self.immediate_promotion_program.active = False |
| 15 |
order.write({'order_line': [ |
| 16 |
(0, False, { |
| 17 |
'product_id': self.product_A.id, |
| 18 |
'name': 'Ordinary Product A', |
| 19 |
'product_uom_qty': 1.0, |
| 20 |
}), |
| 21 |
(0, False, { |
| 22 |
'product_id': self.product_gift_card.id, |
| 23 |
'name': 'Gift Card Product', |
| 24 |
'product_uom_qty': 1.0, |
| 25 |
}) |
| 26 |
]}) |
| 27 |
self.assertEqual(len(order.order_line.ids), 2) |
| 28 |
self.assertEqual(len(order._get_reward_coupons()), 0) |
| 29 |
order._update_programs_and_rewards() |
| 30 |
self.assertEqual(len(order._get_reward_coupons()), 1) |
| 31 |
order.order_line[1].product_uom_qty = 2 |
| 32 |
order._update_programs_and_rewards() |
| 33 |
self.assertEqual(len(order._get_reward_coupons()), 2) |
| 34 |
order.order_line[1].product_uom_qty = 1 |
| 35 |
order._update_programs_and_rewards() |
| 36 |
self.assertEqual(len(order._get_reward_coupons()), 1) |
| 37 |
|
| 38 |
def test_gift_card_email_sender(self): |
| 39 |
"""Ensure that sending gift card emails have a sender. |
| 40 |
Either the order's salesman if available, otherwise the order's company. |
| 41 |
""" |
| 42 |
mail_template = self.env['mail.template'].create({ |
| 43 |
'name': "Gift Card Mail", |
| 44 |
'model_id': self.env.ref('loyalty.model_loyalty_card').id, |
| 45 |
'auto_delete': False, |
| 46 |
}) |
| 47 |
self.program_gift_card.communication_plan_ids = [Command.create({ |
| 48 |
'trigger': 'create', |
| 49 |
'mail_template_id': mail_template.id, |
| 50 |
})] |
| 51 |
order = self.empty_order |
| 52 |
salesman = order.user_id.partner_id.ensure_one() |
| 53 |
salesman.email = "[email protected]" |
| 54 |
company = order.company_id.partner_id |
| 55 |
company.email = "[email protected]" |
| 56 |
order.write({ |
| 57 |
'order_line': [Command.create({'product_id': self.product_gift_card.id})], |
| 58 |
}) |
| 59 |
order._update_programs_and_rewards() |
| 60 |
|
| 61 |
|
| 62 |
orders = order + order.copy({'user_id': None}) |
| 63 |
|
| 64 |
|
| 65 |
self.env['mail.mail'].search([]).sudo().unlink() |
| 66 |
|
| 67 |
|
| 68 |
public_user = self.env.ref('base.public_user') |
| 69 |
orders.with_user(public_user).with_company(order.company_id).sudo().action_confirm() |
| 70 |
|
| 71 |
mails = self.env['mail.mail'].search([]) |
| 72 |
self.assertEqual(len(mails), 2) |
| 73 |
salesman_mail = mails.filtered(lambda m: m.author_id == salesman).ensure_one() |
| 74 |
company_mail = mails.filtered(lambda m: m.author_id == company).ensure_one() |
| 75 |
self.assertEqual(salesman_mail.email_from, salesman.email_formatted) |
| 76 |
self.assertEqual(company_mail.email_from, company.email_formatted) |