Exemplo n.º 1
0
 public override bool DeleteData(int userId, int id, out Notification notification)
 {
     notification = new Library.DTO.Notification {
         Type = Library.DTO.NotificationType.Success
     };
     try
     {
         using (PurchaseInvoiceMngEntities context = CreateContext())
         {
             var dbItem = context.PurchaseInvoice.Where(o => o.PurchaseInvoiceID == id).FirstOrDefault();
             context.PurchaseInvoice.Remove(dbItem);
             context.SaveChanges();
         }
         return(true);
     }
     catch (Exception ex)
     {
         notification.Type    = Library.DTO.NotificationType.Error;
         notification.Message = ex.Message;
         notification.DetailMessage.Add(ex.Message);
         if (ex.GetBaseException() != null)
         {
             notification.DetailMessage.Add(ex.GetBaseException().Message);
         }
         return(false);
     }
 }
Exemplo n.º 2
0
        public int Cancel(int userId, int purchaseInvoiceID, int purchaseInvoiceStatusID, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            int?oldPurchaseInvoiceStatusID = null;

            try
            {
                using (PurchaseInvoiceMngEntities context = CreateContext())
                {
                    var purchaseInvoiceStatuses = context.SupportMng_PurchaseInvoiceStatus_View.ToList();
                    var currentPurchaseInvoice  = context.PurchaseInvoice.Where(o => o.PurchaseInvoiceID == purchaseInvoiceID).FirstOrDefault();
                    oldPurchaseInvoiceStatusID = currentPurchaseInvoice.PurchaseInvoiceStatusID;

                    //check valid status
                    int?oldDisplayOrder = purchaseInvoiceStatuses.Where(o => o.PurchaseInvoiceStatusID == oldPurchaseInvoiceStatusID).FirstOrDefault().DisplayOrder;
                    int?newDisplayOrder = purchaseInvoiceStatuses.Where(o => o.PurchaseInvoiceStatusID == purchaseInvoiceStatusID).FirstOrDefault().DisplayOrder;

                    if (oldDisplayOrder == 2)
                    {
                        notification.Message = purchaseInvoiceStatuses.Where(o => o.PurchaseInvoiceStatusID == 4).FirstOrDefault().PurchaseInvoiceStatusNM + " success !!!";
                        currentPurchaseInvoice.PurchaseInvoiceStatusID = 4;
                        currentPurchaseInvoice.SetStatusBy             = userId;
                        currentPurchaseInvoice.SetStatusDate           = DateTime.Now;
                        context.SaveChanges();
                    }
                    else
                    {
                        if (oldDisplayOrder == 3)
                        {
                            if (context.PaymentNoteInvoice.Where(o => o.PurchaseInvoiceID == purchaseInvoiceID).Count() > 0)
                            {
                                throw new Exception("Invoice has been created Payment Note. Can not cancel");
                            }
                            else
                            {
                                notification.Message = purchaseInvoiceStatuses.Where(o => o.PurchaseInvoiceStatusID == 4).FirstOrDefault().PurchaseInvoiceStatusNM + " success !!!";
                                currentPurchaseInvoice.PurchaseInvoiceStatusID = 4;
                                currentPurchaseInvoice.SetStatusBy             = userId;
                                currentPurchaseInvoice.SetStatusDate           = DateTime.Now;
                                context.SaveChanges();
                            }
                        }
                    }
                    return(purchaseInvoiceID);
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = Library.Helper.GetInnerException(ex).Message;
                return(oldPurchaseInvoiceStatusID.Value);
            }
        }
Exemplo n.º 3
0
        public List <DTO.PaymentTermDTO> GetSupplierPaymentTerm(int factoryRawMaterialID, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            var dbItems = new List <PurchaseInvoiceMng_SupplierPaymentTerm_View>();

            using (PurchaseInvoiceMngEntities context = CreateContext())
            {
                dbItems = context.PurchaseInvoiceMng_SupplierPaymentTerm_View.Where(o => o.FactoryRawMaterialID == factoryRawMaterialID).ToList();
            }
            return(AutoMapper.Mapper.Map <List <PurchaseInvoiceMng_SupplierPaymentTerm_View>, List <DTO.PaymentTermDTO> >(dbItems));
        }
Exemplo n.º 4
0
 public override EditFormData GetData(int userId, int id, Hashtable param, out Notification notification)
 {
     DTO.EditFormData editFormData = new DTO.EditFormData();
     notification = new Library.DTO.Notification()
     {
         Type = Library.DTO.NotificationType.Success
     };
     try
     {
         using (PurchaseInvoiceMngEntities context = CreateContext())
         {
             if (id > 0)
             {
                 var dbItem = context.PurchaseInvoiceMng_PurchaseInvoice_View.Where(o => o.PurchaseInvoiceID == id).FirstOrDefault();
                 editFormData.Data = converter.DB2DTO_PurchaseInvoice(dbItem);
             }
             else
             {
                 int typeID = Convert.ToInt32(param["typeID"]);
                 if (typeID > 0)
                 {
                     editFormData.Data.PurchaseInvoiceTypeID = typeID;
                     editFormData.Data.PurchaseInvoiceTypeNM = context.SupportMng_PurchaseInvoiceType_View.Where(o => o.PurchaseInvoiceTypeID == typeID).FirstOrDefault().PurchaseInvoiceTypeNM;
                 }
                 //initialize
                 editFormData.Data.PurchaseInvoiceStatusID = 1;
                 editFormData.Data.VAT = 0;
                 editFormData.Data.PurchaseInvoiceDate = DateTime.Now.ToString("dd/MM/yyyy");
                 editFormData.Data.PostingDate         = DateTime.Now.ToString("dd/MM/yyyy");
                 editFormData.Data.CreatedDate         = DateTime.Now.ToString("dd/MM/yyyy");
             }
             //get support data
             editFormData.PurchaseInvoiceStatusDTOs = AutoMapper.Mapper.Map <List <SupportMng_PurchaseInvoiceStatus_View>, List <DTO.PurchaseInvoiceStatusDTO> >(context.SupportMng_PurchaseInvoiceStatus_View.ToList());
             editFormData.PurchaseInvoiceTypeDTOs   = AutoMapper.Mapper.Map <List <SupportMng_PurchaseInvoiceType_View>, List <DTO.PurchaseInvoiceTypeDTO> >(context.SupportMng_PurchaseInvoiceType_View.ToList());
             editFormData.FactoryWarehouseDTOs      = AutoMapper.Mapper.Map <List <PurchaseInvoiceMng_FactoryWarehouse_View>, List <DTO.FactoryWarehouseDTO> >(context.PurchaseInvoiceMng_FactoryWarehouse_View.ToList());
             editFormData.FactoryRawMaterialDTOs    = converter.DB2DTO_GetFactoryRawMaterial(context.PurchaseInvoiceMng_GetFactoryRawMaterial_View.ToList());
             editFormData.PaymentTermDTOs           = AutoMapper.Mapper.Map <List <PurchaseInvoiceMng_SupplierPaymentTerm_View>, List <DTO.PaymentTermDTO> >(context.PurchaseInvoiceMng_SupplierPaymentTerm_View.Where(o => o.FactoryRawMaterialID == editFormData.Data.FactoryRawMaterialID).ToList());
         }
     }
     catch (Exception ex)
     {
         notification.Type    = Library.DTO.NotificationType.Error;
         notification.Message = Library.Helper.GetInnerException(ex).Message;
     }
     return(editFormData);
 }
Exemplo n.º 5
0
        public List <DTO.ProductionItemDTO> GetProductionItem(int userId, Hashtable filters)
        {
            List <DTO.ProductionItemDTO> result = new List <DTO.ProductionItemDTO>();

            using (PurchaseInvoiceMngEntities context = CreateContext())
            {
                Module.Framework.DAL.DataFactory fw_factory = new Framework.DAL.DataFactory();
                string searchQuery = filters["searchQuery"].ToString();
                int?   type        = Convert.ToInt32(filters["itemType"]);
                var    dbItems     = new List <PurchaseInvoiceMng_ProductionItem_View>();
                if (type == 0)
                {
                    dbItems = context.PurchaseInvoiceMng_ProductionItem_View.Where(o => (o.ProductionItemNM.Contains(searchQuery) || o.ProductionItemUD.Contains(searchQuery))).ToList();
                }
                if (type == 1)
                {
                    dbItems = context.PurchaseInvoiceMng_ProductionItem_View.Where(o => o.ProductionItemTypeID == 7 && (o.ProductionItemNM.Contains(searchQuery) || o.ProductionItemUD.Contains(searchQuery))).ToList();
                }
                return(AutoMapper.Mapper.Map <List <PurchaseInvoiceMng_ProductionItem_View>, List <DTO.ProductionItemDTO> >(dbItems));
            }
        }
Exemplo n.º 6
0
        public DTO.PurchaseOrderSearchFromData GetPurchaseOrderData(int userId, System.Collections.Hashtable filters, int pageSize, int pageIndex, string orderBy, string orderDirection, out int totalRows, out Library.DTO.Notification notification)
        {
            DTO.PurchaseOrderSearchFromData data = new DTO.PurchaseOrderSearchFromData();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            totalRows = 0;
            try
            {
                string receivingNoteUD      = null;
                string purchaseOrderUD      = null;
                string purchaseOrderDate    = null;
                string productionItemUD     = null;
                string productionItemNM     = null;
                int?   factoryRawMaterialID = null;
                string FromDate             = null;
                string ToDate = null;

                if (filters.ContainsKey("receivingNoteUD") && !string.IsNullOrEmpty(filters["receivingNoteUD"].ToString()))
                {
                    receivingNoteUD = filters["receivingNoteUD"].ToString().Replace("'", "''");
                }
                if (filters.ContainsKey("purchaseOrderUD") && !string.IsNullOrEmpty(filters["purchaseOrderUD"].ToString()))
                {
                    purchaseOrderUD = filters["purchaseOrderUD"].ToString().Replace("'", "''");
                }
                if (filters.ContainsKey("purchaseOrderDate") && !string.IsNullOrEmpty(filters["purchaseOrderDate"].ToString()))
                {
                    purchaseOrderDate = filters["purchaseOrderDate"].ToString().Replace("'", "''");
                }
                if (filters.ContainsKey("productionItemUD") && !string.IsNullOrEmpty(filters["productionItemUD"].ToString()))
                {
                    productionItemUD = filters["productionItemUD"].ToString().Replace("'", "''");
                }
                if (filters.ContainsKey("productionItemNM") && !string.IsNullOrEmpty(filters["productionItemNM"].ToString()))
                {
                    productionItemNM = filters["productionItemNM"].ToString().Replace("'", "''");
                }
                if (filters.ContainsKey("factoryRawMaterialID") && !string.IsNullOrEmpty(filters["factoryRawMaterialID"].ToString()))
                {
                    factoryRawMaterialID = Convert.ToInt32(filters["factoryRawMaterialID"].ToString());
                }
                if (filters.ContainsKey("FromDate") && !string.IsNullOrEmpty(filters["FromDate"].ToString()))
                {
                    FromDate = filters["FromDate"].ToString().Replace("'", "''");
                }
                if (filters.ContainsKey("ToDate") && !string.IsNullOrEmpty(filters["ToDate"].ToString()))
                {
                    ToDate = filters["ToDate"].ToString().Replace("'", "''");
                }
                DateTime?valpurchaseOrderDate = purchaseOrderDate.ConvertStringToDateTime();
                DateTime?valFromDate          = FromDate.ConvertStringToDateTime();
                DateTime?valToDate            = ToDate.ConvertStringToDateTime();
                using (PurchaseInvoiceMngEntities context = CreateContext())
                {
                    totalRows = context.PurchaseInvoiceMng_function_SearchPurchaseOrderItem(orderBy, orderDirection, purchaseOrderUD, valpurchaseOrderDate, productionItemUD, productionItemNM, factoryRawMaterialID, receivingNoteUD, valFromDate, valToDate).Count();
                    var result = context.PurchaseInvoiceMng_function_SearchPurchaseOrderItem(orderBy, orderDirection, purchaseOrderUD, valpurchaseOrderDate, productionItemUD, productionItemNM, factoryRawMaterialID, receivingNoteUD, valFromDate, valToDate).OrderByDescending(o => o.PurchaseOrderUD).OrderByDescending(o => o.PurchaseOrderDate);
                    data.Data      = converter.DB2DTO_SearchPurchaseOrderData(result.Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList());
                    data.TotalRows = totalRows;
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = Library.Helper.GetInnerException(ex).Message;
            }
            return(data);
        }
Exemplo n.º 7
0
        //Status
        public int SetPurchaseInvoiceStatus(int userId, int purchaseInvoiceID, int purchaseInvoiceStatusID, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            int?oldPurchaseInvoiceStatusID = null;

            try
            {
                using (PurchaseInvoiceMngEntities context = CreateContext())
                {
                    var purchaseInvoiceStatuses = context.SupportMng_PurchaseInvoiceStatus_View.ToList();
                    var currentPurchaseInvoice  = context.PurchaseInvoice.Where(o => o.PurchaseInvoiceID == purchaseInvoiceID).FirstOrDefault();
                    oldPurchaseInvoiceStatusID = currentPurchaseInvoice.PurchaseInvoiceStatusID;
                    Hashtable input = new Hashtable();
                    DTO.PurchaseInvoiceDTO dtoItem = GetData(userId, currentPurchaseInvoice.PurchaseInvoiceID, input, out notification).Data;


                    //check valid status
                    int?oldDisplayOrder = purchaseInvoiceStatuses.Where(o => o.PurchaseInvoiceStatusID == oldPurchaseInvoiceStatusID).FirstOrDefault().DisplayOrder;
                    int?newDisplayOrder = purchaseInvoiceStatuses.Where(o => o.PurchaseInvoiceStatusID == purchaseInvoiceStatusID).FirstOrDefault().DisplayOrder;

                    if (newDisplayOrder <= oldDisplayOrder)
                    {
                        string currentStatusName = purchaseInvoiceStatuses.Where(o => o.PurchaseInvoiceStatusID == oldPurchaseInvoiceStatusID).FirstOrDefault().PurchaseInvoiceStatusNM;
                        string newStatusName     = purchaseInvoiceStatuses.Where(o => o.PurchaseInvoiceStatusID == purchaseInvoiceStatusID).FirstOrDefault().PurchaseInvoiceStatusNM;
                        throw new Exception("Can not set status from " + currentStatusName + " to " + newStatusName);
                    }
                    else
                    {
                        if (newDisplayOrder == 2)
                        {
                            var checkConfirm = false;
                            var itemName     = "";
                            if (dtoItem.PurchaseInvoiceTypeID == 1)
                            {
                                decimal?remain = 0;
                                decimal?total  = 0;
                                for (int i = 0; i < dtoItem.PurchaseInvoiceDetailDTOs.Count; i++)
                                {
                                    remain = dtoItem.PurchaseInvoiceDetailDTOs[i].POReceivedQnt;
                                    total  = total + dtoItem.PurchaseInvoiceDetailDTOs[i].Quantity;

                                    if ((i + 1) >= dtoItem.PurchaseInvoiceDetailDTOs.Count || dtoItem.PurchaseInvoiceDetailDTOs[i + 1].PurchaseOrderID != dtoItem.PurchaseInvoiceDetailDTOs[i].PurchaseOrderID || dtoItem.PurchaseInvoiceDetailDTOs[i + 1].ProductionItemID != dtoItem.PurchaseInvoiceDetailDTOs[i].ProductionItemID)
                                    {
                                        if (total > remain)
                                        {
                                            checkConfirm = true;
                                            itemName     = dtoItem.PurchaseInvoiceDetailDTOs[i].ProductionItemNM;
                                            break;
                                        }
                                        else
                                        {
                                            remain = 0;
                                            total  = 0;
                                        }
                                    }
                                }
                            }
                            if (checkConfirm)
                            {
                                throw new Exception(itemName + " has Quantity larger than ReceivedQnt, Can not confirm!");
                            }
                            else
                            {
                                notification.Message = purchaseInvoiceStatuses.Where(o => o.PurchaseInvoiceStatusID == purchaseInvoiceStatusID).FirstOrDefault().PurchaseInvoiceStatusNM + " success !!!";
                                currentPurchaseInvoice.PurchaseInvoiceStatusID = purchaseInvoiceStatusID;
                                currentPurchaseInvoice.SetStatusBy             = userId;
                                currentPurchaseInvoice.SetStatusDate           = DateTime.Now;
                                context.SaveChanges();
                            }
                        }
                        else
                        {
                            if (newDisplayOrder == 3)
                            {
                                var paymentNoteInvoices  = context.PurchaseInvoiceMng_ListPaymentNoteInvoice_View.ToList();
                                var checkInvoiceNoCancel = false;
                                foreach (var item in paymentNoteInvoices)
                                {
                                    if (item.PurchaseInvoiceID == purchaseInvoiceID)
                                    {
                                        if (item.StatusID != 3)
                                        {
                                            checkInvoiceNoCancel = true;
                                            break;
                                        }
                                    }
                                }
                                if (checkInvoiceNoCancel == true)
                                {
                                    throw new Exception("This Purchasing Invoice exists in Payment Note and The status is not Cancel");
                                }
                                notification.Message = purchaseInvoiceStatuses.Where(o => o.PurchaseInvoiceStatusID == purchaseInvoiceStatusID).FirstOrDefault().PurchaseInvoiceStatusNM + " success !!!";
                                currentPurchaseInvoice.PurchaseInvoiceStatusID = purchaseInvoiceStatusID;
                                currentPurchaseInvoice.SetStatusBy             = userId;
                                currentPurchaseInvoice.SetStatusDate           = DateTime.Now;
                                currentPurchaseInvoice.FinishDate = DateTime.Now;
                                context.SaveChanges();
                            }
                            else
                            {
                                notification.Message = purchaseInvoiceStatuses.Where(o => o.PurchaseInvoiceStatusID == purchaseInvoiceStatusID).FirstOrDefault().PurchaseInvoiceStatusNM + " success !!!";
                                currentPurchaseInvoice.PurchaseInvoiceStatusID = purchaseInvoiceStatusID;
                                currentPurchaseInvoice.SetStatusBy             = userId;
                                currentPurchaseInvoice.SetStatusDate           = DateTime.Now;
                                context.SaveChanges();
                            }
                        }
                    }
                    return(purchaseInvoiceStatusID);
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = Library.Helper.GetInnerException(ex).Message;
                return(oldPurchaseInvoiceStatusID.Value);
            }
        }
Exemplo n.º 8
0
        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.PurchaseInvoiceDTO dtoPurchaseInvoice = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.PurchaseInvoiceDTO>();
            try
            {
                using (PurchaseInvoiceMngEntities context = CreateContext())
                {
                    PurchaseInvoice dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new PurchaseInvoice();
                        context.PurchaseInvoice.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.PurchaseInvoice.Where(o => o.PurchaseInvoiceID == id).FirstOrDefault();
                    }
                    if (dbItem == null)
                    {
                        notification.Message = "data not found!";
                        return(false);
                    }
                    else
                    {
                        if (!dtoPurchaseInvoice.PurchaseInvoiceTypeID.HasValue)
                        {
                            throw new Exception("Please select type of purchasing invoice");
                        }

                        //upload file
                        Module.Framework.DAL.DataFactory fwFactory = new Module.Framework.DAL.DataFactory();
                        string tempFolder = FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\";
                        if (dtoPurchaseInvoice.File_HasChange.HasValue && dtoPurchaseInvoice.File_HasChange.Value)
                        {
                            dtoPurchaseInvoice.AttachedFile = fwFactory.CreateFilePointer(tempFolder, dtoPurchaseInvoice.File_NewFile, dtoPurchaseInvoice.AttachedFile, dtoPurchaseInvoice.FriendlyName);
                        }
                        //convert dto to db
                        converter.DTO2DB_PurchaseInvoice(dtoPurchaseInvoice, ref dbItem, userId);

                        if (id == 0)
                        {
                            dbItem.PurchaseInvoiceStatusID = 1;
                            dbItem.SetStatusBy             = userId;
                            dbItem.SetStatusDate           = DateTime.Now;
                        }
                        //remove orphan
                        context.PurchaseInvoiceDetail.Local.Where(o => o.PurchaseInvoice == null).ToList().ForEach(o => context.PurchaseInvoiceDetail.Remove(o));

                        //save data
                        context.SaveChanges();

                        //generate purchase invoice code
                        if (string.IsNullOrEmpty(dbItem.PurchaseInvoiceUD))
                        {
                            context.PurchaseInvoiceMng_function_GeneratePurchaseInvoiceUD(dbItem.PurchaseInvoiceID, dbItem.PurchaseInvoiceDate.Value.Year, dbItem.PurchaseInvoiceDate.Value.Month);
                        }

                        //get return data
                        dtoItem = GetData(userId, dbItem.PurchaseInvoiceID, null, out notification).Data;
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = Library.Helper.GetInnerException(ex).Message;
                return(false);
            }
        }
Exemplo n.º 9
0
        public override SearchFormData GetDataWithFilter(int userId, Hashtable filters, int pageSize, int pageIndex, string orderBy, string orderDirection, out int totalRows, out Notification notification)
        {
            DTO.SearchFormData searchFormData = new DTO.SearchFormData();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            totalRows = 0;
            try
            {
                string purchaseInvoiceUD       = null;
                string purchaseInvoiceDate     = null;
                string postingDate             = null;
                int?   factoryRawMaterialID    = null;
                int?   purchaseInvoiceStatusID = null;
                int?   purchaseInvoiceTypeID   = null;
                string invoiceNo = null;

                if (filters.ContainsKey("purchaseInvoiceUD") && !string.IsNullOrEmpty(filters["purchaseInvoiceUD"].ToString()))
                {
                    purchaseInvoiceUD = filters["purchaseInvoiceUD"].ToString().Replace("'", "''");
                }

                if (filters.ContainsKey("purchaseInvoiceDate") && !string.IsNullOrEmpty(filters["purchaseInvoiceDate"].ToString()))
                {
                    purchaseInvoiceDate = filters["purchaseInvoiceDate"].ToString().Replace("'", "''");
                }

                if (filters.ContainsKey("postingDate") && !string.IsNullOrEmpty(filters["postingDate"].ToString()))
                {
                    postingDate = filters["postingDate"].ToString().Replace("'", "''");
                }

                if (filters.ContainsKey("factoryRawMaterialID") && filters["factoryRawMaterialID"] != null)
                {
                    factoryRawMaterialID = Convert.ToInt32(filters["factoryRawMaterialID"]);
                }

                if (filters.ContainsKey("purchaseInvoiceStatusID") && filters["purchaseInvoiceStatusID"] != null)
                {
                    purchaseInvoiceStatusID = Convert.ToInt32(filters["purchaseInvoiceStatusID"]);
                }

                if (filters.ContainsKey("purchaseInvoiceTypeID") && filters["purchaseInvoiceTypeID"] != null)
                {
                    purchaseInvoiceTypeID = Convert.ToInt32(filters["purchaseInvoiceTypeID"]);
                }

                if (filters.ContainsKey("invoiceNo") && !string.IsNullOrEmpty(filters["invoiceNo"].ToString()))
                {
                    invoiceNo = filters["invoiceNo"].ToString().Replace("'", "''");
                }
                DateTime?valpurchaseInvoiceDate = purchaseInvoiceDate.ConvertStringToDateTime();
                DateTime?valpostingDate         = postingDate.ConvertStringToDateTime();
                using (PurchaseInvoiceMngEntities context = CreateContext())
                {
                    var result1 = context.PurchaseInvoiceMng_function_SearchPurchaseInvoice(purchaseInvoiceUD, valpurchaseInvoiceDate, valpostingDate, factoryRawMaterialID, purchaseInvoiceStatusID, purchaseInvoiceTypeID, invoiceNo, orderBy, orderDirection).ToList();
                    totalRows = result1.Count();
                    if (totalRows > 0)
                    {
                        searchFormData.TotalAmountDTO.totalAmountVND    = result1.Where(x => x.Currency == "VND").Sum(x => x.Amount);
                        searchFormData.TotalAmountDTO.totalAMountUSD    = result1.Where(x => x.Currency == "USD").Sum(x => x.Amount);
                        searchFormData.TotalAmountDTO.totalVATAmount    = result1.Sum(x => x.VATAmount);
                        searchFormData.TotalAmountDTO.totalAllAmountVND = searchFormData.TotalAmountDTO.totalVATAmount + searchFormData.TotalAmountDTO.totalAmountVND + (searchFormData.TotalAmountDTO.totalAMountUSD * result1[0].ExchangeRate);
                    }

                    var result = context.PurchaseInvoiceMng_function_SearchPurchaseInvoice(purchaseInvoiceUD, valpurchaseInvoiceDate, valpostingDate, factoryRawMaterialID, purchaseInvoiceStatusID, purchaseInvoiceTypeID, invoiceNo, orderBy, orderDirection);
                    searchFormData.Data = converter.DB2DTO_Search(result.Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList());
                    //get support list
                    searchFormData.PurchaseInvoiceStatusDTOs = AutoMapper.Mapper.Map <List <SupportMng_PurchaseInvoiceStatus_View>, List <DTO.PurchaseInvoiceStatusDTO> >(context.SupportMng_PurchaseInvoiceStatus_View.ToList());
                    searchFormData.PurchaseInvoiceTypeDTOs   = AutoMapper.Mapper.Map <List <SupportMng_PurchaseInvoiceType_View>, List <DTO.PurchaseInvoiceTypeDTO> >(context.SupportMng_PurchaseInvoiceType_View.ToList());
                    searchFormData.FactoryRawMaterialDTOs    = AutoMapper.Mapper.Map <List <PurchaseInvoiceMng_GetFactoryRawMaterial_View>, List <DTO.FactoryRawMaterialDTO> >(context.PurchaseInvoiceMng_GetFactoryRawMaterial_View.ToList());
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = Library.Helper.GetInnerException(ex).Message;
            }
            return(searchFormData);
        }