facturacionelectronicapy-ts-xmlgen
Version:
Genera el contenido del archivo XML del Documento electrónico exigido por la SET
101 lines (100 loc) • 4.03 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NotificationEventSchema = void 0;
const zod_1 = require("zod");
const CommonValidators_1 = __importDefault(require("../../helpers/validation/CommonValidators"));
const idDocsCarriers_table_1 = require("../../data/idDocsCarriers.table");
const NumberLenght_1 = __importDefault(require("../../helpers/validation/NumberLenght"));
const taxpayerNotTaxpayer_table_1 = require("../../data/taxpayerNotTaxpayer.table");
const ZodValidator_1 = __importDefault(require("../../helpers/validation/ZodValidator"));
const Path_1 = require("../../helpers/Path");
const SDParser_1 = __importDefault(require("../../helpers/SDParser"));
// GEN001
exports.NotificationEventSchema = zod_1.z
.object({
// GEN002
cdc: CommonValidators_1.default.cdc().describe(SDParser_1.default.stringify('GEN002')),
// GEN003
fechaEmision: CommonValidators_1.default.isoDateTime().describe(SDParser_1.default.stringify('GEN003')),
// GEN004
fechaRecepcion: CommonValidators_1.default.isoDateTime().describe(SDParser_1.default.stringify('GEN004')),
// GEN005
contribuyente: CommonValidators_1.default.taxpayer().describe(SDParser_1.default.stringify('GEN005')),
// GEN006
nombre: CommonValidators_1.default.name().describe(SDParser_1.default.stringify('GEN006')),
// para obtener GEN007 y GEN008
ruc: CommonValidators_1.default.ruc()
.optional()
.describe(SDParser_1.default.stringify('GEN007 y GEN008')),
// GEN009
documentoTipo: zod_1.z
.nativeEnum(idDocsCarriers_table_1.IdentityDocumentCarrier)
.optional()
.describe(SDParser_1.default.stringify('GEN009', { e: 'IdentityDocumentCarrier' })),
// GEN010
documentoNumero: CommonValidators_1.default.identityDocNumber()
.optional()
.describe(SDParser_1.default.stringify('GEN010')),
// GEN011
totalPYG: zod_1.z
.number()
.superRefine((value, ctx) => {
new NumberLenght_1.default(value, ctx).max(15).maxDecimals(8);
})
.describe(SDParser_1.default.stringify('GEN011')),
})
.transform((data, ctx) => {
const validator = new ZodValidator_1.default(ctx, data);
// ⚠️ esto no esta en el manual
{
const emission = new Date(data.fechaEmision);
const reception = new Date(data.fechaRecepcion);
const receptionDatePath = new Path_1.Path('fechaRecepcion');
validator.validate('fechaEmision', emission > reception, `$path no puede ser después de '${receptionDatePath}'`);
}
/**GEN005 = 1 */
const isTaxpayer = data.contribuyente == taxpayerNotTaxpayer_table_1.TaxpayerNotTaxpayer.CONTRIBUYENTE;
/**GEN005 = 2 */
const isNotTaxpayer = data.contribuyente == taxpayerNotTaxpayer_table_1.TaxpayerNotTaxpayer.NO_CONTRIBUYENTE;
// GEN007 y GEN008 - ruc
{
/*
Requerido solo cuando el tipo de receptor
es Contribuyente (GEN005=1)
No Informar si GEN005=2
*/
if (isTaxpayer) {
validator.requiredField('ruc');
}
else if (isNotTaxpayer) {
validator.undesiredField('ruc');
}
}
// GEN009 y GEN010 - documentoTipo y documentoNumero
{
/*
No Informar si GEN005=1
Requerido solo cuando el tipo de receptor
es No Contribuyente (GEN005=2)
*/
if (isNotTaxpayer) {
validator.requiredField('documentoTipo');
validator.requiredField('documentoNumero');
}
else if (isTaxpayer) {
validator.undesiredField('documentoTipo');
validator.undesiredField('documentoNumero');
}
}
const [rucID, rucDV] = data.ruc?.split('-') ?? [];
return {
...data,
// GEN007
rucID: rucID,
// GEN008
rucDV: rucDV,
};
});