def test_copy_project_with_subtasks(self): self.env['project.task'].with_context({'mail_create_nolog': True}).create({ 'name': 'Parent Task', 'project_id': self.project_goats.id, 'child_ids': [ Command.create({'name': 'child 1'}), Command.create({'name': 'child 2', 'display_project_id': self.project_goats.id}), Command.create({'name': 'child 3', 'display_project_id': self.project_pigs.id}), Command.create({'name': 'child 4 with subtask', 'child_ids': [Command.create({'name': 'child 5'})]}), Command.create({'name': 'child archived', 'active': False}), ], }) task_count_with_subtasks_including_archived_in_project_goats = self.project_goats.with_context(active_test=False).task_count_with_subtasks task_count_in_project_pigs = self.project_pigs.task_count self.project_goats._compute_task_count() # recompute without archived tasks and subtasks task_count_in_project_goats = self.project_goats.task_count project_goats_duplicated = self.project_goats.copy() self.project_pigs._compute_task_count() # retrigger since a new task should be added in the project after the duplication of Project Goats self.assertEqual( project_goats_duplicated.with_context(active_test=False).task_count_with_subtasks, task_count_with_subtasks_including_archived_in_project_goats - 1, 'The number of duplicated tasks (subtasks included) should be equal to the number of all tasks (with active subtasks included) of both projects, ' 'that is only the active subtasks are duplicated.') self.assertEqual(self.project_goats.task_count, task_count_in_project_goats, 'The number of tasks should be the same before and after the duplication of this project.') self.assertEqual(self.project_pigs.task_count, task_count_in_project_pigs + 1, 'The project pigs should an additional task after the duplication of the project goats.')
def test_create_task_in_project_sharing(self): """ Test when portal user creates a task in project sharing views. Test Cases: ========== 1) Give the 'read' access mode to a portal user in a project and try to create task with this user. 2) Give the 'comment' access mode to a portal user in a project and try to create task with this user. 3) Give the 'edit' access mode to a portal user in a project and try to create task with this user. 3.1) Try to change the project of the new task with this user. """ # 1) Give the 'read' access mode to a portal user in a project and try to create task with this user. with self.assertRaises( AccessError, msg= "Should not accept the portal user create a task in the project when he has not the edit access right." ): with self.get_project_sharing_form_view( self.env['project.task'].with_context({ 'tracking_disable': True, 'default_project_id': self.project_portal.id }), self.user_portal) as form: form.name = 'Test' task = form.save() self.project_portal.write({ 'collaborator_ids': [ Command.create({'partner_id': self.user_portal.partner_id.id}), ], }) with self.get_project_sharing_form_view( self.env['project.task'].with_context({ 'tracking_disable': True, 'default_project_id': self.project_portal.id, 'default_user_ids': [(4, self.user_portal.id)] }), self.user_portal) as form: form.name = 'Test' task = form.save() self.assertEqual(task.name, 'Test') self.assertEqual(task.project_id, self.project_portal) self.assertEqual(task.portal_user_names, self.user_portal.name) # 3.1) Try to change the project of the new task with this user. with self.assertRaises( AssertionError, msg= "Should not accept the portal user changes the project of the task." ): form.project_id = self.project_cows task = form.save()
def test_relational_mapped(self): # create 1000 records with one line each recs = self.env['test_performance.base'].create([ {'name': 'foo%d' % index, 'line_ids': [Command.create({'value': index})]} for index in range(1000) ]) self.env.flush_all() self.env.invalidate_all() # expected same performance as recs.line_ids.mapped('value') with self.assertQueryCount(3): for rec in recs: rec.line_ids.mapped('value')
def test_write_4_one2many(self): """ Check that we can write on an inherited one2many field. """ box = self.env.ref('test_inherits.box_a') box.write({'line_ids': [Command.create({'name': 'Line 1'})]}) self.assertTrue(all(box.line_ids._ids)) self.assertEqual(box.line_ids.mapped('name'), ['Line 1']) self.assertEqual(box.line_ids, box.unit_id.line_ids) self.env.flush_all() box.invalidate_model(['line_ids']) box.write({'line_ids': [Command.create({'name': 'Line 2'})]}) self.assertTrue(all(box.line_ids._ids)) self.assertEqual(box.line_ids.mapped('name'), ['Line 1', 'Line 2']) self.assertEqual(box.line_ids, box.unit_id.line_ids) self.env.flush_all() box.invalidate_model(['line_ids']) box.write({ 'line_ids': [Command.update(box.line_ids[0].id, {'name': 'First line'})] }) self.assertTrue(all(box.line_ids._ids)) self.assertEqual(box.line_ids.mapped('name'), ['First line', 'Line 2']) self.assertEqual(box.line_ids, box.unit_id.line_ids)
def test_auto_join(self): model = self.env['test_read_group.order'] records = model.create([{ 'line_ids': [Command.create({'value': 1}), Command.create({'value': 2})], }, { 'line_ids': [Command.create({'value': 1})], }]) domain1 = [('id', 'in', records.ids), ('line_ids.value', '=', 1)] domain2 = [('id', 'in', records.ids), ('line_ids.value', '>', 0)] # reference results self.assertEqual(len(model.search(domain1)), 2) self.assertEqual(len(model.search(domain2)), 2) result1 = model.read_group(domain1, [], []) self.assertEqual(len(result1), 1) self.assertEqual(result1[0]['__count'], 2) result2 = model.read_group(domain2, [], []) self.assertEqual(len(result2), 1) self.assertEqual(result2[0]['__count'], 2) # same requests, with auto_join self.patch(type(model).line_ids, 'auto_join', True) self.assertEqual(len(model.search(domain1)), 2) self.assertEqual(len(model.search(domain2)), 2) result1 = model.read_group(domain1, [], []) self.assertEqual(len(result1), 1) self.assertEqual(result1[0]['__count'], 2) result2 = model.read_group(domain2, [], []) self.assertEqual(len(result2), 1) self.assertEqual(result2[0]['__count'], 2)
def test_readonly_o2m(self): """ Tests that o2m fields flagged as readonly (readonly="1" in the view) can't be written to """ r = self.env['test_testing_utilities.parent'].create( {'subs': [Command.create({})]}) f = Form(r, view='test_testing_utilities.o2m_parent_readonly') with self.assertRaises(AssertionError): f.subs.new() with self.assertRaises(AssertionError): f.subs.edit(index=0) with self.assertRaises(AssertionError): f.subs.remove(index=0)
def test_30_crud_write(self): _name = 'TestNew' # Do: update partner name self.action.write({ 'state': 'object_write', 'fields_lines': [Command.create({'col1': self.res_partner_name_field.id, 'value': _name})], }) run_res = self.action.with_context(self.context).run() self.assertFalse(run_res, 'ir_actions_server: create record action correctly finished should return False') # Test: partner updated partner = self.test_partner.search([('name', 'ilike', _name)]) self.assertEqual(len(partner), 1, 'ir_actions_server: TODO') self.assertEqual(partner.city, 'OrigCity', 'ir_actions_server: TODO')
def test_20_crud_create_link_many2many(self): # Do: create a new record in another model self.action.write({ 'state': 'object_create', 'crud_model_id': self.res_partner_category_model.id, 'link_field_id': self.res_partner_category_field.id, 'fields_lines': [Command.create({'col1': self.res_partner_category_name_field.id, 'value': 'record.name', 'evaluation_type': 'equation'})], }) run_res = self.action.with_context(self.context).run() self.assertFalse(run_res, 'ir_actions_server: create record action correctly finished should return False') # Test: new category created category = self.env['res.partner.category'].search([('name', 'ilike', 'TestingPartner')]) self.assertEqual(len(category), 1, 'ir_actions_server: TODO') self.assertIn(category, self.test_partner.category_id)
def setUpClass(cls, chart_template_ref=None): super().setUpClass(chart_template_ref=chart_template_ref) cls.alt_exp_account = cls.company_data['default_account_expense'].copy() # set 'type' to 'service' to allow manualy set 'qty_delivered' even with purchase_stock installed cls.product_a.type = 'service' cls.product_b.type = 'service' cls.product_b.property_account_expense_id = cls.alt_exp_account cls.purchase_order = cls.env['purchase.order'].with_context(tracking_disable=True).create({ 'partner_id': cls.partner_a.id, 'order_line': [ Command.create({ 'name': cls.product_a.name, 'product_id': cls.product_a.id, 'product_qty': 10.0, 'product_uom': cls.product_a.uom_id.id, 'price_unit': cls.product_a.list_price, 'taxes_id': False, }), Command.create({ 'name': cls.product_b.name, 'product_id': cls.product_b.id, 'product_qty': 10.0, 'product_uom': cls.product_b.uom_id.id, 'price_unit': cls.product_b.list_price, 'taxes_id': False, }), ], }) cls.purchase_order.button_confirm() cls.account_revenue = cls.company_data['default_account_revenue'] cls.account_expense = cls.company_data['default_account_expense'] cls.wizard = cls.env['account.accrued.orders.wizard'].with_context({ 'active_model': 'purchase.order', 'active_ids': cls.purchase_order.ids }).create({ 'account_id': cls.account_revenue.id, })
def test_amounts_sign(self): for tax_sign in (1, -1): tax = self.env['account.tax'].create({ 'name': "tax", 'amount_type': 'percent', 'amount': tax_sign * 10.0, }) amounts_list = [ (-1000.0, 7000.0, -2000.0), (1000.0, -7000.0, 2000.0), (-1000.0, -7000.0, 2000.0), (1000.0, 7000.0, -2000.0), ] for amounts in amounts_list: with self.subTest(tax_sign=tax_sign, amounts=amounts): invoice = self.env['account.move'].create({ 'move_type': 'in_invoice', 'partner_id': self.partner_a.id, 'invoice_date': '2019-01-01', 'invoice_line_ids': [ Command.create({ 'name': 'line2', 'account_id': self.company_data['default_account_revenue']. id, 'price_unit': amount, 'tax_ids': [Command.set(tax.ids)], }) for amount in amounts ], }) _base_lines, tax_lines = self._dispatch_move_lines(invoice) tax_details = self._get_tax_details( extra_domain=[('move_id', '=', invoice.id)]) self.assertTaxDetailsValues( tax_details, [{ 'tax_line_id': tax_lines[0].id, 'base_amount': amount, 'tax_amount': tax_sign * amount * 0.1, } for amount in amounts], ) self.assertTotalAmounts(invoice, tax_details)
def setUpClass(cls, chart_template_ref=None): super().setUpClass(chart_template_ref=chart_template_ref) cls.sale_order = cls.env['sale.order'].with_context( mail_notrack=True, mail_create_nolog=True).create({ 'partner_id': cls.partner_b.id, 'partner_invoice_id': cls.partner_b.id, 'partner_shipping_id': cls.partner_b.id, 'order_line': [ Command.create({ 'product_id': cls.product_milestone.id, 'product_uom_qty': 3, }), Command.create({ 'product_id': cls.product_delivery_manual1.id, 'product_uom_qty': 2, }) ] }) cls.product_milestone_sale_line = cls.sale_order.order_line.filtered( lambda sol: sol.product_id == cls.product_milestone) cls.product_delivery_manual1_sale_line = cls.sale_order.order_line.filtered( lambda sol: sol.product_id == cls.product_delivery_manual1) cls.sale_order.action_confirm() cls.milestone = cls.env['project.milestone'].create({ 'name': 'Test Milestone', 'sale_line_id': cls.product_milestone_sale_line.id, 'project_id': cls.project_global.id, })
def _add_to_wave(self, wave=False): """ Detach lines (and corresponding stock move from a picking to another). If wave is passed, attach new picking into it. If not attach line to their original picking. :param int wave: id of the wave picking on which to put the move lines. """ if not wave: wave = self.env['stock.picking.batch'].create({ 'is_wave': True }) line_by_picking = defaultdict(lambda: self.env['stock.move.line']) for line in self: # if not wave and line.orig_picking_id.is_wave: # continue line_by_picking[line.picking_id] |= line picking_to_wave_vals_list = [] for picking, lines in line_by_picking.items(): # Move the entire picking if all the line are taken if lines == picking.move_line_ids: wave.picking_ids = [Command.link(picking.id)] continue # Split the picking in two part to extract only line that are taken on the wave picking_to_wave_vals = picking.copy_data({ 'move_lines': [], 'move_line_ids': [], 'batch_id': wave.id, })[0] line_by_move = defaultdict(lambda: self.env['stock.move.line']) for line in lines: line_by_move[line.move_id] |= line for move, move_lines in line_by_move.items(): picking_to_wave_vals['move_line_ids'] += [Command.link(line.id) for line in lines] # if all the line of a stock move are taken we change the picking on the stock move if move_lines == move.move_line_ids: picking_to_wave_vals['move_lines'] += [Command.link(move.id)] continue # Split the move qty = sum(lines.mapped('product_qty')) new_move = move._split(qty) new_move[0]['move_line_ids'] = [Command.set(move_lines.ids)] picking_to_wave_vals['move_lines'] += [Command.create(new_move)] picking_to_wave_vals_list.append(picking_to_wave_vals) if picking_to_wave_vals_list: self.env['stock.picking'].create(picking_to_wave_vals_list) wave.action_confirm()
def test_tax_on_payment(self): percent_tax = self.env['account.tax'].create({ 'name': "percent_tax", 'amount_type': 'percent', 'amount': 10.0, 'tax_exigibility': 'on_payment', 'cash_basis_transition_account_id': self.company_data['default_account_assets'].id, }) invoice = self.env['account.move'].create({ 'move_type': 'out_invoice', 'partner_id': self.partner_a.id, 'invoice_date': '2019-01-01', 'invoice_line_ids': [ Command.create({ 'name': 'line1', 'account_id': self.company_data['default_account_revenue'].id, 'price_unit': 1000.0, 'tax_ids': [Command.set(percent_tax.ids)], }), ] }) base_lines, tax_lines = self._dispatch_move_lines(invoice) tax_details = self._get_tax_details() self.assertTaxDetailsValues( tax_details, [ { 'base_line_id': base_lines[0].id, 'tax_line_id': tax_lines[0].id, 'base_amount': -1000.0, 'tax_amount': -100.0, }, ], ) self.assertTotalAmounts(invoice, tax_details)
def test_60_prefetch_model(self): """ Check the prefetching model. """ partners = self.env['res.partner'].search( [('id', 'in', self.partners.ids)], limit=models.PREFETCH_MAX) self.assertTrue(partners) def same_prefetch(a, b): self.assertEqual(set(a._prefetch_ids), set(b._prefetch_ids)) def diff_prefetch(a, b): self.assertNotEqual(set(a._prefetch_ids), set(b._prefetch_ids)) # the recordset operations below use different prefetch sets diff_prefetch(partners, partners.browse()) diff_prefetch(partners, partners[0]) diff_prefetch(partners, partners[:5]) # the recordset operations below share the prefetch set same_prefetch(partners, partners.browse(partners.ids)) same_prefetch(partners, partners.with_user(self.user_demo)) same_prefetch(partners, partners.with_context(active_test=False)) same_prefetch(partners, partners[:10].with_prefetch(partners._prefetch_ids)) # iteration and relational fields should use the same prefetch set self.assertEqual(type(partners).country_id.type, 'many2one') self.assertEqual(type(partners).bank_ids.type, 'one2many') self.assertEqual(type(partners).category_id.type, 'many2many') vals0 = { 'name': 'Empty relational fields', 'country_id': False, 'bank_ids': [], 'category_id': [], } vals1 = { 'name': 'Non-empty relational fields', 'country_id': self.ref('base.be'), 'bank_ids': [Command.create({'acc_number': 'FOO42'})], 'category_id': [Command.link(self.partner_category.id)], } partners = partners.create(vals0) + partners.create(vals1) for partner in partners: same_prefetch(partner, partners) same_prefetch(partner.country_id, partners.country_id) same_prefetch(partner.bank_ids, partners.bank_ids) same_prefetch(partner.category_id, partners.category_id)
def assertAppliedRate( journal_currency, foreign_currency, aml_currency, amount, amount_currency, aml_amount_currency, aml_balance, expected_amount_currency, expected_balance, ): journal = self.bank_journal_1.copy() journal.currency_id = journal_currency statement = self.env['account.bank.statement'].create({ 'name': 'test_statement', 'date': '2019-01-01', 'journal_id': journal.id, 'line_ids': [ Command.create({ 'date': '2019-01-01', 'payment_ref': 'test_prepare_counterpart_amounts_using_st_line_rate', 'foreign_currency_id': foreign_currency.id if foreign_currency != journal_currency else None, 'amount': amount, 'amount_currency': amount_currency if foreign_currency != journal_currency else 0.0, }), ], }) statement_line = statement.line_ids res = statement_line._prepare_counterpart_amounts_using_st_line_rate( aml_currency, -aml_balance, -aml_amount_currency) self.assertAlmostEqual(res['amount_currency'], expected_amount_currency) self.assertAlmostEqual(res['balance'], expected_balance)
def test_20_crud_create_link_one2many(self): _name = 'TestNew' # Do: create a new record in the same model and link it with a one2many self.action.write({ 'state': 'object_create', 'crud_model_id': self.action.model_id.id, 'link_field_id': self.res_partner_children_field.id, 'fields_lines': [Command.create({'col1': self.res_partner_name_field.id, 'value': _name})], }) run_res = self.action.with_context(self.context).run() self.assertFalse(run_res, 'ir_actions_server: create record action correctly finished should return False') # Test: new partner created partner = self.test_partner.search([('name', 'ilike', _name)]) self.assertEqual(len(partner), 1, 'ir_actions_server: TODO') self.assertEqual(partner.name, _name, 'ir_actions_server: TODO') # Test: new partner linked self.assertIn(partner, self.test_partner.child_ids, 'ir_actions_server: TODO')
def test_broken_configuration(self): percent_tax = self.env['account.tax'].create({ 'name': "percent_tax", 'amount_type': 'percent', 'amount': 10.0, }) invoice = self.env['account.move'].create({ 'move_type': 'out_invoice', 'partner_id': self.partner_a.id, 'invoice_date': '2019-01-01', 'invoice_line_ids': [ Command.create({ 'name': 'line1', 'account_id': self.company_data['default_account_revenue'].id, 'price_unit': 1000.0, 'tax_ids': [Command.set(percent_tax.ids)], }), ] }) base_lines, tax_lines = self._dispatch_move_lines(invoice) # Break the configuration tax_lines.account_id = self.company_data['default_account_assets'] tax_details = self._get_tax_details(fallback=True) self.assertTaxDetailsValues( tax_details, [ { 'base_line_id': base_lines[0].id, 'tax_line_id': tax_lines[0].id, 'base_amount': -1000.0, 'tax_amount': -100.0, }, ], ) self.assertTotalAmounts(invoice, tax_details)
def _prepare_order_line_update_values( self, order_line, quantity, event_booth_pending_ids=False, registration_values=None, **kwargs ): """Delete existing booth registrations and create new ones with the update values.""" values = super()._prepare_order_line_update_values(order_line, quantity, **kwargs) if not event_booth_pending_ids: return values booths = self.env['event.booth'].browse(event_booth_pending_ids) values['event_booth_registration_ids'] = [ Command.delete(registration.id) for registration in order_line.event_booth_registration_ids ] + [ Command.create({ 'event_booth_id': booth.id, **registration_values, }) for booth in booths ]
def test_paying_with_single_gift_card(self): gift_card = self.env['gift.card'].create({ 'initial_amount': 100, }) order = self.empty_order order.write({ 'order_line': [ Command.create({ 'product_id': self.product_A.id, 'name': 'Ordinary Product A', 'product_uom': self.uom_unit.id, 'product_uom_qty': 1.0, }) ] }) before_gift_card_payment = order.amount_total self.assertNotEqual(before_gift_card_payment, 0) order._pay_with_gift_card(gift_card) order.action_confirm() self.assertEqual(before_gift_card_payment - order.amount_total, gift_card.initial_amount - gift_card.balance)
def _prepare_order_line_values( self, product_id, quantity, event_booth_pending_ids=False, registration_values=None, **kwargs ): """Add corresponding event to the SOline creation values (if booths are provided).""" values = super()._prepare_order_line_values(product_id, quantity, **kwargs) if not event_booth_pending_ids: return values booths = self.env['event.booth'].browse(event_booth_pending_ids) values['event_id'] = booths.event_id.id values['event_booth_registration_ids'] = [ Command.create({ 'event_booth_id': booth.id, **registration_values, }) for booth in booths ] return values
def test_paying_with_multiple_gift_card(self): gift_card_1 = self.env['gift.card'].create({ 'initial_amount': 100, }) gift_card_2 = self.env['gift.card'].create({ 'initial_amount': 100, }) order = self.empty_order order.write({ 'order_line': [ Command.create({ 'product_id': self.product_A.id, 'name': 'Ordinary Product A', 'product_uom': self.uom_unit.id, 'product_uom_qty': 20.0, }) ] }) before_gift_card_payment = order.amount_total order._pay_with_gift_card(gift_card_1) order._pay_with_gift_card(gift_card_2) self.assertEqual(order.amount_total, before_gift_card_payment - 200)
def test_create_base_with_tags(self): """ Create records with many2many tags. """ with self.assertQueryCount(2): self.env['test_performance.base'].create({'name': 'X'}) # create N tags: add O(N) queries with self.assertQueryCount(13): self.env['test_performance.base'].create({ 'name': 'X', 'tag_ids': [Command.create({'name': val}) for val in range(10)], }) # link N tags: add O(1) queries tags = self.env['test_performance.tag'].create([{ 'name': val } for val in range(10)]) with self.assertQueryCount(3): self.env['test_performance.base'].create({ 'name': 'X', 'tag_ids': [Command.link(tag.id) for tag in tags], }) with self.assertQueryCount(2): self.env['test_performance.base'].create({ 'name': 'X', 'tag_ids': [Command.set([])], }) with self.assertQueryCount(3): self.env['test_performance.base'].create({ 'name': 'X', 'tag_ids': [Command.set(tags.ids)], })
def test_copy(self): Model = self.env['test_new_api.compute.onchange'] # create tags tag_foo, tag_bar = self.env['test_new_api.multi.tag'].create([ { 'name': 'foo1' }, { 'name': 'bar1' }, ]) # compute 'bar' (readonly), 'baz', 'line_ids' and 'tag_ids' (editable) record = Model.create({'active': True, 'foo': "foo1"}) self.assertEqual(record.bar, "foo1r") self.assertEqual(record.baz, "foo1z") self.assertEqual(record.line_ids.mapped('foo'), ['foo1']) self.assertEqual(record.tag_ids, tag_foo) # manually update 'baz' and 'lines' to test copy attribute record.write({ 'baz': "baz1", 'line_ids': [Command.create({'foo': 'bar'})], 'tag_ids': [Command.link(tag_bar.id)], }) self.assertEqual(record.bar, "foo1r") self.assertEqual(record.baz, "baz1") self.assertEqual(record.line_ids.mapped('foo'), ['foo1', 'bar']) self.assertEqual(record.tag_ids, tag_foo + tag_bar) # copy the record, and check results copied = record.copy() self.assertEqual(copied.foo, "foo1 (copy)") # copied and modified self.assertEqual(copied.bar, "foo1 (copy)r") # computed self.assertEqual(copied.baz, "baz1") # copied self.assertEqual(record.line_ids.mapped('foo'), ['foo1', 'bar']) # copied self.assertEqual(record.tag_ids, tag_foo + tag_bar) # copied
def test_paying_with_multiple_gift_card(self): self.env['loyalty.generate.wizard'].with_context( active_id=self.program_gift_card.id).create({ 'coupon_qty': 2, 'points_granted': 100, }).generate_coupons() gift_card_1, gift_card_2 = self.program_gift_card.coupon_ids order = self.empty_order order.write({ 'order_line': [ Command.create({ 'product_id': self.product_A.id, 'name': 'Ordinary Product A', 'product_uom': self.uom_unit.id, 'product_uom_qty': 20.0, }) ] }) before_gift_card_payment = order.amount_total self._apply_promo_code(order, gift_card_1.code) self._apply_promo_code(order, gift_card_2.code) self.assertEqual(order.amount_total, before_gift_card_payment - 200)
def action_create_alternative(self): if self.env.user.has_group('purchase.group_warning_purchase') and self.creation_blocked: raise UserError( _('The vendor you have selected or at least one of the products you are copying from the original ' 'order has a blocking warning on it and cannot be selected to create an alternative.') ) vals = { 'date_order': self.origin_po_id.date_order, 'partner_id': self.partner_id.id, 'user_id': self.origin_po_id.user_id.id, } if self.copy_products and self.origin_po_id: vals['order_line'] = [Command.create({'product_id': line.product_id.id, 'product_qty': line.product_qty}) for line in self.origin_po_id.order_line] alt_po = self.env['purchase.order'].with_context(origin_po_id=self.origin_po_id.id).create(vals) return { 'type': 'ir.actions.act_window', 'view_mode': 'form', 'res_model': 'purchase.order', 'res_id': alt_po.id, 'context': { 'active_id': alt_po.id, }, }
def _website_product_id_change(self, order_id, product_id, qty=0, **kwargs): values = super(SaleOrder, self)._website_product_id_change(order_id, product_id, qty=qty, **kwargs) event_booth_pending_ids = kwargs.get('event_booth_pending_ids') if event_booth_pending_ids: order_line = self.env['sale.order.line'].sudo().search([ ('id', 'in', self.order_line.ids), ('event_booth_pending_ids', 'in', event_booth_pending_ids) ]) booths = self.env['event.booth'].browse(event_booth_pending_ids) new_registration_ids = [ Command.create({ 'event_booth_id': booth.id, **kwargs.get('registration_values'), }) for booth in booths ] if order_line: event_booth_registration_ids = [ Command.delete(reg.id) for reg in order_line.event_booth_registration_ids ] + new_registration_ids else: event_booth_registration_ids = new_registration_ids values.update( event_id=booths.event_id.id, event_booth_registration_ids=event_booth_registration_ids, name=booths._get_booth_multiline_description, ) return values
def setUpClass(cls, chart_template_ref=None): super().setUpClass(chart_template_ref=chart_template_ref) uom_unit = cls.env.ref('uom.product_uom_unit') product = cls.env['product.product'].create({ 'name': "Product", 'list_price': 30.0, 'type': 'consu', 'uom_id': uom_unit.id, 'uom_po_id': uom_unit.id, 'invoice_policy': 'delivery', }) cls.purchase_order = cls.env['purchase.order'].with_context( tracking_disable=True).create({ 'partner_id': cls.partner_a.id, 'order_line': [ Command.create({ 'name': product.name, 'product_id': product.id, 'product_qty': 10.0, 'product_uom': product.uom_id.id, 'price_unit': product.list_price, 'taxes_id': False, }), ] }) cls.purchase_order.button_confirm() cls.account_expense = cls.company_data['default_account_expense'] cls.account_revenue = cls.company_data['default_account_revenue']
def test_paying_with_single_gift_card_under(self): self.env['loyalty.generate.wizard'].with_context( active_id=self.program_gift_card.id).create({ 'coupon_qty': 1, 'points_granted': 100, }).generate_coupons() gift_card = self.program_gift_card.coupon_ids[0] order = self.empty_order order.write({ 'order_line': [ Command.create({ 'product_id': self.product_B.id, 'name': 'Ordinary Product b', 'product_uom': self.uom_unit.id, 'product_uom_qty': 1.0, }) ] }) before_gift_card_payment = order.amount_total self.assertNotEqual(before_gift_card_payment, 0) self._apply_promo_code(order, gift_card.code) order.action_confirm() self.assertEqual(before_gift_card_payment - order.amount_total, 100 - gift_card.points)
def _compute_event_ticket_ids(self): """ Update event configuration from its event type. Depends are set only on event_type_id itself, not its sub fields. Purpose is to emulate an onchange: if event type is changed, update event configuration. Changing event type content itself should not trigger this method. When synchronizing tickets: * lines that have no registrations linked are remove; * type lines are added; Note that updating event_ticket_ids triggers _compute_start_sale_date (start_sale_datetime computation) so ensure result to avoid cache miss. """ for event in self: if not event.event_type_id and not event.event_ticket_ids: event.event_ticket_ids = False continue # lines to keep: those with existing registrations tickets_to_remove = event.event_ticket_ids.filtered( lambda ticket: not ticket._origin.registration_ids) command = [ Command.unlink(ticket.id) for ticket in tickets_to_remove ] if event.event_type_id.event_type_ticket_ids: command += [ Command.create({ attribute_name: line[attribute_name] if not isinstance(line[attribute_name], models.BaseModel) else line[attribute_name].id for attribute_name in self.env['event.type.ticket']. _get_event_ticket_fields_whitelist() }) for line in event.event_type_id.event_type_ticket_ids ] event.event_ticket_ids = command
def test_create_multi(self): """ create for multiple records """ # assumption: 'res.bank' does not override 'create' vals_list = [{'name': name} for name in ('Foo', 'Bar', 'Baz')] vals_list[0]['email'] = '*****@*****.**' for vals in vals_list: record = self.env['res.bank'].create(vals) self.assertEqual(len(record), 1) self.assertEqual(record.name, vals['name']) self.assertEqual(record.email, vals.get('email', False)) records = self.env['res.bank'].create([]) self.assertFalse(records) records = self.env['res.bank'].create(vals_list) self.assertEqual(len(records), len(vals_list)) for record, vals in zip(records, vals_list): self.assertEqual(record.name, vals['name']) self.assertEqual(record.email, vals.get('email', False)) # create countries and states vals_list = [{ 'name': 'Foo', 'state_ids': [ Command.create({'name': 'North Foo', 'code': 'NF'}), Command.create({'name': 'South Foo', 'code': 'SF'}), Command.create({'name': 'West Foo', 'code': 'WF'}), Command.create({'name': 'East Foo', 'code': 'EF'}), ], }, { 'name': 'Bar', 'state_ids': [ Command.create({'name': 'North Bar', 'code': 'NB'}), Command.create({'name': 'South Bar', 'code': 'SB'}), ], }] foo, bar = self.env['res.country'].create(vals_list) self.assertEqual(foo.name, 'Foo') self.assertCountEqual(foo.mapped('state_ids.code'), ['NF', 'SF', 'WF', 'EF']) self.assertEqual(bar.name, 'Bar') self.assertCountEqual(bar.mapped('state_ids.code'), ['NB', 'SB'])