예제 #1
0
        private void DeletePerson <Type>(DbSet <Type> people, string userId) where Type : Person
        {
            var person = people.FirstOrDefault(p => p.SystemUser.Id == userId);
            var entry  = context.Entry <Type>(person);

            entry.State = EntityState.Deleted;
            context.SaveChanges();
        }
예제 #2
0
        public void Should_Add_User()
        {
            // given
            var department = new Department {
                Name = "Oddział Testowy"
            };

            context.Departments.Add(department);
            context.SaveChanges();
            var newUser = new UserViewModel
            {
                DepartmentID    = department.ID,
                Email           = "*****@*****.**",
                ConfirmPassword = "******",
                Password        = "******",
                Role            = UserRoleTypes.Admin,
                FirstName       = "Testowy",
                SecondName      = "User",
                PhoneNumber     = "555666777",
                UserName        = "******"
            };
            var expectedUserCount = context.Admins.Count() + 1;

            // when
            useCase.AddNewUser(newUser);

            // then
            Assert.AreEqual(expectedUserCount, context.Admins.Count());
            Assert.IsNotNull(context.Admins.FirstOrDefault(a => a.FirstName == newUser.FirstName));
        }
예제 #3
0
        public void SetUp()
        {
            MapperDependencyResolver.Resolve();
            context = new DrugstoreDbContext(options);
            #region Data seed
            var stockMed = new MedicineOnStock
            {
                MedicineCategory = MedicineCategory.Special,
                PricePerOne      = 30,
                Quantity         = 50,
                Refundation      = 0.20,
                Name             = "Lek testowy"
            };
            var doctor = new Doctor
            {
                FirstName  = "Testowy",
                SecondName = "Lekarz"
            };
            var patient = new Patient
            {
                FirstName  = "Testowy",
                SecondName = "Pacjent"
            };

            context.Doctors.Add(doctor);

            context.Patients.Add(patient);

            context.Medicines.Add(stockMed);

            context.SaveChanges();
            #endregion
        }
        private ResultViewModel <Dictionary <string, object> > UpdateStore(XmlMedicineSupplyModel supply)
        {
            double pricePerOne;
            double totalMedicinesCost = 0.0f;
            int    totalMedicineCount = 0;

            var medicines = context.ExternalDrugstoreMedicines;

            foreach (var med in supply.Medicines)
            {
                if (med.StockId.HasValue)
                {
                    var medicine = medicines
                                   .Include(m => m.StockMedicine)
                                   .SingleOrDefault(m => m.StockMedicine.ID == med.StockId);

                    if (medicine == null)
                    {
                        throw new MedicineNotFoundException($"Id == {med.StockId}");
                    }

                    if ((int)((int)medicine.Quantity - (int)med.Quantity) < 0)
                    {
                        throw new OnStockMedicineQuantityException($"External Drugstore, " +
                                                                   $"ID: {med.StockId} Available quantity: {medicine.Quantity} Sold Quantity: {med.Quantity}");
                    }

                    medicine.Quantity -= med.Quantity;

                    pricePerOne = med.PricePerOne ?? medicine.StockMedicine.PricePerOne;

                    context.ExternalDrugstoreSoldMedicines.Add(new ExternalDrugstoreSoldMedicine
                    {
                        Date          = DateTime.Now,
                        SoldQuantity  = (int)med.Quantity,
                        StockMedicine = medicine.StockMedicine,
                        PricePerOne   = pricePerOne
                    });


                    totalMedicinesCost += med.Quantity * pricePerOne;
                    totalMedicineCount += (int)med.Quantity;
                }
                else
                {
                    throw new MedicineNotFoundException("Medicine's Id is null");
                }
            }
            context.SaveChanges();

            return(new ResultViewModel <Dictionary <string, object> >
            {
                Succes = true,
                Data = new Dictionary <string, object> {
                    { "TotalCount", totalMedicineCount },
                    { "TotalCost", totalMedicinesCost }
                },
                Message = ""
            });
        }
예제 #5
0
        public ResultViewModel Execute(int doctorId, int prescriptionId)
        {
            ResultViewModel result = new ResultViewModel();

            try
            {
                var prescription = context.MedicalPrescriptions
                                   .Include(p => p.Medicines)
                                   .Where(p => p.Doctor.ID == doctorId)
                                   .FirstOrDefault(p => p.ID == prescriptionId);

                var medicines = prescription.Medicines.ToList();

                foreach (var medicine in medicines)
                {
                    context.AssignedMedicines.Remove(medicine);
                }

                context.MedicalPrescriptions.Remove(prescription);
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                result.Message = ex.Message;
                result.Succes  = false;
            }

            return(result);
        }
