@api.one
@api.depends('siren')
def _get_ongoing_amount(self):
lines_invoice = self.env['account.invoice.line'].search([
('invoice_id.state','in',['open']),
('partner_id.siren','=',self.siren),
('partner_id.siren','!=',False),
])
lines_order = self.env['sale.order.line'].search([
('order_partner_id.siren','=',self.siren),
('order_partner_id.siren','!=',False),
('qty_invoiced','=','0'),
('state','in',['sale']),
('cash','=',False)
])
self.ongoing_amount = sum(line.price_subtotal for line in lines_invoice) \
+ sum(line.price_subtotal for line in lines_order)
SQL method : @api.one
@api.depends('siren')
def _get_ongoing_amount(self):
# make sure param doesn't get to false
param = self.siren
if not param:
param = '999999999'
sum_lines_invoice = self._cr.execute("""
SELECT SUM(price_subtotal)
FROM account_invoice_line
FULL JOIN account_invoice so ON (state=so.state)
FULL JOIN res_partner res ON (siren=res.siren)
WHERE siren = %s
AND state IN ('open')""", (param,))
sum_lines_invoice = self._cr.fetchone()[0] or 0.0
sum_lines_order = self._cr.execute("""
SELECT SUM(price_subtotal)
FROM sale_order_line
WHERE siren = %s
AND qty_invoiced = '0'
AND state IN ('sale')
AND cash IS FALSE""", (param,))
sum_lines_order = self._cr.fetchone()[0] or 0.0
self.ongoing_amount = sum_lines_invoice + sum_lines_order