Skip to content

Commit

Permalink
Merge pull request #1074 from AbleKSaju/feat-pos-print-template
Browse files Browse the repository at this point in the history
feat: print template for POS
  • Loading branch information
akshayitzme authored Jan 3, 2025
2 parents 74bd577 + fda3718 commit cdeb7a9
Show file tree
Hide file tree
Showing 12 changed files with 245 additions and 26 deletions.
14 changes: 12 additions & 2 deletions main/getPrintTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs/promises';
import path from 'path';
import { TemplateFile } from 'utils/types';

export async function getTemplates() {
export async function getTemplates(posTemplateWidth?: number) {
const paths = await getPrintTemplatePaths();
if (!paths) {
return [];
Expand All @@ -13,7 +13,17 @@ 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' ? posTemplateWidth ?? 0 : 0;
const height = file?.split('-')[1]?.split('.')[0] === 'POS' ? 22 : 0;

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

return templates;
Expand Down
5 changes: 3 additions & 2 deletions main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ const ipc = {
};
},

async getTemplates(): Promise<TemplateFile[]> {
async getTemplates(posTemplateWidth?: number): Promise<TemplateFile[]> {
return (await ipcRenderer.invoke(
IPC_ACTIONS.GET_TEMPLATES
IPC_ACTIONS.GET_TEMPLATES,
posTemplateWidth
)) as TemplateFile[];
},

Expand Down
9 changes: 6 additions & 3 deletions main/registerIpcMainActionListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,12 @@ export default function registerIpcMainActionListeners(main: Main) {
};
});

ipcMain.handle(IPC_ACTIONS.GET_TEMPLATES, async () => {
return getTemplates();
});
ipcMain.handle(
IPC_ACTIONS.GET_TEMPLATES,
async (_, posPrintWidth?: number) => {
return getTemplates(posPrintWidth);
}
);

/**
* Database Related Actions
Expand Down
7 changes: 7 additions & 0 deletions models/baseModels/Invoice/Invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,13 @@ export abstract class Invoice extends Transactional {
return taxArr;
}

async getTotalTax() {
const taxArr = await this.getTaxSummary();
return taxArr
.map(({ amount }) => amount)
.reduce((a, b) => a.add(b), this.fyo.pesa(0));
}

async getTax(tax: string) {
if (!this._taxes[tax]) {
this._taxes[tax] = await this.fyo.doc.getDoc('Tax', tax);
Expand Down
1 change: 1 addition & 0 deletions models/baseModels/PrintSettings/PrintSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class PrintSettings extends Doc {
font?: string;
displayLogo?: boolean;
displayTime?: boolean;
posPrintWidth?: number;
amountInWords?: boolean;
override hidden: HiddenMap = {};
}
7 changes: 7 additions & 0 deletions schemas/app/PrintSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@
"label": "Display Time In Invoice",
"fieldtype": "Check",
"section": "Customizations"
},
{
"fieldname": "posPrintWidth",
"label": "Pos Print Width",
"fieldtype": "Float",
"default": 8,
"section": "Customizations"
}
]
}
24 changes: 12 additions & 12 deletions src/pages/POS/POS.vue
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,8 @@ export default defineComponent({
async createTransaction(shouldPrint = false) {
try {
await this.validate();
await this.submitSinvDoc(shouldPrint);
await this.makePayment();
await this.submitSinvDoc();
await this.makePayment(shouldPrint);
await this.makeStockTransfer();
await this.afterTransaction();
await this.setItems();
Expand All @@ -587,7 +587,7 @@ export default defineComponent({
});
}
},
async makePayment() {
async makePayment(shouldPrint: boolean) {
this.paymentDoc = this.sinvDoc.getPayment() as Payment;
const paymentMethod = this.paymentMethod;
Expand Down Expand Up @@ -623,6 +623,13 @@ export default defineComponent({
try {
await this.paymentDoc?.sync();
await this.paymentDoc?.submit();
if (shouldPrint) {
await routeTo(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`/print/${this.sinvDoc.schemaName}/${this.sinvDoc.name}`
);
}
} catch (error) {
return showToast({
type: 'error',
Expand Down Expand Up @@ -660,20 +667,13 @@ export default defineComponent({
});
}
},
async submitSinvDoc(shouldPrint: boolean) {
this.sinvDoc.once('afterSubmit', async () => {
async submitSinvDoc() {
this.sinvDoc.once('afterSubmit', () => {
showToast({
type: 'success',
message: t`Sales Invoice ${this.sinvDoc.name as string} is Submitted`,
duration: 'short',
});
if (shouldPrint) {
await routeTo(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`/print/${this.sinvDoc.schemaName}/${this.sinvDoc.name}`
);
}
});
try {
Expand Down
1 change: 0 additions & 1 deletion src/pages/TemplateBuilder/TemplateBuilder.vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
:container-styles="{ 'border-radius': '0px' }"
@change="async (value) => await setType(value)"
/>

<!-- Display Doc -->
<Link
v-if="doc.type"
Expand Down
52 changes: 47 additions & 5 deletions src/utils/printTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ import {
showExportInFolder,
} from './ui';
import { Money } from 'pesa';
import { SalesInvoice } from 'models/baseModels/SalesInvoice/SalesInvoice';

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 @@ -40,6 +47,28 @@ export async function getPrintTemplatePropValues(
const values: PrintValues = { doc: {}, print: {} };
values.doc = await getPrintTemplateDocValues(doc);

const totalTax = await (doc as Invoice)?.getTotalTax();
const paymentId = await (doc as SalesInvoice).getPaymentIds();

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

(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
);

const printSettings = await fyo.doc.getDoc(ModelNameEnum.PrintSettings);
const printValues = await getPrintTemplateDocValues(
printSettings,
Expand Down Expand Up @@ -413,7 +442,9 @@ function getAllCSSAsStyleElem() {
}

export async function updatePrintTemplates(fyo: Fyo) {
const templateFiles = await ipc.getTemplates();
const templateFiles = await ipc.getTemplates(
fyo.singles.PrintSettings?.posPrintWidth as number
);
const existingTemplates = (await fyo.db.getAll(ModelNameEnum.PrintTemplate, {
fields: ['name', 'modified'],
filters: { isCustom: false },
Expand All @@ -438,20 +469,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 @@ -465,6 +505,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
Loading

0 comments on commit cdeb7a9

Please sign in to comment.