예제 #6
0
 public IActionResult AddDepartment(Department department)
 {
     if (ModelState.IsValid)
     {
         if (drugstore.Departments
             .Any(d => d.Name.Contains(department.Name, StringComparison.OrdinalIgnoreCase)))
         {
             ModelState.AddModelError(nameof(department.Name), "Oddział o podanej nazwie już istnieje");
         }
         else
         {
             drugstore.Departments.Add(department);
             drugstore.SaveChanges();
             return(RedirectToAction("Departments"));
         }
     }
     return(View(department));
 }
예제 #7
0
        public void SetUp()
        {
            MapperDependencyResolver.Resolve();
            context = new DrugstoreDbContext(options);
            #region Data seed
            var stockMed = new MedicineOnStock
            {
                MedicineCategory = MedicineCategory.Special,
                PricePerOne      = 30,
                Quantity         = 50,
                Refundation      = 0.20,
                Name             = "Lek testowy"
            };
            var doctor = new Doctor
            {
                FirstName  = "Testowy",
                SecondName = "Lekarz"
            };
            var patient = new Patient
            {
                FirstName  = "Testowy",
                SecondName = "Pacjent"
            };
            var prescription = new MedicalPrescription
            {
                CreationTime      = DateTime.Now,
                Doctor            = doctor,
                Patient           = patient,
                VerificationState = VerificationState.NotVerified,
                Medicines         = new List <AssignedMedicine> {
                    new AssignedMedicine {
                        StockMedicine    = stockMed,
                        PricePerOne      = stockMed.PricePerOne * (1 - stockMed.Refundation),
                        AssignedQuantity = 10
                    }
                }
            };

            context.Doctors.Add(doctor);

            context.Patients.Add(patient);

            context.Medicines.Add(stockMed);

            context.MedicalPrescriptions.Add(prescription);
            context.SaveChanges();
            #endregion
        }
예제 #8
0
        public ResultViewModel Execute(MedicineViewModel [] medicines, int prescriptionId, int doctorId)
        {
            var result = new ResultViewModel();

            try
            {
                if (!medicines.Any())
                {
                    throw new Exception("Empty medicine list");
                }

                var prescription = context.MedicalPrescriptions
                                   .Include(p => p.Medicines)
                                   .Include(p => p.Doctor)
                                   .Where(p => p.VerificationState != VerificationState.Accepted)
                                   .Where(p => p.Doctor.ID == doctorId)
                                   .Single(p => p.ID == prescriptionId);

                prescription.CreationTime = DateTime.Now;

                var meds = prescription.Medicines;

                foreach (var m in meds)
                {
                    context.AssignedMedicines.Remove(m);
                }

                foreach (var m in medicines)
                {
                    var stockMedicine = context.Medicines.Single(med => med.ID == m.StockId);
                    var assignedMed   = AutoMapper.Mapper.Map <AssignedMedicine>(m);
                    assignedMed.StockMedicine = stockMedicine;

                    prescription.Medicines.Add(assignedMed);
                }

                context.SaveChanges();
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                result.Message = ex.Message;
                result.Succes  = false;
            }

            return(result);
        }
        public ResultViewModel Execute(DoctorPrescriptionViewModel prescription, int doctorId)
        {
            var result = new ResultViewModel();

            try
            {
                if (!prescription.Medicines.Any())
                {
                    throw new Exception("Empty medicine list");
                }

                var doctor  = context.Doctors.First(d => d.ID == doctorId);
                var patient = context.Patients.First(p => p.ID == prescription.Patient.Id);

                var assignedMedicines = prescription.Medicines.Select(m =>
                {
                    var med           = AutoMapper.Mapper.Map <AssignedMedicine>(m);
                    med.StockMedicine = context.Medicines
                                        .First(c => c.ID == m.StockId);

                    med.PricePerOne = m.PricePerOne * (1 - m.Refundation);

                    return(med);
                }).ToList();

                doctor.IssuedPresciptions.Add(new MedicalPrescription
                {
                    Medicines         = assignedMedicines,
                    Doctor            = doctor,
                    Patient           = patient,
                    CreationTime      = DateTime.Now,
                    VerificationState = VerificationState.NotVerified
                });
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                result.Succes  = false;
                result.Message = ex.Message;
            }

            return(result);
        }
        public ResultViewModel Execute(int prescriptionId)
        {
            ResultViewModel result = new ResultViewModel();

            try
            {
                var prescription = context.MedicalPrescriptions.Single(p => p.ID == prescriptionId);
                prescription.VerificationState = VerificationState.Rejected;
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                result.Succes  = false;
                result.Message = ex.Message;
            }

            return(result);
        }
