コード例 #1
0
        //Update Zone
        public void DTO2DB_Update(DTO.PaymentNoteEditResult dtoItem, ref PaymentNote dbItem, int userId)
        {
            //From Invoce
            if (dtoItem.PaymentNoteTypeID == 1)
            {
                if (dtoItem.paymentNoteInvoiceResults != null)
                {
                    //remove
                    foreach (var item in dbItem.PaymentNoteInvoice.ToArray())
                    {
                        if (!dtoItem.paymentNoteInvoiceResults.Select(o => o.PaymentNoteInvoiceID).Contains(item.PaymentNoteInvoiceID))
                        {
                            dbItem.PaymentNoteInvoice.Remove(item);
                        }
                    }

                    //Checking And Map
                    foreach (var dtoItemInvoice in dtoItem.paymentNoteInvoiceResults)
                    {
                        PaymentNoteInvoice dbInvoice = new PaymentNoteInvoice();
                        if (dtoItemInvoice.PaymentNoteInvoiceID < 0)
                        {
                            dbItem.PaymentNoteInvoice.Add(dbInvoice);
                        }
                        else
                        {
                            dbInvoice = dbItem.PaymentNoteInvoice.Where(o => o.PaymentNoteInvoiceID == dtoItemInvoice.PaymentNoteInvoiceID).FirstOrDefault();
                        }
                        if (dbInvoice != null)
                        {
                            foreach (var item in dtoItemInvoice.paymentNotePODepositResults)
                            {
                                PaymentNotePODeposit notePODeposit = new PaymentNotePODeposit();
                                if (item.PaymentNotePODepositID < 0)
                                {
                                    dbInvoice.PaymentNotePODeposit.Add(notePODeposit);
                                }
                                else
                                {
                                    PaymentNoteInvoice itemData = dbItem.PaymentNoteInvoice.FirstOrDefault(o => o.PaymentNoteInvoiceID == dtoItemInvoice.PaymentNoteInvoiceID);
                                    if (itemData != null)
                                    {
                                        notePODeposit = itemData.PaymentNotePODeposit.Where(o => o.PaymentNotePODepositID == item.PaymentNotePODepositID).FirstOrDefault();
                                    }
                                }
                                if (notePODeposit != null)
                                {
                                    AutoMapper.Mapper.Map <DTO.PaymentNotePODepositResult, PaymentNotePODeposit>(item, notePODeposit);
                                }
                            }

                            AutoMapper.Mapper.Map <DTO.PaymentNoteInvoiceResult, PaymentNoteInvoice>(dtoItemInvoice, dbInvoice);
                        }
                    }
                }
            }

            //From Supplier
            if (dtoItem.PaymentNoteTypeID == 2)
            {
                if (dtoItem.paymentNoteSupplierResults != null)
                {
                    //remove
                    foreach (var item in dbItem.PaymentNoteSupplier.ToArray())
                    {
                        if (!dtoItem.paymentNoteSupplierResults.Select(o => o.PaymentNoteSupplierID).Contains(item.PaymentNoteSupplierID))
                        {
                            dbItem.PaymentNoteSupplier.Remove(item);
                        }
                    }

                    //Checking And Map
                    foreach (var dtoItemSupplier in dtoItem.paymentNoteSupplierResults)
                    {
                        PaymentNoteSupplier dbClient = new PaymentNoteSupplier();
                        if (dtoItemSupplier.PaymentNoteSupplierID < 0)
                        {
                            dbItem.PaymentNoteSupplier.Add(dbClient);
                        }
                        else
                        {
                            dbClient = dbItem.PaymentNoteSupplier.Where(o => o.PaymentNoteSupplierID == dtoItemSupplier.PaymentNoteSupplierID).FirstOrDefault();
                        }
                        if (dbClient != null)
                        {
                            AutoMapper.Mapper.Map <DTO.PaymentNoteSupplierResult, PaymentNoteSupplier>(dtoItemSupplier, dbClient);
                        }
                    }
                }
            }

            //From other
            if (dtoItem.PaymentNoteTypeID == 3)
            {
                if (dtoItem.paymentNoteOtherResults != null)
                {
                    //remove
                    foreach (var item in dbItem.PaymentNoteOther.ToArray())
                    {
                        if (!dtoItem.paymentNoteOtherResults.Select(o => o.PaymentNoteOtherID).Contains(item.PaymentNoteOtherID))
                        {
                            dbItem.PaymentNoteOther.Remove(item);
                        }
                    }

                    //Checking And Map
                    foreach (var dtoItemOther in dtoItem.paymentNoteOtherResults)
                    {
                        PaymentNoteOther dbOther = new PaymentNoteOther();
                        if (dtoItemOther.PaymentNoteOtherID < 0)
                        {
                            dbItem.PaymentNoteOther.Add(dbOther);
                        }
                        else
                        {
                            dbOther = dbItem.PaymentNoteOther.Where(o => o.PaymentNoteOtherID == dtoItemOther.PaymentNoteOtherID).FirstOrDefault();
                        }
                        if (dbOther != null)
                        {
                            AutoMapper.Mapper.Map <DTO.PaymentNoteOtherResult, PaymentNoteOther>(dtoItemOther, dbOther);
                        }
                    }
                }
            }

            AutoMapper.Mapper.Map <DTO.PaymentNoteEditResult, PaymentNote>(dtoItem, dbItem);

            //Manual mapping
            dbItem.PostingDate     = dtoItem.PostingDate.ConvertStringToDateTime();
            dbItem.PaymentNoteDate = dtoItem.PaymentNoteDate.ConvertStringToDateTime();
        }
