| 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 |
models, |
| 10 |
mountView, |
| 11 |
onRpc, |
| 12 |
} from '@web/../tests/web_test_helpers'; |
| 13 |
|
| 14 |
class SaleOrderTemplateLine extends models.ServerModel { |
| 15 |
_name = 'sale.order.template.line'; |
| 16 |
|
| 17 |
product_uom_qty = fields.Float({ default: 1.00 }); |
| 18 |
_records = [ |
| 19 |
{ id: 1, name: "r1", sequence: 1 }, |
| 20 |
{ id: 2, name: "r2", sequence: 2 }, |
| 21 |
{ |
| 22 |
id: 3, |
| 23 |
name: "Sec1", |
| 24 |
sequence: 3, |
| 25 |
display_type: 'line_section', |
| 26 |
product_uom_qty: 0, |
| 27 |
}, |
| 28 |
{ |
| 29 |
id: 4, |
| 30 |
name: "Sec2", |
| 31 |
sequence: 4, |
| 32 |
display_type: 'line_section', |
| 33 |
product_uom_qty: 0, |
| 34 |
}, |
| 35 |
{ |
| 36 |
id: 5, |
| 37 |
name: "Sec3", |
| 38 |
sequence: 5, |
| 39 |
display_type: 'line_section', |
| 40 |
product_uom_qty: 0, |
| 41 |
}, |
| 42 |
{ id: 6, name: "Sec3-r1", sequence: 6 }, |
| 43 |
{ id: 7, name: "Sec3-r2", sequence: 7 }, |
| 44 |
{ |
| 45 |
id: 8, |
| 46 |
name: "Sec3-sub1", |
| 47 |
sequence: 8, |
| 48 |
display_type: 'line_subsection', |
| 49 |
product_uom_qty: 0, |
| 50 |
}, |
| 51 |
{ id: 9, name: "Sec3-sub1-r1", sequence: 9 }, |
| 52 |
{ |
| 53 |
id: 10, |
| 54 |
name: "Sec3-sub2", |
| 55 |
sequence: 10, |
| 56 |
display_type: 'line_subsection', |
| 57 |
product_uom_qty: 0, |
| 58 |
}, |
| 59 |
{ id: 11, name: "Sec3-sub2-r1", sequence: 11 }, |
| 60 |
{ |
| 61 |
id: 12, |
| 62 |
name: "Sec4", |
| 63 |
sequence: 12, |
| 64 |
display_type: 'line_section', |
| 65 |
product_uom_qty: 0, |
| 66 |
}, |
| 67 |
{ id: 13, name: "Sec4-r1", sequence: 13 }, |
| 68 |
{ |
| 69 |
id: 14, |
| 70 |
name: "Sec4-sub1", |
| 71 |
sequence: 14, |
| 72 |
display_type: 'line_subsection', |
| 73 |
product_uom_qty: 0, |
| 74 |
}, |
| 75 |
{ id: 15, name: "Sec4-sub1-r1", sequence: 15 }, |
| 76 |
]; |
| 77 |
} |
| 78 |
|
| 79 |
class SaleOrderTemplate extends models.ServerModel { |
| 80 |
_name = 'sale.order.template'; |
| 81 |
_records = [ |
| 82 |
{ |
| 83 |
id: 1, |
| 84 |
name: "Optional Sections Sale order template", |
| 85 |
sale_order_template_line_ids: SaleOrderTemplateLine._records.map(record => record.id), |
| 86 |
}, |
| 87 |
]; |
| 88 |
_views = { |
| 89 |
form: ` |
| 90 |
<form> |
| 91 |
<field |
| 92 |
name="sale_order_template_line_ids" |
| 93 |
widget="so_template_line_o2m" |
| 94 |
options="{'subsections': True}" |
| 95 |
> |
| 96 |
<list editable="bottom"> |
| 97 |
<control> |
| 98 |
<create name="add_line_control" string="Add a line"/> |
| 99 |
<create name="add_section_control" string="Add a section" context="{'default_display_type': 'line_section'}"/> |
| 100 |
<create name="add_note_control" string="Add a note" context="{'default_display_type': 'line_note'}"/> |
| 101 |
</control> |
| 102 |
<field name="sequence" widget="handle"/> |
| 103 |
<field name="name"/> |
| 104 |
<field name="product_uom_qty"/> |
| 105 |
<field name="display_type" column_invisible="1"/> |
| 106 |
<field name="is_optional" column_invisible="1"/> |
| 107 |
</list> |
| 108 |
</field> |
| 109 |
</form> |
| 110 |
`, |
| 111 |
}; |
| 112 |
} |
| 113 |
|
| 114 |
defineModels({ SaleOrderTemplateLine, SaleOrderTemplate }); |
| 115 |
defineMailModels(); |
| 116 |
|
| 117 |
const EXPECTED_LINE_RECORDS = [ |
| 118 |
"r1", |
| 119 |
"r2", |
| 120 |
"Sec1", |
| 121 |
"Sec2", |
| 122 |
"Sec3", |
| 123 |
"Sec3-r1", |
| 124 |
"Sec3-r2", |
| 125 |
"Sec3-sub1", |
| 126 |
"Sec3-sub1-r1", |
| 127 |
"Sec3-sub2", |
| 128 |
"Sec3-sub2-r1", |
| 129 |
"Sec4", |
| 130 |
"Sec4-r1", |
| 131 |
"Sec4-sub1", |
| 132 |
"Sec4-sub1-r1", |
| 133 |
]; |
| 134 |
|
| 135 |
test("drag and drop regular template lines inside optional section resets some fields", async () => { |
| 136 |
SaleOrderTemplateLine._records.find(record => record.name === 'Sec3').is_optional = true; |
| 137 |
SaleOrderTemplateLine._records.find(record => record.name === 'Sec3-sub2-r1').product_uom_qty = 0; |
| 138 |
SaleOrderTemplateLine._records.find(record => record.name === 'Sec3-sub1-r1').product_uom_qty = 1; |
| 139 |
|
| 140 |
onRpc('web_save', ({ args }) => { |
| 141 |
expect.step('web_save'); |
| 142 |
|
| 143 |
expect( |
| 144 |
args[1].sale_order_template_line_ids.find( |
| 145 |
commands => commands[1] === 13 |
| 146 |
)[2].product_uom_qty |
| 147 |
).toEqual(0, { |
| 148 |
message: "Drag and drop inside optional section should reset product_uom_qty to 0" |
| 149 |
}); |
| 150 |
expect( |
| 151 |
args[1].sale_order_template_line_ids.find( |
| 152 |
commands => commands[1] === 11 |
| 153 |
)[2].product_uom_qty |
| 154 |
).toEqual(1, { |
| 155 |
message: "Drag and drop line with 0 quantity outside optional section should reset product_uom_qty to 1" |
| 156 |
}); |
| 157 |
expect( |
| 158 |
args[1].sale_order_template_line_ids.find( |
| 159 |
commands => commands[1] === 9 |
| 160 |
)?.[2].product_uom_qty |
| 161 |
).toEqual(undefined, { |
| 162 |
message: "Drag and drop line with non-zero quantity outside optional section shouldn't reset product_uom_qty" |
| 163 |
}); |
| 164 |
}) |
| 165 |
|
| 166 |
await mountView({ |
| 167 |
type: 'form', |
| 168 |
resModel: 'sale.order.template', |
| 169 |
resId: 1, |
| 170 |
}); |
| 171 |
|
| 172 |
expect(queryAllTexts('.o_data_row .o_list_text')).toEqual(EXPECTED_LINE_RECORDS); |
| 173 |
|
| 174 |
await contains('.o_data_row:contains(Sec4-r1):first .o_row_handle').dragAndDrop('.o_data_row:contains(Sec3-sub2):first'); |
| 175 |
await contains('.o_data_row:contains(Sec3-sub2-r1):first .o_row_handle').dragAndDrop('.o_data_row:contains(Sec4-sub1):first'); |
| 176 |
await contains('.o_data_row:contains(Sec3-sub1-r1):first .o_row_handle').dragAndDrop('.o_data_row:contains(Sec4-sub1):first'); |
| 177 |
|
| 178 |
await clickSave(); |
| 179 |
await expect.verifySteps(['web_save']); |
| 180 |
}) |
| 181 |
|
| 182 |
test("Moving Optional Sections to include some template lines should set quantity to 0", async () => { |
| 183 |
SaleOrderTemplateLine._records.find(record => record.name === 'Sec4').is_optional = true; |
| 184 |
SaleOrderTemplateLine._records.find(record => record.name === 'Sec4-sub1-r1').product_uom_qty = 0; |
| 185 |
onRpc('web_save', ({ args }) => { |
| 186 |
expect.step('web_save'); |
| 187 |
|
| 188 |
expect( |
| 189 |
args[1].sale_order_template_line_ids.find( |
| 190 |
commands => commands[1] === 7 |
| 191 |
)[2].product_uom_qty |
| 192 |
).toEqual(0, { |
| 193 |
message: "New lines added to an optional section should have product_uom_qty set to 0", |
| 194 |
}); |
| 195 |
expect( |
| 196 |
args[1].sale_order_template_line_ids.find( |
| 197 |
commands => commands[1] === 9 |
| 198 |
)[2].product_uom_qty |
| 199 |
).toEqual(0, { |
| 200 |
message: "New lines added to a subsection of an optional section should also have product_uom_qty set to 0", |
| 201 |
}); |
| 202 |
expect( |
| 203 |
args[1].sale_order_template_line_ids.find( |
| 204 |
commands => commands[1] === 13 |
| 205 |
)?.[2].product_uom_qty |
| 206 |
).toEqual(undefined, { |
| 207 |
message: "Existing optional lines should keep their current product_uom_qty", |
| 208 |
}); |
| 209 |
}); |
| 210 |
|
| 211 |
await mountView({ |
| 212 |
type: 'form', |
| 213 |
resModel: 'sale.order.template', |
| 214 |
resId: 1, |
| 215 |
}); |
| 216 |
|
| 217 |
expect(queryAllTexts('.o_data_row .o_list_text')).toEqual(EXPECTED_LINE_RECORDS); |
| 218 |
|
| 219 |
await contains('.o_data_row:contains(Sec4):first .o_row_handle').dragAndDrop('.o_data_row:contains(Sec3-r2):first'); |
| 220 |
await clickSave(); |
| 221 |
await expect.verifySteps(['web_save']); |
| 222 |
}) |
| 223 |
|
| 224 |
test("Moving Optional Sections to exclude some template lines should set quantity to 1", async () => { |
| 225 |
SaleOrderTemplateLine._records.find(record => record.name === 'Sec3').is_optional = true; |
| 226 |
SaleOrderTemplateLine._records.find(record => record.name === 'Sec3-r1').product_uom_qty = 0; |
| 227 |
SaleOrderTemplateLine._records.find(record => record.name === 'Sec3-sub1-r1').product_uom_qty = 0; |
| 228 |
onRpc('web_save', ({ args }) => { |
| 229 |
expect.step('web_save'); |
| 230 |
|
| 231 |
expect( |
| 232 |
args[1].sale_order_template_line_ids.find( |
| 233 |
command => command[1] === 6 |
| 234 |
)[2].product_uom_qty |
| 235 |
).toEqual(1, { |
| 236 |
message: "Non-optional lines should reset product_uom_qty to 1 when it was previously 0.", |
| 237 |
}); |
| 238 |
expect( |
| 239 |
args[1].sale_order_template_line_ids.find( |
| 240 |
command => command[1] === 7 |
| 241 |
)?.[2].product_uom_qty |
| 242 |
).toEqual(undefined, { |
| 243 |
message: "Non-optional lines should keep their existing product_uom_qty when it was already non-zero.", |
| 244 |
}); |
| 245 |
expect( |
| 246 |
args[1].sale_order_template_line_ids.find( |
| 247 |
command => command[1] === 9 |
| 248 |
)[2].product_uom_qty |
| 249 |
).toEqual(1, { |
| 250 |
message: "Lines moved out of an optional subsection should reset product_uom_qty to 1 when it was 0.", |
| 251 |
}); |
| 252 |
expect( |
| 253 |
args[1].sale_order_template_line_ids.find( |
| 254 |
command => command[1] === 11 |
| 255 |
)?.[2].product_uom_qty |
| 256 |
).toEqual(undefined, { |
| 257 |
message: "Lines moved out of an optional subsection should keep their existing product_uom_qty when it was already non-zero.", |
| 258 |
}); |
| 259 |
}); |
| 260 |
|
| 261 |
await mountView({ |
| 262 |
type: 'form', |
| 263 |
resModel: 'sale.order.template', |
| 264 |
resId: 1, |
| 265 |
}); |
| 266 |
|
| 267 |
expect(queryAllTexts('.o_data_row .o_list_text')).toEqual(EXPECTED_LINE_RECORDS); |
| 268 |
|
| 269 |
await contains('.o_data_row:contains(Sec3):first .o_row_handle').dragAndDrop('.o_data_row:contains(Sec4):first'); |
| 270 |
await clickSave(); |
| 271 |
await expect.verifySteps(['web_save']); |
| 272 |
}) |