1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
| SELECT FechaAperturaOT,ttOTCab.NumOT as nor,
ttOTCab.Matric as immat,
ttotcab.anoot,
ttotcab.chasis,
ttotcab.km,
(SELECT coalesce (sum (ttotfichado.Tiempopasado),0)
FROM ttotfichado
WHERE ttotfichado.hayabono<> 1 AND
(NOT (ttOTFichado.RepartoPendiente = 0 AND ttOTFichado.FichajeContraOT > 0) OR ttOTFichado.RepartoPendiente IS NULL) AND
ttOTIntervencion.Emp = ttOTFichado.Emp AND
ttOTIntervencion.Numinterno = ttOTFichado.Numinterno AND
ttOTIntervencion.Intervencion = ttOTFichado.Intervencion AND
ttotfichado.fecha <= '12-1-2011 23:59:59.000') AS tt,
Count(DISTINCT ttOTCab.NumOT),
SUM(case tipocab
when 0 then
case tipolinea
when 1 then
case ttOTIntervencion.esabono
when 1 then (-1 * ttotlinea.PrecioCosteMedio * coalesce (ttotcargolinea.cantidadhoras,ttotlinea.cantidadhoras))
else (ttotlinea.PrecioCosteMedio * coalesce (ttotcargolinea.cantidadhoras,ttotlinea.cantidadhoras))
end
else 0
end
else 0
end) as ImpPCPMrec,
tgtaller.descrip as atelier,
tgempresa.razon as societe
FROM ttoTCab
inner join tgempresa on
ttotcab.emp = tgempresa.emp
inner join tgtaller on
ttotcab.emp = tgtaller.emp and ttotcab.taller = tgtaller.taller
INNER JOIN ttOTIntervencion ON
ttOTCab.Emp = ttOTIntervencion.Emp AND
ttOTCab.NumInterno = ttOTIntervencion.Numinterno
INNER JOIN ttOTLinea ON
ttOTIntervencion.Emp = ttOTLinea.Emp AND
ttOTIntervencion.Numinterno = ttOTLinea.NumIntOT AND
ttOTIntervencion.Intervencion = ttOTLinea.NumIntIntervencion
INNER JOIN ttSeccion ON
ttOTCab.Seccion = ttSeccion.Seccion
INNER JOIN ttSeccion seccion ON
ttOTIntervencion.Seccion = seccion.Seccion
LEFT OUTER JOIN TTrecepcionista ON
ttOtCab.emp = TTrecepcionista.emp and
ttOtCab.taller = TTrecepcionista.taller and
ttOtCab.recepcionista = TTrecepcionista.recepcionista
LEFT OUTER JOIN Tgmarca ON
ttOtCab.marca = tgmarca.marca
LEFT OUTER JOIN ttOTCargoLinea ON
ttOTLinea.Emp = ttOTCargoLinea.Emp AND
ttOTLinea.NumIntOT = ttOTCargoLinea.NumIntOT AND
ttOTLinea.NumIntIntervencion = ttOTCargoLinea.NumIntIntervencion AND
ttOTLinea.LineaItem = ttOTCargoLinea.LineaItem
LEFT OUTER JOIN ttOTCargoInt on
ttOTCargoLinea.Emp = ttotcargoint.emp AND
ttOTCargoLinea.NumIntOT = ttotcargoint.Numintot AND
ttOTCargoLinea.NumIntIntervencion = ttotcargoint.numintintervencion AND
ttOTCargoLinea.NumIntcargo = ttotcargoint.numintcargo
LEFT OUTER JOIN ttotcargo on
ttotcargoint.emp=ttotcargo.emp and
ttotcargoint.NumIntOT=ttotcargo.NumInterno and
ttotcargoint.NumIntCargo=ttotcargo.Cargo
LEFT OUTER JOIN ttgrupocargo on
ttOTCargo.grupocargo = ttgrupocargo.grupo
LEFT OUTER JOIN tgGrupoCont ON
ttOTLinea.GrupoCont = tgGrupoCont.GrupoCont
LEFT OUTER JOIN tgMarca MarcaLinea ON
ttOTLinea.Marca = MarcaLinea.Marca
LEFT OUTER JOIN tacategoriapieza ON
ttotlinea.marca = tacategoriapieza.marca and ttotlinea.codigocategoria = tacategoriapieza.codigo
LEFT OUTER JOIN ttotstatus ON ttOTCab.statusencurso = ttotstatus.Codigo
WHERE
(ttotintervencion.hayfactura = 0 ) and
(ttOTCab.Anulada = 0) and
(ttOTLinea.Anulada = 0) AND
(ttOTCab.FechaAperturaOT <= '12-1-2011 23:59:59.000' ) AND
(
ttOTLinea.FechaAlta <= '12-1-2011 23:59:59.000'
or ttotlinea.tipocab =1
) and
(ttotlinea.tipocab <> 2) and
(ttOTCab.Status = 30 ) and
(ttotlinea.cantidadhoras <> 0 or ttotlinea.almservido <> 1)
group by FechaAperturaOT, tgempresa.razon, tgtaller.descrip, ttOTIntervencion.Emp, ttOTIntervencion.Numinterno, ttOTIntervencion.Intervencion, ttOTCab.NumOT, ttOTCab.Matric, ttotcab.anoot, ttotcab.chasis, ttotcab.km
UNION ALL
SELECT FechaAperturaOT,ttOTCab.NumOT as nor,
ttOTCab.Matric as immat,
ttotcab.anoot,
ttotcab.chasis,
ttotcab.km,
(SELECT coalesce (sum (ttotfichado.Tiempopasado),0)
FROM ttotfichado
WHERE ttotfichado.hayabono<> 1 AND
(NOT (ttOTFichado.RepartoPendiente = 0 AND ttOTFichado.FichajeContraOT > 0) OR ttOTFichado.RepartoPendiente IS NULL) AND
ttOTIntervencion.Emp = ttOTFichado.Emp AND
ttOTIntervencion.Numinterno = ttOTFichado.Numinterno AND
ttOTIntervencion.Intervencion = ttOTFichado.Intervencion AND
ttotfichado.fecha <= '12-1-2011 23:59:59.000') AS tt,
Count(DISTINCT ttOTCab.NumOT),
SUM( case tipocab
when 0 then
case tipolinea
when 1 then
case ttOTIntervencion.esabono
when 1 then (-1 * ttotlinea.PrecioCosteMedio * coalesce (ttotcargolinea.cantidadhoras,ttotlinea.cantidadhoras))
else (ttotlinea.PrecioCosteMedio * coalesce (ttotcargolinea.cantidadhoras,ttotlinea.cantidadhoras))
end
else 0
end
else 0
end) as ImpPCPMrec,
tgtaller.descrip as atelier,
tgempresa.razon as societe
FROM
ttOTCab
LEFT OUTER JOIN tgmarca ON ttotcab.marca = tgmarca.marca
LEFT OUTER JOIN ttotstatus ON ttOTCab.statusencurso = ttotstatus.Codigo
LEFT OUTER JOIN ttrecepcionista ON ttOtCab.emp = TTrecepcionista.emp and
ttOtCab.taller = TTrecepcionista.taller and
ttOtCab.recepcionista = TTrecepcionista.recepcionista,
ttOTCargo,
ttOTCargoInt,
ttOTCargoLinea,
ttOTIntervencion,
ttOTLinea
LEFT OUTER JOIN tgGrupoCont ON tgGrupoCont.GrupoCont = ttOTLinea.GrupoCont
LEFT OUTER JOIN tgMarca MarcaLinea ON ttOTLinea.Marca = MarcaLinea.Marca
LEFT OUTER JOIN tacategoriapieza ON ttotlinea.marca = tacategoriapieza.marca and ttotlinea.codigocategoria = tacategoriapieza.codigo,
ttSeccion, ttOTFac, tgtaller, tgempresa, ttgrupocargo, ttSeccion seccion
WHERE
ttOTCargo.grupocargo = ttgrupocargo.grupo and
ttotfac.abono<> 1 and
ttotfac.fechafactura > '12-1-2011 23:59:59.000' and
ttotcab.emp = tgempresa.emp and
ttotcab.emp = tgtaller.emp and
ttotcab.taller = tgtaller.taller and
ttotcargo.emp = ttotfac.emp and
ttotcargo.NumInterno = ttotfac.NumInterno and
ttotcargo.Cargo = ttotfac.NumIntCargo and
ttotcargoint.emp = ttotcargo.emp and
ttotcargoint.NumIntOT = ttotcargo.NumInterno and
ttotcargoint.NumIntCargo = ttotcargo.Cargo and
ttotcargoLinea.emp = ttotcargoint.emp and
ttotcargoLinea.NumIntOT = ttotcargoint.NumIntOT and
ttotcargoLinea.NumIntCargo = ttotcargoint.NumIntCargo and
ttotcargoLinea.NumIntIntervencion = ttotcargoint.NumIntIntervencion and
ttotlinea.emp = ttotcargoLinea.emp and
ttotlinea.NumIntOT = ttotcargoLinea.NumIntOT and
ttotlinea.NumIntIntervencion = ttotcargoLinea.NumIntIntervencion and
ttotlinea.LineaItem=ttotcargoLinea.LineaItem and
(
ttOTLinea.FechaAlta <= '12-1-2011 23:59:59.000'
or ttotlinea.tipocab =1
) and
ttotlinea.tipocab <> 2 and
ttotintervencion.emp = ttotlinea.emp and
ttotintervencion.Numinterno = ttotlinea.NumIntOT and
ttotintervencion.Intervencion = ttotlinea.NumIntIntervencion and
ttOTCab.Seccion = ttSeccion.Seccion and
ttOTIntervencion.Seccion = Seccion.SECCION and
ttotcab.emp = ttotintervencion.emp and
ttotcab.numinterno = ttotintervencion.Numinterno and
ttotcab.fechaaperturaOT <= '12-1-2011 23:59:59.000' and
ttotcab.status >=30 and
(ttotlinea.cantidadhoras <> 0 or ttotlinea.almservido <> 1)
group by FechaAperturaOT, tgempresa.razon, tgtaller.descrip, ttOTIntervencion.Emp, ttOTIntervencion.Numinterno, ttOTIntervencion.Intervencion, ttOTCab.NumOT, ttOTCab.Matric, ttotcab.anoot, ttotcab.chasis, ttotcab.km |
Partager