| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
from odoo import fields, models, tools |
| 4 |
from odoo.fields import Domain |
| 5 |
from odoo.tools import SQL |
| 6 |
|
| 7 |
|
| 8 |
class VendorDelayReport(models.Model): |
| 9 |
_name = 'vendor.delay.report' |
| 10 |
_description = "Vendor Delay Report" |
| 11 |
_auto = False |
| 12 |
|
| 13 |
partner_id = fields.Many2one('res.partner', 'Vendor', readonly=True) |
| 14 |
product_id = fields.Many2one('product.product', 'Product', readonly=True) |
| 15 |
category_id = fields.Many2one('product.category', 'Product Category', readonly=True) |
| 16 |
date = fields.Datetime('Effective Date', readonly=True) |
| 17 |
qty_total = fields.Float('Total Quantity', readonly=True) |
| 18 |
qty_on_time = fields.Float('On-Time Quantity', readonly=True) |
| 19 |
on_time_rate = fields.Float('On-Time Delivery Rate', readonly=True) |
| 20 |
|
| 21 |
def init(self): |
| 22 |
tools.drop_view_if_exists(self.env.cr, 'vendor_delay_report') |
| 23 |
self.env.cr.execute(""" |
| 24 |
CREATE OR replace VIEW vendor_delay_report AS( |
| 25 |
SELECT pol.id AS id, |
| 26 |
Min(m.date) AS date, |
| 27 |
pol.id AS purchase_line_id, |
| 28 |
pol.product_id AS product_id, |
| 29 |
Min(pc.id) AS category_id, |
| 30 |
pol.partner_id AS partner_id, |
| 31 |
pol.product_uom_qty AS qty_total, |
| 32 |
Sum(CASE |
| 33 |
WHEN (m.state = 'done' and pol.date_planned::date >= m.date::date) THEN ((ml.quantity * ml_uom.factor) / pt_uom.factor) |
| 34 |
ELSE 0 |
| 35 |
END) AS qty_on_time |
| 36 |
FROM stock_move m |
| 37 |
JOIN purchase_order_line pol |
| 38 |
ON pol.id = m.purchase_line_id |
| 39 |
JOIN product_product p |
| 40 |
ON p.id = m.product_id |
| 41 |
JOIN product_template pt |
| 42 |
ON pt.id = p.product_tmpl_id |
| 43 |
JOIN uom_uom pt_uom |
| 44 |
ON pt_uom.id = pt.uom_id |
| 45 |
LEFT JOIN product_category pc |
| 46 |
ON pc.id = pt.categ_id |
| 47 |
LEFT JOIN stock_move_line ml |
| 48 |
ON ml.move_id = m.id |
| 49 |
LEFT JOIN uom_uom ml_uom |
| 50 |
ON ml_uom.id = ml.product_uom_id |
| 51 |
GROUP BY pol.id |
| 52 |
)""") |
| 53 |
|
| 54 |
def _read_group_select(self, aggregate_spec, query): |
| 55 |
if aggregate_spec == 'on_time_rate:sum': |
| 56 |
# Make a weigthed average instead of simple average for these fields |
| 57 |
return SQL( |
| 58 |
'CASE WHEN SUM(%s) !=0 THEN SUM(%s) / SUM(%s) * 100 ELSE 100 END', |
| 59 |
self._field_to_sql(self._table, 'qty_total', query), |
| 60 |
self._field_to_sql(self._table, 'qty_on_time', query), |
| 61 |
self._field_to_sql(self._table, 'qty_total', query), |
| 62 |
) |
| 63 |
return super()._read_group_select(aggregate_spec, query) |
| 64 |
|
| 65 |
def _read_group(self, domain, groupby=(), aggregates=(), having=(), offset=0, limit=None, order=None): |
| 66 |
if 'on_time_rate:sum' in aggregates: |
| 67 |
having = Domain.AND([having, [('qty_total:sum', '>', '0')]]) |
| 68 |
return super()._read_group(domain, groupby, aggregates, having, offset, limit, order) |