示例#1
0
        public static bool SendTransactionDB(DataTable tablePajak, DataTable tableLampiran, string queryPajak, string queryLampiran, string urlApi,
                                             string username, List <SettingDBEntity> lstDB, string jenisPajak, string CultureInfos, out string errMsg, out string sccMsg)
        {
            //Read POF Name
            errMsg = string.Empty;
            sccMsg = string.Empty;
            string serialFile = "POFtpSender.pof";
            string messageReg = string.Empty;
            string mode       = string.Empty;

            ValidateRuntimeMode(serialFile, out messageReg, out mode);
            if (mode.ToUpper().Contains("TRIAL"))
            {
                errMsg = "Tidak dapat mengirim data, Aplikasi dalam mode TRIAL";
                return(false);
            }


            //Send into API
            try
            {
                DBPostData dbData = new DBPostData();
                dbData.dtPajak      = tablePajak;
                dbData.dtLampiran   = tableLampiran;
                dbData.CultureInfos = CultureInfos;
                dbData.lstQuery     = new List <NopQuery>();
                var lstNopPajak = lstDB.Where(m => m.jenisPajak.Equals(jenisPajak)).ToList();
                foreach (var itemQuery in lstNopPajak)
                {
                    NopQuery nopQuery = new NopQuery();
                    nopQuery.nop           = itemQuery.nop;
                    nopQuery.queryPajak    = queryPajak;
                    nopQuery.queryLampiran = queryLampiran;

                    dbData.lstQuery.Add(nopQuery);
                }

                DateTime tglTransaksi = Convert.ToDateTime(tablePajak.Rows[0]["TGL_TRANSAKSI"]);

                var httpWebRequestDB = (HttpWebRequest)WebRequest.Create(urlApi + "/dev/postTransactionClient");

                var           jsonBodyDB = JsonConvert.SerializeObject(dbData, Newtonsoft.Json.Formatting.Indented);
                StringBuilder sbDB       = new StringBuilder();
                sbDB.Append("{\"datauser\":[{\"username\":\"" + username + "\",\"key\":\"datatable\"}],");
                sbDB.Append("\"masapajak\":[{\"bulan\":\"" + tglTransaksi.Month.ToString() + "\",\"tahun\":\"" + tglTransaksi.Year.ToString() + "\"}],");
                sbDB.Append("\"items\":" + jsonBodyDB + "}");
                httpWebRequestDB.ContentType = "application/json";
                httpWebRequestDB.Method      = "POST";
                using (var streamWriter = new StreamWriter(httpWebRequestDB.GetRequestStream()))
                {
                    streamWriter.Write(sbDB.ToString());
                    streamWriter.Flush();
                    streamWriter.Close();
                }

                var    httpResponseDB = (HttpWebResponse)httpWebRequestDB.GetResponse();
                int    statusCode     = 0;
                string pesan          = string.Empty;
                using (var streamReader = new StreamReader(httpResponseDB.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    Newtonsoft.Json.Linq.JObject jObj     = Newtonsoft.Json.Linq.JObject.Parse(result);
                    Newtonsoft.Json.Linq.JToken  jCode    = jObj["code"];
                    Newtonsoft.Json.Linq.JToken  jMessage = jObj["message"];
                    statusCode = Convert.ToInt32(jCode);
                    pesan      = jMessage.ToString();
                }

                if (statusCode == 200)
                {
                    sccMsg = "Data sukses terkirim ke server.";
                    //_errGetDataMsg.AppendLine(DateTime.Now.ToString("HH:mm:ss") + " = " + "Data sukses terkirim ke server.");
                }
                else
                {
                    errMsg = "Data gagal terkirim, " + pesan;
                    //_errGetDataMsg.AppendLine(DateTime.Now.ToString("HH:mm:ss") + " = " + "Data gagal terkirim, " + pesan);
                }
                httpResponseDB.Close();

                return(true);
            }
            catch (Exception ex)
            {
                errMsg = "Terjadi kesalahan : " + ex.Message;
                return(false);
            }
        }
示例#2
0
        public static bool SendTransactionFile(string urlApi, DataTable tableTransaction, string username, int iBln, int iThn, string filePath,
                                               string successPath, string failedPath, string cultureInfos, out List <string> errMsg, out List <string> sccMsg)
        {
            //Read POF Name
            errMsg = new List <string>();
            sccMsg = new List <string>();
            string serialFile = "POFtpSender.pof";
            string messageReg = string.Empty;
            string mode       = string.Empty;

            ValidateRuntimeMode(serialFile, out messageReg, out mode);
            if (mode.ToUpper().Contains("TRIAL"))
            {
                errMsg.Add("Tidak dapat mengirim data, Aplikasi dalam mode TRIAL");
                return(false);
            }

            try
            {
                var        httpWebRequest = (HttpWebRequest)WebRequest.Create(urlApi + "/dev/postTransactionClient");
                DBPostData postData       = new DBPostData();
                postData.dtPajak      = tableTransaction;
                postData.dtLampiran   = tableTransaction;
                postData.CultureInfos = cultureInfos;
                postData.lstQuery     = new List <NopQuery>();


                var           jsonBody = JsonConvert.SerializeObject(postData, Newtonsoft.Json.Formatting.Indented);
                StringBuilder sb       = new StringBuilder();
                sb.Append("{\"datauser\":[{\"username\":\"" + username + "\",\"key\":\"datatable\"}],");
                sb.Append("\"masapajak\":[{\"bulan\":\"" + iBln.ToString() + "\",\"tahun\":\"" + iThn.ToString() + "\"}],");
                sb.Append("\"items\":" + jsonBody + "}");
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method      = "POST";
                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    streamWriter.Write(sb.ToString());
                    streamWriter.Flush();
                    streamWriter.Close();
                }

                var      httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                int      statusCode   = 0;
                string   pesan        = string.Empty;
                FileInfo fi           = new FileInfo(filePath);
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    Newtonsoft.Json.Linq.JObject jObj     = Newtonsoft.Json.Linq.JObject.Parse(result);
                    Newtonsoft.Json.Linq.JToken  jCode    = jObj["code"];
                    Newtonsoft.Json.Linq.JToken  jMessage = jObj["message"];
                    statusCode = Convert.ToInt32(jCode);
                    pesan      = jMessage.ToString();
                }

                if (statusCode == 200)
                {
                    //Move file to success directory
                    fi.MoveTo(successPath + @"\" + DateTime.Now.ToString("ddMMyyHHmmss") + "_" + fi.Name);
                    sccMsg.Add("Data sukses terkirim ke FTP");
                    sccMsg.Add("File " + fi.Name + " dipindah ke folder SUCCESS.");
                    httpResponse.Close();
                    return(true);
                }
                else
                {
                    //Move file to failed directory
                    fi.MoveTo(failedPath + @"\" + DateTime.Now.ToString("ddMMyyHHmmss") + "_" + fi.Name);
                    errMsg.Add("Data gagal terkirim : " + pesan);
                    errMsg.Add("File " + fi.Name + " dipindah ke folder FAILED.");
                    httpResponse.Close();
                    return(true);
                }
            }
            catch (Exception ex)
            {
                errMsg.Add("Data gagal terkirim : " + ex.Message);
                return(false);
            }
        }