UNPKG

facturacionelectronicapy-ts-xmlgen

Version:

Genera el contenido del archivo XML del Documento electrónico exigido por la SET

310 lines (309 loc) 12.2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TransportUpdateEventSchema = void 0; const zod_1 = require("zod"); const CommonValidators_1 = __importDefault(require("../../helpers/validation/CommonValidators")); const ZodValidator_1 = __importDefault(require("../../helpers/validation/ZodValidator")); const db_service_1 = __importDefault(require("../../services/db.service")); const idDocsCarriers_table_1 = require("../../data/idDocsCarriers.table"); const transportTypes_table_1 = require("../../data/transportTypes.table"); const transportModalities_table_1 = require("../../data/transportModalities.table"); const vehicleIdentifications_table_1 = require("../../data/vehicleIdentifications.table"); const Path_1 = require("../../helpers/Path"); const taxpayerNotTaxpayer_table_1 = require("../../data/taxpayerNotTaxpayer.table"); const SDParser_1 = __importDefault(require("../../helpers/SDParser")); const TransporteUpdateMotive_1 = require("../../types/TransporteUpdateMotive"); // GET001 exports.TransportUpdateEventSchema = zod_1.z .object({ // GET002 cdc: CommonValidators_1.default.cdc().describe(SDParser_1.default.stringify('GET002')), // GET003 motivo: zod_1.z .nativeEnum(TransporteUpdateMotive_1.TransportUpdateMotive) .describe(SDParser_1.default.stringify('GET003', { e: 'TransportUpdateMotive' })), // GET004 - GET030 (excluyendo GET013 - GET021) entrega: zod_1.z .object({ // GET004 departamento: CommonValidators_1.default.department() .optional() .describe(SDParser_1.default.stringify('GET004', { e: 'Department' })), // GET006 distrito: CommonValidators_1.default.district() .optional() .describe(SDParser_1.default.stringify('GET006')), // GET008 ciudad: CommonValidators_1.default.city() .optional() .describe(SDParser_1.default.stringify('GET008')), // GET010 direccion: CommonValidators_1.default.address() .optional() .describe(SDParser_1.default.stringify('GET010')), // GET011 numeroCasa: CommonValidators_1.default.houseNumber() .optional() .describe(SDParser_1.default.stringify('GET011')), // GET012 direccionComplementaria1: CommonValidators_1.default.address() .optional() .describe(SDParser_1.default.stringify('GET012')), // GET022 tipoTransporte: zod_1.z .nativeEnum(transportTypes_table_1.TransportType) .optional() .describe(SDParser_1.default.stringify('GET022', { e: 'TransportType' })), // GET024 modalidadTransporte: zod_1.z .nativeEnum(transportModalities_table_1.TransportModality) .optional() .describe(SDParser_1.default.stringify('GET024', { e: 'TransportModality' })), // GET026 - GET030 vehiculo: zod_1.z .object({ // GET026 tipo: zod_1.z .string() .min(4) .max(10) .optional() .describe(SDParser_1.default.stringify('GET026')), // GET027 marca: zod_1.z .string() .min(1) .max(10) .optional() .describe(SDParser_1.default.stringify('GET027')), // GET028 documentoTipo: zod_1.z .nativeEnum(vehicleIdentifications_table_1.VehicleIdentification) .optional() .describe(SDParser_1.default.stringify('GET028', { e: 'VehicleIdentification' })), // GET029 documentoNumero: zod_1.z .string() .min(1) .max(20) .optional() .describe(SDParser_1.default.stringify('GET029')), // GET030 numeroMatricula: zod_1.z .string() .length(6) .optional() .describe(SDParser_1.default.stringify('GET030')), }) .describe(SDParser_1.default.stringify('GET026 - GET030')), }) .superRefine((data, ctx) => { CommonValidators_1.default.location(ctx, data.departamento, data.distrito, data.ciudad); }) .describe(SDParser_1.default.stringify('GET004 - GET030 (excluyendo GET013 - GET021)')), // GET013 - GET021 transportista: zod_1.z .object({ // GET013 - GET014 chofer: zod_1.z .object({ // GET013: TODO_NT: obligatorio SI, pero "Obligatorio si ..." debería ser opcional nombre: CommonValidators_1.default.name().describe(SDParser_1.default.stringify('GET013')), // GET014 documentoNumero: CommonValidators_1.default.identityDocNumber().describe(SDParser_1.default.stringify('GET014')), }) .optional() .describe(SDParser_1.default.stringify('GET013 - GET014')), // GET015 contribuyente: CommonValidators_1.default.taxpayer() .optional() .describe(SDParser_1.default.stringify('GET015')), // para calcular GET016 y GET017 ruc: CommonValidators_1.default.ruc() .optional() .describe(SDParser_1.default.stringify('GET016 y GET017')), // GET018 nombre: CommonValidators_1.default.name() .optional() .describe(SDParser_1.default.stringify('GET018')), // GET019 documentoTipo: zod_1.z .nativeEnum(idDocsCarriers_table_1.IdentityDocumentCarrier) .optional() .describe(SDParser_1.default.stringify('GET019', { e: 'IdentityDocumentCarrier' })), // GET021 documentoNumero: CommonValidators_1.default.identityDocNumber() .optional() .describe(SDParser_1.default.stringify('GET021')), }) .describe(SDParser_1.default.stringify('GET013 - GET021')), }) .transform((data, ctx) => { const validator = new ZodValidator_1.default(ctx, data); const deliveryPath = new Path_1.Path('entrega'); const vehiclePath = deliveryPath.concat('vehiculo'); const transporterPath = new Path_1.Path('transportista'); const driverPath = transporterPath.concat('chofer'); /**GET003 = 1 */ const isDeliveryLocationChange = data.motivo == TransporteUpdateMotive_1.TransportUpdateMotive.CAMBIO_LOCAL_ENTREGA; /**GET003 = 2 */ const isDriverChange = data.motivo == TransporteUpdateMotive_1.TransportUpdateMotive.CAMBIO_CHOFER; /**GET003 = 3 */ const isTransporterChange = data.motivo == TransporteUpdateMotive_1.TransportUpdateMotive.CAMBIO_TRANSPORTISTA; /**GET003 = 4 */ const isVehicleChange = data.motivo == TransporteUpdateMotive_1.TransportUpdateMotive.CAMBIO_VEHICULO; /**GET015 = 1 */ const isTaxpayer = data.transportista.contribuyente == taxpayerNotTaxpayer_table_1.TaxpayerNotTaxpayer.CONTRIBUYENTE; /**GET015 = 2 */ const isNotTaxpayer = data.transportista.contribuyente == taxpayerNotTaxpayer_table_1.TaxpayerNotTaxpayer.NO_CONTRIBUYENTE; // GET004, GET008, GET010, GET011 { /* Obligatorio si GET003=1 */ if (isDeliveryLocationChange) { validator.requiredField(deliveryPath.concat('departamento')); validator.requiredField(deliveryPath.concat('ciudad')); validator.requiredField(deliveryPath.concat('direccion')); validator.requiredField(deliveryPath.concat('numeroCasa')); } } // GET012 { /* TODO_TEST: asumo que en caso contrario es obligatorio o si no para que esta esta condición Opcional si GET003=1 */ if (!isDeliveryLocationChange) { validator.requiredField(deliveryPath.concat('direccionComplementaria1')); } } // GET013, GET014 { /* Obligatorio si GET003=2 */ if (isDriverChange) { validator.requiredField(driverPath); } } // GET015, GET018 { /* Obligatorio si GET003=3 */ if (isTransporterChange) { validator.requiredField(transporterPath.concat('contribuyente')); validator.requiredField(transporterPath.concat('nombre')); } } // GET016, GET017 { /* Obligatorio si GET015 = 1 No informar si GET015 ≠ 1 */ if (isTaxpayer) { validator.requiredField(transporterPath.concat('ruc')); } else { validator.undesiredField(transporterPath.concat('ruc')); } } // GET019 { /* Obligatorio si GET015 = 2 No informar si GET015 = 1 */ if (isNotTaxpayer) { validator.requiredField(transporterPath.concat('documentoTipo')); } else { validator.undesiredField(transporterPath.concat('documentoTipo')); } } // GET021 { /* Obligatorio si existe el campo GET019 */ if (data.transportista.documentoTipo) { validator.requiredField(transporterPath.concat('documentoNumero')); } } // GET022, GET024, GET026, GET027, GET028 { /* Obligatorio si GET003=4 */ if (isVehicleChange) { validator.requiredField(deliveryPath.concat('tipoTransporte')); validator.requiredField(deliveryPath.concat('modalidadTransporte')); validator.requiredField(vehiclePath.concat('tipo')); validator.requiredField(vehiclePath.concat('marca')); validator.requiredField(vehiclePath.concat('documentoTipo')); } } // GET029 { /* Debe informarse cuando el GET028=1 */ if (data.entrega.vehiculo.documentoTipo == vehicleIdentifications_table_1.VehicleIdentification.NUMERO_DE_IDENTIFICACION_DEL_VEHICULO) { validator.requiredField(vehiclePath.concat('documentoNumero')); } } // GET030 { /* Debe informarse cuando el GET028=2 */ if (data.entrega.vehiculo.documentoTipo == vehicleIdentifications_table_1.VehicleIdentification.NUMERO_DE_MATRICULA_DEL_VEHICULO) { validator.requiredField(vehiclePath.concat('numeroMatricula')); } } const entrega = data.entrega; const transportista = data.transportista; const chofer = transportista.chofer; const vehiculo = entrega.vehiculo; const [rucID, rucDV] = transportista.ruc?.split('-') ?? []; return { ...data, entrega: { ...entrega, // GET005 descripcionDepartamento: db_service_1.default.departments._findByIdIfExist(entrega.departamento)?.description, // GET007 descripcionDistrito: db_service_1.default.districts._findByIdIfExist(entrega.distrito)?.description, // GET009 descripcionCiudad: db_service_1.default.cities._findByIdIfExist(entrega.ciudad) ?.description, // GET023 descripcionTipoTransporte: db_service_1.default.transportTypes._findByIdIfExist(data.entrega.tipoTransporte)?.description, // GET025 descripcionModalidadTransporte: db_service_1.default.transportModalities._findByIdIfExist(data.entrega.modalidadTransporte)?.description, vehiculo: { ...vehiculo, }, }, transportista: { ...transportista, // GET016 rucID: rucID, // GET017 rucDV: rucDV, // GET020 descripcionTipoDocumento: db_service_1.default.idDocsCarriers._findByIdIfExist(transportista.documentoTipo)?.description, chofer: { ...chofer, }, }, }; });