import java.util.*
import com.haulmont.cuba.core.global.*
import com.haulmont.cuba.core.entity.*;
import com.groupstp.rtneo.entity.*
import java.text.SimpleDateFormat
import com.haulmont.cuba.core.global.ViewRepository;
import com.haulmont.cuba.core.global.View;
return getContragentBills(contragentID)
public List<Accrual> getContragentBills(UUID contragentID) {
ViewRepository vRep = AppBeans.get(ViewRepository.NAME)
DataManager dataManager = AppBeans.get(DataManager.NAME)
View viewAccrual = vRep.getView(Accrual.class, "_local")
.addProperty("category", vRep.getView(RealEstateCategory.class, "_local")
.addProperty("unit", vRep.getView(Unit.class, "_local"))
)
.addProperty("realEstate", vRep.getView(RealEstate.class, "_local")
)
String sqlQuery = 'select e from rtneo$Accrual e where 1=0';
List<Accrual> accruals = new ArrayList<Accrual>();
List<Contract> contracts = dataManager.load(Contract.class)
.query('select e from rtneo$Contract e where e.contragent.id = :contragent and e.accepted =true order by e.createTs desc')
.parameter("contragent", contragentID)
.view("contract-edit")
.list();
if (contracts.size() > 0) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Contract lastContract = null;
for (Contract contract : contracts) {
if (lastContract == null) {
lastContract = contract;
sqlQuery = 'cast(a.contractPosition.contract.id text)=\'' + contract.getId().toString() + '\' ';
} else {
if (contract.getFrom().before(lastContract.getFrom())) {
sqlQuery = sqlQuery + ' or (cast(a.contractPosition.contract.id text)=\'' + contract.getId().toString() + '\' and a.period < \'' + sdf.format(lastContract.getFrom()) + '\' )';
lastContract = contract;
}
}
}
sqlQuery = 'select a from rtneo$Accrual a where a.contragent.id = :contragentID and (' + sqlQuery + ') order by a.period';
accruals = dataManager.load(Accrual.class)
.query(sqlQuery)
.parameter("contragentID", contragentID)
.view(viewAccrual)
.list();
}
return accruals;
}