public static string CreateContractForm(ContractForm contractForm, QueryHelper qh)
        {
            var head =
               new[]
                {
                    ContractForm.User_ID, ContractForm.CUSTOMER_ID, ContractForm.AGREEMENT_NO,
                    ContractForm.EFFECTIVE_DATE, ContractForm.EXPIRY_DATE, ContractForm.TOTAL_CONTRACT_AMT,
                    ContractForm.CONTRACT_TYPE_ID,ContractForm.REMARK, ContractForm.STATUS_FLG
                }.ToList();
            var data = new[]
            {
                new[]
                {
                    contractForm.UserId.ToString(),contractForm.CustomerId.ToString(),contractForm.AgreementNo,
                    contractForm.EffectiveDate.ToUniversalTime().ToString("yyyy-MM-dd H:mm:ss"), contractForm.ExpiryDate.ToUniversalTime().ToString("yyyy-MM-dd H:mm:ss"), contractForm.TotalContractAmt.ToString(),
                    contractForm.ContractTypeId.ToString(),contractForm.Remarks,contractForm.StatusFlg.ToString()

                }.ToList()
            }.ToList();

            if (!string.IsNullOrWhiteSpace(contractForm.id.ToString()))
            {
                head.Add(ContractForm.DB_ID);
                data[0].Add(contractForm.id.ToString());
            }
            var query = QueryHelper.BuildBulkInsertQuery(ContractForm.DB_TABLE_NAME, head, data, true, ContractForm.DB_ID);

            return qh.QueryInsertUpdateDelete(query).ToString();
        }
        public static string ModifyContractFormWithId(string contract_id, ContractForm contractForm, QueryHelper qh)
        {
            var head =
               new[]
                {
                    ContractForm.User_ID, ContractForm.CUSTOMER_ID, ContractForm.AGREEMENT_NO,
                    ContractForm.EFFECTIVE_DATE, ContractForm.EXPIRY_DATE, ContractForm.TOTAL_CONTRACT_AMT,
                    ContractForm.CONTRACT_TYPE_ID,ContractForm.REMARK, ContractForm.STATUS_FLG
                }.ToList();
            var data = new[]
            {
                    contractForm.UserId.ToString(),contractForm.CustomerId.ToString(),contractForm.AgreementNo,
                    contractForm.EffectiveDate.ToUniversalTime().ToString("yyyy-MM-dd H:mm:ss"), contractForm.ExpiryDate.ToUniversalTime().ToString("yyyy-MM-dd H:mm:ss"), contractForm.TotalContractAmt.ToString(),
                    contractForm.ContractTypeId.ToString(),contractForm.Remarks,contractForm.StatusFlg.ToString()

            }.ToList();

            var query = QueryHelper.BuildUpdateQuery(ContractForm.DB_TABLE_NAME,
                head,
                data,
                new[] { ContractForm.DB_ID }.ToList(),
                new[] { contract_id }.ToList());
            return qh.QueryInsertUpdateDelete(query).ToString();
        }
 public static ContractForm Parse(Dictionary<string, string> databaseSelectDictionary)
 {
     var ContractForm = new ContractForm();
     if (databaseSelectDictionary.ContainsKey(DB_ID))
     {
         ContractForm.id = Int32.Parse(databaseSelectDictionary[DB_ID]);
     }
     if (databaseSelectDictionary.ContainsKey(User_ID))
     {
         ContractForm.UserId = Int32.Parse(databaseSelectDictionary[User_ID]);
     }
     if (databaseSelectDictionary.ContainsKey(CUSTOMER_ID))
     {
         ContractForm.CustomerId = Int32.Parse(databaseSelectDictionary[CUSTOMER_ID]);
     }
     if (databaseSelectDictionary.ContainsKey(AGREEMENT_NO))
     {
         ContractForm.AgreementNo = databaseSelectDictionary[AGREEMENT_NO];
     }
     if (databaseSelectDictionary.ContainsKey(EFFECTIVE_DATE))
     {
         ContractForm.EffectiveDate = DateTime.Parse(databaseSelectDictionary[EFFECTIVE_DATE]);
     }
     if (databaseSelectDictionary.ContainsKey(EXPIRY_DATE))
     {
         ContractForm.ExpiryDate = DateTime.Parse(databaseSelectDictionary[EXPIRY_DATE]);
     }
     if (databaseSelectDictionary.ContainsKey(TOTAL_CONTRACT_AMT))
     {
         ContractForm.TotalContractAmt = double.Parse(databaseSelectDictionary[TOTAL_CONTRACT_AMT]);
     }
     if (databaseSelectDictionary.ContainsKey(CONTRACT_TYPE_ID))
     {
         ContractForm.ContractTypeId = Int32.Parse(databaseSelectDictionary[CONTRACT_TYPE_ID]);
     }
     if (databaseSelectDictionary.ContainsKey(REMARK))
     {
         ContractForm.Remarks = databaseSelectDictionary[REMARK];
     }
     if (databaseSelectDictionary.ContainsKey(STATUS_FLG))
     {
         ContractForm.StatusFlg = Int32.Parse(databaseSelectDictionary[STATUS_FLG]);
     }
     return ContractForm;
 }
 private static bool IsValidCreateContractForm(ContractForm cf, out string invalidType)
 {
     try
     {
         if (!isCustomerExist(cf.CustomerId))
         {
             invalidType = "invalid customerId";
             return false;
         }
         if (!isValidUser(cf.UserId))
         {
             invalidType = "invalid UserId";
             return false;
         }
         if (!isValidDatesInput(cf.EffectiveDate, cf.ExpiryDate))
         {
             invalidType = "invalid EffectiveDate or ExpiryDate";
             return false;
         }
         if (!isContractTypeExist(cf.ContractTypeId))
         {
             invalidType = "invalid ContractTypeId";
             return false;
         }
         invalidType = "";
         return true;
     }
     catch (ArgumentNullException ex)
     {
         throw ex;
     }
 }
        public static string CreateContractForm(string jsonString)
        {
            //convert jsonString to object
            /* {
                    "UserId" : "1",
                    "ContractTypeId" : 1,
                    "CustomerId" : 1,
                    "EffectiveDate" : "2015-06-21T18:24:18Z",
                    "ExpiryDate" : "2015-06-22T18:24:18Z",
                    "TotalContractAmt" : 200.36,
                    "Remarks" : "testing remark text"
                    "StatusFlg" : 0
                }
             */
            ContractForm cf = null;
            try
            {
                cf = new ContractForm(jsonString);
            }
            catch (JsonException ex)
            {
                throw new Exception("Unable to convert jsonString to ContractForm object", ex);
            }

            try
            {
                string contractFormId = null;
                //validate user inputs
                string invalidType = "";

                if (IsValidCreateContractForm(cf, out invalidType))
                {
                    // save the contract form into DB
                    QueryHelper qh = new QueryHelper();

                    contractFormId = ContractFormManager.CreateContractForm(cf, qh);
                    // generate agreement no. based on DB's id

                    cf.AgreementNo = GenerateAgreementNo(cf, contractFormId, qh);
                    // save the agreement no. into DB

                    ContractFormManager.ModifyContractFormWithId(contractFormId,cf,qh);

                    //save everything

                    qh.Commit();
                }
                // pass feedback message
                string feedbackMsg = "";

                if (!invalidType.Equals(""))
                {
                    feedbackMsg = "Fail to create contract form due to " + invalidType;
                    return feedbackMsg;
                }

                if (contractFormId == null)
                {
                    feedbackMsg = "Unable to create contract form";
                    return feedbackMsg;
                }
                feedbackMsg = "Success: contractForm Id - " + contractFormId;
                return feedbackMsg;

            }
            catch (ArgumentNullException ex)
            {
                return ex.Message;
            }
        }
        private static string GenerateAgreementNo(ContractForm cf , string cf_id, QueryHelper qh)
        {
            try
            {
                 ContractType contractType = ContractTypeManager.GetContractType(cf.ContractTypeId.ToString(), qh);
                User user = UserManager.GetUser(cf.UserId.ToString(), qh);
                Customer customer = CustomerManager.GetCustomer(cf.CustomerId.ToString(), qh);
                string temp = customer.customerName;

                // below block of code is to get the runningNo
                return "" + contractType.contractTypeName + "/"
                    + user.userName + "/"
                    + customer.customerName + "/"
                    + "C" + cf.EffectiveDate.Year + "-"
                    + cf_id;

            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
        }