-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtest_automatic_workflow.py
More file actions
229 lines (211 loc) · 8.88 KB
/
test_automatic_workflow.py
File metadata and controls
229 lines (211 loc) · 8.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# Copyright 2014 Camptocamp SA (author: Guewen Baconnier)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from datetime import timedelta
from unittest import mock
from freezegun import freeze_time
from odoo import fields
from odoo.tests import tagged
from .common import TestAutomaticWorkflowMixin, TestCommon
@tagged("post_install", "-at_install")
class TestAutomaticWorkflow(TestCommon, TestAutomaticWorkflowMixin):
def setUp(self):
super().setUp()
self.env = self.env(
context=dict(
self.env.context,
tracking_disable=True,
# Compatibility with sale_automatic_workflow_job: even if
# the module is installed, ensure we don't delay a job.
# Thus, we test the usual flow.
queue_job__no_delay=True,
)
)
def test_01_full_automatic(self):
workflow = self.create_full_automatic()
sale = self.create_sale_order(workflow)
self.assertEqual(sale.state, "draft")
self.assertEqual(sale.workflow_process_id, workflow)
self.run_job()
self.assertEqual(sale.state, "sale")
self.assertTrue(sale.invoice_ids)
invoice = sale.invoice_ids
self.assertEqual(invoice.state, "posted")
def test_02_onchange(self):
team_1 = self.env["crm.team"].create({"name": "Test Team 1"})
team_2 = self.env.ref("sales_team.team_sales_department")
workflow = self.create_full_automatic(override={"team_id": team_1.id})
sale = self.create_sale_order(workflow)
self.assertEqual(sale.team_id, team_1)
workflow2 = self.create_full_automatic(override={"team_id": team_2.id})
sale.workflow_process_id = workflow2.id
self.assertEqual(sale.team_id, team_2)
@freeze_time("2025-1-1")
def test_03_date_invoice_from_sale_order(self):
workflow = self.create_full_automatic()
# date_order on sale.order is date + time
# invoice_date on account.move is date only
last_week_time = fields.Datetime.now() - timedelta(days=7)
override = {"date_order": last_week_time}
sale = self.create_sale_order(workflow, override=override)
self.assertEqual(sale.date_order, last_week_time)
self.run_job()
self.assertTrue(sale.invoice_ids)
invoice = sale.invoice_ids
self.assertEqual(invoice.invoice_date, last_week_time.date())
self.assertEqual(invoice.workflow_process_id, sale.workflow_process_id)
def test_04_create_invoice_from_sale_order(self):
workflow = self.create_full_automatic()
sale = self.create_sale_order(workflow)
line = sale.order_line[0]
# Make sure this addon works properly in regards to it.
mock_path = "odoo.addons.sale.models.sale_order.SaleOrder._create_invoices"
workflow.invoice_service_delivery = True
line.qty_delivered_method = "manual"
with mock.patch(mock_path) as mocked:
sale._create_invoices()
mocked.assert_called()
self.assertEqual(line.qty_delivered, 1.0)
def test_05_invoice_from_picking_with_service_product(self):
workflow = self.create_full_automatic()
product_service = self.env["product.product"].create(
{
"name": "Remodeling Service",
"categ_id": self.env.ref("product.product_category_services").id,
"standard_price": 40.0,
"list_price": 90.0,
"type": "service",
"uom_id": self.env.ref("uom.product_uom_hour").id,
"description": "Example of product to invoice on order",
"default_code": "PRE-PAID",
"invoice_policy": "order",
}
)
product_uom_hour = self.env.ref("uom.product_uom_hour")
override = {
"order_line": [
(
0,
0,
{
"name": "Prepaid Consulting",
"product_id": product_service.id,
"product_uom_qty": 1,
"product_uom_id": product_uom_hour.id,
},
)
]
}
sale = self.create_sale_order(workflow, override=override)
self.run_job()
self.assertTrue(sale.invoice_ids)
invoice = sale.invoice_ids
self.assertEqual(invoice.workflow_process_id, sale.workflow_process_id)
def test_06_journal_on_invoice(self):
sale_journal = self.env["account.journal"].search(
[("type", "=", "sale")], limit=1
)
new_sale_journal = self.env["account.journal"].create(
{"name": "TTSA", "code": "TTSA", "type": "sale"}
)
workflow = self.create_full_automatic()
sale = self.create_sale_order(workflow)
self.run_job()
self.assertTrue(sale.invoice_ids)
invoice = sale.invoice_ids
self.assertEqual(invoice.journal_id.id, sale_journal.id)
workflow = self.create_full_automatic(
override={"property_journal_id": new_sale_journal.id}
)
sale = self.create_sale_order(workflow)
self.run_job()
self.assertTrue(sale.invoice_ids)
invoice = sale.invoice_ids
self.assertEqual(invoice.journal_id.id, new_sale_journal.id)
def test_no_copy(self):
workflow = self.create_full_automatic()
sale = self.create_sale_order(workflow)
self.run_job()
invoice = sale.invoice_ids
self.assertTrue(sale.workflow_process_id)
self.assertTrue(invoice.workflow_process_id)
sale2 = sale.copy()
invoice2 = invoice.copy()
self.assertFalse(sale2.workflow_process_id)
self.assertFalse(invoice2.workflow_process_id)
def test_automatic_sale_order_confirmation_mail(self):
workflow = self.create_full_automatic()
workflow.send_order_confirmation_mail = True
sale = self.create_sale_order(workflow)
previous_message_ids = sale.message_ids
self.run_job()
self.assertEqual(sale.state, "sale")
new_messages = self.env["mail.message"].search(
[
("id", "in", sale.message_ids.ids),
("id", "not in", previous_message_ids.ids),
]
)
self.assertTrue(
new_messages.filtered(
lambda x: x.subtype_id == self.env.ref("mail.mt_comment")
)
)
def test_create_payment_with_invoice_currency_id(self):
workflow = self.create_full_automatic()
pricelist_id = self.env["product.pricelist"].create(
{
"name": "default_pricelist",
"currency_id": 1,
}
)
product_service = self.env["product.product"].create(
{
"name": "Remodeling Service",
"categ_id": self.env.ref("product.product_category_services").id,
"standard_price": 40.0,
"list_price": 90.0,
"type": "service",
"uom_id": self.env.ref("uom.product_uom_hour").id,
"description": "Example of product to invoice on order",
"default_code": "PRE-PAID",
"invoice_policy": "order",
}
)
product_uom_hour = self.env.ref("uom.product_uom_hour")
override = {
"pricelist_id": pricelist_id.id,
"order_line": [
(
0,
0,
{
"name": "Prepaid Consulting",
"product_id": product_service.id,
"product_uom_qty": 1,
"product_uom_id": product_uom_hour.id,
},
)
],
}
sale = self.create_sale_order(workflow, override=override)
self.run_job()
self.assertTrue(sale.invoice_ids)
invoice = sale.invoice_ids
self.assertEqual(invoice.state, "posted")
payment_id = self.env["automatic.workflow.job"]._register_payment_invoice(
invoice
)
self.assertTrue(payment_id)
self.assertEqual(invoice.currency_id.id, payment_id.currency_id.id)
self.assertEqual(invoice.payment_state, invoice._get_invoice_in_payment_state())
def test_create_payment_with_specified_payment_journal(self):
workflow = self.create_full_automatic()
workflow.register_payment = True
payment_journal = self.env["account.journal"].create(
{"name": "Payment Journal Test", "code": "TESTJOURNAL", "type": "bank"}
)
workflow.property_payment_journal_id = payment_journal
self.create_sale_order(workflow)
self.run_job()
payment = self.env["account.payment"].search([], limit=1, order="id desc")
self.assertEqual(payment.journal_id, payment_journal)