Esempio n. 1
0
        //
        // CUSTOM FUNCTION
        //
        public string GetExcelReportData(int userId, int supplierId, string season, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            ReportDataObject ds = new ReportDataObject();

            try
            {
                // check permission
                if (fwFactory.CheckSupplierPermission(userId, supplierId) == 0)
                {
                    throw new Exception("Current user don't have access permission for the selected supplier data");
                }

                //SqlDataAdapter adap = new SqlDataAdapter();
                //adap.SelectCommand = new SqlCommand("FactoryPaymentOverviewRpt_function_getDetailData", new SqlConnection(Library.Helper.GetSQLConnectionString()));
                //adap.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;
                //adap.SelectCommand.Parameters.AddWithValue("@SupplierID", supplierId);
                //adap.SelectCommand.Parameters.AddWithValue("@Season", season);
                //adap.Fill(ds.FactoryPaymentOverviewRpt_function_getDetailData);

                //ReportDataObject.ReportHeaderRow row = ds.ReportHeader.NewReportHeaderRow();
                //row.Season = season;
                //Support.DTO.Supplier dtoSupplier = supportFactory.GetSupplier(userId).FirstOrDefault(o => o.SupplierID == supplierId);
                //if (dtoSupplier != null)
                //{
                //    row.SupplierUD = dtoSupplier.SupplierUD;
                //    row.SupplierNM = dtoSupplier.SupplierNM;
                //}
                //ds.ReportHeader.AddReportHeaderRow(row);
                //ds.AcceptChanges();

                // dev
                //Library.Helper.DevCreateReportXMLSource(ds, "FactoryProformaInvoiceOverview");
                //return string.Empty;

                // generate xml file
                return(Library.Helper.CreateCOMReportFile(ds, "FactoryProformaInvoiceOverview"));
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                if (ex.InnerException != null && !string.IsNullOrEmpty(ex.InnerException.Message))
                {
                    notification.DetailMessage.Add(ex.InnerException.Message);
                }
                return(string.Empty);
            }
        }
Esempio n. 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 (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);
            }
        }
Esempio n. 3
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);
            }
        }
Esempio n. 4
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.FactoryPayment dtoFactoryPayment = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryPayment>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            int    number;
            string indexName;

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

                using (FactoryPayment2MngEntities context = CreateContext())
                {
                    FactoryPayment2 dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new FactoryPayment2();
                        context.FactoryPayment2.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.FactoryPayment2.FirstOrDefault(o => o.FactoryPayment2ID == id);
                    }

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

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

                        // validata data
                        if (dbItem.ConfirmedAmount.HasValue && dbItem.TotalAmount.HasValue && dbItem.TotalAmount < dbItem.ConfirmedAmount)
                        {
                            throw new Exception("Confirmed received amount greater than total paid amount!");
                        }
                        if (fwFactory.IsDPBalanceClosed(userId, dtoFactoryPayment.SupplierID.Value, dtoFactoryPayment.Season))
                        {
                            throw new Exception("Balance for season: " + dbItem.Season + " is closed or you dont have access permission for the balance data!");
                        }

                        using (DbContextTransaction scope = context.Database.BeginTransaction())
                        {
                            context.Database.ExecuteSqlCommand("SELECT * FROM FactoryPayment2 WITH (TABLOCKX, HOLDLOCK); SELECT * FROM FactoryPayment2Balance WITH (TABLOCKX, HOLDLOCK); SELECT * FROM FactoryPayment2Detail WITH (TABLOCKX, HOLDLOCK);");

                            try
                            {
                                converter.DTO2DB(dtoFactoryPayment, ref dbItem);

                                decimal remainDPAmount        = context.FactoryPayment2Mng_function_GetRemainDPAmount(dbItem.Season, dbItem.SupplierID).FirstOrDefault().Value;
                                decimal totalDPDeductedAmount = dbItem.FactoryPayment2Detail.Where(o => o.DPDeductedAmount.HasValue).Sum(o => o.DPDeductedAmount.Value);
                                if (totalDPDeductedAmount > 0 && totalDPDeductedAmount > remainDPAmount)
                                {
                                    throw new Exception("DP deducted amount (" + totalDPDeductedAmount.ToString() + ") larger than remain DP amount (" + remainDPAmount.ToString() + ")!");
                                }
                                dbItem.UpdatedBy   = userId;
                                dbItem.UpdatedDate = DateTime.Now;

                                // remove orphan
                                context.FactoryPayment2Detail.Local.Where(o => o.FactoryPayment2 == null).ToList().ForEach(o => context.FactoryPayment2Detail.Remove(o));
                                context.SaveChanges();

                                dbItem.ReceiptNo = Library.Common.Helper.formatIndex(dbItem.FactoryPayment2ID.ToString(), 8, "0");
                                context.SaveChanges();
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                            finally
                            {
                                scope.Commit();
                            }
                        }
                    }
                    dtoItem = GetData(userId, dbItem.FactoryPayment2ID, -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 "ReceipNoUnique":
                        notification.Message = "Duplicate payment receipt number (payment receipt number 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);
            }
        }