Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SELECT    
  2.              pt.id
  3.             ,max(pp.project_name) AS project_name
  4.             ,max(pt.name) AS disposition_name
  5.             ,max(pt.external_reference) AS SAP_code
  6.             ,max(pt.work_order) AS work_order
  7.             ,max(ru.name) AS project_manager
  8.             ,max(pt.date_start) AS date_start
  9.             ,max(pt.date_end) AS date_end
  10.             ,max(pt.location) AS location
  11.             ,COALESCE(ROUND(max(dl.contracted_quantity),2), 0.00) AS contracted_quantity
  12.             ,COALESCE(ROUND(max(dl.realized_quantity),2), 0.00) AS realized_quantity
  13.             ,COALESCE(ROUND(max(contracted_quantity - realized_quantity),2), 0.00) AS unrealized_quantity
  14.             ,COALESCE(ROUND(max(dl.contracted_amount),2), 0.00) AS contracted_amount
  15.             ,COALESCE(ROUND(max(dl.realized_amount),2), 0.00) AS realized_amount
  16.             ,COALESCE(ROUND(max(contracted_amount - realized_amount),2), 0.00) AS unrealized_amount
  17.             ,COALESCE(ROUND(max(ai.lcy_amount_untaxed),2), 0.00) AS invoiced_untaxed
  18.             ,COALESCE(ROUND(max(ai.lcy_amount_total),2), 0.00) AS total_invoiced
  19.             ,COALESCE(ROUND(max(ai.lcy_amount_total - ai.residual),2), 0.00) AS paid_amount
  20.             ,COALESCE(ROUND(max(ai.residual),2), 0.00) AS unpaid_amount
  21.            
  22.            
  23.  
  24. FROM project_task pt
  25. LEFT JOIN  (
  26.         SELECT max(ru.id) AS id , max(rr.name) AS name
  27.         FROM res_users ru
  28.         LEFT JOIN resource_resource rr ON rr.user_id=ru.id
  29.         GROUP BY ru.id)
  30.             ru ON ru.id= pt.user_id
  31.  
  32. LEFT JOIN (
  33.             SELECT max(task_id) AS task_id
  34.                 ,COALESCE(ROUND(SUM(quantity),2), 0.00) AS contracted_quantity
  35.                 ,COALESCE(ROUND(SUM(total_realized_quantity),2), 0.00) AS realized_quantity
  36.                 ,COALESCE(ROUND(SUM(quantity * price_unit),2), 0.00) AS contracted_amount
  37.                 ,COALESCE(ROUND(SUM(total_realized_quantity * price_unit),2), 0.00) AS realized_amount
  38.                 FROM disposition_line
  39.            GROUP BY task_id
  40.                         ) dl ON dl.task_id= pt.id
  41. LEFT JOIN   (SELECT MAX(disposition_id) AS disposition_id
  42.                     ,COALESCE(ROUND(sum(CASE WHEN  ai.type= 'out_invoice'
  43.                                                 THEN ai.lcy_amount_untaxed
  44.                                             WHEN   ai.type= 'out_refund'
  45.                                                 THEN ABS(ai.lcy_amount_untaxed) * (-1)
  46.                                                           END), 2), 0.00) AS lcy_amount_untaxed
  47.  
  48.                     ,COALESCE(ROUND(sum(CASE WHEN  ai.type= 'out_invoice'
  49.                                                  THEN ai.lcy_amount_total
  50.                                             WHEN ai.type= 'out_refund'
  51.                                                 THEN ABS(ai.lcy_amount_total) * (-1)
  52.                                                           END), 2), 0.00) AS lcy_amount_total
  53.                     ,COALESCE(ROUND(sum(CASE WHEN  ai.type= 'out_invoice'
  54.                                                  THEN ai.residual
  55.                                             WHEN ai.type= 'out_refund'
  56.                                                 THEN ABS(ai.residual) * (-1)
  57.                                                           END), 2), 0.00) AS residual
  58.         FROM account_invoice ai
  59.             GROUP BY ai.disposition_id) ai ON ai.disposition_id= pt.id
  60. LEFT JOIN (SELECT MAX(pp.id) AS id,
  61.                     MAX(aaa.name) AS project_name
  62.                     FROM project_project pp
  63.                     LEFT JOIN account_analytic_account aaa
  64.                     ON pp.analytic_account_id= aaa.id
  65.         GROUP BY pp.id) pp on pp.id= pt.project_id
  66.  
  67.     GROUP BY pt.id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement