Example #1
0
        private static void ChangeStatus(ContractDetailsDto contractDetails, string text)
        {
            using (var context = new PawnShopContext())
            {
                var contract = context.Contracts.Find(contractDetails.Id);
                contract.Status = (Status)Enum.Parse(typeof(Status), text);

                if (text == "Saled")
                {
                    CommandParser parser  = new CommandParser();
                    var           command =
                        parser.ParseCommand(new[]
                                            { "CashBoxTransaction", "Deposit", contract.PropertyValue.ToString(), "Sold item" });
                    command.Execute();
                }

                if (text == "Ended")
                {
                    CommandParser parser  = new CommandParser();
                    var           command =
                        parser.ParseCommand(new[]
                    {
                        "CashBoxTransaction", "Deposit", (contract.PropertyValue * contract.Interest).ToString(),
                        "Ended item"
                    });
                    command.Execute();
                }

                context.SaveChanges();
            }
        }
Example #2
0
        private void CashBoxTransaction(OperationType operationType, decimal value, string details)
        {
            using (var context = new PawnShopContext())
            {
                context.Users.Attach(LoginUser.User);

                if (operationType == OperationType.Deposit)
                {
                    LoginUser.User.Office.CashBox.Balance += value;
                }
                else
                {
                    LoginUser.User.Office.CashBox.Balance -= value;
                }

                var cashOperation = new CashOperation()
                {
                    CashBox       = LoginUser.User.Office.CashBox,
                    Details       = details,
                    OperationType = operationType,
                    Value         = value
                };

                context.CashOperations.Add(cashOperation);
                context.SaveChanges();
            }
        }
        private void RegisterUser(string officeName, string username, string password, string confirmedPassword)
        {
            using (var context = new PawnShopContext())
            {
                if (context.Users.Any(u => u.Credentials.Email == username))
                {
                    throw new ArgumentException($"User with that username already exists!");
                }

                if (!password.Equals(confirmedPassword))
                {
                    throw new ArgumentException($"Passwords do not match!");
                }

                if (LoginUser.User != null)
                {
                    throw new ArgumentException($"Login users cannot register. Logout first!");
                }

                var office = context.Offices.FirstOrDefault(o => o.Name == officeName);
                var user   = new User()
                {
                    Office      = office,
                    Credentials = new Credentials()
                    {
                        Email    = username,
                        Password = password
                    }
                };

                context.Users.Add(user);
                context.SaveChanges();
                LoginUser.User = user;
            }
        }
Example #4
0
        private void AddNewContract(string property, decimal properyValue, decimal interest, DateTime endDate,
                                    string clientName)
        {
            using (var context = new PawnShopContext())
            {
                var client =
                    context.Clients.FirstOrDefault(
                        c => string.Concat(c.FirstName, " ", c.MiddleName, " ", c.LastName) == clientName);

                var user = context.Users.FirstOrDefault();

                var contract = new Contract()
                {
                    PledgedProperty = property,
                    PropertyValue   = properyValue,
                    Interest        = interest,
                    StartDate       = DateTime.Today,
                    EndDate         = endDate,
                    Employee        = user,
                    Client          = client,
                    Status          = Status.New
                };

                context.Contracts.Add(contract);
                context.SaveChanges();
            }
        }
        public static IEnumerable <StatisticDto> GetOperations(string startDateString, string endDateString)
        {
            using (var context = new PawnShopContext())
            {
                MapperInitiliazer.InitializeStatistics();
                context.Users.Attach(LoginUser.User);

                var startDate = DateTime.Parse(startDateString);
                var endDate   = DateTime.Parse(endDateString);

                if (startDate > endDate)
                {
                    throw new InvalidOperationException("Invalid data provided.");
                }

                var operations =
                    context.CashOperations.Where(c => c.CashBox.Office.Name == LoginUser.User.Office.Name &&
                                                 (SqlFunctions.DateDiff("day", c.DateTime, startDate).Value <= 0 &&
                                                  SqlFunctions.DateDiff("day", c.DateTime, endDate).Value >= 0))
                    .OrderBy(c => c.DateTime)
                    .ProjectTo <StatisticDto>()
                    .ToList();

                return(operations);
            }
        }
        public static IEnumerable <CashOperation> GetFirstAndLastOperation(string startDateString, string endDateString)
        {
            using (var context = new PawnShopContext())
            {
                context.Users.Attach(LoginUser.User);

                var startDate = DateTime.Parse(startDateString);
                var endDate   = DateTime.Parse(endDateString);

                if (startDate > endDate)
                {
                    throw new InvalidOperationException("Invalid data provided.");
                }

                var operations =
                    context.CashOperations.Where(c => c.CashBox.Office.Name == LoginUser.User.Office.Name &&
                                                 (SqlFunctions.DateDiff("day", c.DateTime, startDate).Value <= 0 &&
                                                  SqlFunctions.DateDiff("day", c.DateTime, endDate).Value >= 0))
                    .OrderBy(c => c.DateTime)
                    .ToList();

                var filtredOperations = new []
                {
                    operations.First(),
                    operations.Last()
                };

                return(operations);
            }
        }