예제 #11
0
        public void Should_Not_Add_Medicines_To_Sold_And_Substract_From_External_Drugstore()
        {
            // given
            var expectedSoldAmount = context.ExternalDrugstoreSoldMedicines.Sum(ex => ex.SoldQuantity);
            var fileFormMock       = new Mock <IFormFile>();

            fileFormMock.Setup(f => f.ContentType).Returns("text/xml");
            var fileCopyMock   = new Mock <ICopy>();
            var loggerMock     = new Mock <ILogger <UploadSoldMedicinesListUseCase> >();
            var serializerMock = new Mock <ISerializer <MemoryStream, XmlMedicineSupplyModel> >();

            serializerMock.Setup(s => s.Deserialize(It.IsAny <MemoryStream>())).Returns(supply);

            var useCase = new UploadSoldMedicinesListUseCase(
                context,
                loggerMock.Object,
                serializerMock.Object,
                fileCopyMock.Object);

            supply.Medicines.First().Quantity = 100;
            context.SaveChanges();

            // when
            var result = useCase.Execute(fileFormMock.Object);

            // then
            int expectedAdditionalSoldMeds = 0;
            var expectedQuantity           = initialQuantity;

            supply.Medicines.ForEach(m =>
            {
                expectedAdditionalSoldMeds += (int)m.Quantity;

                var actualQuantity = context.ExternalDrugstoreMedicines
                                     .First(ex => ex.StockMedicine.ID == m.StockId).Quantity;
                Assert.AreEqual(expectedQuantity, actualQuantity);
            });

            int actualAmount = context.ExternalDrugstoreSoldMedicines.Sum(ex => ex.SoldQuantity);

            Assert.AreEqual(expectedSoldAmount, actualAmount);
        }
예제 #12
0
        public void AddPatient(UserViewModel newUser)
        {
            var systemUser = new SystemUser
            {
                UserName    = newUser.UserName,
                Email       = newUser.Email,
                PhoneNumber = newUser.PhoneNumber
            };
            var passHash = userManager.PasswordHasher.HashPassword(systemUser, newUser.Password);

            systemUser.PasswordHash = passHash;
            userManager.CreateAsync(systemUser).Wait();

            var role = UserRoleTypes.Patient.ToString();

            userManager.AddToRoleAsync(systemUser, role).Wait();

            var person = new Core.Patient();

            SetPersonProperties(person, newUser, systemUser);
            context.Patients.Add(person);
            context.SaveChanges();
        }
예제 #13
0
        public void Should_Not_Delete_Prescription()
        {
            // given
            var expectedResult            = false;
            var expectedPrescriptionCount = context.MedicalPrescriptions.Count();
            var loggerMock = new Mock <ILogger <DeletePrescriptionUseCase> >();
            var useCase    = new DeletePrescriptionUseCase(context, loggerMock.Object);
            var doctor     = context.Doctors.First();
            var presc      = context.MedicalPrescriptions.First();
            var uselessDoc = new Doctor {
                FirstName = "Useless", SecondName = "Doc"
            };

            context.Doctors.Add(uselessDoc);
            context.SaveChanges();

            // when
            var actualResult = useCase.Execute(uselessDoc.ID, presc.ID);

            // then
            Assert.AreEqual(expectedResult, actualResult.Succes);
            Assert.AreEqual(expectedPrescriptionCount, context.MedicalPrescriptions.Count());
        }
        public ResultViewModel Execute(int prescriptionId)
        {
            ResultViewModel result = new ResultViewModel();
            int             newQuantity;
            var             prescription = context.MedicalPrescriptions
                                           .Include(p => p.Medicines).ThenInclude(p => p.StockMedicine)
                                           .Single(p => p.ID == prescriptionId);

            try
            {
                foreach (var med in prescription.Medicines)
                {
                    var stockMedicine = context.Medicines
                                        .Single(m => m.ID == med.StockMedicine.ID);
                    newQuantity = (int)stockMedicine.Quantity - (int)med.AssignedQuantity;
                    if (newQuantity > 0)
                    {
                        stockMedicine.Quantity = (uint)newQuantity;
                    }
                    else
                    {
                        throw new Exception($"Lek {stockMedicine.Name} niedostępny.");
                    }
                }

                prescription.VerificationState = VerificationState.Accepted;
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                result.Message = ex.Message;
                result.Succes  = false;
            }

            return(result);
        }
