/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package beans.report.dloregister;
import beans.UserRightsSet;
import beans.contract.entity.Polis;
import beans.directory.dlodruglist.DloDrugList;
import beans.directory.lpu.entity.Lpu;
import beans.directory.simple.entities.Excemption;
import beans.directory.simple.entities.FinanceSource;
import beans.doctor.prescriptiondlo.PrescriptionDlo;
import beans.user.client.ClientBean;
import beans.user.client.entity.Client;
import beans.user.client.facilityClient.DloClientUnique;
import beans.user.client.facilityClient.FacilityClient;
import beans.user.collaborator.entities.Collaborator;
import framework.beans.SecuredBean;
import framework.beans.address.entities.Address;
import framework.beans.address.entities.AddressObject;
import framework.beans.client.clientDocument.ClientDocument;
import framework.beans.config.server.ConfigBeanM;
import framework.beans.config.server.ConfigBeanRemoteM;
import framework.beans.directory.simple.entities.ClientDocumentType;
import framework.beans.directory.simple.entities.Name;
import framework.beans.directory.simple.entities.Patronymic;
import framework.beans.directory.simple.entities.Sex;
import framework.beans.directory.simple.entities.Surname;
import framework.beans.security.BeanRights;
import framework.generic.ClipsServerException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.Query;
/**
*
* @author lacoste
*/
@Stateless(mappedName="clips-beans/DloRegisterBean")
public class DloRegisterBean extends SecuredBean implements DloRegisterBeanRemote {
public static final int COMMAND_READ = 0;
private HashMap<DloClientUnique, Integer> clientMap;
private HashMap<Integer, Polis> clientPolisMap;
@Override
protected void initBeanRights() throws ClipsServerException {
int[] r = new int[1];
r[COMMAND_READ] = RightPresence(UserRightsSet.EXECUTE_REPORT.id);
rights = new BeanRights(r);
}
@Override
public List<PersonalData> getPersonalDataList() throws ClipsServerException {
checkCommandAccessibility(COMMAND_READ);
List<PersonalData> res = new ArrayList<PersonalData>();
clientPolisMap = new HashMap<Integer, Polis>();
clientMap = new HashMap<DloClientUnique, Integer>();
List<FacilityClient> facClientList = findEntityList(FacilityClient.class);
String sql = "select c.id, c.surname, c.name, c.pathronymic, c.born, c.snils from Client c WHERE c.id=132702";
Query query = manager.createQuery(sql);
List<Object[]> list = query.getResultList();
for (Object[] line: list) {
DloClientUnique dcu = new DloClientUnique();
Integer clientID = (Integer) line[0];
Surname surname = (Surname) line[1];
Name name = (Name) line[2];
Patronymic pathronymic = (Patronymic) line[3];
dcu.surnameID = surname == null ? 0 : surname.getId();
dcu.nameID = name == null ? 0 : name.getId();
dcu.pathronID = pathronymic == null ? 0 : pathronymic.getId();
dcu.dateBorn = (Date) line[4];
dcu.snils = line[5] != null ? ((String) line[5]).trim() : "";
clientMap.put(dcu, clientID);
}
for (FacilityClient fc : facClientList) {
Surname surname = fc.getSurname();
Name name = fc.getName();
Patronymic patronymic = fc.getPathronymic();
Date born = fc.getBorn();
String snils = fc.getSnils();
DloClientUnique dcu = new DloClientUnique();
dcu.surnameID = surname == null ? 0 : surname.getId();
dcu.nameID = name == null ? 0 : name.getId();
dcu.pathronID = patronymic == null ? 0 : patronymic.getId();
dcu.dateBorn = born;
dcu.snils = snils != null ? snils.trim() : "";
Integer id = clientMap.get(dcu);
if (id != null) {
Client client = findEntity(Client.class, id);
PersonalData data = new PersonalData();
data.id = id;
data.snils = snils;
Polis polisOMI = ClientBean.findPolisOMI(client, manager);
if (polisOMI != null) {
clientPolisMap.put(client.getId(), polisOMI);
String polisSN ="";
if (polisOMI.getSeries() != null) {
polisSN = polisOMI.getSeries();
}
if (polisOMI.getNumber() != null) {
polisSN = polisSN + " " + polisOMI.getNumber();
}
data.polisSN = polisSN.trim();
String code = polisOMI.getRegionCode().getTitle();
code += "00000000000000000";
Field[] f = new Field[]{new Field("kladrCode", code)};
List<AddressObject> adrs = findEntityList(AddressObject.class, f);
if (!adrs.isEmpty()) {
String okatoS = Long.toString(adrs.get(0).getOkato());
if (okatoS.length() > 5) {
okatoS = okatoS.substring(0, 5);
}
int okatoI = Integer.parseInt(okatoS);
data.okatoOMC = okatoI;
}
else {
data.okatoOMC = Integer.MIN_VALUE;
}
//TODO ОГРН СМО
}
data.surname = surname != null ? surname.getTitle() : null;
data.name = name != null ? name.getTitle() : null;
data.pathronymic = patronymic != null ? patronymic.getTitle() : null;
Sex sex = client.getSex();
if (sex != null) {
data.sex = client.getSex().getId() == Sex.CLIENT_SEX_MALE ? 'М' : 'Ж';
}
else {
data.sex = Character.MIN_VALUE;
}
data.bornDate = born;
Excemption ex = fc.getExcemption1();
if (ex == null) {
ex = fc.getExcemption2();
}
if (ex != null) {
data.exemptionID = ex.getExtKey();
}
ClientDocument doc = client.getClientDocument();
if (doc != null) {
ClientDocumentType type = doc.getDoctype();
if (type != null) {
data.docTypeID = type.getExtKey();
}
String docSN ="";
if (doc.getSeries() != null) {
docSN = doc.getSeries();
}
if (doc.getNumber() != null) {
docSN = docSN + " " + doc.getNumber();
}
data.docSN = docSN.trim();
}
else {
ClientDocumentType type = fc.getDocumentType();
if (type != null) {
data.docTypeID = type.getExtKey();
}
data.docSN = fc.getDocumentSN();
}
Address add = client.getAddress();
if (add != null) {
AddressObject aObj = add.getAddressObject();
String okatoS = Long.toString(aObj.getOkato());
if (okatoS.length() > 5) {
okatoS = okatoS.substring(0, 5);
}
int okatoI = Integer.parseInt(okatoS);
data.okatoReg = okatoI;
}
else {
data.okatoReg = Integer.MIN_VALUE;
}
data.spCase = fc.getdType();
res.add(data);
}
}
return res;
}
@Override
public List<RecipeData> getRecipeDataList(Date begin, Date end) throws ClipsServerException {
ConfigBeanRemoteM conf = getBean(ConfigBeanM.class);
Collaborator collaborator = findEntity(Collaborator.class, getCollaborator().getId());
Lpu lpu = collaborator.getLpu();
String lpuTFOMScode = lpu.getLpuCode() != null ? lpu.getLpuCode() : "";
List<RecipeData> res = new ArrayList<RecipeData>();
Field[] f = new Field[]{
new Field("date", begin, Field.OPERATOR_EQUAL_OR_MORE),
new Field("date", end, Field.OPERATOR_EQUAL_OR_LESS),
};
List<PrescriptionDlo> recipeList = findEntityList(PrescriptionDlo.class, f);
for (PrescriptionDlo pr : recipeList) {
if (!pr.getServiceRender().isRendered()) {
continue;
}
RecipeData data = new RecipeData();
data.id = pr.getId();
Client cl = pr.getServiceRender().getPolis().getClient();
data.snils = cl.getSnils();
data.lpuTFOMScode = lpuTFOMScode;
String sql = "SELECT fc FROM FacilityClient fc " +
"WHERE fc.surname = :surname " +
"AND fc.name = :name " +
"AND fc.pathronymic = :pathronymic " +
"AND fc.snils = :snils";
Query query = manager.createQuery(sql);
query.setParameter("surname", cl.getSurname());
query.setParameter("name", cl.getName());
query.setParameter("pathronymic", cl.getPathronymic());
query.setParameter("snils", cl.getSnils());
List<FacilityClient> fcList = query.getResultList();
if (!fcList.isEmpty()) {
FacilityClient fc = fcList.get(0);
Excemption ex = fc.getExcemption1();
if (ex == null) {
ex = fc.getExcemption2();
}
if (ex != null) {
data.exemptionID = ex.getExtKey();
}
}
Polis polisOMI = clientPolisMap.get(cl.getId());
if (polisOMI != null) {
String polisSN ="";
if (polisOMI.getSeries() != null) {
polisSN = polisOMI.getSeries();
}
if (polisOMI.getNumber() != null) {
polisSN = polisSN + " " + polisOMI.getNumber();
}
data.polisSN = polisSN.trim();
}
data.lpuOGRN = lpu.getOgrn() != null ? lpu.getOgrn() : "";
Collaborator collab = pr.getServiceRender().getFunctions().getCollaborator();
data.collabCode = lpuTFOMScode + " " + collab.getCode();
data.mkbCode = pr.getDiagnosis().getMkb10().getCode();
String recipeSN ="";
if (pr.getSeries() != null) {
recipeSN = pr.getSeries();
}
if (pr.getNumber() != null) {
recipeSN = recipeSN + " " + pr.getNumber();
}
data.recipeSN = recipeSN.trim();
data.recipeDate = pr.getDate();
FinanceSource financeSource = pr.getFinance();
if (financeSource != null) {
data.financeSource = pr.getFinance().getId();
}
data.percent = pr.getPercent();
DloDrugList drug = pr.getDrug();
if (drug.getMnn() != null) {
data.mnn = drug.getMnn().getExtKey();
}
else {
if (drug.getTradeName() != null) {
data.trn = drug.getTradeName().getExtKey();
}
}
data.dosageForm = drug.getDosageForm() != null ? drug.getDosageForm().getExtKey() : "";
data.dosage = drug.getDosage();
data.dosageUnit = drug.getDosageUnit() != null ? drug.getDosageUnit().getExtKey() : "";
data.quantity = pr.getQuantity();
data.period = pr.getPeriod();
data.kek = pr.getKek() != null;
data.spCase = pr.getSpecialCase();
res.add(data);
}
return res;
}
}