Skip to content

Commit

Permalink
feat: create and save POS print templates with matched sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
AbleKSaju committed Jan 3, 2025
1 parent 952d795 commit 550e819
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 30 deletions.
11 changes: 10 additions & 1 deletion main/getPrintTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ export async function getTemplates() {
const filePath = path.join(paths.root, file);
const template = await fs.readFile(filePath, 'utf-8');
const { mtime } = await fs.stat(filePath);
templates.push({ template, file, modified: mtime.toISOString() });
const width = file?.split('-')[1]?.split('.')[0] === 'POS' ? 8 : 0;
const height = file?.split('-')[1]?.split('.')[0] === 'POS' ? 22 : 0;

templates.push({
template,
file,
modified: mtime.toISOString(),
width,
height,
});
}

return templates;
Expand Down
8 changes: 2 additions & 6 deletions src/pages/PrintView/PrintView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,8 @@
:template="printProps.template"
:values="printProps.values"
:scale="scale"
:width="
templateName?.startsWith('POS')
? fyo.singles.PrintSettings?.posPrintWidth
: templateDoc?.width
"
:height="templateName.startsWith('POS') ? 22 : templateDoc?.height"
:width="templateDoc?.width"
:height="templateDoc?.height"
/>
</div>
</div>
Expand Down
9 changes: 2 additions & 7 deletions src/pages/TemplateBuilder/TemplateBuilder.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,8 @@
:template="doc.template!"
:values="values!"
:scale="scale"
:height="doc.name.startsWith('POS') ? 20 : doc.height"
:width="
doc.name?.startsWith('POS') &&
fyo.singles.PrintSettings?.posPrintWidth
? fyo.singles.PrintSettings.posPrintWidth
: doc.width
"
:height="doc.height"
:width="doc.width"
/>
</div>

Expand Down
44 changes: 33 additions & 11 deletions src/utils/printTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ export type PrintTemplateHint = {
[key: string]: string | PrintTemplateHint | PrintTemplateHint[];
};
type PrintTemplateData = Record<string, unknown>;
type TemplateUpdateItem = { name: string; template: string; type: string };
type TemplateUpdateItem = {
name: string;
template: string;
type: string;
width: number;
height: number;
};

const printSettingsFields = [
'logo',
Expand All @@ -44,20 +50,25 @@ export async function getPrintTemplatePropValues(
const totalTax = await (doc as Invoice)?.getTotalTax();
const paymentId = await (doc as SalesInvoice).getPaymentIds();

const paymentDoc = await fyo.doc.getDoc(ModelNameEnum.Payment, paymentId[0]);
if (paymentId.length) {
const paymentDoc = await fyo.doc.getDoc(
ModelNameEnum.Payment,
paymentId[0]
);

(values.doc as PrintTemplateData).paymentMethod = paymentDoc.paymentMethod;

(values.doc as PrintTemplateData).paymentMethod = paymentDoc.paymentMethod;
(values.doc as PrintTemplateData).paidAmount = doc.fyo.format(
paymentDoc.amount as Money,
ModelNameEnum.Currency
);
}

(values.doc as PrintTemplateData).subTotal = doc.fyo.format(
(doc.grandTotal as Money).sub(totalTax),
ModelNameEnum.Currency
);

(values.doc as PrintTemplateData).paidAmount = doc.fyo.format(
paymentDoc.amount as Money,
ModelNameEnum.Currency
);

const printSettings = await fyo.doc.getDoc(ModelNameEnum.PrintSettings);
const printValues = await getPrintTemplateDocValues(
printSettings,
Expand Down Expand Up @@ -435,20 +446,29 @@ export async function updatePrintTemplates(fyo: Fyo) {

const isLogging = fyo.store.skipTelemetryLogging;
fyo.store.skipTelemetryLogging = true;
for (const { name, type, template } of updateList) {
for (const { name, type, template, width, height } of updateList) {
const doc = await getDocFromNameIfExistsElseNew(
ModelNameEnum.PrintTemplate,
name
);

await doc.set({ name, type, template, isCustom: false });
const updateData = {
name,
type,
template,
isCustom: false,
...(width ? { width } : {}),
...(height ? { height } : {}),
};

await doc.set(updateData);
await doc.sync();
}
fyo.store.skipTelemetryLogging = isLogging;
}

function getPrintTemplateUpdateList(
{ file, template, modified: modifiedString }: TemplateFile,
{ file, template, modified: modifiedString, width, height }: TemplateFile,
nameModifiedMap: Record<string, Date>,
fyo: Fyo
): TemplateUpdateItem[] {
Expand All @@ -462,6 +482,8 @@ function getPrintTemplateUpdateList(
}

templateList.push({
height,
width,
name,
type,
template,
Expand Down
5 changes: 5 additions & 0 deletions src/utils/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,7 @@ export const printSizes = [
'B7',
'B8',
'B9',
'POS',
'Letter',
'Legal',
'Executive',
Expand Down Expand Up @@ -923,6 +924,10 @@ export const paperSizeMap: Record<
width: 4.4,
height: 6.2,
},
POS: {
width: 8,
height: 22,
},
Letter: {
width: 21.59,
height: 27.94,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ <h2 class="font-semibold text-xl" :style="{ color: print.color }">
<!-- Company Contacts -->
<p class="mt-1 text-base">{{ print.phone }}</p>
<p class="text-base">{{ print.email }}</p>
<p class="text-base">VAT: 100202308100003</p>
</header>
<!-- Invoice Details -->

Expand Down Expand Up @@ -134,9 +133,6 @@ <h4 class="font-semibold text-xl">Invoice</h4>
</section>

<div class="w-full mt-3 flex pb-5 flex-col justify-center items-center">
<p class="w-11/12 text-sm text-center">
No Cash Refund Exchange Only..Keep bill for Exchange
</p>
<p class="pt-1 text-lg">***** Thank You Visit Again *****</p>
</div>
</main>
8 changes: 7 additions & 1 deletion utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ export type PropertyEnum<T extends Record<string, any>> = {
[key in keyof Required<T>]: key;
};

export type TemplateFile = { file: string; template: string; modified: string };
export type TemplateFile = {
file: string;
template: string;
modified: string;
width: number;
height: number;
};

export interface Keys extends ModMap {
pressed: Set<string>;
Expand Down

0 comments on commit 550e819

Please sign in to comment.