예제 #15
0
        public void SetUp()
        {
            MapperDependencyResolver.Resolve();
            context = new DrugstoreDbContext(options);
            var med1 = new Core.MedicineOnStock
            {
                MedicineCategory = Core.MedicineCategory.Normal,
                Name             = "F;irst Medicine",
                PricePerOne      = 25.66,
                Quantity         = 100,
                Refundation      = 0.2
            };
            var med2 = new Core.MedicineOnStock
            {
                MedicineCategory = Core.MedicineCategory.Normal,
                Name             = "Second Medicine",
                PricePerOne      = 6.66,
                Quantity         = 100,
                Refundation      = 0.1
            };
            var med3 = new Core.MedicineOnStock
            {
                MedicineCategory = Core.MedicineCategory.Normal,
                Name             = "Third Medicine",
                PricePerOne      = 22.66,
                Quantity         = 100,
                Refundation      = 0.25
            };

            context.Medicines
            .AddRange(new Core.MedicineOnStock [] { med1, med2, med3 });
            context.SaveChanges();

            var exMed1 = new Core.ExternalDrugstoreMedicine
            {
                StockMedicine = med1,
                Name          = med1.Name,
                PricePerOne   = med1.PricePerOne,
                Quantity      = initialQuantity,
            };
            var exMed2 = new Core.ExternalDrugstoreMedicine
            {
                StockMedicine = med2,
                Name          = med2.Name,
                PricePerOne   = med2.PricePerOne,
                Quantity      = initialQuantity,
            };
            var exMed3 = new Core.ExternalDrugstoreMedicine
            {
                StockMedicine = med3,
                Name          = med3.Name,
                PricePerOne   = med3.PricePerOne,
                Quantity      = initialQuantity,
            };

            context.ExternalDrugstoreMedicines
            .AddRange(new Core.ExternalDrugstoreMedicine [] { exMed1, exMed2, exMed3 });
            context.SaveChanges();

            var sold1 = new ExternalDrugstoreSoldMedicine
            {
                StockMedicine = med1,
                PricePerOne   = med1.PricePerOne,
                Date          = DateTime.Now,
                SoldQuantity  = 10
            };
            var sold2 = new ExternalDrugstoreSoldMedicine
            {
                StockMedicine = med2,
                PricePerOne   = med2.PricePerOne,
                Date          = DateTime.Now,
                SoldQuantity  = 10
            };
            var sold3 = new ExternalDrugstoreSoldMedicine
            {
                StockMedicine = med3,
                PricePerOne   = med3.PricePerOne,
                Date          = DateTime.Now,
                SoldQuantity  = 10
            };

            context.ExternalDrugstoreSoldMedicines
            .AddRange(new ExternalDrugstoreSoldMedicine [] { sold1, sold2, sold3 });
            context.SaveChanges();

            supply = new XmlMedicineSupplyModel
            {
                Medicines = new List <XmlMedicineModel>
                {
                    new XmlMedicineModel
                    {
                        StockId  = med1.ID,
                        Quantity = 10,
                    },
                    new XmlMedicineModel
                    {
                        StockId  = med2.ID,
                        Quantity = 10,
                    },
                    new XmlMedicineModel
                    {
                        StockId  = med3.ID,
                        Quantity = 20,
                    }
                }
            };
        }
