Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: price list #653

Merged
merged 3 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions models/baseModels/AccountingSettings/AccountingSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { getCountryInfo } from 'utils/misc';
export class AccountingSettings extends Doc {
enableDiscounting?: boolean;
enableInventory?: boolean;
enablePriceList?: boolean;

static filters: FiltersMap = {
writeOffAccount: () => ({
Expand Down
6 changes: 6 additions & 0 deletions models/baseModels/Invoice/Invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export abstract class Invoice extends Transactional {
party?: string;
account?: string;
currency?: string;
priceList?: string;
netTotal?: Money;
grandTotal?: Money;
baseGrandTotal?: Money;
Expand Down Expand Up @@ -514,6 +515,7 @@ export abstract class Invoice extends Transactional {
attachment: () =>
!(this.attachment || !(this.isSubmitted || this.isCancelled)),
backReference: () => !this.backReference,
priceList: () => !this.fyo.singles.AccountingSettings?.enablePriceList,
};

static defaults: DefaultMap = {
Expand Down Expand Up @@ -544,6 +546,10 @@ export abstract class Invoice extends Transactional {
accountType: doc.isSales ? 'Receivable' : 'Payable',
}),
numberSeries: (doc: Doc) => ({ referenceType: doc.schemaName }),
priceList: (doc: Doc) => ({
enabled: true,
...(doc.isSales ? { selling: true } : { buying: true }),
}),
};

static createFilters: FiltersMap = {
Expand Down
21 changes: 20 additions & 1 deletion models/baseModels/InvoiceItem/InvoiceItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { safeParseFloat } from 'utils/index';
import { Invoice } from '../Invoice/Invoice';
import { Item } from '../Item/Item';
import { StockTransfer } from 'models/inventory/StockTransfer';
import { getPriceListRate } from 'models/helpers';

export abstract class InvoiceItem extends Doc {
item?: string;
Expand Down Expand Up @@ -48,6 +49,18 @@ export abstract class InvoiceItem extends Doc {
return this.schemaName === 'SalesInvoiceItem';
}

get date() {
return this.parentdoc?.date ?? undefined;
}

get party() {
return this.parentdoc?.party ?? undefined;
}

get priceList() {
return this.parentdoc?.priceList ?? undefined;
}

get discountAfterTax() {
return !!this?.parentdoc?.discountAfterTax;
}
Expand Down Expand Up @@ -101,12 +114,15 @@ export abstract class InvoiceItem extends Doc {
},
rate: {
formula: async (fieldname) => {
const rate = (await this.fyo.getValue(
const priceListRate = await getPriceListRate(this);
const itemRate = (await this.fyo.getValue(
'Item',
this.item as string,
'rate'
)) as undefined | Money;

const rate = priceListRate instanceof Money ? priceListRate : itemRate;

if (!rate?.float && this.rate?.float) {
return this.rate;
}
Expand Down Expand Up @@ -144,6 +160,9 @@ export abstract class InvoiceItem extends Doc {
return rateFromTotals ?? rate ?? this.fyo.pesa(0);
},
dependsOn: [
'date',
'priceList',
'batch',
'party',
'exchangeRate',
'item',
Expand Down
60 changes: 60 additions & 0 deletions models/baseModels/ItemPrice/ItemPrice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { t } from 'fyo';
import { DocValue } from 'fyo/core/types';
import { Doc } from 'fyo/model/doc';
import { ValidationMap } from 'fyo/model/types';
import { ValidationError } from 'fyo/utils/errors';
import { getItemPrice } from 'models/helpers';
import { ModelNameEnum } from 'models/types';
import { Money } from 'pesa';

export class ItemPrice extends Doc {
item?: string;
rate?: Money;
validFrom?: Date;
validUpto?: Date;

get isBuying() {
return !!this.parentdoc?.buying;
}

get isSelling() {
return !!this.parentdoc?.selling;
}

get priceList() {
return this.parentdoc?.name;
}

validations: ValidationMap = {
validUpto: async (value: DocValue) => {
if (!value || !this.validFrom) {
return;
}
if (value < this.validFrom) {
throw new ValidationError(
t`Valid From date can not be greater than Valid To date.`
);
}

const itemPrice = await getItemPrice(
this,
this.validFrom,
this.validUpto
);

if (!itemPrice) {
return;
}

const priceList = (await this.fyo.getValue(
ModelNameEnum.ItemPrice,
itemPrice,
'parent'
)) as string;

throw new ValidationError(
t`an Item Price already exists for the given date in Price List ${priceList}`
);
},
};
}
18 changes: 18 additions & 0 deletions models/baseModels/PriceList/PriceList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Doc } from 'fyo/model/doc';
import { ListViewSettings } from 'fyo/model/types';
import { ItemPrice } from '../ItemPrice/ItemPrice';
import { getPriceListStatusColumn } from 'models/helpers';

export class PriceList extends Doc {
enabled?: boolean;
buying?: boolean;
selling?: boolean;
isUomDependent?: boolean;
priceListItem?: ItemPrice[];

static getListViewSettings(): ListViewSettings {
return {
columns: ['name', getPriceListStatusColumn()],
};
}
}
220 changes: 220 additions & 0 deletions models/baseModels/tests/testPriceList.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
import test from 'tape';
import { getDefaultMetaFieldValueMap } from 'backend/helpers';
import { closeTestFyo, getTestFyo, setupTestFyo } from 'tests/helpers';
import { ModelNameEnum } from 'models/types';
import { getItem } from 'models/inventory/tests/helpers';
import { getItemPrice } from 'models/helpers';
import { SalesInvoiceItem } from '../SalesInvoiceItem/SalesInvoiceItem';
import { SalesInvoice } from '../SalesInvoice/SalesInvoice';
import { PurchaseInvoiceItem } from '../PurchaseInvoiceItem/PurchaseInvoiceItem';

const fyo = getTestFyo();
setupTestFyo(fyo, __filename);

const itemMap = {
Pen: {
name: 'Pen',
rate: 100,
hasBatch: true,
},
Ink: {
name: 'Ink',
rate: 50,
},
};

const partyMap = {
partyOne: {
name: 'John Whoe',
email: 'john@whoe.com',
},
};

const batchMap = {
batchOne: {
name: 'PL-AB001',
manufactureDate: '2022-11-03T09:57:04.528',
},
};

const priceListMap = {
PL_SELL: {
name: 'PL_SELL',
enabled: true,
party: 'Shaju',
buying: false,
selling: true,
isUomDependent: false,
itemPrice: [
{
enabled: true,
item: itemMap.Pen.name,
rate: 101,
buying: false,
selling: true,
party: partyMap.partyOne.name,
validFrom: '2023-02-28T18:30:00.678Z',
validUpto: '2023-03-30T18:30:00.678Z',
...getDefaultMetaFieldValueMap(),
},
],
},
PL_BUY: {
name: 'PL_BUY',
enabled: true,
buying: true,
selling: false,
isUomDependent: false,
itemPrice: [
{
enabled: true,
item: itemMap.Pen.name,
rate: 102,
buying: true,
selling: false,
party: partyMap.partyOne.name,
validFrom: '2023-02-28T18:30:00.678Z',
validUpto: '2023-03-30T18:30:00.678Z',
...getDefaultMetaFieldValueMap(),
},
],
},
PL_SB: {
name: 'PL_SB',
enabled: true,
selling: true,
buying: true,
isUomDependent: false,
itemPrice: [
{
enabled: true,
item: itemMap.Pen.name,
rate: 104,
batch: batchMap.batchOne.name,
buying: true,
selling: true,
party: partyMap.partyOne.name,
validFrom: '2023-05-05T18:30:00.000Z',
validUpto: '2023-06-05T18:30:00.000Z',
...getDefaultMetaFieldValueMap(),
},
],
},
};

test('Price List: create dummy items, parties and batches', async (t) => {
// Create Items
for (const { name, rate } of Object.values(itemMap)) {
const item = getItem(name, rate, false);
await fyo.doc.getNewDoc(ModelNameEnum.Item, item).sync();
t.ok(await fyo.db.exists(ModelNameEnum.Item, name), `Item: ${name} exists`);
}

// Create Parties
for (const { name, email } of Object.values(partyMap)) {
await fyo.doc.getNewDoc(ModelNameEnum.Party, { name, email }).sync();
t.ok(
await fyo.db.exists(ModelNameEnum.Party, name),
`Party: ${name} exists`
);
}

// Create Batches
for (const batch of Object.values(batchMap)) {
await fyo.doc.getNewDoc(ModelNameEnum.Batch, batch).sync();
t.ok(
await fyo.db.exists(ModelNameEnum.Batch, batch.name),
`Batch: ${batch.name} exists`
);
}
});

test('create Price Lists', async (t) => {
for (const priceListItem of Object.values(priceListMap)) {
await fyo.doc.getNewDoc(ModelNameEnum.PriceList, priceListItem).sync();
t.ok(
await fyo.db.exists(ModelNameEnum.PriceList, priceListItem.name),
`Price List ${priceListItem.name} exists`
);
}
});

test('check item price', async (t) => {
// check selling enabled item price
const sinv = fyo.doc.getNewDoc(ModelNameEnum.SalesInvoice, {
items: [{ item: itemMap.Pen.name, quantity: 1 }],
date: priceListMap.PL_SELL.itemPrice[0].validFrom,
priceList: priceListMap.PL_SELL.name,
party: partyMap.partyOne.name,
}) as SalesInvoice;

const sinvItem = Object.values(sinv.items ?? {})[0];
const sellEnabled = await getItemPrice(sinvItem as SalesInvoiceItem);

const sellEnabledPLName = await fyo.getValue(
ModelNameEnum.ItemPrice,
sellEnabled as string,
'parent'
);

t.equal(sellEnabledPLName, priceListMap.PL_SELL.name);

// check buying enabled item price
const pinv = fyo.doc.getNewDoc(ModelNameEnum.PurchaseInvoice, {
items: [{ item: itemMap.Pen.name, quantity: 1 }],
date: priceListMap.PL_BUY.itemPrice[0].validFrom,
priceList: priceListMap.PL_BUY.name,
party: partyMap.partyOne.name,
}) as SalesInvoice;

const pinvItem = Object.values(pinv.items ?? {})[0];
const buyEnabled = await getItemPrice(pinvItem as PurchaseInvoiceItem);

const buyEnabledPLName = await fyo.getValue(
ModelNameEnum.ItemPrice,
buyEnabled as string,
'parent'
);

t.equal(buyEnabledPLName, priceListMap.PL_BUY.name);

// check sell batch enabled
const sinv1 = fyo.doc.getNewDoc(ModelNameEnum.SalesInvoice, {
items: [
{ item: itemMap.Pen.name, quantity: 1, batch: batchMap.batchOne.name },
],
date: priceListMap.PL_SB.itemPrice[0].validFrom,
priceList: priceListMap.PL_SB.name,
party: partyMap.partyOne.name,
}) as SalesInvoice;

const sinv1Item = Object.values(sinv1.items ?? {})[0];
const sellBatchEnabled = await getItemPrice(sinv1Item as SalesInvoiceItem);

const sellBatchEnabledPLName = await fyo.getValue(
ModelNameEnum.ItemPrice,
sellBatchEnabled as string,
'parent'
);

t.equal(sellBatchEnabledPLName, priceListMap.PL_SB.name);

// undefined returns
const sinv2 = fyo.doc.getNewDoc(ModelNameEnum.SalesInvoice, {
items: [{ item: itemMap.Ink.name, quantity: 1 }],
date: priceListMap.PL_SELL.itemPrice[0].validFrom,
priceList: priceListMap.PL_SELL.name,
party: partyMap.partyOne.name,
}) as SalesInvoice;

const sinv2Item = Object.values(sinv2.items ?? {})[0];
const nonExistItem = await getItemPrice(sinv2Item as SalesInvoiceItem);

t.equal(
nonExistItem,
undefined,
'itemPrice of non-existing item in price list returns false'
);
});

closeTestFyo(fyo, __filename);
Loading
Loading