'How to add as pivot the "date_done" field of "stock.picking in sale.report. odoo

This is what I have tried:

class SaleReport(models.Model):
    _inherit = 'sale.report'

    delivery_amount = fields.Float(
        string="delivery_amount")
    picking_ids = fields.Many2one(
        'stock.picking', 'picking_ids', readonly=True)

    def _query(self, with_clause='', fields={}, groupby='', from_clause=''):
        fields['delivery_amount'] = ", l.delivery_amount as delivery_amount"
        groupby += ', l.delivery_amount '
        fields['picking_ids'] = ", l.picking_ids as picking_ids"
        groupby += ', l.picking_ids '
        return super(SaleReport, self)._query(with_clause, fields, groupby, from_clause)


This is the error.

            CASE WHEN l.product_id IS NOT NULL THEN sum(l.qty_delivered / u.factor * u2.factor) ELSE 0 END as qty_delivered,
            CASE WHEN l.product_id IS NOT NULL THEN sum(l.qty_invoiced / u.factor * u2.factor) ELSE 0 END as qty_invoiced,
            CASE WHEN l.product_id IS NOT NULL THEN sum(l.qty_to_invoice / u.factor * u2.factor) ELSE 0 END as qty_to_invoice,
            CASE WHEN l.product_id IS NOT NULL THEN sum(l.price_total / CASE COALESCE(s.currency_rate, 0) WHEN 0 THEN 1.0 ELSE s.currency_rate END) ELSE 0 END as price_total,
            CASE WHEN l.product_id IS NOT NULL THEN sum(l.price_subtotal / CASE COALESCE(s.currency_rate, 0) WHEN 0 THEN 1.0 ELSE s.currency_rate END) ELSE 0 END as price_subtotal,
            CASE WHEN l.product_id IS NOT NULL THEN sum(l.untaxed_amount_to_invoice / CASE COALESCE(s.currency_rate, 0) WHEN 0 THEN 1.0 ELSE s.currency_rate END) ELSE 0 END as untaxed_amount_to_invoice,
            CASE WHEN l.product_id IS NOT NULL THEN sum(l.untaxed_amount_invoiced / CASE COALESCE(s.currency_rate, 0) WHEN 0 THEN 1.0 ELSE s.currency_rate END) ELSE 0 END as untaxed_amount_invoiced,
            count(*) as nbr,
            s.name as name,
            s.date_order as date,
            s.state as state,
            s.partner_id as partner_id,
            s.user_id as user_id,
            s.company_id as company_id,
            s.campaign_id as campaign_id,
            s.medium_id as medium_id,
            s.source_id as source_id,
            extract(epoch from avg(date_trunc('day',s.date_order)-date_trunc('day',s.create_date)))/(24*60*60)::decimal(16,2) as delay,
            t.categ_id as categ_id,
            s.pricelist_id as pricelist_id,
            s.analytic_account_id as analytic_account_id,
            s.team_id as team_id,
            p.product_tmpl_id,
            partner.country_id as country_id,
            partner.industry_id as industry_id,
            partner.commercial_partner_id as commercial_partner_id,
            CASE WHEN l.product_id IS NOT NULL THEN sum(p.weight * l.product_uom_qty / u.factor * u2.factor) ELSE 0 END as weight,
            CASE WHEN l.product_id IS NOT NULL THEN sum(p.volume * l.product_uom_qty / u.factor * u2.factor) ELSE 0 END as volume,
            l.discount as discount,
            CASE WHEN l.product_id IS NOT NULL THEN sum((l.price_unit * l.product_uom_qty * l.discount / 100.0 / CASE COALESCE(s.currency_rate, 0) WHEN 0 THEN 1.0 ELSE s.currency_rate END))ELSE 0 END as discount_amount,
            s.id as order_id
        , l.delivery_amount as delivery_amount, l.picking_ids as picking_ids, s.warehouse_id as warehouse_id, s.invoice_status as invoice_status FROM
                sale_order_line l
                      right outer join sale_order s on (s.id=l.order_id)
                      join res_partner partner on s.partner_id = partner.id
                        left join product_product p on (l.product_id=p.id)
                            left join product_template t on (p.product_tmpl_id=t.id)
                    left join uom_uom u on (u.id=l.product_uom)
                    left join uom_uom u2 on (u2.id=t.uom_id)
                    left join product_pricelist pp on (s.pricelist_id = pp.id)

         WHERE l.display_type IS NULL GROUP BY
            l.product_id,
            l.order_id,
            t.uom_id,
            t.categ_id,
            s.name,
            s.date_order,
            s.partner_id,
            s.user_id,
            s.state,
            s.company_id,
            s.campaign_id,
            s.medium_id,
            s.source_id,
            s.pricelist_id,
            s.analytic_account_id,
            s.team_id,
            p.product_tmpl_id,
            partner.country_id,
            partner.industry_id,
            partner.commercial_partner_id,
            l.discount,
            s.id , l.delivery_amount , l.picking_ids , s.warehouse_id, s.invoice_status
        ))

ERROR: column l.picking_ids does not exist LINE 37: , l.delivery_amount as delivery_amount, l.picking_id...

I would like to make the filters show by date of entries to the store. But this is a related field. How can I achieve this? This should go mainly in the filters, to obtain the quantities delivered within the sales report.

Edit

This partially work:

def _query(self, with_clause='', fields={}, groupby='', from_clause=''):
        # Unimos stock_picking en funcion del order_id (sale.order)
        # donde en stock.picking se almacena el order_id como sale_id!!!
        from_clause += 'left join stock_picking sp on (l.order_id = sp.sale_id)'
        fields['date_done'] = ", sp.date_done as date_done"
        groupby += ', sp.date_done '

        fields['delivery_amount'] = ", l.delivery_amount as delivery_amount"
        groupby += ', l.delivery_amount '
        
        return super(SaleReport, self)._query(with_clause, fields, groupby, from_clause)

But it is repeating the values ​​of other columns so that the total is not correct.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source