Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
37833 amit 1
package com.smartdukaan.cron.migrations;
2
 
3
import com.spice.profitmandi.dao.entity.transaction.Order;
4
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
5
import com.spice.profitmandi.service.transaction.invoicing.InvoiceService;
6
import org.apache.logging.log4j.LogManager;
7
import org.apache.logging.log4j.Logger;
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.stereotype.Component;
10
import org.springframework.transaction.annotation.Propagation;
11
import org.springframework.transaction.annotation.Transactional;
12
 
13
import java.util.List;
14
 
15
/**
16
 * Re-files the e-invoice for an invoice NIC rejected (irn_generated = 0) once its data is corrected. The reset to
17
 * pending and the filing happen in one transaction: the scheduled IRN job only sees committed pending invoices, so it
18
 * cannot race this one, and a fresh process reads the corrected item master rather than a cached copy.
19
 */
20
@Component
21
public class IrnRetrier {
22
 
23
    private static final Logger LOGGER = LogManager.getLogger(IrnRetrier.class);
24
 
25
    @Autowired
26
    private OrderRepository orderRepository;
27
 
28
    @Autowired
29
    private InvoiceService invoiceService;
30
 
31
    /** Returns the invoice's irn_generated afterwards; on any failure everything rolls back to how it was. */
32
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Throwable.class)
33
    public Boolean retry(String invoiceNumber) throws Exception {
34
        List<Order> orders = orderRepository.selectByInvoiceNumber(invoiceNumber);
35
        if (orders.isEmpty()) {
36
            throw new IllegalArgumentException("No orders for invoice " + invoiceNumber);
37
        }
38
        if (!Boolean.FALSE.equals(orders.get(0).isIrnGenerated())) {
39
            throw new IllegalStateException(invoiceNumber + " is not a rejected e-invoice (irn_generated="
40
                    + orders.get(0).isIrnGenerated() + ")");
41
        }
42
        for (Order order : orders) {
43
            order.setIrnGenerated(null);
44
        }
45
        invoiceService.saveInvoice(orders);
46
        Boolean result = orders.get(0).isIrnGenerated();
47
        if (!Boolean.TRUE.equals(result)) {
48
            throw new IllegalStateException(invoiceNumber + " still not filed (irn_generated=" + result + ")");
49
        }
50
        return result;
51
    }
52
 
53
    public void retryAll(List<String> invoiceNumbers) {
54
        for (String invoiceNumber : invoiceNumbers) {
55
            try {
56
                LOGGER.info("IRN RETRY {} : irn_generated={}", invoiceNumber, this.self().retry(invoiceNumber));
57
            } catch (Exception e) {
58
                LOGGER.error("IRN RETRY {} FAILED, left as it was - {}", invoiceNumber, e.getMessage(), e);
59
            }
60
        }
61
    }
62
 
63
    @Autowired
64
    private IrnRetrier self;
65
 
66
    private IrnRetrier self() {
67
        return self;
68
    }
69
}