mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-23 12:52:20 +01:00
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
|
import { computePriceWithCoupon } from './coupon';
|
||
|
import { Order } from '../models/order';
|
||
|
|
||
|
export default class OrderLib {
|
||
|
/**
|
||
|
* Get the order item total
|
||
|
*/
|
||
|
static itemAmount = (item): number => {
|
||
|
return item.quantity * Math.trunc(item.amount * 100) / 100;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* return true if order has offered item
|
||
|
*/
|
||
|
static hasOfferedItem = (order: Order): boolean => {
|
||
|
return order.order_items_attributes
|
||
|
.filter(i => i.is_offered).length > 0;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Get the offered item total
|
||
|
*/
|
||
|
static offeredAmount = (order: Order): number => {
|
||
|
return order.order_items_attributes
|
||
|
.filter(i => i.is_offered)
|
||
|
.map(i => Math.trunc(i.amount * 100) * i.quantity)
|
||
|
.reduce((acc, curr) => acc + curr, 0) / 100;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Get the total amount before offered amount
|
||
|
*/
|
||
|
static totalBeforeOfferedAmount = (order: Order): number => {
|
||
|
return (Math.trunc(order.total * 100) + Math.trunc(this.offeredAmount(order) * 100)) / 100;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Get the coupon amount
|
||
|
*/
|
||
|
static couponAmount = (order: Order): number => {
|
||
|
return (Math.trunc(order.total * 100) - Math.trunc(computePriceWithCoupon(order.total, order.coupon) * 100)) / 100.00;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Get the paid total amount
|
||
|
*/
|
||
|
static paidTotal = (order: Order): number => {
|
||
|
return computePriceWithCoupon(order.total, order.coupon);
|
||
|
};
|
||
|
}
|