public List<CustomerContractViewModel> ReadProduct(string CRef)
        {
            List<CustomerContractViewModel> ret = new List<CustomerContractViewModel>();
            int tmp = 0;
            if (CRef != null && CRef != "")
            {
                tmp = Int32.Parse(CRef);
            }
            var qry = db.CustomerContract.Where(x => x.CustomerRef == tmp && x.ContractStatus != "XX");
            foreach (CustomerContract o in qry)
            {
                CustomerContractViewModel v = new CustomerContractViewModel();

                //s.EventClass = "";                
                v.ProductNote = db.ProductData.Where(x => x.ProductID == o.ProductID).Select(x => x.Note2).First();
                v.ProductClassID = db.ProductData.Where(x => x.ProductID == o.ProductID).Select(x => x.ProductClassID).First().Value;
                v.ProductID = o.ProductID;
                switch (o.ContractStatus)
                {//01.洽談中 (可能用計算方式:合約到期日 +N天) A1.總行合約中 B1.分行合約中 C1.叫修 L1.租賃中 W1.保固中 ZZ.未分類 XX.停用
                    case "01": v.ContractStatusStr = "洽談中"; break;
                    case "A1": v.ContractStatusStr = "總行合約中"; break;
                    case "B1": v.ContractStatusStr = "分行合約中"; break;
                    case "C1": v.ContractStatusStr = "叫修"; break;
                    case "L1": v.ContractStatusStr = "租賃中"; break;
                    case "W1": v.ContractStatusStr = "保固中"; break;
                    case "ZZ": v.ContractStatusStr = "未分類"; break;
                    case "XX": v.ContractStatusStr = "停用"; break;
                    default: v.ContractStatusStr = ""; break;
                }
                v.MaintainNum = o.MaintainNum;
                v.MaintainMonthMemo = o.MaintainMonthMemo;
                ret.Add(v);
            }
            return ret;
        }
        public string Update(CustomerContractViewModel c)
        {
            string ret = "false";

            try
            {                
                CustomerContract contract = db.CustomerContract.Find(c.ContractSeq);
                contract.CallToArrivalRangeTime = c.CallToArrivalRangeTime;
                contract.CallToRepairRangeTime = c.CallToRepairRangeTime;
                contract.ContractAutoEndDate = c.ContractAutoEndDate;
                contract.ContractAutoSW = c.ContractAutoSW;
                contract.ContractCharDay = c.ContractCharDay;
                contract.ContractMark = c.ContractMark;
                contract.ContractMode = c.ContractMode;
                contract.ContractName = c.ContractName;
                contract.ContractNum = c.ContractNum;
                contract.ContractPeriod = c.ContractPeriod;
                contract.ContractSeq = c.ContractSeq;
                contract.ContractStatus = c.ContractStatus;
                contract.ContractSW = c.ContractSW;
                //contract.CreateDateTime = DateTime.Now;
                //contract.CreateUserID = c.CreateUserID;
                contract.CustomerRef = c.CustomerRef;
                contract.DeliveryDT = c.DeliveryDT;
                contract.MaintainBetweenDay = c.MaintainBetweenDay;
                contract.MaintainCoverageMemo = c.MaintainCoverageMemo;
                contract.MaintainEndDT = c.MaintainEndDT;
                contract.MaintainEndMonth = c.MaintainEndMonth;
                contract.MaintainEndMonthMemo = c.MaintainEndMonthMemo;
                contract.MaintainMonth01 = c.MaintainMonth01;
                contract.MaintainMonth02 = c.MaintainMonth02;
                contract.MaintainMonth03 = c.MaintainMonth03;
                contract.MaintainMonth04 = c.MaintainMonth04;
                contract.MaintainMonth05 = c.MaintainMonth05;
                contract.MaintainMonth06 = c.MaintainMonth06;
                contract.MaintainMonth07 = c.MaintainMonth07;
                contract.MaintainMonth08 = c.MaintainMonth08;
                contract.MaintainMonth09 = c.MaintainMonth09;
                contract.MaintainMonth10 = c.MaintainMonth10;
                contract.MaintainMonth11 = c.MaintainMonth11;
                contract.MaintainMonth12 = c.MaintainMonth12;
                contract.MaintainMonthDay = c.MaintainMonthDay;
                contract.MaintainMonthMemo = c.MaintainMonthMemo;
                contract.MaintainNum = c.MaintainNum;
                contract.MaintainReportOnly = c.MaintainReportOnly;
                contract.MaintainReportRuleMemo = c.MaintainReportRuleMemo;
                contract.MaintainStartDT = c.MaintainStartDT;
                contract.MaintainWholeMonthSW = c.MaintainWholeMonthSW;
                contract.ModifyDateTime = DateTime.Now;
                contract.ModifyUserID = c.ModifyUserID;
                //contract.ProductClassID = c.ProductClassID;
                contract.ProductID = c.ProductID;
                contract.ProductQuantity = c.ProductQuantity;
                contract.ReceiptMemo = c.ReceiptMemo;
                contract.ReceiptMonth = c.ReceiptMonth;
                contract.ReceiptMonthMemo = c.ReceiptMonthMemo;
                contract.ReceiptPriceMemo = c.ReceiptPriceMemo;
                contract.ReceiptSW = c.ReceiptSW;
                contract.ReplaceOlderMemo = c.ReplaceOlderMemo;
                contract.SalesMemo = c.SalesMemo;
                contract.StandardMemo = c.StandardMemo;

                //db.CustomerContract.Add(contract);
                db.SaveChanges();
                ret = "true";
            }
            catch
            {
            }
            return ret;
        }
        //判斷有人使用了就不能更改ProductID跟CustomerRef
        public Boolean CheckHaveUsedChangeREF(CustomerContractViewModel c)//false:沒人一樣,true:有人一樣
        {
            CustomerContract o = db.CustomerContract.Find(c.ContractSeq);
            try
            {
                if (db.WBSEventC.Where(x => x.WBSEventM.CustomerRef == o.CustomerRef && x.WBSEventM.ProductID == o.ProductID && x.ExpectArriveSW == "1").Count() > 0)
                { 
                    if (db.CustomerContract.Where(x => (x.CustomerRef != c.CustomerRef || x.ProductID != c.ProductID )&& x.ContractSeq == c.ContractSeq).Count() > 0)
                        return false;
                }

            }
            catch
            {
            }
            return true;
        }
        //合約詳情讀取
        public CustomerContractViewModel ReadOneCase(int CSeq)
        {
            CustomerContractViewModel v = new CustomerContractViewModel();
            CustomerContract C = db.CustomerContract.Find(CSeq);

            v.ContractSeq = C.ContractSeq;
            v.ContractNum = C.ContractNum;
            v.ContractName = C.ContractName;
            v.MaintainNum = C.MaintainNum;
            v.CustomerRef = C.CustomerRef;
            //6
            v.ProductID = C.ProductID;
            v.ProductQuantity = C.ProductQuantity.Value;
            v.ContractSW = C.ContractSW;
            v.ContractPeriod = C.ContractPeriod.Value;
            v.ContractAutoSW = C.ContractAutoSW;
            //11
            v.ContractAutoEndDate = C.ContractAutoEndDate.Value;
            v.ContractMode = C.ContractMode;
            v.ContractStatus = C.ContractStatus;
            v.ContractCharDay = C.ContractCharDay.Value;
            v.ContractMark = C.ContractMark;
           //16
            v.MaintainStartDT = C.MaintainStartDT.Value;
            v.MaintainEndDT = C.MaintainEndDT.Value;
            v.CallToArrivalRangeTime = C.CallToArrivalRangeTime.Value;
            v.CallToRepairRangeTime = C.CallToRepairRangeTime.Value;
            v.MaintainWholeMonthSW = C.MaintainWholeMonthSW;
            //21
            v.MaintainMonthDay = C.MaintainMonthDay.Value;
            v.MaintainMonth01 = C.MaintainMonth01.Value;
            v.MaintainMonth02 = C.MaintainMonth02.Value;
            v.MaintainMonth03 = C.MaintainMonth03.Value;
            v.MaintainMonth04 = C.MaintainMonth04.Value;
            //26
            v.MaintainMonth05 = C.MaintainMonth05.Value;
            v.MaintainMonth06 = C.MaintainMonth06.Value;
            v.MaintainMonth07 = C.MaintainMonth07.Value;
            v.MaintainMonth08 = C.MaintainMonth08.Value;
            v.MaintainMonth09 = C.MaintainMonth09.Value;
            //31
            v.MaintainMonth10 = C.MaintainMonth10.Value;
            v.MaintainMonth11 = C.MaintainMonth11.Value;
            v.MaintainMonth12 = C.MaintainMonth12.Value;
            v.MaintainMonthMemo = C.MaintainMonthMemo;
            v.MaintainEndMonth = C.MaintainEndMonth;
            //36
            v.MaintainEndMonthMemo = C.MaintainEndMonthMemo;
            v.MaintainBetweenDay = C.MaintainBetweenDay.Value;
            v.ReceiptSW = C.ReceiptSW;
            v.ReceiptMonth = C.ReceiptMonth;
            v.ReceiptMonthMemo = C.ReceiptMonthMemo;
            //41
            v.ReceiptMemo = C.ReceiptMemo;
            v.ReceiptPriceMemo = C.ReceiptPriceMemo;
            v.MaintainReportRuleMemo = C.MaintainReportRuleMemo;
            v.MaintainReportOnly = C.MaintainReportOnly;
            v.DeliveryDT = C.DeliveryDT.Value;
            //46
            v.MaintainCoverageMemo = C.MaintainCoverageMemo;
            v.ReplaceOlderMemo = C.ReplaceOlderMemo;
            v.SalesMemo = C.SalesMemo;
            v.StandardMemo = C.StandardMemo;
            
            //for view
            v.MaintainMonthAllStr = "";
            if (C.MaintainMonth01.Value)
                v.MaintainMonthAllStr += "1月";
            if (C.MaintainMonth02.Value)
                v.MaintainMonthAllStr += (v.MaintainMonthAllStr == "") ? "2月" : "/2月";
            if (C.MaintainMonth03.Value)
                v.MaintainMonthAllStr += (v.MaintainMonthAllStr == "") ? "3月" : "/3月";
            if (C.MaintainMonth04.Value)
                v.MaintainMonthAllStr += (v.MaintainMonthAllStr == "") ? "4月" : "/4月";
            if (C.MaintainMonth05.Value)
                v.MaintainMonthAllStr += (v.MaintainMonthAllStr == "") ? "5月" : "/5月";
            if (C.MaintainMonth06.Value)
                v.MaintainMonthAllStr += (v.MaintainMonthAllStr == "") ? "6月" : "/6月";
            if (C.MaintainMonth07.Value)
                v.MaintainMonthAllStr += (v.MaintainMonthAllStr == "") ? "7月" : "/7月";
            if (C.MaintainMonth08.Value)
                v.MaintainMonthAllStr += (v.MaintainMonthAllStr == "") ? "8月" : "/8月";
            if (C.MaintainMonth09.Value)
                v.MaintainMonthAllStr += (v.MaintainMonthAllStr == "") ? "9月" : "/9月";
            if (C.MaintainMonth10.Value)
                v.MaintainMonthAllStr += (v.MaintainMonthAllStr == "") ? "10月" : "/10月";
            if (C.MaintainMonth11.Value)
                v.MaintainMonthAllStr += (v.MaintainMonthAllStr == "") ? "11月" : "/11月";
            if (C.MaintainMonth12.Value)
                v.MaintainMonthAllStr += (v.MaintainMonthAllStr == "") ? "12月" : "/12月";

            v.CreateDTStr = C.CreateDateTime.Value.ToString("yyyy/MM/dd HH:mm");
            v.CreateUserName = C.UserData.UserName;
            v.ModifyDTStr = C.ModifyDateTime.Value.ToString("yyyy/MM/dd HH:mm");
            v.ModifyUserName = C.UserData1.UserName;
            v.CustomerSName = C.Customer.ShortName;
            v.ProductID = C.ProductID;
            //合約狀況代碼
            switch(C.ContractStatus){//01.洽談中 (可能用計算方式:合約到期日 +N天) A1.總行合約中 B1.分行合約中 C1.叫修 L1.租賃中 W1.保固中 ZZ.未分類 XX.停用
                case "01": v.ContractStatusStr = "洽談中"; break;
                case "A1": v.ContractStatusStr = "總行合約中"; break;
                case "B1": v.ContractStatusStr = "分行合約中"; break;
                case "C1": v.ContractStatusStr = "叫修"; break;
                case "L1": v.ContractStatusStr = "租賃中"; break;
                case "W1": v.ContractStatusStr = "保固中"; break;
                case "ZZ": v.ContractStatusStr = "未分類"; break;
                case "XX": v.ContractStatusStr = "停用"; break;
                default: v.ContractStatusStr = ""; break;
            }
            //合約類別
            switch(C.ContractSW){//A全行合約(ALL) B分行合約(Branch) L租賃(Warrant) W保固中(Warrant) O其他(Other)        
                case "A": v.ContractSWStr = "全行合約"; break;
                case "B": v.ContractSWStr = "分行合約"; break;
                case "L": v.ContractSWStr = "租賃"; break;
                case "W": v.ContractSWStr = "保固中"; break;
                case "O": v.ContractSWStr = "其他"; break;                
                default: v.ContractSWStr = ""; break;
            }
            //自動展延            
            switch(C.ContractAutoSW){//Y:自動展延 N:需再簽訂
                case "Y": v.ContractAutoSWStr = "自動展延"; break;
                case "N": v.ContractAutoSWStr = "需再簽訂"; break;
                default: v.ContractAutoSWStr = ""; break;
            }
            v.ContractAutoEndDateStr = "";
            if(C.ContractAutoEndDate != null)
                v.ContractAutoEndDateStr = C.ContractAutoEndDate.Value.ToString("yyyy/MM/dd");
            //收款方式
            switch(C.ReceiptSW){//01>一次付清 M0>依月(需依ReceiptMonth)
                case "01": v.ReceiptSWStr = "一次付清"; break;
                case "M0": v.ReceiptSWStr = "依月"; break;
                default: v.ReceiptSWStr = ""; break;
            }
            //收款月份 EX: 若1月/4月/7月/10月收款 則表示為YNNYNNYNNYNN
            v.ReceiptMonthStr = "";
            for(int i = 0;i < 12;i++){
                if(v.ReceiptMonth[i]=='Y'){
                    if(v.ReceiptMonthStr != "")
                        v.ReceiptMonthStr += "/";
                    v.ReceiptMonthStr += (i+1).ToString() +"月";
                }
            }
            //是否為專用保養單
            switch(C.MaintainReportOnly){//專用保養單:1 兆銀保養單:0 若為M01單,需加顯示於手機「處理說明:」前提示:「請使用專用保養單」(紅字)
                case "0": v.MaintainReportOnlyStr = "兆銀保養單"; break;
                case "1": v.MaintainReportOnlyStr = "專用保養單"; break;
                default: v.MaintainReportOnlyStr = ""; break;
            }
            v.DeliveryDTStr = "";
            if(C.DeliveryDT != null)
                v.DeliveryDTStr = C.DeliveryDT.Value.ToString("yyyy/MM/dd");
            v.MaintainStartDTStr = "";
            if(C.MaintainStartDT != null)
                v.MaintainStartDTStr = C.MaintainStartDT.Value.ToString("yyyy/MM/dd");
            v.MaintainEndDTStr = "";
            if(C.MaintainEndDT != null)
                v.MaintainEndDTStr = C.MaintainEndDT.Value.ToString("yyyy/MM/dd");
            
            //合約迄日是否為整月
            switch(C.MaintainWholeMonthSW){//合約迄日是否為整月(月初~月底)「找0 為破月資料」1: 整月 0: 破月
                case "0": v.MaintainWholeMonthSWStr = "破月"; break;
                case "1": v.MaintainWholeMonthSWStr = "整月"; break;
                default: v.MaintainWholeMonthSWStr = ""; break;
            }
            //合約保養截止月 EX: 若1月/4月/7月/10月收款 則表示為YNNYNNYNNYNN
            v.MaintainEndMonthStr = "";
            for(int i = 0;i < 12;i++){
                if(v.MaintainEndMonth[i]=='Y'){
                    if(v.MaintainEndMonthStr != "")
                        v.MaintainEndMonthStr += "/";
                    v.MaintainEndMonthStr += (i+1).ToString() +"月";
                }
            }            

            return v;
        }
 public IQueryable<CustomerContractViewModel> Read()
 {
     List<CustomerContractViewModel> ret = new List<CustomerContractViewModel>();
     var qry = db.CustomerContract;
     foreach (CustomerContract o in qry)
     {
         CustomerContractViewModel c = new CustomerContractViewModel();
         c.ContractSeq = o.ContractSeq;
         c.ContractNum = o.ContractNum;
         c.ContractName = o.ContractName;
         c.MaintainNum = o.MaintainNum;
         c.CustomerSName = o.Customer.ShortName;
         c.ProductNote = db.ProductData.Where(x=>x.ProductID == o.ProductID).Select(x=>x.Note2).First();
         c.ProductID = o.ProductID;
         c.ContractMode = o.ContractMode;
         c.ContractMark = o.ContractMark;
         c.CallToArrivalRangeTime = o.CallToArrivalRangeTime.Value;
         c.CallToRepairRangeTime = o.CallToRepairRangeTime.Value;
         c.MaintainMonthAllStr = "";
         if (o.MaintainMonth01.Value)
             c.MaintainMonthAllStr += "1月";
         if (o.MaintainMonth02.Value)
             c.MaintainMonthAllStr += (c.MaintainMonthAllStr == "") ? "2月" : "/2月" ;
         if (o.MaintainMonth03.Value)
             c.MaintainMonthAllStr += (c.MaintainMonthAllStr == "") ? "3月" : "/3月";
         if (o.MaintainMonth04.Value)
             c.MaintainMonthAllStr += (c.MaintainMonthAllStr == "") ? "4月" : "/4月";
         if (o.MaintainMonth05.Value)
             c.MaintainMonthAllStr += (c.MaintainMonthAllStr == "") ? "5月" : "/5月";
         if (o.MaintainMonth06.Value)
             c.MaintainMonthAllStr += (c.MaintainMonthAllStr == "") ? "6月" : "/6月";
         if (o.MaintainMonth07.Value)
             c.MaintainMonthAllStr += (c.MaintainMonthAllStr == "") ? "7月" : "/7月";
         if (o.MaintainMonth08.Value)
             c.MaintainMonthAllStr += (c.MaintainMonthAllStr == "") ? "8月" : "/8月";
         if (o.MaintainMonth09.Value)
             c.MaintainMonthAllStr += (c.MaintainMonthAllStr == "") ? "9月" : "/9月";
         if (o.MaintainMonth10.Value)
             c.MaintainMonthAllStr += (c.MaintainMonthAllStr == "") ? "10月" : "/10月";
         if (o.MaintainMonth11.Value)
             c.MaintainMonthAllStr += (c.MaintainMonthAllStr == "") ? "11月" : "/11月";
         if (o.MaintainMonth12.Value)
             c.MaintainMonthAllStr += (c.MaintainMonthAllStr == "") ? "12月" : "/12月";
         c.MaintainMonthMemo = o.MaintainMonthMemo;
         c.MaintainEndMonthMemo = o.MaintainEndMonthMemo;
         c.MaintainReportRuleMemo = o.MaintainReportRuleMemo;
         c.MaintainReportOnly = o.MaintainReportOnly;
         c.CreateDTStr = o.CreateDateTime.Value.ToString("yyyy/MM/dd HH:mm");
         c.CreateUserName = o.UserData.UserName;
         c.ModifyDTStr = o.ModifyDateTime.Value.ToString("yyyy/MM/dd HH:mm");
         c.ModifyUserName = o.UserData1.UserName;
         ret.Add(c);
     }
     return ret.AsQueryable();
 }
        public Boolean CheckContractUsed(CustomerContractViewModel c){

            if (db.WBSEventC.Where(x => x.WBSEventM.CustomerRef == c.CustomerRef && x.WBSEventM.ProductID == c.ProductID && x.ExpectArriveSW == "1").Count() > 0)
                return true;

            return false;
        }
        public string Delete(CustomerContractViewModel c)
        {
            string ret = "false";
            try
            {

                CustomerContract contract = db.CustomerContract.Find(c.ContractSeq);
                db.CustomerContract.Remove(contract);
                db.SaveChanges();
                ret = "true";
            }
            catch
            {
            }
            return ret;
        }