// Invoice // Income // Recievable // Sales // Stock // StockOut // Transaction public async Task <WrapperSalesListVM> AddSalesAsync(SalesVM salesVM) { // Invoice Invoice invoiceToAdd = new Invoice(); invoiceToAdd = _utilService.Mapper.Map <SalesVM, Invoice>(salesVM); _repositoryWrapper.Invoice.Create(invoiceToAdd); // Setting Up Invoice Id salesVM.InvoiceId = invoiceToAdd.Id; foreach (var item in salesVM.ItemList) { item.InvoiceId = invoiceToAdd.Id; } // Income Income incomeToAdd = new Income(); incomeToAdd = this._utilService.Mapper.Map <SalesVM, Income>(salesVM); _repositoryWrapper.Income.Create(incomeToAdd); // Recievable Recievable recievableToAdd = new Recievable(); recievableToAdd = _utilService.Mapper.Map <SalesVM, Recievable>(salesVM); _repositoryWrapper.Recievable.Create(recievableToAdd); // Sales List <Sales> listOfSalesToAdd = new List <Sales>(); listOfSalesToAdd = _utilService.Mapper.Map <List <SalesItemVM>, List <Sales> >(salesVM.ItemList); _repositoryWrapper.Sales.CreateAll(listOfSalesToAdd); // Stock Stock stockToAdd = new Stock(); IEnumerable <Stock> stockList = await _repositoryWrapper.Stock.FindByConditionAsync(x => x.FactoryId == salesVM.FactoryId); for (int i = 0; i < salesVM.ItemList.Count; i++) { Stock existingStock = stockList.ToList().Where(x => x.ItemId == salesVM.ItemList[i].Item.Id && x.ItemStatusId == salesVM.ItemList[i].ItemStatus.Id).FirstOrDefault(); if (existingStock == null) { var getDatalistVM2 = new GetDataListVM() { FactoryId = salesVM.FactoryId, PageNumber = 1, PageSize = 10 }; WrapperSalesListVM dataToReturn = await GetAllSalesAsync(getDatalistVM2); dataToReturn.HasMessage = true; dataToReturn.Message = "Stock Is Empty"; return(dataToReturn); // _utilService.Log("Stock Is Empty. Not Enough Stock available"); // throw new StockEmptyException(); } else { if (existingStock.Quantity >= salesVM.ItemList[i].Quantity) { existingStock.Quantity -= salesVM.ItemList[i].Quantity; _repositoryWrapper.Stock.Update(existingStock); } else { _utilService.Log("Stock Is Empty"); return(new WrapperSalesListVM()); } } } // StockOut List <StockOut> stockOutsToAdd = new List <StockOut>(); stockOutsToAdd = _utilService.Mapper.Map <List <SalesItemVM>, List <StockOut> >(salesVM.ItemList); _repositoryWrapper.StockOut.CreateAll(stockOutsToAdd); // Transaction TblTransaction transactionRecieved = new TblTransaction(); transactionRecieved = _utilService.Mapper.Map <SalesVM, TblTransaction>(salesVM); transactionRecieved.Amount = salesVM.PaidAmount; transactionRecieved.PaymentStatus = PAYMENT_STATUS.CASH_RECIEVED.ToString(); transactionRecieved.TransactionType = TRANSACTION_TYPE.CREDIT.ToString(); _repositoryWrapper.Transaction.Create(transactionRecieved); //TblTransaction transactionRecievable = new TblTransaction(); //transactionRecievable = _utilService.Mapper.Map<SalesVM, TblTransaction>(salesVM); //transactionRecievable.Amount = salesVM.DueAmount; //transactionRecievable.PaymentStatus = PAYMENT_STATUS.CASH_RECIEVABLE.ToString(); //transactionRecievable.TransactionType = TRANSACTION_TYPE.NOT_YET_EXECUTED.ToString(); //transactionRecievable.TransactionId = transactionRecieved.TransactionId; //_repositoryWrapper.Transaction.Create(transactionRecievable); Task <int> Invoice = _repositoryWrapper.Invoice.SaveChangesAsync(); Task <int> Income = _repositoryWrapper.Income.SaveChangesAsync(); Task <int> Recievable = _repositoryWrapper.Recievable.SaveChangesAsync(); Task <int> Sales = _repositoryWrapper.Sales.SaveChangesAsync(); Task <int> Stock = _repositoryWrapper.Stock.SaveChangesAsync(); Task <int> StockOut = _repositoryWrapper.StockOut.SaveChangesAsync(); Task <int> Transaction = _repositoryWrapper.Transaction.SaveChangesAsync(); await Task.WhenAll(Invoice, Income, Recievable, Sales, Stock, StockOut, Transaction); var getDatalistVM = new GetDataListVM() { FactoryId = salesVM.FactoryId, PageNumber = 1, PageSize = 10 }; return(await GetAllSalesAsync(getDatalistVM)); }
public async Task <WrapperPurchaseReturnVM> AddPurchaseReturnAsync(PurchaseReturnVM vm) { // StockOut // Invoice // Recievable // Stock Invoice invoiceToAdd = new Invoice(); invoiceToAdd = _utilService.Mapper.Map <PurchaseReturnVM, Invoice>(vm); _repositoryWrapper.Invoice.Create(invoiceToAdd); vm.InvoiceId = invoiceToAdd.Id; if (!vm.IsFullyPaid) { Recievable RecievableToAdd = new Recievable(); RecievableToAdd = _utilService.Mapper.Map <PurchaseReturnVM, Recievable>(vm); _repositoryWrapper.Recievable.Create(RecievableToAdd); } Stock stockToAdd = new Stock(); stockToAdd = _utilService.Mapper.Map <PurchaseReturnVM, Stock>(vm); // Stock IEnumerable <Stock> stockList = await _repositoryWrapper.Stock.FindByConditionAsync(x => x.FactoryId == vm.FactoryId); Stock existingStock = stockList .ToList() .Where(x => x.ItemId == vm.ItemId && x.FactoryId == vm.FactoryId && x.ItemStatusId == vm.ItemStatusId) .FirstOrDefault(); // IF NOT PRESENT ADD if (existingStock == null) { _utilService.Log("Stock Is Empty. Not Enough Stock available"); throw new StockEmptyException(); //stockToAdd = _utilService.Mapper.Map<SalesItemVM, Stock>(salesVM.ItemList[i]); //_repositoryWrapper.Stock.Create(stockToAdd); } // IF PRESENT UPDATE else { if (existingStock.Quantity < vm.Quantity) { throw new StockEmptyException(); } else { existingStock.Quantity -= vm.Quantity; _repositoryWrapper.Stock.Update(existingStock); } } StockOut stockOutToAdd = new StockOut(); stockOutToAdd = _utilService.Mapper.Map <PurchaseReturnVM, StockOut>(vm); _repositoryWrapper.StockOut.Create(stockOutToAdd); if (vm.AmountRecieved > 0) { TblTransaction tblTransactionToAdd = new TblTransaction(); tblTransactionToAdd = _utilService.Mapper.Map <PurchaseReturnVM, TblTransaction>(vm); _repositoryWrapper.Transaction.Create(tblTransactionToAdd); Income incomeToAdd = new Income(); incomeToAdd = _utilService.Mapper.Map <PurchaseReturnVM, Income>(vm); _repositoryWrapper.Income.Create(incomeToAdd); } Task <int> invT = _repositoryWrapper.Invoice.SaveChangesAsync(); Task <int> stockInT = _repositoryWrapper.StockOut.SaveChangesAsync(); Task <int> stockT = _repositoryWrapper.Stock.SaveChangesAsync(); Task <int> payableT = _repositoryWrapper.Recievable.SaveChangesAsync(); Task <int> transactionT = _repositoryWrapper.Transaction.SaveChangesAsync(); Task <int> incomeT = _repositoryWrapper.Income.SaveChangesAsync(); await Task.WhenAll(invT, stockInT, stockT, payableT, transactionT, incomeT); var data = new GetDataListVM() { FactoryId = vm.FactoryId, PageSize = 15, PageNumber = 1 }; return(await GetAllPurchaseReturnAsync(data)); }