187 lines
8.8 KiB
Python
187 lines
8.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
from collections import defaultdict
|
|
|
|
from odoo import api, models, fields, _
|
|
from odoo.tools import float_compare, float_is_zero
|
|
|
|
|
|
class SaleOrder(models.Model):
|
|
_inherit = 'sale.order'
|
|
|
|
task_id = fields.Many2one('project.task', string="Source Task", help="Task from which this quotation have been created")
|
|
|
|
@api.model_create_multi
|
|
def create(self, vals_list):
|
|
orders = super().create(vals_list)
|
|
for sale_order in orders:
|
|
if sale_order.task_id:
|
|
message = _("Extra Quotation Created: %s", sale_order._get_html_link())
|
|
sale_order.task_id.message_post(body=message)
|
|
return orders
|
|
|
|
def message_post(self, **kwargs):
|
|
if self.env.context.get('fsm_no_message_post'):
|
|
return self.env['mail.message']
|
|
return super().message_post(**kwargs)
|
|
|
|
def action_confirm(self):
|
|
res = super().action_confirm()
|
|
for sale_order in self:
|
|
if sale_order.task_id:
|
|
sale_order.message_post_with_source(
|
|
'mail.message_origin_link',
|
|
render_values={'self': sale_order, 'origin': sale_order.task_id},
|
|
subtype_xmlid='mail.mt_note',
|
|
)
|
|
return res
|
|
|
|
def _get_product_catalog_record_lines(self, product_ids, *, section_id=None, **kwarg):
|
|
"""
|
|
Accessing the catalog from the smart button of a "field service" should compute
|
|
the content of the catalog related to that field service rather than the content
|
|
of the catalog related to the sale order containing that "field service".
|
|
"""
|
|
task_id = self.env.context.get('fsm_task_id')
|
|
if task_id:
|
|
grouped_lines = defaultdict(lambda: self.env['sale.order.line'])
|
|
if section_id is None:
|
|
section_id = (
|
|
self.order_line[:1].id
|
|
if self.order_line[:1].display_type == 'line_section'
|
|
else False
|
|
)
|
|
order_lines = self.order_line.filtered(
|
|
lambda line: line.get_parent_section_line().id == section_id,
|
|
)
|
|
for line in order_lines:
|
|
if line.task_id.id == task_id and line.product_id.id in product_ids:
|
|
grouped_lines[line.product_id] |= line
|
|
return grouped_lines
|
|
return super()._get_product_catalog_record_lines(
|
|
product_ids, section_id=section_id, **kwarg
|
|
)
|
|
|
|
def action_add_from_catalog(self):
|
|
if self.env.user.has_group('project.group_project_user') and len(self.tasks_ids) == 1 and self.tasks_ids.allow_material:
|
|
return self.tasks_ids.action_fsm_view_material()
|
|
return super().action_add_from_catalog()
|
|
|
|
def _compute_invoice_status(self):
|
|
super()._compute_invoice_status()
|
|
confirmed_orders_with_task_not_invoiced = self.filtered(
|
|
lambda so:
|
|
so.state == 'sale'
|
|
and any(sol.task_id.sudo().allow_material for sol in so.order_line)
|
|
and so.invoice_status not in ('invoiced', 'upselling')
|
|
and not so.company_id.anglo_saxon_accounting
|
|
)
|
|
if not confirmed_orders_with_task_not_invoiced:
|
|
return
|
|
all_material_sale_lines_per_task = confirmed_orders_with_task_not_invoiced.order_line.task_id.sudo()._get_material_sale_lines_by_task()
|
|
fully_invoiced_orders = confirmed_orders_with_task_not_invoiced.filtered(
|
|
lambda so:
|
|
all(
|
|
(
|
|
sol.invoice_status == 'invoiced'
|
|
or (
|
|
sol.invoice_status == 'no'
|
|
and sol.task_id.sudo().allow_material
|
|
and sol in all_material_sale_lines_per_task.get(sol.task_id, self.env['sale.order.line'])
|
|
and float_is_zero(sol.price_unit, precision_rounding=sol.currency_id.rounding)
|
|
)
|
|
) for sol in so.order_line
|
|
)
|
|
)
|
|
fully_invoiced_orders.invoice_status = 'invoiced'
|
|
|
|
|
|
class SaleOrderLine(models.Model):
|
|
_inherit = 'sale.order.line'
|
|
|
|
delivered_price_subtotal = fields.Monetary(compute='_compute_delivered_amount', string='Delivered Subtotal', export_string_translation=False)
|
|
delivered_price_tax = fields.Float(compute='_compute_delivered_amount', string='Delivered Total Tax', export_string_translation=False)
|
|
delivered_price_total = fields.Monetary(compute='_compute_delivered_amount', string='Delivered Total', export_string_translation=False)
|
|
|
|
@api.depends('qty_delivered', 'discount', 'price_unit', 'tax_ids')
|
|
def _compute_delivered_amount(self):
|
|
"""
|
|
Compute the amounts of the SO line for delivered quantity.
|
|
"""
|
|
for line in self:
|
|
price = line.price_unit * (1 - (line.discount or 0.0) / 100.0)
|
|
taxes = line.tax_ids.compute_all(price, line.order_id.currency_id, line.qty_delivered, product=line.product_id, partner=line.order_id.partner_shipping_id)
|
|
line.delivered_price_tax = sum(t.get('amount', 0.0) for t in taxes.get('taxes', []))
|
|
line.delivered_price_total = taxes['total_included']
|
|
line.delivered_price_subtotal = taxes['total_excluded']
|
|
|
|
def _get_sale_order_partner_id(self, project):
|
|
if project.is_fsm:
|
|
return self.order_id.partner_shipping_id.id
|
|
return super()._get_sale_order_partner_id(project)
|
|
|
|
def _timesheet_create_task_prepare_values(self, project):
|
|
res = super()._timesheet_create_task_prepare_values(project)
|
|
if project.is_fsm:
|
|
res.update({'partner_id': self.order_id.partner_shipping_id.id})
|
|
return res
|
|
|
|
def _timesheet_create_project_prepare_values(self):
|
|
"""Generate project values"""
|
|
values = super()._timesheet_create_project_prepare_values()
|
|
if self.product_id.project_template_id.is_fsm:
|
|
values.update({'partner_id': self.order_id.partner_shipping_id.id})
|
|
values.pop('sale_line_id', False)
|
|
return values
|
|
|
|
def _compute_invoice_status(self):
|
|
sol_from_task_without_amount = self.filtered(
|
|
lambda sol:
|
|
sol.task_id.allow_material
|
|
and float_is_zero(sol.price_unit, precision_rounding=sol.currency_id.rounding)
|
|
and sol.invoice_status not in ('invoiced', 'upselling')
|
|
)
|
|
if sol_from_task_without_amount:
|
|
precision = self.env['decimal.precision'].precision_get('Product Unit of Measure')
|
|
sol_from_task_invoiced = sol_from_task_without_amount.filtered(
|
|
lambda sol: float_compare(sol.qty_invoiced, sol.product_uom_qty, precision_digits=precision) >= 0
|
|
)
|
|
sol_from_task_to_invoice = (sol_from_task_without_amount - sol_from_task_invoiced)
|
|
|
|
sol_from_task_without_anglo = sol_from_task_to_invoice.filtered(
|
|
lambda sol: not sol.company_id.anglo_saxon_accounting
|
|
)
|
|
sol_from_task_included_materials = self.env['sale.order.line']
|
|
|
|
if sol_from_task_without_anglo:
|
|
material_sale_lines_per_task = sol_from_task_without_anglo.task_id.sudo()._get_material_sale_lines_by_task()
|
|
sol_from_task_included_materials = sol_from_task_without_anglo.filtered(
|
|
lambda sol: sol in material_sale_lines_per_task.get(sol.task_id, self.env['sale.order.line'])
|
|
)
|
|
sol_from_task_to_invoice -= sol_from_task_included_materials
|
|
|
|
sol_from_task_invoiced.invoice_status = 'invoiced'
|
|
sol_from_task_to_invoice.invoice_status = 'to invoice'
|
|
sol_from_task_included_materials.invoice_status = 'no'
|
|
super(SaleOrderLine, self - sol_from_task_without_amount)._compute_invoice_status()
|
|
|
|
@api.depends('price_unit')
|
|
def _compute_qty_to_invoice(self):
|
|
sol_from_task_without_amount = self.filtered(
|
|
lambda sol:
|
|
sol.task_id.is_fsm
|
|
and float_is_zero(sol.price_unit, precision_rounding=sol.currency_id.rounding)
|
|
)
|
|
sol_from_task_with_anglo_or_prepaid_service = sol_from_task_without_amount.filtered(
|
|
lambda sol:
|
|
sol.company_id.anglo_saxon_accounting
|
|
or (sol.product_id.type == 'service' and sol.product_id.service_policy == 'ordered_prepaid')
|
|
)
|
|
sol_without_anglo_or_prepaid_service = sol_from_task_without_amount - sol_from_task_with_anglo_or_prepaid_service
|
|
sol_without_anglo_or_prepaid_service.qty_to_invoice = 0.0
|
|
super(SaleOrderLine, self - sol_without_anglo_or_prepaid_service)._compute_qty_to_invoice()
|
|
|
|
def action_add_from_catalog(self):
|
|
# TODO: remove me in master
|
|
return super().action_add_from_catalog()
|