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
| declare @idSession varchar(50)='N34L100177' --en test
declare @lettrage varchar(3)
declare @num int=1
declare @ct_num varchar(17)
declare @ct_numOLD varchar(17)=''
declare @ct_numEnCours varchar(17)=''
declare @jm_date smalldatetime
declare @nbLigne int
declare @annee int
declare @listeECNO varchar(max)
declare @req varchar(max)
declare @DernierEcNo int
declare @idTypePaiement numeric(18,0)
declare @AnalytiqueEcartChange varchar(20)
declare @Montant numeric(24,6)
declare @TotalCredit numeric(38,6)
declare @TotalDebit numeric(38,6)
declare @CompteAnalytique varchar(13)
declare @EC_LETTRE smallint
declare @itermax int=(select max(numero) from ZIMPORTECRITURES where idSession=@idSession)
begin try
begin transaction RemiseBancaire
while @num<=@itermax
begin
SELECT @CompteAnalytique=CompteAnalytique, @EC_LETTRE=EC_Lettre,
@ct_num=ct_num, @annee=anneeRemise, @listeECNO=EC_NoOrigine
from ZIMPORTECRITURES
where idsession=@idSession and numero=@num
if @EC_LETTRE=0 --cette écriture ne doit pas être lettrée
begin
INSERT INTO F_ECRITUREC (JO_Num,EC_No,CG_Num,CT_Num,EC_Intitule,EC_Sens,EC_Montant,JM_Date,EC_Jour,EC_Date,EC_Echeance,
EC_RefPiece,remise_bancaire,EC_Piece,N_Devise,EC_Devise,
EC_NoLink,EC_TresoPiece,N_Reglement,EC_Parite,EC_Quantite,EC_Point,EC_Pointage,EC_Impression,
EC_Cloture,EC_CType,EC_Rappel,EC_LettreQ,EC_LettrageQ,EC_ANType,EC_RType,EC_Remise,EC_ExportExpert,
EC_Norme,TA_Provenance,EC_PenalType,EC_DatePenal,EC_DateRelance,EC_DateRappro,
EC_Reference,EC_StatusRegle,EC_MontantRegle,EC_DateRegle,EC_RIB,EC_DateOp,EC_NoCloture,EC_Lettre,EC_Lettrage)
select JO_Num,0,CG_Num,CT_Num,EC_Intitule,EC_Sens,EC_Montant,JM_Date,EC_Jour,EC_Date,EC_Echeance,
EC_RefPiece,remise_bancaire,EC_Piece,N_Devise,EC_Devise,
EC_NoLink,EC_TresoPiece,N_Reglement,EC_Parite,EC_Quantite,EC_Point,EC_Pointage,EC_Impression,
EC_Cloture,EC_CType,EC_Rappel,EC_LettreQ,EC_LettrageQ,EC_ANType,EC_RType,EC_Remise,EC_ExportExpert,
EC_Norme,TA_Provenance,EC_PenalType,EC_DatePenal,EC_DateRelance,EC_DateRappro,
EC_Reference,EC_StatusRegle,EC_MontantRegle,EC_DateRegle,EC_RIB,EC_DateOp,EC_NoCloture,0,''
FROM ZIMPORTECRITURES
where idsession=@idSession and numero=@num
end
else
begin
set @ct_numEnCours=@ct_num
--on met à jour les infos libre SAGE des écritures comptables
set @req='UPDATE F_ECRITUREC SET Remise_bancaire=''@idsession@'', ecritureOrigine=1.0 WHERE EC_NO IN (@ecno@)'
set @req=replace(@req,'@idsession@',@idSession)
set @req=replace(@req,'@ecno@',@listeECNO)
exec(@req)
--on met à jour le lettrage si on change de tiers
if @ct_numOLD<>@ct_numEnCours
begin
set @ct_numOLD=@ct_numEnCours
set @lettrage=[dbo].[z_nouveauLettrage](@ct_numEnCours,@annee)
end
--mise à jour des codes de lettrages SAGE
update F_ECRITUREC set ec_lettre=1, ec_lettrage=@lettrage
where Remise_bancaire=@idSession and ct_num=@ct_numEnCours
--inserer la nouvelle ligne dans SAGE
INSERT INTO F_ECRITUREC (JO_Num,EC_No,CG_Num,CT_Num,EC_Intitule,EC_Sens,EC_Montant,JM_Date,EC_Jour,EC_Date,EC_Echeance,
EC_RefPiece,remise_bancaire,EC_Piece,N_Devise,EC_Devise,
EC_NoLink,EC_TresoPiece,N_Reglement,EC_Parite,EC_Quantite,EC_Point,EC_Pointage,EC_Impression,
EC_Cloture,EC_CType,EC_Rappel,EC_LettreQ,EC_LettrageQ,EC_ANType,EC_RType,EC_Remise,EC_ExportExpert,
EC_Norme,TA_Provenance,EC_PenalType,EC_DatePenal,EC_DateRelance,EC_DateRappro,
EC_Reference,EC_StatusRegle,EC_MontantRegle,EC_DateRegle,EC_RIB,EC_DateOp,EC_NoCloture,EC_Lettre,EC_Lettrage)
select JO_Num,0,CG_Num,CT_Num,EC_Intitule,EC_Sens,EC_Montant,JM_Date,EC_Jour,EC_Date,EC_Echeance,
EC_RefPiece,remise_bancaire,EC_Piece,N_Devise,EC_Devise,
EC_NoLink,EC_TresoPiece,N_Reglement,EC_Parite,EC_Quantite,EC_Point,EC_Pointage,EC_Impression,
EC_Cloture,EC_CType,EC_Rappel,EC_LettreQ,EC_LettrageQ,EC_ANType,EC_RType,EC_Remise,EC_ExportExpert,
EC_Norme,TA_Provenance,EC_PenalType,EC_DatePenal,EC_DateRelance,EC_DateRappro,
EC_Reference,EC_StatusRegle,EC_MontantRegle,EC_DateRegle,EC_RIB,EC_DateOp,EC_NoCloture,1,@lettrage
FROM ZIMPORTECRITURES
where idsession=@idSession and numero=@num
end --if
set @num=@num+1
end --while
--statut:
--0 : non traité
--1 : traité
--2 : echec
--3 : Supprimé
update ZSESSIONIMPORTECRITURES set statut=1 where idSession=@idSession
--test de cohérence : on doit avoit Total débit = Total crédit
set @TotalDebit=(
select sum(convert(numeric(38,6),ec_montant))
from F_ECRITUREC
where Remise_bancaire=@idSession
and ec_sens=0
)
set @TotalCredit=(
select sum(convert(numeric(38,6),ec_montant))
from F_ECRITUREC
where Remise_bancaire=@idSession
and ec_sens=1
)
if @TotalDebit=@TotalCredit --la remise a été correctement générée
begin
commit transaction RemiseBancaire
select 0,'ok'
end
else
begin
declare @Message varchar(max)
set @Message='An error occured in the generation of your delivery, total debit amount=@1, total credit amount=@2'
set @Message=replace(@Message,'@1',@TotalDebit)
set @Message=replace(@Message,'@2',@TotalCredit)
raiserror(@Message, 16, 1)
end
end try
begin catch
select 99, ERROR_MESSAGE();
rollback transaction RemiseBancaire
update ZSESSIONIMPORTECRITURES set statut=2 where idSession=@idSession
end catch |
Partager