Untitled

mail@pastecode.io avatar
unknown
plain_text
10 days ago
4.0 kB
1
Indexable
Never
async function GetTotalBalance(model: any):
    Promise<ServiceResponse<any>> {
        try {
        const currentDate = new Date();
        const date = currentDate.getDate();
        const month = currentDate.getMonth() + 1;
        const year = currentDate.getFullYear();
        const Getdates = () => {
            let startdate;
            let enddate;

            if (date <= 30 && date >= 21) {
                enddate = new Date(year, month - 1, 20);
                startdate = new Date(year, month - 2, 20);
            }
            if (date <= 20 && date >= 11) {
                enddate = new Date(year, month - 1, 10);
                startdate = new Date(year, month - 2, 10);
            }
            if (date <= 1 && date >= 10) {
                enddate = new Date(year, month - 2, 30);
                startdate = new Date(year, month - 3, 30);
            }
            return { startdate, enddate };
        }


        let dateDetails = Getdates();
        console.log(dateDetails);


        console.log('model ', model);



        let productSalesToAgent = await AppDataSource.getRepository(entities.ProductSalesToAgent)
            .createQueryBuilder("productSalesToAgent")
            .leftJoinAndSelect("productSalesToAgent.SoldToAgent", "productSoldToAgent")
            .where("DATE(productSalesToAgent.CreatedAt) >= :startDate", { startDate: moment(dateDetails.startdate).format('DD-MM-YYYY') })
            .andWhere("DATE(productSalesToAgent.CreatedAt) <= :endDate", { endDate: moment(dateDetails.enddate).format('DD-MM-YYYY') })
            .andWhere("productSalesToAgent.SoldToAgent =:SoldToAgent", { SoldToAgent: model.organizationUnitId })
            .getMany();

        const milkCollectionDetails = await AppDataSource.getRepository(entities.MilkCollectionDetails).createQueryBuilder("milkCollectionDetails")
            .leftJoinAndSelect("milkCollectionDetails.OrganizationUnitId", "organization")
            .where("DATE(milkCollectionDetails.CollectedAt) >= :startDate", { startDate: moment(dateDetails.startdate).format('DD-MM-YYYY') })
            .andWhere("DATE(milkCollectionDetails.CollectedAt) <= :endDate", { endDate: moment(dateDetails.enddate).format('DD-MM-YYYY') })
            .andWhere("milkCollectionDetails.OrganizationUnitId =:OrganizationUnitId", { OrganizationUnitId: model.organizationUnitId })
            .getMany();


        const rateMaster = await AppDataSource.getRepository(entities.RateMaster)
            .createQueryBuilder("ratemaster")
            .where("ratemaster.Wef < :currDate", { currDate: new Date() })
            .andWhere("ratemaster.IsActive =:isActive", { isActive: true })
            .orderBy("ratemaster.SeqNo", "ASC")
            .getMany();


        let totalProductValue = 0;
        let totalMilkValue = 0;
        console.log('milk collection details : ', milkCollectionDetails)
        console.log('milk products bougt details : ', milkCollectionDetails)

        productSalesToAgent.forEach(element => {
            // console.log('Balance:',element.Balance);
            totalProductValue += element.Balance;
        });


        milkCollectionDetails.forEach(element => {
            let value = calculateValue(element.Fat, element.Snf, rateMaster);
            // console.log('vales:',value,element.Weight+value);
            totalMilkValue += element.Weight * value
        });

        console.log('total mmilk value  : ', totalMilkValue)
        let result = { balance: totalMilkValue - totalProductValue }

        // console.log("For ID :",req.params.id,"MilkValue:",TotalMilkValue,'Productvalue:',TotalProductValue);

        return {
            status: 200,
            message: SUCCESS_MESSAGES.SUCCESS,
            data: result,
        };

    } catch (e) {
        console.error(e); // Log the actual error
        console.log(e);
        return {
            status: 400,
            message: ERROR_MESSAGES.INTERNAL_SERVER,
            data: null,
        };

    }
}
Leave a Comment