Example #1
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.FactoryInvoice dtoFactoryInvoice = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryInvoice>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            int    number;
            string indexName;

            try
            {
                // check permission
                if (fwFactory.CheckSupplierPermission(userId, dtoFactoryInvoice.SupplierID.Value) == 0)
                {
                    throw new Exception("Current user don't have access permission for the selected supplier data");
                }
                if (id > 0 && fwFactory.CheckFactoryInvoicePermission(userId, id) == 0)
                {
                    throw new Exception("Current user don't have access permission for the selected invoice data");
                }

                using (FactoryInvoiceOtherMngEntities context = CreateContext())
                {
                    FactoryInvoice dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new FactoryInvoice();
                        context.FactoryInvoice.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.FactoryInvoice.FirstOrDefault(o => o.FactoryInvoiceID == id);
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "Factory invoice not found!";
                        return(false);
                    }
                    else
                    {
                        // check if invoice is confirmed
                        if (dbItem.IsConfirmed.HasValue && dbItem.IsConfirmed.Value)
                        {
                            throw new Exception("Can not edit the confirmed invoice!");
                        }

                        // check concurrency
                        if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoFactoryInvoice.ConcurrencyFlag)))
                        {
                            throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT);
                        }

                        converter.DTO2DB(dtoFactoryInvoice, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\");
                        dbItem.UpdatedBy   = userId;
                        dbItem.UpdatedDate = DateTime.Now;
                        context.SaveChanges();
                    }
                    dtoItem = GetData(userId, dbItem.FactoryInvoiceID, -1, out notification).Data;
                    return(true);
                }
            }
            catch (System.Data.DataException dEx)
            {
                notification.Type = Library.DTO.NotificationType.Error;
                Library.ErrorHelper.DataExceptionParser(dEx, out number, out indexName);
                if (number == 2601 && !string.IsNullOrEmpty(indexName))
                {
                    switch (indexName)
                    {
                    case "InvoiceNoUnique":
                        notification.Message = "Duplicate invoice number (invoice no must be unique)!";
                        break;

                    default:
                        notification.Message = dEx.Message;
                        break;
                    }
                }
                else
                {
                    notification.Message = dEx.Message;
                }

                return(false);
            }
            catch (Exception ex)
            {
                notification = new Library.DTO.Notification()
                {
                    Message = ex.Message, Type = Library.DTO.NotificationType.Error
                };
                return(false);
            }
        }
