Exemplo n.º 1
0
        public async Task <IActionResult> Getjournal(DateTimeOffset?from, DateTimeOffset?to, DateTimeOffset?fromReleaseDate, DateTimeOffset?toReleaseDate, long?accountId, long?costCenterId, bool ShowNotPostedEntries = false, bool ShowTotalEntries = true)
        {
            Guid?accountGuid = null, costCenterGuid = null;

            if (accountId.HasValue)
            {
                var account = await _accountRepo.GetAsync(accountId.Value);

                if (account == null)
                {
                    return(NotFound("AcountNotFound"));
                }
                accountGuid = account.Id;
            }

            if (costCenterId.HasValue)
            {
                var costCenter = await _costCenterRepo.GetAsync(costCenterId.Value);

                if (costCenter == null)
                {
                    return(NotFound("CostCenterNotFound"));
                }
                costCenterGuid = costCenter.Id;
            }
            JournalOptions options = new JournalOptions
            {
                ShowNotPostedEntries = ShowNotPostedEntries,
                ShowTotalEntries     = ShowTotalEntries,
            };
            var journalViewModel = _reportService.GetjournalEntry(accountGuid, costCenterGuid, from, to, fromReleaseDate, toReleaseDate, options);

            return(Ok(journalViewModel));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Nodes(long id)
        {
            var costCenter = await _costCenterRepo.GetAsync(id);

            if (costCenter == null)
            {
                return(NotFound(Resources.CostCenters.CostCenterResource.CostCenterNotFound));
            }
            var viewModels = await _costCenterRepo.GetAllNoTracking().Where(e => e.ParentId == costCenter.Id)
                             .Select(e => new
            {
                Id          = e.Number,
                Name        = e.CodeName,
                hasChildren = e.CostCenters.LongCount(ee => ee.EntityStatus == EntityStatus.Active) > 0
            }).ToListAsync();

            return(Ok(viewModels));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> AccountStatment(DateTimeOffset?from, DateTimeOffset?to, DateTimeOffset?fromReleaseDate, DateTimeOffset?toReleaseDate, long?customerId, long?accountId, long?costCenterId, bool ShowNotPostedEntries = false)
        {
            Guid?accountIdGuid = null, customerIdGuid = null, costCenterGuid = null;

            if (customerId.HasValue)
            {
                var customer = await _customerRepo.GetAsync(customerId.Value);

                if (customer == null)
                {
                    return(NotFound("AcountNotFound"));
                }
                customerIdGuid = customer.Id;
            }
            if (accountId.HasValue)
            {
                var account = await _accountRepo.GetAsync(accountId.Value);

                if (account == null)
                {
                    return(NotFound("AcountNotFound"));
                }
                accountIdGuid = account.Id;
            }


            if (costCenterId.HasValue)
            {
                var costCenter = await _costCenterRepo.GetAsync(costCenterId.Value);

                if (costCenter == null)
                {
                    return(NotFound("CostCenterNotFound"));
                }
                costCenterGuid = costCenter.Id;
            }

            var CusAccStatmentsViewModel = _reportService.GetCustomerAccountStatment(customerIdGuid, accountIdGuid, costCenterGuid, from, to, fromReleaseDate, toReleaseDate);

            return(Ok(CusAccStatmentsViewModel));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Post([FromBody] AddStoreViewModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }

            Guid?accountId = null;

            if (model.AccountId.HasValue)
            {
                var account = await _accountRepo.GetAsync(model.AccountId.Value);

                if (account == null)
                {
                    return(NotFound(Resources.Accounts.AccountResource.AccountNotFound));
                }
                accountId = account.Id;
            }

            Guid?costCenterId = null;

            if (model.CostCenterId.HasValue)
            {
                var costCenter = await _costCenterRepo.GetAsync(model.CostCenterId.Value);

                if (costCenter == null)
                {
                    return(NotFound(Resources.CostCenters.CostCenterResource.CostCenterNotFound));
                }
                costCenterId = costCenter.Id;
            }

            Guid?parentId = null;

            if (model.ParentId.HasValue)
            {
                var parentStore = await _storeRepo.GetAsync(model.ParentId.Value);

                if (parentStore == null)
                {
                    return(NotFound(Resources.Stores.StoreResource.ParentStoreNotFound));
                }
                parentId = parentStore.Id;
            }

            if (await _storeRepo.IsExistCodeAsync(model.Code))
            {
                ModelState.AddModelError("Code", Resources.Global.Common.ThisCodeExist);
            }
            if (await _storeRepo.IsExistNameAsync(model.Name))
            {
                ModelState.AddModelError("Name", Resources.Global.Common.ThisNameExist);
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetWithErrorsKey()));
            }

            var store = new Store(model.Name, model.Code, accountId, costCenterId, model.Note);

            if (parentId.HasValue)
            {
                store.ParentId = parentId.Value;
            }

            var affectedRows = await _storeRepo.AddAsync(store);

            if (affectedRows > 0)
            {
                _storeRepo.LoadReferences(store);

                var viewModel = AutoMapper.Mapper.Map <StoreViewModel>(store);

                return(CreatedAtRoute("GetStore", new { id = store.Number }, viewModel));
            }
            return(BadRequest());
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Post(long id, [FromBody] AddPayEntryViewModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }
            if (id != model.PayTypeId)
            {
                return(BadRequest());
            }

            var payType = await _payTypeRep.GetAsync(id);

            if (payType == null)
            {
                return(NotFound("Not Found !"));
            }

            if (_periodManager.GetPeriod() == Guid.Empty)
            {
                ModelState.AddModelError("", Resources.Global.Common.FinancialPeriodMustBeSpecified);
                return(BadRequest(ModelState.GetWithErrorsKey()));
            }

            var financialPeriod = await _financialPeriodRepo.FindAsync(_periodManager.GetPeriod());

            if (financialPeriod == null)
            {
                return(NotFound("FinancialPeriodNotFound"));
            }
            if (!financialPeriod.CheckIfDateInPeriod(model.Date))
            {
                ModelState.AddModelError("Date", Resources.Global.Common.DateOutCurrenctPeriod);
                return(BadRequest(ModelState.GetWithErrorsKey()));
            }
            var debitSum  = payType.ShowItemDebitField ? model.Items.Sum(p => p.Debit) : 0;
            var creditSum = payType.ShowItemDebitField ? model.Items.Sum(p => p.Credit) : 0;

            Guid?payAccountId = null;
            var  payAccount   = await _accountRepo.GetAsync(model.PayAccountId.Value);

            if (payAccount != null)
            {
                payAccountId = payAccount.Id;
            }

            #region checks

            var ShowItemDebitField  = model.Items.ToList().All(e => e.Debit != 0);
            var ShowItemCreditField = model.Items.ToList().All(e => e.Credit != 0);

            if (payAccount != null && ((ShowItemDebitField && creditSum != 0) || (ShowItemCreditField && debitSum != 0)))
            {
                return(NotFound("Error in writing entry Details "));
            }
            if (ShowItemDebitField && ShowItemCreditField && payAccount != null)
            {
                return(NotFound("Error payAccount must be leaved empty in Daily or Beginning Entry ! "));
            }
            if ((payType.ShowItemDebitField && payType.ShowItemCreditField) && payAccount == null)
            {
                var isBalance = debitSum == creditSum ? true : false;
                return(NotFound("Entry is not Balanced ! "));
            }
            if ((ShowItemDebitField || ShowItemCreditField) && payAccount == null)
            {
                return(NotFound("Error payAccount must be Filled !! "));
            }

            Guid currencyId;
            var  currency = await _currencyRepo.GetAsync(model.CurrencyId);

            if (currency != null)
            {
                currencyId          = currency.Id;
                model.CurrencyValue = currency.Value;
            }
            else
            {
                currencyId          = _defaultKeysOptions.Value.CurrencyId;
                model.CurrencyValue = 1;
            }

            Guid?costCenterId = null;
            if (model.CostCenterId.HasValue)
            {
                var costCenter = await _costCenterRepo.GetAsync(model.CostCenterId.Value);

                if (costCenter == null)
                {
                    return(NotFound(Resources.CostCenters.CostCenterResource.CostCenterNotFound));
                }
                costCenterId = costCenter.Id;
            }

            Guid?branchId = null;
            if (model.BranchId.HasValue)
            {
                var branch = await _branchRepo.GetAsync(model.BranchId.Value);

                if (branch == null)
                {
                    return(NotFound(Resources.Branchs.BranchResource.BranchNotFound));
                }
                branchId = branch.Id;
            }



            #endregion

            long entryNumber = await _entryRepo.GetNextNumberAsync();

            var entry = new Entry(entryNumber, currencyId, model.CurrencyValue, branchId, model.Note);


            if ((payType.ShowItemDebitField && payType.ShowItemCreditField))
            {
                var itemsIndex = 0;
                // check form items if not found
                foreach (var item in model.Items)
                {
                    itemsIndex++;

                    Guid account1Id;
                    var  account1 = await _accountRepo.GetAsync(item.AccountId);

                    if (account1 == null)
                    {
                        return(NotFound($"account in entry item {itemsIndex} not found"));
                    }
                    account1Id = account1.Id;



                    Guid itemCurrencyId;
                    if (model.CurrencyId == item.CurrencyId)
                    {
                        itemCurrencyId     = currencyId;
                        item.CurrencyValue = model.CurrencyValue;
                    }
                    else
                    {
                        var itemCurrency = await _currencyRepo.GetAsync(item.CurrencyId.Value);

                        if (itemCurrency != null)
                        {
                            itemCurrencyId     = itemCurrency.Id;
                            item.CurrencyValue = itemCurrency.Value;
                        }
                    }

                    Guid?itemCostCenterId = null;
                    if (model.CostCenterId.HasValue && item.CostCenterId.HasValue && model.CostCenterId == item.CostCenterId)
                    {
                        itemCostCenterId = costCenterId;
                    }
                    else
                    {
                        if (item.CostCenterId.HasValue)
                        {
                            var costCenter = await _costCenterRepo.GetAsync(item.CostCenterId.Value);

                            if (costCenter == null)
                            {
                                return(NotFound(Resources.CostCenters.CostCenterResource.CostCenterNotFound));
                            }
                            itemCostCenterId = costCenter.Id;
                        }
                    }

                    var entryItem = new EntryItem(account1Id, item.Debit, item.Credit, itemCurrencyId, item.CurrencyValue.Value, costCenterId, item.Date.Value.UtcDateTime, item.Note);
                    entry.Items.Add(entryItem);
                }
            }
            else if (payType.ShowItemDebitField || payType.ShowItemCreditField)
            {
                var itemsIndex = 0;
                // check form items if not found
                foreach (var item in model.Items)
                {
                    itemsIndex++;

                    Guid account1Id;
                    var  account1 = await _accountRepo.GetAsync(item.AccountId);

                    if (account1 == null)
                    {
                        return(NotFound($"account in entry item {itemsIndex} not found"));
                    }
                    account1Id = account1.Id;



                    Guid itemCurrencyId;
                    if (model.CurrencyId == item.CurrencyId)
                    {
                        itemCurrencyId     = currencyId;
                        item.CurrencyValue = model.CurrencyValue;
                    }
                    else
                    {
                        var itemCurrency = await _currencyRepo.GetAsync(item.CurrencyId.Value);

                        if (itemCurrency != null)
                        {
                            itemCurrencyId     = itemCurrency.Id;
                            item.CurrencyValue = itemCurrency.Value;
                        }
                    }

                    Guid?itemCostCenterId = null;
                    if (model.CostCenterId.HasValue && item.CostCenterId.HasValue && model.CostCenterId == item.CostCenterId)
                    {
                        itemCostCenterId = costCenterId;
                    }
                    else
                    {
                        if (item.CostCenterId.HasValue)
                        {
                            var costCenter = await _costCenterRepo.GetAsync(item.CostCenterId.Value);

                            if (costCenter == null)
                            {
                                return(NotFound(Resources.CostCenters.CostCenterResource.CostCenterNotFound));
                            }
                            itemCostCenterId = costCenter.Id;
                        }
                    }


                    var entryItem = new EntryItem(account1Id, item.Debit, item.Credit, itemCurrencyId, item.CurrencyValue.Value, costCenterId, item.Date.Value.UtcDateTime, item.Note);

                    entry.Items.Add(entryItem);
                }

                var entryItem1 = new EntryItem(payAccountId.Value, creditSum, debitSum, currencyId, model.CurrencyValue, costCenterId, model.Date.UtcDateTime, model.Note);
                entry.Items.Add(entryItem1);
            }
            entry.PayEntry = new PayEntry
            {
                Number       = await _payEntryRepo.GetNextNumberAsync(payType.Id),
                PayAccountId = payAccountId.Value,
                PayTypeId    = payType.Id,
            };

            if (payType.AutoPostToAccounts)
            {
                await _accountBalanceService.PostEntryToAccounts(entry);
            }

            var affectedRows = await _entryRepo.AddAsync(entry);

            if (affectedRows > 0)
            {
                var viewModel = AutoMapper.Mapper.Map <EntryViewModel>(entry);
                return(CreatedAtRoute("GetPayEntry", new { id = entry.Number }, viewModel));
            }

            return(BadRequest());
        }
