On 2017-12-30 15:08, 'Udo Spallek' via tryton-dev wrote:
> Hi,
> 
> I would like to extend account_invoice.Invoice.search_total_amount[A]
> which is based on a python-sql query.
> It should be extended with another module.
> In my extension I would like to replace the following column[B]::
> 
>     Coalesce(Sum(Round(
>         (
>             line.quantity * line.unit_price).cast(type_name),
>               currency.digits)), 0).as_('total_amount'), 
> 
> with something like this::
> 
>     Coalesce(Sum(Round(
>         (
>             line.quantity * line.unit_price * (1 - line.discount /
>     100.0) ).cast(type_name), currency.digits)), 0).as_('total_amount')
> 
> 
> 
> With introspection I found the column::
> 
>         columns = list(
>             result[0][2].columns[0].table.select.im_self
>             .queries[0].columns)
> 
> But I have no idea, how to replace the column, because I have not the
> correct line object from the depends module.
> When I try something like this::
> 
>     pool = Pool()
>     Line = pool.get('account.invoice.line')
>     line = Line.__table__()
> 
>     columns[1] = Coalesce(Sum(Round((
>         line.quantity * line.unit_price * (1 - line.discount / 100.0)
>     ).cast(type_name), currency.digits)), 0).as_('total_amount')
> 
>     result[0][2].columns[0].table.select.im_self.queries[0].columns = \
>         tuple(columns)
> 
> the line table is wrong, I need the table from [B].

Yes, you must take it from the 'from_' of the select but it is a
complicated task and fragile.

> Questions:
> 1. Is it a good practice trying to extend python-sql?

Not really this way. The usual way is to have composable query.

> 2. Is there a better way to introspect the python-sql then this::
> 
>         columns = list(
>             result[0][2].columns[0].table.select.im_self
>             .queries[0].columns)

Normally you should not need to use 'im_self' attributes otherwise it
seems OK. But as I said it is quite fragile as it will fail if any order
is changed in the initial query.

> 3. How to get the line-object from the base sql?

It is stored in the 'from_' of the SelectQuery.


In general, I think it is the wrong way to implement a discount. The
discount should modify the unit price. Otherwise you are changing the
definition of the field 'unit_price' and this may break other modules
which may rely on it.

-- 
Cédric Krier - B2CK SPRL
Email/Jabber: cedric.kr...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/

-- 
You received this message because you are subscribed to the Google Groups 
"tryton-dev" group.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tryton-dev/20180104102605.GC3925%40kei.

Attachment: signature.asc
Description: Digital signature

Reply via email to