Example #7
0
        private static void LogUser(string username, string password)
        {
            using (var context = new PawnShopContext())
            {
                var checkUser     = context.Users.Select(u => u.Credentials).Any(c => c.Email == username && c.Password == password);
                var checkPassword = context.Users.Select(u => u.Credentials)
                                    .Where(c => c.Email == username)
                                    .Select(c => c.Password)
                                    .FirstOrDefault();

                if (!checkUser || checkPassword != password)
                {
                    throw new ArgumentException("Invalid username or password");
                }

                if (LoginUser.User != null)
                {
                    throw new ArgumentException("You should log out first");
                }

                var userId = context.Credentials.FirstOrDefault(c => c.Email == username).Id;
                var user   = context.Users.Find(userId);
                LoginUser.User = user;
            }
        }
Example #8
0
        public static IEnumerable <ContractDto> GetContracts(Status status = Status.All)
        {
            using (var context = new PawnShopContext())
            {
                MapperInitiliazer.InitiliazeContracts();

                if (status == Status.All)
                {
                    var contracts =
                        context.Contracts
                        //.Where(c => c.Employee.Office.Name == LoginUser.User.Office.Name)
                        .ProjectTo <ContractDto>()
                        .ToList();

                    return(contracts);
                }

                var filtredContracts =
                    context.Contracts.Where(c => c.Employee.Office.Name == LoginUser.User.Office.Name && c.Status == status)
                    .ProjectTo <ContractDto>()
                    .ToList();

                return(filtredContracts);
            }
        }
Example #9
0
        private void ImportMoney()
        {
            using (var context = new PawnShopContext())
            {
                context.Users.Attach(LoginUser.User);
                var cashBox = LoginUser.User.Office.CashBox;

                allCashAvailability.Text = cashBox.Balance.ToString();
            }
        }
Example #10
0
        public static IEnumerable <ContractDto> GetContractsByTown(string town)
        {
            using (var context = new PawnShopContext())
            {
                MapperInitiliazer.InitiliazeContracts();

                var contracts = context.Contracts.Where(c => c.Employee.Office.Name == LoginUser.User.Office.Name && c.Employee.Office.Address.Town.Name == town)
                                .ProjectTo <ContractDto>()
                                .ToList();

                return(contracts);
            }
        }
Example #11
0
        public static IEnumerable <ContractDto> GetContractsByClientsName(string name)
        {
            using (var context = new PawnShopContext())
            {
                MapperInitiliazer.InitiliazeContracts();

                var contracts = context.Contracts.Where(c => c.Employee.Office.Name == LoginUser.User.Office.Name && string.Concat(c.Client.FirstName, " ", c.Client.MiddleName, " ", c.Client.LastName) == name)
                                .ProjectTo <ContractDto>()
                                .ToList();

                return(contracts);
            }
        }
Example #12
0
        public static IEnumerable <ContractDto> GetContractsByClientsPersonalId(int personalId)
        {
            using (var context = new PawnShopContext())
            {
                MapperInitiliazer.InitiliazeContracts();

                var contracts = context.Contracts.Where(c => c.Employee.Office.Name == LoginUser.User.Office.Name && c.Client.PersonalID == personalId)
                                .ProjectTo <ContractDto>()
                                .ToList();

                return(contracts);
            }
        }
Example #13
0
        private void ExitCommand(object sender, RoutedEventArgs e)
        {
            using (var context = new PawnShopContext())
            {
                var contractToDelete = context.Contracts.Where(c => c.Status == Status.Saled).ToList();
                foreach (var contract in contractToDelete)
                {
                    context.Contracts.Remove(contract);
                }

                context.SaveChanges();
            }
        }
Example #14
0
        public static IEnumerable <ContractDto> GetContractsByAddress(string address)
        {
            using (var context = new PawnShopContext())
            {
                context.Users.Attach(LoginUser.User);
                MapperInitiliazer.InitiliazeContracts();

                var contracts = context.Contracts.Where(c => c.Employee.Office.Name == LoginUser.User.Office.Name && c.Client.Address.Text == address)
                                .ProjectTo <ContractDto>()
                                .ToList();

                return(contracts);
            }
        }
