Subversion Repositories SmartDukaan

Rev

Rev 37652 | 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 {
37822 amit 91
		WarehouseAddressMaster master = this.mappedAddress(warehouseId);
92
		String stateName = this.stateName(master);
93
 
94
		int synced = 0;
95
		for (FofoStore store : fofoStoreRepository.selectActivePartnerByWarehouse(warehouseId)) {
96
			if (!FofoType.INTERNAL.equals(store.getFofoType())) {
97
				continue;
98
			}
99
			this.applyAddress(store, master, stateName);
100
			synced++;
101
		}
102
		return synced;
103
	}
104
 
105
	@Override
106
	public void syncInternalStoreAddress(FofoStore store) throws ProfitMandiBusinessException {
107
		if (!FofoType.INTERNAL.equals(store.getFofoType())) {
108
			return;
109
		}
110
		WarehouseAddressMaster master = this.mappedAddress(store.getWarehouseId());
111
		this.applyAddress(store, master, this.stateName(master));
112
	}
113
 
114
	private WarehouseAddressMaster mappedAddress(int warehouseId) throws ProfitMandiBusinessException {
37652 amit 115
		WarehouseAddressMapping mapping = warehouseAddressMappingRepository.selectByWarehouseId(warehouseId);
116
		if (mapping == null) {
117
			throw new ProfitMandiBusinessException("Warehouse Address Mapping", warehouseId,
118
					"No address is mapped to this warehouse");
119
		}
37822 amit 120
		return warehouseAddressMasterRepository.selectById(mapping.getAddressId());
121
	}
122
 
123
	private String stateName(WarehouseAddressMaster master) throws ProfitMandiBusinessException {
37652 amit 124
		try {
37822 amit 125
			return stateRepository.selectById(master.getStateId()).getName();
37652 amit 126
		} catch (Exception e) {
127
			throw new ProfitMandiBusinessException("Warehouse Address State", master.getStateId(),
128
					"Problem while fetching state");
129
		}
37822 amit 130
	}
37652 amit 131
 
37822 amit 132
	private void applyAddress(FofoStore store, WarehouseAddressMaster master, String stateName)
133
			throws ProfitMandiBusinessException {
134
		CustomAddress warehouseAddress = master.getCustomAddress();
135
		User user = userUserRepository.selectById(store.getId());
136
		Address address = addressRepository.selectById(user.getAddressId());
137
		address.setLine1(warehouseAddress.getLine1());
138
		address.setLine2(warehouseAddress.getLine2());
139
		address.setCity(warehouseAddress.getCity());
140
		address.setState(stateName);
141
		address.setPinCode(warehouseAddress.getPinCode());
142
		address.setUpdateTimestamp(LocalDateTime.now());
143
		addressRepository.persist(address);
37652 amit 144
	}
31007 amit.gupta 145
}