Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.jleaf.erp.sls.bo.tasksales;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import javax.persistence.Query;
- import org.jleaf.common.CommonExceptionConstants;
- import org.jleaf.common.entity.User;
- import org.jleaf.core.BusinessFunction;
- import org.jleaf.core.BusinessTransaction;
- import org.jleaf.core.CoreException;
- import org.jleaf.core.DefaultBusinessTransaction;
- import org.jleaf.core.Dto;
- import org.jleaf.core.GeneralConstants;
- import org.jleaf.core.annotation.ErrorList;
- import org.jleaf.core.annotation.Info;
- import org.jleaf.core.annotation.InfoIn;
- import org.jleaf.core.annotation.InfoOut;
- import org.jleaf.core.dao.QueryBuilder;
- import org.jleaf.erp.master.MasterExceptionConstants;
- import org.jleaf.erp.sls.SalesConstants;
- import org.jleaf.erp.sls.SalesExceptionConstants;
- import org.jleaf.erp.sls.bo.tasksales.utils.DecodedImageFile;
- import org.jleaf.erp.sls.dao.SalesmanVisitPlanDao;
- import org.jleaf.erp.sls.dao.SalesmanVisitRealizationDao;
- import org.jleaf.erp.sls.entity.SalesmanVisitPlan;
- import org.jleaf.erp.sls.entity.SalesmanVisitRealization;
- import org.jleaf.erp.sls.entity.UserPartner;
- import org.jleaf.util.DateUtil;
- import org.jleaf.util.GsonUtil;
- import org.jleaf.util.ValidationUtil;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.core.env.Environment;
- import org.springframework.stereotype.Service;
- /**
- *
- * @author WTC, Jul 4, 2013
- *
- */
- //@formatter:off
- @Service
- @InfoIn(value = {
- @Info(name = "userLoginId", description = "user Login Id", type = Long.class),
- @Info(name = "roleLoginId", description = "role Login Id", type = Long.class),
- @Info(name = "tenantLoginId", description = "tenant Login Id", type = Long.class),
- @Info(name = "datetime", description = "datetime", type = String.class),
- @Info(name = "visitId", description = "visit Id", type = Long.class),
- @Info(name = "customerId", description = "customer Id", type = Long.class),
- @Info(name = "customerCode", description = "customer Code", type = String.class),
- @Info(name = "image", description = "image", type = String.class),
- @Info(name = "latitude", description = "latitude", type = String.class),
- @Info(name = "longtitude", description = "longtitude", type = String.class),
- @Info(name = "checkInTime", description = "check In Time", type = String.class),
- @Info(name = "strategy", description = "strategy", type = String.class),
- })
- @InfoOut(value = {
- @Info(name = "tenantId", description = "tenant Id", type = Long.class),
- @Info(name = "salesmanId", description = "salesman Id", type = Long.class),
- @Info(name = "customerId", description = "customer Id", type = Long.class),
- @Info(name = "createUserId", description = "create User Id", type = Long.class),
- @Info(name = "updateUserId", description = "update User Id", type = Long.class),
- @Info(name = "version", description = "version", type = Long.class),
- @Info(name = "salesmanVisitPlanId", description = "salesman Visit PlanId", type = Long.class),
- @Info(name = "checkInTimeLocal", description = "check In Time Local", type = String.class),
- @Info(name = "checkInTimeServer", description = "check In Time Server", type = String.class),
- @Info(name = "checkInLongitude", description = "check In Longitude", type = String.class),
- @Info(name = "checkInLatitude", description = "check In Latitude", type = String.class),
- @Info(name = "visitDate", description = "visit Date", type = String.class),
- @Info(name = "statusVisit", description = "status Visit", type = String.class),
- @Info(name = "checkInPhotoFileName", description = "check In Photo File Name", type = String.class),
- @Info(name = "createDatetime", description = "create Datetime", type = String.class),
- @Info(name = "updateDatetime", description = "update Datetime", type = String.class),
- })
- @ErrorList(errorKeys = {
- CommonExceptionConstants.TENANT_LOGIN_CANT_USED,
- CommonExceptionConstants.OU_ID_NOT_FOUND,
- CommonExceptionConstants.OU_NOT_LEGAL_ENTITY,
- CommonExceptionConstants.OULEGAL_ID_NOT_FOUND,
- MasterExceptionConstants.PARTNER_ID_NOT_FOUND,
- MasterExceptionConstants.POLICY_PARTNER_AUTHORIZATION_FAILED,
- })
- //@formatter:on
- public class AddSalesmanVisitCheckIn extends DefaultBusinessTransaction implements BusinessTransaction {
- private static final Logger log = LoggerFactory.getLogger(AddSalesmanVisitCheckIn.class);
- @Autowired
- private SalesmanVisitPlanDao salesmanVisitPlanDao;
- @Autowired
- private SalesmanVisitRealizationDao salesmanVisitRalizationDao;
- @Autowired
- Environment env;
- @Autowired
- @Qualifier("findSalesmanVisitPlanById")
- private BusinessFunction findSalesmanVisitPlanById;
- @Autowired
- @Qualifier("findPartnerById")
- private BusinessFunction findPartnerById;
- @Override
- public String getDescription() {
- return "Add Salesman Visit Check In";
- }
- @SuppressWarnings("unchecked")
- @Override
- public Dto prepare(Dto inputDto, Dto originalDto) throws Exception {
- // log.info("INPUT DTO prepare {} --> {}", inputDto);
- ValidationUtil.valDtoContainsKey(inputDto, "userLoginId");
- ValidationUtil.valDtoContainsKey(inputDto, "tenantLoginId");
- ValidationUtil.valDtoContainsKey(inputDto, "roleLoginId");
- ValidationUtil.valDtoContainsKey(inputDto, "datetime");
- Long userId = inputDto.getLong("userLoginId");
- Long tenantId = inputDto.getLong("tenantLoginId");
- Long roleLoginId = inputDto.getLong("roleLoginId");
- String datetime = inputDto.getString("datetime");
- Long visitId = inputDto.getLong("visitId");
- String visitStrategy = inputDto.getString("strategy");
- Long customerId = inputDto.getLong("customerId");
- String customerCode = inputDto.getString("customerCode");
- String image = inputDto.getString("image");
- Double latitude = inputDto.getDouble("latitude");
- Double longitude = inputDto.getDouble("longitude");
- String checkInTime = inputDto.getString("checkInTime");
- String checkInTimeServer = DateUtil.dateTimeNow();
- QueryBuilder builderUserPartner = new QueryBuilder();
- builderUserPartner.add("SELECT A.partner_id ")
- .add(" FROM ").add(UserPartner.TABLE_NAME).add(" A ")
- .add(" INNER JOIN ").add(User.TABLE_NAME).add(" B ON A.user_id = B.user_id AND A.tenant_id =B.tenant_id")
- .add(" WHERE B.user_id = :userId AND B.tenant_id = :tenantId");
- Query qUserPartner = salesmanVisitPlanDao.createNativeQuery(builderUserPartner.toString());
- qUserPartner.setParameter("tenantId", tenantId);
- qUserPartner.setParameter("userId", userId);
- String salesmanIdString = qUserPartner.getSingleResult().toString();
- Long salesmanId = Long.valueOf(salesmanIdString) != null ? Long.valueOf(salesmanIdString) : -99l;
- Dto inputFindSalesmanVistPlanById = new Dto();
- inputFindSalesmanVistPlanById.put("id", visitId);
- String now = DateUtil.dateTimeNow();
- if( Long.valueOf(now) < Long.valueOf(checkInTime)) {
- throw new CoreException(SalesExceptionConstants.CHECK_IN_TIME_INVALID);
- }
- Dto inputFindSalesmanByIdDto = new Dto();
- inputFindSalesmanByIdDto.put("id", salesmanId);
- Dto salesmanDto = findPartnerById.execute(inputFindSalesmanByIdDto);
- String salesmanCode = salesmanDto.getString("code");
- if (!GeneralConstants.YES.equals(salesmanDto.getString("active"))) {
- throw new CoreException(SalesExceptionConstants.DATA_NOT_ACTIVE,"Salesman : ".concat(salesmanCode));
- }
- Dto inputFindCustomerByIdDto = new Dto();
- inputFindCustomerByIdDto.put("id", customerId);
- Dto customerDto = findPartnerById.execute(inputFindCustomerByIdDto);
- if (!GeneralConstants.YES.equals(customerDto.getString("active"))) {
- throw new CoreException(SalesExceptionConstants.DATA_NOT_ACTIVE,"Customer : ".concat(customerCode));
- }
- Dto salesmanVisitPlanDto = findSalesmanVisitPlanById.execute(inputFindSalesmanVistPlanById);
- String statusVisit = salesmanVisitPlanDto.getString("statusVisit");
- if(!GeneralConstants.NO.equals(statusVisit) && !SalesConstants.VISIT_PENDING.equals(statusVisit)) {
- throw new CoreException(SalesExceptionConstants.CANT_VISIT_TO_CUSTOMER,customerCode);
- }
- salesmanVisitPlanDto.put("visitStrategy",visitStrategy);
- salesmanVisitPlanDto.put("statusVisit",SalesConstants.IN_PROGRESS_TRANSACTION);
- this.prepareUpdateAudit(salesmanVisitPlanDto, userId, datetime);
- inputDto.put("salesmanVisitPlanDto", salesmanVisitPlanDto);
- //image
- String[] verificationImage = image.split(",");
- String imageEncoded = verificationImage[1];
- String imageHome = env.getProperty("jleaf.taskhub.imageHome");
- String imageFullDir = imageHome.concat(SalesConstants.TASKSALES).concat("/");
- String imageName = DateUtil.dateNow().concat("/").concat(salesmanId.toString()) .concat("-")
- .concat(String.valueOf(System.currentTimeMillis())
- .concat(".jpeg"));
- image = DecodedImageFile.writeFile(imageEncoded, imageFullDir, imageName);
- // log.info("IMAGE ENCODE {} --> {}", imageEncoded);
- Dto salesmanVisitRealizationDto = new Dto();
- salesmanVisitRealizationDto.put("salesmanVisitPlanId", visitId);
- salesmanVisitRealizationDto.put("tenantId", tenantId);
- salesmanVisitRealizationDto.put("salesmanId", salesmanId);
- salesmanVisitRealizationDto.put("visitDate", salesmanVisitPlanDto.getString("visitDate"));
- salesmanVisitRealizationDto.put("customerId", customerId);
- salesmanVisitRealizationDto.put("checkInTimeLocal", checkInTime);
- salesmanVisitRealizationDto.put("checkInTimeServer", checkInTimeServer);
- salesmanVisitRealizationDto.put("checkInLatitude", latitude);
- salesmanVisitRealizationDto.put("checkInLongitude", longitude);
- salesmanVisitRealizationDto.put("checkInPhotoFileName", image);
- salesmanVisitRealizationDto.put("statusVisit", SalesConstants.IN_PROGRESS_TRANSACTION);
- this.prepareInsertAudit(salesmanVisitRealizationDto, userId, datetime);
- this.prepareUpdateAudit(salesmanVisitRealizationDto, userId, datetime);
- // log.info("SALESMAN VISIT REALIZATION DTO prepare {} --> {}", salesmanVisitRealizationDto);
- inputDto.put("salesmanVisitRealizationDto", salesmanVisitRealizationDto);
- return null;
- }
- @SuppressWarnings("unchecked")
- @Override
- public Dto process(Dto inputDto, Dto originalDto) throws Exception {
- // log.info("INPUT DTO proses {} --> {}", inputDto);
- Dto outputDto = new Dto();
- Dto salesmanVisitPlanDto = inputDto.getDto("salesmanVisitPlanDto");
- log.info("SALESMAN VISIT PLAN DTO proses {} --> {}", salesmanVisitPlanDto);
- Dto salesmanVisitRealizationDto = inputDto.getDto("salesmanVisitRealizationDto");
- log.info("SALESMAN VISIT REALIZATION DTO proses {} --> {}", salesmanVisitRealizationDto);
- SalesmanVisitPlan salesmanVisitPlan = GsonUtil.fromDto(salesmanVisitPlanDto, SalesmanVisitPlan.class);
- salesmanVisitPlan = salesmanVisitPlanDao.merge(salesmanVisitPlan.getId(), salesmanVisitPlan);
- SalesmanVisitRealization salesmanVisitRealization = GsonUtil.fromDto(salesmanVisitRealizationDto, SalesmanVisitRealization.class);
- salesmanVisitRalizationDao.persist(salesmanVisitRealization);
- outputDto = new Dto(salesmanVisitRealization);
- return outputDto;
- }
- }
Add Comment
Please, Sign In to add comment