<commit_before>import { Price } from "../catalogue/model/publish/price";
import { Quantity } from "../catalogue/model/publish/quantity";
import {currencyToString, roundToTwoDecimals} from "./utils";
import { ItemPriceWrapper } from "./item-price-wrapper";
import {defaultVatRate} from "./constants";
import {Item} from '../catalogue/model/publish/item';

/**
 * Wrapper around a price and a quantity, contains convenience methods to get the total price,
 * price per item and their string representations.
 *
 * This class can also be substituted for a Quantity.
 */
export class PriceWrapper {
    /** hjid field from Quantity class */
    //hjid: string = null;

    itemPrice: ItemPriceWrapper;

    constructor(public price: Price,
                public vatPercentage: number = defaultVatRate,
                public orderedQuantity: Quantity = new Quantity(1, price.baseQuantity.unitCode),
                public item:Item = null) {
        this.itemPrice = new ItemPriceWrapper(price);
    }

    get totalPrice(): number {
        if(!this.itemPrice.hasPrice()) {
            return 0;
        }

        const amount = Number(this.price.priceAmount.value);
        const baseQuantity = this.price.baseQuantity.value || 1;
        return this.orderedQuantity.value * amount / baseQuantity;
    }

    set totalPrice(price: number) {
        const quantity = this.orderedQuantity.value || 1;
        const baseQuantity = this.price.baseQuantity.value || 1;
        this.price.priceAmount.value = price / quantity * baseQuantity
    }

    get totalPriceString(): string {
        if(!this.itemPrice.hasPrice()) {
            return "Not specified";
        }
        return `${roundToTwoDecimals(this.totalPrice)} ${this.currency}`;
    }

    get pricePerItem(): number {
        return this.price.priceAmount.value / this.price.baseQuantity.value;
    }

    get pricePerItemString(): string {
        const amount = this.price.priceAmount;
        const qty = this.price.baseQuantity
        const baseQuantity = qty.value || 1;

        if(!amount.value || amount.value == 0 || !this.isOrderedQuantityValid()) {
            return "On demand";
        }

        if(baseQuantity === 1) {
            return `${roundToTwoDecimals(amount.value)} ${currencyToString(amount.currencyID)} per ${qty.unitCode}`
        }
        return `${roundToTwoDecimals(amount.value)} ${currencyToString(amount.currencyID)} for ${baseQuantity} ${qty.unitCode}`
    }

    get vatTotal(): number {
        return this.totalPrice * this.vatPercentage / 100;
    }

    get vatTotalString(): string {
        return `${roundToTwoDecimals(this.vatTotal)} ${this.currency}`
    }

    get grossTotal(): number {
        return this.totalPrice + this.vatTotal;
    }

    get grossTotalString(): string {
        return `${roundToTwoDecimals(this.grossTotal)} ${this.currency}`;
    }

    get currency(): string {
        return currencyToString(this.price.priceAmount.currencyID);
    }

    set currency(currency: string) {
        this.price.priceAmount.currencyID = currency;
    }

    private isOrderedQuantityValid(): boolean {<commit_msg>modified pricePerItemString method of price-wrapper to have a common logic in pricePerItem methods<commit_after>import { Price } from "../catalogue/model/publish/price";
import { Quantity } from "../catalogue/model/publish/quantity";
import {currencyToString, roundToTwoDecimals} from "./utils";
import { ItemPriceWrapper } from "./item-price-wrapper";
import {defaultVatRate} from "./constants";
import {Item} from '../catalogue/model/publish/item';

/**
 * Wrapper around a price and a quantity, contains convenience methods to get the total price,
 * price per item and their string representations.
 *
 * This class can also be substituted for a Quantity.
 */
export class PriceWrapper {
    /** hjid field from Quantity class */
    //hjid: string = null;

    itemPrice: ItemPriceWrapper;

    constructor(public price: Price,
                public vatPercentage: number = defaultVatRate,
                public orderedQuantity: Quantity = new Quantity(1, price.baseQuantity.unitCode),
                public item:Item = null) {
        this.itemPrice = new ItemPriceWrapper(price);
    }

    get totalPrice(): number {
        if(!this.itemPrice.hasPrice()) {
            return 0;
        }

        const amount = Number(this.price.priceAmount.value);
        const baseQuantity = this.price.baseQuantity.value || 1;
        return this.orderedQuantity.value * amount / baseQuantity;
    }

    set totalPrice(price: number) {
        const quantity = this.orderedQuantity.value || 1;
        const baseQuantity = this.price.baseQuantity.value || 1;
        this.price.priceAmount.value = price / quantity * baseQuantity
    }

    get totalPriceString(): string {
        if(!this.itemPrice.hasPrice()) {
            return "Not specified";
        }
        return `${roundToTwoDecimals(this.totalPrice)} ${this.currency}`;
    }

    get pricePerItem(): number {
        return this.price.priceAmount.value / this.price.baseQuantity.value;
    }

    get pricePerItemString(): string {
        const amount = this.price.priceAmount;
        const qty = this.price.baseQuantity
        const baseQuantity = qty.value || 1;

        if(!amount.value || amount.value == 0 || !this.isOrderedQuantityValid()) {
            return "On demand";
        }

        return `${roundToTwoDecimals(this.pricePerItem)} ${currencyToString(this.price.priceAmount.currencyID)} per ${qty.unitCode}`;
    }

    get vatTotal(): number {
        return this.totalPrice * this.vatPercentage / 100;
    }

    get vatTotalString(): string {
        return `${roundToTwoDecimals(this.vatTotal)} ${this.currency}`
    }

    get grossTotal(): number {
        return this.totalPrice + this.vatTotal;
    }

    get grossTotalString(): string {
        return `${roundToTwoDecimals(this.grossTotal)} ${this.currency}`;
    }

    get currency(): string {
        return currencyToString(this.price.priceAmount.currencyID);
    }

    set currency(currency: string) {
        this.price.priceAmount.currencyID = currency;
    }

    private isOrderedQuantityValid(): boolean {
{'fraction_non_alphanumeric': 0.1164891900613101, 'fraction_numerical': 0.003388189738625363, 'mean_word_length': 2.8030674846625767, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3374624', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1677, 'n_words': 418}