Skip to content

Commit

Permalink
Merge pull request #661 from frappe/report-print
Browse files Browse the repository at this point in the history
feat: print reports to PDF
  • Loading branch information
18alantom authored Jun 13, 2023
2 parents 494e25e + dd54450 commit b184992
Show file tree
Hide file tree
Showing 12 changed files with 610 additions and 220 deletions.
3 changes: 3 additions & 0 deletions fyo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import * as errors from './utils/errors';
import { format } from './utils/format';
import { t, T } from './utils/translation';
import { ErrorLog } from './utils/types';
import type { reports } from 'reports/index';
import type { Report } from 'reports/Report';

export class Fyo {
t = t;
Expand Down Expand Up @@ -234,6 +236,7 @@ export class Fyo {
deviceId: '',
openCount: -1,
appFlags: {} as Record<string, boolean>,
reports: {} as Record<keyof typeof reports, Report | undefined>,
};
}

Expand Down
9 changes: 9 additions & 0 deletions models/baseModels/PrintSettings/PrintSettings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { Attachment } from 'fyo/core/types';
import { Doc } from 'fyo/model/doc';
import { HiddenMap } from 'fyo/model/types';

export class PrintSettings extends Doc {
logo?: Attachment;
email?: string;
phone?: string;
address?: string;
companyName?: string;
color?: string;
font?: string;
displayLogo?: boolean;
override hidden: HiddenMap = {};
}
7 changes: 3 additions & 4 deletions reports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { BalanceSheet } from './BalanceSheet/BalanceSheet';
import { GeneralLedger } from './GeneralLedger/GeneralLedger';
import { GSTR1 } from './GoodsAndServiceTax/GSTR1';
import { GSTR2 } from './GoodsAndServiceTax/GSTR2';
import { StockLedger } from './inventory/StockLedger';
import { StockBalance } from './inventory/StockBalance';
import { ProfitAndLoss } from './ProfitAndLoss/ProfitAndLoss';
import { Report } from './Report';
import { TrialBalance } from './TrialBalance/TrialBalance';
import { StockBalance } from './inventory/StockBalance';
import { StockLedger } from './inventory/StockLedger';

export const reports = {
GeneralLedger,
Expand All @@ -17,4 +16,4 @@ export const reports = {
GSTR2,
StockLedger,
StockBalance,
} as Record<string, typeof Report>;
} as const;
93 changes: 52 additions & 41 deletions src/pages/PrintView/PrintView.vue
Original file line number Diff line number Diff line change
@@ -1,47 +1,45 @@
<template>
<div class="flex">
<div class="flex flex-col flex-1 bg-gray-25">
<PageHeader :border="true">
<template #left>
<AutoComplete
v-if="templateList.length"
:df="{
fieldtype: 'AutoComplete',
fieldname: 'templateName',
label: t`Template Name`,
options: templateList.map((n) => ({ label: n, value: n })),
}"
input-class="text-base py-0 h-8"
class="w-56"
:border="true"
:value="templateName ?? ''"
@change="onTemplateNameChange"
/>
</template>
<DropdownWithActions :actions="actions" :title="t`More`" />
<Button class="text-xs" type="primary" @click="savePDF">
{{ t`Save as PDF` }}
</Button>
</PageHeader>
<!-- Template Display Area -->
<div class="overflow-auto custom-scroll p-4">
<!-- Display Hints -->
<div v-if="helperMessage" class="text-sm text-gray-700">
{{ helperMessage }}
</div>
<!-- Template Container -->
<PrintContainer
ref="printContainer"
v-if="printProps"
:template="printProps.template"
:values="printProps.values"
:scale="scale"
:width="templateDoc?.width"
:height="templateDoc?.height"
<div class="flex flex-col flex-1 bg-gray-25">
<PageHeader :border="true">
<template #left>
<AutoComplete
v-if="templateList.length"
:df="{
fieldtype: 'AutoComplete',
fieldname: 'templateName',
label: t`Template Name`,
options: templateList.map((n) => ({ label: n, value: n })),
}"
input-class="text-base py-0 h-8"
class="w-56"
:border="true"
:value="templateName ?? ''"
@change="onTemplateNameChange"
/>
</template>
<DropdownWithActions :actions="actions" :title="t`More`" />
<Button class="text-xs" type="primary" @click="savePDF">
{{ t`Save as PDF` }}
</Button>
</PageHeader>
<!-- Template Display Area -->
<div class="overflow-auto custom-scroll p-4">
<!-- Display Hints -->
<div v-if="helperMessage" class="text-sm text-gray-700">
{{ helperMessage }}
</div>
<!-- Template Container -->
<PrintContainer
ref="printContainer"
v-if="printProps"
:template="printProps.template"
:values="printProps.values"
:scale="scale"
:width="templateDoc?.width"
:height="templateDoc?.height"
/>
</div>
</div>
</template>
Expand All @@ -61,6 +59,7 @@ import { PrintValues } from 'src/utils/types';
import { getFormRoute, openSettings, routeTo } from 'src/utils/ui';
import { defineComponent } from 'vue';
import PrintContainer from '../TemplateBuilder/PrintContainer.vue';
import { showSidebar } from 'src/utils/refs';
export default defineComponent({
name: 'PrintView',
Expand Down Expand Up @@ -204,11 +203,22 @@ export default defineComponent({
this.values = await getPrintTemplatePropValues(this.doc as Doc);
}
},
setScale() {
this.scale = 1;
const width = (this.templateDoc?.width ?? 21) * 37.8;
let containerWidth = window.innerWidth - 32;
if (showSidebar.value) {
containerWidth -= 12 * 16;
}
this.scale = Math.min(containerWidth / width, 1);
},
reset() {
this.doc = null;
this.values = null;
this.templateList = [];
this.templateDoc = null;
this.scale = 1;
},
async onTemplateNameChange(value: string | null): Promise<void> {
if (!value) {
Expand All @@ -225,6 +235,7 @@ export default defineComponent({
} catch (error) {
await handleErrorWithDialog(error);
}
this.setScale();
},
async setTemplateList(): Promise<void> {
const list = (await this.fyo.db.getAllRaw(ModelNameEnum.PrintTemplate, {
Expand Down
Loading

0 comments on commit b184992

Please sign in to comment.