2022-04-22 11:02:03 +00:00
|
|
|
import { Fyo } from 'fyo';
|
2022-04-24 06:48:44 +00:00
|
|
|
import { Doc } from 'fyo/model/doc';
|
2022-11-07 07:15:38 +00:00
|
|
|
import { Action } from 'fyo/model/types';
|
2022-05-23 05:30:54 +00:00
|
|
|
import { Money } from 'pesa';
|
2022-04-27 08:37:06 +00:00
|
|
|
import { Field, OptionField, SelectOption } from 'schemas/types';
|
2022-11-07 07:15:38 +00:00
|
|
|
import { getIsNullOrUndef, safeParseInt } from 'utils';
|
2022-04-08 06:59:42 +00:00
|
|
|
|
|
|
|
export function slug(str: string) {
|
|
|
|
return str
|
|
|
|
.replace(/(?:^\w|[A-Z]|\b\w)/g, function (letter, index) {
|
|
|
|
return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
|
|
|
|
})
|
|
|
|
.replace(/\s+/g, '');
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:08:36 +00:00
|
|
|
export function unique<T>(list: T[], key = (it: T) => String(it)) {
|
2022-04-08 06:59:42 +00:00
|
|
|
const seen: Record<string, boolean> = {};
|
|
|
|
return list.filter((item) => {
|
|
|
|
const k = key(item);
|
|
|
|
return seen.hasOwnProperty(k) ? false : (seen[k] = true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getDuplicates(array: unknown[]) {
|
|
|
|
const duplicates: unknown[] = [];
|
|
|
|
for (const i in array) {
|
2022-11-07 07:15:38 +00:00
|
|
|
const previous = array[safeParseInt(i) - 1];
|
2022-04-08 06:59:42 +00:00
|
|
|
const current = array[i];
|
|
|
|
|
|
|
|
if (current === previous) {
|
|
|
|
if (!duplicates.includes(current)) {
|
|
|
|
duplicates.push(current);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return duplicates;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isPesa(value: unknown): boolean {
|
2022-05-23 05:30:54 +00:00
|
|
|
return value instanceof Money;
|
2022-04-08 06:59:42 +00:00
|
|
|
}
|
2022-04-20 06:38:47 +00:00
|
|
|
|
2022-04-21 13:08:36 +00:00
|
|
|
export function getActions(doc: Doc): Action[] {
|
|
|
|
const Model = doc.fyo.models[doc.schemaName];
|
2022-04-20 06:38:47 +00:00
|
|
|
if (Model === undefined) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:08:36 +00:00
|
|
|
return Model.getActions(doc.fyo);
|
2022-04-20 06:38:47 +00:00
|
|
|
}
|
2022-04-22 11:02:03 +00:00
|
|
|
|
|
|
|
export async function getSingleValue(
|
|
|
|
fieldname: string,
|
|
|
|
parent: string,
|
|
|
|
fyo: Fyo
|
|
|
|
) {
|
|
|
|
if (!fyo.db.isConnected) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const res = await fyo.db.getSingleValues({ fieldname, parent });
|
|
|
|
const singleValue = res.find(
|
|
|
|
(f) => f.fieldname === fieldname && f.parent === parent
|
|
|
|
);
|
|
|
|
|
|
|
|
if (singleValue === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return singleValue.value;
|
|
|
|
}
|
2022-04-27 08:37:06 +00:00
|
|
|
|
|
|
|
export function getOptionList(
|
|
|
|
field: Field,
|
2022-04-29 19:04:08 +00:00
|
|
|
doc: Doc | undefined | null
|
2022-04-27 08:37:06 +00:00
|
|
|
): SelectOption[] {
|
|
|
|
const list = getRawOptionList(field, doc);
|
|
|
|
return list.map((option) => {
|
|
|
|
if (typeof option === 'string') {
|
|
|
|
return {
|
|
|
|
label: option,
|
|
|
|
value: option,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return option;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-29 19:04:08 +00:00
|
|
|
function getRawOptionList(field: Field, doc: Doc | undefined | null) {
|
2022-04-27 08:37:06 +00:00
|
|
|
const options = (field as OptionField).options;
|
|
|
|
if (options && options.length > 0) {
|
|
|
|
return (field as OptionField).options;
|
|
|
|
}
|
|
|
|
|
2022-04-29 19:04:08 +00:00
|
|
|
if (getIsNullOrUndef(doc)) {
|
2022-04-27 08:37:06 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-04-29 19:04:08 +00:00
|
|
|
const Model = doc!.fyo.models[doc!.schemaName];
|
2022-04-27 08:37:06 +00:00
|
|
|
if (Model === undefined) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const getList = Model.lists[field.fieldname];
|
|
|
|
if (getList === undefined) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-04-29 19:04:08 +00:00
|
|
|
return getList(doc!);
|
2022-04-27 08:37:06 +00:00
|
|
|
}
|