| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 3 |
|
| 4 |
from odoo import fields, models |
| 5 |
from odoo.tools import SQL |
| 6 |
|
| 7 |
|
| 8 |
class PurchaseReport(models.Model): |
| 9 |
_inherit = "purchase.report" |
| 10 |
|
| 11 |
picking_type_id = fields.Many2one('stock.warehouse', 'Warehouse', readonly=True) |
| 12 |
effective_date = fields.Datetime(string="Effective Date") |
| 13 |
days_to_arrival = fields.Float('Effective Days To Arrival', digits=(16, 2), readonly=True, aggregator='avg') |
| 14 |
|
| 15 |
def _select(self) -> SQL: |
| 16 |
return SQL( |
| 17 |
""" |
| 18 |
%s, |
| 19 |
spt.warehouse_id as picking_type_id, po.effective_date as effective_date, |
| 20 |
extract( |
| 21 |
epoch from age( |
| 22 |
l.date_planned, |
| 23 |
COALESCE( |
| 24 |
order_effective_date.date_done, |
| 25 |
po.date_order |
| 26 |
) |
| 27 |
) |
| 28 |
)/(24*60*60)::decimal(16,2) as days_to_arrival |
| 29 |
""", super()._select() |
| 30 |
) |
| 31 |
|
| 32 |
def _from(self) -> SQL: |
| 33 |
return SQL( |
| 34 |
""" |
| 35 |
%s |
| 36 |
LEFT JOIN stock_picking_type spt ON (spt.id=po.picking_type_id) |
| 37 |
LEFT JOIN ( |
| 38 |
SELECT MIN(picking.date_done) AS date_done, |
| 39 |
purchase.id AS purchase_id |
| 40 |
FROM purchase_order AS purchase |
| 41 |
JOIN purchase_order_line AS order_line |
| 42 |
ON order_line.order_id = purchase.id |
| 43 |
JOIN stock_move AS move |
| 44 |
ON move.purchase_line_id = order_line.id |
| 45 |
JOIN stock_picking AS picking |
| 46 |
ON picking.id = move.picking_id |
| 47 |
JOIN stock_location AS location_dest |
| 48 |
ON location_dest.id = picking.location_dest_id |
| 49 |
WHERE picking.state = 'done' |
| 50 |
AND location_dest.usage != 'supplier' |
| 51 |
AND picking.date_done IS NOT NULL |
| 52 |
GROUP BY |
| 53 |
purchase.id |
| 54 |
) order_effective_date |
| 55 |
ON order_effective_date.purchase_id = l.order_id |
| 56 |
""", super()._from() |
| 57 |
) |
| 58 |
|
| 59 |
def _group_by(self) -> SQL: |
| 60 |
return SQL("%s, spt.warehouse_id, effective_date, order_effective_date.date_done", super()._group_by()) |