| 1 |
import { defineMailModels } from '@mail/../tests/mail_test_helpers'; |
| 2 |
import { expect, test } from '@odoo/hoot'; |
| 3 |
import { queryAllTexts } from '@odoo/hoot-dom'; |
| 4 |
import { |
| 5 |
clickSave, |
| 6 |
contains, |
| 7 |
defineModels, |
| 8 |
fields, |
| 9 |
mountView, |
| 10 |
onRpc, |
| 11 |
} from '@web/../tests/web_test_helpers'; |
| 12 |
import { saleModels } from '@sale/../tests/sale_test_helpers'; |
| 13 |
|
| 14 |
class SaleOrderLine extends saleModels.SaleOrderLine { |
| 15 |
|
| 16 |
price_unit = fields.Float({ default: 3.00 }); |
| 17 |
price_total = fields.Float({ default: 3.00 }); |
| 18 |
price_subtotal = fields.Float({ default: 3.50 }); |
| 19 |
product_uom_qty = fields.Float({ default: 1.00 }); |
| 20 |
|
| 21 |
_records = [ |
| 22 |
{ id: 1, name: "r1", sequence: 1 }, |
| 23 |
{ id: 2, name: "r2", sequence: 2 }, |
| 24 |
{ |
| 25 |
id: 3, |
| 26 |
name: "Sec1", |
| 27 |
sequence: 3, |
| 28 |
display_type: 'line_section', |
| 29 |
product_uom_qty: 0, |
| 30 |
price_unit: 0, |
| 31 |
price_total: 0, |
| 32 |
price_subtotal: 0, |
| 33 |
collapse_prices: true, |
| 34 |
}, |
| 35 |
{ |
| 36 |
id: 4, |
| 37 |
name: "Sec2", |
| 38 |
sequence: 4, |
| 39 |
display_type: 'line_section', |
| 40 |
product_uom_qty: 0, |
| 41 |
price_unit: 0, |
| 42 |
price_total: 0, |
| 43 |
price_subtotal: 0, |
| 44 |
collapse_composition: true, |
| 45 |
}, |
| 46 |
{ |
| 47 |
id: 5, |
| 48 |
name: "Sec3", |
| 49 |
sequence: 5, |
| 50 |
display_type: 'line_section', |
| 51 |
product_uom_qty: 0, |
| 52 |
price_unit: 0, |
| 53 |
price_total: 0, |
| 54 |
price_subtotal: 0, |
| 55 |
}, |
| 56 |
{ id: 6, name: "Sec3-r1", sequence: 6 }, |
| 57 |
{ id: 7, name: "Sec3-r2", sequence: 7 }, |
| 58 |
{ |
| 59 |
id: 8, |
| 60 |
name: "Sec3-sub1", |
| 61 |
sequence: 8, |
| 62 |
display_type: 'line_subsection', |
| 63 |
product_uom_qty: 0, |
| 64 |
price_unit: 0, |
| 65 |
price_total: 0, |
| 66 |
}, |
| 67 |
{ id: 9, name: "Sec3-sub1-r1", sequence: 9 }, |
| 68 |
{ |
| 69 |
id: 10, |
| 70 |
name: "Sec3-sub2", |
| 71 |
sequence: 10, |
| 72 |
display_type: 'line_subsection', |
| 73 |
product_uom_qty: 0, |
| 74 |
price_unit: 0, |
| 75 |
price_total: 0, |
| 76 |
}, |
| 77 |
{ id: 11, name: "Sec3-sub2-r1", sequence: 11 }, |
| 78 |
{ |
| 79 |
id: 12, |
| 80 |
name: "Sec4", |
| 81 |
sequence: 12, |
| 82 |
display_type: 'line_section', |
| 83 |
product_uom_qty: 0, |
| 84 |
price_unit: 0, |
| 85 |
price_total: 0, |
| 86 |
price_subtotal: 0 |
| 87 |
}, |
| 88 |
{ id: 13, name: "Sec4-r1", sequence: 13 }, |
| 89 |
{ |
| 90 |
id: 14, |
| 91 |
name: "Sec4-sub1", |
| 92 |
sequence: 14, |
| 93 |
display_type: 'line_subsection', |
| 94 |
product_uom_qty: 0, |
| 95 |
price_unit: 0, |
| 96 |
price_total: 0, |
| 97 |
collapse_composition: true, |
| 98 |
collapse_prices: true, |
| 99 |
}, |
| 100 |
{ id: 15, name: "Sec4-sub1-r1", sequence: 15 }, |
| 101 |
]; |
| 102 |
} |
| 103 |
|
| 104 |
class SaleOrder extends saleModels.SaleOrder { |
| 105 |
_records = [ |
| 106 |
{ |
| 107 |
id: 1, |
| 108 |
name: "Optional Sections Sale order", |
| 109 |
order_line: SaleOrderLine._records.map(record => record.id), |
| 110 |
}, |
| 111 |
]; |
| 112 |
_views = { |
| 113 |
form: ` |
| 114 |
<form> |
| 115 |
<field |
| 116 |
name="order_line" |
| 117 |
widget="sol_o2m" |
| 118 |
options="{'subsections': True, 'hide_composition': True, 'hide_prices': True}" |
| 119 |
> |
| 120 |
<list editable="bottom"> |
| 121 |
<control> |
| 122 |
<create name="add_line_control" string="Add a line"/> |
| 123 |
<create name="add_section_control" string="Add a section" context="{'default_display_type': 'line_section'}"/> |
| 124 |
<create name="add_note_control" string="Add a note" context="{'default_display_type': 'line_note'}"/> |
| 125 |
</control> |
| 126 |
<field name="sequence" widget="handle"/> |
| 127 |
<field name="name"/> |
| 128 |
<field name="product_uom_qty"/> |
| 129 |
<field name="price_unit"/> |
| 130 |
<field name="price_total"/> |
| 131 |
<field name="price_subtotal"/> |
| 132 |
<field name="display_type" column_invisible="1"/> |
| 133 |
<field name="collapse_composition" column_invisible="1"/> |
| 134 |
<field name="collapse_prices" column_invisible="1"/> |
| 135 |
<field name="is_optional" column_invisible="1"/> |
| 136 |
</list> |
| 137 |
</field> |
| 138 |
</form> |
| 139 |
`, |
| 140 |
}; |
| 141 |
} |
| 142 |
|
| 143 |
defineModels({ SaleOrderLine, SaleOrder }); |
| 144 |
defineMailModels(); |
| 145 |
|
| 146 |
const EXPECTED_LINE_RECORDS = [ |
| 147 |
"r1", |
| 148 |
"r2", |
| 149 |
"Sec1", |
| 150 |
"Sec2", |
| 151 |
"Sec3", |
| 152 |
"Sec3-r1", |
| 153 |
"Sec3-r2", |
| 154 |
"Sec3-sub1", |
| 155 |
"Sec3-sub1-r1", |
| 156 |
"Sec3-sub2", |
| 157 |
"Sec3-sub2-r1", |
| 158 |
"Sec4", |
| 159 |
"Sec4-r1", |
| 160 |
"Sec4-sub1", |
| 161 |
"Sec4-sub1-r1", |
| 162 |
]; |
| 163 |
|
| 164 |
test("Can't mark section hidden if optional and vice versa", async () => { |
| 165 |
await mountView({ |
| 166 |
type: 'form', |
| 167 |
resModel: 'sale.order', |
| 168 |
resId: 1, |
| 169 |
}); |
| 170 |
|
| 171 |
expect(queryAllTexts('.o_data_row .o_list_text')).toEqual(EXPECTED_LINE_RECORDS); |
| 172 |
|
| 173 |
await contains('.o_data_row:contains(Sec1) .o_list_section_options button').click(); |
| 174 |
expect('.o-dropdown-item:contains(Set Optional)').toHaveClass('disabled', { |
| 175 |
message: "Section with hidden prices can't be optional" |
| 176 |
}); |
| 177 |
|
| 178 |
await contains('.o_data_row:contains(Sec2) .o_list_section_options button').click(); |
| 179 |
expect('.o-dropdown-item:contains(Set Optional)').toHaveClass('disabled', { |
| 180 |
message: "Hidden section can't be optional" |
| 181 |
}); |
| 182 |
}) |
| 183 |
|
| 184 |
test("Setting section optional should reset some fields", async () => { |
| 185 |
onRpc('web_save', ({ args }) => { |
| 186 |
expect.step('web_save'); |
| 187 |
expect(args[1]).toEqual( |
| 188 |
{ |
| 189 |
order_line: [ |
| 190 |
[1, 10, { collapse_composition: false, collapse_prices: false }], |
| 191 |
[1, 8, { collapse_composition: false, collapse_prices: false }], |
| 192 |
[1, 5, { is_optional: true }], |
| 193 |
[1, 6, { product_uom_qty: 0, price_total: 0, price_subtotal: 0 }], |
| 194 |
[1, 7, { product_uom_qty: 0, price_total: 0, price_subtotal: 0 }], |
| 195 |
[1, 9, { product_uom_qty: 0, price_total: 0, price_subtotal: 0 }], |
| 196 |
[1, 11, { product_uom_qty: 0, price_total: 0, price_subtotal: 0 }], |
| 197 |
], |
| 198 |
}, |
| 199 |
{ message: "Subsections reset collapse_* fields' value and product lines reset qty/price when section becomes optional" } |
| 200 |
); |
| 201 |
}); |
| 202 |
|
| 203 |
await mountView({ |
| 204 |
type: 'form', |
| 205 |
resModel: 'sale.order', |
| 206 |
resId: 1, |
| 207 |
}); |
| 208 |
|
| 209 |
expect(queryAllTexts('.o_data_row .o_list_text')).toEqual(EXPECTED_LINE_RECORDS); |
| 210 |
|
| 211 |
await contains('.o_data_row:contains(Sec3-sub2) .o_list_section_options button').click(); |
| 212 |
await contains('.o-dropdown-item:contains(Hide Composition)').click(); |
| 213 |
|
| 214 |
await contains('.o_data_row:contains(Sec3-sub1) .o_list_section_options button').click(); |
| 215 |
await contains('.o-dropdown-item:contains(Hide Prices)').click(); |
| 216 |
|
| 217 |
await contains('.o_data_row:contains(Sec3) .o_list_section_options button').click(); |
| 218 |
await contains('.o-dropdown-item:contains(Set Optional)').click(); |
| 219 |
|
| 220 |
await clickSave(); |
| 221 |
await expect.verifySteps(['web_save']); |
| 222 |
}) |
| 223 |
|
| 224 |
test("Unsetting optional section should reset some fields", async () => { |
| 225 |
SaleOrderLine._records.find(record => record.name === 'Sec3').is_optional = true; |
| 226 |
SaleOrderLine._records.find(record => record.name === 'Sec3-r1').product_uom_qty = 0; |
| 227 |
SaleOrderLine._records.find(record => record.name === 'Sec3-r2').product_uom_qty = 0; |
| 228 |
SaleOrderLine._records.find(record => record.name === 'Sec3-sub1-r1').product_uom_qty = 0; |
| 229 |
|
| 230 |
SaleOrderLine._records.find(record => record.name === 'Sec3-sub2-r1').product_uom_qty = 5; |
| 231 |
|
| 232 |
onRpc('web_save', ({ args }) => { |
| 233 |
expect.step('web_save'); |
| 234 |
expect(args[1]).toEqual( |
| 235 |
{ |
| 236 |
order_line: [ |
| 237 |
[1, 5, { is_optional: false }], |
| 238 |
[1, 6, { product_uom_qty: 1 }], |
| 239 |
[1, 7, { product_uom_qty: 1 }], |
| 240 |
[1, 9, { product_uom_qty: 1 }], |
| 241 |
[1, 11, { product_uom_qty: 5 }], |
| 242 |
], |
| 243 |
}, |
| 244 |
{ message: "The subsections should reset products lines with 0 quantity with 1 as soon as section becomes non optional" } |
| 245 |
); |
| 246 |
}); |
| 247 |
|
| 248 |
await mountView({ |
| 249 |
type: 'form', |
| 250 |
resModel: 'sale.order', |
| 251 |
resId: 1, |
| 252 |
}); |
| 253 |
|
| 254 |
expect(queryAllTexts('.o_data_row .o_list_text')).toEqual(EXPECTED_LINE_RECORDS); |
| 255 |
|
| 256 |
expect('.o_data_row:contains(Sec3-r1)').toHaveClass('text-primary', { |
| 257 |
message: "Line under optional section should be text-primary" |
| 258 |
}); |
| 259 |
expect('.o_data_row:contains(Sec3-sub1)').toHaveClass('text-primary', { |
| 260 |
message: "Subsection under optional section should be text-primary" |
| 261 |
}); |
| 262 |
expect('.o_data_row:contains(Sec3-sub1-r1)').toHaveClass('text-primary', { |
| 263 |
message: "Line under subsection(which is under optional section) should be text-primary" |
| 264 |
}); |
| 265 |
|
| 266 |
await contains('.o_data_row:contains(Sec3) .o_list_section_options button').click(); |
| 267 |
await contains('.o-dropdown-item:contains(Unset Optional)').click(); |
| 268 |
|
| 269 |
await clickSave(); |
| 270 |
await expect.verifySteps(['web_save']); |
| 271 |
}) |
| 272 |
|
| 273 |
test("drag and drop regular line inside optional section resets some fields", async () => { |
| 274 |
SaleOrderLine._records.find(record => record.name === 'Sec3').is_optional = true; |
| 275 |
SaleOrderLine._records.find(record => record.name === 'Sec3-sub2-r1').product_uom_qty = 0; |
| 276 |
SaleOrderLine._records.find(record => record.name === 'Sec3-sub1-r1').product_uom_qty = 1; |
| 277 |
|
| 278 |
onRpc('web_save', ({ args }) => { |
| 279 |
expect.step('web_save'); |
| 280 |
|
| 281 |
expect(args[1].order_line.find(commands => commands[1] === 13)[2].product_uom_qty).toEqual( |
| 282 |
0, |
| 283 |
{ message: "Drag and drop inside optional section should reset product_uom_qty to 0" }, |
| 284 |
); |
| 285 |
expect(args[1].order_line.find(commands => commands[1] === 11)[2].product_uom_qty).toEqual( |
| 286 |
1, |
| 287 |
{ message: "Drag and drop line with 0 quantity outside optional section should reset product_uom_qty to 1" }, |
| 288 |
); |
| 289 |
expect(args[1].order_line.find(commands => commands[1] === 9)?.[2].product_uom_qty).toEqual( |
| 290 |
undefined, |
| 291 |
{ message: "Drag and drop line with non-zero quantity outside optional section shouldn't reset product_uom_qty" } |
| 292 |
); |
| 293 |
}) |
| 294 |
|
| 295 |
await mountView({ |
| 296 |
type: 'form', |
| 297 |
resModel: 'sale.order', |
| 298 |
resId: 1, |
| 299 |
}); |
| 300 |
|
| 301 |
expect(queryAllTexts('.o_data_row .o_list_text')).toEqual(EXPECTED_LINE_RECORDS); |
| 302 |
|
| 303 |
await contains('.o_data_row:contains(Sec4-r1):first .o_row_handle').dragAndDrop('.o_data_row:contains(Sec3-sub2):first'); |
| 304 |
await contains('.o_data_row:contains(Sec3-sub2-r1):first .o_row_handle').dragAndDrop('.o_data_row:contains(Sec4-sub1):first'); |
| 305 |
await contains('.o_data_row:contains(Sec3-sub1-r1):first .o_row_handle').dragAndDrop('.o_data_row:contains(Sec4-sub1):first'); |
| 306 |
|
| 307 |
await clickSave(); |
| 308 |
await expect.verifySteps(['web_save']); |
| 309 |
}) |
| 310 |
|
| 311 |
test("Moving Optional Sections to include some lines should set quantity to 0", async () => { |
| 312 |
SaleOrderLine._records.find(record => record.name === 'Sec4').is_optional = true; |
| 313 |
|
| 314 |
SaleOrderLine._records.find(record => record.name === 'Sec4-sub1-r1').product_uom_qty = 0; |
| 315 |
onRpc('web_save', ({ args }) => { |
| 316 |
expect.step('web_save'); |
| 317 |
|
| 318 |
expect(args[1].order_line.find(commands => commands[1] === 7)[2].product_uom_qty).toEqual( |
| 319 |
0, |
| 320 |
{ message: "New lines added to an optional section should have product_uom_qty set to 0" }, |
| 321 |
); |
| 322 |
expect(args[1].order_line.find(commands => commands[1] === 9)[2].product_uom_qty).toEqual( |
| 323 |
0, |
| 324 |
{ message: "New lines added to a subsection of an optional section should also have product_uom_qty set to 0" }, |
| 325 |
); |
| 326 |
expect(args[1].order_line.find(commands => commands[1] === 13)?.[2].product_uom_qty).toEqual( |
| 327 |
undefined, |
| 328 |
{ message: "Existing optional lines should keep their current product_uom_qty" } |
| 329 |
); |
| 330 |
}); |
| 331 |
|
| 332 |
await mountView({ |
| 333 |
type: 'form', |
| 334 |
resModel: 'sale.order', |
| 335 |
resId: 1, |
| 336 |
}); |
| 337 |
|
| 338 |
expect(queryAllTexts('.o_data_row .o_list_text')).toEqual(EXPECTED_LINE_RECORDS); |
| 339 |
|
| 340 |
await contains('.o_data_row:contains(Sec4):first .o_row_handle').dragAndDrop('.o_data_row:contains(Sec3-r2):first'); |
| 341 |
await clickSave(); |
| 342 |
await expect.verifySteps(['web_save']); |
| 343 |
}) |
| 344 |
|
| 345 |
test("Moving Optional Sections to exclude some lines should set quantity to 1", async () => { |
| 346 |
SaleOrderLine._records.find(record => record.name === 'Sec3').is_optional = true; |
| 347 |
SaleOrderLine._records.find(record => record.name === 'Sec3-r1').product_uom_qty = 0; |
| 348 |
SaleOrderLine._records.find(record => record.name === 'Sec3-sub1-r1').product_uom_qty = 0; |
| 349 |
onRpc('web_save', ({ args }) => { |
| 350 |
expect.step('web_save'); |
| 351 |
|
| 352 |
expect(args[1].order_line.find(command => command[1] === 6)[2].product_uom_qty).toEqual( |
| 353 |
1, |
| 354 |
{ message: "Non-optional lines should reset product_uom_qty to 1 when it was previously 0." }, |
| 355 |
); |
| 356 |
expect(args[1].order_line.find(command => command[1] === 7)?.[2].product_uom_qty).toEqual( |
| 357 |
undefined, |
| 358 |
{ message: "Non-optional lines should keep their existing product_uom_qty when it was already non-zero." }, |
| 359 |
); |
| 360 |
expect(args[1].order_line.find(command => command[1] === 9)[2].product_uom_qty).toEqual( |
| 361 |
1, |
| 362 |
{ message: "Lines moved out of an optional subsection should reset product_uom_qty to 1 when it was 0." }, |
| 363 |
); |
| 364 |
expect(args[1].order_line.find(command => command[1] === 11)?.[2].product_uom_qty).toEqual( |
| 365 |
undefined, |
| 366 |
{ message: "Lines moved out of an optional subsection should keep their existing product_uom_qty when it was already non-zero." }, |
| 367 |
); |
| 368 |
}); |
| 369 |
|
| 370 |
await mountView({ |
| 371 |
type: 'form', |
| 372 |
resModel: 'sale.order', |
| 373 |
resId: 1, |
| 374 |
}); |
| 375 |
|
| 376 |
expect(queryAllTexts('.o_data_row .o_list_text')).toEqual(EXPECTED_LINE_RECORDS); |
| 377 |
|
| 378 |
await contains('.o_data_row:contains(Sec3):first .o_row_handle').dragAndDrop('.o_data_row:contains(Sec4):first'); |
| 379 |
await clickSave(); |
| 380 |
await expect.verifySteps(['web_save']); |
| 381 |
}) |