236 lines
6.4 KiB
JavaScript
236 lines
6.4 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.js';
|
|
import store from '../app/store.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 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 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 function RoundOffFun(amount) {
|
|
const roundedAmount = Math.round(amount);
|
|
const roundOffValue = parseFloat((roundedAmount - amount).toFixed(2));
|
|
const symbol = roundOffValue > 0 ? '+' : ''; // Add "+" symbol for positive values
|
|
return `${symbol}${roundOffValue.toFixed(2)}`;
|
|
}
|
|
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
|
|
};
|
|
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 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 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');
|
|
},
|
|
});
|
|
}
|
|
}
|