Ejemplo n.º 1
0
        public int SetStatus(int userId, int receiptNoteID, int statusID, int receiptNoteTypeID, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };

            try
            {
                Module.Framework.BLL fwBLL = new Framework.BLL();
                using (var context = CreateContext())
                {
                    int?dbstatusID = context.ReceiptNote.Where(o => o.ReceiptNoteID == receiptNoteID).Select(o => o.StatusID).FirstOrDefault();

                    if (fwBLL.CanPerformAction(userId, "ReceiptNoteMng", Library.DTO.ModuleAction.CanReset))
                    {
                        if (statusID == 1 && dbstatusID == 2) //Confirm 2 Open
                        {
                            if (receiptNoteTypeID == 1)
                            {
                                ValidatePayment(receiptNoteID, 1);
                            }
                            ReceiptNote dbReceiptNote = context.ReceiptNote.Where(o => o.ReceiptNoteID == receiptNoteID).FirstOrDefault();
                            dbReceiptNote.StatusID = 1;
                            context.SaveChanges();
                        }
                        else if (statusID <= dbstatusID)
                        {
                            string newStatusNM = null;
                            if (statusID == 1)
                            {
                                newStatusNM = "Open";
                            }
                            else if (statusID == 2)
                            {
                                newStatusNM = "Confirm";
                            }
                            else
                            {
                                newStatusNM = "Cancel";
                            };

                            string dbStatusNM = null;
                            if (dbstatusID == 1)
                            {
                                dbStatusNM = "Open";
                            }
                            else if (dbstatusID == 2)
                            {
                                dbStatusNM = "Confirm";
                            }
                            else
                            {
                                dbStatusNM = "Cancel";
                            };

                            throw new Exception("Can not set status from " + dbStatusNM + " to " + newStatusNM);
                        }
                    }
                    else
                    {
                        if (statusID <= dbstatusID)
                        {
                            string newStatusNM = null;
                            if (statusID == 1)
                            {
                                newStatusNM = "Open";
                            }
                            else if (statusID == 2)
                            {
                                newStatusNM = "Confirm";
                            }
                            else
                            {
                                newStatusNM = "Cancel";
                            };

                            string dbStatusNM = null;
                            if (dbstatusID == 1)
                            {
                                dbStatusNM = "Open";
                            }
                            else if (dbstatusID == 2)
                            {
                                dbStatusNM = "Confirm";
                            }
                            else
                            {
                                dbStatusNM = "Cancel";
                            };

                            throw new Exception("Can not set status from " + dbStatusNM + " to " + newStatusNM);
                        }
                    }

                    if (statusID == 2) //Confirm
                    {
                        if (receiptNoteTypeID == 1)
                        {
                            ValidatePayment(receiptNoteID, 2);
                        }
                        ReceiptNote dbReceiptNote = context.ReceiptNote.Where(o => o.ReceiptNoteID == receiptNoteID).FirstOrDefault();
                        dbReceiptNote.StatusID   = 2;
                        dbReceiptNote.UpdateBy   = userId;
                        dbReceiptNote.UpdateDate = DateTime.Now;

                        context.SaveChanges();
                    }
                    if (statusID == 3) //Cancel
                    {
                        ReceiptNote dbReceiptNote = context.ReceiptNote.Where(o => o.ReceiptNoteID == receiptNoteID).FirstOrDefault();

                        //if (receiptNoteTypeID == 1)
                        //{
                        //    var dbReceiptNotPurchasing = context.ReceiptNoteInvoice.Where(o => o.ReceiptNoteID == receiptNoteID).ToList();
                        //    foreach (var item in dbReceiptNotPurchasing.ToList())
                        //    {
                        //        PurchasingInvoice dbPurchasingInvoice = context.PurchasingInvoice.Where(o => o.PurchasingInvoiceID == item.PurchasingInvoiceID).FirstOrDefault();
                        //        dbPurchasingInvoice.StatusID = 2;
                        //        dbPurchasingInvoice.FinishDate = null;
                        //    }
                        //}
                        dbReceiptNote.StatusID   = 3;
                        dbReceiptNote.UpdateBy   = userId;
                        dbReceiptNote.UpdateDate = DateTime.Now;
                        context.SaveChanges();
                    }
                }
                return(receiptNoteID);
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = Library.Helper.GetInnerException(ex).Message;
                return(receiptNoteID);
            }
        }
