Contract entity = getExistentEntity();
ArrayList<AuditDoc> auditDocList = new ArrayList<AuditDoc>();
Polis polis = null;
AuditDoc<Polis> auditDoc;
Address a = new Address();
if (details.id != 0) {
polis = findEntity(Polis.class, details.id);
checkTheSame(polis.getContract());
int count = getEntityCount(ServiceRender.class, new Field[]{new Field("polis", polis)});
count += getEntityCount(ProfcheckupItem.class, new Field[]{new Field("polis", polis)});
if (count > 0) {
//помещение старого в корзину
auditDoc = new AuditDoc<Polis>(polis, getCollaborator());
auditDocList.add(auditDoc);
polis.setTrash(true);
saveEntity(polis);
auditDoc.check(polis);
//создание нового
auditDoc = new AuditDoc<Polis>(null, getCollaborator());
auditDocList.add(auditDoc);
polis = new Polis();
}
else {
auditDoc = new AuditDoc<Polis>(polis, getCollaborator());
auditDocList.add(auditDoc);
a = polis.getAddress();
if (a == null) {
a = new Address();
}
}
}
else {
auditDoc = new AuditDoc<Polis>(null, getCollaborator());
auditDocList.add(auditDoc);
polis = new Polis();
}
polis.setClient(findEntity(Client.class, details.clientID));
polis.setContract(entity);
polis.setCollaborator(findEntity(Collaborator.class, getCollaboratorId()));
polis.setMember(details.member);
polis.setSeries(details.series);
polis.setNumber(details.number);
polis.setTillDate(details.tillDate);
polis.setFromDate(details.fromDate);
polis.setInsurer(details.insurerID != 0 ? findEntity(Insurer.class, details.insurerID) : null);
polis.setRegionCode(findEntity(RegionIdCode.class, details.regionCodeId));
AddressDetails ad = details.addressDetail;
if (ad != null) {
a.setAddressObject(ad.objectId != 0 ? findEntity(AddressObject.class, ad.objectId) : null);
a.setBuilding(ad.building);
a.setFlat(ad.flat);
saveEntity(a);
polis.setAddress(a);
}
int id = saveEntity(polis);
auditDoc.check(polis);