Subversion Repositories SmartDukaan

Rev

Rev 32562 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
31702 amit.gupta 1
package com.spice.profitmandi.web.controller;
2
 
33243 ranu 3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
31702 amit.gupta 4
import com.spice.profitmandi.common.util.FileUtil;
5
import com.spice.profitmandi.common.util.FormattingUtils;
6
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
7
import com.spice.profitmandi.dao.entity.fofo.SamsungPCM;
8
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
9
import com.spice.profitmandi.dao.repository.fofo.SamsungPCMRepository;
10
import com.spice.profitmandi.dao.repository.warehouse.BilledImeiModel;
11
import com.spice.profitmandi.dao.repository.warehouse.WarehouseInventoryItemRepository;
12
import com.spice.profitmandi.service.user.RetailerService;
13
import org.apache.logging.log4j.LogManager;
14
import org.apache.logging.log4j.Logger;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.core.io.InputStreamResource;
17
import org.springframework.http.HttpHeaders;
18
import org.springframework.http.HttpStatus;
19
import org.springframework.http.ResponseEntity;
20
import org.springframework.stereotype.Controller;
21
import org.springframework.transaction.annotation.Transactional;
22
import org.springframework.ui.Model;
23
import org.springframework.web.bind.annotation.RequestBody;
24
import org.springframework.web.bind.annotation.RequestMapping;
25
import org.springframework.web.bind.annotation.RequestMethod;
26
import org.springframework.web.bind.annotation.RequestParam;
27
 
28
import java.io.ByteArrayInputStream;
29
import java.io.InputStream;
30
import java.time.LocalDate;
31
import java.util.Arrays;
32
import java.util.List;
33
import java.util.Locale;
34
import java.util.Map;
35
import java.util.stream.Collectors;
36
 
37
@Controller
38
@Transactional(rollbackFor = Throwable.class)
39
public class BrandPCMController {
40
    private static final Logger LOGGER = LogManager.getLogger(AuthUserController.class);
41
 
42
    @Autowired
43
    SamsungPCMRepository samsungPCMRepository;
44
    @Autowired
45
    RetailerService retailerService;
46
 
47
    @Autowired
48
    FofoStoreRepository fofoStoreRepository;
49
 
50
    @Autowired
51
    WarehouseInventoryItemRepository warehouseInventoryItemRepository;
52
 
53
    @RequestMapping(value = "/brand-pcm", method = RequestMethod.GET)
33243 ranu 54
    public String brandPcm(Model model) throws ProfitMandiBusinessException {
31702 amit.gupta 55
        Map<Integer, LocalDate> samsungPCMMap = samsungPCMRepository.selectAll().stream().collect(Collectors.toMap(x -> x.getFofoId(), x -> x.getPcmDate()));
56
        model.addAttribute("samsungPCMMap", samsungPCMMap);
57
        model.addAttribute("retailerMap", retailerService.getAllFofoRetailers());
58
        return "manage-pcm";
59
    }
60
 
61
    @RequestMapping(value = "/brand-pcm", method = RequestMethod.POST)
32562 amit.gupta 62
    public String addBrandPCM(Model model, @RequestBody BrandPcmDateModel brandPcmDateModel) throws Exception {
31702 amit.gupta 63
 
64
        SamsungPCM samsungPCM = new SamsungPCM();
65
        samsungPCM.setFofoId(brandPcmDateModel.getFofoId());
66
        samsungPCM.setPcmDate(brandPcmDateModel.getPcmDate());
67
        samsungPCMRepository.persist(samsungPCM);
68
 
69
        model.addAttribute("response1", true);
70
        return "response";
71
    }
72
 
73
    @RequestMapping(value = "/brand-pcm/download", method = RequestMethod.GET)
32562 amit.gupta 74
    public ResponseEntity<InputStreamResource> downloadBrandPCM(@RequestParam int fofoId) throws Exception {
31702 amit.gupta 75
 
76
        FofoStore fs = fofoStoreRepository.selectByRetailerId(fofoId);
77
 
78
        List<BilledImeiModel> billedImeiModels = warehouseInventoryItemRepository.findStockByFofoIdBrandToRebill(fofoId, "Samsung");
79
        List<List<?>> rows = billedImeiModels.stream().map(x -> Arrays.asList(x.getFofoId(), x.getStoreCode(), x.getStoreName(), x.getStoreCity(), x.getBrand(), x.getModelName(), x.getModelNumber(), x.getColor(), x.getSerialNumber(),
80
                FormattingUtils.formatDate(x.getPcmDate()), FormattingUtils.formatDate(x.getInvoiceDate())
81
        )).collect(Collectors.toList());
82
 
83
        org.apache.commons.io.output.ByteArrayOutputStream baos = FileUtil.getCSVByteStream(Arrays.asList("Store Id",
84
                "Store Code", "Business Name", "City", "Brand", "Model Name", "Model Number", "Color", "Imei", "Invoice Date", "PCM Date"), rows);
85
 
86
        final HttpHeaders headers = new HttpHeaders();
87
        headers.set("Content-Type", "text/csv");
88
        headers.set("Content-disposition", "inline; filename=pcm-" + fs.getCode().toLowerCase(Locale.ROOT) + ".csv");
89
        headers.setContentLength(baos.toByteArray().length);
90
 
91
        final InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
92
        final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
93
 
94
        return new ResponseEntity<>(inputStreamResource, headers, HttpStatus.OK);
95
    }
96
 
97
 
98
}