import com.haulmont.cuba.core.global.*
import java.util.*;
import java.util.function.Function;
import java.util.stream.*;
import com.groupstp.rtneo.service.BillService
import com.groupstp.rtneo.core.bean.tools.*
import java.text.SimpleDateFormat
import com.haulmont.cuba.core.entity.KeyValueEntity;
import com.haulmont.bali.util.ParamsMap;
BillService billService = AppBeans.get(BillService.NAME)
DatePeriodTools datePeriodTools = AppBeans.get(DatePeriodTools.class)
def df = new SimpleDateFormat("dd-MM-yyyy")
List<String> contragentsId = Stream.of('d693c9f3-ab3c-11b0-959b-42d6520d3221',
//'3323883d-bc6b-2b3d-6cae-c8f3e7c0f00b',
//'73cc71ad-9781-df00-cec0-3cdd10b4665a',
//'12e3f863-bb14-f791-5d96-db5dec07bdd4',
//'57b0aed7-7c7b-e846-ecfb-c0b2de3f647b',
//'951fd1ee-8516-6558-4e16-22627b2ba39c',
//'5328ef64-a3f4-9dd6-624d-08f7544fca66',
//'01b5d8ca-4644-e4b1-d293-a31a2175f525',
//'302a57e5-321a-7a8f-c782-e58c90f80efd',
//'9a043162-86ab-30e6-cbfb-f08e21cf7032',
//'96628742-5397-0a5c-8b87-2e46e24b0d1d',
//'4d2f7dc4-2c40-5ea0-c558-6c77d14855b7',
//'5a5cd480-23c1-c20d-e7eb-705fcb0139cf',
//'4265d203-17a7-7e91-60ff-694fa87d57d1',
//'bf3387ec-04d0-fd2a-0b61-d7130ef97e81',
//'9cdb3b45-44a2-0867-b939-867c4db81273',
//'18048d32-113c-b3c1-2d69-1ad44615e176',
//'65e474a2-fea4-e61c-237b-c266111ace28',
//'f98b1fcc-8779-dad0-35fa-129977b98119',
//'647e772f-2bbc-c1bf-510d-317246d293e6',
//'e1cd2753-aaec-b024-ff63-f4b8c2f0b2f1',
//'adb86ed7-1658-82d7-9fdd-1f76aaafa2e3',
//'8bce8ac6-d459-2488-0cf1-108c86635e2e',
//'71e77985-8ebf-3398-be87-7878a5283282',
//'60b6eb26-d6e4-ac39-b9c9-0df9b7ea11e2',
//'5adc88f9-acf9-aea1-e349-4a83adfb251c',
//'f0cd0b2b-ccfe-0ee3-8b99-b9e07b823151',
//'a08765b2-ed3b-2c6f-0219-50bbdbc180f7',
//'59c4e362-a607-ad53-e5c3-28ba5a5c4449',
//'a2255219-1941-b311-3b97-769278a70af4',
//'0a808a77-fa58-7c08-8dbd-1b6a0819b1c6',
//'9c942c2e-db09-7640-eb78-1aec67ac2990',
//'49c9f17c-b19a-568c-a1a9-c258cbb4bccb',
//'9e3240e0-597d-5aa3-f831-5135bac43cfe',
//'a416e142-a6a1-89c4-7982-d7270aab92f3',
//'878ee9bf-ff3a-8e84-e342-e84c709266a4',
//'25742a21-88ed-ce08-4011-89921846167f',
//'c8592177-8559-e627-09df-1ea3e0d1b93d',
//'b25d7b86-9459-5058-2d55-dd6b8f5de1d0',
//'3b74c39f-b58b-e73f-76ae-4dfeb2f72201',
//'bcb2c104-a632-a4c3-26ee-50591587c29d',
//'f8ab2b4c-7d77-092f-21a7-12092ded2588',
//'4f1c07c1-cbc4-0aa4-9b86-ebe811b1fe86',
//'9684a5fc-dcbe-c719-d407-a3480d99857f',
//'9622a3f9-3d40-db14-9f7d-35317d6f53fd',
//'4a4bf44f-d787-8efa-b562-ab5bbc6ddca7',
//'d72c63b3-bab5-3508-76e2-8b0140550067',
'c0b9dde7-9665-2026-2fbf-cbe19067998f').collect(Collectors.toList())
List<KeyValueEntity> listPA = dataManager.loadValues('select distinct r.renterRecord.contragent.id, r.renterRecord.contragent.personalAccount, r.renterRecord.contragent.inn, r.renterRecord.contragent.kpp from rtneo$RealEstateRenter r inner join rtneo$Payment p where r.renterRecord.contragent.inn = p.inn and r.contragentRealEstate.contragent.id in :contragentsId')
.properties("id", "pa", "inn", "kpp")
.parameter("contragentsId", contragentsId)
.list()
//def listPA = dataManager.loadValues('select distinct a.contragent.id, a.contragent.personalAccount, a.contragent.inn, a.contragent.kpp from rtneo$Accrual a join rtneo$Payment p '+
// 'where a.contragent.inn = p.inn '+
// 'and (p.inn is not null or not p.inn = \'\') '+
// 'and a.period > a.contractPosition.contract.before '+
// 'and (select t.tariff from rtneo$Tariff t where t.name = 1 and a.period between t.dateSince and t.dateTill)*1.2 <> a.price order by a.contragent.id')
// .properties("id", "pa", "inn", "kpp")
//// .firstResult(0)
//// .maxResults(10)
// .list()
List<Object> listItems = new ArrayList<>()
for(def pa : listPA){
def count = dataManager.loadValue('select count(c) from rtneo$Contragent c where c.inn = :inn', Integer.class)
.parameter("inn", pa.getValue('inn'))
.optional().orElse(0)
if(count>1)continue
def accruals = billService.getContragentBills(pa.getValue('id'));
// def maps = accruals.stream().collect(Collectors.toMap({pa.getValue("inn")}, {p -> ParamsMap.of("number", p.getId())}))
// def maps2 = accruals.stream().collect(Collectors.groupingBy(
// {pa.getValue("inn")}, Collectors.mapping(
// {pa.getValue("id")},
// {p->ParamsMap.of("inn", pa.getValue("inn"), "kpp", pa.getValue("kpp"))}
// )
// ))
// def maps2 = accruals.stream().collect(Collectors.groupingBy({}))
// maps2.each{_(it)}
def prevNumber = null
for(def accrual : accruals){
if(accrual.getPeriod().compareTo(df.parse("31-12-2019"))<=0){
// if(prevNumber.equals(accrual.getDocumentNumber()))continue
HashMap<String, Object> itemMap = new HashMap<>();
itemMap.put("pa", pa.getValue('pa'))
itemMap.put("inn", pa.getValue('inn'))
itemMap.put("kpp", pa.getValue('kpp'))
itemMap.put("period", df.format(accrual.getPeriod()))
itemMap.put("number", accrual.getDocumentNumber())
// def sum = accruals.stream().filter{e->e.getDocumentNumber().equals(accrual.getDocumentNumber())}.map{e->e.getTotalSum()}.reduce{a, b -> a.add(b)}.orElse(null)
// itemMap.put("sum", sum)
// def sumNoNDS = sum/120*100
// def sumNDS = sum.setScale(2, BigDecimal.ROUND_HALF_UP) - sumNoNDS.setScale(2, BigDecimal.ROUND_HALF_UP)
// itemMap.put("sumNoNDS", sumNoNDS.setScale(2, BigDecimal.ROUND_HALF_UP))
// itemMap.put("sumNDS", sumNDS.setScale(2, BigDecimal.ROUND_HALF_UP))
// itemMap.put("goodTariff", tariff.getValue('tariff').setScale(2, BigDecimal.ROUND_HALF_UP))
prevNumber = accrual.getDocumentNumber()
listItems.add(itemMap)
continue
}
}
}
//listItems = listItems.stream().sorted{o1, o2 -> o1.get("period").compareTo(o2.get("period"))}
_("|ИНН|КПП|Период|Номер")
def maps2 = listItems.stream()
// .sorted{o1, o2 -> df.parse(o1.get("period")).compareTo(df.parse(o2.get("period")))}//Судя по всему не сортируемый список
.collect(
Collectors.groupingBy({p->p.get("inn")},
Collectors.groupingBy({p->p.get("period")},
Collectors.groupingBy({p->p.get("number")}))
))
for(def inn : maps2.entrySet()){
for(def period : inn.getValue().entrySet()){
period = period.getValue().entrySet().stream().forEach{e->_(e)}//.sorted{o1, o2 -> df.parse(o1.get("period")).compareTo(df.parse(o2.get("period")))}
for(def number : period.getValue().entrySet()){
_("|${inn.getKey()}|${number.getValue().get(0).get('kpp')}|${period.getKey()}|${number.getKey()}")
}
}
}
//listItems.each{_("|${it.get('inn')}|${it.get('kpp')}|${it.get('period')}|${it.get('number')}")}
/**
* Логирование
*/
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)}