Example #2
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.FactoryInvoice dtoFactoryInvoice = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryInvoice>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            int    number;
            string indexName;

            try
            {
                // check permission
                if (dtoFactoryInvoice.BookingID.HasValue && dtoFactoryInvoice.BookingID.Value > 0 && fwFactory.CheckBookingPermission(userId, dtoFactoryInvoice.BookingID.Value) == 0)
                {
                    throw new Exception("Current user don't have access permission for the selected booking data");
                }
                if (fwFactory.CheckSupplierPermission(userId, dtoFactoryInvoice.SupplierID.Value) == 0)
                {
                    throw new Exception("Current user don't have access permission for the selected supplier data");
                }
                if (id > 0 && fwFactory.CheckFactoryInvoicePermission(userId, id) == 0)
                {
                    throw new Exception("Current user don't have access permission for the selected invoice data");
                }

                using (FactoryInvoiceMngEntities context = CreateContext())
                {
                    FactoryInvoice dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new FactoryInvoice();
                        context.FactoryInvoice.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.FactoryInvoice.FirstOrDefault(o => o.FactoryInvoiceID == id);
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "Factory invoice not found!";
                        return(false);
                    }
                    else
                    {
                        // check if invoice is confirmed
                        if (dbItem.IsConfirmed.HasValue && dbItem.IsConfirmed.Value)
                        {
                            throw new Exception("Can not edit the confirmed invoice!");
                        }

                        // check concurrency
                        if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoFactoryInvoice.ConcurrencyFlag)))
                        {
                            throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT);
                        }

                        // check remain quantity
                        if (dtoFactoryInvoice.BookingID.HasValue)
                        {
                            int bookingID  = dtoFactoryInvoice.BookingID.Value;
                            int supplierID = dtoFactoryInvoice.SupplierID.Value;
                            foreach (DTO.FactoryInvoiceDetail dtoDetail in dtoFactoryInvoice.FactoryInvoiceDetails)
                            {
                                int factoryOrderDetailID = dtoDetail.FactoryOrderDetailID.Value;

                                // check total order
                                FactoryInvoiceMng_FactoryOrderDetailSearchResult_View dtoDetailOrder = context.FactoryInvoiceMng_FactoryOrderDetailSearchResult_View.FirstOrDefault(o => o.FactoryOrderDetailID == factoryOrderDetailID && o.BookingID == bookingID && o.SupplierID == supplierID);
                                if (dtoDetailOrder == null || !dtoDetailOrder.TotalQuantity.HasValue)
                                {
                                    throw new Exception("Booking: " + dtoFactoryInvoice.BookingUD + " not contain item: " + dtoDetail.Description);
                                }

                                FactoryInvoiceMng_IssuedFactoryOrderDetail_View dtoDetailQnt = context.FactoryInvoiceMng_function_CheckOrderDetailQuantity(dtoDetail.FactoryOrderDetailID.Value, dtoFactoryInvoice.BookingID.Value, dtoFactoryInvoice.SupplierID.Value, dtoFactoryInvoice.FactoryInvoiceID).FirstOrDefault();
                                if (dtoDetailQnt != null && dtoDetailQnt.TotalQuantity.HasValue)
                                {
                                    if (dtoDetailOrder.TotalQuantity.Value < dtoDetailQnt.TotalQuantity.Value + dtoDetail.Quantity.Value)
                                    {
                                        throw new Exception("Quantity exceed shipped quantity<br/><u>Product</u>: " + dtoDetail.Description + "<br/><u>Total shipped</u>: " + dtoDetailOrder.TotalQuantity.Value.ToString() + "<br/><u>Total issued</u>: " + dtoDetailQnt.TotalQuantity.Value.ToString() + "<br/><u>Previous invoice</u>: " + dtoDetailQnt.InvoiceNo + "<br/><u>To be issued in this invoice</u>: " + dtoDetail.Quantity.Value.ToString());
                                    }
                                }
                            }

                            foreach (DTO.FactoryInvoiceSparepartDetail dtoSparepartDetail in dtoFactoryInvoice.FactoryInvoiceSparepartDetails)
                            {
                                int factoryOrderSparepartDetailID = dtoSparepartDetail.FactoryOrderSparepartDetailID.Value;

                                // check total order
                                FactoryInvoiceMng_FactoryOrderSparepartDetailSearchResult_View dtoSparepartDetailOrder = context.FactoryInvoiceMng_FactoryOrderSparepartDetailSearchResult_View.FirstOrDefault(o => o.FactoryOrderSparepartDetailID == factoryOrderSparepartDetailID && o.BookingID == bookingID && o.SupplierID == supplierID);
                                if (dtoSparepartDetailOrder == null || !dtoSparepartDetailOrder.TotalQuantity.HasValue)
                                {
                                    throw new Exception("Booking: " + dtoFactoryInvoice.BookingUD + " not contain sparepart: " + dtoSparepartDetailOrder.Description);
                                }

                                FactoryInvoiceMng_IssuedFactoryOrderSparepartDetail_View dtoSparepartDetailQnt = context.FactoryInvoiceMng_function_CheckOrderSparepartDetailQuantity(dtoSparepartDetail.FactoryOrderSparepartDetailID.Value, dtoFactoryInvoice.BookingID.Value, dtoFactoryInvoice.SupplierID.Value, dtoFactoryInvoice.FactoryInvoiceID).FirstOrDefault();
                                if (dtoSparepartDetailQnt != null && dtoSparepartDetailQnt.TotalQuantity.HasValue)
                                {
                                    if (dtoSparepartDetailOrder.TotalQuantity.Value < dtoSparepartDetailQnt.TotalQuantity.Value + dtoSparepartDetail.Quantity.Value)
                                    {
                                        throw new Exception("Quantity exceed shipped quantity<br/><u>Sparepart</u>: " + dtoSparepartDetail.Description + "<br/><u>Total shipped</u>: " + dtoSparepartDetailOrder.TotalQuantity.Value.ToString() + "<br/><u>Total issued</u>: " + dtoSparepartDetailQnt.TotalQuantity.Value.ToString() + "<br/><u>Previous invoice</u>: " + dtoSparepartDetailQnt.InvoiceNo + "<br/><u>To be issued in this invoice</u>: " + dtoSparepartDetail.Quantity.Value.ToString());
                                    }
                                }
                            }
                        }

                        converter.DTO2DB(dtoFactoryInvoice, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\");
                        dbItem.UpdatedBy   = userId;
                        dbItem.UpdatedDate = DateTime.Now;

                        // remove orphan
                        context.FactoryInvoiceDetail.Local.Where(o => o.FactoryInvoice == null).ToList().ForEach(o => context.FactoryInvoiceDetail.Remove(o));
                        context.FactoryInvoiceSparepartDetail.Local.Where(o => o.FactoryInvoice == null).ToList().ForEach(o => context.FactoryInvoiceSparepartDetail.Remove(o));
                        context.FactoryInvoiceExtra.Local.Where(o => o.FactoryInvoice == null).ToList().ForEach(o => context.FactoryInvoiceExtra.Remove(o));

                        context.SaveChanges();
                    }
                    dtoItem = GetData(userId, dbItem.FactoryInvoiceID, -1, -1, string.Empty, out notification).Data;
                    return(true);
                }
            }
            catch (System.Data.DataException dEx)
            {
                notification.Type = Library.DTO.NotificationType.Error;
                Library.ErrorHelper.DataExceptionParser(dEx, out number, out indexName);
                if (number == 2601 && !string.IsNullOrEmpty(indexName))
                {
                    switch (indexName)
                    {
                    case "InvoiceNoUnique":
                        notification.Message = "Duplicate invoice number (invoice no must be unique)!";
                        break;

                    default:
                        notification.Message = dEx.Message;
                        break;
                    }
                }
                else
                {
                    notification.Message = dEx.Message;
                }

                return(false);
            }
            catch (Exception ex)
            {
                notification = new Library.DTO.Notification()
                {
                    Message = ex.Message, Type = Library.DTO.NotificationType.Error
                };
                return(false);
            }
        }