Help

0

Limit the pivot report between 2 dates with wizard

Avatar
Jakub Parcheta

Hi, 

It takes too long to generate the report because there are to many data.


I created a wizard with start_date and finish_date and I would like to know how to limit the report between both dates.


Wizard .py:

class GetDatesWizard(models.TransientModel):

    _name = 'get.dates.wizard'


    start_date = fields.Date("Start date")

    stop_date = fields.Date("Stop date")


    def create_report(self):

        return {

            'name': 'generate report',

            'type': 'ir.actions.act_window',

            'res_model': 'irco.nutri.report.costeplatos',

            'view_mode': 'pivot',

        }


Wizard .xml:

    <record id="view_get_dates_wizard" model="ir.ui.view">

        <field name="name">view.get.dates.wizard</field>

        <field name="model">get.dates.wizard</field>

        <field name="arch" type="xml">

            <form>

                <group>

                    <field name="start_date"/>

                    <field name="stop_date"/>

                </group>

                <footer>

                    <button name="create_report" type="object" string="Create" class="oe_highlight"/>

                    <button name="cancel" string="Cancel" special="cancel" class="oe_link"/>

                </footer>

            </form>

        </field>

    </record>


Report .py:

class CostPlateReport(models.Model):

    _name = "cost.plate.report"

    _order = 'date desc'


    partner = fields.Many2one('res.partner','Client')

    date = fields.Date('Date')

    plate = fields.Char('Plato')

    cost = fields.Float('Cost')


    def _query(self, with_clause='', fields={}, groupby='', from_clause=''):

        with_ = ("WITH %s" % with_clause) if with_clause else ""


        select_ = """

            min(m.id) as id,

            m.date as date,

            m.partner as partner,

            pt.name as plate,

            pc.cost as cost

        """  

        for field in fields.values(): 

            select_ += field


        from_ = """

                irco_menu m

                    join irco_menu_plates p on (p.plate_menu_id=m.id)

                        join res_partner r on (r.id=p.partner)

                        join product_template pt on (p.plate_ids=pt.id)

                            join irco_plate_cost pc on (pc.product_template_id=pt.id)

                            join product_product pp on (pp.product_tmpl_id=pt.id)

                %s

        """ % from_clause


        groupby_ = """

            m.id,

            m.date,

            m.partner,

            pt.name,

            pc.cost

            %s

        """ % (groupby)


        return "%s (SELECT %s FROM %s WHERE m.state = 'done' GROUP BY %s)" % (with_, select_, from_, groupby_)


    @api.model_cr

    def init(self):

        tools.drop_view_if_exists(self.env.cr, self._table)

        self.env.cr.execute("""CREATE or REPLACE VIEW %s as (%s)""" % (self._table, self._query()))


Report .xml:

    <record id="view_cost_plate_report" model="ir.ui.view">

         <field name="name">view.cost.plate.report</field>

         <field name="model">cost.plate.report</field>

         <field name="arch" type="xml">

             <pivot string="Cost Analysis" disable_linking="True">

                 <field name="date" interval="month" type="row"/>

                <field name="cost" type="measure"/>

             </pivot>

         </field>

    </record>

Avatar
Discard