summaryrefslogtreecommitdiff
path: root/addons/sale_timesheet/wizard
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/sale_timesheet/wizard
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/sale_timesheet/wizard')
-rw-r--r--addons/sale_timesheet/wizard/__init__.py7
-rw-r--r--addons/sale_timesheet/wizard/project_create_invoice.py58
-rw-r--r--addons/sale_timesheet/wizard/project_create_invoice_views.xml31
-rw-r--r--addons/sale_timesheet/wizard/project_create_sale_order.py368
-rw-r--r--addons/sale_timesheet/wizard/project_create_sale_order_views.xml53
-rw-r--r--addons/sale_timesheet/wizard/project_task_create_sale_order.py145
-rw-r--r--addons/sale_timesheet/wizard/project_task_create_sale_order_views.xml46
-rw-r--r--addons/sale_timesheet/wizard/sale_make_invoice_advance.py49
-rw-r--r--addons/sale_timesheet/wizard/sale_make_invoice_advance_views.xml28
9 files changed, 785 insertions, 0 deletions
diff --git a/addons/sale_timesheet/wizard/__init__.py b/addons/sale_timesheet/wizard/__init__.py
new file mode 100644
index 00000000..42502ae6
--- /dev/null
+++ b/addons/sale_timesheet/wizard/__init__.py
@@ -0,0 +1,7 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from . import project_create_sale_order
+from . import project_create_invoice
+from . import sale_make_invoice_advance
+from . import project_task_create_sale_order
diff --git a/addons/sale_timesheet/wizard/project_create_invoice.py b/addons/sale_timesheet/wizard/project_create_invoice.py
new file mode 100644
index 00000000..b1da2068
--- /dev/null
+++ b/addons/sale_timesheet/wizard/project_create_invoice.py
@@ -0,0 +1,58 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, fields, models, _
+from odoo.exceptions import UserError
+
+
+class ProjectCreateInvoice(models.TransientModel):
+ _name = 'project.create.invoice'
+ _description = "Create Invoice from project"
+
+ @api.model
+ def default_get(self, fields):
+ result = super(ProjectCreateInvoice, self).default_get(fields)
+
+ active_model = self._context.get('active_model')
+ if active_model != 'project.project':
+ raise UserError(_('You can only apply this action from a project.'))
+
+ active_id = self._context.get('active_id')
+ if 'project_id' in fields and active_id:
+ result['project_id'] = active_id
+ return result
+
+ project_id = fields.Many2one('project.project', "Project", help="Project to make billable", required=True)
+ _candidate_orders = fields.Many2many('sale.order', compute='_compute_candidate_orders')
+ sale_order_id = fields.Many2one(
+ 'sale.order', string="Choose the Sales Order to invoice", required=True,
+ domain="[('id', 'in', _candidate_orders)]"
+ )
+ amount_to_invoice = fields.Monetary("Amount to invoice", compute='_compute_amount_to_invoice', currency_field='currency_id', help="Total amount to invoice on the sales order, including all items (services, storables, expenses, ...)")
+ currency_id = fields.Many2one(related='sale_order_id.currency_id', readonly=True)
+
+ @api.depends('project_id.tasks.sale_line_id.order_id.invoice_status')
+ def _compute_candidate_orders(self):
+ for p in self:
+ p._candidate_orders = p.project_id\
+ .mapped('tasks.sale_line_id.order_id')\
+ .filtered(lambda so: so.invoice_status == 'to invoice')
+
+ @api.depends('sale_order_id')
+ def _compute_amount_to_invoice(self):
+ for wizard in self:
+ amount_untaxed = 0.0
+ amount_tax = 0.0
+ for line in wizard.sale_order_id.order_line.filtered(lambda sol: sol.invoice_status == 'to invoice'):
+ amount_untaxed += line.price_reduce * line.qty_to_invoice
+ amount_tax += line.price_tax
+ wizard.amount_to_invoice = amount_untaxed + amount_tax
+
+ def action_create_invoice(self):
+ if not self.sale_order_id and self.sale_order_id.invoice_status != 'to invoice':
+ raise UserError(_("The selected Sales Order should contain something to invoice."))
+ action = self.env["ir.actions.actions"]._for_xml_id("sale.action_view_sale_advance_payment_inv")
+ action['context'] = {
+ 'active_ids': self.sale_order_id.ids
+ }
+ return action
diff --git a/addons/sale_timesheet/wizard/project_create_invoice_views.xml b/addons/sale_timesheet/wizard/project_create_invoice_views.xml
new file mode 100644
index 00000000..55e2bdff
--- /dev/null
+++ b/addons/sale_timesheet/wizard/project_create_invoice_views.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<odoo>
+
+ <record id="project_create_invoice_view_form" model="ir.ui.view">
+ <field name="name">project.create.invoice.view.form</field>
+ <field name="model">project.create.invoice</field>
+ <field name="arch" type="xml">
+ <form string="Create Sales Order from Project">
+ <group>
+ <field name="project_id" readonly="1"/>
+ <field name="_candidate_orders" invisible="1"/>
+ <field name="sale_order_id" options="{'no_create_edit': True}" context="{'sale_show_partner_name': True}"/>
+ <field name="amount_to_invoice"/>
+ </group>
+ <footer>
+ <button string="Create Invoice" type="object" name="action_create_invoice" class="oe_highlight"/>
+ <button string="Cancel" special="cancel" type="object" class="btn btn-secondary oe_inline"/>
+ </footer>
+ </form>
+ </field>
+ </record>
+
+ <record id="project_project_action_multi_create_invoice" model="ir.actions.act_window">
+ <field name="name">Create Invoice</field>
+ <field name="res_model">project.create.invoice</field>
+ <field name="view_mode">form</field>
+ <field name="view_id" ref="project_create_invoice_view_form"/>
+ <field name="target">new</field>
+ </record>
+
+</odoo>
diff --git a/addons/sale_timesheet/wizard/project_create_sale_order.py b/addons/sale_timesheet/wizard/project_create_sale_order.py
new file mode 100644
index 00000000..8c327112
--- /dev/null
+++ b/addons/sale_timesheet/wizard/project_create_sale_order.py
@@ -0,0 +1,368 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, fields, models, _
+from odoo.exceptions import UserError
+
+
+class ProjectCreateSalesOrder(models.TransientModel):
+ _name = 'project.create.sale.order'
+ _description = "Create SO from project"
+
+ @api.model
+ def default_get(self, fields):
+ result = super(ProjectCreateSalesOrder, self).default_get(fields)
+
+ active_model = self._context.get('active_model')
+ if active_model != 'project.project':
+ raise UserError(_("You can only apply this action from a project."))
+
+ active_id = self._context.get('active_id')
+ if 'project_id' in fields and active_id:
+ project = self.env['project.project'].browse(active_id)
+ if project.sale_order_id:
+ raise UserError(_("The project has already a sale order."))
+ result['project_id'] = active_id
+ if not result.get('partner_id', False):
+ result['partner_id'] = project.partner_id.id
+ if project.bill_type == 'customer_project' and not result.get('line_ids', False):
+ if project.pricing_type == 'employee_rate':
+ default_product = self.env.ref('sale_timesheet.time_product', False)
+ result['line_ids'] = [
+ (0, 0, {
+ 'employee_id': e.employee_id.id,
+ 'product_id': e.timesheet_product_id.id or default_product.id,
+ 'price_unit': e.price_unit if e.timesheet_product_id else default_product.lst_price
+ }) for e in project.sale_line_employee_ids]
+ employee_from_timesheet = project.task_ids.timesheet_ids.employee_id - project.sale_line_employee_ids.employee_id
+ result['line_ids'] += [
+ (0, 0, {
+ 'employee_id': e.id,
+ 'product_id': default_product.id,
+ 'price_unit': default_product.lst_price
+ }) for e in employee_from_timesheet]
+ else:
+ result['line_ids'] = [
+ (0, 0, {
+ 'product_id': p.id,
+ 'price_unit': p.lst_price
+ }) for p in project.task_ids.timesheet_product_id]
+ return result
+
+ project_id = fields.Many2one('project.project', "Project", domain=[('sale_line_id', '=', False)], help="Project for which we are creating a sales order", required=True)
+ company_id = fields.Many2one(related='project_id.company_id')
+ partner_id = fields.Many2one('res.partner', string="Customer", required=True, help="Customer of the sales order")
+ commercial_partner_id = fields.Many2one(related='partner_id.commercial_partner_id')
+
+ pricing_type = fields.Selection(related="project_id.pricing_type")
+ link_selection = fields.Selection([('create', 'Create a new sales order'), ('link', 'Link to an existing sales order')], required=True, default='create')
+ sale_order_id = fields.Many2one(
+ 'sale.order', string="Sales Order",
+ domain="['|', '|', ('partner_id', '=', partner_id), ('partner_id', 'child_of', commercial_partner_id), ('partner_id', 'parent_of', partner_id)]")
+
+ line_ids = fields.One2many('project.create.sale.order.line', 'wizard_id', string='Lines')
+ info_invoice = fields.Char(compute='_compute_info_invoice')
+
+ @api.depends('sale_order_id', 'link_selection')
+ def _compute_info_invoice(self):
+ for line in self:
+ tasks = line.project_id.tasks.filtered(lambda t: not t.non_allow_billable)
+ domain = self.env['sale.order.line']._timesheet_compute_delivered_quantity_domain()
+ timesheet = self.env['account.analytic.line'].read_group(domain + [('task_id', 'in', tasks.ids), ('so_line', '=', False), ('timesheet_invoice_id', '=', False)], ['unit_amount'], ['task_id'])
+ unit_amount = round(sum(t.get('unit_amount', 0) for t in timesheet), 2) if timesheet else 0
+ if not unit_amount:
+ line.info_invoice = False
+ continue
+ company_uom = self.env.company.timesheet_encode_uom_id
+ label = _("hours")
+ if company_uom == self.env.ref('uom.product_uom_day'):
+ label = _("days")
+ if line.link_selection == 'create':
+ line.info_invoice = _("%(amount)s %(label)s will be added to the new Sales Order.", amount=unit_amount, label=label)
+ else:
+ line.info_invoice = _("%(amount)s %(label)s will be added to the selected Sales Order.", amount=unit_amount, label=label)
+
+ @api.onchange('partner_id')
+ def _onchange_partner_id(self):
+ self.sale_order_id = False
+
+ def action_link_sale_order(self):
+ task_no_sale_line = self.project_id.tasks.filtered(lambda task: not task.sale_line_id)
+ # link the project to the SO line
+ self.project_id.write({
+ 'sale_line_id': self.sale_order_id.order_line[0].id,
+ 'sale_order_id': self.sale_order_id.id,
+ 'partner_id': self.partner_id.id,
+ })
+
+ if self.pricing_type == 'employee_rate':
+ lines_already_present = dict([(l.employee_id.id, l) for l in self.project_id.sale_line_employee_ids])
+ EmployeeMap = self.env['project.sale.line.employee.map'].sudo()
+
+ for wizard_line in self.line_ids:
+ if wizard_line.employee_id.id not in lines_already_present:
+ EmployeeMap.create({
+ 'project_id': self.project_id.id,
+ 'sale_line_id': wizard_line.sale_line_id.id,
+ 'employee_id': wizard_line.employee_id.id,
+ })
+ else:
+ lines_already_present[wizard_line.employee_id.id].write({
+ 'sale_line_id': wizard_line.sale_line_id.id
+ })
+
+ self.project_id.tasks.filtered(lambda task: task.non_allow_billable).sale_line_id = False
+ tasks = self.project_id.tasks.filtered(lambda t: not t.non_allow_billable)
+ # assign SOL to timesheets
+ for map_entry in self.project_id.sale_line_employee_ids:
+ self.env['account.analytic.line'].search([('task_id', 'in', tasks.ids), ('employee_id', '=', map_entry.employee_id.id), ('so_line', '=', False)]).write({
+ 'so_line': map_entry.sale_line_id.id
+ })
+ else:
+ dict_product_sol = dict([(l.product_id.id, l.id) for l in self.sale_order_id.order_line])
+ # remove SOL for task without product
+ # and if a task has a product that match a product from a SOL, we put this SOL on task.
+ for task in task_no_sale_line:
+ if not task.timesheet_product_id:
+ task.sale_line_id = False
+ elif task.timesheet_product_id.id in dict_product_sol:
+ task.write({'sale_line_id': dict_product_sol[task.timesheet_product_id.id]})
+
+ def action_create_sale_order(self):
+ # if project linked to SO line or at least on tasks with SO line, then we consider project as billable.
+ if self.project_id.sale_line_id:
+ raise UserError(_("The project is already linked to a sales order item."))
+ # at least one line
+ if not self.line_ids:
+ raise UserError(_("At least one line should be filled."))
+
+ if self.pricing_type == 'employee_rate':
+ # all employee having timesheet should be in the wizard map
+ timesheet_employees = self.env['account.analytic.line'].search([('task_id', 'in', self.project_id.tasks.ids)]).mapped('employee_id')
+ map_employees = self.line_ids.mapped('employee_id')
+ missing_meployees = timesheet_employees - map_employees
+ if missing_meployees:
+ raise UserError(_('The Sales Order cannot be created because you did not enter some employees that entered timesheets on this project. Please list all the relevant employees before creating the Sales Order.\nMissing employee(s): %s') % (', '.join(missing_meployees.mapped('name'))))
+
+ # check here if timesheet already linked to SO line
+ timesheet_with_so_line = self.env['account.analytic.line'].search_count([('task_id', 'in', self.project_id.tasks.ids), ('so_line', '!=', False)])
+ if timesheet_with_so_line:
+ raise UserError(_('The sales order cannot be created because some timesheets of this project are already linked to another sales order.'))
+
+ # create SO according to the chosen billable type
+ sale_order = self._create_sale_order()
+
+ view_form_id = self.env.ref('sale.view_order_form').id
+ action = self.env["ir.actions.actions"]._for_xml_id("sale.action_orders")
+ action.update({
+ 'views': [(view_form_id, 'form')],
+ 'view_mode': 'form',
+ 'name': sale_order.name,
+ 'res_id': sale_order.id,
+ })
+ return action
+
+ def _create_sale_order(self):
+ """ Private implementation of generating the sales order """
+ sale_order = self.env['sale.order'].create({
+ 'project_id': self.project_id.id,
+ 'partner_id': self.partner_id.id,
+ 'analytic_account_id': self.project_id.analytic_account_id.id,
+ 'client_order_ref': self.project_id.name,
+ 'company_id': self.project_id.company_id.id,
+ })
+ sale_order.onchange_partner_id()
+ sale_order.onchange_partner_shipping_id()
+ # rewrite the user as the onchange_partner_id erases it
+ sale_order.write({'user_id': self.project_id.user_id.id})
+ sale_order.onchange_user_id()
+
+ # create the sale lines, the map (optional), and assign existing timesheet to sale lines
+ self._make_billable(sale_order)
+
+ # confirm SO
+ sale_order.action_confirm()
+ return sale_order
+
+ def _make_billable(self, sale_order):
+ if self.pricing_type == 'fixed_rate':
+ self._make_billable_at_project_rate(sale_order)
+ else:
+ self._make_billable_at_employee_rate(sale_order)
+
+ def _make_billable_at_project_rate(self, sale_order):
+ self.ensure_one()
+ task_left = self.project_id.tasks.filtered(lambda task: not task.sale_line_id)
+ ticket_timesheet_ids = self.env.context.get('ticket_timesheet_ids', [])
+ for wizard_line in self.line_ids:
+ task_ids = self.project_id.tasks.filtered(lambda task: not task.sale_line_id and task.timesheet_product_id == wizard_line.product_id)
+ task_left -= task_ids
+ # trying to simulate the SO line created a task, according to the product configuration
+ # To avoid, generating a task when confirming the SO
+ task_id = False
+ if task_ids and wizard_line.product_id.service_tracking in ['task_in_project', 'task_global_project']:
+ task_id = task_ids.ids[0]
+
+ # create SO line
+ sale_order_line = self.env['sale.order.line'].create({
+ 'order_id': sale_order.id,
+ 'product_id': wizard_line.product_id.id,
+ 'price_unit': wizard_line.price_unit,
+ 'project_id': self.project_id.id, # prevent to re-create a project on confirmation
+ 'task_id': task_id,
+ 'product_uom_qty': 0.0,
+ })
+
+ if ticket_timesheet_ids and not self.project_id.sale_line_id and not task_ids:
+ # With pricing = "project rate" in project. When the user wants to create a sale order from a ticket in helpdesk
+ # The project cannot contain any tasks. Thus, we need to give the first sale_order_line created to link
+ # the timesheet to this first sale order line.
+ # link the project to the SO line
+ self.project_id.write({
+ 'sale_order_id': sale_order.id,
+ 'sale_line_id': sale_order_line.id,
+ 'partner_id': self.partner_id.id,
+ })
+
+ # link the tasks to the SO line
+ task_ids.write({
+ 'sale_line_id': sale_order_line.id,
+ 'partner_id': sale_order.partner_id.id,
+ 'email_from': sale_order.partner_id.email,
+ })
+
+ # assign SOL to timesheets
+ search_domain = [('task_id', 'in', task_ids.ids), ('so_line', '=', False)]
+ if ticket_timesheet_ids:
+ search_domain = [('id', 'in', ticket_timesheet_ids), ('so_line', '=', False)]
+
+ self.env['account.analytic.line'].search(search_domain).write({
+ 'so_line': sale_order_line.id
+ })
+ sale_order_line.with_context({'no_update_planned_hours': True}).write({
+ 'product_uom_qty': sale_order_line.qty_delivered
+ })
+
+ if ticket_timesheet_ids and self.project_id.sale_line_id and not self.project_id.tasks and len(self.line_ids) > 1:
+ # Then, we need to give to the project the last sale order line created
+ self.project_id.write({
+ 'sale_line_id': sale_order_line.id
+ })
+ else: # Otherwise, we are in the normal behaviour
+ # link the project to the SO line
+ self.project_id.write({
+ 'sale_order_id': sale_order.id,
+ 'sale_line_id': sale_order_line.id, # we take the last sale_order_line created
+ 'partner_id': self.partner_id.id,
+ })
+
+ if task_left:
+ task_left.sale_line_id = False
+
+ def _make_billable_at_employee_rate(self, sale_order):
+ # trying to simulate the SO line created a task, according to the product configuration
+ # To avoid, generating a task when confirming the SO
+ task_id = self.env['project.task'].search([('project_id', '=', self.project_id.id)], order='create_date DESC', limit=1).id
+ project_id = self.project_id.id
+
+ lines_already_present = dict([(l.employee_id.id, l) for l in self.project_id.sale_line_employee_ids])
+
+ non_billable_tasks = self.project_id.tasks.filtered(lambda task: not task.sale_line_id)
+ non_allow_billable_tasks = self.project_id.tasks.filtered(lambda task: task.non_allow_billable)
+
+ map_entries = self.env['project.sale.line.employee.map']
+ EmployeeMap = self.env['project.sale.line.employee.map'].sudo()
+
+ # create SO lines: create on SOL per product/price. So many employee can be linked to the same SOL
+ map_product_price_sol = {} # (product_id, price) --> SOL
+ for wizard_line in self.line_ids:
+ map_key = (wizard_line.product_id.id, wizard_line.price_unit)
+ if map_key not in map_product_price_sol:
+ values = {
+ 'order_id': sale_order.id,
+ 'product_id': wizard_line.product_id.id,
+ 'price_unit': wizard_line.price_unit,
+ 'product_uom_qty': 0.0,
+ }
+ if wizard_line.product_id.service_tracking in ['task_in_project', 'task_global_project']:
+ values['task_id'] = task_id
+ if wizard_line.product_id.service_tracking in ['task_in_project', 'project_only']:
+ values['project_id'] = project_id
+
+ sale_order_line = self.env['sale.order.line'].create(values)
+ map_product_price_sol[map_key] = sale_order_line
+
+ if wizard_line.employee_id.id not in lines_already_present:
+ map_entries |= EmployeeMap.create({
+ 'project_id': self.project_id.id,
+ 'sale_line_id': map_product_price_sol[map_key].id,
+ 'employee_id': wizard_line.employee_id.id,
+ })
+ else:
+ map_entries |= lines_already_present[wizard_line.employee_id.id]
+ lines_already_present[wizard_line.employee_id.id].write({
+ 'sale_line_id': map_product_price_sol[map_key].id
+ })
+
+ # link the project to the SO
+ self.project_id.write({
+ 'sale_order_id': sale_order.id,
+ 'sale_line_id': sale_order.order_line[0].id,
+ 'partner_id': self.partner_id.id,
+ })
+ non_billable_tasks.write({
+ 'partner_id': sale_order.partner_id.id,
+ 'email_from': sale_order.partner_id.email,
+ })
+ non_allow_billable_tasks.sale_line_id = False
+
+ tasks = self.project_id.tasks.filtered(lambda t: not t.non_allow_billable)
+ # assign SOL to timesheets
+ for map_entry in map_entries:
+ search_domain = [('employee_id', '=', map_entry.employee_id.id), ('so_line', '=', False)]
+ ticket_timesheet_ids = self.env.context.get('ticket_timesheet_ids', [])
+ if ticket_timesheet_ids:
+ search_domain.append(('id', 'in', ticket_timesheet_ids))
+ else:
+ search_domain.append(('task_id', 'in', tasks.ids))
+ self.env['account.analytic.line'].search(search_domain).write({
+ 'so_line': map_entry.sale_line_id.id
+ })
+ map_entry.sale_line_id.with_context({'no_update_planned_hours': True}).write({
+ 'product_uom_qty': map_entry.sale_line_id.qty_delivered
+ })
+
+ return map_entries
+
+
+class ProjectCreateSalesOrderLine(models.TransientModel):
+ _name = 'project.create.sale.order.line'
+ _description = 'Create SO Line from project'
+ _order = 'id,create_date'
+
+ wizard_id = fields.Many2one('project.create.sale.order', required=True)
+ product_id = fields.Many2one('product.product', domain=[('type', '=', 'service'), ('invoice_policy', '=', 'delivery'), ('service_type', '=', 'timesheet')], string="Service",
+ help="Product of the sales order item. Must be a service invoiced based on timesheets on tasks.")
+ price_unit = fields.Float("Unit Price", help="Unit price of the sales order item.")
+ currency_id = fields.Many2one('res.currency', string="Currency")
+ employee_id = fields.Many2one('hr.employee', string="Employee", help="Employee that has timesheets on the project.")
+ sale_line_id = fields.Many2one('sale.order.line', "Sale Order Item", compute='_compute_sale_line_id', store=True, readonly=False)
+
+ _sql_constraints = [
+ ('unique_employee_per_wizard', 'UNIQUE(wizard_id, employee_id)', "An employee cannot be selected more than once in the mapping. Please remove duplicate(s) and try again."),
+ ]
+
+ @api.onchange('product_id', 'sale_line_id')
+ def _onchange_product_id(self):
+ if self.wizard_id.link_selection == 'link':
+ self.price_unit = self.sale_line_id.price_unit
+ self.currency_id = self.sale_line_id.currency_id
+ else:
+ self.price_unit = self.product_id.lst_price or 0
+ self.currency_id = self.product_id.currency_id
+
+ @api.depends('wizard_id.sale_order_id')
+ def _compute_sale_line_id(self):
+ for line in self:
+ if line.sale_line_id and line.sale_line_id.order_id != line.wizard_id.sale_order_id:
+ line.sale_line_id = False
diff --git a/addons/sale_timesheet/wizard/project_create_sale_order_views.xml b/addons/sale_timesheet/wizard/project_create_sale_order_views.xml
new file mode 100644
index 00000000..4ca4db51
--- /dev/null
+++ b/addons/sale_timesheet/wizard/project_create_sale_order_views.xml
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<odoo>
+
+ <record id="project_create_sale_order_view_form" model="ir.ui.view">
+ <field name="name">project.create.sale.order.wizard.form</field>
+ <field name="model">project.create.sale.order</field>
+ <field name="arch" type="xml">
+ <form string="Create a Sales Order">
+ <field name="link_selection" nolabel="1" widget="radio" options="{'horizontal': true}" invisible="1"/>
+ <group>
+ <group>
+ <field name="project_id" readonly="1"/>
+ <field name="company_id" invisible="1"/>
+ <field name="partner_id" domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]"/>
+ <field name="pricing_type" invisible="1"/>
+ </group>
+ <group attrs="{'invisible': [('link_selection', '!=', 'link')]}">
+ <field name="sale_order_id" options="{'no_create': True, 'no_create_edit': True}" attrs="{'required': [('link_selection', '=', 'link')]}"/>
+ <field name="commercial_partner_id" invisible="1"/>
+ </group>
+ </group>
+ <group attrs="{'invisible': ['&amp;', ('link_selection', '=', 'link'), ('pricing_type', '!=', 'employee_rate')]}">
+ <field name="line_ids" nolabel="1" attrs="{'required': [('pricing_type', '=', 'employee_rate')]}">
+ <tree editable="bottom">
+ <field name="employee_id" options="{'no_create_edit': True, 'no_create': True}" attrs="{'column_invisible': [('parent.pricing_type', '=', 'fixed_rate')], 'required': [('parent.pricing_type', '=', 'employee_rate')]}"/>
+ <field name="sale_line_id" options="{'no_create_edit': True, 'no_create': True, 'no_open': True}" domain="[('is_service', '=', True), ('order_id', '=', parent.sale_order_id)]" attrs="{'column_invisible': ['|', ('parent.link_selection', '!=', 'link'), ('parent.pricing_type', '=', 'fixed_rate')], 'required': ['&amp;', ('parent.link_selection', '=', 'link'), ('parent.pricing_type', '=', 'employee_rate')]}"/>
+ <field name="product_id" options="{'no_create_edit': True, 'no_create': True}" attrs="{'column_invisible': [('parent.link_selection', '=', 'link')], 'required': [('parent.link_selection', '!=', 'link')]}"/>
+ <field name="price_unit" widget='monetary' options="{'currency_field': 'currency_id', 'field_digits': True}" attrs="{'readonly': [('parent.link_selection', '=', 'link')]}"/>
+ <field name="currency_id" invisible="1"/>
+ </tree>
+ </field>
+ </group>
+ <group attrs="{'invisible': [('info_invoice', '=', False)]}">
+ <field name="info_invoice" nolabel="1"/>
+ </group>
+ <footer>
+ <button string="Link to Sales Order" type="object" name="action_link_sale_order" class="oe_highlight" attrs="{'invisible': [('link_selection', '!=', 'link')]}"/>
+ <button string="Create Sales Order" type="object" name="action_create_sale_order" class="oe_highlight" attrs="{'invisible': [('link_selection', '=', 'link')]}"/>
+ <button string="Cancel" special="cancel" type="object" class="btn btn-secondary oe_inline"/>
+ </footer>
+ </form>
+ </field>
+ </record>
+
+ <record id="project_project_action_multi_create_sale_order" model="ir.actions.act_window">
+ <field name="name">Create a Sales Order</field>
+ <field name="res_model">project.create.sale.order</field>
+ <field name="view_mode">form</field>
+ <field name="view_id" ref="project_create_sale_order_view_form"/>
+ <field name="target">new</field>
+ </record>
+
+</odoo>
diff --git a/addons/sale_timesheet/wizard/project_task_create_sale_order.py b/addons/sale_timesheet/wizard/project_task_create_sale_order.py
new file mode 100644
index 00000000..02dbda23
--- /dev/null
+++ b/addons/sale_timesheet/wizard/project_task_create_sale_order.py
@@ -0,0 +1,145 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, fields, models, _
+from odoo.exceptions import UserError
+
+
+class ProjectTaskCreateSalesOrder(models.TransientModel):
+ _name = 'project.task.create.sale.order'
+ _description = "Create SO from task"
+
+ @api.model
+ def default_get(self, fields):
+ result = super(ProjectTaskCreateSalesOrder, self).default_get(fields)
+
+ active_model = self._context.get('active_model')
+ if active_model != 'project.task':
+ raise UserError(_("You can only apply this action from a task."))
+
+ active_id = self._context.get('active_id')
+ if 'task_id' in fields and active_id:
+ task = self.env['project.task'].browse(active_id)
+ if task.sale_order_id:
+ raise UserError(_("The task has already a sale order."))
+ result['task_id'] = active_id
+ if not result.get('partner_id', False):
+ result['partner_id'] = task.partner_id.id
+ return result
+
+ link_selection = fields.Selection([('create', 'Create a new sales order'), ('link', 'Link to an existing sales order')], required=True, default='create')
+
+ task_id = fields.Many2one('project.task', "Task", domain=[('sale_line_id', '=', False)], help="Task for which we are creating a sales order", required=True)
+ partner_id = fields.Many2one('res.partner', string="Customer", help="Customer of the sales order", required=True)
+ product_id = fields.Many2one('product.product', domain=[('type', '=', 'service'), ('invoice_policy', '=', 'delivery'), ('service_type', '=', 'timesheet')], string="Service", help="Product of the sales order item. Must be a service invoiced based on timesheets on tasks. The existing timesheet will be linked to this product.", required=True)
+ price_unit = fields.Float("Unit Price", help="Unit price of the sales order item.")
+ currency_id = fields.Many2one('res.currency', string="Currency", related='product_id.currency_id', readonly=False)
+ commercial_partner_id = fields.Many2one(related='partner_id.commercial_partner_id')
+ sale_order_id = fields.Many2one(
+ 'sale.order', string="Sales Order",
+ domain="['|', '|', ('partner_id', '=', partner_id), ('partner_id', 'child_of', commercial_partner_id), ('partner_id', 'parent_of', partner_id)]")
+ sale_line_id = fields.Many2one(
+ 'sale.order.line', 'Sales Order Item',
+ domain="[('is_service', '=', True), ('order_partner_id', 'child_of', commercial_partner_id), ('is_expense', '=', False), ('state', 'in', ['sale', 'done']), ('order_id', '=?', sale_order_id)]")
+
+ info_invoice = fields.Char(compute='_compute_info_invoice')
+
+ @api.depends('sale_line_id', 'price_unit', 'link_selection')
+ def _compute_info_invoice(self):
+ for line in self:
+ domain = self.env['sale.order.line']._timesheet_compute_delivered_quantity_domain()
+ timesheet = self.env['account.analytic.line'].read_group(domain + [('task_id', '=', self.task_id.id), ('so_line', '=', False), ('timesheet_invoice_id', '=', False)], ['unit_amount'], ['task_id'])
+ unit_amount = round(timesheet[0].get('unit_amount', 0), 2) if timesheet else 0
+ if not unit_amount:
+ line.info_invoice = False
+ continue
+ company_uom = self.env.company.timesheet_encode_uom_id
+ label = _("hours")
+ if company_uom == self.env.ref('uom.product_uom_day'):
+ label = _("days")
+ if line.link_selection == 'create' and line.price_unit:
+ line.info_invoice = _("%(amount)s %(label)s will be added to the new Sales Order.", amount=unit_amount, label=label)
+ else:
+ line.info_invoice = _("%(amount)s %(label)s will be added to the selected Sales Order.", amount=unit_amount, label=label)
+
+ @api.onchange('product_id')
+ def _onchange_product_id(self):
+ if self.product_id:
+ self.price_unit = self.product_id.lst_price
+ else:
+ self.price_unit = 0.0
+
+ @api.onchange('partner_id')
+ def _onchange_partner_id(self):
+ self.sale_order_id = False
+ self.sale_line_id = False
+
+ @api.onchange('sale_order_id')
+ def _onchange_sale_order_id(self):
+ self.sale_line_id = False
+
+ def action_link_sale_order(self):
+ # link task to SOL
+ self.task_id.write({
+ 'sale_line_id': self.sale_line_id.id,
+ 'partner_id': self.partner_id.id,
+ 'email_from': self.partner_id.email,
+ })
+
+ # assign SOL to timesheets
+ self.env['account.analytic.line'].search([('task_id', '=', self.task_id.id), ('so_line', '=', False), ('timesheet_invoice_id', '=', False)]).write({
+ 'so_line': self.sale_line_id.id
+ })
+
+ def action_create_sale_order(self):
+ sale_order = self._prepare_sale_order()
+ sale_order.action_confirm()
+ view_form_id = self.env.ref('sale.view_order_form').id
+ action = self.env["ir.actions.actions"]._for_xml_id("sale.action_orders")
+ action.update({
+ 'views': [(view_form_id, 'form')],
+ 'view_mode': 'form',
+ 'name': sale_order.name,
+ 'res_id': sale_order.id,
+ })
+ return action
+
+ def _prepare_sale_order(self):
+ # if task linked to SO line, then we consider it as billable.
+ if self.task_id.sale_line_id:
+ raise UserError(_("The task is already linked to a sales order item."))
+
+ # create SO
+ sale_order = self.env['sale.order'].create({
+ 'partner_id': self.partner_id.id,
+ 'company_id': self.task_id.company_id.id,
+ 'analytic_account_id': self.task_id.project_id.analytic_account_id.id,
+ })
+ sale_order.onchange_partner_id()
+ sale_order.onchange_partner_shipping_id()
+ # rewrite the user as the onchange_partner_id erases it
+ sale_order.write({'user_id': self.task_id.user_id.id})
+ sale_order.onchange_user_id()
+
+ sale_order_line = self.env['sale.order.line'].create({
+ 'order_id': sale_order.id,
+ 'product_id': self.product_id.id,
+ 'price_unit': self.price_unit,
+ 'project_id': self.task_id.project_id.id, # prevent to re-create a project on confirmation
+ 'task_id': self.task_id.id,
+ 'product_uom_qty': round(sum(self.task_id.timesheet_ids.filtered(lambda t: not t.non_allow_billable and not t.so_line).mapped('unit_amount')), 2),
+ })
+
+ # link task to SOL
+ self.task_id.write({
+ 'sale_line_id': sale_order_line.id,
+ 'partner_id': sale_order.partner_id.id,
+ 'email_from': sale_order.partner_id.email,
+ })
+
+ # assign SOL to timesheets
+ self.env['account.analytic.line'].search([('task_id', '=', self.task_id.id), ('so_line', '=', False), ('timesheet_invoice_id', '=', False)]).write({
+ 'so_line': sale_order_line.id
+ })
+
+ return sale_order
diff --git a/addons/sale_timesheet/wizard/project_task_create_sale_order_views.xml b/addons/sale_timesheet/wizard/project_task_create_sale_order_views.xml
new file mode 100644
index 00000000..2521b53b
--- /dev/null
+++ b/addons/sale_timesheet/wizard/project_task_create_sale_order_views.xml
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<odoo>
+
+ <record id="project_task_create_sale_order_view_form" model="ir.ui.view">
+ <field name="name">project.task.create.sale.order.wizard.form</field>
+ <field name="model">project.task.create.sale.order</field>
+ <field name="arch" type="xml">
+ <form string="Create a Sales Order">
+ <field name="link_selection" nolabel="1" widget="radio" options="{'horizontal': true}" invisible="1"/>
+ <group>
+ <group>
+ <field name="task_id" readonly="1"/>
+ <field name="partner_id"/>
+ </group>
+ <group attrs="{'invisible': [('link_selection', '=', 'link')]}">
+ <field name="product_id" context="{'default_type': 'service', 'default_service_policy': 'delivered_timesheet'}" attrs="{'required': [('link_selection', '!=', 'link')]}"/>
+ <field name="price_unit" widget='monetary' options="{'currency_field': 'currency_id', 'field_digits': True}" attrs="{'required': [('link_selection', '!=', 'link')]}"/>
+ <field name="currency_id" invisible="1"/>
+ </group>
+ <group attrs="{'invisible': [('link_selection', '!=', 'link')]}">
+ <field name="sale_order_id" options="{'no_create': True, 'no_create_edit': True}" attrs="{'required': [('link_selection', '=', 'link')]}"/>
+ <field name="sale_line_id" options="{'no_create': True, 'no_create_edit': True}" attrs="{'required': [('link_selection', '=', 'link')]}"/>
+ <field name="commercial_partner_id" invisible="1"/>
+ </group>
+ </group>
+ <group attrs="{'invisible': [('info_invoice', '=', False)]}">
+ <field name="info_invoice" nolabel="1"/>
+ </group>
+ <footer>
+ <button string="Link to Sales Order" type="object" name="action_link_sale_order" class="oe_highlight" attrs="{'invisible': [('link_selection', '!=', 'link')]}"/>
+ <button string="Create Sales Order" type="object" name="action_create_sale_order" class="oe_highlight" attrs="{'invisible': [('link_selection', '=', 'link')]}"/>
+ <button string="Cancel" special="cancel" type="object" class="btn btn-secondary oe_inline"/>
+ </footer>
+ </form>
+ </field>
+ </record>
+
+ <record id="project_task_action_multi_create_sale_order" model="ir.actions.act_window">
+ <field name="name">Create a Sales Order</field>
+ <field name="res_model">project.task.create.sale.order</field>
+ <field name="view_mode">form</field>
+ <field name="view_id" ref="project_task_create_sale_order_view_form"/>
+ <field name="target">new</field>
+ </record>
+
+</odoo>
diff --git a/addons/sale_timesheet/wizard/sale_make_invoice_advance.py b/addons/sale_timesheet/wizard/sale_make_invoice_advance.py
new file mode 100644
index 00000000..24faf0b3
--- /dev/null
+++ b/addons/sale_timesheet/wizard/sale_make_invoice_advance.py
@@ -0,0 +1,49 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, fields, models
+
+
+class SaleAdvancePaymentInv(models.TransientModel):
+ _inherit = "sale.advance.payment.inv"
+
+ @api.model
+ def _default_invoicing_timesheet_enabled(self):
+ if 'active_id' not in self._context and 'active_ids' not in self._context:
+ return False
+ sale_orders = self.env['sale.order'].browse(self._context.get('active_id') or self._context.get('active_ids'))
+ order_lines = sale_orders.mapped('order_line').filtered(lambda sol: sol.invoice_status == 'to invoice')
+ product_ids = order_lines.mapped('product_id').filtered(lambda p: p._is_delivered_timesheet())
+ return bool(product_ids)
+
+ date_start_invoice_timesheet = fields.Date(
+ string='Start Date',
+ help="Only timesheets not yet invoiced (and validated, if applicable) from this period will be invoiced. If the period is not indicated, all timesheets not yet invoiced (and validated, if applicable) will be invoiced without distinction.")
+ date_end_invoice_timesheet = fields.Date(
+ string='End Date',
+ help="Only timesheets not yet invoiced (and validated, if applicable) from this period will be invoiced. If the period is not indicated, all timesheets not yet invoiced (and validated, if applicable) will be invoiced without distinction.")
+ invoicing_timesheet_enabled = fields.Boolean(default=_default_invoicing_timesheet_enabled)
+
+ def create_invoices(self):
+ """ Override method from sale/wizard/sale_make_invoice_advance.py
+
+ When the user want to invoice the timesheets to the SO
+ up to a specific period then we need to recompute the
+ qty_to_invoice for each product_id in sale.order.line,
+ before creating the invoice.
+ """
+ sale_orders = self.env['sale.order'].browse(
+ self._context.get('active_ids', [])
+ )
+
+ if self.advance_payment_method == 'delivered' and self.invoicing_timesheet_enabled:
+ if self.date_start_invoice_timesheet or self.date_end_invoice_timesheet:
+ sale_orders.mapped('order_line')._recompute_qty_to_invoice(self.date_start_invoice_timesheet, self.date_end_invoice_timesheet)
+
+ sale_orders._create_invoices(final=self.deduct_down_payments, start_date=self.date_start_invoice_timesheet, end_date=self.date_end_invoice_timesheet)
+
+ if self._context.get('open_invoices', False):
+ return sale_orders.action_view_invoice()
+ return {'type': 'ir.actions.act_window_close'}
+
+ return super(SaleAdvancePaymentInv, self).create_invoices()
diff --git a/addons/sale_timesheet/wizard/sale_make_invoice_advance_views.xml b/addons/sale_timesheet/wizard/sale_make_invoice_advance_views.xml
new file mode 100644
index 00000000..0090accb
--- /dev/null
+++ b/addons/sale_timesheet/wizard/sale_make_invoice_advance_views.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<odoo>
+ <record id="sale_advance_payment_inv_timesheet_view_form" model="ir.ui.view">
+ <field name="name">sale_timesheet.sale.advance.payment.inv.view.form</field>
+ <field name="model">sale.advance.payment.inv</field>
+ <field name="inherit_id" ref="sale.view_sale_advance_payment_inv"/>
+ <field name="arch" type="xml">
+ <xpath expr="//form" position="attributes">
+ <attribute name="disable_autofocus">true</attribute>
+ </xpath>
+ <xpath expr="//field[@name='deposit_taxes_id']" position="after">
+ <field name="invoicing_timesheet_enabled" invisible="1"/>
+ <label for="date_start_invoice_timesheet" string="Timesheets Period" attrs="{'invisible': [ '|', ('invoicing_timesheet_enabled', '=', False), ('advance_payment_method', '!=', 'delivered')]}"/>
+ <div class="o_row" attrs="{'invisible': [ '|',('invoicing_timesheet_enabled', '=', False), ('advance_payment_method', '!=', 'delivered')]}">
+ <field name="date_start_invoice_timesheet"
+ class="oe_inline" widget="daterange"
+ options="{'related_end_date': 'date_end_invoice_timesheet'}"
+ title="Only timesheets not yet invoiced (and validated, if applicable) from this period will be invoiced. If the period is not indicated, all timesheets not yet invoiced (and validated, if applicable) will be invoiced without distinction."/>
+ <i class="fa fa-long-arrow-right mx-2" aria-label="Arrow icon" title="Arrow"/>
+ <field name="date_end_invoice_timesheet"
+ class="oe_inline" widget="daterange"
+ options="{'related_start_date': 'date_start_invoice_timesheet'}"
+ title="Only timesheets not yet invoiced (and validated, if applicable) from this period will be invoiced. If the period is not indicated, all timesheets not yet invoiced (and validated, if applicable) will be invoiced without distinction."/>
+ </div>
+ </xpath>
+ </field>
+ </record>
+</odoo>