Advertisement
Guest User

Untitled

a guest
Nov 7th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SELECT *  FROM (
  2.             SELECT * FROM(
  3.                         SELECT
  4.                             orcamento.placaveiculo as PLATE,
  5.                             orcamento.kmveiculo as KM,
  6.                             oficinas.supplicer as SUPPLIER_CODE,
  7.  
  8.  
  9.                        
  10.                                     CASE WHEN COALESCE(detalhe.event_number,0) = 0  
  11.                                             THEN NULL ELSE (
  12.                         CASE WHEN detalhe.tipo_evento = 'M'
  13.                             THEN orcamento.event_number_manutencao ELSE (
  14.                         CASE WHEN detalhe.tipo_evento = 'P'
  15.                             THEN orcamento.event_number_pneu  ELSE (
  16.                         CASE WHEN orcamento.tipomanu = detalhe.tipo_evento
  17.                             THEN orcamento.event_number END )
  18.                                     END  ) END ) END  as EVENT_NUMBER ,
  19.  
  20.  
  21.  
  22.            
  23.  
  24.            
  25.            
  26.                     CASE WHEN COALESCE(detalhe.event_number,0) = 0  THEN NULL ELSE (
  27.                         CASE WHEN detalhe.tipo_evento = 'M' AND orcamento.tipomanu = detalhe.tipo_evento
  28.                         THEN orcamento.acordomanutencao ELSE (
  29.                     CASE WHEN detalhe.tipo_evento = 'P' AND orcamento.tipomanu = detalhe.tipo_evento
  30.                         THEN orcamento.acordopneu  ELSE (
  31.                     CASE WHEN orcamento.tipomanu = detalhe.tipo_evento
  32.                         THEN orcamento.acordo END )
  33.                                     END  ) END ) END  as AGREEMENT_NUMBER ,
  34.            
  35.            
  36.  
  37.                    CASE WHEN detalhe.jobcode  = '1114' THEN
  38.                 orcamento.tipomanu ELSE
  39.                             detalhe.tipo_evento  END as NOE,
  40.  
  41.  
  42.  
  43.  
  44.                             '' as TYPE,
  45.                             orcamento.datachegada as EVENT_DATE,
  46.                             detalhe.jobcode as JOB_CODE,
  47.                             detalhe.descricao as JOB_CODE_DESCRIPTION,
  48.                             replace(replace(replace(to_char(detalhe.qtd, '999999999990D99'),',','-' ),'.',','),'-','.')  as QTT,
  49.                             replace(replace(replace(to_char(detalhe.vunit, '999999999990D9999'),',','-' ),'.',','),'-','.')  as UNIT_PRICE,
  50.                             'BRL' as CURRENCY,
  51.                             '0,00' as DISCOUNT,
  52.                             replace(replace(replace(to_char((detalhe.qtd * detalhe.vunit), '999999999990D9999'),',','-' ),'.',','),'-','.') as TOTAL_WO_TAX,
  53.                             'PAS DE TAXES' as TAX,
  54.                             '0,00' as TOTAL_W_TAX,
  55.                             orcamento.datachegada as REAL_START_DATE,
  56.                             orcamento.horachegada as REAL_START_HOUR,
  57.                             orcamento.datafim as  PLANNED_END_DATE,
  58.                             orcamento.horafim as PLANNED_END_HOUR,
  59.                        
  60.                             'Observações:             ' || regexp_replace(orcamento.observacao, E'<[^>]+>', '', 'gi') || '   ' ||
  61.                             'Observações da Oficina:  ' || regexp_replace(orcamento.observacaoofi, E'<[^>]+>', '', 'gi') || '   ' ||
  62.                             'Defeitos Apontados:        ' || regexp_replace(orcamento.defeitosapontados, E'<[^>]+>', '', 'gi') || '   ' ||
  63.                             'Avarias:           ' || regexp_replace(orcamento.avarias, E'<[^>]+>', '', 'gi') as COMMENT,
  64.                            
  65.                            
  66.  
  67.                            CASE WHEN COALESCE(detalhe.event_number,0) = 0   THEN 'Em Curso' ELSE 'Dado Acordo' END  as REASON,
  68.                             veiculos.contrato as CONTRAT,
  69.                                 '0' as REINVOICE,
  70.                             orcamento.id as EXTERNAL_ID,
  71.                            
  72.            
  73.                            CASE WHEN COALESCE(detalhe.event_number,0) > 0  THEN 0 ELSE 1 END  as CREATE_AGREEMENT,
  74.            
  75.                            
  76.                            detalhe.importado as importado,
  77.                detalhe.id as id_det
  78.                
  79.  
  80.                                 FROM os.os orcamento
  81.                                 INNER JOIN os.detos detalhe ON orcamento.id = detalhe.os_id
  82.                                 INNER JOIN cadastro.oficinas oficinas ON orcamento.oficina_id = oficinas.id
  83.                                 INNER JOIN veiculo.veiculos veiculos ON orcamento.veiculo_id = veiculos.id
  84.                                
  85.                                 WHERE orcamento.status <> 'F'
  86.                                 AND detalhe.reprova = 'N'
  87.                                 AND detalhe.cancelamento = 'N'
  88.  
  89.  
  90.                                
  91.            
  92.            
  93.                                 ORDER BY orcamento.id
  94.            
  95.            
  96.                                
  97.                         ) AS foo
  98.            
  99.            
  100.                         WHERE  importado = 0
  101.                       --  AND COALESCE(event_number,0) = 0
  102.                        -- AND COALESCE(AGREEMENT_NUMBER,NULL) IS NULL
  103.                        -- AND CREATE_AGREEMENT = 1
  104.                      
  105.            
  106.                         ORDER BY create_agreement ASC
  107.                         ) RES
  108.  
  109.  
  110.                        WHERE
  111.                      id_det =  (select  det.id from os.detos det  
  112.                 inner join os.os os on os.id = det.os_id
  113.                 where 1=1
  114.                 and  det.importado = 0
  115.                 AND det.id = RES.id_det
  116.                 --AND det.tipo_evento <> os.tipomanu
  117.                 AND COALESCE(det.event_number,null) is null)
  118. AND
  119.             external_id IN (select os_id from os.detos where  importado = 0
  120. group by 1)
  121.  
  122. --and external_id = 581
  123. --select * from os.detos where os_id = 581
  124.  
  125.  
  126.            
  127.                         --  noe  exist (select tipo_evento from os.detos where os_id = RES.external_id)          
  128.                   --    COALESCE((select count(event_number) as total from os.detos where os_id = RES.external_id ),0) = 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement