Subversion Repositories SmartDukaan

Rev

Rev 32383 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
31007 amit.gupta 1
package com.spice.profitmandi.service;
2
 
3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
37652 amit 4
import com.spice.profitmandi.common.enumuration.FofoType;
31007 amit.gupta 5
import com.spice.profitmandi.common.model.CustomAddress;
6
import com.spice.profitmandi.common.model.CustomRetailer;
7
import com.spice.profitmandi.dao.entity.transaction.*;
37652 amit 8
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
9
import com.spice.profitmandi.dao.entity.user.Address;
10
import com.spice.profitmandi.dao.entity.user.User;
11
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
31007 amit.gupta 12
import com.spice.profitmandi.dao.repository.inventory.StateRepository;
37652 amit 13
import com.spice.profitmandi.dao.repository.user.AddressRepository;
14
import com.spice.profitmandi.dao.repository.user.UserRepository;
31007 amit.gupta 15
import com.spice.profitmandi.dao.repository.transaction.*;
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.stereotype.Component;
18
 
37652 amit 19
import java.time.LocalDateTime;
20
 
31007 amit.gupta 21
@Component
22
public class SellerServiceImpl implements SellerService {
23
 
24
	@Autowired
25
	OrganisationRepository organisationRepository;
26
 
27
	@Autowired
28
	SellerRepository sellerRepository;
29
 
30
	@Autowired
31
	SellerWarehouseRepository sellerWarehouseRepository;
32
 
33
	@Autowired
34
	StateRepository stateRepository;
35
 
36
	@Autowired
32383 amit.gupta 37
	WarehouseAddressMappingRepository warehouseAddressMappingRepository;
31007 amit.gupta 38
 
39
	@Autowired
32383 amit.gupta 40
	WarehouseAddressMasterRepository warehouseAddressMasterRepository;
31007 amit.gupta 41
 
37652 amit 42
	@Autowired
43
	FofoStoreRepository fofoStoreRepository;
44
 
45
	@Autowired
46
	UserRepository userUserRepository;
47
 
48
	@Autowired
49
	AddressRepository addressRepository;
50
 
31007 amit.gupta 51
	public CustomRetailer convertToSeller(String sellerPrefix) throws ProfitMandiBusinessException {
52
		SellerWarehouse sellerWarehouse = sellerWarehouseRepository.selectByPrefix(sellerPrefix);
53
		Seller sellerInfo = sellerRepository.selectById(sellerWarehouse.getSellerId());
54
		Organisation organisation = organisationRepository.selectById(sellerInfo.getOrganisationId());
55
		CustomRetailer customRetailer = new CustomRetailer();
56
		customRetailer.setBusinessName(organisation.getName());
57
		customRetailer.setGstNumber(sellerInfo.getGstin());
32383 amit.gupta 58
		WarehouseAddressMapping warehouseAddressMapping = warehouseAddressMappingRepository.selectByWarehouseId(sellerWarehouse.getWarehouseId());
59
		WarehouseAddressMaster warehouseAddressMaster = warehouseAddressMasterRepository.selectById(warehouseAddressMapping.getAddressId());
31007 amit.gupta 60
 
61
		CustomAddress customAddress = new CustomAddress();
62
		try {
63
			customAddress.setState(stateRepository.selectById(warehouseAddressMaster.getStateId()).getName());
64
		} catch (Exception e) {
65
			throw new ProfitMandiBusinessException("Warehouse Address State", warehouseAddressMaster.getStateId(),
66
					"Problem while fetching state");
67
		}
68
		customAddress.setCity(warehouseAddressMaster.getAddress());
69
		customAddress.setPinCode(warehouseAddressMaster.getPin());
70
		customAddress.setPhoneNumber(warehouseAddressMaster.getContacNumber());
71
		customAddress.setName(organisation.getName());
72
 
73
		customRetailer.setAddress(customAddress);
74
 
75
		return customRetailer;
76
	}
37652 amit 77
 
78
	/**
79
	 * An INTERNAL store is the buying identity of a warehouse, so its user.address has to BE the
80
	 * warehouse address: order creation reads user.address for every buyer alike and no longer
81
	 * special-cases internal stores. Lines are split exactly as getCustomAddress() does, because
82
	 * that is the shape the order columns were previously filled with directly - any other split
83
	 * would silently change the address printed on the next invoice.
84
	 *
85
	 * No fallback to whatever the store already had: a warehouse with no address mapping is a data
86
	 * fault and must surface here, rather than leave an internal store quietly billing to a stale
87
	 * address, which is the failure this sync exists to end.
88
	 */
89
	@Override
90
	public int syncInternalStoreAddresses(int warehouseId) throws ProfitMandiBusinessException {
91
		WarehouseAddressMapping mapping = warehouseAddressMappingRepository.selectByWarehouseId(warehouseId);
92
		if (mapping == null) {
93
			throw new ProfitMandiBusinessException("Warehouse Address Mapping", warehouseId,
94
					"No address is mapped to this warehouse");
95
		}
96
		WarehouseAddressMaster master = warehouseAddressMasterRepository.selectById(mapping.getAddressId());
97
		CustomAddress warehouseAddress = master.getCustomAddress();
98
		String stateName;
99
		try {
100
			stateName = stateRepository.selectById(master.getStateId()).getName();
101
		} catch (Exception e) {
102
			throw new ProfitMandiBusinessException("Warehouse Address State", master.getStateId(),
103
					"Problem while fetching state");
104
		}
105
 
106
		int synced = 0;
107
		for (FofoStore store : fofoStoreRepository.selectActivePartnerByWarehouse(warehouseId)) {
108
			if (!FofoType.INTERNAL.equals(store.getFofoType())) {
109
				continue;
110
			}
111
			User user = userUserRepository.selectById(store.getId());
112
			Address address = addressRepository.selectById(user.getAddressId());
113
			address.setLine1(warehouseAddress.getLine1());
114
			address.setLine2(warehouseAddress.getLine2());
115
			address.setCity(warehouseAddress.getCity());
116
			address.setState(stateName);
117
			address.setPinCode(warehouseAddress.getPinCode());
118
			address.setUpdateTimestamp(LocalDateTime.now());
119
			addressRepository.persist(address);
120
			synced++;
121
		}
122
		return synced;
123
	}
31007 amit.gupta 124
}