Example #15
0
        public override string Execute()
        {
            using (var context = new PawnShopContext())
            {
                if (LoginUser.User == null)
                {
                    throw new InvalidOperationException("There is no loged in user!");
                }

                var username = context.Credentials.Find(LoginUser.User.Id).Email;

                LoginUser.User = null;

                return(string.Format($"User {username} successfully loged out in!"));
            }
        }
Example #16
0
        public static IEnumerable <ClientDto> GetClients()
        {
            MapperInitiliazer.InitializeClients();

            using (var context = new PawnShopContext())
            {
                var clients =
                    context.Clients
                    //.Where(
                    //        c => c.Contracts.Any(con => con.Employee.Office.Name == LoginUser.User.Office.Name))
                    .ProjectTo <ClientDto>()
                    .ToList();

                return(clients);
            }
        }
        private static void AddNewClient(string address, string firstName, string middleName, string lastName, string personalId,
                                         string idCardNumber, string phonenumber, string town, string property, string propertyValue, string interest, string endDate)
        {
            using (var context = new PawnShopContext())
            {
                var addressLiving = context.Addresses.FirstOrDefault(a => a.Text == address);

                var client = new Client()
                {
                    FirstName        = firstName,
                    MiddleName       = middleName,
                    LastName         = lastName,
                    PersonalID       = int.Parse(personalId),
                    IDCardNumber     = idCardNumber,
                    PhoneNumber      = phonenumber,
                    RegistrationDate = DateTime.Now
                };

                var townLiving = context.Towns.FirstOrDefault(t => t.Name == town);
                if (townLiving == null)
                {
                    townLiving = new Town()
                    {
                        Name = town
                    };
                }

                if (addressLiving == null)
                {
                    client.Address = new ClientAddress()
                    {
                        Text = address,
                        Town = townLiving
                    };
                }
                else
                {
                    client.Address = (ClientAddress)addressLiving;
                }

                var clientName = client.FirstName + " " + client.MiddleName + " " + client.LastName;
                context.Clients.Add(client);
                context.SaveChanges();
                AddContract.AddNewContract(property, decimal.Parse(propertyValue), decimal.Parse(interest), DateTime.Parse(endDate), clientName);
            }
        }
Example #18
0
        public static ContractDetailsDto GetContractDetails(ContractDto contractDto)
        {
            using (var context = new PawnShopContext())
            {
                context.Users.Attach(LoginUser.User);
                var employee = context.Contracts.Find(contractDto.ContractId).Employee.Credentials.Email;

                var contractDetails = new ContractDetailsDto()
                {
                    Id                 = contractDto.ContractId,
                    Date               = contractDto.DateOfRegistrationAndExpiring,
                    Property           = contractDto.PledgedProperty,
                    ProperyValue       = contractDto.PropertyValue.ToString(),
                    ValueAfterInterest = (contractDto.PropertyValue * contractDto.Interest + contractDto.PropertyValue).ToString()
                };

                return(contractDetails);
            }
        }
Example #19
0
        public static ContractDto GetContractById(int contractId)
        {
            using (var context = new PawnShopContext())
            {
                var contract    = context.Contracts.Find(contractId);
                var contractDto = new ContractDto()
                {
                    ContractId = contract.Id,
                    Client     = contract.Client.FirstName + " " + contract.Client.MiddleName + " " + contract.Client.LastName + Environment.NewLine + contract.Client.PersonalID,
                    DateOfRegistrationAndExpiring =
                        contract.StartDate.Date.ToString() + " - " + contract.EndDate.Date.ToString(),
                    PropertyValue   = contract.PropertyValue,
                    Interest        = contract.Interest,
                    Days            = (contract.StartDate - contract.EndDate).Days,
                    PledgedProperty = contract.PledgedProperty,
                    Status          = contract.Status
                };

                return(contractDto);
            }
        }
        private void CashBoxTransaction(OperationType operationType, decimal value, string details)
        {
            if (value <= 0)
            {
                throw new ArgumentException("Invalid value");
            }

            using (var context = new PawnShopContext())
            {
                context.Users.Attach(LoginUser.User);

                var cashOperation = new CashOperation()
                {
                    CashBox            = LoginUser.User.Office.CashBox,
                    Details            = details,
                    OperationType      = operationType,
                    Value              = value,
                    DateTime           = DateTime.Now,
                    CashBoxValueBefore = LoginUser.User.Office.CashBox.Balance
                };

                if (operationType == OperationType.Deposit)
                {
                    LoginUser.User.Office.CashBox.Balance += value;
                }
                else
                {
                    LoginUser.User.Office.CashBox.Balance -= value;
                }

                cashOperation.CashBoxValueAfter = LoginUser.User.Office.CashBox.Balance;

                context.CashOperations.Add(cashOperation);
                context.SaveChanges();
            }
        }