コード例 #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);
            }
        }
コード例 #3
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.PaymentNoteEditResult dtoPayment = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.PaymentNoteEditResult>();
            try
            {
                //get companyID
                Module.Framework.DAL.DataFactory fw_factory = new Framework.DAL.DataFactory();
                int?companyID = fw_factory.GetCompanyID(userId);
                using (var context = CreateContext())
                {
                    if (dtoPayment.PaymentNoteTypeID == 3)
                    {
                        foreach (var item in dtoPayment.paymentNoteOtherResults)
                        {
                            if (item.FactoryRawMaterialID == null)
                            {
                                throw new Exception("missing Suppiler !!!");
                            }
                        }
                    }
                    if (dtoPayment.PaymentNoteTypeID == 1)
                    {
                        foreach (var item in dtoPayment.paymentNoteInvoiceResults)
                        {
                            decimal?totalDpsAmount = 0;

                            if (dtoPayment.Currency == "USD" && item.Currency == "VND")
                            {
                                totalDpsAmount = (item.Amount == null ? 0 : item.Amount * dtoPayment.ExchangeRate);
                            }
                            else if (dtoPayment.Currency == "VND" && item.Currency == "USD")
                            {
                                totalDpsAmount = (item.Amount == null ? 0 : item.Amount / dtoPayment.ExchangeRate);
                            }
                            else
                            {
                                totalDpsAmount = (item.Amount == null ? 0 : item.Amount);
                            }
                            if (item.Remain < totalDpsAmount)
                            {
                                throw new Exception("Amount more than over remain " + item.PurchaseInvoiceUD);
                            }
                        }
                    }
                    ///
                    if (dtoPayment.PaymentNoteTypeID == 2)
                    {
                        foreach (var item in dtoPayment.paymentNoteSupplierResults)
                        {
                            var itemx = context.PaymentNoteMng_CheckingCurrencySupplier_View.FirstOrDefault(o => o.PurchaseOrderID == item.PurchaseOrderID);
                            if (itemx != null && itemx.Currency != dtoPayment.Currency)
                            {
                                throw new Exception("There are differences between the two currencies " + item.PurchaseOrderUD);
                            }
                        }
                    }

                    #region Convert
                    //if (dtoPayment.paymentNoteInvoiceResults != null && dtoPayment.PaymentNoteTypeID == 1)
                    //{
                    //    decimal? totalByCurrency = 0;
                    //    foreach (var dtoItemInvoice in dtoPayment.paymentNoteInvoiceResults)
                    //    {
                    //        string currency = context.PaymentNoteMng_PurchaseInvoiceCurrency_View.Where(o => o.PurchaseInvoiceID == dtoItemInvoice.PurchaseInvoiceID).Select(s => s.Currency).FirstOrDefault();
                    //        //If Purchase Invoice null Currency Throw
                    //        if (string.IsNullOrEmpty(currency))
                    //        {
                    //            string purchaseUD = context.PaymentNoteMng_PurchaseInvoiceCurrency_View.Where(o => o.PurchaseInvoiceID == dtoItemInvoice.PurchaseInvoiceID).Select(s => s.PurchaseInvoiceUD).FirstOrDefault();
                    //            throw new Exception("Missing Currency for Invoice " + purchaseUD);
                    //        }
                    //        //Convert USD => VND or
                    //        if (dtoPayment.Currency == "VND")
                    //        {
                    //            if (currency == "VND")
                    //            {
                    //                dtoItemInvoice.Amount = dtoItemInvoice.AmountByCurrency + dtoItemInvoice.Deposit;
                    //            }
                    //            else
                    //            {
                    //                dtoItemInvoice.Amount = ((dtoItemInvoice.AmountByCurrency == null ? 0 : dtoItemInvoice.AmountByCurrency) + (dtoItemInvoice.Deposit == null ? 0 : dtoItemInvoice.Deposit)) / dtoPayment.ExchangeRate;
                    //            }
                    //            totalByCurrency += (dtoItemInvoice.Deposit == null ? 0 : dtoItemInvoice.Deposit);
                    //        }
                    //        else if (dtoPayment.Currency == "USD")
                    //        {
                    //            if (currency == "VND")
                    //            {
                    //                dtoItemInvoice.Amount = (dtoItemInvoice.AmountByCurrency == null ? 0 : dtoItemInvoice.AmountByCurrency) * dtoPayment.ExchangeRate;
                    //            }
                    //            else
                    //            {
                    //                dtoItemInvoice.Amount = dtoItemInvoice.AmountByCurrency + dtoItemInvoice.Deposit;
                    //            }
                    //            totalByCurrency += ((dtoItemInvoice.Deposit == null ? 0 : dtoItemInvoice.Deposit) + (dtoItemInvoice.Deposit == null ? 0 : dtoItemInvoice.Deposit));
                    //        }
                    //    }
                    //    dtoPayment.TotalByCurrency = totalByCurrency;
                    //}
                    #endregion

                    PaymentNote dbItem = null;

                    if (id == 0)
                    {
                        if (dtoPayment.StatusID == 2 || dtoPayment.StatusID == 3)
                        {
                            throw new Exception("Set Status open to Save !!!");
                        }

                        dbItem = new PaymentNote();
                        context.PaymentNote.Add(dbItem);

                        //Automatically generate code
                        if (dtoPayment.PaymentTypeID == 1)// Payment type Cash
                        {
                            int    year            = DateTime.Now.Year;
                            string month           = DateTime.Now.Month.ToString().PadLeft(2, '0');
                            string receipt_pattern = "PC" + "_" + year.ToString().Substring(2) + month;
                            var    db_receiptNo    = context.PaymentNote.Where(o => o.PaymentNoteNo.Substring(0, 7) == receipt_pattern).OrderByDescending(o => o.PaymentNoteNo);
                            if (db_receiptNo.ToList().Count() == 0)
                            {
                                dtoPayment.PaymentNoteNo = receipt_pattern + "_" + "001";
                            }
                            else
                            {
                                var select_receipt = db_receiptNo.FirstOrDefault();
                                int iNo            = Convert.ToInt32(select_receipt.PaymentNoteNo.Substring(8, 3)) + 1;
                                dtoPayment.PaymentNoteNo = select_receipt.PaymentNoteNo.Substring(0, 7) + "_" + iNo.ToString().PadLeft(3, '0');
                            }
                        }
                        else if (dtoPayment.PaymentTypeID == 2)// Payment type Bank
                        {
                            int    year            = DateTime.Now.Year;
                            string month           = DateTime.Now.Month.ToString().PadLeft(2, '0');
                            string receipt_pattern = "BN" + "_" + year.ToString().Substring(2) + month;
                            var    db_receiptNo    = context.PaymentNote.Where(o => o.PaymentNoteNo.Substring(0, 7) == receipt_pattern).OrderByDescending(o => o.PaymentNoteNo);
                            if (db_receiptNo.ToList().Count() == 0)
                            {
                                dtoPayment.PaymentNoteNo = receipt_pattern + "_" + "001";
                            }
                            else
                            {
                                var select_receipt = db_receiptNo.FirstOrDefault();
                                int iNo            = Convert.ToInt32(select_receipt.PaymentNoteNo.Substring(8, 3)) + 1;
                                dtoPayment.PaymentNoteNo = select_receipt.PaymentNoteNo.Substring(0, 7) + "_" + iNo.ToString().PadLeft(3, '0');
                            }
                        }
                    }
                    else
                    {
                        dbItem = context.PaymentNote.Where(o => o.PaymentNoteID == id).FirstOrDefault();

                        if (dbItem.PaymentNoteTypeID != dtoPayment.PaymentNoteTypeID)
                        {
                            throw new Exception("Can't change Payment Note type !!!");
                        }
                    }
                    if (dbItem == null)
                    {
                        notification.Message = "data not found!";
                        return(false);
                    }
                    else
                    {
                        //check  status

                        /*
                         *  WorkOrderStatusID :
                         *      1 : Open
                         *      2 : Confimred
                         */

                        if (dbItem.StatusID == 2 || dbItem.StatusID == 3)
                        {
                            throw new Exception("Can't Update because Payment Comfirmed or Cancel !!!");
                        }
                        if (dtoPayment.StatusID == 2 && dtoPayment.PaymentNoteTypeID == 1)
                        {
                            ValidatePayment(dbItem.PaymentNoteID, 2);
                        }

                        //convert dto to db
                        converter.DTO2DB_Update(dtoPayment, ref dbItem, userId);

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

                        if (id == 0)
                        {
                            dbItem.CreateDate = DateTime.Now;
                            dbItem.CreateBy   = userId;
                        }
                        else
                        {
                            dbItem.UpdateDate = DateTime.Now;
                            dbItem.UpdateBy   = userId;
                        }

                        //remove orphan
                        context.PaymentNoteSupplier.Local.Where(o => o.PaymentNote == null).ToList().ForEach(o => context.PaymentNoteSupplier.Remove(o));
                        context.PaymentNoteInvoice.Local.Where(o => o.PaymentNote == null).ToList().ForEach(o => context.PaymentNoteInvoice.Remove(o));
                        context.PaymentNoteOther.Local.Where(o => o.PaymentNote == null).ToList().ForEach(o => context.PaymentNoteOther.Remove(o));
                        context.PaymentNotePODeposit.Local.Where(o => o.PaymentNoteInvoice == null).ToList().ForEach(o => context.PaymentNotePODeposit.Remove(o));

                        //save data
                        context.SaveChanges();
                        dtoItem = GetData(userId, dbItem.PaymentNoteID, null, out notification).Data;
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = Library.Helper.GetInnerException(ex).Message;
                return(false);
            }
        }