protected override void Up(MigrationBuilder migrationBuilder) { var optionsBuilder = new DbContextOptionsBuilder <SiriusContext>(); string connectionString = "Host=127.0.0.1;Port=5432;Database=siriusdb;Username=siriususer;Password=Uncle340571578;Integrated Security=false;"; var options = optionsBuilder .UseNpgsql(connectionString) .Options; using (var context = new SiriusContext(options)) { Role viewerRole = context.Roles.Where(role => role.Id == DefaultValues.Roles.Viewer.Id).FirstOrDefault(); var startDate = DateConverter.ConvertToRTS(DateTime.UtcNow.ToLocalTime()); byte[] passwordHash, passwordSalt; PasswordHash.CreatePasswordHash("user2019", out passwordHash, out passwordSalt); var user = new User { FirstName = "Пользователь", LastName = "", Username = "******", Id = Guid.NewGuid(), Role = viewerRole, StartDate = startDate, IsConfirmed = true, PasswordHash = passwordHash, PasswordSalt = passwordSalt }; context.Users.Add(user); context.SaveChanges(); } }
/// <summary> /// Создание пользователя /// </summary> /// <param name="user"></param> /// <param name="password"></param> /// <returns></returns> public User CreateUser(User user, string password) { // validation if (string.IsNullOrWhiteSpace(password)) { throw new AppException("Требуется ввести пароль."); } if (_unitOfWork.UserRepository.CheckUsername(user.Username)) { throw new AppException("Пользователь " + user.Username + " существует."); } byte[] passwordHash, passwordSalt; CreatePasswordHash(password, out passwordHash, out passwordSalt); user.PasswordHash = passwordHash; user.PasswordSalt = passwordSalt; user.Role = _unitOfWork.RoleRepository.GetRoleById(DefaultValues.Roles.Viewer.Id); user.StartDate = DateConverter.ConvertToRTS(DateTime.UtcNow.ToLocalTime()); // Учетная запись первого зарегистрированного пользователя должна быть подтверждена if (GetUserAmount() == 0) { user.IsConfirmed = true; } _unitOfWork.UserRepository.Insert(user); _unitOfWork.Save(); return(user); }
/// <summary> /// Проведение накладной /// </summary> /// <param name="invoiceId"></param> /// <returns></returns> public string FixInvoice(Guid invoiceId, Guid userId) { string result; var invoice = _unitOfWork.InvoiceRepository.GetByID(invoiceId); var registers = _unitOfWork.RegisterRepository.GetByInvoiceId(invoiceId); // Взаимодействие с регистром накопления registers.ToList().ForEach(register => { var storageRegister = _unitOfWork.StorageRegisterRepository.GetByItemIdAndCost(register.ItemId, register.Cost); if (storageRegister != null) { storageRegister.Amount += register.Amount * invoice.Factor; } else { var newStorageRegister = new StorageRegister() { Id = Guid.NewGuid(), Date = DateConverter.ConvertToRTS(DateTime.UtcNow.ToLocalTime()), Amount = register.Amount, Cost = register.Cost, ItemId = register.ItemId }; _unitOfWork.StorageRegisterRepository.Insert(newStorageRegister); } if (storageRegister != null) { if (storageRegister.Amount == 0) { _unitOfWork.StorageRegisterRepository.Delete(storageRegister); } } }); // Изменение свойств существующей накладной invoice.IsTemporary = false; invoice.IsFixed = true; _unitOfWork.Save(); //UpdateInvoice(invoiceId, invoice); // Проверка сохраненных изменений invoice = _unitOfWork.InvoiceRepository.GetByID(invoiceId); if (invoice != null && invoice.IsFixed == true) { var invoiceType = _unitOfWork.InvoiceRepository.GetTypeById(invoice.TypeId); AddLog("UPDATED", string.Format("{0} {1}", invoiceType.Name, invoice.Name), userId); result = "Накладная " + invoice.Name + " успешно проведена!"; } else { result = "Накладная не проведена."; } return(result); }
public void AddLog(string action, string content, Guid userId) { var log = new Log() { Id = Guid.NewGuid(), Content = content, Action = action, CreateDate = DateConverter.ConvertToRTS(DateTime.UtcNow.ToLocalTime()), User = _unitOfWork.UserRepository.GetByID(userId) }; _unitOfWork.LogRepository.Insert(log); _unitOfWork.Save(); }
/// <summary> /// Добавить новую накладную /// </summary> /// <param name="invoice"></param> /// <returns></returns> public object AddInvoice(Invoice invoice, Guid userId) { var newInvoiceId = Guid.NewGuid(); var newInvoice = new Invoice() { Id = newInvoiceId, Name = "", UserId = invoice.UserId, VendorId = DefaultValues.Vendor.Primary.Id, Date = DateConverter.ConvertToRTS(DateTime.UtcNow.ToLocalTime()), IsTemporary = true, IsFixed = false, TypeId = invoice.TypeId, Comment = invoice.Comment, Factor = invoice.Factor }; _unitOfWork.InvoiceRepository.Insert(newInvoice); _unitOfWork.Save(); var addedInvoice = _unitOfWork.InvoiceRepository.GetByID(newInvoiceId); if (addedInvoice != null) { var year = addedInvoice.Date.Year; var number = _unitOfWork.InvoiceRepository.Get(x => x.Date.Year == year && x.TypeId == invoice.TypeId).Select(i => i.Number).Max() + 1; // Получаем информацию о типе накладной var invoiceType = GetInvoiceTypeByTypeId(invoice.TypeId); // И узнаём префикс из настроек var prefix = GetSettingValueByTypeIdAndAlias(Types.SettingsTypes.Invoice.Prefix.Id, invoiceType.Alias); addedInvoice.Name = $"{prefix}-{year}/{number}"; addedInvoice.Number = number; _unitOfWork.InvoiceRepository.Update(addedInvoice); _unitOfWork.Save(); AddLog("CREATED", string.Format("{0} {1}", invoiceType.Name, addedInvoice.Name), userId); return(_unitOfWork.InvoiceRepository.GetById(newInvoiceId)); } return(null); }