commit
e77631694e
@ -16,7 +16,6 @@ const CompanySettingsRepository = require('../../repositories/settings/CompanySe
|
|||||||
class InvoiceService {
|
class InvoiceService {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.repo = new InvoiceRepository();
|
this.repo = new InvoiceRepository();
|
||||||
this._qrDataUriCache = new Map();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_inferImageMimeFromBase64(base64) {
|
_inferImageMimeFromBase64(base64) {
|
||||||
@ -59,17 +58,12 @@ class InvoiceService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
_getLocalQrImagePath(pieceCount) {
|
|
||||||
const safePieceCount = pieceCount === 120 ? 120 : 60;
|
|
||||||
const fileName = safePieceCount === 120 ? 'qr_120.png' : 'qr_60.png';
|
|
||||||
return path.resolve(__dirname, '../../templates/invoice/qr', fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
async _getCompanySettingsQrDataUri(pieceCount) {
|
async _getCompanySettingsQrDataUri(pieceCount) {
|
||||||
const safePieceCount = pieceCount === 120 ? 120 : 60;
|
const safePieceCount = pieceCount === 120 ? 120 : 60;
|
||||||
try {
|
try {
|
||||||
const repo = new CompanySettingsRepository();
|
const repo = new CompanySettingsRepository();
|
||||||
const row = await repo.get();
|
const row = await repo.get();
|
||||||
|
if (!row) return null;
|
||||||
const raw = safePieceCount === 120 ? row?.qr_code_120_base64 : row?.qr_code_60_base64;
|
const raw = safePieceCount === 120 ? row?.qr_code_120_base64 : row?.qr_code_60_base64;
|
||||||
const value = (raw == null) ? '' : String(raw).trim();
|
const value = (raw == null) ? '' : String(raw).trim();
|
||||||
if (!value) return null;
|
if (!value) return null;
|
||||||
@ -85,34 +79,11 @@ class InvoiceService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async _getLocalQrDataUri(pieceCount) {
|
|
||||||
const safePieceCount = pieceCount === 120 ? 120 : 60;
|
|
||||||
|
|
||||||
if (this._qrDataUriCache.has(safePieceCount)) {
|
|
||||||
return this._qrDataUriCache.get(safePieceCount);
|
|
||||||
}
|
|
||||||
|
|
||||||
const filePath = this._getLocalQrImagePath(safePieceCount);
|
|
||||||
try {
|
|
||||||
const buffer = await fs.readFile(filePath);
|
|
||||||
const dataUri = `data:image/png;base64,${buffer.toString('base64')}`;
|
|
||||||
this._qrDataUriCache.set(safePieceCount, dataUri);
|
|
||||||
return dataUri;
|
|
||||||
} catch (e) {
|
|
||||||
logger.warn('InvoiceService._getLocalQrDataUri:missing_qr_file', {
|
|
||||||
pieceCount: safePieceCount,
|
|
||||||
filePath,
|
|
||||||
message: e?.message,
|
|
||||||
});
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async _buildQrCodeImageTag({ abonement }) {
|
async _buildQrCodeImageTag({ abonement }) {
|
||||||
const pieceCount = this._resolvePieceCountForQr(abonement);
|
const pieceCount = this._resolvePieceCountForQr(abonement);
|
||||||
if (!pieceCount) return '';
|
if (!pieceCount) return '';
|
||||||
|
|
||||||
const dataUri = await this._getCompanySettingsQrDataUri(pieceCount) || await this._getLocalQrDataUri(pieceCount);
|
const dataUri = await this._getCompanySettingsQrDataUri(pieceCount);
|
||||||
if (!dataUri) return '';
|
if (!dataUri) return '';
|
||||||
|
|
||||||
return `<img alt="QR Code" src="${this._escapeHtml(dataUri)}" />`;
|
return `<img alt="QR Code" src="${this._escapeHtml(dataUri)}" />`;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user