2
0
mirror of https://github.com/frappe/books.git synced 2024-11-14 01:14:03 +00:00

fix: TypeError, .round is not a func

- type vue files for Currency, Float, Int, Data
- use Number for safe parse cause faster
This commit is contained in:
18alantom 2023-03-31 11:16:09 +05:30 committed by Alan
parent 7c99a76e20
commit 368714a84a
6 changed files with 72 additions and 39 deletions

View File

@ -1,9 +1,9 @@
import { Fyo } from 'fyo'; import { Fyo } from 'fyo';
import { Doc } from 'fyo/model/doc'; import { Doc } from 'fyo/model/doc';
import { DateTime } from 'luxon'; import { DateTime } from 'luxon';
import { Money } from 'pesa';
import { Field, FieldType, FieldTypeEnum } from 'schemas/types'; import { Field, FieldType, FieldTypeEnum } from 'schemas/types';
import { getIsNullOrUndef, safeParseFloat, titleCase } from 'utils'; import { getIsNullOrUndef, safeParseFloat, titleCase } from 'utils';
import { isPesa } from '.';
import { import {
DEFAULT_CURRENCY, DEFAULT_CURRENCY,
DEFAULT_DATE_FORMAT, DEFAULT_DATE_FORMAT,
@ -138,8 +138,8 @@ function formatNumber(value: unknown, fyo: Fyo): string {
value = fyo.pesa(value.toFixed(20)); value = fyo.pesa(value.toFixed(20));
} }
if ((value as Money).round) { if (isPesa(value)) {
const floatValue = safeParseFloat((value as Money).round()); const floatValue = safeParseFloat(value.round());
return numberFormatter.format(floatValue); return numberFormatter.format(floatValue);
} }

View File

@ -9,7 +9,7 @@
class="text-end" class="text-end"
:class="[inputClasses, containerClasses]" :class="[inputClasses, containerClasses]"
:type="inputType" :type="inputType"
:value="value?.round()" :value="round(value)"
:placeholder="inputPlaceholder" :placeholder="inputPlaceholder"
:readonly="isReadOnly" :readonly="isReadOnly"
:tabindex="isReadOnly ? '-1' : '0'" :tabindex="isReadOnly ? '-1' : '0'"
@ -29,13 +29,16 @@
</div> </div>
</div> </div>
</template> </template>
<script lang="ts">
<script> // @ts-nocheck
import { isPesa } from 'fyo/utils';
import { Money } from 'pesa';
import { fyo } from 'src/initFyo'; import { fyo } from 'src/initFyo';
import { nextTick } from 'vue'; import { safeParseFloat } from 'utils/index';
import { defineComponent, nextTick } from 'vue';
import Float from './Float.vue'; import Float from './Float.vue';
export default { export default defineComponent({
name: 'Currency', name: 'Currency',
extends: Float, extends: Float,
emits: ['input', 'focus'], emits: ['input', 'focus'],
@ -51,17 +54,44 @@ export default {
this.showInput = true; this.showInput = true;
this.$emit('focus', e); this.$emit('focus', e);
}, },
parse(value) { round(v: unknown) {
return fyo.pesa(value); if (!isPesa(v)) {
v = this.parse(v);
}
if (isPesa(v)) {
return v.round();
}
return fyo.pesa(0).round();
}, },
onBlur(e) { parse(value: unknown): Money {
let { value } = e.target; if (isPesa(value)) {
if (value !== 0 && !value) { return value;
value = fyo.pesa(0).round(); }
if (typeof value === 'string') {
value = safeParseFloat(value);
}
if (typeof value === 'number') {
return fyo.pesa(value);
}
if (typeof value === 'bigint') {
return fyo.pesa(value);
}
return fyo.pesa(0);
},
onBlur(e: FocusEvent) {
const target = e.target;
if (!(target instanceof HTMLInputElement)) {
return;
} }
this.showInput = false; this.showInput = false;
this.triggerChange(value); this.triggerChange(target.value);
}, },
activateInput() { activateInput() {
if (this.isReadOnly) { if (this.isReadOnly) {
@ -79,5 +109,5 @@ export default {
return fyo.format(this.value ?? fyo.pesa(0), this.df, this.doc); return fyo.format(this.value ?? fyo.pesa(0), this.df, this.doc);
}, },
}, },
}; });
</script> </script>

View File

@ -1,13 +1,14 @@
<script> <script lang="ts">
import Base from './Base'; import Base from './Base.vue';
import { defineComponent } from 'vue';
export default { export default defineComponent({
name: 'Data', name: 'Data',
extends: Base, extends: Base,
computed: { computed: {
inputType() { inputType() {
return 'text'; return 'text';
} },
} },
}; });
</script> </script>

View File

@ -1,19 +1,20 @@
<script> <script lang="ts">
import { safeParseFloat } from 'utils/index'; import { safeParseFloat } from 'utils/index';
import { defineComponent } from 'vue';
import Int from './Int.vue'; import Int from './Int.vue';
export default { export default defineComponent({
name: 'Float', name: 'Float',
extends: Int, extends: Int,
computed: { computed: {
inputType() { inputType() {
return 'number'; return 'number';
}
},
methods: {
parse(value) {
return safeParseFloat(value)
}, },
}, },
}; methods: {
parse(value: unknown): number {
return safeParseFloat(value);
},
},
});
</script> </script>

View File

@ -1,7 +1,9 @@
<script> <script lang="ts">
import Data from "./Data.vue"; import Data from './Data.vue';
import { defineComponent } from 'vue';
import { safeParseInt } from 'utils/index';
export default { export default defineComponent({
name: 'Int', name: 'Int',
extends: Data, extends: Data,
computed: { computed: {
@ -10,10 +12,9 @@ export default {
}, },
}, },
methods: { methods: {
parse(value) { parse(value: unknown): number {
const parsedValue = parseInt(value, 10); return safeParseInt(value);
return isNaN(parsedValue) ? 0 : parsedValue;
}, },
}, },
}; });
</script> </script>

View File

@ -180,11 +180,11 @@ function safeParseNumber(value: unknown, parser: (v: string) => number) {
} }
export function safeParseFloat(value: unknown): number { export function safeParseFloat(value: unknown): number {
return safeParseNumber(value, parseFloat); return safeParseNumber(value, Number);
} }
export function safeParseInt(value: unknown): number { export function safeParseInt(value: unknown): number {
return safeParseNumber(value, parseInt); return safeParseNumber(value, (v: string) => Math.trunc(Number(v)));
} }
export function joinMapLists<A, B>( export function joinMapLists<A, B>(