onehr_ebarimt
Version:
Ebarimt integration package
321 lines • 11 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteVat = exports.sendData = exports.saveVat = exports.getPostDataContained = exports.getPostData = exports.getCompanyInfo = exports.ebarimtGetConfig = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
const types_1 = require("./types");
const ebarimtGetConfig = (config) => {
if (!config?.taxPayerUrl || !config?.posApiEndpoint) {
throw new Error("taxPayerUrl is required");
}
return {
taxPayerUrl: config?.taxPayerUrl,
posApiEndpoint: config?.posApiEndpoint,
easyRegisterEndpoint: config?.easyRegisterEndpoint,
};
};
exports.ebarimtGetConfig = ebarimtGetConfig;
const getCompanyInfo = async ({ no, config, }) => {
const tinRegex = /(^\d{11}$)|(^\d{12}$)|(^\d{14}$)/;
const regNoRegex = /(^[А-ЯЁӨҮ]{2}\d{8}$)|(^\d{7}$)/giu;
if (tinRegex.test(no)) {
const response = await (0, node_fetch_1.default)(`${config.taxPayerUrl}/getInfo?tin=${no}`);
const result = (await response.json());
return { status: "checked", result, tin: no };
}
if (!regNoRegex.test(no)) {
return { status: "notValid" };
}
const tinInfoResponse = await (0, node_fetch_1.default)(`${config.taxPayerUrl}/getTinInfo?regNo=${no}`);
const tinInfo = (await tinInfoResponse.json());
if (tinInfo.status !== 200) {
return { status: "notValid" };
}
const tinNo = tinInfo.data;
const companyInfoResponse = await (0, node_fetch_1.default)(`${config.taxPayerUrl}/getInfo?tin=${tinNo}`);
const result = (await companyInfoResponse.json());
return { status: "checked", result, tin: tinNo };
};
exports.getCompanyInfo = getCompanyInfo;
const getPostData = async (config, params) => {
if (!config.taxPayerUrl) {
throw new Error("taxPayerUrl is required in the configuration");
}
if (!params.merchantRegNo ||
!params.districtCode ||
!params.type ||
!params.items) {
throw new Error("Required parameters are missing in IEbarimtSaveType");
}
let customerTin = "";
if (params.type === types_1.EbarimtVatType.B2B_RECEIPT && params.customerRegNo) {
const result = await (0, exports.getCompanyInfo)({
no: params.customerRegNo,
config: config,
});
customerTin = result.tin || "";
}
const merchantData = await (0, exports.getCompanyInfo)({
no: params.merchantRegNo,
config: config,
});
if (!merchantData?.tin) {
throw new Error("Unable to find merchantTin");
}
const isVatPayer = merchantData.result?.data?.vatPayer;
const isCityTaxPayer = merchantData.result?.data?.cityPayer;
let totalAmount = 0;
let totalVat = 0;
let totalCityTax = 0;
const items = params.items.map((item) => {
let vat = 0;
let cityTax = 0;
const itemTotalAmount = item.unitPrice * item.qty;
let itemFinalAmount = itemTotalAmount;
if (isVatPayer) {
vat = parseFloat((itemTotalAmount / 10).toFixed(2));
totalVat += vat;
itemFinalAmount += vat;
}
if (isCityTaxPayer) {
cityTax = parseFloat((itemTotalAmount / 50).toFixed(2));
totalCityTax += cityTax;
itemFinalAmount += cityTax;
}
totalAmount += itemFinalAmount;
return {
name: item.name,
barCode: "",
barCodeType: "UNDEFINED",
classificationCode: item.classificationCode,
measureUnit: item.measureUnit || "ш",
qty: item.qty,
totalCityTax: cityTax,
totalVAT: isVatPayer ? vat : null,
totalAmount: itemFinalAmount,
unitPrice: itemFinalAmount / item.qty,
};
});
totalAmount = parseFloat(totalAmount.toFixed(2));
totalVat = parseFloat(totalVat.toFixed(2));
totalCityTax = parseFloat(totalCityTax.toFixed(2));
return {
totalAmount,
totalVAT: totalVat,
totalCityTax: totalCityTax,
districtCode: params.districtCode + "08",
// districtCode: params.districtCode + "01",
merchantTin: merchantData.tin,
posNo: "001",
customerTin,
consumerNo: "",
branchNo: "001",
type: params.type,
inactiveId: null,
invoiceId: params.invoiceId || null,
reportMonth: null,
receipts: [
{
totalAmount,
taxType: "VAT_ABLE",
merchantTin: merchantData.tin,
customerTin,
totalCityTax: totalCityTax,
totalVAT: totalVat,
type: params.type,
bankAccountNo: null,
items,
},
],
payments: [
{
code: "CASH",
status: "PAID",
paidAmount: totalAmount,
},
],
};
};
exports.getPostData = getPostData;
const getPostDataContained = async (config, params) => {
if (!config.taxPayerUrl) {
throw new Error("taxPayerUrl is required in the configuration");
}
if (!params.merchantRegNo ||
!params.districtCode ||
!params.type ||
!params.items) {
throw new Error("Required parameters are missing in IEbarimtSaveType");
}
let customerTin = params.customerRegNo || "";
if (params.type === types_1.EbarimtVatType.B2B_RECEIPT && params.customerRegNo) {
const result = await (0, exports.getCompanyInfo)({
no: params.customerRegNo,
config: config,
});
customerTin = result.tin || "";
}
const merchantData = await (0, exports.getCompanyInfo)({
no: params.merchantRegNo,
config: config,
});
if (!merchantData?.tin) {
throw new Error("Unable to find merchantTin");
}
const isVatPayer = merchantData.result?.data?.vatPayer;
// const isCityTaxPayer = merchantData.result?.data?.cityPayer;
const isCityTaxPayer = false;
let totalAmount = 0;
let totalVat = 0;
let totalCityTax = 0;
const items = params.items.map((item) => {
let vat = 0;
let cityTax = 0;
let unitTotalPrice = item.unitPrice * item.qty;
let unitTotalVatPercent = 1;
if (isVatPayer) {
unitTotalVatPercent += 0.1;
}
if (isCityTaxPayer) {
unitTotalVatPercent += 0.02;
}
unitTotalPrice = parseFloat((unitTotalPrice / unitTotalVatPercent).toFixed(2));
if (isVatPayer) {
vat = parseFloat((unitTotalPrice / 10).toFixed(2));
totalVat += vat;
}
if (isCityTaxPayer) {
cityTax = parseFloat((unitTotalPrice / 50).toFixed(2));
totalCityTax += cityTax;
}
totalAmount += item.unitPrice * item.qty;
return {
name: item.name,
barCode: "",
barCodeType: "UNDEFINED",
classificationCode: item.classificationCode,
measureUnit: item.measureUnit || "ш",
qty: item.qty,
totalCityTax: cityTax,
totalVAT: isVatPayer ? vat : null,
totalAmount: item.unitPrice * item.qty,
unitPrice: item.unitPrice,
};
});
totalAmount = parseFloat(totalAmount.toFixed(2));
totalVat = parseFloat(totalVat.toFixed(2));
totalCityTax = parseFloat(totalCityTax.toFixed(2));
return {
totalAmount,
totalVAT: totalVat,
totalCityTax: totalCityTax,
// districtCode: params.districtCode + "01",
districtCode: params.districtCode + "08",
merchantTin: merchantData.tin,
posNo: "001",
customerTin,
consumerNo: "",
branchNo: "001",
type: params.type,
inactiveId: null,
reportMonth: null,
invoiceId: params.invoiceId || null,
receipts: [
{
totalAmount,
taxType: "VAT_ABLE",
merchantTin: merchantData.tin,
customerTin,
totalCityTax: totalCityTax,
totalVAT: totalVat,
type: params.type,
bankAccountNo: params.bankAccountNo || null,
items,
},
],
payments: (params.type === types_1.EbarimtVatType.B2B_INVOICE || params.type === types_1.EbarimtVatType.B2C_INVOICE) ? null : [
{
code: "CASH",
status: "PAID",
paidAmount: totalAmount,
},
],
};
};
exports.getPostDataContained = getPostDataContained;
const saveVat = async (config, data
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) => {
if (!config.posApiEndpoint) {
throw new Error("posApiEndpoint is required in the configuration");
}
try {
const response = await (0, node_fetch_1.default)(`${config.posApiEndpoint}/rest/receipt`, {
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
},
});
return await response.json();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}
catch (error) {
return {
status: "ERROR",
message: error.message,
};
}
};
exports.saveVat = saveVat;
const sendData = async (config) => {
if (!config.posApiEndpoint) {
throw new Error("posApiEndpoint is required in the configuration");
}
try {
await (0, node_fetch_1.default)(`${config.posApiEndpoint}/rest/sendData`, {
method: "GET",
});
return {
status: "SUCCESS",
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}
catch (error) {
return {
status: "ERROR",
message: error.message,
};
}
};
exports.sendData = sendData;
const deleteVat = async (config, data
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) => {
if (!config.posApiEndpoint) {
throw new Error("posApiEndpoint is required in the configuration");
}
try {
await (0, node_fetch_1.default)(`${config.posApiEndpoint}/rest/receipt`, {
method: "DELETE",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
},
});
return {
status: "SUCCESS",
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}
catch (error) {
return {
status: "ERROR",
message: error.message,
};
}
};
exports.deleteVat = deleteVat;
//# sourceMappingURL=utils.js.map