예제 #16
0
        public void SetUp()
        {
            MapperDependencyResolver.Resolve();
            context = new DrugstoreDbContext(options);
            #region Data seed
            var stockMedOne = new MedicineOnStock
            {
                MedicineCategory = MedicineCategory.Special,
                PricePerOne      = 30,
                Quantity         = 50,
                Refundation      = 0.20,
                Name             = "Lek testowy"
            };
            var stockMedTwo = new MedicineOnStock
            {
                MedicineCategory = MedicineCategory.Normal,
                PricePerOne      = 20,
                Quantity         = 100,
                Refundation      = 0.60,
                Name             = "Voltaren"
            };

            var doctor = new Doctor
            {
                FirstName  = "Testowy",
                SecondName = "Lekarz"
            };

            var patientOne = new Patient
            {
                FirstName  = "Pacjentka",
                SecondName = "One"
            };

            var patientTwo = new Patient
            {
                FirstName  = "Pacjent",
                SecondName = "Two"
            };
            var prescriptions = new MedicalPrescription []
            {
                new MedicalPrescription {
                    CreationTime      = DateTime.Parse("2019/01/20"),
                    Doctor            = doctor,
                    Patient           = patientOne,
                    VerificationState = VerificationState.NotVerified,
                    Medicines         = new List <AssignedMedicine> {
                        new AssignedMedicine {
                            StockMedicine    = stockMedOne,
                            PricePerOne      = stockMedOne.PricePerOne * (1 - stockMedOne.Refundation),
                            AssignedQuantity = 10
                        }
                    }
                },
                new MedicalPrescription
                {
                    CreationTime      = DateTime.Parse("2019/01/22"),
                    Doctor            = doctor,
                    Patient           = patientOne,
                    VerificationState = VerificationState.NotVerified,
                    Medicines         = new List <AssignedMedicine> {
                        new AssignedMedicine {
                            StockMedicine    = stockMedTwo,
                            PricePerOne      = stockMedTwo.PricePerOne * (1 - stockMedTwo.Refundation),
                            AssignedQuantity = 10
                        }
                    }
                },

                new MedicalPrescription
                {
                    CreationTime      = DateTime.Parse("2019/01/28"),
                    Doctor            = doctor,
                    Patient           = patientTwo,
                    VerificationState = VerificationState.Accepted,
                    Medicines         = new List <AssignedMedicine> {
                        new AssignedMedicine {
                            StockMedicine    = stockMedOne,
                            PricePerOne      = stockMedOne.PricePerOne * (1 - stockMedOne.Refundation),
                            AssignedQuantity = 10
                        }
                    }
                },
                new MedicalPrescription
                {
                    CreationTime      = DateTime.Parse("2019/01/30"),
                    Doctor            = doctor,
                    Patient           = patientTwo,
                    VerificationState = VerificationState.Accepted,
                    Medicines         = new List <AssignedMedicine> {
                        new AssignedMedicine {
                            StockMedicine    = stockMedTwo,
                            PricePerOne      = stockMedTwo.PricePerOne * (1 - stockMedTwo.Refundation),
                            AssignedQuantity = 10
                        }
                    }
                }
            };

            context.Doctors.Add(doctor);

            context.Patients.Add(patientTwo);

            context.Medicines.Add(stockMedOne);
            context.Medicines.Add(stockMedTwo);

            context.MedicalPrescriptions.AddRange(prescriptions);

            context.SaveChanges();
            #endregion
        }
