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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
| USE [DZINDZIO_TRUCKS_MANAGEMENT_GILLES]
GO
/****** Object: Trigger [dbo].[CAMION_LOCALISATION_UPDATE_TR] Script Date: 2019-05-23 15:34:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[CAMION_LOCALISATION_UPDATE_TR] ON [dbo].[CAMION_LOCALISATION_V] INSTEAD OF UPDATE
AS
DECLARE @theTrigger as VARCHAR(64) = 'CAMION_LOCALISATION_UPDATE_TR'
DECLARE @EngueuladeIntro AS VARCHAR(128) = @theTrigger + ' - Création de camions (insert) : '
+ CHAR(13) + SPACE(8) ;
DECLARE @Engueulade AS VARCHAR(512) ;
DECLARE @ErrorZeroTuple AS VARCHAR(64) = 'Veuillez fournir le VIN correct du camion à modifier.' ;
DECLARE @ErrorMultiTuple AS VARCHAR(64) = 'Une seule modification à la fois SVP.'
DECLARE @ErrorDoublonVIN AS VARCHAR(64) = 'Il existe déjà un camion de VIN '''
DECLARE @ErrorDoublonImmat AS VARCHAR(64) = 'il existe déjà un camion immatriculé '''
DECLARE @ErrorDoublonNumber AS VARCHAR(64) = 'il existe déjà un camion portant le Number '''
DECLARE @errorMissingManufacturerNameNew AS VARCHAR(64) = 'Missing new Manufacturer name' ;
DECLARE @errorUnknownManufacturerNameNew AS VARCHAR(64) = 'Unknown new Manufacturer name' ;
DECLARE @errorMissingModelNameNew AS VARCHAR(64) = 'Missing new Model name' ;
DECLARE @errorUnknownModelNameNew AS VARCHAR(64) = 'Unknown new Model name' ;
DECLARE @errorManuacturerModelViolIR AS VARCHAR(160) = 'On ne peut changer le manufacturier d''un camion qu''à condition d''en remplacer en même temps le modèle par un modèle du nouveau manufacturier'
DECLARE @ErrorMissingModel AS VARCHAR(64) = 'Modèle de camion inexistant '
DECLARE @ErrorUnknownModel AS VARCHAR(64) = 'Modèle de camion inconnu '
DECLARE @errorSequenceDatesMfgAchat AS VARCHAR(96) = 'La date de fabrication du camion ne peut pas être postérieure à sa date d''achat'
DECLARE @errorSequenceDatesAchatVente AS VARCHAR(96) = 'La date d''achat du camion ne peut pas être postérieure à sa date de vente'
DECLARE @VinOld as varchar(24) ;
DECLARE @VinNew as varchar(24) ;
DECLARE @RemplacerCamionVIN as char(1) = 'N';
DECLARE @LocalisationId AS INT ;
DECLARE @Ninserted as int ;
DECLARE @Ndeleted as int ;
DECLARE @immatOld as varchar(16) ;
DECLARE @immatNew as varchar(16) ;
DECLARE @numberOld as varchar(6) ;
DECLARE @numberNew as varchar(6) ;
DECLARE @ManufacturerNameOld AS VARCHAR(48) ;
DECLARE @ManufacturerNameNew AS VARCHAR(48) ;
DECLARE @ManufacturerIdOld AS INT ;
DECLARE @ManufacturerIdNew AS INT ;
DECLARE @ModelOld AS VARCHAR(48) ;
DECLARE @ModelNew AS VARCHAR(48) ;
-----------DECLARE @ManufacturerNameNewId AS INT ; utiliser @ManufacturerIdNew !
DECLARE @ModelNewId AS INT ;
DECLARE @AutreCamion as varchar(24) ;
DECLARE @DateParDefaut as DATE = '9999-12-31' ;
DECLARE @MfgDateOld as DATE ;
DECLARE @MfgDateNew as DATE ;
DECLARE @PurchaseDateOld as DATE ;
DECLARE @PurchaseDateNew as DATE ;
DECLARE @SaleDateOld as DATE ;
DECLARE @SaleDateNew as DATE ;
DECLARE @Degager AS CHAR(1) = 'N'
DECLARE @ErrorSerialNumber AS CHAR(1) = 'N'
DECLARE @ErrorManufacturerName AS CHAR(1) = 'N'
DECLARE @ErrorModelName AS CHAR(1) = 'N'
DECLARE @ErrorSequenceDates AS CHAR(1) = 'N'
DECLARE @VinAffichage AS VARCHAR(196) = ''
DECLARE @ManufacturerAffichage AS VARCHAR(128) = '' ;
DECLARE @errorSequenceDatesMfgVente AS VARCHAR(96) = 'La date de fabrication du camion ne peut pas être postérieure à sa date de vente'
DECLARE @CategorieQuelconque as CHAR(1) = 'z'
DECLARE @w AS VARCHAR(64) ;
----pour debug : ---- SELECT '' as 'LOCALISATION_CAMION_UPDATE_TR, INSERTED', * from INSERTED
----pour debug : ---- SELECT '' as 'LOCALISATION_CAMION_UPDATE_TR, DELETED', * from DELETED
SET @Ninserted = (SELECT COUNT(*) FROM INSERTED)
SET @Ndeleted = (SELECT COUNT(*) FROM DELETED)
------------------------------------------------------------------
-- Vérifier qu'on au moins une ligne (INSERTED)
------------------------------------------------------------------
IF @Ninserted = 0 OR @Ndeleted = 0
BEGIN
SET @Engueulade = @EngueuladeIntro + @ErrorZeroTuple
---- RAISERROR (@Engueulade, 16,1) -- state = 16 pour bloquer
RAISERROR (@Engueulade, 0,1) -- state = 0 pour les tests
RETURN
END
------------------------------------------------------------------
-- Vérifier qu'on traite exactement une seule ligne
------------------------------------------------------------------
IF @Ninserted <> 1 OR @Ndeleted <> 1
BEGIN
SET @Engueulade = @EngueuladeIntro + @ErrorMultiTuple
+ '(Truck VIN = ''' + (select TOP 1 VIN from DELETED) + ''', etc.)' ;
---- RAISERROR (@Engueulade, 16,1) -- state = 16 pour bloquer
RAISERROR (@Engueulade, 0,1) -- state = 0 pour les tests
RETURN
END
--------------------------------------------------------
-- affectation des variables
--------------------------------------------------------
SET @VinOld = (select VIN from DELETED) ;
SET @VinNew = TRIM((select VIN from INSERTED)) ;
SET @LocalisationId = (SELECT CamionId FROM CAMION WHERE CamionVIN = @VinOld) ;
SET @immatNew = TRIM((SELECT COALESCE(Immat, '') FROM INSERTED)) ;
SET @immatOld = (SELECT COALESCE(Immat, '') FROM DELETED) ;
SET @numberNew = TRIM((SELECT COALESCE(Number, '') FROM INSERTED)) ;
SET @numberOld = (SELECT COALESCE(Number, '') FROM DELETED) ;
SET @ManufacturerNameNew = TRIM((SELECT COALESCE(Manufacturer, '') FROM INSERTED)) ;
SET @ManufacturerNameOld = (SELECT COALESCE(Manufacturer, '') FROM DELETED) ;
SET @ModelNew = TRIM((SELECT COALESCE(Model, '') FROM INSERTED)) ;
SET @ModelOld = (SELECT COALESCE(Model, '') FROM DELETED) ;
------------------------------------------------------------------
-- Vérifier s'il y a changement du VIN
------------------------------------------------------------------
IF @VinNew <> @VinOld
BEGIN
-------------------------------------------------------------------------------------
-- On prend note de la modif du VIN, on le remplacera au moment opportun
-------------------------------------------------------------------------------------
SET @w = (SELECT CamionVIN FROM CAMION WHERE CamionVIN = @VinNew)
-----------------------------------------------------------
-- Ne pas créer de VIN doublon
-----------------------------------------------------------
IF @w IS NOT NULL
BEGIN
SET @Engueulade = @EngueuladeIntro
+ @ErrorDoublonVIN
+ @VinNew + '''.' ;
---- RAISERROR (@Engueulade, 16,1) -- state = 16 pour bloquer
RAISERROR (@Engueulade, 0,1) -- state = 0 pour les tests
RETURN
END
SET @RemplacerCamionVIN = 'Y' ;
END
----pour debug : ---- select '' as '@RemplacerCamionVIN', @RemplacerCamionVIN ;
------------------------------------------------------------------
-- contrôle du changement d'immat
------------------------------------------------------------------
IF @immatNew <> @immatOld
BEGIN
---------------------------------------------------------------------------------------------------
-- On interdit qu'un changement d'immat provoque un doublon avec l'immat d'un autre camion
---------------------------------------------------------------------------------------------------
IF @immatNew <> '' ---- les immats non renseignées sont systématiquement autorisées
BEGIN
SET @AutreCamion = (SELECT TOP 1 CamionVIN FROM CAMION WHERE CamionImmat = @immatNew AND CamionVIN <> @VinOld) ;
----pour debug : ---- SELECT '' AS 'immat : @AutreCamion', @AutreCamion ;
IF @AutreCamion <> ''
BEGIN
SET @Engueulade = @EngueuladeIntro
+ 'VIN Truck : ' + @VinOld + ''', '
+ @ErrorDoublonImmat
+ @immatNew + ''' (VIN de ce camion : ''' + @AutreCamion + ''').'
---- RAISERROR (@Engueulade, 16,1) -- state = 16 pour bloquer
RAISERROR (@Engueulade, 0,1) -- state = 0 pour les tests
SET @Degager = 'Y'
---- RETURN différé
END
END
END
------------------------------------------------------------------
-- contrôle du changement de number
------------------------------------------------------------------
IF @numberNew <> @numberOld
BEGIN
---------------------------------------------------------------------------------------------------
-- On interdit qu'un changement de number provoque un doublon avec le number d'un autre camion
---------------------------------------------------------------------------------------------------
IF @numberNew <> '' AND @numberNew <> '0' ---- les numbers non renseignés sont systématiquement autorisés
BEGIN
SET @AutreCamion = (SELECT TOP 1 CamionVIN FROM CAMION WHERE CamionNumber = @numberNew AND CamionVIN <> @VinOld) ;
----pour debug : ---- select '' as 'Number : @AutreCamion', @AutreCamion ;
IF @AutreCamion <> ''
BEGIN
SET @Engueulade = @EngueuladeIntro
+ 'VIN Truck : ' + @VinOld + ''', '
+ @ErrorDoublonNumber
+ @numberNew + ''' (VIN de ce camion : ''' + @AutreCamion + ''').'
---- RAISERROR (@Engueulade, 16,1) -- state = 16 pour bloquer
RAISERROR (@Engueulade, 0,1) -- state = 0 pour les tests
SET @Degager = 'Y'
---- RETURN différé
END
END
END
-------------------------------------
-- pour faciliter les affichages
-- à suivre
-------------------------------------
IF @VinNew = @VinOld
BEGIN
SET @VinAffichage = ' (VIN : '''
+ COALESCE(@VinOld, '??') + ''''
END
ELSE
BEGIN
SET @VinAffichage = ' (Previous VIN : '''
+ COALESCE(@VinOld, '??')
+ ', New VIN : '''
+ COALESCE(@VinNew, '??') + ''''
END
----------------------------------------
-- Contrôle des dates
----------------------------------------
SET @MfgDateNew = (SELECT DateMfg FROM INSERTED) ;
SET @PurchaseDateNew = (SELECT DateAchat FROM INSERTED) ;
SET @SaleDateNew = (SELECT DateSold FROM INSERTED) ;
IF @MfgDateNew IS NULL SET @MfgDateNew = @DateParDefaut
IF @PurchaseDateNew IS NULL SET @PurchaseDateNew = @DateParDefaut
IF @SaleDateNew IS NULL SET @SaleDateNew = @DateParDefaut
IF @MfgDateNew <> @DateParDefaut AND @PurchaseDateNew <> @DateParDefaut
AND @MfgDateNew > @PurchaseDateNew
BEGIN
SET @Engueulade = @EngueuladeIntro + @errorSequenceDatesMfgAchat
+ @VinAffichage
+ ' ; Mfg Date = ''' + CAST(@MfgDateNew AS VARCHAR) + ''''
+ ', Purchase Date = ''' + CAST(@PurchaseDateNew AS VARCHAR) + ''').'
SELECT @Engueulade AS Engueulons
-------- RAISERROR (@Engueulade,16,1) -- state = 16 pour bloquer
RAISERROR (@Engueulade,0,1) -- state = 0 pour les tests
SET @ErrorSequenceDates = 'Y'
SET @Degager = 'Y'
---- RETURN différé
END
IF @PurchaseDateNew <> @DateParDefaut AND @SaleDateNew <> @DateParDefaut
AND @PurchaseDateNew > @SaleDateNew
BEGIN
SET @Engueulade = @EngueuladeIntro + @errorSequenceDatesAchatVente
+ @VinAffichage
+ ' ; Purchase Date = ''' + CAST(@PurchaseDateNew AS VARCHAR) + ''''
+ ', Sale Date = ''' + CAST(@SaleDateNew AS VARCHAR) + ''').'
SELECT @Engueulade AS Engueulons
-------- RAISERROR (@Engueulade,16,1) -- state = 16 pour bloquer
RAISERROR (@Engueulade,0,1) -- state = 0 pour les tests
SET @ErrorSequenceDates = 'Y'
SET @Degager = 'Y'
---- RETURN différé
END
IF @MfgDateNew <> @DateParDefaut AND @MfgDateNew > @SaleDateNew
BEGIN
SET @Engueulade = @EngueuladeIntro + @errorSequenceDatesMfgVente
+ @VinAffichage
+ ' ; Mfg Date = ''' + CAST(@MfgDateNew AS VARCHAR) + ''''
+ ', Sale Date = ''' + CAST(@SaleDateNew AS VARCHAR) + ''').'
SELECT @Engueulade AS Engueulons
-------- RAISERROR (@Engueulade,16,1) -- state = 16 pour bloquer
RAISERROR (@Engueulade,0,1) -- state = 0 pour les tests
SET @ErrorSequenceDates = 'Y'
SET @Degager = 'Y'
---- RETURN différé
END
----------------------------------------------------------------
-- Le manufacturier (new) doit être connu en tant que contact
----------------------------------------------------------------
SET @ManufacturerNameOld = (SELECT Manufacturer FROM DELETED)
SET @ManufacturerIdOld = (SELECT ContactId FROM CONTACT WHERE ContactName = @ManufacturerNameOld)
SET @ManufacturerNameNew = TRIM((SELECT Manufacturer FROM INSERTED))
IF @ManufacturerNameNew IS NOT NULL AND LEN(@ManufacturerNameNew) > 0
SET @ManufacturerIdNew = (SELECT ContactId FROM CONTACT WHERE ContactName = @ManufacturerNameNew)
---- pour debug ----- select @ManufacturerNameOld as '@ManufacturerNameOld' ;
---- pour debug ----- select @ManufacturerNameNew as '@ManufacturerNameNew' ;
---- pour debug ----- select @ManufacturerIdOld as '@ManufacturerIdOld' ;
---- pour debug ----- select @ManufacturerIdNew as '@ManufacturerIdNew' ;
IF @ManufacturerNameNew <> @ManufacturerNameOld OR @ManufacturerNameNew IS NULL
BEGIN
IF @ManufacturerNameNew IS NULL OR LEN(@ManufacturerNameNew) = 0
BEGIN
SET @Engueulade = @EngueuladeIntro + @errorMissingManufacturerNameNew
+ @VinAffichage
+ ' ; Previous Manufacturer : '''
+ COALESCE(@ManufacturerNameOld, '??') + ''').'
SELECT @Engueulade AS Engueulons, * FROM INSERTED
--------------- RAISERROR (@Engueulade, 16,1) -- state = 16 pour bloquer
RAISERROR (@Engueulade, 0,1) -- state = 0 pour les tests
SET @ErrorManufacturerName = 'Y'
SET @Degager = 'Y'
---- RETURN
END
ELSE
IF @ManufacturerIdNew IS NULL
BEGIN
SET @Engueulade = @EngueuladeIntro + @errorUnknownManufacturerNameNew
+ @VinAffichage
+ ' ; Previous Manufacturer : '''
+ COALESCE(@ManufacturerNameOld, '??') + ''''
+ ', New Manufacturer : '''
+ COALESCE(@ManufacturerNameNew, '??') + ''').'
SELECT @Engueulade AS Engueulons
-------- RAISERROR (@Engueulade,16,1) -- state = 16 pour bloquer
RAISERROR (@Engueulade,0,1) -- state = 0 pour les tests
SET @ErrorManufacturerName = 'Y'
SET @Degager = 'Y'
---- RETURN différé
END
END
------------------------------------------------------------------
-- contrôle du changement de modèle de camion
------------------------------------------------------------------
SET @ModelNewId = (SELECT TruckModelId FROM TRUCK_MODEL
WHERE TruckModelName = @ModelNew
AND TruckManufacturerId = @ManufacturerIdNew)
IF @ErrorManufacturerName = 'N'
AND (@ManufacturerNameNew <> @ManufacturerNameOld OR @ModelNew <> @ModelOld)
-------------------------------------------------
-- Pas d'erreur concernant le manufacturier,
-- on peut contrôler le modèle
-------------------------------------------------
BEGIN
IF NOT EXISTS (
SELECT '' FROM TRUCK_MODEL_V
WHERE ManufacturerName = @ManufacturerNameNew
AND ModelName = @ModelNew
)
----------------------------------------------
-- Modèle absent ou inconnu
----------------------------------------------
BEGIN
IF LEN(@ModelNew) = 0
BEGIN
SET @Engueulade = @EngueuladeIntro + @ErrorMissingModel
+ ' (VIN Truck : ''' + @VinOld + ''','
+ ' (Manufacturer : ''' + @ManufacturerNameNew + ''').'
---- RAISERROR (@Engueulade, 16,1) -- state = 16 pour bloquer
RAISERROR (@Engueulade, 0,1) -- state = 0 pour les tests
SET @Degager = 'Y'
---- RETURN différé
END
ELSE
BEGIN
SET @Engueulade = @EngueuladeIntro + @ErrorUnknownModel
+ '''' + @ModelNew + ''''
+ ' (VIN Truck : ''' + @VinOld + ''','
+ ' (Manufacturer : ''' + @ManufacturerNameNew + ''').'
---- RAISERROR (@Engueulade, 16,1) -- state = 16 pour bloquer
RAISERROR (@Engueulade, 0,1) -- state = 0 pour les tests
SET @Degager = 'Y'
---- RETURN différé
END
END
---- pour debug ----- select @@ManufacturerIdNew AS '@@ManufacturerIdNew'
---- pour debug ----- select @ModelNewId AS '@ModelNewId'
END
------------------------------------------------------------------
-- Modification du vendeur.
-- Si le new vendeur existe déjà dans la table CONTACT,
-- on se contente de mettre à jour la table TRUCK_PURCHASE.
-- S'il n'existe pas dans la table CONTACT, on le crée dans
-- la table CONTACT et dans la table TRUCK_PURCHASE.
-------------------------------------------------------------------
DECLARE @VendeurOld AS VARCHAR(48) ;
DECLARE @VendeurNew AS VARCHAR(48) ;
DECLARE @VendeurIdNew AS VARCHAR(48) ;
DECLARE @TruckContactPrecedentId AS INT
DECLARE @IsVendeurEnBase AS CHAR(1) = 'N'
DECLARE @CamionId AS INT
DECLARE @TruckPurchaseId AS INT
SET @VendeurOld = TRIM((SELECT Seller FROM DELETED))
SET @VendeurNew = TRIM((SELECT Seller FROM INSERTED))
IF @VendeurNew IS NOT NULL AND LEN(@VendeurNew) > 0
BEGIN
SET @VendeurIdNew = (SELECT ContactId
FROM CONTACT
WHERE ContactName = @VendeurNew)
IF @VendeurIdNew IS NOT NULL
BEGIN
SET @IsVendeurEnBase = 'Y'
END
END
---- pour debug ---- select @VendeurOld as '@VendeurOld'
---- pour debug ---- select @VendeurNew as '@VendeurNew'
---- pour debug ---- select @VendeurIdNew as '@VendeurIdNew'
---- pour debug ---- select @IsVendeurEnBase as '@IsVendeurEnBase'
-----------------------------------------------------------
-- Si anomalies détectées, on dégage.
-----------------------------------------------------------
IF @Degager = 'Y'
BEGIN
----pour debug : ---- select 'ciao !'
RETURN
END
------------------------------------------------------------------------
-- On met à jour les colonnes non contraintes
------------------------------------------------------------------------
IF UPDATE(DateAchat) OR UPDATE(DateSold) OR UPDATE(DateMfg)
OR UPDATE(Manufacturer) OR UPDATE(Model) OR UPDATE(Color)
OR UPDATE(WheelBase) OR UPDATE(USDOT) OR UPDATE(ICCMC)
OR UPDATE(GVWR)
BEGIN
----pour debug : ---- select 'update du reste'
UPDATE CAMION
SET
CamionDateMfg = (SELECT DateMfg FROM INSERTED)
, CamionDateAchat = (SELECT DateAchat FROM INSERTED)
, CamionDateVente = (SELECT DateSold FROM INSERTED)
, TruckManufacturerId = @ManufacturerIdNew -----@ManufacturerNameNewId
, TruckModelId = @ModelNewId
, CamionWheelBase = (SELECT WheelBase FROM INSERTED)
, CamionColor = (SELECT Color FROM INSERTED)
, USDOT = (SELECT USDOT FROM INSERTED)
, ICCMC = (SELECT ICCMC FROM INSERTED)
, GVWR = (SELECT GVWR FROM INSERTED)
WHERE CamionVIN = @VinOld
----WHERE EXISTS (SELECT *
---- FROM DELETED
---- WHERE VIN = CAMION.CamionVIN)
;
END
----pour debug : ----SELECT '' as '2e ! LOCALISATION_CAMION_UPDATE_TR, CAMION', * from CAMION
----pour debug : ----SELECT '' as '2e ! LOCALISATION_CAMION_UPDATE_TR, CAMION_LOCALISATION_V', * from CAMION_LOCALISATION_V
------------------------------------------------------------------------
-- Prise en compte des changements d'immat
------------------------------------------------------------------------
IF @immatNew <> @immatOld
BEGIN
UPDATE CAMION
SET CamionImmat = (SELECT CASE TRIM(Immat) WHEN '' THEN '0' ELSE COALESCE(TRIM(Immat),'0') END FROM INSERTED)
WHERE CamionVIN = @VinOld
----WHERE EXISTS (SELECT *
---- FROM DELETED
---- WHERE VIN = CAMION.CamionVIN)
;
END
------------------------------------------------------------------------
-- Prise en compte des changements de number
------------------------------------------------------------------------
IF @numberNew <> @numberOld
BEGIN
UPDATE CAMION
SET CamionNumber = (SELECT CASE TRIM(Number) WHEN '' THEN '0' ELSE COALESCE(TRIM(Number),'0') END FROM INSERTED)
WHERE CamionVIN = @VinOld
----WHERE EXISTS (SELECT *
---- FROM DELETED
---- WHERE VIN = CAMION.CamionVIN)
;
END
------------------------------------------------------------------------
-- Prise en compte du changement de vendeur
------------------------------------------------------------------------
IF @VendeurNew <> @VendeurOld AND @VendeurNew IS NOT NULL
BEGIN
IF @IsVendeurEnBase = 'N' AND LEN(@VendeurNew) > 0
BEGIN
INSERT INTO CONTACT (ContactName, ContactTypeId)
SELECT @VendeurNew
, (SELECT ContactTypeId
FROM CONTACT_TYPE
WHERE LOWER(ContactTypeShort) = @CategorieQuelconque)
END
SET @VendeurIdNew = (SELECT ContactId FROM CONTACT WHERE ContactName = @VendeurNew)
SET @CamionId = (SELECT CamionId FROM CAMION WHERE CamionVIN = @VinOld)
SET @TruckPurchaseId = (SELECT TruckId FROM TRUCK_PURCHASE WHERE TruckId = @CamionId)
SET @TruckContactPrecedentId = (SELECT ContactId FROM TRUCK_PURCHASE WHERE TruckId = @CamionId)
-----------------------------------------------------
-- On insère dans TRUCK_PURCHASE ou on modifie
-- y remplace le contact s'il y en a déjà un.
-----------------------------------------------------
IF @VendeurNew IS NOT NULL AND LEN(@VendeurNew) > 0
BEGIN
IF @TruckPurchaseId IS NULL
BEGIN
---------------------------------------------
-- Insert paire TruckId, ContactId
-- dans TRUCK_PURCHASE (TruckId, ContactId)
---------------------------------------------
INSERT INTO TRUCK_PURCHASE (TruckId, ContactId)
SELECT @CamionId, @VendeurIdNew
END
ELSE
BEGIN
UPDATE TRUCK_PURCHASE
SET ContactId = @VendeurIdNew
WHERE TruckId = @CamionId
END
END
---- pour debug ----select '' as 'CONTACT après upd', * from CONTACT where Contacttypeid = 4
---- pour debug ----select '' as 'TRUCK_PURCHASE aaprès upd', * from TRUCK_PURCHASE
---- pour debug ---- select @VendeurIdNew as '@VendeurIdNew'
---- pour debug ---- select @CamionId as '@CamionId'
END
------------------------------------------------------------------------
-- Prise en compte des changements de VIN
------------------------------------------------------------------------
IF @RemplacerCamionVIN = 'Y'
BEGIN
----pour debug : ---- SELECT '' as 'on tente le new', @VinNew
UPDATE CAMION
SET CamionVIN = @VinNew
WHERE CamionVIN = @VinOld
----WHERE EXISTS (SELECT *
---- FROM DELETED
---- WHERE VIN = CAMION.CamionVIN)
;
END
------------------------------------------------------------------------
-- On en termine avec les notes
------------------------------------------------------------------------
IF UPDATE(Note)
BEGIN
UPDATE LOCALISATION
SET LocalisationNote = (SELECT Note FROM INSERTED)
WHERE LocalisationId = @LocalisationId
;
END |