Ejemplo n.º 1
0
        public async Task <IActionResult> GenerateAccountsGuide(int id)
        {
            if (await _currencyRepository.CountAsync() == 0)
            {
                return(BadRequest("you should to set default currency before."));
            }

            if (await _accountRepository.CountAsync() > 0)
            {
                return(BadRequest("you already created accounts guide"));
            }

            var accounts = _predefinedGuideService.GetGuideAccounts(id);

            foreach (var accountModel in accounts)
            {
                var finalAccountId  = accountModel.FinalAccountId == Guid.Empty ? null : accountModel.FinalAccountId;
                var parentAccountId = accountModel.ParentId == Guid.Empty ? null : accountModel.ParentId;
                var account         =
                    new Account(accountModel.GetName(_language), accountModel.Code, parentAccountId, finalAccountId,
                                (AccountType)accountModel.Type, AccountDirectionType.Both, _defaultKeysOptions.Value.CurrencyId);
                account.Id = accountModel.Id;
                _accountRepository.Add(account, false);
            }

            var affectedRows = await _accountRepository.SaveAllAsync();

            if (affectedRows > 0)
            {
                return(Ok());
            }
            return(BadRequest());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Init(InitConfigViewModel model)
        {
            // check if it's initialized
            if (_defaultKeysOptions.Value.Initialized)
            {
                return(BadRequest("Already Initialized!"));
            }
            // 1. create company 2. finanital period
            string financialPeriodName = string.Format(Resources.Global.Common.FinancialPeriodName, DateTime.Now.Year, model.CompanyName);
            var    financialPeriod     = new FinancialPeriod(financialPeriodName, model.StartDate, model.EndtDate);

            financialPeriod.Company = new Company(model.CompanyName);
            _financialPeriodRepo.Add(financialPeriod, false);
            // 3. currency
            Currency currency = new Currency();

            if (model.CurrencyId.HasValue)
            {
                var preDefinedCurrency = _predefinedGuideService.GetCurrecny(model.CurrencyId.Value);
                currency = new Currency(preDefinedCurrency.Code, preDefinedCurrency.GetName(_language), 1, preDefinedCurrency.GetPartName(_language), preDefinedCurrency.PartRate, preDefinedCurrency.ISOCode);
            }
            else
            {
                var preDefinedCurrency = _predefinedGuideService.GetCurrecny(1); // USD
                currency = new Currency(preDefinedCurrency.Code, preDefinedCurrency.GetName(_language), 1, preDefinedCurrency.GetPartName(_language), preDefinedCurrency.PartRate, preDefinedCurrency.ISOCode);
            }

            _currencyRepo.Add(currency, false);

            // 4. accounts
            var accounts = _predefinedGuideService.GetGuideAccounts(model.AccoutGuideId);

            foreach (var accountModel in accounts)
            {
                var finalAccountId  = accountModel.FinalAccountId == Guid.Empty ? null : accountModel.FinalAccountId;
                var parentAccountId = accountModel.ParentId == Guid.Empty ? null : accountModel.ParentId;
                var account         =
                    new Account(accountModel.GetName(_language), accountModel.Code, parentAccountId, finalAccountId,
                                (AccountType)accountModel.Type, AccountDirectionType.Both, currency.Id);
                account.Id = accountModel.Id;
                _accountRepo.Add(account, false);
            }

            // 5. billTypes

            // 6. payTypes

            // 7. check and complete defulat items accountId and default cash accountId in predefienedGuides file

            var affectedRows = await _accountRepo.SaveAllAsync();

            if (affectedRows > 0)
            {
                // update appSettings
                //      1. update initialized prop
                _defaultKeysOptions.Update(a => a.Initialized = true);
                //      2. update default currency
                _defaultKeysOptions.Update(a => a.CurrencyId = currency.Id);

                return(Ok());
            }
            return(BadRequest());
        }