Example #1
0
        private void SynchronizeOrders()
        {
            LastUpdate = new DateTime(1983, 11, 20);

            if (dataHandler.QueryOrders().Count > 0)
            {
                LastUpdate = dataHandler.QueryOrders().OrderByDescending(i => i.Modified).Select(j => j.Modified.GetValueOrDefault()).First();
            }

            List <Order> ServerOrders = serviceHandler.CallService <List <Order> >(@"QueryOrders");

            if (ServerOrders != null)
            {
                List <Order> newOrders = ServerOrders.Where(i => i.Modified > LastUpdate).Select(j => j).ToList();

                foreach (var item in newOrders)
                {
                    List <OrderContentPackage>   tempOcPackage = serviceHandler.CallService <List <OrderContentPackage> >(@"QueryOrdersContentPackage/" + item.OrderId);
                    List <OrderContentChocolate> tempOcChoco   = serviceHandler.CallService <List <OrderContentChocolate> >(@"QueryOrdersContentChocolate/" + item.OrderId);
                    item.Content = new List <OrderContent>();
                    item.Content.AddRange(tempOcPackage);
                    item.Content.AddRange(tempOcChoco);

                    if (dataHandler.QueryOrders().Where(p => p.OrderId.Equals(item.OrderId)).Count() == 0)
                    {
                        SynchronizeCustomers();
                        dataHandler.InsertOrder(item);

                        foreach (var item2 in item.Content)
                        {
                            dataHandler.InsertOrderContent(item2, item.OrderId);
                        }
                        if (tempOcChoco.Count > 0)
                        {
                            foreach (var item3 in tempOcChoco)
                            {
                                dataHandler.InsertOcHasChoco(item3);
                            }
                        }
                        if (tempOcPackage.Count > 0)
                        {
                            foreach (var item4 in tempOcPackage)
                            {
                                dataHandler.InsertOcHasPackage(item4);
                            }
                        }


                        OrderInformer.Invoke();
                    }
                    else
                    {
                        dataHandler.UpdateOrder(item);

                        foreach (var item2 in item.Content)
                        {
                            dataHandler.UpdateOrderContent(item2, item.OrderId);
                        }
                        if (tempOcChoco.Count > 0)
                        {
                            foreach (var item3 in tempOcChoco)
                            {
                                dataHandler.UpdateOcHasChoco(item3);
                            }
                        }
                        if (tempOcPackage.Count > 0)
                        {
                            foreach (var item4 in tempOcPackage)
                            {
                                dataHandler.UpdateOcHasPackage(item4);
                            }
                        }
                        OrderInformer.Invoke();
                    }
                }
            }
        }