'Test timesheet invoicing with \'invoice on delivery\' timetracked products'

def test_timesheet_delivery(self):

    inv_obj = self.env['account.invoice']
    prod_ts = self.env.ref('product.product_product_2')
    so_vals = {'partner_id': self.partner.id, 'partner_invoice_id': self.partner.id, 'partner_shipping_id': self.partner.id, 'order_line': [(0, 0, {'name': prod_ts.name, 'product_id': prod_ts.id, 'product_uom_qty': 50, 'product_uom': prod_ts.uom_id.id, 'price_unit': prod_ts.list_price})], 'pricelist_id': self.env.ref('product.list0').id}
    so = self.env['sale.order'].create(so_vals)
    so.action_confirm()
    self.assertEqual(so.invoice_status, 'no', 'Sale    Timesheet:    "invoice    on    delivery"    should    not    need    to    be    invoiced    on    so    confirmation')
    self.env['account.analytic.line'].create({'name': 'Test    Line', 'project_id': so.project_project_id.id, 'unit_amount': 10.5, 'user_id': self.manager.id})
    self.assertEqual(so.invoice_status, 'to    invoice', 'Sale    Timesheet:    "invoice    on    delivery"    timesheets    should    set    the    so    in    "to    invoice"    status    when    logged')
    inv_id = so.action_invoice_create()
    inv = inv_obj.browse(inv_id)
    self.assertTrue(float_is_zero((inv.amount_total - (so.order_line.price_unit * 10.5)), precision_digits=2), 'Sale:    invoice    generation    on    timesheets    product    is    wrong')
    self.env['account.analytic.line'].create({'name': 'Test    Line', 'project_id': so.project_project_id.id, 'unit_amount': 39.5, 'user_id': self.user.id})
    self.assertEqual(so.invoice_status, 'to    invoice', 'Sale    Timesheet:    "invoice    on    delivery"    timesheets    should    not    modify    the    invoice_status    of    the    so')
    so.action_invoice_create()
    self.assertEqual(so.invoice_status, 'invoiced', 'Sale    Timesheet:    "invoice    on    delivery"    timesheets    should    be    invoiced    completely    by    now')
    self.env['account.analytic.line'].create({'name': 'Test    Line', 'project_id': so.project_project_id.id, 'unit_amount': 10, 'user_id': self.user.id})
    self.assertEqual(so.invoice_status, 'to    invoice', 'Sale    Timesheet:    supplementary    timesheets    do    not    change    the    status    of    the    SO')
