Example #1
0
        public void DTO2DB(DTO.FactoryProformaInvoice dtoItem, ref FactoryProformaInvoice2 dbItem, string _tempFolder, bool ignoreDetail)
        {
            // map fields
            AutoMapper.Mapper.Map <DTO.FactoryProformaInvoice, FactoryProformaInvoice2>(dtoItem, dbItem);
            if (dtoItem.AttachedFile_HasChanged)
            {
                dbItem.AttachedFile = (new Module.Framework.DAL.DataFactory()).CreateFilePointer(_tempFolder, dtoItem.AttachedFile_NewFile, dtoItem.AttachedFile);
            }
            if (!string.IsNullOrEmpty(dtoItem.InvoiceDate))
            {
                if (DateTime.TryParse(dtoItem.InvoiceDate, cInfo, System.Globalization.DateTimeStyles.None, out tmpDate))
                {
                    dbItem.InvoiceDate = tmpDate;
                }
            }

            // map detail
            if (dtoItem.FactoryProformaInvoiceDetails != null && !ignoreDetail)
            {
                // check for child rows deleted
                foreach (FactoryProformaInvoiceDetail2 dbDetail in dbItem.FactoryProformaInvoiceDetail2.ToArray())
                {
                    if (!dtoItem.FactoryProformaInvoiceDetails.Select(o => o.FactoryProformaInvoiceDetailID).Contains(dbDetail.FactoryProformaInvoiceDetailID))
                    {
                        dbItem.FactoryProformaInvoiceDetail2.Remove(dbDetail);
                    }
                }

                // map child rows
                foreach (DTO.FactoryProformaInvoiceDetail dtoDetail in dtoItem.FactoryProformaInvoiceDetails)
                {
                    FactoryProformaInvoiceDetail2 dbDetail;
                    if (dtoDetail.FactoryProformaInvoiceDetailID <= 0)
                    {
                        dbDetail = new FactoryProformaInvoiceDetail2();
                        dbItem.FactoryProformaInvoiceDetail2.Add(dbDetail);
                    }
                    else
                    {
                        dbDetail = dbItem.FactoryProformaInvoiceDetail2.FirstOrDefault(o => o.FactoryProformaInvoiceDetailID == dtoDetail.FactoryProformaInvoiceDetailID);
                    }

                    if (dbDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.FactoryProformaInvoiceDetail, FactoryProformaInvoiceDetail2>(dtoDetail, dbDetail);
                        if (!string.IsNullOrEmpty(dtoDetail.UpdatedDate))
                        {
                            if (DateTime.TryParse(dtoDetail.UpdatedDate, cInfo, System.Globalization.DateTimeStyles.None, out tmpDate))
                            {
                                dbDetail.UpdatedDate = tmpDate;
                            }
                        }
                    }
                }
            }
        }
Example #2
0
        public bool FactoryConfirm(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.FactoryProformaInvoice dtoFactoryProformaInvoice = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryProformaInvoice>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (FactoryProformaInvoice2MngEntities context = CreateContext())
                {
                    FactoryProformaInvoice2 dbItem = context.FactoryProformaInvoice2.FirstOrDefault(o => o.FactoryProformaInvoiceID == id);
                    if (dbItem == null)
                    {
                        notification.Message = "Factory proforma invoice not found!";
                        return(false);
                    }

                    // check if ok for factory confirm
                    if (dbItem.IsFactoryConfirmed.HasValue && dbItem.IsFactoryConfirmed.Value)
                    {
                        notification.Message = "The proforma invoice is already confirmed by factory!";
                        return(false);
                    }

                    dbItem.IsFactoryConfirmed     = true;
                    dbItem.FactoryConfirmedBy     = userId;
                    dbItem.FactoryConfirmedDate   = DateTime.Now;
                    dbItem.FactoryConfirmedRemark = dtoFactoryProformaInvoice.FactoryConfirmedRemark;
                    context.SaveChanges();

                    dtoItem = GetData(userId, dbItem.FactoryProformaInvoiceID, -1, -1, string.Empty, out notification).Data;
                    return(true);
                }
            }
            catch (Exception ex)
            {
                notification = new Library.DTO.Notification()
                {
                    Message = ex.Message, Type = Library.DTO.NotificationType.Error
                };
                return(false);
            }
        }