예제 #17
0
        public void SetUp()
        {
            //dateProvider.AddDays(-1);
            MapperDependencyResolver.Resolve();
            context = new DrugstoreDbContext(options);
            var med1 = new Core.MedicineOnStock
            {
                MedicineCategory = Core.MedicineCategory.Normal,
                Name             = "First Medicine",
                PricePerOne      = 25.66,
                Quantity         = 100,
                Refundation      = 0.2
            };
            var med2 = new Core.MedicineOnStock
            {
                MedicineCategory = Core.MedicineCategory.Normal,
                Name             = "Second Medicine",
                PricePerOne      = 6.66,
                Quantity         = 100,
                Refundation      = 0.1
            };
            var med3 = new Core.MedicineOnStock
            {
                MedicineCategory = Core.MedicineCategory.Normal,
                Name             = "Third Medicine",
                PricePerOne      = 22.66,
                Quantity         = 100,
                Refundation      = 0.25
            };
            var med4 = new Core.MedicineOnStock
            {
                MedicineCategory = Core.MedicineCategory.Normal,
                Name             = "fourth Medicine",
                PricePerOne      = 25.66,
                Quantity         = 40,
                Refundation      = 0.2
            };
            var med5 = new Core.MedicineOnStock
            {
                MedicineCategory = Core.MedicineCategory.Normal,
                Name             = "fifth Medicine",
                PricePerOne      = 26.66,
                Quantity         = 10,
                Refundation      = 0.1
            };
            var med6 = new Core.MedicineOnStock
            {
                MedicineCategory = Core.MedicineCategory.Normal,
                Name             = "sixth Medicine",
                PricePerOne      = 24.66,
                Quantity         = 100,
                Refundation      = 0.25
            };

            context.Medicines
            .AddRange(new Core.MedicineOnStock [] { med1, med2, med3, med4, med5, med6 });
            context.SaveChanges();

            var exMed1 = new Core.ExternalDrugstoreMedicine
            {
                StockMedicine = med1,
                Name          = med1.Name,
                PricePerOne   = med1.PricePerOne,
                Quantity      = 50,
            };
            var exMed2 = new Core.ExternalDrugstoreMedicine
            {
                StockMedicine = med2,
                Name          = med2.Name,
                PricePerOne   = med2.PricePerOne,
                Quantity      = 50,
            };
            var exMed3 = new Core.ExternalDrugstoreMedicine
            {
                StockMedicine = med3,
                Name          = med3.Name,
                PricePerOne   = med3.PricePerOne,
                Quantity      = 50,
            };
            var exMed4 = new Core.ExternalDrugstoreMedicine
            {
                StockMedicine = med4,
                Name          = med4.Name,
                PricePerOne   = med4.PricePerOne,
                Quantity      = 50,
            };
            var exMed5 = new Core.ExternalDrugstoreMedicine
            {
                StockMedicine = med5,
                Name          = med5.Name,
                PricePerOne   = med5.PricePerOne,
                Quantity      = 50,
            };
            var exMed6 = new Core.ExternalDrugstoreMedicine
            {
                StockMedicine = med6,
                Name          = med6.Name,
                PricePerOne   = med6.PricePerOne,
                Quantity      = 50,
            };

            context.ExternalDrugstoreMedicines
            .AddRange(new Core.ExternalDrugstoreMedicine [] { exMed1, exMed2, exMed3, exMed4, exMed5, exMed6 });
            context.SaveChanges();
            //dodane dzisiaj
            var sold1 = new ExternalDrugstoreSoldMedicine
            {
                StockMedicine = med1,
                PricePerOne   = med1.PricePerOne,
                Date          = dateProvider,
                SoldQuantity  = 5
            };
            var sold2 = new ExternalDrugstoreSoldMedicine
            {
                StockMedicine = med2,
                PricePerOne   = med2.PricePerOne,
                Date          = dateProvider,
                SoldQuantity  = 20
            };
            var sold3 = new ExternalDrugstoreSoldMedicine
            {
                StockMedicine = med3,
                PricePerOne   = med3.PricePerOne,
                Date          = dateProvider,
                SoldQuantity  = 1
            };

            dateProvider = DateTime.Today.AddDays(-1); //wczoraj
            var sold4 = new ExternalDrugstoreSoldMedicine
            {
                StockMedicine = med1,
                PricePerOne   = med1.PricePerOne,
                Date          = dateProvider,
                SoldQuantity  = 2
            };
            var sold5 = new ExternalDrugstoreSoldMedicine
            {
                StockMedicine = med2,
                PricePerOne   = med2.PricePerOne,
                Date          = dateProvider,
                SoldQuantity  = 24
            };
            var sold6 = new ExternalDrugstoreSoldMedicine
            {
                StockMedicine = med3,
                PricePerOne   = med3.PricePerOne,
                Date          = dateProvider,
                SoldQuantity  = 36
            };

            dateProvider = DateTime.Today.AddDays(-2); // te nizej beda sprzed 2 dni
            var sold7 = new ExternalDrugstoreSoldMedicine
            {
                StockMedicine = med1,
                PricePerOne   = med1.PricePerOne,
                Date          = dateProvider,
                SoldQuantity  = 3
            };
            var sold8 = new ExternalDrugstoreSoldMedicine
            {
                StockMedicine = med2,
                PricePerOne   = med2.PricePerOne,
                Date          = dateProvider,
                SoldQuantity  = 11
            };
            var sold9 = new ExternalDrugstoreSoldMedicine
            {
                StockMedicine = med3,
                PricePerOne   = med3.PricePerOne,
                Date          = dateProvider,
                SoldQuantity  = 12
            };

            dateProvider = DateTime.Today.AddDays(-7); // te nizej beda sprzed 7 dni, powinny byc
            var sold17 = new ExternalDrugstoreSoldMedicine
            {
                StockMedicine = med1,
                PricePerOne   = med1.PricePerOne,
                Date          = dateProvider,
                SoldQuantity  = 30
            };
            var sold18 = new ExternalDrugstoreSoldMedicine
            {
                StockMedicine = med2,
                PricePerOne   = med2.PricePerOne,
                Date          = dateProvider,
                SoldQuantity  = 5
            };
            var sold19 = new ExternalDrugstoreSoldMedicine
            {
                StockMedicine = med3,
                PricePerOne   = med3.PricePerOne,
                Date          = dateProvider,
                SoldQuantity  = 47
            };

            dateProvider = DateTime.Today.AddDays(-8); //daaawno temu, nie powinno byc tego na liscie
            var sold10 = new ExternalDrugstoreSoldMedicine
            {
                StockMedicine = med1,
                PricePerOne   = med1.PricePerOne,
                Date          = dateProvider,
                SoldQuantity  = 10
            };
            var sold11 = new ExternalDrugstoreSoldMedicine
            {
                StockMedicine = med2,
                PricePerOne   = med2.PricePerOne,
                Date          = dateProvider,
                SoldQuantity  = 10
            };
            var sold12 = new ExternalDrugstoreSoldMedicine
            {
                StockMedicine = med3,
                PricePerOne   = med3.PricePerOne,
                Date          = dateProvider,
                SoldQuantity  = 10
            };

            context.ExternalDrugstoreSoldMedicines
            .AddRange(new ExternalDrugstoreSoldMedicine [] { sold1, sold2, sold3, sold4, sold5, sold6, sold7, sold8, sold9, sold10, sold11, sold12, sold17, sold18, sold19 });
            context.SaveChanges();
        }
