public void DTO2DB_FactorySaleInvoice(DTO.FactorySaleInvoiceDTO dtoItem, ref FactorySaleInvoice dbItem, int userId) { if (dtoItem.FactorySaleInvoiceDetailDTOs != null) { foreach (var item in dbItem.FactorySaleInvoiceDetail.ToArray()) { if (!dtoItem.FactorySaleInvoiceDetailDTOs.Select(s => s.FactorySaleInvoiceDetailID).Contains(item.FactorySaleInvoiceDetailID)) { dbItem.FactorySaleInvoiceDetail.Remove(item); } } foreach (var item in dtoItem.FactorySaleInvoiceDetailDTOs) { FactorySaleInvoiceDetail dbDetail = new FactorySaleInvoiceDetail(); if (item.FactorySaleInvoiceDetailID < 0) { dbItem.FactorySaleInvoiceDetail.Add(dbDetail); } else { dbDetail = dbItem.FactorySaleInvoiceDetail.Where(s => s.FactorySaleInvoiceDetailID == item.FactorySaleInvoiceDetailID).FirstOrDefault(); } if (dbDetail != null) { AutoMapper.Mapper.Map <DTO.FactorySaleInvoiceDetailDTO, FactorySaleInvoiceDetail>(item, dbDetail); } } } AutoMapper.Mapper.Map <DTO.FactorySaleInvoiceDTO, FactorySaleInvoice>(dtoItem, dbItem); if (!string.IsNullOrEmpty(dtoItem.InvoiceDate)) { dbItem.InvoiceDate = dtoItem.InvoiceDate.ConvertStringToDateTime(); } if (!string.IsNullOrEmpty(dtoItem.PostingDate)) { dbItem.PostingDate = dtoItem.PostingDate.ConvertStringToDateTime(); } if (dbItem.FactorySaleInvoiceID > 0) { dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; } else { dbItem.CreatedBy = userId; dbItem.CreatedDate = DateTime.Now; } }
//Status public int SetFactorySaleInvoiceStatus(int userId, int factorySaleInvoiceID, int factorySaleInvoiceStatusID, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; int?oldFactorySaleInvoiceStatusID = null; try { using (FactorySaleInvoiceEntities context = CreateContext()) { var purchaseInvoiceStatuses = context.SupportMng_FactorySaleInvoiceStatus_View.ToList(); var currentFactorySaleInvoice = context.FactorySaleInvoice.Where(o => o.FactorySaleInvoiceID == factorySaleInvoiceID).FirstOrDefault(); oldFactorySaleInvoiceStatusID = currentFactorySaleInvoice.FactorySaleInvoiceStatusID; Hashtable input = new Hashtable(); DTO.FactorySaleInvoiceDTO dtoItem = GetData(userId, currentFactorySaleInvoice.FactorySaleInvoiceID, input, out notification).Data; //check valid status int?oldDisplayOrder = purchaseInvoiceStatuses.Where(o => o.FactorySaleInvoiceStatusID == oldFactorySaleInvoiceStatusID).FirstOrDefault().DisplayOrder; int?newDisplayOrder = purchaseInvoiceStatuses.Where(o => o.FactorySaleInvoiceStatusID == factorySaleInvoiceStatusID).FirstOrDefault().DisplayOrder; if (newDisplayOrder <= oldDisplayOrder) { var checkinvoice = context.ReceiptNoteSaleInvoice.Where(o => o.FactorySaleInvoiceID == factorySaleInvoiceID).Count(); if (newDisplayOrder == 1 && oldDisplayOrder == 2 && checkinvoice == 0) { notification.Message = purchaseInvoiceStatuses.Where(o => o.FactorySaleInvoiceStatusID == factorySaleInvoiceStatusID).FirstOrDefault().FactorySaleInvoiceStatusNM + " success !!!"; currentFactorySaleInvoice.FactorySaleInvoiceStatusID = factorySaleInvoiceStatusID; currentFactorySaleInvoice.UpdatedBy = userId; currentFactorySaleInvoice.UpdatedDate = DateTime.Now; context.SaveChanges(); } else { string currentStatusName = purchaseInvoiceStatuses.Where(o => o.FactorySaleInvoiceStatusID == oldFactorySaleInvoiceStatusID).FirstOrDefault().FactorySaleInvoiceStatusNM; string newStatusName = purchaseInvoiceStatuses.Where(o => o.FactorySaleInvoiceStatusID == factorySaleInvoiceStatusID).FirstOrDefault().FactorySaleInvoiceStatusNM; if (checkinvoice > 0) { throw new Exception("Invoice has been created Receipt Note .Can not set status from " + currentStatusName + " to " + newStatusName); } else { throw new Exception("Can not set status from " + currentStatusName + " to " + newStatusName); } } } else { if (newDisplayOrder == 2) { notification.Message = purchaseInvoiceStatuses.Where(o => o.FactorySaleInvoiceStatusID == factorySaleInvoiceStatusID).FirstOrDefault().FactorySaleInvoiceStatusNM + " success !!!"; currentFactorySaleInvoice.FactorySaleInvoiceStatusID = factorySaleInvoiceStatusID; //currentFactorySaleInvoice.SetStatusBy = userId; //currentFactorySaleInvoice.SetStatusDate = DateTime.Now; currentFactorySaleInvoice.UpdatedBy = userId; currentFactorySaleInvoice.UpdatedDate = DateTime.Now; context.SaveChanges(); } else { if (newDisplayOrder == 3) { notification.Message = purchaseInvoiceStatuses.Where(o => o.FactorySaleInvoiceStatusID == factorySaleInvoiceStatusID).FirstOrDefault().FactorySaleInvoiceStatusNM + " success !!!"; currentFactorySaleInvoice.FactorySaleInvoiceStatusID = factorySaleInvoiceStatusID; //currentFactorySaleInvoice.SetStatusBy = userId; //currentFactorySaleInvoice.SetStatusDate = DateTime.Now; //currentFactorySaleInvoice.FinishDate = DateTime.Now; currentFactorySaleInvoice.UpdatedBy = userId; currentFactorySaleInvoice.UpdatedDate = DateTime.Now; context.SaveChanges(); } else { notification.Message = purchaseInvoiceStatuses.Where(o => o.FactorySaleInvoiceStatusID == factorySaleInvoiceStatusID).FirstOrDefault().FactorySaleInvoiceStatusNM + " success !!!"; currentFactorySaleInvoice.FactorySaleInvoiceStatusID = factorySaleInvoiceStatusID; //currentFactorySaleInvoice.SetStatusBy = userId; //currentFactorySaleInvoice.SetStatusDate = DateTime.Now; currentFactorySaleInvoice.UpdatedBy = userId; currentFactorySaleInvoice.UpdatedDate = DateTime.Now; context.SaveChanges(); } } } return(factorySaleInvoiceStatusID); } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = Library.Helper.GetInnerException(ex).Message; return(oldFactorySaleInvoiceStatusID.Value); } }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; DTO.FactorySaleInvoiceDTO dtoFactorySaleInvoice = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactorySaleInvoiceDTO>(); try { using (FactorySaleInvoiceEntities context = CreateContext()) { FactorySaleInvoice dbItem = null; if (id == 0) { dbItem = new FactorySaleInvoice(); context.FactorySaleInvoice.Add(dbItem); } else { dbItem = context.FactorySaleInvoice.Where(o => o.FactorySaleInvoiceID == id).FirstOrDefault(); } if (dbItem == null) { notification.Message = "data not found!"; return(false); } else { //upload file Module.Framework.DAL.DataFactory fwFactory = new Module.Framework.DAL.DataFactory(); string tempFolder = FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\"; if (dtoFactorySaleInvoice.File_HasChange.HasValue && dtoFactorySaleInvoice.File_HasChange.Value) { dtoFactorySaleInvoice.AttachedFile = fwFactory.CreateFilePointer(tempFolder, dtoFactorySaleInvoice.File_NewFile, dtoFactorySaleInvoice.AttachedFile, dtoFactorySaleInvoice.FriendlyName); } //convert dto to db converter.DTO2DB_FactorySaleInvoice(dtoFactorySaleInvoice, ref dbItem, userId); if (id == 0) { dbItem.FactorySaleInvoiceStatusID = 1; //dbItem.SetStatusBy = userId; //dbItem.SetStatusDate = DateTime.Now; } //remove orphan context.FactorySaleInvoiceDetail.Local.Where(o => o.FactorySaleInvoice == null).ToList().ForEach(o => context.FactorySaleInvoiceDetail.Remove(o)); //save data context.SaveChanges(); //generate purchase invoice code if (string.IsNullOrEmpty(dbItem.DocCode)) { context.FactorySaleInvoice_function_GenerateFactorySaleInvoiceUD(dbItem.FactorySaleInvoiceID, dbItem.InvoiceDate.Value.Year, dbItem.InvoiceDate.Value.Month); } //get return data dtoItem = GetData(userId, dbItem.FactorySaleInvoiceID, null, out notification).Data; return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = Library.Helper.GetInnerException(ex).Message; return(false); } }