Exemplo n.º 1
0
        public void AddCustomerWithDependencies(Customer customer)
        {
            customer.PrimaryAddress.Key = _addressRepository.GetSurrogateKey();
            customer.PrimaryContact.Key = _contactRepository.GetSurrogateKey();
            var custKey = _customerRepository.GetSurrogateKey();

            customer.Key = custKey;

            customer.PrimaryCntctKey = customer.PrimaryContact.Key;
            customer.PrimaryAddrKey  = customer.PrimaryAddress.Key;
            customer.DocTransmittals.ForEach(dt => dt.Key = customer.Key);

            customer.CustAddresses.ForEach(a => a.CustKey = customer.Key);
            customer.CustAddresses.Single(a => a.Type == Enums.CustAddrType.Primary).Key          = customer.PrimaryAddress.Key;
            customer.CustAddresses.Single(a => a.Type == Enums.CustAddrType.Primary).DfltCntctKey = customer.PrimaryCntctKey;

            if (customer.DefaultBillToAddress != null) //not same as primary
            {
                customer.DefaultBillToAddress.Key = _addressRepository.GetSurrogateKey();
                customer.DfltBillToAddrKey        = customer.DefaultBillToAddress.Key;

                if (customer.CustAddresses.Single(a => a.Type == Enums.CustAddrType.BillTo) != null)
                {
                    customer.CustAddresses.Single(a => a.Type == Enums.CustAddrType.BillTo).Key        = customer.DefaultBillToAddress.Key;
                    customer.CustAddresses.Single(a => a.Type == Enums.CustAddrType.BillTo).CustAddrID = customer.DefaultBillToAddress.Key.ToString();
                }
            }
            else //same as primary
            {
                customer.DefaultBillToAddress = customer.PrimaryAddress;
                customer.DfltBillToAddrKey    = customer.DefaultBillToAddress.Key;
            }

            if (customer.DefaultShipToAddress != null)
            {
                customer.DefaultShipToAddress.Key = _addressRepository.GetSurrogateKey();
                customer.DfltShipToAddrKey        = customer.DefaultShipToAddress.Key;

                if (customer.CustAddresses.Single(a => a.Type == Enums.CustAddrType.ShipTo) != null)
                {
                    customer.CustAddresses.Single(a => a.Type == Enums.CustAddrType.ShipTo).Key        = customer.DefaultShipToAddress.Key;
                    customer.CustAddresses.Single(a => a.Type == Enums.CustAddrType.ShipTo).CustAddrID = customer.DefaultShipToAddress.Key.ToString();
                }
            }
            else
            {
                customer.DefaultShipToAddress = customer.PrimaryAddress;
                customer.DfltShipToAddrKey    = customer.DefaultShipToAddress.Key;
            }


            var custStatus = new CustStatus
            {
                Key           = customer.Key,
                AgeCat1Amt    = 0,
                AgeCat2Amt    = 0,
                AgeCat3Amt    = 0,
                AgeCat4Amt    = 0,
                AgeCurntAmt   = 0,
                AgeFutureAmt  = 0,
                AvgInvcAmt    = 0,
                FinChgBal     = 0,
                HighestBal    = 0,
                LastStmtAmt   = 0,
                LastStmtAmtNC = 0,
                OnSalesOrdAmt = 0,
                RetntBal      = 0
            };

            customer.CustStatus = custStatus;

            using (var scope = new TransactionScope())
            {
                try
                {
                    _customerRepository.AddCustomerWithDependecies(customer);
                    customer.PrimaryContact.CntctOwnerKey = custKey;
                    _contactRepository.Update(customer.PrimaryContact);

                    scope.Complete();
                }
                catch (Exception)
                {
                    scope.Dispose();

                    throw;
                }
            }
        }