/// <summary>
        /// This method is used to convert the Purchase order to order
        /// </summary>
        /// <param name="orderData"></param>
        /// <returns></returns>
        public int ConvertToPurchaseOrder(PurchaseOrderForm orderData)
        {
            int autoId = 0;
            //Add purchase order
            PurchaseOrder obj = new PurchaseOrder();

            //obj.ID = orderData.Order.ID;
            obj.Sup_Id         = orderData.Order.SupplierID;
            obj.PO_Date        = orderData.Order.OrderDate;
            obj.PO_Del_Date    = DateTime.Now;
            obj.PO_GST_Amt     = Convert.ToDecimal(orderData.Order.TotalTax);
            obj.PO_No          = "PO-" + GetLastOrderNo();
            obj.PO_TandC       = orderData.Order.TermsAndConditions;
            obj.PO_Tot_aft_Tax = Convert.ToDecimal(orderData.Order.TotalAfterTax);
            obj.PO_Tot_bef_Tax = Convert.ToDecimal(orderData.Order.TotalBeforeTax);

            obj.Exc_Inc_GST = orderData.Order.ExcIncGST;
            obj.IsDeleted   = false;

            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    if (entities.PurchaseOrders.AsNoTracking().FirstOrDefault(x => x.ID == orderData.Order.ID) == null)
                    {
                        //obj.CreatedBy = orderData.POModel.CreatedBy;
                        obj.CreatedDate = DateTime.Now;
                        entities.PurchaseOrders.Add(obj);
                        entities.SaveChanges();
                        autoId = obj.ID;
                    }
                    else
                    {
                        // obj.ModifiedBy = orderData.POModel.ModifiedBy;
                        obj.ModifiedDate          = DateTime.Now;
                        entities.Entry(obj).State = EntityState.Modified;
                        autoId = entities.SaveChanges();
                    }
                    if (autoId > 0)
                    {
                        PurchaseOrderDetail PODetails;
                        if (orderData.OrderDetails != null)
                        {
                            foreach (PurchaseOrderDetailEntity PODetailEntity in orderData.OrderDetails)
                            {
                                PODetails             = new PurchaseOrderDetail();
                                PODetails.PO_ID       = autoId;
                                PODetails.PO_No       = PODetailEntity.PONo;
                                PODetails.PandS_Code  = PODetailEntity.PandSCode;
                                PODetails.PandS_Name  = PODetailEntity.PandSName;
                                PODetails.PO_Amount   = PODetailEntity.POAmount;
                                PODetails.PO_Discount = PODetailEntity.PODiscount;
                                PODetails.PO_No       = PODetailEntity.PONo;
                                PODetails.PO_Price    = Convert.ToDecimal(PODetailEntity.POPrice);
                                PODetails.PO_Qty      = PODetailEntity.POQty;
                                PODetails.GST_Code    = PODetailEntity.GSTCode;
                                PODetails.GST_Rate    = PODetailEntity.GSTRate;

                                if (entities.PurchaseOrderDetails.AsNoTracking().FirstOrDefault(x => x.ID == PODetailEntity.ID) == null)
                                {
                                    entities.PurchaseOrderDetails.Add(PODetails);
                                    entities.SaveChanges();
                                }
                                else
                                {
                                    entities.Entry(PODetails).State = EntityState.Modified;
                                    entities.SaveChanges();
                                }
                            }
                        }

                        PurchaseOrder objQ = entities.PurchaseOrders.Where(e => e.PO_No == orderData.Order.OrderNo
                                                                           ).SingleOrDefault();
                        if (objQ != null)
                        {
                            //objQ.PO_Conv_to_PO = true;
                            objQ.ModifiedDate = DateTime.Now;
                            entities.SaveChanges();
                        }
                    }
                }
                return(autoId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// This method is used to add or edit purchase order
        /// </summary>
        /// <param name="orderData"></param>
        /// <returns></returns>
        public int AddUpdateOrder(PurchaseOrderForm orderData)
        {
            int autoId = 0;
            //Add purchase order
            PurchaseOrder obj = new PurchaseOrder();

            obj.ID     = orderData.Order.ID;
            obj.Sup_Id = orderData.Order.SupplierID;
            //obj.PO_Conv_to_PO = orderData.Order.PO_Conv_to_PO;
            obj.PO_Conv_to_PI  = orderData.Order.PO_Conv_to_PI;
            obj.PO_Date        = orderData.Order.OrderDate;
            obj.PO_GST_Amt     = Convert.ToDecimal(orderData.Order.TotalTax);
            obj.PO_No          = orderData.Order.OrderNo;
            obj.PO_TandC       = orderData.Order.TermsAndConditions;
            obj.PO_Tot_aft_Tax = Convert.ToDecimal(orderData.Order.TotalAfterTax);
            obj.PO_Tot_bef_Tax = Convert.ToDecimal(orderData.Order.TotalBeforeTax);
            obj.PO_Status      = Convert.ToByte(PO_Status.unDeposited);
            obj.PO_Del_Date    = orderData.Order.DeliveryDate;
            obj.Exc_Inc_GST    = orderData.Order.ExcIncGST;
            obj.IsDeleted      = false;

            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    if (entities.PurchaseOrders.AsNoTracking().FirstOrDefault(x => x.ID == orderData.Order.ID) == null)
                    {
                        //obj.CreatedBy = orderData.POModel.CreatedBy;
                        obj.CreatedDate = DateTime.Now;
                        entities.PurchaseOrders.Add(obj);
                        entities.SaveChanges();
                        autoId = obj.ID;
                    }
                    else
                    {
                        // obj.ModifiedBy = orderData.POModel.ModifiedBy;
                        obj.ModifiedDate          = DateTime.Now;
                        entities.Entry(obj).State = EntityState.Modified;
                        autoId = entities.SaveChanges();
                    }
                    if (autoId > 0)
                    {
                        PurchaseOrderDetail PODetails;
                        if (orderData.OrderDetails != null)
                        {
                            foreach (PurchaseOrderDetailEntity PODetailEntity in orderData.OrderDetails)
                            {
                                PODetails             = new PurchaseOrderDetail();
                                PODetails.PO_ID       = autoId;
                                PODetails.PO_No       = PODetailEntity.PONo;
                                PODetails.PandS_Code  = PODetailEntity.PandSCode;
                                PODetails.PandS_Name  = PODetailEntity.PandSName;
                                PODetails.PO_Amount   = PODetailEntity.POAmount;
                                PODetails.PO_Discount = PODetailEntity.PODiscount;
                                PODetails.PO_No       = PODetailEntity.PONo;
                                PODetails.PO_Price    = Convert.ToDecimal(PODetailEntity.POPrice);
                                PODetails.PO_Qty      = PODetailEntity.POQty;
                                PODetails.GST_Code    = PODetailEntity.GSTCode;
                                PODetails.GST_Rate    = PODetailEntity.GSTRate;

                                if (entities.PurchaseOrderDetails.AsNoTracking().FirstOrDefault(x => x.ID == PODetailEntity.ID) == null)
                                {
                                    entities.PurchaseOrderDetails.Add(PODetails);
                                    entities.SaveChanges();
                                }
                                else
                                {
                                    entities.Entry(PODetails).State = EntityState.Modified;
                                    entities.SaveChanges();
                                }
                                int PSId = Convert.ToInt32(PODetailEntity.PONo);
                                if (PSId != 0)
                                {
                                    ProductsAndService ps = entities.ProductsAndServices.SingleOrDefault(e => e.ID == PSId);
                                    if (ps != null)
                                    {
                                        ps.PandS_Qty_on_PO = ps.PandS_Qty_on_PO + PODetailEntity.POQty;
                                        entities.SaveChanges();
                                    }
                                }
                            }
                        }
                    }
                }
                return(autoId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// This method is used to convert the Purchase quotation to order
        /// </summary>
        /// <param name="quotationData"></param>
        /// <returns></returns>
        public int ConvertToPurchaseOrder(PurchaseQuotationForm quotationData)
        {
            int autoId = 0;

            PurchaseOrder obj = new PurchaseOrder();

            //obj.ID = quotationData.Quotation.ID;
            obj.Sup_Id         = quotationData.Quotation.SupplierID;
            obj.PO_Date        = quotationData.Quotation.QuotationDate;
            obj.PO_Del_Date    = DateTime.Now;
            obj.PO_GST_Amt     = Convert.ToDecimal(quotationData.Quotation.TotalTax);
            obj.PO_No          = "PO-" + (GetLastOrderNo() + 1);
            obj.PO_TandC       = quotationData.Quotation.TermsAndConditions;
            obj.PO_Tot_aft_Tax = Convert.ToDecimal(quotationData.Quotation.TotalAfterTax);
            obj.PO_Tot_bef_Tax = Convert.ToDecimal(quotationData.Quotation.TotalBeforeTax);
            obj.PO_Status      = Convert.ToByte(PO_Status.unDeposited);
            obj.Exc_Inc_GST    = quotationData.Quotation.ExcIncGST;
            obj.IsDeleted      = false;

            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    if (entities.PurchaseOrders.AsNoTracking().FirstOrDefault(x => x.ID == quotationData.Quotation.ID) == null)
                    {
                        //obj.CreatedBy = quotationData.PQModel.CreatedBy;
                        obj.CreatedDate = DateTime.Now;
                        entities.PurchaseOrders.Add(obj);
                        entities.SaveChanges();
                        autoId = obj.ID;
                    }
                    else
                    {
                        // obj.ModifiedBy = quotationData.PQModel.ModifiedBy;
                        obj.ModifiedDate          = DateTime.Now;
                        entities.Entry(obj).State = EntityState.Modified;
                        autoId = entities.SaveChanges();
                    }
                    if (autoId > 0)
                    {
                        PurchaseOrderDetail PQDetails;
                        if (quotationData.QuotationDetails != null)
                        {
                            foreach (PurchaseQuotationDetailEntity PQDetailEntity in quotationData.QuotationDetails)
                            {
                                PQDetails             = new PurchaseOrderDetail();
                                PQDetails.PO_ID       = autoId;
                                PQDetails.PO_No       = PQDetailEntity.PQNo;
                                PQDetails.PandS_Code  = PQDetailEntity.PandSCode;
                                PQDetails.PandS_Name  = PQDetailEntity.PandSName;
                                PQDetails.PO_Amount   = PQDetailEntity.PQAmount;
                                PQDetails.PO_Discount = PQDetailEntity.PQDiscount;
                                PQDetails.PO_No       = PQDetailEntity.PQNo;
                                PQDetails.PO_Price    = Convert.ToDecimal(PQDetailEntity.PQPrice);
                                PQDetails.PO_Qty      = PQDetailEntity.PQQty;
                                PQDetails.GST_Code    = PQDetailEntity.GSTCode;
                                PQDetails.GST_Rate    = PQDetailEntity.GSTRate;

                                if (entities.PurchaseOrderDetails.AsNoTracking().FirstOrDefault(x => x.ID == PQDetailEntity.ID) == null)
                                {
                                    entities.PurchaseOrderDetails.Add(PQDetails);
                                    entities.SaveChanges();
                                }
                                else
                                {
                                    entities.Entry(PQDetails).State = EntityState.Modified;
                                    entities.SaveChanges();
                                }
                            }
                        }

                        PurchaseQuotation objQ = entities.PurchaseQuotations.Where(e => e.PQ_No == quotationData.Quotation.QuotationNo
                                                                                   ).SingleOrDefault();
                        if (objQ != null)
                        {
                            objQ.PQ_Conv_to_PO = true;
                            objQ.Conv_to_No    = obj.PO_No;
                            objQ.ModifiedDate  = DateTime.Now;
                            entities.SaveChanges();
                        }
                    }
                }
                return(autoId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// This method is used to add or edit purchase invoice
        /// </summary>
        /// <param name="invoiceData"></param>
        /// <returns></returns>
        public int AddUpdateInvoice(PurchaseInvoiceForm invoiceData)
        {
            int autoId = 0;
            //Add purchase invoice
            PurchaseInvoice obj = new PurchaseInvoice();

            obj.ID     = invoiceData.Invoice.ID;
            obj.Sup_Id = invoiceData.Invoice.SupplierID;

            obj.PI_Date         = invoiceData.Invoice.InvoiceDate;
            obj.PI_GST_Amt      = Convert.ToDecimal(invoiceData.Invoice.TotalTax);
            obj.PI_No           = invoiceData.Invoice.InvoiceNo;
            obj.PI_TandC        = invoiceData.Invoice.TermsAndConditions;
            obj.PI_Tot_aft_Tax  = Convert.ToDecimal(invoiceData.Invoice.TotalAfterTax);
            obj.PI_Tot_bef_Tax  = Convert.ToDecimal(invoiceData.Invoice.TotalBeforeTax);
            obj.Our_PO_No       = invoiceData.Invoice.OurPONo;
            obj.PI_Pmt_Due_Date = invoiceData.Invoice.PaymentDueDate;
            obj.Exc_Inc_GST     = invoiceData.Invoice.ExcIncGST;
            obj.PI_Status       = Convert.ToByte(PI_Status.UnPaid);
            obj.IsDeleted       = false;

            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    if (entities.PurchaseInvoices.AsNoTracking().FirstOrDefault(x => x.ID == invoiceData.Invoice.ID) == null)
                    {
                        //obj.CreatedBy = invoiceData.PIModel.CreatedBy;
                        obj.CreatedDate = DateTime.Now;
                        entities.PurchaseInvoices.Add(obj);
                        entities.SaveChanges();
                        autoId = obj.ID;
                    }
                    else
                    {
                        // obj.ModifiedBy = invoiceData.PIModel.ModifiedBy;
                        obj.ModifiedDate          = DateTime.Now;
                        entities.Entry(obj).State = EntityState.Modified;
                        autoId = entities.SaveChanges();
                    }

                    invoiceData.Invoice.ID = autoId;
                    CreateDebitNote(invoiceData);

                    if (autoId > 0)
                    {
                        PurchaseInvoiceDetail PIDetails;
                        if (invoiceData.InvoiceDetails != null)
                        {
                            foreach (PurchaseInvoiceDetailEntity PIDetailEntity in invoiceData.InvoiceDetails)
                            {
                                PIDetails             = new PurchaseInvoiceDetail();
                                PIDetails.PI_ID       = autoId;
                                PIDetails.PI_No       = PIDetailEntity.PINo;
                                PIDetails.PandS_Code  = PIDetailEntity.PandSCode;
                                PIDetails.PandS_Name  = PIDetailEntity.PandSName;
                                PIDetails.PI_Amount   = PIDetailEntity.PIAmount;
                                PIDetails.PI_Discount = PIDetailEntity.PIDiscount;
                                PIDetails.PI_No       = PIDetailEntity.PINo;
                                PIDetails.PI_Price    = Convert.ToDecimal(PIDetailEntity.PIPrice);
                                PIDetails.PI_Qty      = PIDetailEntity.PIQty;
                                PIDetails.GST_Code    = PIDetailEntity.GSTCode;
                                PIDetails.GST_Rate    = PIDetailEntity.GSTRate;


                                if (entities.PurchaseInvoiceDetails.AsNoTracking().FirstOrDefault(x => x.ID == PIDetailEntity.ID) == null)
                                {
                                    entities.PurchaseInvoiceDetails.Add(PIDetails);
                                    entities.SaveChanges();
                                }
                                else
                                {
                                    entities.Entry(PIDetails).State = EntityState.Modified;
                                    entities.SaveChanges();
                                }
                                int PSId = Convert.ToInt32(PIDetailEntity.PINo);
                                if (PSId != 0)
                                {
                                    ProductsAndService ps = entities.ProductsAndServices.SingleOrDefault(e => e.ID == PSId);
                                    if (ps != null)
                                    {
                                        ps.PandS_Qty_in_stock = ps.PandS_Qty_in_stock + PIDetailEntity.PIQty;
                                        entities.SaveChanges();
                                    }
                                }
                            }
                        }
                    }

                    int i = entities.Database.ExecuteSqlCommand("exec PRC_Insert_PurchaseInvoiceData {0}", obj.ID);
                }
                return(autoId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// This method is used to add or edit purchase quotation
        /// </summary>
        /// <param name="quotationData"></param>
        /// <returns></returns>
        public int AddUpdateQuotation(PurchaseQuotationForm quotationData)
        {
            int autoId = 0;
            //Add purchase quotation
            PurchaseQuotation obj = new PurchaseQuotation();

            obj.ID             = quotationData.Quotation.ID;
            obj.Sup_Id         = quotationData.Quotation.SupplierID;
            obj.PQ_Conv_to_PO  = quotationData.Quotation.PQ_Conv_to_PO;
            obj.PQ_Conv_to_PI  = quotationData.Quotation.PQ_Conv_to_PI;
            obj.PQ_Date        = quotationData.Quotation.QuotationDate;
            obj.PQ_GST_Amt     = Convert.ToDecimal(quotationData.Quotation.TotalTax);
            obj.PQ_No          = quotationData.Quotation.QuotationNo;
            obj.PQ_TandC       = quotationData.Quotation.TermsAndConditions;
            obj.PQ_Tot_aft_Tax = Convert.ToDecimal(quotationData.Quotation.TotalAfterTax);
            obj.PQ_Tot_bef_Tax = Convert.ToDecimal(quotationData.Quotation.TotalBeforeTax);
            obj.PQ_Valid_for   = quotationData.Quotation.ValidForDays;
            obj.Exc_Inc_GST    = quotationData.Quotation.ExcIncGST;
            obj.IsDeleted      = false;

            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    if (entities.PurchaseQuotations.AsNoTracking().FirstOrDefault(x => x.ID == quotationData.Quotation.ID) == null)
                    {
                        //obj.CreatedBy = quotationData.PQModel.CreatedBy;
                        obj.CreatedDate = DateTime.Now;
                        entities.PurchaseQuotations.Add(obj);
                        entities.SaveChanges();
                        autoId = obj.ID;
                    }
                    else
                    {
                        // obj.ModifiedBy = quotationData.PQModel.ModifiedBy;
                        obj.ModifiedDate          = DateTime.Now;
                        entities.Entry(obj).State = EntityState.Modified;
                        autoId = entities.SaveChanges();
                    }
                    if (autoId > 0)
                    {
                        PurchaseQuotationDetail PQDetails;
                        if (quotationData.QuotationDetails != null)
                        {
                            foreach (PurchaseQuotationDetailEntity PQDetailEntity in quotationData.QuotationDetails)
                            {
                                PQDetails             = new PurchaseQuotationDetail();
                                PQDetails.PQ_ID       = autoId;
                                PQDetails.PQ_No       = PQDetailEntity.PQNo;
                                PQDetails.PandS_Code  = PQDetailEntity.PandSCode;
                                PQDetails.PandS_Name  = PQDetailEntity.PandSName;
                                PQDetails.PQ_Amount   = PQDetailEntity.PQAmount;
                                PQDetails.PQ_Discount = PQDetailEntity.PQDiscount;
                                PQDetails.PQ_No       = PQDetailEntity.PQNo;
                                PQDetails.PQ_Price    = Convert.ToDecimal(PQDetailEntity.PQPrice);
                                PQDetails.PQ_Qty      = PQDetailEntity.PQQty;
                                PQDetails.GST_Code    = PQDetailEntity.GSTCode;
                                PQDetails.GST_Rate    = PQDetailEntity.GSTRate;

                                if (entities.PurchaseQuotationDetails.AsNoTracking().FirstOrDefault(x => x.ID == PQDetailEntity.ID) == null)
                                {
                                    entities.PurchaseQuotationDetails.Add(PQDetails);
                                    entities.SaveChanges();
                                }
                                else
                                {
                                    entities.Entry(PQDetails).State = EntityState.Modified;
                                    entities.SaveChanges();
                                }
                            }
                        }
                    }
                }
                return(autoId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }