Subversion Repositories SmartDukaan

Rev

Rev 37652 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.service;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.enumuration.FofoType;
import com.spice.profitmandi.common.model.CustomAddress;
import com.spice.profitmandi.common.model.CustomRetailer;
import com.spice.profitmandi.dao.entity.transaction.*;
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
import com.spice.profitmandi.dao.entity.user.Address;
import com.spice.profitmandi.dao.entity.user.User;
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
import com.spice.profitmandi.dao.repository.inventory.StateRepository;
import com.spice.profitmandi.dao.repository.user.AddressRepository;
import com.spice.profitmandi.dao.repository.user.UserRepository;
import com.spice.profitmandi.dao.repository.transaction.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class SellerServiceImpl implements SellerService {

        @Autowired
        OrganisationRepository organisationRepository;

        @Autowired
        SellerRepository sellerRepository;

        @Autowired
        SellerWarehouseRepository sellerWarehouseRepository;

        @Autowired
        StateRepository stateRepository;

        @Autowired
        WarehouseAddressMappingRepository warehouseAddressMappingRepository;

        @Autowired
        WarehouseAddressMasterRepository warehouseAddressMasterRepository;

        @Autowired
        FofoStoreRepository fofoStoreRepository;

        @Autowired
        UserRepository userUserRepository;

        @Autowired
        AddressRepository addressRepository;

        public CustomRetailer convertToSeller(String sellerPrefix) throws ProfitMandiBusinessException {
                SellerWarehouse sellerWarehouse = sellerWarehouseRepository.selectByPrefix(sellerPrefix);
                Seller sellerInfo = sellerRepository.selectById(sellerWarehouse.getSellerId());
                Organisation organisation = organisationRepository.selectById(sellerInfo.getOrganisationId());
                CustomRetailer customRetailer = new CustomRetailer();
                customRetailer.setBusinessName(organisation.getName());
                customRetailer.setGstNumber(sellerInfo.getGstin());
                WarehouseAddressMapping warehouseAddressMapping = warehouseAddressMappingRepository.selectByWarehouseId(sellerWarehouse.getWarehouseId());
                WarehouseAddressMaster warehouseAddressMaster = warehouseAddressMasterRepository.selectById(warehouseAddressMapping.getAddressId());

                CustomAddress customAddress = new CustomAddress();
                try {
                        customAddress.setState(stateRepository.selectById(warehouseAddressMaster.getStateId()).getName());
                } catch (Exception e) {
                        throw new ProfitMandiBusinessException("Warehouse Address State", warehouseAddressMaster.getStateId(),
                                        "Problem while fetching state");
                }
                customAddress.setCity(warehouseAddressMaster.getAddress());
                customAddress.setPinCode(warehouseAddressMaster.getPin());
                customAddress.setPhoneNumber(warehouseAddressMaster.getContacNumber());
                customAddress.setName(organisation.getName());

                customRetailer.setAddress(customAddress);

                return customRetailer;
        }

        /**
         * An INTERNAL store is the buying identity of a warehouse, so its user.address has to BE the
         * warehouse address: order creation reads user.address for every buyer alike and no longer
         * special-cases internal stores. Lines are split exactly as getCustomAddress() does, because
         * that is the shape the order columns were previously filled with directly - any other split
         * would silently change the address printed on the next invoice.
         *
         * No fallback to whatever the store already had: a warehouse with no address mapping is a data
         * fault and must surface here, rather than leave an internal store quietly billing to a stale
         * address, which is the failure this sync exists to end.
         */
        @Override
        public int syncInternalStoreAddresses(int warehouseId) throws ProfitMandiBusinessException {
                WarehouseAddressMaster master = this.mappedAddress(warehouseId);
                String stateName = this.stateName(master);

                int synced = 0;
                for (FofoStore store : fofoStoreRepository.selectActivePartnerByWarehouse(warehouseId)) {
                        if (!FofoType.INTERNAL.equals(store.getFofoType())) {
                                continue;
                        }
                        this.applyAddress(store, master, stateName);
                        synced++;
                }
                return synced;
        }

        @Override
        public void syncInternalStoreAddress(FofoStore store) throws ProfitMandiBusinessException {
                if (!FofoType.INTERNAL.equals(store.getFofoType())) {
                        return;
                }
                WarehouseAddressMaster master = this.mappedAddress(store.getWarehouseId());
                this.applyAddress(store, master, this.stateName(master));
        }

        private WarehouseAddressMaster mappedAddress(int warehouseId) throws ProfitMandiBusinessException {
                WarehouseAddressMapping mapping = warehouseAddressMappingRepository.selectByWarehouseId(warehouseId);
                if (mapping == null) {
                        throw new ProfitMandiBusinessException("Warehouse Address Mapping", warehouseId,
                                        "No address is mapped to this warehouse");
                }
                return warehouseAddressMasterRepository.selectById(mapping.getAddressId());
        }

        private String stateName(WarehouseAddressMaster master) throws ProfitMandiBusinessException {
                try {
                        return stateRepository.selectById(master.getStateId()).getName();
                } catch (Exception e) {
                        throw new ProfitMandiBusinessException("Warehouse Address State", master.getStateId(),
                                        "Problem while fetching state");
                }
        }

        private void applyAddress(FofoStore store, WarehouseAddressMaster master, String stateName)
                        throws ProfitMandiBusinessException {
                CustomAddress warehouseAddress = master.getCustomAddress();
                User user = userUserRepository.selectById(store.getId());
                Address address = addressRepository.selectById(user.getAddressId());
                address.setLine1(warehouseAddress.getLine1());
                address.setLine2(warehouseAddress.getLine2());
                address.setCity(warehouseAddress.getCity());
                address.setState(stateName);
                address.setPinCode(warehouseAddress.getPinCode());
                address.setUpdateTimestamp(LocalDateTime.now());
                addressRepository.persist(address);
        }
}