Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,51 @@ public List<TimePeroid> timePeroidsWithBreaksExcluded() {
List<TimePeroid> breaks = getBreaks();

if (!breaks.isEmpty()) {
ArrayList<TimePeroid> toAdd = new ArrayList();
for (TimePeroid break1 : breaks) {
if (break1.getStart().isBefore(workingHours.getStart())) {
break1.setStart(workingHours.getStart());
}
if (break1.getEnd().isAfter(workingHours.getEnd())) {
break1.setEnd(workingHours.getEnd());
}
for (TimePeroid peroid : timePeroidsWithBreaksExcluded) {
if (break1.getStart().equals(peroid.getStart()) && break1.getEnd().isAfter(peroid.getStart()) && break1.getEnd().isBefore(peroid.getEnd())) {
peroid.setStart(break1.getEnd());
}
if (break1.getStart().isAfter(peroid.getStart()) && break1.getStart().isBefore(peroid.getEnd()) && break1.getEnd().equals(peroid.getEnd())) {
peroid.setEnd(break1.getStart());
}
if (break1.getStart().isAfter(peroid.getStart()) && break1.getEnd().isBefore(peroid.getEnd())) {
toAdd.add(new TimePeroid(peroid.getStart(), break1.getStart()));
peroid.setStart(break1.getEnd());
}
}
}
timePeroidsWithBreaksExcluded.addAll(toAdd);
adjustBreaksWithinWorkingHours(timePeroidsWithBreaksExcluded);
Collections.sort(timePeroidsWithBreaksExcluded);
}


return timePeroidsWithBreaksExcluded;
}

private void adjustBreaksWithinWorkingHours(List<TimePeroid> timePeroidsWithBreaksExcluded) {
ArrayList<TimePeroid> toAdd = new ArrayList<>();
for (TimePeroid break1 : getBreaks()) {
adjustBreakStart(break1);
adjustBreakEnd(break1);

for (TimePeroid peroid : timePeroidsWithBreaksExcluded) {
handleBreakOverlap(break1, peroid, toAdd);
}
}
timePeroidsWithBreaksExcluded.addAll(toAdd);
}

private void adjustBreakStart(TimePeroid break1) {
if (break1.getStart().isBefore(workingHours.getStart())) {
break1.setStart(workingHours.getStart());
}
}

private void adjustBreakEnd(TimePeroid break1) {
if (break1.getEnd().isAfter(workingHours.getEnd())) {
break1.setEnd(workingHours.getEnd());
}
}

private void handleBreakOverlap(TimePeroid break1, TimePeroid peroid, List<TimePeroid> toAdd) {
if (break1.getStart().equals(peroid.getStart()) && break1.getEnd().isAfter(peroid.getStart()) && break1.getEnd().isBefore(peroid.getEnd())) {
peroid.setStart(break1.getEnd());
}
if (break1.getStart().isAfter(peroid.getStart()) && break1.getStart().isBefore(peroid.getEnd()) && break1.getEnd().equals(peroid.getEnd())) {
peroid.setEnd(break1.getStart());
}
if (break1.getStart().isAfter(peroid.getStart()) && break1.getEnd().isBefore(peroid.getEnd())) {
toAdd.add(new TimePeroid(peroid.getStart(), break1.getStart()));
peroid.setStart(break1.getEnd());
}
}

public TimePeroid getWorkingHours() {
return workingHours;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,29 +323,39 @@ public String getCancelNotAllowedReason(int userId, int appointmentId) {
}

if (appointment.getProvider().equals(user)) {
if (!appointment.getStatus().equals(AppointmentStatus.SCHEDULED)) {
return "Only appoinmtents with scheduled status can be cancelled.";
} else {
return null;
}
return getCancelNotAllowedReasonForProvider(appointment);
}

if (appointment.getCustomer().equals(user)) {
if (!appointment.getStatus().equals(AppointmentStatus.SCHEDULED)) {
return "Only appoinmtents with scheduled status can be cancelled.";
} else if (LocalDateTime.now().plusDays(1).isAfter(appointment.getStart())) {
return "Appointments which will be in less than 24 hours cannot be canceled.";
} else if (!appointment.getWork().getEditable()) {
return "This type of appointment can be canceled only by Provider.";
} else if (getCanceledAppointmentsByCustomerIdForCurrentMonth(userId).size() >= NUMBER_OF_ALLOWED_CANCELATIONS_PER_MONTH) {
return "You can't cancel this appointment because you exceeded maximum number of cancellations in this month.";
} else {
return null;
}
return getCancelNotAllowedReasonForCustomer(appointment, userId);
}

return null;
}

private String getCancelNotAllowedReasonForProvider(Appointment appointment) {
if (!appointment.getStatus().equals(AppointmentStatus.SCHEDULED)) {
return "Only appointments with scheduled status can be cancelled.";
} else {
return null;
}
}

private String getCancelNotAllowedReasonForCustomer(Appointment appointment, int userId) {
if (!appointment.getStatus().equals(AppointmentStatus.SCHEDULED)) {
return "Only appointments with scheduled status can be cancelled.";
} else if (LocalDateTime.now().plusDays(1).isAfter(appointment.getStart())) {
return "Appointments which will be in less than 24 hours cannot be canceled.";
} else if (!appointment.getWork().getEditable()) {
return "This type of appointment can be canceled only by Provider.";
} else if (getCanceledAppointmentsByCustomerIdForCurrentMonth(userId).size() >= NUMBER_OF_ALLOWED_CANCELATIONS_PER_MONTH) {
return "You can't cancel this appointment because you exceeded the maximum number of cancellations in this month.";
} else {
return null;
}
}


@Override
public int getNumberOfCanceledAppointmentsForUser(int userId) {
return appointmentRepository.findCanceledByUser(userId).size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,47 @@ public PdfGeneratorUtil(SpringTemplateEngine templateEngine, @Value("${base.url}
}

public File generatePdfFromInvoice(Invoice invoice) {
Context ctx = createContextWithInvoice(invoice);
String processedHtml = processHtmlTemplate(ctx);

ITextRenderer renderer = createAndInitializeRenderer(processedHtml);

try {
return createPdfFile(renderer);
} catch (IOException | DocumentException e) {
e.printStackTrace();
}

return null;
}

private Context createContextWithInvoice(Invoice invoice) {
Context ctx = new Context();
ctx.setVariable("invoice", invoice);
String processedHtml = templateEngine.process("email/pdf/invoice", ctx);
return ctx;
}

private String processHtmlTemplate(Context ctx) {
return templateEngine.process("email/pdf/invoice", ctx);
}

private ITextRenderer createAndInitializeRenderer(String processedHtml) {
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(processedHtml, baseUrl);
renderer.layout();
return renderer;
}

private File createPdfFile(ITextRenderer renderer) throws IOException, DocumentException {
String fileName = UUID.randomUUID().toString();
FileOutputStream os = null;
try {
final File outputFile = File.createTempFile(fileName, ".pdf");
os = new FileOutputStream(outputFile);
File outputFile = File.createTempFile(fileName, ".pdf");

try (FileOutputStream os = new FileOutputStream(outputFile)) {
renderer.createPDF(os, false);
renderer.finishPDF();
return outputFile;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();

} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;

return outputFile;
}

}
Loading