/// <summary>
        /// Makes the shipping report.
        /// </summary>
        /// <param name="_sp">The _SP.</param>
        /// <param name="EDC">The EDC.</param>
        /// <param name="_result">The _result.</param>
        private void MakeOutboundReport(Shipping _sp, EntitiesDataContext EDC, ActionResult _result)
        {
            try
            {
                _sp.EuroPalletsQuantity = 0;
                _sp.InduPalletsQuantity = 0;
                _sp.TotalQuantityKU     = 0;
                foreach (LoadDescription _ld in _sp.LoadDescriptions(EDC))
                {
                    switch (_ld.PalletType.Value)
                    {
                    case PalletType.Euro:
                        _sp.EuroPalletsQuantity += _ld.NumberOfPallets.GetValueOrDefault(0);
                        break;

                    case PalletType.Industrial:
                        _sp.InduPalletsQuantity += _ld.NumberOfPallets.GetValueOrDefault(0);
                        break;

                    case PalletType.None:
                    case PalletType.Invalid:
                    case PalletType.Other:
                    default:
                        break;
                    }
                    _sp.TotalQuantityKU += _ld.GoodsQuantity.GetValueOrDefault(0);
                }
                _sp.ShippingCarrierTitle = _sp.Shipping2RouteTitle == null?String.Empty.NotAvailable() : _sp.Shipping2RouteTitle.CarrierTitle.Title();

                Currency _defCurrency = (from Currency _cu in EDC.Currency
                                         where !String.IsNullOrEmpty(_cu.Title) && _cu.Title.ToUpper().Contains(CommonDefinition.DefaultCurrency)
                                         select _cu).FirstOrDefault();
                _sp.Shipping2Currency4CostsPerKU = _defCurrency;
                //Costs calculation
                if (_sp.Shipping2RouteTitle != null)
                {
                    _sp.Shipping2CurrencyForFreight = _defCurrency;
                    if (_sp.Shipping2RouteTitle.CurrencyTitle != null)
                    {
                        _sp.ShippingFreightCost = _sp.Shipping2RouteTitle.TransportCosts * _sp.Shipping2RouteTitle.CurrencyTitle.ExchangeRate;
                    }
                    _sp.ShippingCommodityTitle    = _sp.Shipping2RouteTitle.Route2Commodity.Title();
                    _sp.ShippingFreightPayerTitle = _sp.Shipping2RouteTitle.FreightPayerTitle == null?String.Empty.NotAvailable() : _sp.Shipping2RouteTitle.FreightPayerTitle.Title();

                    _sp.ShippingRouteDepartureCity = _sp.Shipping2RouteTitle.DepartureCity;
                    _sp.ShippingCountryTitle       = _sp.Shipping2RouteTitle.Route2CityTitle == null?String.Empty.NotAvailable() : _sp.Shipping2RouteTitle.Route2CityTitle.CountryTitle.Title();
                }
                if (_sp.SecurityEscortCatalogTitle != null && _sp.SecurityEscortCatalogTitle.CurrencyTitle != null)
                {
                    _sp.ShippingSecurityCost       = _sp.SecurityEscortCatalogTitle.SecurityCost * _sp.SecurityEscortCatalogTitle.CurrencyTitle.ExchangeRate;
                    _sp.Shipping2CurrencyForEscort = _defCurrency;
                }
                double?_totalCost = default(double?);
                double _addCost   = 0;
                if (_sp.Shipping2Currency4AddCosts != null)
                {
                    _addCost = (_sp.AdditionalCosts * _sp.Shipping2Currency4AddCosts.ExchangeRate).GetValueOrDefault(0);
                }
                _totalCost          = _sp.ShippingFreightCost.GetValueOrDefault(0) + _sp.ShippingSecurityCost.GetValueOrDefault(0) + _addCost;
                _sp.TotalCostsPerKU = _sp.TotalQuantityKU.HasValue && _sp.TotalQuantityKU.Value > 1 ? _totalCost / _sp.TotalQuantityKU.Value : new Nullable <double>();
                _sp.ReportPeriod    = _sp.StartTime.Value.ToMonthString();
            }
            catch (Exception ex)
            {
                _result.AddException("MakeShippingReport", ex);
            }
        }
        /// <summary>
        /// Makes the performance report - creates a new entry on the <see cref="CarrierPerformanceReport"/> for each shipping
        /// </summary>
        /// <param name="EDC">The EDC.</param>
        /// <param name="sp">The <see cref="Shipping"/>.</param>
        /// <param name="result">The result.</param>
        /// <exception cref="System.ApplicationException">
        /// <see cref="ApplicationException"/>: ShippingAssociatedPartner
        /// <see cref="ApplicationException"/>: MakePerformanceReportDataInconsistent
        /// </exception>
        private void MakePerformanceReport(EntitiesDataContext EDC, Shipping sp, ActionResult result)
        {
            try
            {
                DateTime _sDate = sp.StartTime.Value.Date;
                if (sp.PartnerTitle == null)
                {
                    throw new ApplicationException("ShippingAssociatedPartner".GetLocalizedString());
                }
                CarrierPerformanceReport _rprt = (from _rx in sp.PartnerTitle.CarrierPerformanceReport
                                                  where _rx.CPRDate.Value == _sDate
                                                  select _rx).FirstOrDefault();
                if (_rprt == null)
                {
                    _rprt = new CarrierPerformanceReport()
                    {
                        CPRDate                     = sp.StartTime.Value.Date,
                        CPR2PartnerTitle            = sp.PartnerTitle,
                        Title                       = sp.PartnerTitle.Title(),
                        CPRNumberDelayed            = 0,
                        CPRNumberDelayed1h          = 0,
                        CPRNumberNotShowingUp       = 0,
                        CPRNumberOnTime             = 0,
                        CPRNumberOrdered            = 0,
                        CPRNumberRejectedBadQuality = 0,
                        ReportPeriod                = sp.StartTime.Value.ToMonthString()
                    };
                    EDC.CarrierPerformanceReport.InsertOnSubmit(_rprt);
                }
                _rprt.CPRNumberOrdered++;
                List <TimeSlotTimeSlot> _dlyd = sp.DelayedTimeSlots(EDC);
                _rprt.CPRNumberNotShowingUp += _dlyd.Count();
                EDC.TimeSlot.DeleteAllOnSubmit(_dlyd);
                EDC.TimeSlot.DeleteAllOnSubmit(sp.OccupiedTimeSlots(EDC));
                if (sp.TrailerCondition.GetValueOrDefault(TrailerCondition.None) == TrailerCondition._1Unexceptable)
                {
                    _rprt.CPRNumberRejectedBadQuality++;
                }
                if (sp.ShippingState.Value == ShippingState.Completed)
                {
                    switch (CalculateDelay(sp.StartTime.Value - sp.TSStartTime.Value))
                    {
                    case Delay.JustInTime:
                        _rprt.CPRNumberOnTime++;
                        break;

                    case Delay.Delayed:
                        _rprt.CPRNumberDelayed++;
                        break;

                    case Delay.VeryLate:
                        _rprt.CPRNumberDelayed1h++;
                        break;
                    }
                }
                //TODO to be delated.
                try
                {
                    EDC.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    EDC.ResolveChangeConflicts(result);
                    EDC.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                result.AddException("MakePerformanceReport", ex);
            }
        }