예제 #18
0
        private UploadResultViewModel UpdateStore(XmlMedicineSupplyModel supply)
        {
            int totalUpdated          = 0;
            int totalAdded            = 0;
            int totalNewInExDrugstore = 0;
            int totalNewOnStock       = 0;

            foreach (var med in supply.Medicines)
            {
                if (med.StockId == null)
                {
                    if (!med.IsNew)
                    {
                        throw new MedicineNotFoundException($"No Id found and is not marked as new");
                    }

                    var existingMedicine = context.Medicines
                                           .FirstOrDefault(m => m.Name.Contains(med.Name, StringComparison.OrdinalIgnoreCase));
                    if (existingMedicine != null)
                    {
                        throw new MedicineNameAlreadyExistsExcpetion(med.Name);
                    }

                    totalNewOnStock++;
                    totalNewInExDrugstore++;
                    totalAdded += (int)med.Quantity;

                    var onStockMedicine = AutoMapper.Mapper.Map <MedicineOnStock>(med);
                    onStockMedicine.Quantity = 0;
                    context.Medicines.Add(onStockMedicine);
                    context.ExternalDrugstoreMedicines.Add(new Core.ExternalDrugstoreMedicine
                    {
                        Name          = onStockMedicine.Name,
                        PricePerOne   = onStockMedicine.PricePerOne,
                        Quantity      = med.Quantity,
                        StockMedicine = onStockMedicine
                    });
                }
                else
                {
                    var medicineOnStock = context.Medicines.SingleOrDefault(m => m.ID == med.StockId);
                    if (medicineOnStock == null)
                    {
                        throw new MedicineNotFoundException($"Id = {med.StockId}");
                    }

                    var medicine = context.ExternalDrugstoreMedicines
                                   .SingleOrDefault(m => m.StockMedicine.ID == medicineOnStock.ID);

                    if (medicine == null)
                    {
                        totalNewInExDrugstore++;
                        context.ExternalDrugstoreMedicines.Add(new ExternalDrugstoreMedicine
                        {
                            Name          = medicineOnStock.Name,
                            PricePerOne   = med.PricePerOne ?? 0.0d,
                            Quantity      = med.Quantity,
                            StockMedicine = medicineOnStock
                        });
                    }
                    else
                    {
                        totalUpdated++;
                        medicine.Quantity   += med.Quantity;
                        medicine.PricePerOne = med.PricePerOne ?? medicine.PricePerOne;
                    }
                    totalAdded += (int)med.Quantity;
                }
            }

            context.SaveChanges();

            return(new UploadResultViewModel
            {
                Succes = true,
                Message = "",
                Data = new Dictionary <string, object> {
                    { "Updated", totalUpdated },
                    { "Added", totalAdded },
                    { "NewInEx", totalNewInExDrugstore },
                    { "NewOnStock", totalNewOnStock }
                }
            });
        }