Skip to content

Commit

Permalink
feat: apply price list in POS
Browse files Browse the repository at this point in the history
  • Loading branch information
AbleKSaju committed Nov 27, 2024
1 parent 73c40d7 commit f573f13
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 41 deletions.
4 changes: 2 additions & 2 deletions models/baseModels/Invoice/Invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import { AppliedCouponCodes } from '../AppliedCouponCodes/AppliedCouponCodes';
import { CouponCode } from '../CouponCode/CouponCode';
import { SalesInvoice } from '../SalesInvoice/SalesInvoice';
import { SalesInvoiceItem } from '../SalesInvoiceItem/SalesInvoiceItem';
import { PriceListItem } from '../PriceList/PriceListItem';
import { PricingRuleItem } from '../PricingRuleItem/PricingRuleItem';

export type TaxDetail = {
account: string;
Expand Down Expand Up @@ -1332,7 +1332,7 @@ export abstract class Invoice extends Transactional {
item: item.item as string,
unit: item.unit as string,
},
})) as PriceListItem[];
})) as PricingRuleItem[];

return docs.map((doc) => doc.parent) as string[];
}
Expand Down
44 changes: 5 additions & 39 deletions models/baseModels/InvoiceItem/InvoiceItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import { safeParseFloat } from 'utils/index';
import { Invoice } from '../Invoice/Invoice';
import { Item } from '../Item/Item';
import { StockTransfer } from 'models/inventory/StockTransfer';
import { PriceList } from '../PriceList/PriceList';
import { isPesa } from 'fyo/utils';
import { PricingRule } from '../PricingRule/PricingRule';
import { getItemRateFromPriceList } from 'models/helpers';

export abstract class InvoiceItem extends Doc {
item?: string;
Expand Down Expand Up @@ -629,7 +629,10 @@ async function getItemRate(doc: InvoiceItem): Promise<Money | undefined> {
let priceListRate: Money | undefined;

if (doc.fyo.singles.AccountingSettings?.enablePriceList) {
priceListRate = await getItemRateFromPriceList(doc);
priceListRate = await getItemRateFromPriceList(
doc,
doc.parentdoc?.priceList as string
);
}

if (priceListRate) {
Expand Down Expand Up @@ -675,43 +678,6 @@ async function getItemRateFromPricingRule(
return pricingRuleDoc.discountRate;
}

async function getItemRateFromPriceList(
doc: InvoiceItem
): Promise<Money | undefined> {
const priceListName = doc.parentdoc?.priceList;
const item = doc.item;
if (!priceListName || !item) {
return;
}

const priceList = await doc.fyo.doc.getDoc(
ModelNameEnum.PriceList,
priceListName
);

if (!(priceList instanceof PriceList)) {
return;
}

const unit = doc.unit;
const transferUnit = doc.transferUnit;
const plItem = priceList.priceListItem?.find((pli) => {
if (pli.item !== item) {
return false;
}

if (transferUnit && pli.unit !== transferUnit) {
return false;
} else if (unit && pli.unit !== unit) {
return false;
}

return true;
});

return plItem?.rate;
}

function getDiscountedTotalBeforeTaxation(
rate: Money,
quantity: number,
Expand Down
40 changes: 40 additions & 0 deletions models/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ import { ValidationError } from 'fyo/utils/errors';
import { isPesa } from 'fyo/utils';
import { numberSeriesDefaultsMap } from './baseModels/Defaults/Defaults';
import { safeParseFloat } from 'utils/index';
import { PriceList } from './baseModels/PriceList/PriceList';
import { InvoiceItem } from './baseModels/InvoiceItem/InvoiceItem';
import { SalesInvoiceItem } from './baseModels/SalesInvoiceItem/SalesInvoiceItem';

export function getQuoteActions(
fyo: Fyo,
Expand Down Expand Up @@ -947,6 +950,43 @@ export async function getPricingRule(
return pricingRules;
}

export async function getItemRateFromPriceList(
doc: InvoiceItem | SalesInvoiceItem,
priceListName: string
): Promise<Money | undefined> {
const item = doc.item;
if (!priceListName || !item) {
return;
}

const priceList = await doc.fyo.doc.getDoc(
ModelNameEnum.PriceList,
priceListName
);

if (!(priceList instanceof PriceList)) {
return;
}

const unit = doc.unit;
const transferUnit = doc.transferUnit;
const plItem = priceList.priceListItem?.find((pli) => {
if (pli.item !== item) {
return false;
}

if (transferUnit && pli.unit !== transferUnit) {
return false;
} else if (unit && pli.unit !== unit) {
return false;
}

return true;
});

return plItem?.rate;
}

export function filterPricingRules(
pricingRuleDocsForItem: PricingRule[],
sinvDate: Date,
Expand Down
12 changes: 12 additions & 0 deletions src/pages/POS/POS.vue
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ import {
getPricingRule,
removeFreeItems,
getAddedLPWithGrandTotal,
getItemRateFromPriceList,
} from 'models/helpers';
import {
POSItem,
Expand Down Expand Up @@ -460,6 +461,17 @@ export default defineComponent({
item: item.name,
});
if (this.sinvDoc.priceList) {
let itemData = this.sinvDoc.items?.filter(
(val) => val.item == item.name
) as SalesInvoiceItem[];
itemData[0].rate = await getItemRateFromPriceList(
itemData[0],
this.sinvDoc.priceList
);
}
await this.applyPricingRule();
await this.sinvDoc.runFormulas();
},
Expand Down

0 comments on commit f573f13

Please sign in to comment.