public HttpResponseMessage PostBundle(QuotationBundleApiDTO quotationBundle)
        {
            try
            {
                if (quotationBundle == null)
                {
                    return CreateWrongFormatMessage();
                }

                //HttpResponseMessage responseMessage = getAPIAuthorizationMessage();
                //if (responseMessage != null)
                //    return responseMessage;

                HttpResponseMessage responseMessage = getTokenAuthorizationMessage();
                if (responseMessage != null)
                    return responseMessage;

                IQuotationService quotationService = (new ServiceFactory(ApplicationSetting.Current.DefaultConnectionString)).GetQuotationService();
                int nQuotationID = quotationService.CreateApiQuotation(quotationBundle);
                var result = new{
                    nQuotationID = nQuotationID
                };
                return CreateNormalMessage(result);
            }
            catch (Exception exception)
            {
                return CreateServerErrorMessages(exception);
            }
        }
        public int CreateApiQuotation(QuotationBundleApiDTO quotationBundle)
        {
            using (var ds = new DataService(this.connectionString, this.consumerInfo))
            {
                if (quotationBundle.quotation != null)
                {
                    //int? currentUserID = UserInfo.Instance.UserID;
                    DateTime now = DateTime.Now;
                    CrmmRelateCustomer relateCustomer = ds.CrmmRelateCustomer.GetAll().Where(rc => rc.nID == quotationBundle.quotation.nCustomerID).FirstOrDefault();
                    if (relateCustomer != null)
                    {
                        #region Create Relate Customer and Quotation
                        CrmmRelateCustomer quotationRelateCustomer = new CrmmRelateCustomer()
                        {
                            nRawCustomerID = relateCustomer.nRawCustomerID,
                            nGroupID = relateCustomer.nGroupID,
                            nCustomerType = relateCustomer.nCustomerType,
                            nRelateType = 3,  //Quotation
                            sPrefix = relateCustomer.sPrefix,
                            nCode = relateCustomer.nCode,
                            sManualInputCode = relateCustomer.sManualInputCode,
                            sCustomerCode = relateCustomer.sCustomerCode,
                            sRefNumber = relateCustomer.sRefNumber,
                            sCenter = relateCustomer.sCenter,
                            nMailingDistrictID = relateCustomer.nMailingDistrictID,
                            nWorkingDistrictID = relateCustomer.nWorkingDistrictID,
                            nTitle = relateCustomer.nTitle,
                            sName = relateCustomer.sName,
                            sChiName = relateCustomer.sChiName,
                            sMailingAddress = relateCustomer.sMailingAddress,
                            sWorkingAddress = relateCustomer.sWorkingAddress,
                            sContactPersonName = relateCustomer.sContactPersonName,
                            sPhoneNumber1 = relateCustomer.sPhoneNumber1,
                            sPhoneNumber2 = relateCustomer.sPhoneNumber2,
                            sFaxNumber1 = relateCustomer.sFaxNumber1,
                            sFaxNumber2 = relateCustomer.sFaxNumber2,
                            sEmail = relateCustomer.sEmail,
                            Quotations = new List<CrmtQuotation>()
                        };

                        CrmtQuotation newQuotation = new CrmtQuotation
                        {
                            //sACQNumber = string.IsNullOrEmpty(quotation.sACQNumber) ? null : quotation.sACQNumber.Trim(),
                            dIssueDate = now,
                            nLanguage = 1,
                            nStatus = 1,
                            dCreateDate = now,
                            dLastUpdate = now,
                            nUpdateUserID = quotationBundle.quotation.nUserID
                        };
                        quotationRelateCustomer.Quotations.Add(newQuotation);
                        ds.CrmmRelateCustomer.Add(quotationRelateCustomer);
                        ds.SaveChanges();   //for getting quotation id
                        #endregion Create Relate Customer and Quotation

                        #region Create Relate Quotation and Order
                        int orderID = ds.CrmtOrder.GetAll().Where(o => o.nCustomerID == relateCustomer.nID).Select(o => o.nID).FirstOrDefault();
                        CrmtTableRelation relateQuotationOrder = new CrmtTableRelation
                        {
                            sTable1 = "quotation",
                            sTable2 = "order",
                            nTable1ID = newQuotation.nID,
                            nTable2ID = orderID
                        };
                        ds.CrmtTableRelation.Add(relateQuotationOrder);
                        ds.SaveChanges();
                        #endregion Create Relate Quotation and Order

                        #region Create Quotation Items
                        if (quotationBundle.quotationItems != null)
                        {
                            int itemSequence = 1;
                            foreach (QuotationItemApiDTO quotationItem in quotationBundle.quotationItems)
                            {
                                //Get Quotation Package Item
                                List<CrmmQuotationPackageItem> packageItems = (from qpi in ds.CrmmQuotationPackageItem.GetAll()
                                                                             where qpi.nQuotationPackageID == quotationItem.nQuotationPackageID & qpi.nStatus == 1
                                                                             orderby qpi.nSequence
                                                                             select qpi).ToList();
                                foreach (CrmmQuotationPackageItem packageItem in packageItems)
                                {
                                    #region Create Quotation Item
                                    CrmtQuotationItem newQuotationItem = new CrmtQuotationItem
                                    {
                                        sCode = packageItem.sCode,
                                        nSequence = itemSequence,
                                        sDetailChi = packageItem.sDetailChi,
                                        sDetailEng = packageItem.sDetailEng,
                                        nPrice = packageItem.nPrice,
                                        dLastUpdate = now,
                                        nUpdateUserID = quotationBundle.quotation.nUserID
                                    };
                                    ds.CrmtQuotationItem.Add(newQuotationItem);
                                    ds.SaveChanges();   //for getting quotation item id
                                    #endregion Create Quotation Item

                                    #region Create Quotation Item Relate Record
                                    CrmtTableRelation relation = new CrmtTableRelation
                                    {
                                        sTable1 = "quotation",
                                        sTable2 = "quotation_items",
                                        nTable1ID = newQuotation.nID,
                                        nTable2ID = newQuotationItem.nID
                                    };
                                    ds.CrmtTableRelation.Add(relation);
                                    ds.SaveChanges();
                                    #endregion Create Quotation Item Relate Record

                                    itemSequence++;
                                }
                            }
                        }
                        return quotationRelateCustomer.Quotations.FirstOrDefault().nID;

                        #endregion Create Quotation Items
                    }

                }
                return -1;
            }
        }