//Methods
        #region SyncTrackingNumbersWithEbay
        public void SyncTrackingNumbersWithEbay()
        {
            try
            {
                var saleItemToSync = MyDataContext.Default.SaleItems
                                     .Include(runner => runner.Sale)
                                     .Include(runner => runner.Sale.Mailing)
                                     .Where(runner => runner.Sale.Source == SaleSources.Ebay)
                                     .Where(runner => runner.EbayOrderLineItemID != null && runner.EbayOrderLineItemID != String.Empty)
                                     .Where(runner => runner.Sale.MailingId.HasValue)
                                     .Where(runner => runner.Sale.Mailing.MustSyncTrackingNumber)
                                     .Where(runner => runner.Sale.Mailing.DateOfShipping.HasValue)
                                     .Where(runner => runner.Sale.Mailing.ShippingMethod != ShippingMethods.None)
                                     .Where(runner => runner.Sale.Mailing.ShippingMethod != ShippingMethods.Undecided)
                                     .Where(runner => runner.Sale.Mailing.TrackingNumber != null && runner.Sale.Mailing.TrackingNumber != String.Empty)
                                     .ToList();

                foreach (var runner in saleItemToSync)
                {
                    System.Diagnostics.Trace.WriteLine(String.Format("Syncing eBay sale {0} with tracking number {1}", runner.Id, runner.Sale.Mailing.TrackingNumber));
                    EbayController.SetShipmentTrackingInfo(
                        runner.EbayOrderLineItemID,
                        runner.Sale.IsPaid,
                        runner.Sale.Mailing.TrackingNumber,
                        runner.Sale.Mailing.ShippingMethod,
                        runner.Sale.Mailing.DateOfShipping.Value);
                }

                foreach (var runner in saleItemToSync.Select(runner => runner.Sale.Mailing))
                {
                    runner.MustSyncTrackingNumber = false;
                }
                MyDataContext.Default.SaveChanges();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(String.Format("Fatal failure of tracking number sync: {0}", ex.Message));
            }
        }
        //Methods
        #region SyncSingleArticle
        private void SyncSingleArticle(Article article, DateTime now)
        {
            try
            {
                //Total
                article.SyncDate  = now;
                article.SyncTotal = (Int32)article.AmountOnStock;

                //Ebay
                EbayStockInfo ebayStockInfo = EbayController.GetEbayStockInfo(article);
                article.SyncEbayActive     = ebayStockInfo.ActiveQuantity;
                article.SyncEbayAvailiable = ebayStockInfo.AvailiableQuantity;
                article.SyncEbayTemplate   = ebayStockInfo.TemplateAmount;

                //Magento
                Int32 magentoStockAmount = (Int32)article.AmountOnStock - ebayStockInfo.TotalStockQuantity;
                magentoStockAmount  = magentoStockAmount <= 0 ? 0 : magentoStockAmount;
                article.SyncMagento = magentoStockAmount;

                //Save
                MagentoController.SetAvailiableQuantity(article, magentoStockAmount);
                article.MustSyncStockAmount = false;
                MyDataContext.Default.SaveChanges();

                //Status
                if (ebayStockInfo.TotalStockQuantity + magentoStockAmount > article.AmountOnStock)
                {
                    article.SyncTechnicalInfo = StringTable.CautionStockExceeded;
                }
                else if (this.IsRunningLowByPerformance(article))
                {
                    article.SyncTechnicalInfo = StringTable.CautionAverageAmount;
                }
                else if (this.IsRunningLowOnEbay(article))
                {
                    article.SyncTechnicalInfo = StringTable.CautionStockLow;
                }
                else if (article.AmountOnStock < 0)
                {
                    article.SyncTechnicalInfo = StringTable.CautionStockOversold;
                }
                else if ((article.SyncEbayActive + article.SyncMagento) > article.AmountOnStock)
                {
                    article.SyncTechnicalInfo = StringTable.CautionToManyOffers;
                }
                else
                {
                    article.SyncTechnicalInfo = StringTable.Okay;
                }

                System.Diagnostics.Trace.WriteLine(String.Format(StringTable.SyncSuccess, article.Id));
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("===========");
                System.Diagnostics.Trace.WriteLine(ex.DeepParse());
                article.SyncTechnicalInfo = ex.DeepParse() + ex.StackTrace;
                System.Diagnostics.Trace.WriteLine(String.Format("Article: {0} - {1}", article == null ? "???" : article.Id.ToString(), ex.Message));
            }
            finally
            {
                MyDataContext.Default.SaveChanges();
            }
        }
        static void Main(String[] args)
        {
            MyDataContext.Default = new MyDataContext();

            try
            {
                ShopperConfiguration.Default = ShopperConfigurationReader.FromWebConfig();
                System.Diagnostics.Trace.Listeners.Add(new Models.Syncer.SyncerTraceListener());
                System.Diagnostics.Trace.Listeners.Add(new System.Diagnostics.ConsoleTraceListener());
                System.Diagnostics.Trace.AutoFlush = true;

                if (args.Contains(SyncProcessRemote.SyncTypes.Stock.ToString()))
                {
                    StockSyncer syncer = new StockSyncer();
                    syncer.PerformInMutex(() =>
                    {
                        System.Diagnostics.Trace.WriteLine("Stock sync started...");
                        syncer.SyncStock();
                        System.Diagnostics.Trace.WriteLine("Stock sync finished!");
                    });
                }

                if (args.Contains(SyncProcessRemote.SyncTypes.TrackingNumber.ToString()))
                {
                    TrackingNumberSyncer syncer = new TrackingNumberSyncer();
                    syncer.PerformInMutex(() =>
                    {
                        System.Diagnostics.Trace.WriteLine("TrackingNumber sync started...");
                        syncer.SyncTrackingNumbersWithEbay();
                        System.Diagnostics.Trace.WriteLine("TrackingNumber sync finished!");
                    });
                }

                if (args.Contains(SyncProcessRemote.SyncTypes.Sales.ToString()))
                {
                    SalesSyncer syncer = new SalesSyncer();
                    syncer.PerformInMutex(() =>
                    {
                        System.Diagnostics.Trace.WriteLine("Sales sync started...");

                        DateTime from  = DateTime.Now.Date.AddDays(ShopperConfiguration.Default.ImportDaysBack * -1);
                        DateTime until = DateTime.Now.Date.AddDays(1);
                        System.Diagnostics.Trace.WriteLine("Loading Magento transactions...");

                        try
                        {
                            var magentoItems  = MagentoController.LoadMagentoTransactions(from);
                            var magentoSyncer = new MagentoSalesSyncer();
                            magentoSyncer.Import(magentoItems);
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Trace.WriteLine("Magento could's not be synced: " + ex.Message);
                        }

                        try
                        {
                            System.Diagnostics.Trace.WriteLine("Loading ebay transactions...");
                            var ebayItems = EbayController.LoadEbayTransactions(from, until);
                            System.Diagnostics.Trace.WriteLine("Ebay transactions loaded!");

                            var ebaySyncer = new EbaySalesSyncer();
                            ebaySyncer.Import(ebayItems);
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Trace.WriteLine("Ebay could's not be synced: " + ex.Message);
                        }

                        System.Diagnostics.Trace.WriteLine("Sales sync finished!");
                    });
                }

                if (args.Contains(SyncProcessRemote.SyncTypes.Articles.ToString()))
                {
                    var products = EbayController.LoadEbaySellingManagerProducts();
                    EbayController.ReviseSellingManagerTemplates(products);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.DeepParse());
            }
        }