return model;
}
@Override
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
ReceptionDTO dto = (ReceptionDTO) command;
Calendar cal = GregorianCalendar.getInstance();
cal.setTimeInMillis(dto.getTime());
if(dto.getTime() == 0) {
errors.reject("reception.incorrect.time");
return showForm(request, errors, getFormView());
}
Day day = new Day(cal);
Calendar dayEnd = day.getCalendar();
dayEnd.add(Calendar.DAY_OF_MONTH, 1);
dayEnd.add(Calendar.MILLISECOND, -1);
ReceptionHelper rh = new ReceptionHelper(getDao(), dto, day.getDate(), dayEnd.getTime());
List<WeekDay> model = rh.getModel();
if(model.size() != 1) {
errors.reject("reception.incorrect.time");
return showForm(request, errors, getFormView());
}
WeekDay weekDay = model.get(0);
Ticket ticket = null;
if(weekDay.getTickets() != null) {
for(Ticket t: weekDay.getTickets()) {
long ticketTime = t.getTime();
if(ticketTime == dto.getTime()) {
ticket = t;
break;
}
}
}
if(ticket == null) {
errors.reject("reception.incorrect.time");
return showForm(request, errors, getFormView());
}
if(ticket.isBuzy()) {
errors.reject("reception.buzy");
return showForm(request, errors, getFormView());
}
if(dto.getClient() == null) {
return new ModelAndView("redirect:/operator/chooseclient.htm"
+ "?type=" + dto.getType().getId()
+ "&lpu=" + dto.getLpu().getId()
+ "&collaborator=" + dto.getCollaborator().getId()
+ "&time=" + dto.getTime()
);
}
//проверка для автоматического подтверждения
//В один день нельзя записываться к сотрудникам с одинаковыми типами работ 2 раза
Date thisDate = DateTimeUtils.getDateOnly(cal).getTime();
Date nextDate = DateTimeUtils.getDateOnly(cal).getTime();
boolean autoconfirm = true;
DetachedCriteria queryCriteria = DetachedCriteria.forClass(SheduleReception.class)
.add(Property.forName("register").ge(thisDate))
.add(Property.forName("register").lt(nextDate))
.add(Property.forName("client").eq(dto.getClient()))
.add(Property.forName("workType").eq(dto.getType()));
List<SheduleReception> conflictList = getDao().getList(queryCriteria, 0, 0);
if (conflictList.size() > 0) {
autoconfirm = false;
//т.к. это рабочее место оператора, то посылать сообщение пользователю ненадо
}
SheduleReception reception = new SheduleReception();
reception.setAuthor(getDao().getById(Collaborator.class, getUserInfo().getCurrentCollaboratorId()));
reception.setCollaborator(dto.getCollaborator());
reception.setBegin(cal.getTime());
reception.setClient(dto.getClient());
reception.setDuration(ticket.getDuration());
reception.setDescription(dto.getType().getDescription());
reception.setConfirmed(autoconfirm);
reception.setRegister(new Date());
reception.setWorkType(dto.getType());
getDao().save(reception);
ticket.setBuzy(true);
updater.addTicketP(dto.getCollaborator(), dto.getType(), ticket);
String date = Converter.dateToString(cal.getTime());
String time = Converter.dateToString(cal.getTime(), "HH:mm");
return new ModelAndView("redirect:/operator/complete.htm"
+ "?client=" + dto.getClient().getId()
+ "&lpu=" + dto.getLpu().getId()
+ "&collaborator=" + dto.getCollaborator().getId()
+ "&date=" + date
+ "&time=" + time
+ "&type=" + dto.getType().getId());
}