Esempio 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);
            }
        }
Esempio n. 2
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.ReceiptNoteEditResult dtoReceipt = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.ReceiptNoteEditResult>();
            try
            {
                //get companyID
                Module.Framework.DAL.DataFactory fw_factory = new Framework.DAL.DataFactory();
                int?companyID = fw_factory.GetCompanyID(userId);
                using (var context = CreateContext())
                {
                    //Check Client
                    if (dtoReceipt.ReceiptNoteTypeID == 2)
                    {
                        foreach (var item in dtoReceipt.receiptNoteClientResults)
                        {
                            if (item.FactoryRawMaterialID == null)
                            {
                                throw new Exception("missing Client !!!");
                            }
                        }
                    }
                    //if (dtoReceipt.ReceiptNoteTypeID == 3)
                    //{
                    //    foreach (var item in dtoReceipt.receiptNoteOtherResults)
                    //    {
                    //        if (item.EmployeeID == null)
                    //        {
                    //            throw new Exception("missing Client !!!");
                    //        }
                    //    }
                    //}

                    if (dtoReceipt.ReceiptNoteTypeID == 4 || dtoReceipt.ReceiptNoteTypeID == 3)
                    {
                        dtoReceipt.SupplierID = null;
                        dtoReceipt.SupplierNM = null;
                        dtoReceipt.SupplierUD = null;
                    }

                    ReceiptNote dbItem = null;

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

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

                        //Automatically generate code
                        if (dtoReceipt.ReceiveTypeID == 1)// Receive type Cash
                        {
                            int    year            = DateTime.Now.Year;
                            string month           = DateTime.Now.Month.ToString().PadLeft(2, '0');
                            string receipt_pattern = "PT" + "_" + year.ToString().Substring(2) + month;
                            var    db_receiptNo    = context.ReceiptNote.Where(o => o.ReceiptNoteNo.Substring(0, 7) == receipt_pattern).OrderByDescending(o => o.ReceiptNoteNo);
                            if (db_receiptNo.ToList().Count() == 0)
                            {
                                dtoReceipt.ReceiptNoteNo = receipt_pattern + "_" + "001";
                            }
                            else
                            {
                                var select_receipt = db_receiptNo.FirstOrDefault();
                                int iNo            = Convert.ToInt32(select_receipt.ReceiptNoteNo.Substring(8, 3)) + 1;
                                dtoReceipt.ReceiptNoteNo = select_receipt.ReceiptNoteNo.Substring(0, 7) + "_" + iNo.ToString().PadLeft(3, '0');
                            }
                        }
                        else if (dtoReceipt.ReceiveTypeID == 2)// Receive type Bank
                        {
                            int    year            = DateTime.Now.Year;
                            string month           = DateTime.Now.Month.ToString().PadLeft(2, '0');
                            string receipt_pattern = "BC" + "_" + year.ToString().Substring(2) + month;
                            var    db_receiptNo    = context.ReceiptNote.Where(o => o.ReceiptNoteNo.Substring(0, 7) == receipt_pattern).OrderByDescending(o => o.ReceiptNoteNo);
                            if (db_receiptNo.ToList().Count() == 0)
                            {
                                dtoReceipt.ReceiptNoteNo = receipt_pattern + "_" + "001";
                            }
                            else
                            {
                                var select_receipt = db_receiptNo.FirstOrDefault();
                                int iNo            = Convert.ToInt32(select_receipt.ReceiptNoteNo.Substring(8, 3)) + 1;
                                dtoReceipt.ReceiptNoteNo = select_receipt.ReceiptNoteNo.Substring(0, 7) + "_" + iNo.ToString().PadLeft(3, '0');
                            }
                        }
                    }
                    else
                    {
                        dbItem = context.ReceiptNote.Where(o => o.ReceiptNoteID == id).FirstOrDefault();

                        if (dbItem.ReceiptNoteTypeID != dtoReceipt.ReceiptNoteTypeID)
                        {
                            throw new Exception("Can't change Receipt 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 receipt comfirmed !!!");
                        }

                        if (dtoReceipt.StatusID == 2 && dtoReceipt.ReceiptNoteTypeID == 1)
                        {
                            ValidatePayment(dbItem.ReceiptNoteID, 2);
                        }

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

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

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

                        //remove orphan
                        context.ReceiptNoteClient.Local.Where(o => o.ReceiptNote == null).ToList().ForEach(o => context.ReceiptNoteClient.Remove(o));
                        context.ReceiptNoteInvoice.Local.Where(o => o.ReceiptNote == null).ToList().ForEach(o => context.ReceiptNoteInvoice.Remove(o));
                        context.ReceiptNoteOther.Local.Where(o => o.ReceiptNote == null).ToList().ForEach(o => context.ReceiptNoteOther.Remove(o));

                        //save data
                        context.SaveChanges();
                        dtoItem = GetData(userId, dbItem.ReceiptNoteID, null, out notification).Data;
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = Library.Helper.GetInnerException(ex).Message;
                return(false);
            }
        }
