1717# TODO: precisa tratar
1818from lxml import etree
1919from nfelib .cte .bindings .v4_0 .cte_v4_00 import Cte
20+ from nfelib .cte .bindings .v4_0 .dfe_tipos_basicos_v1_00 import Tcibs as CTeTcibs
21+ from nfelib .cte .bindings .v4_0 .dfe_tipos_basicos_v1_00 import TtribCte
2022from nfelib .cte .bindings .v4_0 .proc_cte_v4_00 import CteProc
2123
2224# TODO: precisa tratar nfelib
@@ -734,6 +736,92 @@ def _export_fields_cte_40_timp(self, xsd_fields, class_obj, export_dict):
734736 }
735737 export_dict [icms_tag .upper ()] = icms_binding (** sliced_icms_dict )
736738
739+ cte40_vBCIBSCBS = fields .Monetary (
740+ string = "Total Base de Calculo IBS/CBS" ,
741+ compute = "_compute_cte40_ibscbs_fields" ,
742+ )
743+ cte40_vIBS = fields .Monetary (
744+ string = "Valor total do IBS" ,
745+ compute = "_compute_cte40_ibscbs_fields" ,
746+ )
747+ cte40_vIBSUF = fields .Monetary (
748+ string = "Valor total do IBS Estadual" ,
749+ compute = "_compute_cte40_ibscbs_fields" ,
750+ )
751+ cte40_vIBSMun = fields .Monetary (
752+ string = "Valor total do IBS Municipal" ,
753+ compute = "_compute_cte40_ibscbs_fields" ,
754+ )
755+ cte40_vCBS = fields .Monetary (
756+ string = "Valor total da CBS" ,
757+ compute = "_compute_cte40_ibscbs_fields" ,
758+ )
759+ cte40_pIBSUF = fields .Float (
760+ string = "Aliquota IBS UF" ,
761+ digits = (3 , 2 ),
762+ compute = "_compute_cte40_ibscbs_fields" ,
763+ )
764+ cte40_pIBSMun = fields .Float (
765+ string = "Aliquota IBS Municipal" ,
766+ digits = (3 , 2 ),
767+ compute = "_compute_cte40_ibscbs_fields" ,
768+ )
769+ cte40_pCBS = fields .Float (
770+ string = "Aliquota CBS" ,
771+ digits = (3 , 2 ),
772+ compute = "_compute_cte40_ibscbs_fields" ,
773+ )
774+
775+ @api .depends (
776+ "fiscal_line_ids.ibs_base" ,
777+ "fiscal_line_ids.cbs_base" ,
778+ "fiscal_line_ids.price_gross" ,
779+ "fiscal_line_ids.ibs_value" ,
780+ "fiscal_line_ids.cbs_value" ,
781+ "fiscal_line_ids.ibs_percent" ,
782+ "fiscal_line_ids.cbs_percent" ,
783+ )
784+ def _compute_cte40_ibscbs_fields (self ):
785+ """Compute CT-e IBS/CBS totals from fiscal lines."""
786+ for record in self :
787+ lines = record .fiscal_line_ids
788+ if not lines :
789+ record .cte40_vBCIBSCBS = 0.0
790+ record .cte40_vIBS = 0.0
791+ record .cte40_vIBSUF = 0.0
792+ record .cte40_vIBSMun = 0.0
793+ record .cte40_vCBS = 0.0
794+ record .cte40_pIBSUF = 0.0
795+ record .cte40_pIBSMun = 0.0
796+ record .cte40_pCBS = 0.0
797+ continue
798+
799+ first = lines [0 ]
800+ total_ibs = sum (lines .mapped ("ibs_value" ))
801+ total_cbs = sum (lines .mapped ("cbs_value" ))
802+ v_bc = sum (
803+ (line .ibs_base or line .cbs_base or line .price_gross or 0.0 )
804+ for line in lines
805+ )
806+ p_ibs_uf = first .ibs_percent or 0.0
807+ p_ibs_mun = 0.0
808+ p_cbs = first .cbs_percent or 0.0
809+
810+ v_ibs_uf = total_ibs or (
811+ v_bc * p_ibs_uf / 100 if p_ibs_uf and v_bc else 0.0
812+ )
813+ v_ibs_mun = 0.0
814+ v_cbs = total_cbs or (v_bc * p_cbs / 100 if p_cbs and v_bc else 0.0 )
815+
816+ record .cte40_vBCIBSCBS = v_bc
817+ record .cte40_vIBSUF = v_ibs_uf
818+ record .cte40_vIBSMun = v_ibs_mun
819+ record .cte40_vIBS = v_ibs_uf + v_ibs_mun
820+ record .cte40_vCBS = v_cbs
821+ record .cte40_pIBSUF = p_ibs_uf
822+ record .cte40_pIBSMun = p_ibs_mun
823+ record .cte40_pCBS = p_cbs
824+
737825 # ##########################
738826 # # CT-e tag: ICMSUFFim
739827 # ##########################
@@ -1208,6 +1296,45 @@ def _export_field(self, xsd_field, class_obj, member_spec, export_value=None):
12081296 return False
12091297 elif xsd_field == "cte40_toma3" and self .cte40_choice_toma == "cte40_toma4" :
12101298 return False
1299+ if xsd_field == "cte40_IBSCBS" :
1300+ if not (self .cte40_vIBS or self .cte40_vCBS ):
1301+ return False
1302+ lines = self .fiscal_line_ids
1303+ first = lines [0 ]
1304+ c_class_trib = (
1305+ first .tax_classification_id .code .zfill (6 )
1306+ if first .tax_classification_id and first .tax_classification_id .code
1307+ else "000001"
1308+ )
1309+ cst = (
1310+ first .ibs_cst_id .code
1311+ if first .ibs_cst_id and first .ibs_cst_id .code
1312+ else (
1313+ first .cbs_cst_id .code
1314+ if first .cbs_cst_id and first .cbs_cst_id .code
1315+ else "000"
1316+ )
1317+ )
1318+ return TtribCte (
1319+ CST = cst ,
1320+ cClassTrib = c_class_trib ,
1321+ gIBSCBS = CTeTcibs (
1322+ vBC = f"{ self .cte40_vBCIBSCBS :.2f} " ,
1323+ gIBSUF = CTeTcibs .GIbsuf (
1324+ pIBSUF = f"{ self .cte40_pIBSUF :.4f} " ,
1325+ vIBSUF = f"{ self .cte40_vIBSUF :.2f} " ,
1326+ ),
1327+ gIBSMun = CTeTcibs .GIbsmun (
1328+ pIBSMun = f"{ self .cte40_pIBSMun :.4f} " ,
1329+ vIBSMun = f"{ self .cte40_vIBSMun :.2f} " ,
1330+ ),
1331+ vIBS = f"{ self .cte40_vIBS :.2f} " ,
1332+ gCBS = CTeTcibs .GCbs (
1333+ pCBS = f"{ self .cte40_pCBS :.4f} " ,
1334+ vCBS = f"{ self .cte40_vCBS :.2f} " ,
1335+ ),
1336+ ),
1337+ )
12111338 return super ()._export_field (xsd_field , class_obj , member_spec , export_value )
12121339
12131340 def _export_many2one (self , field_name , xsd_required , class_obj = None ):
@@ -1222,6 +1349,10 @@ def _export_many2one(self, field_name, xsd_required, class_obj=None):
12221349 for t in self .cte40_det .mapped ("product_id.tax_icms_or_issqn" )
12231350 ):
12241351 return False
1352+ if field_name == "cte40_IBSCBS" and not (
1353+ self .cte40_vIBS or self .cte40_vCBS
1354+ ):
1355+ return False
12251356
12261357 elif (not xsd_required ) and field_name not in ["cte40_enderDest" ]:
12271358 comodel = self .env [
0 commit comments