public async Task <IActionResult> OnGetAsync(int?companyFilter, int?section, int?copyFromId) { RoutedCompanyId = (companyFilter ?? 0); RoutedSectionId = (section ?? 0); CopyFromId = (copyFromId ?? 0); if (CopyFromId > 0) { var sellMatDoc = await _context.SellDocuments .Include(b => b.Company) .Include(b => b.FiscalPeriod) .Include(b => b.SellDocSeries) .Include(b => b.SellDocType) .Include(b => b.Section) .Include(b => b.Transactor) .Include(b => b.SellDocLines) .ThenInclude(m => m.WarehouseItem) .FirstOrDefaultAsync(m => m.Id == CopyFromId); if (sellMatDoc == null) { return(NotFound()); } //ItemVm = _mapper.Map<BuyDocCreateAjaxDto>(buyMatDoc); CopyFromItemVm = _mapper.Map <SellDocModifyDto>(sellMatDoc); if (CopyFromItemVm != null) { ItemVm = new SellDocCreateAjaxDto() { AmountDiscount = CopyFromItemVm.AmountDiscount, AmountFpa = CopyFromItemVm.AmountFpa, AmountNet = CopyFromItemVm.AmountNet, SellDocSeriesId = CopyFromItemVm.SellDocSeriesId, CompanyId = CopyFromItemVm.CompanyId, Etiology = CopyFromItemVm.Etiology, PaymentMethodId = CopyFromItemVm.PaymentMethodId, SalesChannelId = CopyFromItemVm.SalesChannelId, TransactorId = CopyFromItemVm.TransactorId }; } } LoadCombos(); return(Page()); }
private async Task <IActionResult> CreateSellDocFromRecTrans(SellDocCreateAjaxDto data, RecurringTransDoc recurringTransDoc) { const string sectionCode = "SYS-SELL-COMBINED-SCN"; bool noWarehouseTrans = false; SellDocCreateAjaxNoLinesDto transToAttachNoLines; SellDocument transToAttach; DateTime dateOfTrans; if (data == null) { return(BadRequest(new { error = "Empty request data" })); } try { transToAttachNoLines = _mapper.Map <SellDocCreateAjaxNoLinesDto>(data); transToAttach = _mapper.Map <SellDocument>(transToAttachNoLines); transToAttach.SalesChannelId = 1; dateOfTrans = data.TransDate; } catch (Exception e) { return(BadRequest(new { error = e.Message })); } using (var transaction = _context.Database.BeginTransaction()) { #region Fiscal Period var fiscalPeriod = await _context.FiscalPeriods.FirstOrDefaultAsync(p => dateOfTrans >= p.StartDate && dateOfTrans <= p.EndDate); if (fiscalPeriod == null) { transaction.Rollback(); ModelState.AddModelError(string.Empty, "No Fiscal Period covers Transaction Date"); return(NotFound(new { error = "No Fiscal Period covers Transaction Date" })); } #endregion var docSeries = await _context.SellDocSeriesDefs.SingleOrDefaultAsync(m => m.Id == data.SellDocSeriesId); if (docSeries is null) { transaction.Rollback(); ModelState.AddModelError(string.Empty, "Δεν βρέθηκε η σειρά παραστατικού"); return(NotFound(new { error = "Buy Doc Series not found" })); } await _context.Entry(docSeries).Reference(t => t.SellDocTypeDef).LoadAsync(); var docTypeDef = docSeries.SellDocTypeDef; await _context.Entry(docTypeDef) .Reference(t => t.TransTransactorDef) .LoadAsync(); await _context.Entry(docTypeDef).Reference(t => t.TransWarehouseDef) .LoadAsync(); #region Section Management int sectionId = 0; if (docTypeDef.SectionId == 0) { var sectn = await _context.Sections.SingleOrDefaultAsync(s => s.SystemName == sectionCode); if (sectn == null) { transaction.Rollback(); ModelState.AddModelError(string.Empty, "Δεν υπάρχει το Section"); return(NotFound(new { error = "Could not locate section " })); } sectionId = sectn.Id; } else { sectionId = docTypeDef.SectionId; } #endregion var transTransactorDef = docTypeDef.TransTransactorDef; var transWarehouseDef = docTypeDef.TransWarehouseDef; transToAttach.SectionId = sectionId; transToAttach.FiscalPeriodId = fiscalPeriod.Id; transToAttach.SellDocTypeId = docSeries.SellDocTypeDefId; _context.SellDocuments.Add(transToAttach); try { await _context.SaveChangesAsync(); } catch (Exception e) { Console.WriteLine(e); transaction.Rollback(); string msg = e.InnerException.Message; return(BadRequest(new { error = e.Message + " " + msg })); } var docId = _context.Entry(transToAttach).Entity.Id; if (transTransactorDef.DefaultDocSeriesId > 0) { var transTransactorDefaultSeries = await _context.TransTransactorDocSeriesDefs.FirstOrDefaultAsync(p => p.Id == transTransactorDef.DefaultDocSeriesId); if (transTransactorDefaultSeries == null) { transaction.Rollback(); ModelState.AddModelError(string.Empty, "Default series for transactor transaction not found"); return(NotFound(new { error = "Default series for transactor transaction not found" })); } var sTransactorTransaction = _mapper.Map <TransactorTransaction>(data); sTransactorTransaction.TransactorId = data.TransactorId; sTransactorTransaction.SectionId = sectionId; sTransactorTransaction.TransTransactorDocTypeId = transTransactorDefaultSeries.TransTransactorDocTypeDefId; sTransactorTransaction.TransTransactorDocSeriesId = transTransactorDefaultSeries.Id; sTransactorTransaction.FiscalPeriodId = fiscalPeriod.Id; sTransactorTransaction.CreatorId = docId; ActionHandlers.TransactorFinAction(transTransactorDef.FinancialTransAction, sTransactorTransaction); _context.TransactorTransactions.Add(sTransactorTransaction); try { await _context.SaveChangesAsync(); } catch (Exception e) { transaction.Rollback(); string msg = e.InnerException.Message; return(BadRequest(new { error = e.Message + " " + msg })); } } #region Αυτόματη Εξόφληση //Αυτόματη εξόφληση var paymentMethod = await _context.PaymentMethods.FirstOrDefaultAsync(p => p.Id == transToAttach.PaymentMethodId); if (paymentMethod is null) { transaction.Rollback(); ModelState.AddModelError(string.Empty, "Δεν βρέθηκε ο τρόπος πληρωμής"); return(NotFound(new { error = "Δεν βρέθηκε ο τρόπος πληρωμής" })); } if (paymentMethod.AutoPayoffWay == SeriesAutoPayoffEnum.SeriesAutoPayoffEnumAuto) { var autoPaySeriesId = transToAttach.SellDocSeries.PayoffSeriesId; if (autoPaySeriesId > 0) { var transTransactorPayOffSeries = await _context.TransTransactorDocSeriesDefs.FirstOrDefaultAsync(p => p.Id == autoPaySeriesId); if (transTransactorPayOffSeries == null) { transaction.Rollback(); ModelState.AddModelError(string.Empty, "AutoPayOff series not found"); return(NotFound(new { error = "AutoPayOff series not found" })); } var sTransactorTransaction = _mapper.Map <TransactorTransaction>(data); sTransactorTransaction.TransactorId = data.TransactorId; sTransactorTransaction.SectionId = sectionId; sTransactorTransaction.TransTransactorDocTypeId = transTransactorPayOffSeries.TransTransactorDocTypeDefId; sTransactorTransaction.TransTransactorDocSeriesId = transTransactorPayOffSeries.Id; sTransactorTransaction.FiscalPeriodId = fiscalPeriod.Id; sTransactorTransaction.Etiology = "AutoPayOff"; sTransactorTransaction.CreatorId = docId; await _context.Entry(transTransactorPayOffSeries) .Reference(t => t.TransTransactorDocTypeDef) .LoadAsync(); var transTransactorDocTypeDef = transTransactorPayOffSeries.TransTransactorDocTypeDef; await _context.Entry(transTransactorDocTypeDef) .Reference(t => t.TransTransactorDef) .LoadAsync(); var transPaymentTransactorDef = transTransactorDocTypeDef.TransTransactorDef; ActionHandlers.TransactorFinAction(transPaymentTransactorDef.FinancialTransAction, sTransactorTransaction); _context.TransactorTransactions.Add(sTransactorTransaction); try { await _context.SaveChangesAsync(); } catch (Exception e) { transaction.Rollback(); string msg = e.InnerException.Message; return(BadRequest(new { error = e.Message + " " + msg })); } } } #endregion int warehouseSeriesId = 0; int warehouseTypeId = 0; if (transWarehouseDef.DefaultDocSeriesId > 0) { var transWarehouseDefaultSeries = await _context.TransWarehouseDocSeriesDefs.FirstOrDefaultAsync(p => p.Id == transWarehouseDef.DefaultDocSeriesId); if (transWarehouseDefaultSeries == null) { transaction.Rollback(); ModelState.AddModelError(string.Empty, "Default series for warehouse transaction not found"); return(NotFound(new { error = "Default series for warehouse transaction not found" })); } noWarehouseTrans = false; warehouseSeriesId = transWarehouseDef.DefaultDocSeriesId; warehouseTypeId = transWarehouseDefaultSeries.TransWarehouseDocTypeDefId; } else { noWarehouseTrans = true; } foreach (var docLine in data.SellDocLines) { var warehouseItemId = docLine.WarehouseItemId; var material = await _context.WarehouseItems.SingleOrDefaultAsync(p => p.Id == warehouseItemId); if (material is null) { //Handle error transaction.Rollback(); ModelState.AddModelError(string.Empty, "Doc Line error null WarehouseItem"); return(NotFound(new { error = "Could not locate material in Doc Line " })); } #region MaterialLine var sellDocLine = new SellDocLine(); decimal unitPrice = docLine.Price; decimal units = (decimal)docLine.Q1; decimal fpaRate = (decimal)docLine.FpaRate; decimal discountRate = (decimal)docLine.DiscountRate; decimal lineNetAmount = unitPrice * units; decimal lineDiscountAmount = lineNetAmount * discountRate; decimal lineFpaAmount = (lineNetAmount - lineDiscountAmount) * fpaRate; sellDocLine.UnitPrice = unitPrice; sellDocLine.AmountFpa = lineFpaAmount; sellDocLine.AmountNet = lineNetAmount; sellDocLine.AmountDiscount = lineDiscountAmount; sellDocLine.DiscountRate = discountRate; sellDocLine.FpaRate = fpaRate; sellDocLine.WarehouseItemId = docLine.WarehouseItemId; sellDocLine.Quontity1 = docLine.Q1; sellDocLine.Quontity2 = docLine.Q2; sellDocLine.PrimaryUnitId = docLine.MainUnitId; sellDocLine.SecondaryUnitId = docLine.SecUnitId; sellDocLine.Factor = docLine.Factor; sellDocLine.SellDocumentId = docId; sellDocLine.Etiology = transToAttach.Etiology; //_context.Entry(transToAttach).Entity transToAttach.SellDocLines.Add(sellDocLine); #endregion if (!noWarehouseTrans) { #region Warehouse transaction var warehouseTrans = new WarehouseTransaction(); warehouseTrans.FpaRate = fpaRate; warehouseTrans.DiscountRate = discountRate; warehouseTrans.UnitPrice = unitPrice; warehouseTrans.AmountDiscount = lineDiscountAmount; warehouseTrans.AmountNet = lineNetAmount; warehouseTrans.AmountFpa = lineFpaAmount; warehouseTrans.CompanyId = transToAttach.CompanyId; warehouseTrans.Etiology = transToAttach.Etiology; warehouseTrans.FiscalPeriodId = transToAttach.FiscalPeriodId; warehouseTrans.WarehouseItemId = warehouseItemId; warehouseTrans.PrimaryUnitId = docLine.MainUnitId; warehouseTrans.SecondaryUnitId = docLine.SecUnitId; warehouseTrans.SectionId = sectionId; warehouseTrans.CreatorId = transToAttach.Id; warehouseTrans.TransDate = transToAttach.TransDate; warehouseTrans.TransRefCode = transToAttach.TransRefCode; warehouseTrans.UnitFactor = (decimal)docLine.Factor; warehouseTrans.TransWarehouseDocSeriesId = warehouseSeriesId; warehouseTrans.TransWarehouseDocTypeId = warehouseTypeId; ActionHandlers.ItemNatureHandler(material.WarehouseItemNature, warehouseTrans, transWarehouseDef); ActionHandlers.ItemInventoryActionHandler(warehouseTrans.InventoryAction, docLine.Q1, docLine.Q2, warehouseTrans); ActionHandlers.ItemInventoryValueActionHandler(warehouseTrans.InventoryValueAction, warehouseTrans); _context.WarehouseTransactions.Add(warehouseTrans); #endregion } } try { await _context.SaveChangesAsync(); } catch (Exception e) { transaction.Rollback(); string msg = e.InnerException.Message; return(BadRequest(new { error = e.Message + " " + msg })); } var curDate = recurringTransDoc.NextTransDate; var nextDate = GetNextTransactionDate(curDate, recurringTransDoc.RecurringFrequency); recurringTransDoc.NextTransDate = nextDate; _context.Entry(recurringTransDoc).State = EntityState.Modified; try { await _context.SaveChangesAsync(); transaction.Commit(); } catch (Exception e) { transaction.Rollback(); string msg = e.InnerException.Message; return(BadRequest(new { error = e.Message + " " + msg })); } } return(Ok()); }