Exemplo n.º 6
0
        public async Task <IActionResult> Post(long typeId, [FromBody] AddBillViewModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }

            if (typeId != model.BillTypeId)
            {
                return(BadRequest());
            }

            if (_periodManager.GetPeriod() == Guid.Empty)
            {
                ModelState.AddModelError("", Resources.Global.Common.FinancialPeriodMustBeSpecified);
                return(BadRequest(ModelState.GetWithErrorsKey()));
            }

            var financialPeriod = await _financialPeriodRepo.FindAsync(_periodManager.GetPeriod());

            if (financialPeriod == null)
            {
                return(NotFound("FinancialPeriodNotFound"));
            }
            if (!financialPeriod.CheckIfDateInPeriod(model.Date))
            {
                ModelState.AddModelError("Date", Resources.Global.Common.DateOutCurrenctPeriod);
                return(BadRequest(ModelState.GetWithErrorsKey()));
            }

            var billType = await _billTypeRepo.GetAsync(typeId);

            if (billType == null)
            {
                return(NotFound(Resources.Bills.BillResource.BillTypeNotFound));
            }

            switch (billType.Type)
            {
            case BillsType.Transfer:
                ModelState.AddModelError("", "can't not add new transfare from here!");
                return(BadRequest(ModelState.GetWithErrorsKey()));

            case BillsType.EndPeriodInventory:
                ModelState.AddModelError("", "can't add End Period Inventory bill");
                return(BadRequest(ModelState.GetWithErrorsKey()));
            }

            #region checks

            Guid currencyId;
            var  currency = await _currencyRepo.GetAsync(model.CurrencyId);

            if (currency != null)
            {
                currencyId = currency.Id;
                if (!model.CurrencyValue.HasValue)
                {
                    model.CurrencyValue = currency.Value;
                }
            }
            else
            {
                currencyId          = _defaultKeysOptions.Value.CurrencyId;
                model.CurrencyValue = 1;
            }


            Guid cashAccountId;
            var  cashAccount = await _accountRepo.GetAsync(model.AccountId);

            if (cashAccount == null)
            {
                return(NotFound("Cash account not found"));
            }
            cashAccountId = cashAccount.Id;


            Guid?customerAccountId = null;
            if (model.CustomerAccountId.HasValue)
            {
                var customerAccount = await _accountRepo.GetAsync(model.CustomerAccountId.Value);

                if (customerAccount == null)
                {
                    return(NotFound("Customer account not found"));
                }
                if (customerAccount.CustomerId == null)
                {
                    ModelState.AddModelError("CustomerAccountId", "account not related to customer or supplier");
                    return(BadRequest(ModelState.GetWithErrorsKey()));
                }
                customerAccountId = customerAccount.Id;
            }

            Guid?storeId = null;
            if (model.StoreId.HasValue)
            {
                var store = await _storeRepo.GetAsync(model.StoreId.Value);

                if (store == null)
                {
                    return(NotFound(Resources.Stores.StoreResource.StoreNotFound));
                }
                storeId = store.Id;
            }

            Guid?costCenterId = null;
            if (model.CostCenterId.HasValue)
            {
                var costCenter = await _costCenterRepo.GetAsync(model.CostCenterId.Value);

                if (costCenter == null)
                {
                    return(NotFound(Resources.CostCenters.CostCenterResource.CostCenterNotFound));
                }
                costCenterId = costCenter.Id;
            }

            Guid?branchId = null;
            if (model.BranchId.HasValue)
            {
                var branch = await _branchRepo.GetAsync(model.BranchId.Value);

                if (branch == null)
                {
                    return(NotFound(Resources.Branchs.BranchResource.BranchNotFound));
                }
                branchId = branch.Id;
            }

            #endregion

            var bill = new Bill(billType.Id, currencyId, model.CurrencyValue.Value, cashAccountId, customerAccountId, model.CustomerName, model.Date, model.PayType, storeId, costCenterId, branchId, model.Extra, model.Disc, model.TotalPaid, model.Note);

            // add the payemnt
            if (bill.PayType == PaysType.Credit && billType.DefaultCashAccountId.HasValue)
            {
                double credit = 0;
                double debit  = 0;

                var inOrOut = int.Parse(EnumHelper.GetDescription(billType.Type));
                if (inOrOut == 1)
                {
                    // مشتريات
                    credit = bill.TotalPaid; // صندوق
                    debit  = 0;              // مورد
                }
                else
                {
                    // مبيعات
                    credit = 0;              // زبون
                    debit  = bill.TotalPaid; // صندوق
                }

                var payEntryItem = new BillEntryItem(bill.Date, billType.DefaultCashAccountId.Value, bill.CurrencyId, bill.CurrencyValue, null, BillEntryItemType.Pay, debit, credit, "");
                bill.BillEntryItems.Add(payEntryItem);
            }

            var itemsIndex = 0;
            // check form items if not found
            foreach (var item in model.Items)
            {
                itemsIndex++;
                Guid itemStoreId;
                if (model.StoreId.HasValue && model.StoreId.Value == item.StoreId)
                {
                    itemStoreId = storeId.Value;
                }
                else
                {
                    var itemStore = await _storeRepo.GetAsync(item.StoreId.Value);

                    if (itemStore == null)
                    {
                        return(NotFound($"store in item {itemsIndex} not found"));
                    }
                    itemStoreId = itemStore.Id;
                }

                Guid?itemCostCenterId = null;
                if (model.CostCenterId.HasValue && item.CostCenterId.HasValue && model.CostCenterId == item.CostCenterId)
                {
                    itemCostCenterId = costCenterId;
                }
                else
                {
                    if (item.CostCenterId.HasValue)
                    {
                        var itemCostCenter = await _storeRepo.GetAsync(item.CostCenterId.Value);

                        if (itemCostCenter == null)
                        {
                            return(NotFound($"costCenter in item {0} not found"));
                        }
                    }
                }

                var itemUnit = _itemUnitRepo.Get(item.ItemId, item.UnitId);

                if (itemUnit == null)
                {
                    ModelState.AddModelError($"Items[{itemsIndex}].ItemId", "المادة غير موجودة");
                    return(BadRequest(ModelState.GetWithErrorsKey()));
                }

                var billItem = new BillItem(itemUnit.Id, itemStoreId, itemCostCenterId, item.Quantity, item.Price, item.Extra, item.Disc, model.Note);
                bill.BillItems.Add(billItem);

                if (billType.AutoPostToStores)
                {
                    if (!await PostToStore(bill, billType, billItem, itemUnit))
                    {
                        ModelState.AddModelError($"Items[{itemsIndex}].Quantity", "لا يوجد كل هذه الكمية في المستودع");
                        return(BadRequest(ModelState.GetWithErrorsKey()));
                    }
                    bill.IsPosted = true;
                }
            }

            bill.CalcTotal();

            #region billEntryItem
            if (bill.Extra + bill.TotalItemsExtra > 0 && billType.DefaultExtraAccountId.HasValue)
            {
                var extraEntryItem = new BillEntryItem(bill.Date, billType.DefaultExtraAccountId.Value, bill.CurrencyId, bill.CurrencyValue, bill.CostCenterId, BillEntryItemType.ExtraDisc, 0, bill.Extra + bill.TotalItemsExtra, bill.Note);
                bill.BillEntryItems.Add(extraEntryItem);
            }

            if (bill.Disc + bill.TotalItemsDisc > 0 && billType.DefaultDiscAccountId.HasValue)
            {
                var discEntryItem = new BillEntryItem(bill.Date, billType.DefaultDiscAccountId.Value, bill.CurrencyId, bill.CurrencyValue, bill.CostCenterId, BillEntryItemType.ExtraDisc, bill.Disc + bill.TotalItemsDisc, 0, bill.Note);
                bill.BillEntryItems.Add(discEntryItem);
            }

            foreach (var pay in model.Pays)
            {
                var payEntryItem = new BillEntryItem(pay.Date, pay.AccountId, pay.CurrencyId, pay.CurrencyValue, pay.CostCenterId, BillEntryItemType.Pay, pay.Debit, pay.Credit, pay.Note);
                bill.BillEntryItems.Add(payEntryItem);
            }
            #endregion

            if (billType.AutoGenerateEntry)
            {
                if (bill.PayType == PaysType.Cash)
                {
                    bill.AccountId = cashAccountId;
                }
                else
                {
                    bill.AccountId = customerAccountId;
                }

                Entry entry = GenerateEntry(bill, billType);

                bill.BillEntry = new BillEntry(entry);

                _entryRepo.Add(entry, false);

                bill.IsEntryGenerated = true;

                if (billType.AutoPostEntryToAccounts)
                {
                    await _accountBalanceService.PostEntryToAccounts(entry);
                }
            }

            bill.Number = await _billRepo.GetNextNumberAsync(billType.Id);

            var affectedRows = await _billRepo.AddAsync(bill);

            if (affectedRows > 0)
            {
                var viewModel = AutoMapper.Mapper.Map <BillViewModel>(bill);

                return(CreatedAtRoute("GetBill", new { typeId = bill.BillType.Number, id = bill.Number }, viewModel));
            }
            return(BadRequest());
        }