2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 03:29:00 +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') { if (typeof fieldname === 'object') {
return await this.setMultiple(fieldname as DocValueMap); return await this.setMultiple(fieldname as DocValueMap);
} }
console.log(fieldname, value);
if (!this._canSet(fieldname, value)) { if (!this._canSet(fieldname, value)) {
return false; return false;

View File

@ -7,6 +7,16 @@ import { SelectOption } from 'schemas/types';
import { getCountryInfo } from 'utils/misc'; import { getCountryInfo } from 'utils/misc';
export default class SystemSettings extends Doc { 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 = { validations: ValidationMap = {
async displayPrecision(value: DocValue) { async displayPrecision(value: DocValue) {
if ((value as number) >= 0 && (value as number) <= 9) { if ((value as number) >= 0 && (value as number) <= 9) {

View File

@ -70,7 +70,7 @@ function formatCurrency(
doc: Doc | null, doc: Doc | null,
fyo: Fyo fyo: Fyo
): string { ): string {
const currency = getCurrency(field, doc, fyo); const currency = getCurrency(value as Money, field, doc, fyo);
let valueString; let valueString;
try { 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]; let getCurrency = doc?.getCurrencies?.[field.fieldname];
if (getCurrency !== undefined) { if (getCurrency !== undefined) {
return getCurrency(); return getCurrency();
@ -139,7 +152,7 @@ function getCurrency(field: Field, doc: Doc | null, fyo: Fyo): string {
return getCurrency(); return getCurrency();
} }
return (fyo.singles.SystemSettings?.currency as string) ?? DEFAULT_CURRENCY; return defaultCurrency;
} }
function getField(df: string | Field): Field { 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 { 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 { ValidationError } from 'fyo/utils/errors';
import { getExchangeRate } from 'models/helpers'; import { getExchangeRate } from 'models/helpers';
import { Transactional } from 'models/Transactional/Transactional'; import { Transactional } from 'models/Transactional/Transactional';
import { ModelNameEnum } from 'models/types'; import { ModelNameEnum } from 'models/types';
import { Money } from 'pesa'; import { Money } from 'pesa';
import { FieldTypeEnum, Schema } from 'schemas/types';
import { getIsNullOrUndef } from 'utils'; import { getIsNullOrUndef } from 'utils';
import { InvoiceItem } from '../InvoiceItem/InvoiceItem'; import { InvoiceItem } from '../InvoiceItem/InvoiceItem';
import { Party } from '../Party/Party'; import { Party } from '../Party/Party';
@ -50,6 +59,11 @@ export abstract class Invoice extends Transactional {
return this.fyo.singles.SystemSettings!.currency !== this.currency; return this.fyo.singles.SystemSettings!.currency !== this.currency;
} }
constructor(schema: Schema, data: DocValueMap, fyo: Fyo) {
super(schema, data, fyo);
this._setGetCurrencies();
}
async validate() { async validate() {
await super.validate(); await super.validate();
if ( if (
@ -363,4 +377,22 @@ export abstract class Invoice extends Transactional {
role: doc.isSales ? 'Customer' : 'Supplier', 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 { Doc } from 'fyo/model/doc';
import { import {
CurrenciesMap,
FiltersMap, FiltersMap,
FormulaMap, FormulaMap,
HiddenMap, HiddenMap,
ValidationMap ValidationMap
} from 'fyo/model/types'; } from 'fyo/model/types';
import { DEFAULT_CURRENCY } from 'fyo/utils/consts';
import { ValidationError } from 'fyo/utils/errors'; import { ValidationError } from 'fyo/utils/errors';
import { ModelNameEnum } from 'models/types'; import { ModelNameEnum } from 'models/types';
import { Money } from 'pesa'; import { Money } from 'pesa';
import { FieldTypeEnum, Schema } from 'schemas/types';
import { Invoice } from '../Invoice/Invoice'; import { Invoice } from '../Invoice/Invoice';
export abstract class InvoiceItem extends Doc { export abstract class InvoiceItem extends Doc {
account?: string; account?: string;
amount?: Money; amount?: Money;
baseAmount?: Money; baseAmount?: Money;
exchangeRate?: number;
parentdoc?: Invoice; parentdoc?: Invoice;
rate?: Money; rate?: Money;
quantity?: number; quantity?: number;
@ -39,6 +42,23 @@ export abstract class InvoiceItem extends Doc {
return !!this.fyo.singles?.AccountingSettings?.enableDiscounting; 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> { async getTotalTaxRate(): Promise<number> {
if (!this.tax) { if (!this.tax) {
return 0; return 0;
@ -338,6 +358,24 @@ export abstract class InvoiceItem extends Doc {
return { for: doc.isSales ? 'Sales' : 'Purchases' }; 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( function getDiscountedTotalBeforeTaxation(

View File

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