Skip to Content
Menu
This question has been flagged

I have this wizard where I select date :

    <record id="wizard_forecast_invoice_date_form" model="ir.ui.view">
                <field name="name">wizard.forecast.invoice.date.form</field>
                <field name="model">custom_accounting.wizard_forecast_due_collection</field>
                <field name="arch" type="xml">
                    <form string="Date for Forecast Due Collection">
                        <separator string="Date for Forecast Due Collection"/>
                        <label string="This will list all open customer invoices that are on or before the Due Date selected"/>
                        <group>
                            <field name="before_date"/>                       
                        </group>
                        <footer>
                            <button name="forecast_due_collection" string="Okay" type="object" class="btn-primary" context="{'before_date': before_date}"/>
                            <button special="cancel" string="Cancel" class="btn-default"/>
                        </footer>
                    </form>
                </field>
            </record>


Where I want to send this date value to new class like this :

    class WizardForecastDueCollection(models.TransientModel):
        _name='custom_accounting.wizard_forecast_due_collection'
        before_date = fields.Date(string='Before Date', required=True, default=fields.Date.context_today)
   
        @api.multi
        def forecast_due_collection(self):
            ac = self.env['ir.model.data'].xmlid_to_res_id('custom_accounting.view_forecast_invoice_date_report_pivot')
            return {
                'name': 'ForecastInvoiceDateReport',
                'view_type': 'form',
                'res_model': 'custom_accounting.forecast_invoice_date_report',
                'view_id': ac,
                'context': {'default_before_date': self.before_date},
                'type': 'ir.actions.act_window',
                'view_mode': 'pivot',
                'domain': [('date_invoice', '<=', self.before_date)]
            }


And here's the class which should receive the value to execute the query :

    class ForecastInvoiceDateReport(models.Model):
        _name = "custom_accounting.forecast_invoice_date_report"
        _description = "Forecast Invoices Date Report"
   
        date_maturity = fields.Date(string='Due Date', readonly=True)
        date_invoice = fields.Date(string='Invoice Date', readonly=True)
        before_date = fields.Date(store=False, default=lambda s: fields.Date.context_today(s))
        country = fields.Char('Country', readonly=True)
        sales_person_name = fields.Char('Sales Person', readonly=True)
        partner_name = fields.Char('Customer', readonly=True)
        state = fields.Char('State', readonly=True)
        city = fields.Char('City', readonly=True)
        debit = fields.Float("Debit", readonly=True)
        credit = fields.Float("Credit", readonly=True)
        amount_residual = fields.Float("Balance", digits_compute=dp.get_precision('accounting_financial_report_dp'),
                                       readonly=True)
   
        duration = fields.Char('Duration', readonly=True)
        invoice_number = fields.Char('Invoice Number', readonly=True)
       
        _auto = False
        _rec_name = 'date_maturity'
        _depends = {
            'account.move': ['id', 'name'],
            'account.move.line': ['move_id', 'date_maturity'],
            'res.currency.rate': ['currency_id', 'name'],
            'res.partner': ['country_id', 'state_id', 'city'],
            'account.invoice': ['date_invoice'],
        }
   
        def _select(self):
   
            select_str = """
              SELECT
                    row_number() OVER () as id, duration, date_invoice, date_maturity,
                    country,state,city, sales_person_name, concat(partner_name,'(', Payment_Term, ')') partner_name ,
                    balance,amount_residual, debit,credit,invoice_number
              From
              (
                    SELECT
                        case
                          when "account_move_line".date_maturity is null Then 'No Date'
                          when (%s-ai.date_invoice) <30 then 'b#  0-30 days'
                          else 'Older'
                        END as duration, ai.date_invoice as date_invoice, "account_move_line".date_maturity as date_maturity,
                        res_country.name as country,res_country_state.name as state,res_partner.city as city,
                        new_rp.name as "sales_person_name", res_partner.name as "partner_name", res_partner.id as "partner_id1",         
                       COALESCE(SUM("account_move_line".balance), 0) AS balance,
                       COALESCE(SUM("account_move_line".amount_residual), 0) AS amount_residual,
                       COALESCE(SUM("account_move_line".debit), 0) AS debit,
                       COALESCE(SUM("account_move_line".credit), 0) AS credit,
                       "account_move_line__move_id".name AS invoice_number
                       FROM "account_move" as "account_move_line__move_id","account_move_line"
                       LEFT JOIN res_partner on "account_move_line".partner_id = res_partner.id
                       LEFT JOIN res_country on res_partner.country_id = res_country.id
                       LEFT JOIN res_country_state on res_partner.state_id = res_country_state.id
                       LEFT JOIN res_users on res_partner.user_id = res_users.id
                       LEFT JOIN res_partner as new_rp on res_users.partner_id = new_rp.id
                       INNER JOIN account_account as aa on "account_move_line"."account_id"=aa.id
                       INNER JOIN account_invoice as ai on "account_move_line"."invoice_id"=ai.id
                     INNER JOIN account_account_type as aat on aat.id = aa.user_type_id
                       WHERE ("account_move_line"."move_id"="account_move_line__move_id"."id")
                       AND (((( 
                       (aat.type = 'receivable') and account_move_line__move_id.state='posted' and
                         ("account_move_line"."reconciled" IS NULL or "account_move_line"."reconciled" = false )) 
                       )  AND  ("account_move_line__move_id"."state" = 'posted'))
                       ) GROUP BY
                        case
                          when "account_move_line".date_maturity is null Then 'No Date'
                          when (%s-ai.date_invoice) <30 then 'b#  0-30 days'
                          else 'Older'
                        END
                        , new_rp.name , res_partner.name, res_country.name,res_country_state.name,res_partner.city,res_users.login,
                        "account_move_line".date_maturity,"account_move_line__move_id".name,ai.date_invoice,res_partner.id
                                    order by 3
              ) as q1
              left join
              (
                  SELECT Substr(res_id,13) partner_id2,Substr(value_reference,22) Payment_Term_Id,pt.name Payment_Term
                  FROM public.ir_property ir join account_payment_term pt on pt.id = cast(Substr(value_reference,22) as integer)
                  where ir.name ='property_payment_term_id'
              ) as q2 on q1.partner_id1 = cast(q2.partner_id2 as integer)
   
               """ %(self.before_date, self.before_date)
            return select_str
   
        _table = 'custom_forecast_invoices_date_view'
   
        def init(self, cr):
            # self._table = account_invoice_report
            tools.drop_view_if_exists(cr, self._table)
            cr.execute("""CREATE or REPLACE VIEW %s as (
                        WITH currency_rate AS (%s)
                        %s
                    )""" % (
                self._table, self.pool['res.currency']._select_companies_rates(),
                self._select()))


It works fine if I replace the field before_date by current_date in sql query, But if I set parameters instead, I can't get the expected value.

Avatar
Discard
Author Best Answer

I got it,

Just re-run the query in wizard class , that's all :)

Avatar
Discard
Related Posts Replies Views Activity
1
Apr 21
11408
1
Sep 17
3718
0
Dec 16
7561
0
Aug 16
576
6
Jun 16
5367