@@ -20,6 +20,9 @@ public class IbsCbsUtil {
2020 private static final String TOTAL_IBS_UF = "TOTAL_IBS_UF" ;
2121 private static final String TOTAL_IBS_MUN = "TOTAL_IBS_MUN" ;
2222 private static final String TOTAL_CBS = "TOTAL_CBS" ;
23+ private static final String TOTAL_DIFERIMENTO_IBS_UF = "TOTAL_DIFERIMENTO_IBS_UF" ;
24+ private static final String TOTAL_DIFERIMENTO_IBS_MUN = "TOTAL_DIFERIMENTO_IBS_MUN" ;
25+ private static final String TOTAL_DIFERIMENTO_CBS = "TOTAL_DIFERIMENTO_CBS" ;
2326 private static final BigDecimal CEM = BigDecimal .valueOf (100 );
2427 private static final int SCALE_5 = 5 ;
2528
@@ -35,6 +38,7 @@ public class IbsCbsUtil {
3538 private BigDecimal pAliqIbsUf = new BigDecimal ("0.1" );
3639 private BigDecimal pAliqIbsMun = BigDecimal .ZERO ;
3740 private BigDecimal pAliqCbs = new BigDecimal ("0.9" );
41+ private BigDecimal pAliqDiferimento = BigDecimal .ZERO ;
3842 private BigDecimal baseCalculo = BigDecimal .ZERO ;
3943
4044 public void setpAliqIbsUf (BigDecimal pAliqIbsUf ) {
@@ -49,6 +53,10 @@ public void setpAliqCbs(BigDecimal pAliqCbs) {
4953 this .pAliqCbs = pAliqCbs ;
5054 }
5155
56+ public void setpAliqDiferimento (BigDecimal pAliqDiferimento ) {
57+ this .pAliqDiferimento = pAliqDiferimento ;
58+ }
59+
5260 public IbsCbsUtil (@ NonNull List <CstDTO > listaCstIbsCbs , @ NonNull DocumentoEnum documento ) {
5361 this .listaCstIbsCbs = listaCstIbsCbs ;
5462 this .documento = documento ;
@@ -66,6 +74,9 @@ private void inicializarTotais() {
6674 mapTotais .put (TOTAL_IBS_UF , BigDecimal .ZERO );
6775 mapTotais .put (TOTAL_IBS_MUN , BigDecimal .ZERO );
6876 mapTotais .put (TOTAL_CBS , BigDecimal .ZERO );
77+ mapTotais .put (TOTAL_DIFERIMENTO_IBS_UF , BigDecimal .ZERO );
78+ mapTotais .put (TOTAL_DIFERIMENTO_IBS_MUN , BigDecimal .ZERO );
79+ mapTotais .put (TOTAL_DIFERIMENTO_CBS , BigDecimal .ZERO );
6980 }
7081
7182 public TTribNFe montaImpostosDet (String cclassTrib , TNFe .InfNFe .Det det ) throws NfeException {
@@ -94,7 +105,7 @@ public TTribNFe montaImpostosDet(String cclassTrib, TNFe.InfNFe.Det det, String
94105
95106 private TMonofasia montaGrupoMono (TNFe .InfNFe .Det det ) {
96107 TMonofasia gMono = new TMonofasia ();
97- if (Boolean .TRUE .equals (classTribIbsCbs .getMonofasiaPadrao ())) {
108+ if (Boolean .TRUE .equals (classTribIbsCbs .getMonofasiaPadrao ())) {
98109 TMonofasia .GMonoPadrao monoPadrao = new TMonofasia .GMonoPadrao ();
99110 monoPadrao .setQBCMono (ObjetoUtil .getValor4Casas (new BigDecimal (det .getProd ().getQCom ())));
100111 monoPadrao .setAdRemIBS ("0.00" );
@@ -104,7 +115,7 @@ private TMonofasia montaGrupoMono(TNFe.InfNFe.Det det) {
104115 gMono .setGMonoPadrao (monoPadrao );
105116 }
106117
107- if (Boolean .TRUE .equals (classTribIbsCbs .getMonofasiaRetidaAnt ())) {
118+ if (Boolean .TRUE .equals (classTribIbsCbs .getMonofasiaRetidaAnt ())) {
108119 TMonofasia .GMonoRet monoRet = new TMonofasia .GMonoRet ();
109120 monoRet .setQBCMonoRet (ObjetoUtil .getValor4Casas (new BigDecimal (det .getProd ().getQCom ())));
110121 monoRet .setAdRemCBSRet ("0.00" );
@@ -114,7 +125,7 @@ private TMonofasia montaGrupoMono(TNFe.InfNFe.Det det) {
114125 gMono .setGMonoRet (monoRet );
115126 }
116127
117- if (Boolean .TRUE .equals (classTribIbsCbs .getMonofasiaSujeitaRetencao ())) {
128+ if (Boolean .TRUE .equals (classTribIbsCbs .getMonofasiaSujeitaRetencao ())) {
118129 TMonofasia .GMonoReten monoReten = new TMonofasia .GMonoReten ();
119130 monoReten .setQBCMonoReten (ObjetoUtil .getValor4Casas (new BigDecimal (det .getProd ().getQCom ())));
120131 monoReten .setAdRemCBSReten ("0.00" );
@@ -124,7 +135,7 @@ private TMonofasia montaGrupoMono(TNFe.InfNFe.Det det) {
124135 gMono .setGMonoReten (monoReten );
125136 }
126137
127- if (Boolean .TRUE .equals (classTribIbsCbs .getMonofasiaDiferimento ())) {
138+ if (Boolean .TRUE .equals (classTribIbsCbs .getMonofasiaDiferimento ())) {
128139 TMonofasia .GMonoDif gMonoDif = new TMonofasia .GMonoDif ();
129140 gMonoDif .setPDifCBS ("0.00" );
130141 gMonoDif .setPDifIBS ("0.00" );
@@ -141,9 +152,9 @@ private TMonofasia montaGrupoMono(TNFe.InfNFe.Det det) {
141152
142153 private boolean deveMontarGrupoIBSCBS () {
143154 return Boolean .TRUE .equals (cstIbsCbs .getIndIBSCBS ())
144- || Boolean .TRUE .equals (cstIbsCbs .getIndRedAliq ())
145- || Boolean .TRUE .equals (cstIbsCbs .getIndDif ())
146- || Boolean .TRUE .equals (cstIbsCbs .getIndTransfCred ());
155+ || Boolean .TRUE .equals (cstIbsCbs .getIndRedAliq ())
156+ || Boolean .TRUE .equals (cstIbsCbs .getIndDif ())
157+ || Boolean .TRUE .equals (cstIbsCbs .getIndTransfCred ());
147158 }
148159
149160 private TCIBS montarGrupoIBSCBS () {
@@ -173,6 +184,15 @@ private void atualizarTotais(TCIBS.GIBSUF gIBSUF, TCIBS.GIBSMun gIBSMun, TCIBS.G
173184 mapTotais .merge (TOTAL_IBS_UF , new BigDecimal (gIBSUF .getVIBSUF ()), BigDecimal ::add );
174185 mapTotais .merge (TOTAL_IBS_MUN , new BigDecimal (gIBSMun .getVIBSMun ()), BigDecimal ::add );
175186 mapTotais .merge (TOTAL_CBS , new BigDecimal (gCBS .getVCBS ()), BigDecimal ::add );
187+ if (gIBSUF .getGDif () != null ){
188+ mapTotais .merge (TOTAL_DIFERIMENTO_IBS_UF , new BigDecimal (gIBSUF .getGDif ().getVDif ()), BigDecimal ::add );
189+ }
190+ if (gIBSMun .getGDif () != null ){
191+ mapTotais .merge (TOTAL_DIFERIMENTO_IBS_MUN , new BigDecimal (gIBSMun .getGDif ().getVDif ()), BigDecimal ::add );
192+ }
193+ if (gCBS .getGDif () != null ){
194+ mapTotais .merge (TOTAL_DIFERIMENTO_CBS , new BigDecimal (gCBS .getGDif ().getVDif ()), BigDecimal ::add );
195+ }
176196 }
177197
178198 private void filtraCClasstrib (String cclassTrib , String cclassTribRegular ) {
@@ -218,9 +238,11 @@ private void validaClassTrib(String cclassTrib) throws NfeException {
218238 private TCIBS .GIBSUF criarGIBSUF () {
219239 return criarGrupoImposto (
220240 pAliqIbsUf ,
241+ pAliqDiferimento ,
221242 classTribIbsCbs .getPRedIBS (),
222243 TCIBS .GIBSUF ::new ,
223244 TCIBS .GIBSUF ::setPIBSUF ,
245+ TCIBS .GIBSUF ::setGDif ,
224246 TCIBS .GIBSUF ::setGRed ,
225247 TCIBS .GIBSUF ::setVIBSUF
226248 );
@@ -229,9 +251,11 @@ private TCIBS.GIBSUF criarGIBSUF() {
229251 private TCIBS .GIBSMun criarGIBSMun () {
230252 return criarGrupoImposto (
231253 pAliqIbsMun ,
254+ pAliqDiferimento ,
232255 classTribIbsCbs .getPRedIBS (),
233256 TCIBS .GIBSMun ::new ,
234257 TCIBS .GIBSMun ::setPIBSMun ,
258+ TCIBS .GIBSMun ::setGDif ,
235259 TCIBS .GIBSMun ::setGRed ,
236260 TCIBS .GIBSMun ::setVIBSMun
237261 );
@@ -240,9 +264,11 @@ private TCIBS.GIBSMun criarGIBSMun() {
240264 private TCIBS .GCBS criarGCBS () {
241265 return criarGrupoImposto (
242266 pAliqCbs ,
267+ pAliqDiferimento ,
243268 classTribIbsCbs .getPRedCBS (),
244269 TCIBS .GCBS ::new ,
245270 TCIBS .GCBS ::setPCBS ,
271+ TCIBS .GCBS ::setGDif ,
246272 TCIBS .GCBS ::setGRed ,
247273 TCIBS .GCBS ::setVCBS
248274 );
@@ -263,16 +289,23 @@ private interface RedutorSetter<T> {
263289 void set (T grupo , TRed redutor );
264290 }
265291
292+ @ FunctionalInterface
293+ private interface DifererimentoSetter <T > {
294+ void set (T grupo , TDif diferimento );
295+ }
296+
266297 @ FunctionalInterface
267298 private interface ValorSetter <T > {
268299 void set (T grupo , String valor );
269300 }
270301
271302 private <T > T criarGrupoImposto (
272303 BigDecimal aliqPadrao ,
304+ BigDecimal percentualDiferimento ,
273305 BigDecimal percentualReducao ,
274306 GrupoImpostoFactory <T > factory ,
275307 AliquotaSetter <T > aliqSetter ,
308+ DifererimentoSetter <T > difSetter ,
276309 RedutorSetter <T > redSetter ,
277310 ValorSetter <T > valorSetter ) {
278311
@@ -283,16 +316,25 @@ private <T> T criarGrupoImposto(
283316
284317 aliqSetter .set (grupo , ObjetoUtil .getValor4Casas (aliq ));
285318
286- BigDecimal percentRed = ObjetoUtil .getOrZero (percentualReducao );
287319 BigDecimal aliqEfet = aliq ;
288320
289321 if (Boolean .TRUE .equals (cstIbsCbs .getIndRedAliq ())) {
322+ BigDecimal percentRed = ObjetoUtil .getOrZero (percentualReducao );
290323 TRed gRed = criarRedutor (percentRed , aliq );
291324 redSetter .set (grupo , gRed );
292325 aliqEfet = new BigDecimal (gRed .getPAliqEfet ());
293326 }
294327
295328 BigDecimal valor = calcularValorImposto (aliqEfet );
329+
330+ if (Boolean .TRUE .equals (cstIbsCbs .getIndDif ())) {
331+ BigDecimal percentDif = ObjetoUtil .getOrZero (percentualDiferimento );
332+ TDif gDif = criarDiferimento (percentDif , aliqEfet );
333+ difSetter .set (grupo , gDif );
334+ valor = valor .subtract (new BigDecimal (gDif .getVDif ()));
335+ }
336+
337+
296338 valorSetter .set (grupo , ObjetoUtil .getValor2Casas (valor ));
297339
298340 return grupo ;
@@ -341,6 +383,14 @@ private static TRed criarRedutor(BigDecimal percentualReducao, BigDecimal aliqOr
341383 return gRed ;
342384 }
343385
386+ private TDif criarDiferimento (BigDecimal percentualDiferimento , BigDecimal aliqEfet ) {
387+ TDif gDif = new TDif ();
388+ gDif .setPDif (ObjetoUtil .getValor4Casas (percentualDiferimento ));
389+ BigDecimal valorDif = calcularValorImposto (percentualDiferimento ).multiply (aliqEfet .divide (CEM , SCALE_5 , RoundingMode .HALF_UP ));
390+ gDif .setVDif (ObjetoUtil .getValor2Casas (valorDif ));
391+ return gDif ;
392+ }
393+
344394 @ SuppressWarnings ("unchecked" )
345395 private void calcularBaseCalculoIBSCBS (TNFe .InfNFe .Det det ) {
346396 BigDecimal vProd = ObjetoUtil .getBigDecimalOrZero (det .getProd ().getVProd ());
@@ -397,23 +447,23 @@ private TIBSCBSMonoTot.GIBS criarTotaisIBS() {
397447
398448 private TIBSCBSMonoTot .GIBS .GIBSUF criarGIBSUFTotal () {
399449 TIBSCBSMonoTot .GIBS .GIBSUF gIbsUF = new TIBSCBSMonoTot .GIBS .GIBSUF ();
400- gIbsUF .setVDif ("0.00" );
450+ gIbsUF .setVDif (ObjetoUtil . getValor2Casas ( mapTotais . getOrDefault ( TOTAL_DIFERIMENTO_IBS_UF , BigDecimal . ZERO )) );
401451 gIbsUF .setVDevTrib ("0.00" );
402452 gIbsUF .setVIBSUF (ObjetoUtil .getValor2Casas (mapTotais .getOrDefault (TOTAL_IBS_UF , BigDecimal .ZERO )));
403453 return gIbsUF ;
404454 }
405455
406456 private TIBSCBSMonoTot .GIBS .GIBSMun criarGIBSMunTotal () {
407457 TIBSCBSMonoTot .GIBS .GIBSMun gIbsMun = new TIBSCBSMonoTot .GIBS .GIBSMun ();
408- gIbsMun .setVDif ("0.00" );
458+ gIbsMun .setVDif (ObjetoUtil . getValor2Casas ( mapTotais . getOrDefault ( TOTAL_DIFERIMENTO_IBS_MUN , BigDecimal . ZERO )) );
409459 gIbsMun .setVDevTrib ("0.00" );
410460 gIbsMun .setVIBSMun (ObjetoUtil .getValor2Casas (mapTotais .getOrDefault (TOTAL_IBS_MUN , BigDecimal .ZERO )));
411461 return gIbsMun ;
412462 }
413463
414464 private TIBSCBSMonoTot .GCBS criarTotaisCBS () {
415465 TIBSCBSMonoTot .GCBS gCbs = new TIBSCBSMonoTot .GCBS ();
416- gCbs .setVDif ("0.00" );
466+ gCbs .setVDif (ObjetoUtil . getValor2Casas ( mapTotais . getOrDefault ( TOTAL_DIFERIMENTO_CBS , BigDecimal . ZERO )) );
417467 gCbs .setVDevTrib ("0.00" );
418468 gCbs .setVCBS (ObjetoUtil .getValor2Casas (mapTotais .getOrDefault (TOTAL_CBS , BigDecimal .ZERO )));
419469 gCbs .setVCredPres ("0.00" );
0 commit comments