/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package beans.service;
import reportgen.cores.ejb.annotations.DefineQueryEntity;
import framework.beans.GenericEntity;
import reportgen.cores.ejb.annotations.DefineQueryProperty;
import beans.discount.entity.DiscountCard;
import beans.contract.entity.Polis;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import framework.security.MagicValues;
import framework.security.RightChecker;
import beans.UserRightsSet;
import beans.directory.service.entity.Service;
import beans.directory.simple.entities.ServicingPlace;
import beans.directory.simple.entities.Speciality;
import beans.doctor.PacketService;
import beans.doctor.disease.Disease;
import beans.shedule.reception.SheduleReception;
import beans.user.collaborator.entities.Collaborator;
import beans.user.collaborator.entities.CollaboratorFunctions;
import framework.generic.HasEntityInfo;
import framework.security.UserRight;
import framework.utils.Converter;
import framework.utils.DateTimeUtils;
import java.util.UUID;
/**
*
* @author axe
*/
@Entity
@Table(name = "services_render")
@DefineQueryEntity(title="Назначенная услуга")
public class ServiceRender extends GenericEntity<ServiceRenderDetails> implements Serializable, HasEntityInfo {
private static final long serialVersionUID = 1L;
public static final int DEFAULT_UET = 100;
protected final static String MSGT_ID = "Идентификатор услуги";
@Id
@Column(name = "serren_id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
@DefineQueryProperty(title=MSGT_ID)
private int id;
private final static String MSGT_DATE = "Дата назначения услуги";
@DefineQueryProperty(title=MSGT_DATE)
@Column(name = "serren_date", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date date;
// private final static String MSGT_RENDEREDED = "Услуга оказана (да/нет)";
// @QueryGeneratorProperty(title=MSGT_RENDEREDED)
// @Column(name = "serren_rendered", nullable = false)
// private boolean rendered;
private final static String MSGT_RENDERDATE = "Дата оказания услуги";
@DefineQueryProperty(title=MSGT_RENDERDATE)
@Column(name = "serren_rendereddate")
@Temporal(TemporalType.TIMESTAMP)
private Date renderedDate;
private final static String MSGT_DISCOUNT = "Скидка";
@DefineQueryProperty(title=MSGT_DISCOUNT)
@Column(name = "serren_discount", nullable = false)
private int discount;
private final static String MSGT_CANCELLED = "Услуга отменена (да/нет)";
@DefineQueryProperty(title=MSGT_CANCELLED)
@Column(name = "serren_cancelled", nullable = false)
private boolean cancelled;
private final static String MSGT_REPEAT = "Оказана повторно (кол-во)";
@DefineQueryProperty(title=MSGT_REPEAT)
@Column(name = "serren_repeat", nullable = false)
private int repeat;
private final static String MSGT_DIRECTOR = "Кто направил на услугу";
@DefineQueryProperty(title=MSGT_DIRECTOR)
@JoinColumn(name = "serren_director", referencedColumnName = "coll_id")
@ManyToOne
private Collaborator director;
private final static String MSGT_CONTRACTMEM = "Полис (участник договора)";
@DefineQueryProperty(title=MSGT_CONTRACTMEM)
@JoinColumn(name = "polis_id", referencedColumnName = "polis_id", nullable = false)
@ManyToOne
private Polis polis;
private final static String MSGT_DISCOUNTCARD = "Дисконтная карта";
@DefineQueryProperty(title=MSGT_DISCOUNTCARD)
@JoinColumn(name = "disccard_id", referencedColumnName = "disccard_id")
@ManyToOne
private DiscountCard discountCard;
private final static String MSGT_PACKET = "Номер пакета";
@DefineQueryProperty(title=MSGT_PACKET)
@JoinColumn(name = "psi_id", referencedColumnName = "psi_id")
@ManyToOne
private PacketService packetService;
private final static String MSGT_SERVICE = "Тип услуги";
@DefineQueryProperty(title=MSGT_SERVICE)
@JoinColumn(name = "service_id", referencedColumnName = "service_id", nullable=false)
@ManyToOne
private Service service;
private final static String MSGT_DISEASE = "Группа ТАП";
@DefineQueryProperty(title=MSGT_DISEASE)
@JoinColumn(name = "disease_id", referencedColumnName = "disease_id")
@ManyToOne
private Disease disease;
private final static String MSGT_RECEPTION = "Запись на прием";
@DefineQueryProperty(title=MSGT_RECEPTION)
@JoinColumn(name = "shedrec_id", referencedColumnName = "shedrec_id")
@ManyToOne
private SheduleReception reception;
private final static String MSGT_REF = "Связанная услуга";
@DefineQueryProperty(title=MSGT_REF)
@JoinColumn(name = "serren_ref", referencedColumnName = "serren_id")
@ManyToOne
private ServiceRender referenced;
private final static String MSGT_SERVPLACE = "Место обслуживания";
@DefineQueryProperty(title=MSGT_SERVPLACE)
@JoinColumn(name = "servplace_id", referencedColumnName = "sp_id")
@ManyToOne
private ServicingPlace place;
private final static String MSGT_COLLAB_FUNCTIONS = "Служебные обязанности оказавшего";
@DefineQueryProperty(title=MSGT_COLLAB_FUNCTIONS)
@JoinColumn(name = "function_id", referencedColumnName = "cf_id")
@ManyToOne
private CollaboratorFunctions functions;
@JoinColumn(name = "speciality_id", referencedColumnName = "spec_id")
@ManyToOne
private Speciality speciality;
private final static String MSGT_UET = "Условная единица трудоемкости";
@DefineQueryProperty(title=MSGT_UET)
@Column(name = "serren_uet", nullable = false)
private int uet;
private final static String MSGT_RECEIVED = "Услуга принята (да/нет)";
@DefineQueryProperty(title=MSGT_RECEIVED)
@Column(name = "serren_received", nullable = false)
private boolean received;
private final static String MSGT_UUID = "UUID";
@DefineQueryProperty(title=MSGT_UUID)
@Column(name = "uuid", nullable = false)
private String uuid;
public ServiceRender() {
setUuid(UUID.randomUUID().toString());
}
@Override
public int getId() {
return id;
}
public boolean isRendered() {
return getFunctions() != null && getRenderedDate() != null;
}
@Override
public void setId(int id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public ServicingPlace getPlace() {
return place;
}
public void setPlace(ServicingPlace place) {
this.place = place;
}
public Date getRenderedDate() {
return renderedDate;
}
public void setRenderedDate(Date renderedDate) {
this.renderedDate = renderedDate;
}
public int getDiscount() {
return discount;
}
public void setDiscount(int discount) {
this.discount = discount;
}
public boolean getCancelled() {
return cancelled;
}
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
public int getRepeat() {
return repeat;
}
public void setRepeat(int repeat) {
this.repeat = repeat;
}
public Collaborator getDirector() {
return director;
}
public void setDirector(Collaborator director) {
this.director = director;
}
public Polis getPolis() {
return polis;
}
public void setPolis(Polis polis) {
this.polis = polis;
}
public DiscountCard getDiscountCard() {
return discountCard;
}
public void setDiscountCard(DiscountCard discountCard) {
this.discountCard = discountCard;
}
public PacketService getPacketService() {
return packetService;
}
public void setPacketService(PacketService packetService) {
this.packetService = packetService;
}
public Service getService() {
return service;
}
public void setService(Service service) {
this.service = service;
}
public Disease getDisease() {
return disease;
}
public void setDisease(Disease disease) {
this.disease = disease;
}
public ServiceRender getReferenced() {
return referenced;
}
public void setReferenced(ServiceRender referenced) {
this.referenced = referenced;
}
public CollaboratorFunctions getFunctions() {
return functions;
}
public void setFunctions(CollaboratorFunctions functions) {
this.functions = functions;
}
public int getUet() {
return uet;
}
public void setUet(int uet) {
this.uet = uet;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
@Override
public String toString() {
return "entities.ServicesRender[id=" + id + "]";
}
/*public boolean canWriteMedicalDataNormally(int currentCollaboratorID) {
if (disease == null){
//Если услуга не в заболевании разрешаем ее модифицировать (оказанную другим услугу возможно вносить в заболевание)
return true;
}
if (functions != null && functions.getCollaborator().getId() != currentCollaboratorID) {
//Если услугу оказал другой сотрудник
return false;
}
return ! isRendered() || DateTimeUtils.belongsToCurrentDay(renderedDate);
}*/
/**
* Варианты:
* ПРАВ НЕТ:
* - Суперюзер разрешено
* XXX_DURING_DAY
* - услуга не оказана разрешено
* - услуга оказана сегодня, сотрудник тотже разрешено
* - услуга оказана сегодня, сотрудник другой запрещено
* - услуга оказана не сегодня, сотрудник любой запрещено
* @param isSuperUser
* @param right
* @param collab
* @return
*/
public int canWriteDuringDay(boolean isSuperUser, UserRight right, int collabID) {
if (isSuperUser) {
return right.id;
}
if (renderedDate == null) {
//не оказана
return right.id;
} else {
//оказана
if (DateTimeUtils.belongsToCurrentDay(renderedDate)) {
//сегодня
if (collabID == functions.getCollaborator().getId()) {
//сотрудник тот же
return right.id;
} else {
//сотрудник другой
return -right.id;
}
} else {
return -right.id;
}
}
}
/**
* Варианты:
* ПРАВ НЕТ:
* - Суперюзер разрешено
* XXX_ANY_TIME
* - услуга не оказана разрешено
* - услуга оказана пофик когда, сотрудник тотже разрешено
* - услуга оказана пофик когда, сотрудник другой, из той же клиники разрешено
* - услуга оказана пофик когда, сотрудник другой, из другой клиники запрещено
* @param isSuperUser
* @param right
* @param collab
* @return
*/
public int canWriteAnyTime(boolean isSuperUser, UserRight right, Collaborator collab) {
if (isSuperUser) {
return right.id;
}
if (renderedDate == null) {
//не оказана
return right.id;
} else {
//оказана
Collaborator collRender = functions.getCollaborator();
if (collab.getId() == collRender.getId()) {
//сотрудник тот же
return right.id;
} else {
//сотрудник другой
if (collab.getLpu().getId() == collRender.getLpu().getId()) {
//из той же клиники
return right.id;
} else {
//из другой клиники
return -right.id;
}
}
}
}
public SheduleReception getReception() {
return reception;
}
public void setReception(SheduleReception reception) {
this.reception = reception;
}
public Speciality getSpeciality() {
return speciality;
}
public void setSpeciality(Speciality speciality) {
this.speciality = speciality;
}
public boolean getReceived() {
return received;
}
public void setReceived(boolean received) {
this.received = received;
}
@Override
public String getInfo() {
return "Услуга " + getPolis().getClient().getInfo() + " назначена " + Converter.dateToString(getDate());
}
@Override
public ServiceRenderDetails getDetails(RightChecker rightChecker) {
if(rightChecker == null){
throw new IllegalArgumentException("Интерфейс проверки прав не найден!");
}
ServiceRenderDetails d = new ServiceRenderDetails();
d.id = getId();
d.date = getDate();
d.renderDate = getRenderedDate();
d.discount = getDiscount();
d.cardID = getDiscountCard() == null ? 0 : getDiscountCard().getId();
d.cancelled = getCancelled();
d.repeat = getRepeat();
d.directorID = getDirector().getId();
d.packetServiceID = getPacketService() == null ? 0 : getPacketService().getId();
d.serviceID = getService().getId();
d.diseaseID = getDisease() == null ? 0 : getDisease().getId();
d.polisID = getPolis().getId();
d.servPlaceID = getPlace() != null ? getPlace().getId() : 0;
d.receptionDetails = (getReception() == null) ? null : getReception().getDetails(rightChecker);
d.referencedID = getReferenced() == null ? 0 : getReferenced().getId();
d.functionsID = getFunctions() != null ? getFunctions().getId() : 0;
d.uet = getUet();
d.received = getReceived();
if (!rightChecker.hasRight(UserRightsSet.READ_SERVICE_RENDER_TITLE)){
d.serviceID = MagicValues.ACCESS_DENIED_ID;
}
d.specialityID = getSpeciality() != null ? getSpeciality().getId() : 0;
return d;
}
}