import { SelectQueryBuilder } from "typeorm";
import CommonHelper from "../helpers/common";
import { OrmHelper } from "../helpers/orm";
import { SellingPricePercentage } from "entity";

export class SellingPricePercentageModel {
    static async list(filter = {}): Promise<SelectQueryBuilder<any>> {
        const repo = OrmHelper.DB.getRepository(SellingPricePercentage);
        let whereAttr: string[] = [];
        let whereVal: any = {};
        if (filter && Object.keys(filter).length > 0) {
            whereAttr = [...whereAttr, ...CommonHelper.handleQueryFilter(filter, "selling_price_percentage").whereAttr];
            whereVal = { ...whereVal, ...CommonHelper.handleQueryFilter(filter, "selling_price_percentage").whereVal };
        }

        var query = repo.createQueryBuilder("selling_price_percentage")
            .leftJoinAndSelect("selling_price_percentage.item_master", "item_master")
            .leftJoinAndSelect("selling_price_percentage.item_origin", "item_origin");
        if (whereAttr.length != 0) {
            query = query.where(whereAttr.join(" and "), whereVal);
        }

        return query;
    }
}