429 lines
12 KiB
JavaScript
429 lines
12 KiB
JavaScript
|
|
import Cookies from 'universal-cookie';
|
||
|
|
import CryptoJS from 'crypto-js';
|
||
|
|
import moment from 'moment';
|
||
|
|
import jsPDF from 'jspdf';
|
||
|
|
import { changePrintStatus } from '../Features/BookingScreen/BookingData/BookingData';
|
||
|
|
import store from '../app/store.js';
|
||
|
|
import html2pdf from 'html2pdf.js';
|
||
|
|
|
||
|
|
const cookies = new Cookies();
|
||
|
|
const SECRET_KEY = import.meta.env.ENV_SECRET_KEY;
|
||
|
|
const URL_SECRET_KEY = import.meta.env.ENV_URL_SECRET_KEY;
|
||
|
|
|
||
|
|
export const localstore = (GSTIN) => {
|
||
|
|
localStorage.setItem('GSTIN', GSTIN);
|
||
|
|
};
|
||
|
|
export const generateRandomKey = () => {
|
||
|
|
const randomBytes = CryptoJS.lib.WordArray.random(16); // 128-bit key (16 bytes)
|
||
|
|
return randomBytes.toString(); // Hex string
|
||
|
|
};
|
||
|
|
export const roundToDecimal = (amountStr, allowDecimal = true) => {
|
||
|
|
if (amountStr == null) return allowDecimal ? '0.00' : '0';
|
||
|
|
|
||
|
|
const cleaned = String(amountStr).replace(/[^0-9.-]+/g, '');
|
||
|
|
const num = Number(cleaned);
|
||
|
|
|
||
|
|
if (isNaN(num)) return allowDecimal ? '0.00' : '0';
|
||
|
|
|
||
|
|
return allowDecimal ? num.toFixed(2) : Math.round(num).toString();
|
||
|
|
};
|
||
|
|
|
||
|
|
export const getLocalStorageValues = (key) => {
|
||
|
|
return localStorage.getItem(key);
|
||
|
|
};
|
||
|
|
|
||
|
|
export const sessionStore = (key, value) => {
|
||
|
|
const encryptedValue = CryptoJS.AES.encrypt(
|
||
|
|
JSON.stringify(value),
|
||
|
|
SECRET_KEY
|
||
|
|
).toString();
|
||
|
|
|
||
|
|
if (encryptedValue) sessionStorage.setItem(key, encryptedValue);
|
||
|
|
};
|
||
|
|
|
||
|
|
export const encryptedValuesFun = (value) => {
|
||
|
|
const encryptedValue = CryptoJS.AES.encrypt(
|
||
|
|
JSON.stringify(value),
|
||
|
|
SECRET_KEY
|
||
|
|
).toString();
|
||
|
|
|
||
|
|
if (encryptedValue) {
|
||
|
|
return encryptedValue
|
||
|
|
.replace(/\+/g, '-')
|
||
|
|
.replace(/\//g, '_')
|
||
|
|
.replace(/=+$/, ''); // Base64 URL-safe encoding
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
export const encryptedValuesUrlFun = (value) => {
|
||
|
|
const encryptedValue = CryptoJS.AES.encrypt(
|
||
|
|
JSON.stringify(value),
|
||
|
|
URL_SECRET_KEY
|
||
|
|
).toString();
|
||
|
|
|
||
|
|
if (encryptedValue) {
|
||
|
|
return encryptedValue
|
||
|
|
.replace(/\+/g, '-')
|
||
|
|
.replace(/\//g, '_')
|
||
|
|
.replace(/=+$/, ''); // Base64 URL-safe encoding
|
||
|
|
}
|
||
|
|
};
|
||
|
|
export const decryptedValuesFun = (value) => {
|
||
|
|
if (!value) {
|
||
|
|
return null;
|
||
|
|
} else {
|
||
|
|
value = value.replace(/-/g, '+').replace(/_/g, '/');
|
||
|
|
const decrypted = CryptoJS.AES.decrypt(value, URL_SECRET_KEY).toString(
|
||
|
|
CryptoJS.enc.Utf8
|
||
|
|
);
|
||
|
|
if (decrypted) {
|
||
|
|
return JSON.parse(decrypted);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
export const encryptObject=(obj)=> {
|
||
|
|
const jsonString = JSON.stringify(obj);
|
||
|
|
const encrypted = CryptoJS.AES.encrypt(jsonString, SECRET_KEY).toString();
|
||
|
|
|
||
|
|
// Make Base64 string URL-safe
|
||
|
|
const urlSafe = encrypted
|
||
|
|
.replace(/\+/g, "-")
|
||
|
|
.replace(/\//g, "_")
|
||
|
|
.replace(/=+$/, "");
|
||
|
|
|
||
|
|
return urlSafe;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Decrypt URL-safe AES string to original object
|
||
|
|
export const decryptToObject=(encryptedUrlSafeStr) =>{
|
||
|
|
try {
|
||
|
|
if (!encryptedUrlSafeStr) return null;
|
||
|
|
|
||
|
|
// Restore base64 format
|
||
|
|
let base64 = encryptedUrlSafeStr
|
||
|
|
.replace(/-/g, "+")
|
||
|
|
.replace(/_/g, "/");
|
||
|
|
|
||
|
|
// Add missing padding
|
||
|
|
while (base64.length % 4 !== 0) {
|
||
|
|
base64 += "=";
|
||
|
|
}
|
||
|
|
|
||
|
|
const bytes = CryptoJS.AES.decrypt(base64, SECRET_KEY);
|
||
|
|
const decrypted = bytes.toString(CryptoJS.enc.Utf8);
|
||
|
|
return JSON.parse(decrypted);
|
||
|
|
} catch (err) {
|
||
|
|
console.error("Decryption failed:", err);
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
export const TokendecryptedValuesFun = (value) => {
|
||
|
|
if (!value) {
|
||
|
|
return null;
|
||
|
|
} else {
|
||
|
|
value = value.replace(/-/g, '+').replace(/_/g, '/');
|
||
|
|
const decrypted = CryptoJS.AES.decrypt(value, SECRET_KEY).toString(
|
||
|
|
CryptoJS.enc.Utf8
|
||
|
|
);
|
||
|
|
if (decrypted) {
|
||
|
|
return JSON.parse(decrypted);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
export const getSession = (key) => {
|
||
|
|
const val = sessionStorage.getItem(key);
|
||
|
|
if (val) {
|
||
|
|
const decrypted = CryptoJS.AES.decrypt(val, SECRET_KEY).toString(
|
||
|
|
CryptoJS.enc.Utf8
|
||
|
|
);
|
||
|
|
|
||
|
|
if (decrypted) {
|
||
|
|
return JSON.parse(decrypted);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
export const clearSession = (key) => {
|
||
|
|
const val = sessionStorage.getItem(key);
|
||
|
|
|
||
|
|
if (val) {
|
||
|
|
sessionStorage.removeItem(key);
|
||
|
|
} else {
|
||
|
|
sessionStorage.clear();
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// Remove Single Cookie Data
|
||
|
|
|
||
|
|
export function removeCookieData(key) {
|
||
|
|
return cookies.remove(key, { path: '/' });
|
||
|
|
}
|
||
|
|
|
||
|
|
// Removing all Cookie Data
|
||
|
|
|
||
|
|
export async function ClearCookie() {
|
||
|
|
const allCookie = cookies.getAll();
|
||
|
|
if (allCookie) {
|
||
|
|
for (let key in allCookie) {
|
||
|
|
await cookies.remove(key, { path: '/' });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function printDiv(id, style) {
|
||
|
|
console.log("printDiv",id, style)
|
||
|
|
return new Promise(async (resolve, reject) => {
|
||
|
|
const content = document.getElementById(id);
|
||
|
|
|
||
|
|
if (content) {
|
||
|
|
try {
|
||
|
|
await store.dispatch(changePrintStatus(true));
|
||
|
|
|
||
|
|
const iframe = document.createElement('iframe');
|
||
|
|
iframe.style.position = 'absolute';
|
||
|
|
iframe.style.width = '0px';
|
||
|
|
iframe.style.height = '0px';
|
||
|
|
iframe.style.border = 'none';
|
||
|
|
document.body.appendChild(iframe);
|
||
|
|
|
||
|
|
const doc = iframe.contentWindow.document;
|
||
|
|
doc.open();
|
||
|
|
doc.write('<html><head>');
|
||
|
|
doc.write(
|
||
|
|
'<link rel="preload" as="font" href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,400;0,500;0,600;0,700;1,200&display=swap" rel="stylesheet">'
|
||
|
|
);
|
||
|
|
doc.write('</head><body>');
|
||
|
|
doc.write(style);
|
||
|
|
doc.write(content.innerHTML);
|
||
|
|
doc.write('</body></html>');
|
||
|
|
doc.close();
|
||
|
|
|
||
|
|
iframe.contentWindow.focus();
|
||
|
|
iframe.contentWindow.onafterprint = () => {
|
||
|
|
document.body.removeChild(iframe);
|
||
|
|
resolve();
|
||
|
|
};
|
||
|
|
iframe.contentWindow.onload = () => {
|
||
|
|
console.log('Iframe content loaded.');
|
||
|
|
setTimeout(() => {
|
||
|
|
iframe.contentWindow.print();
|
||
|
|
}, 300); // Allow rendering delay
|
||
|
|
};
|
||
|
|
// iframe.contentWindow.print();
|
||
|
|
await store.dispatch(changePrintStatus(false));
|
||
|
|
} catch (error) {
|
||
|
|
console.error(`Error in printDiv for ID ${id}:`, error);
|
||
|
|
reject(error);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
console.warn(`No content found for ID ${id}`);
|
||
|
|
reject(new Error(`No content found for ID ${id}`));
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export function extractLastNumber(inputString) {
|
||
|
|
const numbersAfterHyphen = inputString?.split('-')?.filter(Boolean);
|
||
|
|
const lastNumber = numbersAfterHyphen?.[numbersAfterHyphen?.length - 1];
|
||
|
|
|
||
|
|
return lastNumber;
|
||
|
|
}
|
||
|
|
export function extractLastNumberOrderId(inputString, Type) {
|
||
|
|
let lastNumber = '';
|
||
|
|
if (Type === 'Y') {
|
||
|
|
const numbersAfterHyphen = inputString?.split('-')?.filter(Boolean);
|
||
|
|
lastNumber = numbersAfterHyphen?.[numbersAfterHyphen?.length - 2];
|
||
|
|
} else {
|
||
|
|
const numbersAfterHyphen = inputString?.split('-')?.filter(Boolean);
|
||
|
|
lastNumber = numbersAfterHyphen?.[numbersAfterHyphen?.length - 1];
|
||
|
|
}
|
||
|
|
|
||
|
|
return lastNumber;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function dateFormatChange(dateString) {
|
||
|
|
let date = moment(dateString, ['YYYY-MM-DDTHH:mm:ss']).format('DD-MM-YYYY');
|
||
|
|
return date;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function dateFormatChange1(dateString) {
|
||
|
|
return moment(dateString, [
|
||
|
|
'YYYY-MM-DDTHH:mm:ss.SSS',
|
||
|
|
'YYYY-MM-DDTHH:mm:ss'
|
||
|
|
]).format('DD-MMM-YYYY hh:mm A');
|
||
|
|
}
|
||
|
|
|
||
|
|
export function ExtractDateFormate(dateString) {
|
||
|
|
if (!dateString) return "";
|
||
|
|
const date = new Date(dateString);
|
||
|
|
if (isNaN(date)) return "";
|
||
|
|
const day = date.getDate();
|
||
|
|
const month = date.toLocaleString("en-US", { month: "short" });
|
||
|
|
const year = date.getFullYear();
|
||
|
|
return `${day}-${month}-${year}`;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
// export function dateTimeFormatChange(dateString) {
|
||
|
|
// let date = moment(dateString, ['YYYY-MM-DDTHH:mm:ss']).format('DD-MM-YYYY hh:mm:ss A');
|
||
|
|
// let finaldate = date.replace('T', ' ');
|
||
|
|
// return finaldate;
|
||
|
|
// }
|
||
|
|
|
||
|
|
export function dateTimeFormatChange(dateString) {
|
||
|
|
return moment(dateString, ['YYYY-MM-DDTHH:mm:ss']).format('DD-MMM-YYYY hh:mm A');
|
||
|
|
}
|
||
|
|
|
||
|
|
export function convertTo12HourFormat(inputDate) {
|
||
|
|
console.log(inputDate, 'mohantest1');
|
||
|
|
const date = new Date(inputDate);
|
||
|
|
let hours = date.getHours();
|
||
|
|
const minutes = String(date.getMinutes()).padStart(2, '0');
|
||
|
|
const seconds = String(date.getSeconds()).padStart(2, '0');
|
||
|
|
const period = hours >= 12 ? 'PM' : 'AM';
|
||
|
|
|
||
|
|
hours = hours % 12 || 12; // Convert to 12-hour format (handles midnight)
|
||
|
|
|
||
|
|
return `${hours}:${minutes}:${seconds} ${period}`;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function handleGeneratePDF(id) {
|
||
|
|
const pdf = new jsPDF();
|
||
|
|
const content = document.getElementById(id);
|
||
|
|
|
||
|
|
if (content) {
|
||
|
|
pdf.html(content, {
|
||
|
|
callback: (pdf) => {
|
||
|
|
pdf.save('generated-pdf.pdf');
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
// export async function pdfDiv(id, style) {
|
||
|
|
// return new Promise(async (resolve, reject) => {
|
||
|
|
// const content = document.getElementById(id);
|
||
|
|
|
||
|
|
// if (!content) {
|
||
|
|
// reject(new Error(`Element with ID "${id}" not found`));
|
||
|
|
// return;
|
||
|
|
// }
|
||
|
|
|
||
|
|
// try {
|
||
|
|
// // Create a wrapper to apply styles
|
||
|
|
// const wrapper = document.createElement('div');
|
||
|
|
// wrapper.innerHTML = `
|
||
|
|
// ${style}
|
||
|
|
// ${content.innerHTML}
|
||
|
|
// `;
|
||
|
|
|
||
|
|
// // Generate PDF Blob
|
||
|
|
// const opt = {
|
||
|
|
// margin: 0.5,
|
||
|
|
// image: { type: 'jpeg', quality: 0.98 },
|
||
|
|
// html2canvas: { scale: 2, useCORS: true },
|
||
|
|
// jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' },
|
||
|
|
// pagebreak: {
|
||
|
|
// mode: ['avoid-all', 'css', 'legacy'],
|
||
|
|
// before: '.page-break-before',
|
||
|
|
// after: '.page-break-after',
|
||
|
|
// avoid: '.page-break-avoid'
|
||
|
|
// }
|
||
|
|
// };
|
||
|
|
|
||
|
|
// const pdfBlob = await html2pdf().set(opt).from(wrapper).outputPdf('blob');
|
||
|
|
// resolve(pdfBlob);
|
||
|
|
// } catch (error) {
|
||
|
|
// reject(error);
|
||
|
|
// }
|
||
|
|
// });
|
||
|
|
// }
|
||
|
|
|
||
|
|
// export async function blobToBase64(blob) {
|
||
|
|
// return new Promise((resolve, reject) => {
|
||
|
|
// const reader = new FileReader();
|
||
|
|
// reader.onloadend = () => resolve(reader.result.split(',')[1]);
|
||
|
|
// reader.onerror = reject;
|
||
|
|
// reader.readAsDataURL(blob);
|
||
|
|
// });
|
||
|
|
// }
|
||
|
|
// New pdfDiv and blobToBase64 functions
|
||
|
|
export const pdfDiv = async (id, style = "") => {
|
||
|
|
return new Promise(async (resolve, reject) => {
|
||
|
|
const content = document.getElementById(id);
|
||
|
|
|
||
|
|
if (!content) {
|
||
|
|
reject(new Error(`Element with ID "${id}" not found`));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
// Create wrapper div
|
||
|
|
const wrapper = document.createElement("div");
|
||
|
|
wrapper.innerHTML = `
|
||
|
|
${style}
|
||
|
|
${content.innerHTML}
|
||
|
|
`;
|
||
|
|
|
||
|
|
// PDF options
|
||
|
|
const opt = {
|
||
|
|
margin: 0,
|
||
|
|
image: { type: "jpeg", quality: 0.9 },
|
||
|
|
html2canvas: {
|
||
|
|
scale: 2,
|
||
|
|
useCORS: true,
|
||
|
|
letterRendering: true,
|
||
|
|
},
|
||
|
|
jsPDF: {
|
||
|
|
unit: "mm",
|
||
|
|
format: "a4",
|
||
|
|
orientation: "portrait",
|
||
|
|
},
|
||
|
|
pagebreak: {
|
||
|
|
mode: ["avoid-all", "css", "legacy"],
|
||
|
|
},
|
||
|
|
};
|
||
|
|
|
||
|
|
const blob = await html2pdf().set(opt).from(wrapper).outputPdf("blob");
|
||
|
|
resolve(blob);
|
||
|
|
} catch (err) {
|
||
|
|
reject(err);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
export const blobToBase64 = (blob) => {
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
const reader = new FileReader();
|
||
|
|
reader.onloadend = () => {
|
||
|
|
const bytes = new Uint8Array(reader.result);
|
||
|
|
let binary = '';
|
||
|
|
bytes.forEach((b) => binary += String.fromCharCode(b));
|
||
|
|
resolve(btoa(binary));
|
||
|
|
};
|
||
|
|
reader.onerror = reject;
|
||
|
|
reader.readAsArrayBuffer(blob);
|
||
|
|
});
|
||
|
|
};
|
||
|
|
export const validateSafeInput = (value) => {
|
||
|
|
const hasHTMLTags = /<[^>]*>/g.test(value);
|
||
|
|
const hasSQLInjection =
|
||
|
|
/\b(select|update|delete|insert|drop|truncate|exec|union|--|\*|;)\b/i.test(
|
||
|
|
value
|
||
|
|
);
|
||
|
|
|
||
|
|
if (hasHTMLTags) {
|
||
|
|
return Promise.reject(new Error('HTML tags are not allowed'));
|
||
|
|
}
|
||
|
|
if (hasSQLInjection) {
|
||
|
|
return Promise.reject(
|
||
|
|
new Error('SQL keywords like SELECT, DELETE are not allowed')
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
return Promise.resolve();
|
||
|
|
};
|
||
|
|
|
||
|
|
|