Example #3
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.FactoryProformaInvoice dtoFactoryProformaInvoice = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryProformaInvoice>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (FactoryProformaInvoice2MngEntities context = CreateContext())
                {
                    // check if user can update this information
                    if (fwFactory.CheckFactoryPermission(userId, dtoFactoryProformaInvoice.FactoryID.Value) == 0)
                    {
                        throw new Exception("Current user don't have access permission for the selected factory data");
                    }
                    if (id > 0 && fwFactory.CheckFactoryProformaInvoicePermission(userId, id) == 0)
                    {
                        throw new Exception("Current user don't have access permission for the selected proforma invoice data");
                    }

                    FactoryProformaInvoice2 dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new FactoryProformaInvoice2();
                        context.FactoryProformaInvoice2.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.FactoryProformaInvoice2.FirstOrDefault(o => o.FactoryProformaInvoiceID == id);
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "Factory proforma invoice not found!";
                        return(false);
                    }
                    else
                    {
                        // check concurrency
                        if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoFactoryProformaInvoice.ConcurrencyFlag)))
                        {
                            throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT);
                        }

                        // check if item already in other proforma invoice or if item belong to the filter: client, factory, season
                        int    itemID;
                        int    clientID  = dtoFactoryProformaInvoice.ClientID.Value;
                        int    factoryID = dtoFactoryProformaInvoice.FactoryID.Value;
                        string season    = dtoFactoryProformaInvoice.Season;
                        List <DTO.FactoryOrderItemSearchResult> checkList = converter.DB2DTO_FactoryOrderItemSearchResultList(context.FactoryProformaInvoice2Mng_FactoryOrderItemSearchResult_View.Where(o => o.ClientID == clientID && o.FactoryID == factoryID && o.Season == season).ToList());
                        foreach (DTO.FactoryProformaInvoiceDetail dtoDetail in dtoFactoryProformaInvoice.FactoryProformaInvoiceDetails)
                        {
                            if (dtoDetail.FactoryOrderDetailID.HasValue)
                            {
                                itemID = dtoDetail.FactoryOrderDetailID.Value;
                                if (checkList.FirstOrDefault(o => o.FactoryOrderDetailID == itemID) == null)
                                {
                                    throw new Exception(dtoDetail.Description + " is invalid");
                                }
                                if (context.FactoryProformaInvoiceDetail2.FirstOrDefault(o => o.FactoryOrderDetailID == itemID && o.FactoryProformaInvoiceID != id) != null)
                                {
                                    throw new Exception(dtoDetail.Description + "(" + dtoDetail.FactoryOrderUD + " - " + dtoDetail.LDS + ")" + " is already exists in other proforma invoice");
                                }
                            }
                            if (dtoDetail.FactoryOrderSparepartDetailID.HasValue)
                            {
                                itemID = dtoDetail.FactoryOrderSparepartDetailID.Value;
                                if (checkList.FirstOrDefault(o => o.FactoryOrderSparepartDetailID == itemID) == null)
                                {
                                    throw new Exception(dtoDetail.Description + " is invalid");
                                }
                                if (context.FactoryProformaInvoiceDetail2.FirstOrDefault(o => o.FactoryOrderSparepartDetailID == itemID && o.FactoryProformaInvoiceID != id) != null)
                                {
                                    throw new Exception(dtoDetail.Description + "(" + dtoDetail.FactoryOrderUD + " - " + dtoDetail.LDS + ")" + " is already exists in other proforma invoice");
                                }
                            }
                        }

                        if ((dbItem.IsFurnindoConfirmed.HasValue && dbItem.IsFurnindoConfirmed.Value) || (dbItem.IsFactoryConfirmed.HasValue && dbItem.IsFactoryConfirmed.Value))
                        {
                            converter.DTO2DB(dtoFactoryProformaInvoice, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", true);
                        }
                        else
                        {
                            converter.DTO2DB(dtoFactoryProformaInvoice, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", false);
                        }

                        dbItem.UpdatedBy   = userId;
                        dbItem.UpdatedDate = DateTime.Now;

                        // remove orphan
                        context.FactoryProformaInvoiceDetail2.Local.Where(o => o.FactoryProformaInvoice2 == null).ToList().ForEach(o => context.FactoryProformaInvoiceDetail2.Remove(o));

                        // generate p/i number
                        if (id <= 0)
                        {
                            using (DbContextTransaction scope = context.Database.BeginTransaction())
                            {
                                context.Database.ExecuteSqlCommand("SELECT * FROM FactoryProformaInvoice2 WITH (TABLOCKX, HOLDLOCK)");
                                context.Database.ExecuteSqlCommand("SELECT * FROM FactoryProformaInvoiceDetail2 WITH (TABLOCKX, HOLDLOCK)");

                                try
                                {
                                    dbItem.ProformaInvoiceNo = context.FactoryProformaInvoice2Mng_function_GeneratePINumber(dbItem.FactoryID, dbItem.ClientID, dbItem.Season).FirstOrDefault();
                                    context.SaveChanges();
                                }
                                catch (Exception ex)
                                {
                                    throw ex;
                                }
                                finally
                                {
                                    scope.Commit();
                                }
                            }
                        }
                        else
                        {
                            context.SaveChanges();
                        }

                        dtoItem = GetData(userId, dbItem.FactoryProformaInvoiceID, -1, -1, string.Empty, out notification).Data;
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification = new Library.DTO.Notification()
                {
                    Message = ex.Message, Type = Library.DTO.NotificationType.Error
                };
                return(false);
            }
        }