2
0
mirror of https://github.com/frappe/books.git synced 2024-11-09 23:30:56 +00:00

incr: update currency display

This commit is contained in:
18alantom 2022-09-29 16:34:35 +05:30
parent a8532f05db
commit 537a8f7153
6 changed files with 100 additions and 9 deletions

View File

@ -191,7 +191,6 @@ export class Doc extends Observable<DocValue | Doc[]> {
if (typeof fieldname === 'object') {
return await this.setMultiple(fieldname as DocValueMap);
}
console.log(fieldname, value);
if (!this._canSet(fieldname, value)) {
return false;

View File

@ -7,6 +7,16 @@ import { SelectOption } from 'schemas/types';
import { getCountryInfo } from 'utils/misc';
export default class SystemSettings extends Doc {
dateFormat?: string;
locale?: string;
displayPrecision?: number;
internalPrecision?: number;
hideGetStarted?: boolean;
countryCode?: string;
currency?: string;
version?: string;
instanceId?: string;
validations: ValidationMap = {
async displayPrecision(value: DocValue) {
if ((value as number) >= 0 && (value as number) <= 9) {

View File

@ -70,7 +70,7 @@ function formatCurrency(
doc: Doc | null,
fyo: Fyo
): string {
const currency = getCurrency(field, doc, fyo);
const currency = getCurrency(value as Money, field, doc, fyo);
let valueString;
try {
@ -128,7 +128,20 @@ function getNumberFormatter(fyo: Fyo) {
}));
}
function getCurrency(field: Field, doc: Doc | null, fyo: Fyo): string {
function getCurrency(
value: Money,
field: Field,
doc: Doc | null,
fyo: Fyo
): string {
const currency = value?.getCurrency?.();
const defaultCurrency =
fyo.singles.SystemSettings?.currency ?? DEFAULT_CURRENCY;
if (currency && currency !== defaultCurrency) {
return currency;
}
let getCurrency = doc?.getCurrencies?.[field.fieldname];
if (getCurrency !== undefined) {
return getCurrency();
@ -139,7 +152,7 @@ function getCurrency(field: Field, doc: Doc | null, fyo: Fyo): string {
return getCurrency();
}
return (fyo.singles.SystemSettings?.currency as string) ?? DEFAULT_CURRENCY;
return defaultCurrency;
}
function getField(df: string | Field): Field {

View File

@ -1,11 +1,20 @@
import { DocValue } from 'fyo/core/types';
import { Fyo } from 'fyo';
import { DocValue, DocValueMap } from 'fyo/core/types';
import { Doc } from 'fyo/model/doc';
import { DefaultMap, FiltersMap, FormulaMap, HiddenMap } from 'fyo/model/types';
import {
CurrenciesMap,
DefaultMap,
FiltersMap,
FormulaMap,
HiddenMap
} from 'fyo/model/types';
import { DEFAULT_CURRENCY } from 'fyo/utils/consts';
import { ValidationError } from 'fyo/utils/errors';
import { getExchangeRate } from 'models/helpers';
import { Transactional } from 'models/Transactional/Transactional';
import { ModelNameEnum } from 'models/types';
import { Money } from 'pesa';
import { FieldTypeEnum, Schema } from 'schemas/types';
import { getIsNullOrUndef } from 'utils';
import { InvoiceItem } from '../InvoiceItem/InvoiceItem';
import { Party } from '../Party/Party';
@ -50,6 +59,11 @@ export abstract class Invoice extends Transactional {
return this.fyo.singles.SystemSettings!.currency !== this.currency;
}
constructor(schema: Schema, data: DocValueMap, fyo: Fyo) {
super(schema, data, fyo);
this._setGetCurrencies();
}
async validate() {
await super.validate();
if (
@ -363,4 +377,22 @@ export abstract class Invoice extends Transactional {
role: doc.isSales ? 'Customer' : 'Supplier',
}),
};
getCurrencies: CurrenciesMap = {};
_getCurrency() {
if (this.exchangeRate === 1) {
return this.fyo.singles.SystemSettings?.currency ?? DEFAULT_CURRENCY;
}
return this.currency ?? DEFAULT_CURRENCY;
}
_setGetCurrencies() {
const currencyFields = this.schema.fields.filter(
({ fieldtype }) => fieldtype === FieldTypeEnum.Currency
);
for (const { fieldname } of currencyFields) {
this.getCurrencies[fieldname] = this._getCurrency.bind(this);
}
}
}

View File

@ -1,21 +1,24 @@
import { DocValue } from 'fyo/core/types';
import { Fyo } from 'fyo';
import { DocValue, DocValueMap } from 'fyo/core/types';
import { Doc } from 'fyo/model/doc';
import {
CurrenciesMap,
FiltersMap,
FormulaMap,
HiddenMap,
ValidationMap
} from 'fyo/model/types';
import { DEFAULT_CURRENCY } from 'fyo/utils/consts';
import { ValidationError } from 'fyo/utils/errors';
import { ModelNameEnum } from 'models/types';
import { Money } from 'pesa';
import { FieldTypeEnum, Schema } from 'schemas/types';
import { Invoice } from '../Invoice/Invoice';
export abstract class InvoiceItem extends Doc {
account?: string;
amount?: Money;
baseAmount?: Money;
exchangeRate?: number;
parentdoc?: Invoice;
rate?: Money;
quantity?: number;
@ -39,6 +42,23 @@ export abstract class InvoiceItem extends Doc {
return !!this.fyo.singles?.AccountingSettings?.enableDiscounting;
}
get currency() {
return this.parentdoc?.currency ?? DEFAULT_CURRENCY;
}
get exchangeRate() {
return this.parentdoc?.exchangeRate ?? 1;
}
get isMultiCurrency() {
return this.parentdoc?.isMultiCurrency ?? false;
}
constructor(schema: Schema, data: DocValueMap, fyo: Fyo) {
super(schema, data, fyo);
this._setGetCurrencies();
}
async getTotalTaxRate(): Promise<number> {
if (!this.tax) {
return 0;
@ -338,6 +358,24 @@ export abstract class InvoiceItem extends Doc {
return { for: doc.isSales ? 'Sales' : 'Purchases' };
},
};
getCurrencies: CurrenciesMap = {};
_getCurrency() {
if (this.exchangeRate === 1) {
return this.fyo.singles.SystemSettings?.currency ?? DEFAULT_CURRENCY;
}
return this.currency;
}
_setGetCurrencies() {
const currencyFields = this.schema.fields.filter(
({ fieldtype }) => fieldtype === FieldTypeEnum.Currency
);
for (const { fieldname } of currencyFields) {
this.getCurrencies[fieldname] = this._getCurrency.bind(this);
}
}
}
function getDiscountedTotalBeforeTaxation(

View File

@ -390,7 +390,6 @@ export default {
}
},
methods: {
log: console.log,
routeTo,
toggleInvoiceSettings() {
if (!this.schemaName) {