import java.util.*
import com.haulmont.cuba.core.global.*
import com.haulmont.cuba.core.entity.*;
import com.groupstp.rtneo.entity.*
import com.haulmont.cuba.core.entity.FileDescriptor
import com.haulmont.cuba.core.app.FileStorageAPI
import java.io.InputStream
import java.io.InputStreamReader
import java.io.ByteArrayOutputStream
import org.apache.commons.io.IOUtils
import java.nio.charset.StandardCharsets;
import com.opencsv.CSVReader;
import com.groupstp.rtneo.service.BillService
import java.text.SimpleDateFormat
import org.apache.commons.collections.CollectionUtils
import java.util.stream.Collectors;
import com.groupstp.rtneo.core.bean.tools.*
def df1 = new SimpleDateFormat("yyyy-MM-dd")
def dateBill = df1.parse('2020-07-16')
FileStorageAPI fileStorageAPI=AppBeans.get(FileStorageAPI.NAME);
BillService billService = AppBeans.get(BillService.NAME)
DatePeriodTools datePeriodTools = AppBeans.get(DatePeriodTools.class)
//Контрагенты для выгрузки из файла
String id1='1199fba4-ffe8-b147-aab2-7ec19fa6edea'
FileDescriptor fd1 = dataManager.load(FileDescriptor.class)
.id(UuidProvider.fromString(id1))
.view(View.MINIMAL)
.optional().orElse(null);
if(fd1 == null){throw new RuntimeException("fileNotFound")}
InputStream inputStream1=fileStorageAPI.openStream(fd1)
CSVReader reader1 = new CSVReader(new InputStreamReader(inputStream1, "UTF-8"));
List<String> listInnInclude = reader1.readAll().stream()
.map{e->e[0]}.collect(Collectors.toList())
//listInnInclude.each{_(it)}
List<Contragent> contragents = dataManager.load(Contragent.class)
.query('select distinct c from rtneo$Contragent c where c.inn in :list and c.personalAccount is not null')
.parameter("list", listInnInclude)
.view("_local")
.list()
for(Contragent contragent : contragents){
List<Contract> contracts = dataManager.load(Contract.class)
.query('select c from rtneo$Contract c where c.contragent.id = :id and c.accepted = true and size(c.files)=0')
.parameter("id", contragent.getId())
.view("_local")
.list()
for(Contract contract : contracts){
createDigitalContract(contract)
}
def periods = dataManager.loadValues('select min(a.period), max(a.period) from rtneo$Accrual a where a.contragent.id = :id and a.contractPosition.contract.accepted = true')
.properties("minPeriod", "maxPeriod")
.parameter("id", contragent.getId())
.one()
_("${contragent.getInn()}: ${df.format(periods.getValue('minPeriod'))} - ${df.format(periods.getValue('maxPeriod'))} : ${contracts.size()}")
def accruals = calculate(periods.getValue('minPeriod'), periods.getValue('maxPeriod'), contragent, dateBill)
_("Create accruals: ${accruals.size()}")
// def sums = accruals.stream().collect(Colectors.groupingBy(p->p.getPeriod(), Collectors.reducing((a,b)->a.getTotalSum().add(b.getTotalSum()))))
def sums = accruals.groupBy{it.getPeriod()}.collectEntries{a,b -> [a, b.stream().map{p->p.getTotalSum()}.reduce{z,x -> z.add(x)}.get()]}
sums.each{
if(it.getValue() == 0)_("${df.format(it.getKey())} - ${it.getValue()}")
}
}
import com.groupstp.rtneo.service.ContractService
import com.haulmont.bali.util.ParamsMap;
private void createDigitalContract(Contract contract) {
ContractService contractService = AppBeans.get(ContractService.NAME)
final List<Map<String, Object>> reportParams = new ArrayList<>();
reportParams.add(ParamsMap.of("contract", contract));
HashMap<String, String> codes = contractService.getTamplateCode(contract);
UUID printKey = contractService.printContractPdfFile(contract, codes.get("report"), codes.get("template"), reportParams);
}
import com.groupstp.rtneo.core.bean.calculation.*;
def calculate(Date from, Date to, Contragent contragent, Date dateBill){
CalculationWorkerBean calculationWorker = AppBeans.get(CalculationWorkerBean.class);
return calculationWorker.calculateAccruals(from, to, contragent, dateBill, null);
}
/**
* Логирование
*/
import com.haulmont.cuba.core.app.serialization.EntitySerializationAPI;
import com.groupstp.rtneo.util.JsonUtil;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
private _(Object obj, String... options){
if(obj == null){log("LOG.ERROR: Object is null!!!");return}
if(options.size() == 0){log(obj)}
for(def option : options){
if(option.equals("str")){log(obj)}
if(option.equals("for")){obj.each({_(it)})}
if(option.equals("json")){
EntitySerializationAPI entitySerializationAPI = AppBeans.get(EntitySerializationAPI.NAME)
try{log(entitySerializationAPI.toJson(obj))}
catch(Exception e){log("LOG.ERROR: JSON entity serialization failed")}
}
if(option.equals("objJson")){
JsonUtil jsonUtil = AppBeans.get(JsonUtil.NAME)
try{log.(jsonUtil.toJson(obj))}
catch(Exception e){log("LOG.ERROR: JSON object serialization failed")}
}
if(option.equals("date")){
DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
try{log(df.format(obj))}
catch(Exception e){log("LOG.ERROR: Failed date format")}
}
}
}
private log(Object obj){log.debug(obj)}