package com.axelor.apps.suppliermanagement.db.repo;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.axelor.apps.account.db.BudgetDistribution;
import com.axelor.apps.suppliermanagement.db.PurchaseOrderSupplierLine;

public class PurchaseOrderSupplierLineManagementRepository extends PurchaseOrderSupplierLineRepository {

    @Override
    public Map<String, Object> populate(Map<String, Object> json, Map<String, Object> context) {
        Long supplierLine = (Long) json.get("id");
        PurchaseOrderSupplierLine line = find(supplierLine);
        List<BudgetDistribution> budgetDistributiList = line.getPurchaseOrderLine().getBudgetDistributionList();
        BigDecimal  totalBudgetAmountAvailable = BigDecimal.ZERO;
        for (BudgetDistribution budgetDistribution : budgetDistributiList) {
            totalBudgetAmountAvailable = totalBudgetAmountAvailable.add(budgetDistribution.getBudgetAmountAvailable());
        }
        json.put("budgetRemainingAmount", totalBudgetAmountAvailable);

        return super.populate(json, context);
    }

}