Ejemplo n.º 2
0
        public int SetStatus(int userId, int paymentNoteID, int statusID, int paymentNoteTypeID, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                Module.Framework.BLL fwBLL = new Framework.BLL();
                using (var context = CreateContext())
                {
                    int?dbstatusID = context.PaymentNote.Where(o => o.PaymentNoteID == paymentNoteID).Select(o => o.StatusID).FirstOrDefault();

                    if (fwBLL.CanPerformAction(userId, "PaymentNoteMng", Library.DTO.ModuleAction.CanReset))
                    {
                        if (statusID == 1 && dbstatusID == 2) //Confirm 2 Open
                        {
                            if (paymentNoteTypeID == 1)
                            {
                                ValidatePayment(paymentNoteID, 1);
                            }
                            PaymentNote dbPaymentNote = context.PaymentNote.Where(o => o.PaymentNoteID == paymentNoteID).FirstOrDefault();
                            dbPaymentNote.StatusID = 1;
                            context.SaveChanges();
                        }
                        else if (statusID <= dbstatusID)
                        {
                            string newStatusNM = null;
                            if (statusID == 1)
                            {
                                newStatusNM = "Open";
                            }
                            else if (statusID == 2)
                            {
                                newStatusNM = "Confirm";
                            }
                            else
                            {
                                newStatusNM = "Cancel";
                            };

                            string dbStatusNM = null;
                            if (dbstatusID == 1)
                            {
                                dbStatusNM = "Open";
                            }
                            else if (dbstatusID == 2)
                            {
                                dbStatusNM = "Confirm";
                            }
                            else
                            {
                                dbStatusNM = "Cancel";
                            };

                            throw new Exception("Can not set status from " + dbStatusNM + " to " + newStatusNM);
                        }
                    }
                    else
                    {
                        if (statusID <= dbstatusID)
                        {
                            string newStatusNM = null;
                            if (statusID == 1)
                            {
                                newStatusNM = "Open";
                            }
                            else if (statusID == 2)
                            {
                                newStatusNM = "Confirm";
                            }
                            else
                            {
                                newStatusNM = "Cancel";
                            };

                            string dbStatusNM = null;
                            if (dbstatusID == 1)
                            {
                                dbStatusNM = "Open";
                            }
                            else if (dbstatusID == 2)
                            {
                                dbStatusNM = "Confirm";
                            }
                            else
                            {
                                dbStatusNM = "Cancel";
                            };

                            throw new Exception("Can not set status from " + dbStatusNM + " to " + newStatusNM);
                        }
                    }


                    if (statusID == 2) //Confirm
                    {
                        if (paymentNoteTypeID == 1)
                        {
                            ValidatePayment(paymentNoteID, 2);
                        }
                        PaymentNote dbPaymentNote = context.PaymentNote.Where(o => o.PaymentNoteID == paymentNoteID).FirstOrDefault();
                        dbPaymentNote.StatusID   = 2;
                        dbPaymentNote.UpdateBy   = userId;
                        dbPaymentNote.UpdateDate = DateTime.Now;

                        context.SaveChanges();

                        var listPurchaseInvoice = context.PaymentNoteInvoice.Where(o => o.PaymentNoteID == paymentNoteID).GroupBy(o => o.PurchaseInvoiceID).ToList();

                        foreach (IGrouping <int?, PaymentNoteInvoice> PurchaseInvoice in listPurchaseInvoice)
                        {
                            foreach (var item in PurchaseInvoice)
                            {
                                var checkFinish = context.PaymentNoteMng_PurchaseInvoiceCheckRemain_View.Where(o => o.PurchaseInvoiceID == item.PurchaseInvoiceID).FirstOrDefault();
                                if (checkFinish.Remain <= 0)
                                {
                                    var PurchaseInvoiceData = context.PurchaseInvoice.Where(o => o.PurchaseInvoiceID == item.PurchaseInvoiceID).FirstOrDefault();
                                    PurchaseInvoiceData.PurchaseInvoiceStatusID = 5;
                                    context.SaveChanges();
                                }
                            }
                        }
                        ////Update Supplier Deposit table
                        //if(dbPaymentNote.PaymentNoteSupplier.Count > 0)
                        //{
                        //    var year = dbPaymentNote.PostingDate.Value.Year.ToString();
                        //    foreach(var item in dbPaymentNote.PaymentNoteSupplier)
                        //    {
                        //        var deposit = context.SupplierDeposit.FirstOrDefault(o => o.FactoryRawMaterialID == item.FactoryRawMaterialID && o.Currency == dbPaymentNote.Currency && o.Year == year);
                        //        if(deposit != null)
                        //        {
                        //            deposit.Amount = deposit.Amount + item.Amount;
                        //        }
                        //        else
                        //        {
                        //            SupplierDeposit supplierDeposit = new SupplierDeposit();
                        //            supplierDeposit.Amount = item.Amount;
                        //            supplierDeposit.Currency = dbPaymentNote.Currency;
                        //            supplierDeposit.FactoryRawMaterialID = item.FactoryRawMaterialID;
                        //            supplierDeposit.Year = year;
                        //            context.SupplierDeposit.Add(supplierDeposit);
                        //        }
                        //    }
                        //    context.SaveChanges();
                        //}
                    }
                    if (statusID == 3) //Cancel
                    {
                        PaymentNote dbPaymentNote = context.PaymentNote.Where(o => o.PaymentNoteID == paymentNoteID).FirstOrDefault();

                        if (paymentNoteTypeID == 1)
                        {
                            var dbpaymentNoteInvoice = context.PaymentNoteInvoice.Where(o => o.PaymentNoteID == dbPaymentNote.PaymentNoteID).ToList();
                            foreach (var item in dbpaymentNoteInvoice.ToList())
                            {
                                PurchaseInvoice dbPurchaseInvoice = context.PurchaseInvoice.Where(o => o.PurchaseInvoiceID == item.PurchaseInvoiceID).FirstOrDefault();
                                if (dbPurchaseInvoice != null && dbPurchaseInvoice.PurchaseInvoiceStatusID == 3)
                                {
                                    dbPurchaseInvoice.PurchaseInvoiceStatusID = 2;
                                    dbPurchaseInvoice.SetStatusDate           = DateTime.Now;
                                    dbPurchaseInvoice.FinishDate = null;
                                }
                            }
                        }
                        dbPaymentNote.StatusID   = 3;
                        dbPaymentNote.UpdateBy   = userId;
                        dbPaymentNote.UpdateDate = DateTime.Now;

                        context.SaveChanges();
                    }
                }
                return(paymentNoteID);
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = Library.Helper.GetInnerException(ex).Message;
                return(paymentNoteID);
            }
        }