parent
f26e5e12c9
commit
8c26d3e430
@ -1,8 +1,16 @@
|
||||
package info.bukova.isspst.services.tripbill;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import info.bukova.isspst.data.TripBill;
|
||||
import info.bukova.isspst.data.TripRequirement;
|
||||
import info.bukova.isspst.services.Service;
|
||||
|
||||
public interface TripBillService extends Service<TripBill> {
|
||||
|
||||
public TripBill createTripBill(TripRequirement requirement);
|
||||
public void loadItems(TripBill bill);
|
||||
public void calculate(TripBill bill);
|
||||
public List<TripBill> getMy();
|
||||
|
||||
}
|
||||
|
@ -1,9 +1,177 @@
|
||||
package info.bukova.isspst.services.tripbill;
|
||||
|
||||
import info.bukova.isspst.data.SettingsData;
|
||||
import info.bukova.isspst.data.TripBill;
|
||||
import info.bukova.isspst.data.TripBillItem;
|
||||
import info.bukova.isspst.data.TripRequirement;
|
||||
import info.bukova.isspst.services.AbstractOwnedService;
|
||||
import info.bukova.isspst.services.LazyLoader;
|
||||
import info.bukova.isspst.services.settings.GlobalSettingsService;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.hibernate.LazyInitializationException;
|
||||
import org.hibernate.Query;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
public class TripBillServiceImpl extends AbstractOwnedService<TripBill> implements
|
||||
TripBillService {
|
||||
|
||||
@Autowired
|
||||
private GlobalSettingsService globalSettings;
|
||||
|
||||
@Override
|
||||
public TripBill createTripBill(TripRequirement requirement) {
|
||||
TripBill bill = new TripBill();
|
||||
|
||||
bill.setRequirement(requirement);
|
||||
long daysCount = TimeUnit.DAYS.convert(requirement.getEndDate().getTime() - requirement.getTripDate().getTime(), TimeUnit.MILLISECONDS) + 1;
|
||||
|
||||
for (int i = 0 ; i < daysCount ; i++) {
|
||||
TripBillItem item = new TripBillItem();
|
||||
if (i == 0) {
|
||||
item.setTo(requirement.getFrom() + " - " + requirement.getTo());
|
||||
item.setToVehicle(requirement.getVehicle());
|
||||
}
|
||||
if (i == daysCount - 1) {
|
||||
item.setBack(requirement.getTo() + " - " + requirement.getFrom());
|
||||
item.setBackVehicle(requirement.getVehicle());
|
||||
}
|
||||
|
||||
Calendar calTripDate = Calendar.getInstance();
|
||||
calTripDate.setTime(requirement.getTripDate());
|
||||
calTripDate.add(Calendar.DATE, i);
|
||||
item.setDate(calTripDate.getTime());
|
||||
|
||||
bill.getBillItems().add(item);
|
||||
}
|
||||
|
||||
this.calculate(bill);
|
||||
|
||||
return bill;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
@LazyLoader("form")
|
||||
public void loadItems(TripBill entity) {
|
||||
try {
|
||||
if (entity.getBillItems() == null) {
|
||||
throw new LazyInitializationException("");
|
||||
}
|
||||
|
||||
entity.getBillItems().size();
|
||||
} catch (LazyInitializationException ex) {
|
||||
TripBill e = dao.getById(entity.getId());
|
||||
e.getBillItems().size();
|
||||
entity.setBillItems(e.getBillItems());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void calculate(TripBill bill) {
|
||||
bill.setTotal(BigDecimal.ZERO);
|
||||
|
||||
for (TripBillItem item : bill.getBillItems()) {
|
||||
if (!bill.isFreeMeals()) {
|
||||
item.setFreeMealsCount(0);
|
||||
}
|
||||
|
||||
calculateItem(item);
|
||||
bill.setTotal(bill.getTotal().add(item.getTotal()));
|
||||
}
|
||||
}
|
||||
|
||||
private void calculateItem(TripBillItem item) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
Date to = item.getToArrival();
|
||||
|
||||
if (to == null) {
|
||||
to = new Date();
|
||||
cal.setTime(to);
|
||||
cal.set(Calendar.HOUR_OF_DAY, 0);
|
||||
cal.set(Calendar.MINUTE, 0);
|
||||
} else {
|
||||
cal.setTime(to);
|
||||
}
|
||||
|
||||
cal.set(Calendar.DAY_OF_MONTH, 1);
|
||||
cal.set(Calendar.MONTH, 1);
|
||||
cal.set(Calendar.YEAR, 2000);
|
||||
|
||||
to.setTime(cal.getTimeInMillis());
|
||||
|
||||
Date back = item.getBackDeparture();
|
||||
|
||||
if (back == null) {
|
||||
back = new Date(to.getTime());
|
||||
}
|
||||
|
||||
cal.setTime(back);
|
||||
|
||||
if (back.equals(to)) {
|
||||
cal.set(Calendar.HOUR, 23);
|
||||
cal.set(Calendar.MINUTE, 59);
|
||||
}
|
||||
|
||||
cal.set(Calendar.DAY_OF_MONTH, 1);
|
||||
cal.set(Calendar.MONTH, 1);
|
||||
cal.set(Calendar.YEAR, 2000);
|
||||
back.setTime(cal.getTimeInMillis());
|
||||
|
||||
if (to.getTime() > back.getTime()) {
|
||||
item.setTotal(BigDecimal.ZERO);
|
||||
return;
|
||||
}
|
||||
|
||||
SettingsData settings = globalSettings.getSettings();
|
||||
long hours = TimeUnit.HOURS.convert(back.getTime() - to.getTime(), TimeUnit.MILLISECONDS);
|
||||
int refundHour = 0;
|
||||
List<Integer> refundHours = new ArrayList<Integer>(settings.getRefunds().keySet());
|
||||
Collections.sort(refundHours);
|
||||
|
||||
for (int key : refundHours) {
|
||||
if (refundHour <= hours && key > hours) {
|
||||
break;
|
||||
}
|
||||
|
||||
refundHour = key;
|
||||
}
|
||||
|
||||
item.setMeals(settings.getRefunds().get(refundHour)[item.getFreeMealsCount()]);
|
||||
|
||||
BigDecimal carefare = item.getCarefare() == null ? BigDecimal.ZERO : item.getCarefare();
|
||||
BigDecimal housing = item.getHousing() == null ? BigDecimal.ZERO : item.getHousing();
|
||||
BigDecimal otherExp = item.getOtherExpenses() == null ? BigDecimal.ZERO : item.getOtherExpenses();
|
||||
BigDecimal distanceAmount = item.getDistanceAmount() == null ? BigDecimal.ZERO : item.getDistanceAmount();
|
||||
BigDecimal fuelAmount = item.getFuelAmount() == null ? BigDecimal.ZERO : item.getFuelAmount();
|
||||
|
||||
item.setTotal(carefare.add(housing).add(item.getMeals()).add(otherExp).add(distanceAmount).add(fuelAmount));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
@Transactional
|
||||
@PreAuthorize("hasPermission(this, 'PERM_READ')")
|
||||
public List<TripBill> getMy() {
|
||||
Query q = dao.getQuery("from TripBill where ownedBy = :owner");
|
||||
q.setParameter("owner", getLoggedInUser());
|
||||
return q.list();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
@PreAuthorize("hasPermission(this, 'PERM_SHOW_ALL_BILL')")
|
||||
public List<TripBill> getAll() {
|
||||
return this.execQuery("from TripBill as tb join fetch tb.ownedBy");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,14 +1,48 @@
|
||||
package info.bukova.isspst.ui.tripbill;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import info.bukova.isspst.data.TripBill;
|
||||
import info.bukova.isspst.data.Vehicle;
|
||||
import info.bukova.isspst.services.settings.GlobalSettingsService;
|
||||
import info.bukova.isspst.services.tripbill.TripBillService;
|
||||
import info.bukova.isspst.ui.BigDecimalConverter;
|
||||
import info.bukova.isspst.ui.FormViewModel;
|
||||
|
||||
import org.zkoss.bind.annotation.Command;
|
||||
import org.zkoss.bind.annotation.Init;
|
||||
import org.zkoss.bind.annotation.NotifyChange;
|
||||
import org.zkoss.zk.ui.select.annotation.WireVariable;
|
||||
|
||||
public class TripBillForm extends FormViewModel<TripBill> {
|
||||
|
||||
private BigDecimalConverter bigDecimalConverter;
|
||||
@WireVariable
|
||||
private TripBillService tripBillService;
|
||||
private List<Vehicle> vehicles;
|
||||
@WireVariable
|
||||
private GlobalSettingsService settingsService;
|
||||
|
||||
@Init(superclass = true)
|
||||
public void init() {
|
||||
bigDecimalConverter = new BigDecimalConverter();
|
||||
vehicles = new ArrayList<Vehicle>();
|
||||
vehicles.add(null);
|
||||
vehicles.addAll(settingsService.getSettings().getVehicles());
|
||||
}
|
||||
|
||||
public BigDecimalConverter getBigDecimalConverter() {
|
||||
return bigDecimalConverter;
|
||||
}
|
||||
|
||||
@Command
|
||||
@NotifyChange("dataBean")
|
||||
public void calculate() {
|
||||
tripBillService.calculate(getDataBean());
|
||||
}
|
||||
|
||||
public List<Vehicle> getVehicles() {
|
||||
return vehicles;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue