2
0
mirror of https://github.com/frappe/books.git synced 2024-12-22 10:58:59 +00:00

fix: Print View sync issue

- hide multi currency fields cause widget is sufficient
- exchange rate update issue
This commit is contained in:
18alantom 2023-05-08 12:00:14 +05:30
parent a8e8225ad6
commit 2007e4339d
5 changed files with 77 additions and 21 deletions

View File

@ -20,6 +20,7 @@ import { isPesa } from '../utils/index';
import { getDbSyncError } from './errorHelpers';
import {
areDocValuesEqual,
getFormulaSequence,
getMissingMandatoryMessage,
getPreDefaultValues,
setChildDocIdx,
@ -815,9 +816,9 @@ export class Doc extends Observable<DocValue | Doc[]> {
}
async _applyFormulaForFields(doc: Doc, fieldname?: string) {
const formulaFields = this.schema.fields.filter(
({ fieldname }) => this.formulas?.[fieldname]
);
const formulaFields = getFormulaSequence(this.formulas)
.map((f) => this.fyo.getField(this.schemaName, f))
.filter(Boolean);
let changed = false;
for (const field of formulaFields) {

View File

@ -1,11 +1,11 @@
import { Fyo } from 'fyo';
import { DocValue } from 'fyo/core/types';
import { isPesa } from 'fyo/utils';
import { isEqual } from 'lodash';
import { Money } from 'pesa';
import { cloneDeep, isEqual } from 'lodash';
import { Field, FieldType, FieldTypeEnum } from 'schemas/types';
import { getIsNullOrUndef } from 'utils';
import { Doc } from './doc';
import { FormulaMap } from './types';
export function areDocValuesEqual(
dvOne: DocValue | Doc[],
@ -149,3 +149,42 @@ export function setChildDocIdx(childDocs: Doc[]) {
childDocs[idx].idx = +idx;
}
}
export function getFormulaSequence(formulas: FormulaMap) {
const depMap = Object.keys(formulas).reduce((acc, k) => {
acc[k] = formulas[k]?.dependsOn;
return acc;
}, {} as Record<string, string[] | undefined>);
return sequenceDependencies(cloneDeep(depMap));
}
function sequenceDependencies(
depMap: Record<string, string[] | undefined>
): string[] {
/**
* Sufficiently okay algo to sequence dependents after
* their dependencies
*/
const keys = Object.keys(depMap);
const independent = keys.filter((k) => !depMap[k]?.length);
const dependent = keys.filter((k) => depMap[k]?.length);
const keyset = new Set(independent);
for (const k of dependent) {
const deps = depMap[k] ?? [];
deps.push(k);
while (deps.length) {
const d = deps.shift()!;
if (keyset.has(d)) {
continue;
}
keyset.add(d);
}
}
return Array.from(keyset).filter((k) => k in depMap);
}

View File

@ -384,6 +384,7 @@ export abstract class Invoice extends Transactional {
return await this.getExchangeRate();
},
dependsOn: ['party', 'currency'],
},
netTotal: { formula: async () => this.getSum('items', 'amount', false) },
taxes: { formula: async () => await this.getTaxSummary() },
@ -391,6 +392,7 @@ export abstract class Invoice extends Transactional {
baseGrandTotal: {
formula: async () =>
(this.grandTotal as Money).mul(this.exchangeRate! ?? 1),
dependsOn: ['grandTotal', 'exchangeRate'],
},
outstandingAmount: {
formula: async () => {
@ -471,9 +473,6 @@ export abstract class Invoice extends Transactional {
baseGrandTotal: () =>
this.exchangeRate === 1 || this.baseGrandTotal!.isZero(),
grandTotal: () => !this.taxes?.length,
entryCurrency: () => !this.isMultiCurrency,
currency: () => !this.isMultiCurrency,
exchangeRate: () => !this.isMultiCurrency,
stockNotTransferred: () => !this.stockNotTransferred,
outstandingAmount: () =>
!!this.outstandingAmount?.isZero() || !this.isSubmitted,

View File

@ -121,7 +121,7 @@
"fieldtype": "Link",
"target": "Currency",
"readOnly": true,
"tab": "Settings"
"hidden": true
},
{
"fieldname": "exchangeRate",
@ -129,7 +129,7 @@
"fieldtype": "Float",
"default": 1,
"readOnly": true,
"tab": "Settings"
"hidden": true
},
{
"fieldname": "discountAfterTax",

View File

@ -91,21 +91,20 @@ export default defineComponent({
};
},
async mounted() {
this.doc = await fyo.doc.getDoc(this.schemaName, this.name);
await this.setTemplateList();
await this.initialize();
if (fyo.store.isDevelopment) {
// @ts-ignore
window.pv = this;
}
await this.setTemplateFromDefault();
if (!this.templateDoc && this.templateList.length) {
await this.onTemplateNameChange(this.templateList[0]);
}
if (this.doc) {
this.values = await getPrintTemplatePropValues(this.doc as Doc);
}
},
async activated() {
await this.initialize();
},
unmounted() {
this.reset();
},
deactivated() {
this.reset();
},
computed: {
helperMessage() {
@ -191,6 +190,24 @@ export default defineComponent({
},
},
methods: {
async initialize() {
this.doc = await fyo.doc.getDoc(this.schemaName, this.name);
await this.setTemplateList();
await this.setTemplateFromDefault();
if (!this.templateDoc && this.templateList.length) {
await this.onTemplateNameChange(this.templateList[0]);
}
if (this.doc) {
this.values = await getPrintTemplatePropValues(this.doc as Doc);
}
},
reset() {
this.doc = null;
this.values = null;
this.templateList = [];
this.templateDoc = null;
},
async onTemplateNameChange(value: string | null): Promise<void> {
if (!value) {
this.templateDoc = null;