Skip to content
Snippets Groups Projects
Commit 685decd1 authored by Nicolas Martinelli's avatar Nicolas Martinelli
Browse files

[ADD] sale_timesheet: adaptation due to the new Sale module

New module to be able to link timesheet on a sale order.

Reason: complete rewrite of the Sale module.

Responsible: fp, dbo, nim
parent e57ac164
No related branches found
No related tags found
No related merge requests found
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import models
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
{
'name': 'Sales Timesheet',
'version': '1.0',
'category': 'Hidden',
'summary': 'Sell based on timesheets',
'description': """
Allows to sell timesheets in your sales order
=============================================
This module set the right product on all timesheet lines
according to the order/contract you work on. This allows to
have real delivered quantities in sales orders.
""",
'author': 'OpenERP SA',
'website': 'https://www.odoo.com/page/warehouse',
'depends': ['sale', 'hr_timesheet'],
'data': ['views/sale_timesheet_view.xml'],
'demo': ['data/sale_timesheet_demo.xml'],
'installable': True,
'auto_install': True,
}
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data noupdate="1">
<record id="product.product_product_0" model="product.product">
<field name="track_service">timesheet</field>
</record>
<record id="product.product_product_2" model="product.product">
<field name="name">Support Contract (on timesheet)</field>
<field name="track_service">timesheet</field>
</record>
</data>
</openerp>
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import sale_timesheet
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from openerp import models, api, fields
from openerp.tools.translate import _
from openerp.exceptions import UserError
class ResCompany(models.Model):
_inherit = 'res.company'
@api.model
def _get_uom_hours(self):
try:
return self.env.ref("product.product_uom_hour")
except ValueError:
return False
project_time_mode_id = fields.Many2one('product.uom', string='Timesheet UoM', default=_get_uom_hours)
class HrEmployee(models.Model):
_inherit = 'hr.employee'
timesheet_cost = fields.Float(string='Timesheet Cost', default=0.0)
class ProductTemplate(models.Model):
_inherit = 'product.template'
track_service = fields.Selection(selection_add=[('timesheet', 'Timesheets on contract')])
@api.onchange('type', 'invoice_policy')
def onchange_type_timesheet(self):
if self.type == 'service' and self.invoice_policy == 'cost':
self.track_service = 'timesheet'
if self.type != 'service':
self.track_service = 'manual'
return {}
class AccountAnalyticLine(models.Model):
_inherit = 'account.analytic.line'
def _get_sale_order_line(self, vals=None):
result = dict(vals or {})
if self.is_timesheet:
sol = result.get('so_line', False) or self.so_line
if not sol and self.account_id:
sol = self.env['sale.order.line'].search([
('order_id.project_id', '=', self.account_id.id),
('state', '=', 'sale'),
('product_id.track_service', '=', 'timesheet'),
('product_id.type', '=', 'service')],
limit=1)
else:
sol = self.so_line
if sol:
emp = self.env['hr.employee'].search([('user_id', '=', self.user_id.id)], limit=1)
if result.get('amount', False):
amount = result['amount']
elif emp and emp.timesheet_cost:
amount = -self.unit_amount * emp.timesheet_cost
elif self.product_id and self.product_id.standard_price:
amount = -self.unit_amount * self.product_id.standard_price
else:
amount = self.amount or 0.0
result.update({
'product_id': sol.product_id.id,
'product_uom_id': self.env.user.company_id.project_time_mode_id.id or sol.product_id.uom_id.id,
'amount': amount,
'so_line': sol.id,
})
result = super(AccountAnalyticLine, self)._get_sale_order_line(vals=result)
return result
class SaleOrder(models.Model):
_inherit = 'sale.order'
@api.one
@api.constrains('order_line')
def _check_multi_timesheet(self):
count = 0
for line in self.order_line:
if line.product_id.track_service == 'timesheet':
count += 1
if count > 1:
raise UserError(_("You can use only one product on timesheet within the same sale order. You should split your order to include only one contract based on time and material."))
return {}
@api.one
def action_confirm(self):
result = super(SaleOrder, self).action_confirm()
if not self.project_id:
for line in self.order_line:
if line.product_id.track_service == 'timesheet':
self._create_analytic_account(prefix=self.product_id.default_code or None)
break
return result
class SaleOrderLine(models.Model):
_inherit = "sale.order.line"
@api.multi
def _compute_analytic(self, domain=None):
if not domain:
domain = [('so_line', 'in', self.ids), '|', ('amount', '<', 0.0), ('is_timesheet', '=', True)]
return super(SaleOrderLine, self)._compute_analytic(domain=domain)
import test_sale_timesheet
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from openerp.tools import float_is_zero
from openerp.addons.sale.tests.test_sale_common import TestSale
class TestSaleTimesheet(TestSale):
def test_timesheet_order(self):
""" Test timesheet invoicing with 'invoice on order' timetracked products """
# intial so
prod_ts = self.env.ref('product.product_product_0')
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()
so.action_invoice_create()
# let's log some timesheets
self.env['account.analytic.line'].create({
'name': 'Test Line',
'account_id': so.project_id.id,
'unit_amount': 10.5,
'user_id': self.manager.id,
'is_timesheet': True,
})
self.assertEqual(so.order_line.qty_delivered, 10.5, 'Sale Timesheet: timesheet does not increase delivered quantity on so line')
self.assertEqual(so.invoice_status, 'invoiced', 'Sale Timesheet: "invoice on order" timesheets should not modify the invoice_status of the so')
self.env['account.analytic.line'].create({
'name': 'Test Line',
'account_id': so.project_id.id,
'unit_amount': 39.5,
'user_id': self.user.id,
'is_timesheet': True,
})
self.assertEqual(so.order_line.qty_delivered, 50, 'Sale Timesheet: timesheet does not increase delivered quantity on so line')
self.assertEqual(so.invoice_status, 'invoiced', 'Sale Timesheet: "invoice on order" timesheets should not modify the invoice_status of the so')
self.env['account.analytic.line'].create({
'name': 'Test Line',
'account_id': so.project_id.id,
'unit_amount': 10,
'user_id': self.user.id,
'is_timesheet': True,
})
self.assertEqual(so.order_line.qty_delivered, 60, 'Sale Timesheet: timesheet does not increase delivered quantity on so line')
self.assertEqual(so.invoice_status, 'upselling', 'Sale Timesheet: "invoice on order" timesheets should not modify the invoice_status of the so')
def test_timesheet_delivery(self):
""" Test timesheet invoicing with 'invoice on delivery' timetracked products """
inv_obj = self.env['account.invoice']
# intial so
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')
# let's log some timesheets
self.env['account.analytic.line'].create({
'name': 'Test Line',
'account_id': so.project_id.id,
'unit_amount': 10.5,
'user_id': self.manager.id,
'is_timesheet': True,
})
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',
'account_id': so.project_id.id,
'unit_amount': 39.5,
'user_id': self.user.id,
'is_timesheet': True,
})
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',
'account_id': so.project_id.id,
'unit_amount': 10,
'user_id': self.user.id,
'is_timesheet': True,
})
self.assertEqual(so.invoice_status, 'to invoice', 'Sale Timesheet: supplementary timesheets do not change the status of the SO')
def test_timesheet_uom(self):
""" Test timesheet invoicing and uom conversion """
# intial so
prod_ts = self.env.ref('product.product_product_2')
uom_days = self.env.ref('product.product_uom_day')
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': 5, 'product_uom': uom_days.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()
# let's log some timesheets
self.env['account.analytic.line'].create({
'name': 'Test Line',
'account_id': so.project_id.id,
'unit_amount': 16,
'user_id': self.manager.id,
'is_timesheet': True,
})
self.assertEqual(so.order_line.qty_delivered, 2, 'Sale: uom conversion of timesheets is wrong')
self.env['account.analytic.line'].create({
'name': 'Test Line',
'account_id': so.project_id.id,
'unit_amount': 24,
'user_id': self.user.id,
'is_timesheet': True,
})
so.action_invoice_create()
self.assertEqual(so.invoice_status, 'invoiced', 'Sale Timesheet: "invoice on delivery" timesheets should not modify the invoice_status of the so')
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="view_product_timesheet_form" model="ir.ui.view">
<field name="name">product.template.timesheet.form</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="sale.product_template_form_view_invoice_policy"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='track_service']" position="attributes">
<attribute name="invisible">False</attribute>
<attribute name="attrs">{'invisible': [('type','!=','service')]}</attribute>
</xpath>
</field>
</record>
<record id="hr_timesheet_employee_extd_form" model="ir.ui.view">
<field name="name">hr.timesheet.employee.extd_form</field>
<field name="model">hr.employee</field>
<field name="inherit_id" ref="hr.view_employee_form"/>
<field name="arch" type="xml">
<xpath expr="//group[@name='active_group']" position="before">
<group string="Timesheets">
<field name="timesheet_cost"/>
</group>
</xpath>
</field>
</record>
</data>
</openerp>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment