You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
12KB

  1. package vn.azteam.tpf.web.rest;
  2. import com.codahale.metrics.annotation.Timed;
  3. import io.github.jhipster.service.filter.LongFilter;
  4. import org.springframework.transaction.annotation.Transactional;
  5. import vn.azteam.tpf.service.*;
  6. import vn.azteam.tpf.service.dto.*;
  7. import vn.azteam.tpf.web.rest.errors.BadRequestAlertException;
  8. import vn.azteam.tpf.web.rest.util.HeaderUtil;
  9. import vn.azteam.tpf.web.rest.util.PaginationUtil;
  10. import io.github.jhipster.web.util.ResponseUtil;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.data.domain.Page;
  14. import org.springframework.data.domain.Pageable;
  15. import org.springframework.http.HttpHeaders;
  16. import org.springframework.http.HttpStatus;
  17. import org.springframework.http.ResponseEntity;
  18. import org.springframework.web.bind.annotation.*;
  19. import vn.azteam.tpf.service.util.UserRoleUtil;
  20. import java.net.URI;
  21. import java.net.URISyntaxException;
  22. import java.util.List;
  23. import java.util.Optional;
  24. /**
  25. * REST controller for managing TBStockUpdate.
  26. */
  27. @RestController
  28. @RequestMapping("/api")
  29. public class TBStockUpdateResource {
  30. private final Logger log = LoggerFactory.getLogger(TBStockUpdateResource.class);
  31. private static final String ENTITY_NAME = "tBStockUpdate";
  32. private final TBStockUpdateService tBStockUpdateService;
  33. private final TBStockUpdateQueryService tBStockUpdateQueryService;
  34. private final UserRoleUtil userRoleUtil;
  35. private final UserService userService;
  36. private final TBSuppliesService tBSuppliesService;
  37. private final TBSuppliesInWarehouseQueryService tBSuppliesInWarehouseQueryService;
  38. private final TBSuppliesUsingDetailsService tbSuppliesUsingDetailsService;
  39. private final TBSuppliesUsingDetailsQueryService tbSuppliesUsingDetailsQueryService;
  40. private final TBSuppliesInWarehouseService tBSuppliesInWarehouseService;
  41. public TBStockUpdateResource(TBStockUpdateService tBStockUpdateService,
  42. TBStockUpdateQueryService tBStockUpdateQueryService,
  43. UserRoleUtil userRoleUtil,
  44. UserService userService,
  45. TBSuppliesService tBSuppliesService,
  46. TBSuppliesInWarehouseQueryService tBSuppliesInWarehouseQueryService,
  47. TBSuppliesUsingDetailsService tbSuppliesUsingDetailsService,
  48. TBSuppliesUsingDetailsQueryService tbSuppliesUsingDetailsQueryService, TBSuppliesInWarehouseService tBSuppliesInWarehouseService) {
  49. this.tBStockUpdateService = tBStockUpdateService;
  50. this.tBStockUpdateQueryService = tBStockUpdateQueryService;
  51. this.userRoleUtil = userRoleUtil;
  52. this.userService = userService;
  53. this.tBSuppliesService = tBSuppliesService;
  54. this.tBSuppliesInWarehouseQueryService = tBSuppliesInWarehouseQueryService;
  55. this.tbSuppliesUsingDetailsService = tbSuppliesUsingDetailsService;
  56. this.tbSuppliesUsingDetailsQueryService = tbSuppliesUsingDetailsQueryService;
  57. this.tBSuppliesInWarehouseService = tBSuppliesInWarehouseService;
  58. }
  59. /**
  60. * POST /tb-stock-updates : Create a new tBStockUpdate.
  61. *
  62. * @param tBStockUpdateDTO the tBStockUpdateDTO to create
  63. * @return the ResponseEntity with status 201 (Created) and with body the new tBStockUpdateDTO, or with status 400 (Bad Request) if the tBStockUpdate has already an ID
  64. * @throws URISyntaxException if the Location URI syntax is incorrect
  65. */
  66. @PostMapping("/tb-stock-updates")
  67. @Timed
  68. public ResponseEntity<TBStockUpdateDTO> createTBStockUpdate(@RequestBody TBStockUpdateDTO tBStockUpdateDTO) throws URISyntaxException {
  69. log.debug("REST request to save TBStockUpdate : {}", tBStockUpdateDTO);
  70. if (tBStockUpdateDTO.getId() != null) {
  71. throw new BadRequestAlertException("A new tBStockUpdate cannot already have an ID", ENTITY_NAME, "idexists");
  72. }
  73. TBStockUpdateDTO result = tBStockUpdateService.save(tBStockUpdateDTO);
  74. return ResponseEntity.created(new URI("/api/tb-stock-updates/" + result.getId()))
  75. .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
  76. .body(result);
  77. }
  78. /**
  79. * PUT /tb-stock-updates : Updates an existing tBStockUpdate.
  80. *
  81. * @param tBStockUpdateDTO the tBStockUpdateDTO to update
  82. * @return the ResponseEntity with status 200 (OK) and with body the updated tBStockUpdateDTO,
  83. * or with status 400 (Bad Request) if the tBStockUpdateDTO is not valid,
  84. * or with status 500 (Internal Server Error) if the tBStockUpdateDTO couldn't be updated
  85. * @throws URISyntaxException if the Location URI syntax is incorrect
  86. */
  87. @PutMapping("/tb-stock-updates")
  88. @Timed
  89. public ResponseEntity<TBStockUpdateDTO> updateTBStockUpdate(@RequestBody TBStockUpdateDTO tBStockUpdateDTO) throws URISyntaxException {
  90. log.debug("REST request to update TBStockUpdate : {}", tBStockUpdateDTO);
  91. if (tBStockUpdateDTO.getId() == null) {
  92. throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
  93. }
  94. TBStockUpdateDTO result = tBStockUpdateService.save(tBStockUpdateDTO);
  95. return ResponseEntity.ok()
  96. .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, tBStockUpdateDTO.getId().toString()))
  97. .body(result);
  98. }
  99. /**
  100. * GET /tb-stock-updates : get all the tBStockUpdates.
  101. *
  102. * @param pageable the pagination information
  103. * @param criteria the criterias which the requested entities should match
  104. * @return the ResponseEntity with status 200 (OK) and the list of tBStockUpdates in body
  105. */
  106. @GetMapping("/tb-stock-updates")
  107. @Timed
  108. public ResponseEntity<List<TBStockUpdateDTO>> getAllTBStockUpdates(TBStockUpdateCriteria criteria, Pageable pageable) {
  109. log.debug("REST request to get TBStockUpdates by criteria: {}", criteria);
  110. Page<TBStockUpdateDTO> page = tBStockUpdateQueryService.findByCriteria(criteria, pageable);
  111. HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-stock-updates");
  112. return ResponseEntity.ok().headers(headers).body(page.getContent());
  113. }
  114. /**
  115. * GET /tb-stock-updates/count : count all the tBStockUpdates.
  116. *
  117. * @param criteria the criterias which the requested entities should match
  118. * @return the ResponseEntity with status 200 (OK) and the count in body
  119. */
  120. @GetMapping("/tb-stock-updates/count")
  121. @Timed
  122. public ResponseEntity<Long> countTBStockUpdates(TBStockUpdateCriteria criteria) {
  123. log.debug("REST request to count TBStockUpdates by criteria: {}", criteria);
  124. return ResponseEntity.ok().body(tBStockUpdateQueryService.countByCriteria(criteria));
  125. }
  126. /**
  127. * GET /tb-stock-updates/:id : get the "id" tBStockUpdate.
  128. *
  129. * @param id the id of the tBStockUpdateDTO to retrieve
  130. * @return the ResponseEntity with status 200 (OK) and with body the tBStockUpdateDTO, or with status 404 (Not Found)
  131. */
  132. @GetMapping("/tb-stock-updates/{id}")
  133. @Timed
  134. public ResponseEntity<TBStockUpdateDTO> getTBStockUpdate(@PathVariable Long id) {
  135. log.debug("REST request to get TBStockUpdate : {}", id);
  136. Optional<TBStockUpdateDTO> tBStockUpdateDTO = tBStockUpdateService.findOne(id);
  137. return ResponseUtil.wrapOrNotFound(tBStockUpdateDTO);
  138. }
  139. /**
  140. * DELETE /tb-stock-updates/:id : delete the "id" tBStockUpdate.
  141. *
  142. * @param id the id of the tBStockUpdateDTO to delete
  143. * @return the ResponseEntity with status 200 (OK)
  144. */
  145. @DeleteMapping("/tb-stock-updates/{id}")
  146. @Timed
  147. @Transactional(readOnly = false)
  148. public ResponseEntity<Void> deleteTBStockUpdate(@PathVariable Long id) {
  149. log.debug("REST request to delete TBStockUpdate : {}", id);
  150. TBStockUpdateDTO tBStockUpdateDTO = tBStockUpdateService.findOne(id).get();
  151. tBStockUpdateService.delete(id);
  152. TBSuppliesUsingDetailsCriteria criteria = new TBSuppliesUsingDetailsCriteria();
  153. LongFilter tbSuppliesInWarehouseIdFilter = new LongFilter();
  154. tbSuppliesInWarehouseIdFilter.setEquals(tBStockUpdateDTO.getTbSuppliesInWarehouseId());
  155. criteria.setTbSuppliesInWarehouseId(tbSuppliesInWarehouseIdFilter);
  156. LongFilter tbActivityIdFilter = new LongFilter();
  157. tbActivityIdFilter.setEquals(tBStockUpdateDTO.getTbActivityId());
  158. criteria.setTbActivityId(tbActivityIdFilter);
  159. LongFilter tbBlockDetailsIdFilter = new LongFilter();
  160. tbBlockDetailsIdFilter.setEquals(tBStockUpdateDTO.getTbBlockDetailsId());
  161. criteria.setTbBlockDetailsId(tbBlockDetailsIdFilter);
  162. List<TBSuppliesUsingDetailsDTO> tbSuppliesUsingDetailsDTOs = tbSuppliesUsingDetailsQueryService.findByCriteria(criteria);
  163. if(tbSuppliesUsingDetailsDTOs != null && tbSuppliesUsingDetailsDTOs.size() > 0) {
  164. TBSuppliesUsingDetailsDTO tbSuppliesUsingDetailsDTO = tbSuppliesUsingDetailsDTOs.get(0);
  165. tbSuppliesUsingDetailsService.delete(tbSuppliesUsingDetailsDTO.getId());
  166. }
  167. tBSuppliesInWarehouseQueryService.updateQuantityForTBSuppliesInWarehouseDTO(tBStockUpdateDTO.getTbSuppliesInWarehouseId());
  168. TBSuppliesInWarehouseDTO tbSuppliesInWarehouseDTO = tBSuppliesInWarehouseService.findOne(tBStockUpdateDTO.getTbSuppliesInWarehouseId()).get();
  169. tBSuppliesInWarehouseQueryService.updateQuantityForTBBlockDetailsDTO(tBStockUpdateDTO.getTbSuppliesInWarehouseId(), tBStockUpdateDTO.getTbBlockDetailsId(), tbSuppliesInWarehouseDTO.getTbSuppliesId());
  170. return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
  171. }
  172. /**
  173. * SEARCH /_search/tb-stock-updates?query=:query : search for the tBStockUpdate corresponding
  174. * to the query.
  175. *
  176. * @param query the query of the tBStockUpdate search
  177. * @param pageable the pagination information
  178. * @return the result of the search
  179. */
  180. @GetMapping("/_search/tb-stock-updates")
  181. @Timed
  182. public ResponseEntity<List<TBStockUpdateDTO>> searchTBStockUpdates(@RequestParam String query, Pageable pageable) {
  183. log.debug("REST request to search for a page of TBStockUpdates for query {}", query);
  184. Page<TBStockUpdateDTO> page = tBStockUpdateService.search(query, pageable);
  185. HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/tb-stock-updates");
  186. return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
  187. }
  188. @GetMapping("/historySupplies/{suppliesId}")
  189. @Timed
  190. public ResponseEntity<List<TBStockUpdateDTO>> historySupplies(@PathVariable String suppliesId,
  191. TBStockUpdateCriteria criteria, Pageable pageable) {
  192. log.debug("REST request to get TBSupplies by criteria: {}", criteria);
  193. Long suppliesIdLong;
  194. suppliesIdLong = Long.parseLong(suppliesId);
  195. if (suppliesId == null){
  196. throw new BadRequestAlertException("Invalid suppliesId", ENTITY_NAME, "idnull");
  197. }
  198. //@CUONGLT - Filter scope customer's data
  199. UserDTO currentUser = userService.getCurrentUserDTO().get();
  200. Optional<TBSuppliesDTO> suppliesDTO = tBSuppliesService.findOne(suppliesIdLong);
  201. if (currentUser.getCustomerId() != null
  202. && suppliesDTO.get().getTbCustomerId() != null
  203. && currentUser.getCustomerId() != suppliesDTO.get().getTbCustomerId()){
  204. throw new BadRequestAlertException("1013", ENTITY_NAME, "1013");
  205. }
  206. Page<TBStockUpdateDTO> page = tBStockUpdateQueryService.getHistoryTBStockUpdate(criteria, pageable, suppliesIdLong);
  207. HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/historySupplies");
  208. return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
  209. }
  210. }