import com.haulmont.cuba.core.entity.Entity
import com.haulmont.cuba.core.global.*
import com.groupstp.rtneo.entity.*
import com.groupstp.rtneo.service.*
import com.groupstp.rtneo.core.bean.*
import com.haulmont.cuba.core.global.ViewRepository;
import com.haulmont.cuba.core.global.View;
import java.util.function.*;
import com.haulmont.cuba.core.TransactionalDataManager
import java.math.RoundingMode;
import org.apache.commons.collections4.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import com.haulmont.bali.util.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.diedavids.cuba.runtimediagnose.groovy.*
import groovy.transform.InheritConstructors;
import com.groupstp.rtneo.util.*;
import java.util.stream.Collectors
import java.time.*;
df = new SimpleDateFormat("dd.MM.yyyy");
def start = df.parse('01.01.2020')
def end = df.parse('31.12.2020')
CalculationWorker calcWorker = AppBeans.get(CalculationWorker.class)
ViewRepository vRep = AppBeans.get(ViewRepository.NAME)
View contractView = vRep.getView(Contract.class, "contract-edit")
.addProperty("positions", vRep.getView(ContractPosition.class, "_local")
.addProperty("contragent", vRep.getView(Contragent.class, "_local"))
.addProperty("category", vRep.getView(RealEstateCategory.class, "_local")
.addProperty("unit", vRep.getView(Unit.class, "_local"))
)
.addProperty("wasteProject", vRep.getView(WasteGenerationProject.class, "_local"))
.addProperty("contragentRealEstate", vRep.getView(ContragentRealEstate.class, "_local")
.addProperty("category", vRep.getView(RealEstateCategory.class, "_local"))
.addProperty("wasteGenerationProjects", vRep.getView(WasteGenerationProject.class, "_local"))
.addProperty("contragent", vRep.getView(Contragent.class, "_local"))
.addProperty("realEstate", vRep.getView(RealEstate.class, "_local")
.addProperty("zone", vRep.getView(Zone.class, "_local"))
)
)
)
class Period{
Date max
Date min
Period(Date maxPeriod, Date minPeriod){
this.max = maxPeriod
this.min = minPeriod
}
}
HashMap<Contract, Period> map = new HashMap<>()
//String query = 'select a.contractPosition.contract.id, min(a.period), max(a.period) from rtneo$Accrual a where a.contractPosition.contract.template = \'MSG310820\' group by a.contractPosition.contract.id having max(a.period) < cast(\'2020-12-01\' Date)'
String query = 'select distinct a.contractPosition.contract.id, min(a.period), max(a.period) from rtneo$Accrual a join rtneo$EntityesTemporarySet e on a.contractPosition.contract.id = e.entityId where e.setName = \'ContractForProlongation-28-11-20\' group by a.contractPosition.contract.id having max(a.period) < cast(\'2020-12-01\' Date)'
def valueContracts = dataManager.loadValues(query)
.properties('contract_id', 'min_period', 'max_period')
//.firstResult(2)
//.maxResults(1)
.list()
log.debug(valueContracts.size())
//return
//map = valueContracts.stream().collect(Collectors.toMap(
// {k-> dataManager.load(Contract.class).id(k.getValue('contract_id')).view(contractView).one()},
// {v-> new Period(v.getValue('max_period'), v.getValue('min_period'))}
// ))
for(def item : valueContracts){
Contract c = dataManager.load(Contract.class).id(item.getValue('contract_id')).view(contractView).one()
Period period = new Period(item.getValue('max_period'), item.getValue('min_period'))
Calendar cal = Calendar.getInstance()
cal.setTime(period.max)
cal.add(Calendar.MONTH, 1)
try{
calcWorker.calculateAccruals(cal.getTime(), end, c.getContragent(), c.getDate(), c);
}catch(Exception e){
log.debug("=========")
log.debug(cal.getTime())
log.debug(end)
log.debug(c)
log.debug(c.getContragent())
log.debug(e.getStackTrace())
log.debug("---------")
}
}
//for(def item : map.entrySet()){
// Contract item_ct = item.getKey()
// Period item_val = item.getValue()
// log.debug(item_ct)
//
// log.debug(item_val.minPeriod)
// log.debug(item_val.maxPeriod)
//}
//for(Contract item : contracts){
// log.debug(item.getContragent())
//// CommitContext commitContext = new CommitContext();
//// accruals = calcWorker.calculateAccruals(start, end, item.getContragent(), item.getDate(), item, commitContext);
// log.debug(accruals)
//// dataManager.commit(commitContext)
//}