if (packetServiceTemplateID != 0) {
PacketTemplate packetTemplate = findEntity(PacketTemplate.class, packetServiceTemplateID);
List<ContractPacket> contractPachetList = findEntityList(ContractPacket.class, "packetServiceTemplate", packetTemplate);
contractSet = new HashSet<Contract>();
for (int i = 0; i < contractPachetList.size(); i++) {
Contract contract = (contractPachetList.get(i)).getContract();
contractSet.add(contract);
}
}
Collaborator collab = findEntity(Collaborator.class, getCollaboratorId());
Lpu lpu = collab.getLpu();
//Поиск действующих полисов для пациента
Client client = findEntity(Client.class, clientID);
String sql = "SELECT a FROM Polis a" + " WHERE a.client=:client" +
" AND (a.fromDate <= :current OR a.fromDate IS NULL)" +
" AND (a.tillDate >= :current OR a.tillDate IS NULL)" +
" AND (a.trash = false)" +
" AND (a.contract.begin<=:current)" +
" AND (a.contract.end>=:current OR a.contract.end IS NULL) " +
" AND ((a.contract.lpu is null) or (a.contract.lpu = :lpu))";
Query query = manager.createQuery(sql);
query.setParameter("client", client);
query.setParameter("current", new Date());
query.setParameter("lpu", lpu);
//Найденные действующие полиса для пациента, которые находятся в контракте
List<Polis> existPolisList = query.getResultList();
String sql1 = "SELECT c FROM Contract c WHERE" +
" c.begin<=:current" +
" AND (c.end>=:current OR c.end IS NULL)" +
" AND c.openForAll = true";
Query query1 = manager.createQuery(sql1);
query1.setParameter("current", new Date());
/**Список контрактов, открытых для всех*/
List<Contract> cList = query1.getResultList();
//Виртуальные полиса, которые можно создать
ArrayList<Polis> virtualPolisList = new ArrayList<Polis>(cList.size());
for (Contract contract : cList) {
Polis polis = new Polis();
polis.setClient(client);
polis.setContract(contract);
virtualPolisList.add(polis);
}
List<ServiceContractPriceChunk> chunkList = new ArrayList<ServiceContractPriceChunk>();
//Складываем виртуальные и существующие полиса в один массив
for (int i = 0; i < virtualPolisList.size(); i++) {
Polis virtPolis = virtualPolisList.get(i);
boolean founded = false;
for (Polis existPolis : existPolisList) {
if (existPolis.getContract().getId() == virtPolis.getContract().getId()) {
founded = true;
break;
}
}
if (!founded) {
existPolisList.add(virtPolis);
}
}
for (Polis polis : existPolisList) {
ServiceContractPriceChunk chunk = new ServiceContractPriceChunk();
Contract contract = polis.getContract();
if (contractSet != null && !contractSet.contains(contract)) {
//Если данный договор не содержит данного пакета услуг
continue;
}
chunk.polisDetails = polis.getDetails((RightChecker) this);
ContractDetails contractDetails = contract.getDetails((RightChecker) this);
chunk.enterpriseID = contractDetails.enterpriseID;
chunk.index = contractDetails.index;
chunk.contractType = Contract.TYPES_ARRAY[contractDetails.type].str;
if (services.size() > 0) {