Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Query used in python file of Purchase Module Report

By
Logicious
on 9/4/15, 5:22 AM 439 views

Anybody have idea about how the query used in python file??

i was  pasted the one of our base module query, if you can please explain me whats the use of this query, and how its works in odoo database.( i founded it from purchase ->report -> purchase_report.py file)


code:-


def _select(self):

select_str = """

SELECT min(l.id) as id,

l.product_id as product_id,

t.uom_id as product_uom,

sum(l.product_uom_qty / u.factor * u2.factor) as product_uom_qty,

sum(l.product_uom_qty * l.price_unit * (100.0-l.discount) / 100.0) as price_total,

count(*) as nbr,

s.date_order as date,

s.date_confirm as date_confirm,

s.partner_id as partner_id,

s.user_id as user_id,

s.company_id as company_id,

extract(epoch from avg(date_trunc('day',s.date_confirm)-date_trunc('day',s.create_date)))/(24*60*60)::decimal(16,2) as delay,

l.state,

t.categ_id as categ_id,

s.pricelist_id as pricelist_id,

s.project_id as analytic_account_id,

s.section_id as section_id

"""

return select_str

def _from(self):

from_str = """

sale_order_line l

join sale_order s on (l.order_id=s.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 product_uom u on (u.id=l.product_uom)

left join product_uom u2 on (u2.id=t.uom_id)

"""

return from_str

def _group_by(self):

group_by_str = """

GROUP BY l.product_id,

l.order_id,

t.uom_id,

t.categ_id,

s.date_order,

s.date_confirm,

s.partner_id,

s.user_id,

s.company_id,

l.state,

s.pricelist_id,

s.project_id,

s.section_id

"""

return group_by_str

def init(self, cr):

# self._table = sale_report

tools.drop_view_if_exists(cr, self._table)

cr.execute("""CREATE or REPLACE VIEW %s as (

%s

FROM ( %s )

%s

)""" % (self._table, self._select(), self._from(), self._group_by()))

1

Pawan

--Pawan--
1267
| 4 3 5
Hyderabad, India
--Pawan--


Pawan
On 9/4/15, 6:36 AM

Logicious,

Here a simple string construction is being done from the data returned from different variables and methods[self._table, self._select(), self._from(), self._group_by()] in the __init__() method of your class(defined at the last).

in __init__() method, at

cr.execute("""CREATE or REPLACE VIEW %s as (

%s

FROM ( %s )

%s

)""" % (self._table, self._select(), self._from(), self._group_by()))

'%s' are bieng replaced by the strings returned from (self._table, self._select(), self._from(), self._group_by()) respectively......

and then the query formed will be executed under "cr.execute()"

here they are running a join query on tables

[

sale_order_line l

join sale_order s on (l.order_id=s.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 product_uom u on (u.id=l.product_uom)

left join product_uom u2 on (u2.id=t.uom_id)

] (under _from() method)

to get data

Hope it helps,,,    

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 9/4/15, 5:22 AM
Seen: 439 times
Last updated: 9/4/15, 7:29 AM