Long shippingMethodId = Format.getLong(form.getShippingMethodId());
Long shippingTypeId = Format.getLong(form.getShippingTypeId());
ShippingMethod shippingMethod = (ShippingMethod) em.find(ShippingMethod.class, shippingMethodId);
ShippingRate shippingRate = null;
ShippingRateCurrency shippingRateCurrency = null;
ShippingMethodRegionType shippingMethodRegionType = null;
for (ShippingMethodRegion shippingMethodRegion : shippingMethod.getShippingMethodRegions()) {
if (shippingMethodRegion.getShippingRegion().getShippingRegionId().equals(shippingRegionId)) {
for (ShippingMethodRegionType smrt : shippingMethodRegion.getShippingMethodRegionTypes()) {
if (smrt.getShippingType().getShippingTypeId().equals(shippingTypeId)) {
shippingMethodRegionType = smrt;
shippingRate = shippingMethodRegionType.getShippingRate();
break;
}
}
}
}
if (shippingRate == null) {
shippingRate = new ShippingRate();
shippingRate.setRecUpdateBy(adminBean.getUser().getUserId());
shippingRate.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
shippingRate.setRecCreateBy(adminBean.getUser().getUserId());
shippingRate.setRecCreateDatetime(new Date(System.currentTimeMillis()));
shippingRate.setPublished(Constants.VALUE_YES);
shippingMethodRegionType.setShippingRate(shippingRate);
}
if (form.isSiteCurrencyClassDefault()) {
shippingRateCurrency = shippingRate.getShippingRateCurrency();
if (shippingRateCurrency == null) {
shippingRateCurrency = new ShippingRateCurrency();
shippingRateCurrency.setShippingRate(shippingRate);
shippingRateCurrency.setRecCreateBy(adminBean.getUser().getUserId());
shippingRateCurrency.setRecCreateDatetime(new Date(System.currentTimeMillis()));
SiteCurrencyClass siteCurrencyClass = SiteCurrencyClassDAO.load(form.getSiteCurrencyClassDefaultId());
shippingRateCurrency.setSiteCurrencyClass(siteCurrencyClass);
shippingRate.setShippingRateCurrency(shippingRateCurrency);
shippingRate.getShippingRateCurrencies().add(shippingRateCurrency);
}
if (!Format.isNullOrEmpty(form.getShippingRateFee())) {
shippingRateCurrency.setShippingRateFee(Format.getFloatObj(form.getShippingRateFee()));
}
if (!Format.isNullOrEmpty(form.getShippingRatePercentage())) {
shippingRate.setShippingRatePercentage(Format.getFloatObj(form.getShippingRatePercentage()));
}
if (!Format.isNullOrEmpty(form.getShippingAdditionalRateFee())) {
shippingRateCurrency.setShippingAdditionalRateFee(Format.getFloatObj(form.getShippingAdditionalRateFee()));
}
if (!Format.isNullOrEmpty(form.getShippingAdditionalRatePercentage())) {
shippingRate.setShippingAdditionalRatePercentage(Format.getFloatObj(form.getShippingAdditionalRatePercentage()));
}
shippingRate.setRecUpdateBy(adminBean.getUser().getUserId());
shippingRate.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
shippingRateCurrency.setRecUpdateBy(adminBean.getUser().getUserId());
shippingRateCurrency.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
if (shippingRateCurrency.getShippingRateCurrId() == null) {
em.persist(shippingRateCurrency);
}
if (shippingRate.getShippingRateId() == null) {
em.persist(shippingRate);
}
if (shippingRate.getShippingRateCurrency().getShippingRateFee() != null) {
jsonResult.put("shippingRateFee", Format.getFloat(shippingRate.getShippingRateCurrency().getShippingRateFee()));
}
else {
jsonResult.put("shippingRateFee", "");
}
if (shippingRate.getShippingRatePercentage() != null) {
jsonResult.put("shippingRatePercentage", Format.getFloat(shippingRate.getShippingRatePercentage()));
}
else {
jsonResult.put("shippingRatePercentage", "");
}
if (shippingRate.getShippingRateCurrency().getShippingAdditionalRateFee() != null) {
jsonResult.put("shippingAdditionalRateFee", Format.getFloat(shippingRate.getShippingRateCurrency().getShippingAdditionalRateFee()));
}
else {
jsonResult.put("shippingAdditionalRateFee", "");
}
if (shippingRate.getShippingAdditionalRatePercentage() != null) {
jsonResult.put("shippingAdditionalRatePercentage", Format.getFloat(shippingRate.getShippingAdditionalRatePercentage()));
}
else {
jsonResult.put("shippingAdditionalRatePercentage", "");
}
}
else {
shippingRateCurrency = null;
for (ShippingRateCurrency srCurrency : shippingRate.getShippingRateCurrencies()) {
if (srCurrency.getSiteCurrencyClass().getSiteCurrencyClassId().equals(form.getSiteCurrencyClassId())) {
shippingRateCurrency = srCurrency;
}
}
if (shippingRateCurrency == null) {
shippingRateCurrency = new ShippingRateCurrency();
SiteCurrencyClass siteCurrencyClass = SiteCurrencyClassDAO.load(form.getSiteCurrencyClassId());
shippingRateCurrency.setRecCreateBy(adminBean.getUser().getUserId());
shippingRateCurrency.setRecCreateDatetime(new Date(System.currentTimeMillis()));
shippingRateCurrency.setSiteCurrencyClass(siteCurrencyClass);
shippingRateCurrency.setShippingRate(shippingRate);
shippingRate.getShippingRateCurrencies().add(shippingRateCurrency);
}
shippingRate.setRecUpdateBy(adminBean.getUser().getUserId());
shippingRate.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
if (form.isShippingRateFeeCurrFlag()) {
shippingRateCurrency.setShippingRateFee(Format.getFloat(form.getShippingRateFeeCurr()));
}
else {
shippingRateCurrency.setShippingRateFee(null);
}
if (form.isShippingAdditionalRateFeeCurrFlag()) {
shippingRateCurrency.setShippingAdditionalRateFee(Format.getFloat(form.getShippingAdditionalRateFeeCurr()));
}
else {
shippingRateCurrency.setShippingAdditionalRateFee(null);
}
shippingRateCurrency.setRecUpdateBy(adminBean.getUser().getUserId());
shippingRateCurrency.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
if (shippingRate.getShippingRateId() == null) {
em.persist(shippingRate);
}
if (shippingRateCurrency.getShippingRateCurrId() == null) {
em.persist(shippingRateCurrency);
}
if (shippingRate.getShippingRateCurrency() != null && shippingRate.getShippingRateCurrency().getShippingRateFee() != null) {
jsonResult.put("shippingRateFee", Format.getFloat(shippingRate.getShippingRateCurrency().getShippingRateFee()));
}
else {
jsonResult.put("shippingRateFee", "");
}
if (shippingRate.getShippingRatePercentage() != null) {
jsonResult.put("shippingRatePercentage", Format.getFloat(shippingRate.getShippingRatePercentage()));
}
else {
jsonResult.put("shippingRatePercentage", "");
}
if (shippingRate.getShippingRateCurrency() != null && shippingRate.getShippingRateCurrency().getShippingAdditionalRateFee() != null) {
jsonResult.put("shippingAdditionalRateFee", Format.getFloat(shippingRate.getShippingRateCurrency().getShippingAdditionalRateFee()));
}
else {
jsonResult.put("shippingAdditionalRateFee", "");
}
if (shippingRate.getShippingAdditionalRatePercentage() != null) {
jsonResult.put("shippingAdditionalRatePercentage", Format.getFloat(shippingRate.getShippingAdditionalRatePercentage()));
}
else {
jsonResult.put("shippingAdditionalRatePercentage", "");
}
if (shippingRateCurrency.getShippingRateFee() != null) {
jsonResult.put("shippingRateFeeCurr", Format.getFloat(shippingRateCurrency.getShippingRateFee()));
jsonResult.put("shippingRateFeeCurrFlag", true);
}
else {
jsonResult.put("shippingRateFeeCurr", "");
jsonResult.put("shippingRateFeeCurrFlag", false);
}
if (shippingRateCurrency.getShippingAdditionalRateFee() != null) {
jsonResult.put("shippingAdditionalRateFeeCurr", Format.getFloat(shippingRateCurrency.getShippingAdditionalRateFee()));
jsonResult.put("shippingAdditionalRateFeeCurrFlag", true);
}
else {
jsonResult.put("shippingAdditionalRateFeeCurr", "");
jsonResult.put("shippingAdditionalRateFeeCurrFlag", false);