示例#1
0
        public static bool PerformCalculation(Purchase purchase, CalculationTargetProperty targetProperty)
        {
            logger.SendEmail = SettingsService.GetInstance().GetSetting(SettingsService.SEND_EMAIL_AUTO_KEY).SettingBoolValue;

            bool result = true;
            try
            {
                switch (targetProperty)
                {
                    case CalculationTargetProperty.ItemCost:
                        purchase.ItemCost = purchase.TotalCost / purchase.ItemsNumber;
                        logger.Debug("Calculating item cost");
                        break;
                    case CalculationTargetProperty.ItemsNumber:
                        purchase.ItemsNumber = purchase.TotalCost / purchase.ItemCost;
                        logger.Debug("Calculating items number");
                        break;
                    case CalculationTargetProperty.TotalCost:
                        purchase.TotalCost = purchase.ItemCost * purchase.ItemsNumber;
                        logger.Debug("Calculating total cost");
                        break;
                    default:
                        break;
                }
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }
        public AddDataViewModel()
        {
            MsgDispatcher.AddHandler(HandleMessage);
            purchase = new Purchase();

            AddCommand("Save", new DelegateCommand(SaveCommandExecute, SaveCommandCanExecute));

            Status.Post("Все готово для роботи!");
        }
示例#3
0
 public Purchase(Purchase purchase)
 {
     Id = purchase.Id;
     Name = purchase.Name;
     Date = purchase.Date;
     ItemCost = purchase.ItemCost;
     ItemsNumber = purchase.ItemsNumber;
     TotalCost = purchase.TotalCost;
     Type = purchase.Type;
     SubType = purchase.SubType;
     IsMonthly = purchase.IsMonthly;
     PurchaseRate = purchase.PurchaseRate;
     PurchaseComment = purchase.PurchaseComment;
     StoreName = purchase.StoreName;
     StoreRate = purchase.StoreRate;
     StoreComment = purchase.StoreComment;
     StoreId = purchase.StoreId;
 }
示例#4
0
 private PurchaseModel PurchaseToModel(Purchase purchase, long productId, long storeId)
 {
     return new PurchaseModel
     {
         Id = purchase.Id,
         ProductId = productId,
         ProductName = purchase.Name,
         StoreId = storeId,
         Timestamp = purchase.Date.Ticks,
         ItemsNumber = purchase.ItemsNumber,
         ItemCost = purchase.ItemCost,
         TotalCost = purchase.TotalCost,
         Rate = purchase.PurchaseRate
     };
 }
示例#5
0
 private ProductModel ProductToModel(Purchase purchase)
 {
     return new ProductModel
     {
         Name = StringUtilities.EscapeStringForDatabase(purchase.Name),
         Type = TypeToModel(purchase.Type),
         SubType = SubTypeToModel(purchase.SubType),
         IsMonthly = purchase.IsMonthly
     };
 }
示例#6
0
 public async Task<bool> DeletePurchase(Purchase purchase)
 {
     if (await DBService.DeletePurchase(purchase.Id).ConfigureAwait(false))
     {
         Status.Post("Запис \"{0}\" видалено", purchase.Name);
         return true;
     }
     return false;
 }
示例#7
0
        public async Task<Purchase> AddPurchase(Purchase purchase)
        {
            //Product always has name
            var productId = await DBService.SaveProduct(ProductToModel(purchase)).ConfigureAwait(false);
            if (productId < 1)
            {
                logger.Error("AddPurchase: Error occured during product saving: {0}", purchase.Name);
                return null;
            }
            
            if (!string.IsNullOrEmpty(purchase.StoreName))
            {
                purchase.StoreId = await DBService.SaveStore(
                    new StoreModel 
                    {
                        Id = purchase.StoreId,
                        Name = StringUtilities.EscapeStringForDatabase(purchase.StoreName)
                    }).ConfigureAwait(false);
                if (purchase.StoreId < 1)
                {
                    logger.Error("AddPurchase: Error occured during store saving: {0}", purchase.Name);
                    return null;
                }
            }

            var purchaseId = await DBService.SavePurchase(PurchaseToModel(purchase, productId, purchase.StoreId)).ConfigureAwait(false);
            if (purchaseId > 0)
            {
                purchaseHistory.Add(new Purchase(purchase));
                AnnounceHistoryUpdate();
            }
            else
            {
                return null;
        }
            if (!string.IsNullOrEmpty(purchase.PurchaseComment))
        {
                long commentId = await DBService.SaveComment(
                    new CommentModel
            {
                        PurchaseId = purchaseId,
                        StoreId = purchase.StoreId,
                        Text = StringUtilities.EscapeStringForDatabase(purchase.PurchaseComment),
                        Rate = purchase.PurchaseRate
                    }).ConfigureAwait(false);
                if (commentId < 1)
                {
                    logger.Error("AddPurchase: Error occured during comment saving: {0}", purchase.Name);
                    return null;
            }
        }
            //Store comment not binded to purchase
            if (!string.IsNullOrEmpty(purchase.StoreComment))
        {
                long storeCommentId = await DBService.SaveComment(
                    new CommentModel
                    {
                        PurchaseId = purchase.Id,
                        StoreId = purchase.StoreId,
                        Text = StringUtilities.EscapeStringForDatabase(purchase.StoreComment),
                        Rate = purchase.StoreRate
                    }).ConfigureAwait(false);
                if (storeCommentId < 1)
            {
                    logger.Error("AddPurchase: Error occured during store comment saving: {0}", purchase.Name);
                    return null;
                }
            }

            purchase.Id = purchaseId;

            return purchase;
        }
        private bool RecalculatePurchase(Purchase purchase, Purchase referencePurchase)
        {
            if (ShowDataCalcPopup)
            {
                return false;
            }
            bool calculationNeeded = false;
            if (purchase.ItemCost != referencePurchase.ItemCost)
            {
                ShowCalcItemCost = false;
                calculationNeeded = true;
            }
            else if (purchase.ItemsNumber != referencePurchase.ItemsNumber)
            {
                ShowCalcItemNumber = false;
                calculationNeeded = true;
            }
            else if (purchase.TotalCost != referencePurchase.TotalCost)
            {
                ShowCalcTotalCost = false;
                calculationNeeded = true;
            }
            editingPurchase = purchase;
            ShowDataCalcPopup = calculationNeeded;

            return calculationNeeded;
        }
示例#9
0
        public void DatabaseStateTest()
        {
            StoreService = StorageService.GetInstance();

            var purchase = new Purchase
            {
                Date = DateTime.Now,
                Id = 0,
                ItemCost = 10.4,
                ItemsNumber = 135.2,
                TotalCost = 1406.08,
                IsMonthly = true,
                Name = "Cote d'Azure apple",
                PurchaseComment = "Воно чудо'ве",
                PurchaseRate = 10,
                StoreComment = "Цікави'й магазин",
                StoreName = "Нову'с магазин",
                StoreRate = 5,
                Type = new ProductType { Name = "Їжа", Id = 0 },
                SubType = new ProductSubType { Name = "Фрукт", Id = 0 }
            };
            var purchase2 = new Purchase
            {
                Date = DateTime.Now,
                Id = 0,
                ItemCost = 17.5,
                ItemsNumber = 544,
                TotalCost = 9520,
                IsMonthly = false,
                Name = "Ozone Mantra M6",
                PurchaseComment = "Це палапрал",
                PurchaseRate = 8,
                StoreComment = "Завжди тут беру",
                StoreName = "Від Перевалова",
                StoreRate = 7,
                Type = new ProductType { Name = "Спорядження", Id = 0 },
                SubType = new ProductSubType { Name = "Льотне спорядження", Id = 0 }
            };
            var purchase3 = new Purchase
            {
                Date = DateTime.Now,
                Id = 0,
                ItemCost = 234,
                ItemsNumber = 53,
                TotalCost = 12402,
                IsMonthly = true,
                Name = "Помаранч",
                PurchaseComment = "Нічотак",
                PurchaseRate = 9,
                StoreComment = "Зайшов після роботи",
                StoreName = "Фора",
                StoreRate = 6,
                Type = new ProductType { Name = "Їжа", Id = 0 },
                SubType = new ProductSubType { Name = "Фрукт", Id = 0 }
            };
            var purchase4 = new Purchase
            {
                Date = DateTime.Now,
                Id = 0,
                ItemCost = 1,
                ItemsNumber = 2,
                TotalCost = 2,
                IsMonthly = false,
                Name = "Груша",
                Type = new ProductType { Name = "Їжа", Id = 0 }
            };
            //Cleanup
            Assert.IsTrue(StoreService.DeletePurchase(purchase.Name).Result, "Cleanup failed");
            Assert.IsTrue(StoreService.DeletePurchase(purchase2.Name).Result, "Cleanup failed");
            Assert.IsTrue(StoreService.DeletePurchase(purchase3.Name).Result, "Cleanup failed");
            Assert.IsTrue(StoreService.DeletePurchase(purchase4.Name).Result, "Cleanup failed");

            //Save test
            purchase = StoreService.AddPurchase(purchase).Result;
            Assert.IsTrue(purchase != null, "Purchase saving error");
            purchase2 = StoreService.AddPurchase(purchase2).Result;
            Assert.IsTrue(purchase != null, "Purchase saving error");
            purchase3 = StoreService.AddPurchase(purchase3).Result;
            Assert.IsTrue(purchase != null, "Purchase saving error");
            purchase4 = StoreService.AddPurchase(purchase4).Result;
            Assert.IsTrue(purchase != null, "Purchase saving error");
            //read test

            var searchRequest = new SearchRequestModel
            {
                Name = purchase.Name,
                SearchByName = true
            };
            var searchRequest2 = new SearchRequestModel
            {
                Name = purchase2.Name,
                SearchByName = true
            };
            var searchRequest3 = new SearchRequestModel
            {
                Name = purchase3.Name,
                SearchByName = true
            };

            var searchRequest4 = new SearchRequestModel
            {
                Name = purchase4.Name,
                SearchByName = true
            };

            var foundList = StoreService.LoadPurchaseList(searchRequest).Result;
            Assert.IsTrue(foundList.Count == 1, "Purchase storage error: records duplicating found");

            var controlPurchase = foundList.FirstOrDefault();
            Assert.IsTrue(controlPurchase.DeepEquals(purchase), "Purchase storage error: not consistent");

            var foundList2 = StoreService.LoadPurchaseList(searchRequest2).Result;
            Assert.IsTrue(foundList2.Count == 1, "Purchase storage error: records duplicating found");

            var controlPurchase2 = foundList2.FirstOrDefault();
            Assert.IsTrue(controlPurchase2.DeepEquals(purchase2), "Purchase storage error: not consistent");

            var foundList3 = StoreService.LoadPurchaseList(searchRequest3).Result;
            Assert.IsTrue(foundList3.Count == 1, "Purchase storage error: records duplicating found");

            var controlPurchase3 = foundList3.FirstOrDefault();
            Assert.IsTrue(controlPurchase3.DeepEquals(purchase3), "Purchase storage error: not consistent");

            var foundList4 = StoreService.LoadPurchaseList(searchRequest4).Result;
            Assert.IsTrue(foundList4.Count == 1, "Purchase storage error: records duplicating found");

            var controlPurchase4 = foundList4.FirstOrDefault();
            Assert.IsTrue(controlPurchase4.DeepEquals(purchase4), "Purchase storage error: not consistent");

            //update test
            purchase.IsMonthly = false;
            purchase.Name = "Звичайне яблуко";

            purchase = StoreService.AddPurchase(purchase).Result;
            Assert.IsTrue(purchase != null, "Purchase saving error");
            //repeated read test
            var controlPurchase_repeat = StoreService.LoadPurchase(controlPurchase.Id).Result;
            Assert.IsTrue(controlPurchase_repeat.DeepEquals(purchase), "Purchase storage error: not consistent");

            //delete test
            Assert.IsTrue(StoreService.DeletePurchase(purchase).Result, "Purchase deletion error");

            var deletedPurchase = StoreService.LoadPurchase(purchase.Id).Result;
            Assert.IsTrue (deletedPurchase == null, "Purchase deletion error: not deleted");
        }
示例#10
0
        private void SaveCommandExecute(object obj)
        {
            purchase.Name = purchase.Name.Trim();

            Task.Factory.StartNew(async () =>
            {
                var pName = purchase.Name;
                var result = await StoreService.AddPurchase(purchase);
                if (result != null)
                {
                    int tId = Thread.CurrentThread.ManagedThreadId;
                    logger.Info("Purchase saved: {0}", pName);
                    Status.Post("Покупка \"{0}\" збережена", pName);

                    purchase = new Purchase
                    {
                        Type = purchase.Type,
                        SubType = purchase.SubType
                    };

                    CleanInputFields();
                }
                else
                {
                    logger.Warn("Purchase not saved: {0}", pName);
                    Status.Post("Помилка: покупка \"{0}\" не збережена", pName);
                }
            });
        }