Esempio n. 3
0
        //Update Zone
        public void DTO2DB_Update(DTO.ReceiptNoteEditResult dtoItem, ref ReceiptNote dbItem, int userId)
        {
            decimal?totalByCurrency = 0;

            //From Invoce
            if (dtoItem.ReceiptNoteTypeID == 1)
            {
                if (dtoItem.receiptNoteInvoiceResults != null)
                {
                    //remove
                    foreach (var item in dbItem.ReceiptNoteInvoice.ToArray())
                    {
                        if (!dtoItem.receiptNoteInvoiceResults.Select(o => o.ReceiptNoteInvoiceID).Contains(item.ReceiptNoteInvoiceID))
                        {
                            dbItem.ReceiptNoteInvoice.Remove(item);
                        }
                    }

                    //Checking And Map
                    foreach (var dtoItemInvoice in dtoItem.receiptNoteInvoiceResults)
                    {
                        ReceiptNoteInvoice dbInvoice = new ReceiptNoteInvoice();
                        if (dtoItemInvoice.ReceiptNoteInvoiceID < 0)
                        {
                            dbItem.ReceiptNoteInvoice.Add(dbInvoice);
                        }
                        else
                        {
                            dbInvoice = dbItem.ReceiptNoteInvoice.Where(o => o.ReceiptNoteInvoiceID == dtoItemInvoice.ReceiptNoteInvoiceID).FirstOrDefault();
                        }
                        if (dbInvoice != null)
                        {
                            if (dtoItem.Currency == "VND")
                            {
                                totalByCurrency += (dtoItemInvoice.AmountByCurrency == null ? 0 : dtoItemInvoice.AmountByCurrency);

                                dtoItemInvoice.Amount = (dtoItemInvoice.AmountByCurrency == null ? 0 : dtoItemInvoice.AmountByCurrency) / dtoItem.ExchangeRate;
                            }
                            else
                            {
                                dtoItemInvoice.Amount = dtoItemInvoice.AmountByCurrency;
                            }
                            AutoMapper.Mapper.Map <DTO.ReceiptNoteInvoiceResult, ReceiptNoteInvoice>(dtoItemInvoice, dbInvoice);
                        }
                    }
                }
            }

            //From Client
            if (dtoItem.ReceiptNoteTypeID == 2)
            {
                if (dtoItem.receiptNoteClientResults != null)
                {
                    //remove
                    foreach (var item in dbItem.ReceiptNoteClient.ToArray())
                    {
                        if (!dtoItem.receiptNoteClientResults.Select(o => o.ReceiptNoteClientID).Contains(item.ReceiptNoteClientID))
                        {
                            dbItem.ReceiptNoteClient.Remove(item);
                        }
                    }

                    //Checking And Map
                    foreach (var dtoItemClient in dtoItem.receiptNoteClientResults)
                    {
                        ReceiptNoteClient dbClient = new ReceiptNoteClient();
                        if (dtoItemClient.ReceiptNoteClientID < 0)
                        {
                            dbItem.ReceiptNoteClient.Add(dbClient);
                        }
                        else
                        {
                            dbClient = dbItem.ReceiptNoteClient.Where(o => o.ReceiptNoteClientID == dtoItemClient.ReceiptNoteClientID).FirstOrDefault();
                        }
                        if (dbClient != null)
                        {
                            if (dtoItem.Currency == "VND")
                            {
                                totalByCurrency += (dtoItemClient.Amount == null ? 0 : dtoItemClient.Amount);
                            }
                            AutoMapper.Mapper.Map <DTO.ReceiptNoteClientResult, ReceiptNoteClient>(dtoItemClient, dbClient);
                        }
                    }
                }
            }

            //From other
            if (dtoItem.ReceiptNoteTypeID == 3)
            {
                if (dtoItem.receiptNoteOtherResults != null)
                {
                    //remove
                    foreach (var item in dbItem.ReceiptNoteOther.ToArray())
                    {
                        if (!dtoItem.receiptNoteOtherResults.Select(o => o.ReceiptNoteOtherID).Contains(item.ReceiptNoteOtherID))
                        {
                            dbItem.ReceiptNoteOther.Remove(item);
                        }
                    }

                    //Checking And Map
                    foreach (var dtoItemOther in dtoItem.receiptNoteOtherResults)
                    {
                        ReceiptNoteOther dbOther = new ReceiptNoteOther();
                        if (dtoItemOther.ReceiptNoteOtherID < 0)
                        {
                            dbItem.ReceiptNoteOther.Add(dbOther);
                        }
                        else
                        {
                            dbOther = dbItem.ReceiptNoteOther.Where(o => o.ReceiptNoteOtherID == dtoItemOther.ReceiptNoteOtherID).FirstOrDefault();
                        }
                        if (dbOther != null)
                        {
                            if (dtoItem.Currency == "VND")
                            {
                                totalByCurrency += (dtoItemOther.Amount == null ? 0 : dtoItemOther.Amount);
                            }
                            AutoMapper.Mapper.Map <DTO.ReceiptNoteOtherResult, ReceiptNoteOther>(dtoItemOther, dbOther);
                        }
                    }
                }
            }

            //From sale invoice
            if (dtoItem.ReceiptNoteTypeID == 4)
            {
                if (dtoItem.receiptNoteSaleInvoiceResults != null)
                {
                    //remove
                    foreach (var item in dbItem.ReceiptNoteSaleInvoice.ToArray())
                    {
                        if (!dtoItem.receiptNoteSaleInvoiceResults.Select(o => o.ReceiptNoteSaleInvoiceID).Contains(item.ReceiptNoteSaleInvoiceID))
                        {
                            dbItem.ReceiptNoteSaleInvoice.Remove(item);
                        }
                    }

                    //Checking And Map
                    foreach (var dtoItemSaleInvoice in dtoItem.receiptNoteSaleInvoiceResults)
                    {
                        ReceiptNoteSaleInvoice dbSaleInvoice = new ReceiptNoteSaleInvoice();
                        if (dtoItemSaleInvoice.ReceiptNoteSaleInvoiceID < 0)
                        {
                            dbItem.ReceiptNoteSaleInvoice.Add(dbSaleInvoice);
                        }
                        else
                        {
                            dbSaleInvoice = dbItem.ReceiptNoteSaleInvoice.Where(o => o.ReceiptNoteSaleInvoiceID == dtoItemSaleInvoice.ReceiptNoteSaleInvoiceID).FirstOrDefault();
                        }
                        if (dbSaleInvoice != null)
                        {
                            if (dtoItem.Currency == "VND")
                            {
                                totalByCurrency += (dtoItemSaleInvoice.AmountByCurrency == null ? 0 : dtoItemSaleInvoice.AmountByCurrency);

                                dtoItemSaleInvoice.Amount = (dtoItemSaleInvoice.AmountByCurrency == null ? 0 : dtoItemSaleInvoice.AmountByCurrency) / dtoItem.ExchangeRate;
                            }
                            else
                            {
                                dtoItemSaleInvoice.Amount = dtoItemSaleInvoice.AmountByCurrency;
                            }
                            AutoMapper.Mapper.Map <DTO.ReceiptNoteSaleInvoiceResult, ReceiptNoteSaleInvoice>(dtoItemSaleInvoice, dbSaleInvoice);
                        }
                    }
                }
            }


            dtoItem.TotalByCurrency = totalByCurrency;

            AutoMapper.Mapper.Map <DTO.ReceiptNoteEditResult, ReceiptNote>(dtoItem, dbItem);

            //Manual mapping
            dbItem.PostingDate     = dtoItem.PostingDate.ConvertStringToDateTime();
            dbItem.ReceiptNoteDate = dtoItem.ReceiptNoteDate.ConvertStringToDateTime();
        }