using System;
using CoreSavingLibrary;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Sybase.DataWindow;
using DataLibrary;
using CoreSavingLibrary.WcfNKeeping;
using CoreSavingLibrary.WcfNShrlon;
using System.Web.Services.Protocols;
using System.Globalization;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.ServiceModel.Description;
using System.ServiceModel;
namespace Saving.Applications.keeping
{
public partial class w_sheet_kp_proc_loanpayment_summary : PageWebSheet, WebSheet
{
CultureInfo th = System.Globalization.CultureInfo.GetCultureInfo("th-TH");
//ประกาศตัวแปร
#region Variable
private n_keepingClient KeepingService;
private n_shrlon NshrlonService;
protected String postNewClear;
protected String postRefresh;
protected String postProcClsDay;
private DwThDate tDwOption;
protected String postMonth;
//mikekong new process
public string outputProcess;
protected String Call_process;
public String logsFilePath = "";
#endregion
//============================================
#region Websheet Members
public void InitJsPostBack()
{
tDwOption = new DwThDate(Dw_option, this);
tDwOption.Add("process_date", "process_tdate");
tDwOption.Add("sbfyear_date", "sbfyear_tdate");
tDwOption.Add("s_date", "s_tdate");
tDwOption.Add("e_date", "e_tdate");
tDwOption.Add("nx_date", "nx_tdate");
//=========================================
postNewClear = WebUtil.JsPostBack(this, "postNewClear");
postRefresh = WebUtil.JsPostBack(this, "postRefresh");
postProcClsDay = WebUtil.JsPostBack(this, "postProcClsDay");
postMonth = WebUtil.JsPostBack(this, "postMonth");
Call_process = WebUtil.JsPostBack(this, "Call_process");
}
public void WebSheetLoadBegin()
{
Hd_process.Value = "false";
if (!IsPostBack)
{
JspostNewClear();
// of_checkclsday();
}
else
{
this.RestoreContextDw(Dw_option);
}
}
public void CheckJsPostBack(string eventArg)
{
// Event ที่เกิดจาก JavaScript
switch (eventArg)
{
case "postNewClear":
JspostNewClear();
break;
case "postRefresh":
//Refresh();
break;
case "postProcClsDay":
JspostProcClsDay();
break;
case "Call_process":
update_interest();
break;
case "postMonth":
//เปิดไว้ก่อนปิดสิ้นปี 59
int year = Convert.ToInt32(Dw_option.GetItemDecimal(1, "year_no")); //ปี 59
int diff = year - (WebUtil.GetDateAccyear(state.SsCoopId, state.SsWorkDate).Year + 543);
// int year = WebUtil.GetDateAccyear(state.SsCoopId, state.SsWorkDate).Year; //ปี 59
DateTime accyear = WebUtil.GetDateAccyear(state.SsCoopId, state.SsWorkDate);
int month = Convert.ToInt32(Dw_option.GetItemDecimal(1, "month_no"));//accyear.Month;
Dw_option.SetItemDecimal(1, "year_no", year);
Dw_option.SetItemDecimal(1, "month_no", month);
DateTime sbfyear_date = new DateTime(year - 1, 4, 1, 0, 0, 0, 0);
sbfyear_date = accyear.AddYears(-1 + diff);
DateTime s_date = accyear.AddYears(diff);// new DateTime(year, 4, 1, 0, 0, 0, 0);
DateTime e_date = accyear.AddMonths(12).AddDays(-1).AddYears(diff);// new DateTime(year + 1, 3, 31, 0, 0, 0, 0);
DateTime nx_date = accyear.AddYears(2 + diff).AddDays(-1);// new DateTime(year + 2, 3, 31, 0, 0, 0, 0);
Dw_option.SetItemDateTime(1, "sbfyear_date", sbfyear_date);
Dw_option.SetItemDateTime(1, "s_date", s_date);
Dw_option.SetItemDateTime(1, "e_date", e_date);
Dw_option.SetItemDateTime(1, "process_date", e_date);
Dw_option.SetItemDateTime(1, "nx_date", nx_date);
Hdprocess.Value = e_date.ToShortDateString();// "3103" + Convert.ToString(year + 544);
Hdsdate.Value = s_date.ToShortDateString();//"0104" + Convert.ToString(year + 543);
Hdyear_no.Value = Convert.ToString(year);
Hdmonth_no.Value = Convert.ToString(month);
tDwOption.Eng2ThaiAllRow();
break;
}
}
public void SaveWebSheet()
{
}
public void WebSheetLoadEnd()
{
Dw_option.SaveDataCache();
}
#endregion
//=============================================
// function เคลียร์หน้าจอ
private void JspostNewClear()
{
Dw_option.Reset();
Dw_option.InsertRow(0);
WebUtil.RetrieveDDDW(Dw_option, "as_slntype", "sl_shlnproc", null);
WebUtil.RetrieveDDDW(Dw_option, "as_sgrp", "sl_shlnproc", state.SsCoopControl);
WebUtil.RetrieveDDDW(Dw_option, "as_elntype", "sl_shlnproc", null);
WebUtil.RetrieveDDDW(Dw_option, "as_egrp", "sl_shlnproc", state.SsCoopControl);
//เปิดไว้ก่อนปิดสิ้นปี 59
int year = WebUtil.GetDateAccyear(state.SsCoopId, state.SsWorkDate).Year; //ปี 59
DateTime accyear = WebUtil.GetDateAccyear(state.SsCoopId, state.SsWorkDate);
int month = accyear.Month-1;
Dw_option.SetItemDecimal(1, "year_no", year + 543);
Dw_option.SetItemDecimal(1, "month_no", month);
DateTime sbfyear_date = new DateTime(year - 1, 4, 1, 0, 0, 0, 0);
sbfyear_date = accyear.AddYears(-1);
DateTime s_date = accyear;// new DateTime(year, 4, 1, 0, 0, 0, 0);
DateTime e_date = accyear.AddMonths(12).AddDays(-1);// new DateTime(year + 1, 3, 31, 0, 0, 0, 0);
DateTime nx_date = accyear.AddYears(2).AddDays(-1);// new DateTime(year + 2, 3, 31, 0, 0, 0, 0);
Dw_option.SetItemDateTime(1, "sbfyear_date", sbfyear_date);
Dw_option.SetItemDateTime(1, "s_date", s_date);
Dw_option.SetItemDateTime(1, "e_date", e_date);
Dw_option.SetItemDateTime(1, "process_date", e_date);
Dw_option.SetItemDateTime(1, "nx_date", nx_date);
//Dw_option.SetItemString(1, "sbfyear_tdate", "0104" + Convert.ToString(year + 542));
//Dw_option.SetItemString(1, "s_tdate", "0104" + Convert.ToString(year + 543));
//Dw_option.SetItemString(1, "e_tdate", "3103" + Convert.ToString(year + 544));
//Dw_option.SetItemString(1, "process_tdate", "3103" + Convert.ToString(year + 544));
//Dw_option.SetItemString(1, "nx_tdate", "3103" + Convert.ToString(year + 545));
Hdprocess.Value = e_date.ToShortDateString();// "3103" + Convert.ToString(year + 544);
Hdsdate.Value = s_date.ToShortDateString();//"0104" + Convert.ToString(year + 543);
Hdyear_no.Value = Convert.ToString(year + 543);
Hdmonth_no.Value = Convert.ToString(month);
//Dw_option.SetItemDate(1, "process_date", e_date);
//Dw_option.SetItemDate(1, "sbfyear_date", sbfyear_date);
//Dw_option.SetItemDate(1, "s_date", s_date);
//Dw_option.SetItemDate(1, "e_date", e_date);
//Dw_option.SetItemDate(1, "nx_date", nx_date);
string[] minmax = ReportUtil.GetMinMaxLoantype();
Dw_option.SetItemString(1, "as_slntype", minmax[0]);
Dw_option.SetItemString(1, "as_elntype", minmax[1]);
string[] minmax2 = ReportUtil.GetMinMaxMembgroup();
Dw_option.SetItemString(1, "as_sgrp", minmax2[0]);
Dw_option.SetItemString(1, "as_egrp", minmax2[1]);
tDwOption.Eng2ThaiAllRow();
}
private void JspostProcClsDay()
{
//DateTime ldtm_clsdate = Dw_option.GetItemDateTime(1, "clsday_date");
//DateTime ldtm_workdate = state.SsWorkDate;
//if (ldtm_clsdate != ldtm_workdate)
//{
// this.SetOnLoadedScript("alert('วันที่ปิดสิ้นวันไม่ใช่วันทำการ \\nวันที่ทำรายการ : " + ldtm_workdate.ToString("dd/MM/yyyy", th) + "\\nกรุณาตรวจสอบ')");
// return;
//}
/// start
string sgroup = Dw_option.GetItemString(1, "as_sgrp").Trim();
string egroup = Dw_option.GetItemString(1, "as_egrp").Trim();
try
{
CultureInfo th = System.Globalization.CultureInfo.GetCultureInfo("th-TH");
// DateTime ss = dw_periodpay.GetItemDate(row, "expireperiod_date");
//*****************
String process_tdate = Dw_option.GetItemString(1, "process_tdate");
Int16 month = Convert.ToInt16(process_tdate.Substring(2, 2));
// month = month;
DateTime loanrcv_dates = DateTime.ParseExact(process_tdate, "ddmmyyyy", null).AddMonths(month - 1).AddYears(-543);
Dw_option.SetItemDate(1, "process_date", loanrcv_dates);
//***************
//*****************
String sbfyear_date = Dw_option.GetItemString(1, "sbfyear_tdate");
Int16 month22 = Convert.ToInt16(sbfyear_date.Substring(2, 2));
// month = month;
DateTime sbfyear_dates = DateTime.ParseExact(sbfyear_date, "ddmmyyyy", null).AddMonths(month22 - 1).AddYears(-543);
Dw_option.SetItemDate(1, "sbfyear_date", sbfyear_dates);
//***************
//*****************
String s_date = Dw_option.GetItemString(1, "s_tdate");
Int16 month3 = Convert.ToInt16(s_date.Substring(2, 2));
// month = month;
DateTime s_dates = DateTime.ParseExact(s_date, "ddmmyyyy", null).AddMonths(month3 - 1).AddYears(-543);
Dw_option.SetItemDate(1, "s_date", s_dates);
//***************
//*****************
String e_date = Dw_option.GetItemString(1, "e_tdate");
Int16 month4 = Convert.ToInt16(process_tdate.Substring(2, 2));
// month = month;
DateTime e_dates = DateTime.ParseExact(e_date, "ddmmyyyy", null).AddMonths(month4 - 1).AddYears(-543);
Dw_option.SetItemDate(1, "e_date", e_dates);
//***************
//*****************
String nx_date = Dw_option.GetItemString(1, "nx_tdate");
Int16 month5 = Convert.ToInt16(process_tdate.Substring(2, 2));
// month = month;
DateTime nx_dates = DateTime.ParseExact(nx_date, "ddmmyyyy", null).AddMonths(month5 - 1).AddYears(-543);
Dw_option.SetItemDate(1, "nx_date", nx_dates);
//***************
// decimal y = Dw_option.GetItemDecimal(1, "intfine_status");
Hdprocess.Value = process_tdate;
Hdsdate.Value = s_date;
NshrlonService = wcf.NShrlon;
String dwMain_XML = Dw_option.Describe("DataWindow.Data.XML");
try
{
string delete = "delete from LNLOANPAYMENTSUMMARY where year_no = " + Dw_option.GetItemDecimal(1, "year_no") + " and month_no = " + Dw_option.GetItemDecimal(1, "month_no") + " ";
delete = WebUtil.SQLFormat(delete);
WebUtil.QuerySdt(delete);
}
catch { }
//mikekong new process
if (state.SsCoopControl == "000301" || state.SsCoopId == "500001")
{
outputProcess = WebUtil.runProcessing(state, "PRO18SKM", dwMain_XML, "", "");
}
else
{
outputProcess = WebUtil.runProcessing(state, "PRO18TRN", dwMain_XML, "", "");
}
// start
}
catch (SoapException ex)
{
LtServerMessage.Text = WebUtil.ErrorMessage(WebUtil.SoapMessage(ex));
}
catch (Exception ex)
{
LtServerMessage.Text = WebUtil.ErrorMessage(ex.ToString());
}
//end
}
public void update_interest()
{
// intfine_status
this.connectionString = state.SsConnectionString;
String slip_date = state.SsWorkDate.ToShortDateString(); // รับค่าจากหน้าจอ
String coop_id = state.SsCoopId;
String log_file = "trans_log_" + coop_id.Replace(",", "-") + "_" + slip_date.Replace("/", "-") + ".log";
try
{
String xml = "" +
"" + coop_id + "" +
"" + slip_date + "" +
"1" +
"1" +
"" + log_file + ""; //post_flag 0:View , 1:run Import
outputProcess = WebUtil.runProcessingCappExtend(state, "runPostProcess", xml, "", "");
//Task.Factory.StartNew(() => this.runProgress(state,"runPostProcess", xml, "", ""));
logsFilePath = Environment.GetEnvironmentVariable("windir") + "\\Temp\\" + log_file;
HdLogFile.Value = logsFilePath;
Task.Factory.StartNew(() => this.runObjectProcessing(outputProcess, this.xmlconfig, "runPostProcess", xml, "", ""));
//this.runObjectProcessing(outputProcess, "runPostProcess", xml, "", "");
File.WriteAllText(logsFilePath, "<<ประมวลเสร็จกดดูรายละเอียดที่ Log>>");
}
catch (Exception ex)
{
LtServerMessage.Text = WebUtil.ErrorMessage(ex);
}
finally
{
}
}
public void runProgress(WebState state, String obj, String xml1, String xml2, String xml3)
{
outputProcess = WebUtil.runProcessingCappExtend(state, obj, xml1, xml2, xml3);
}
private XmlConfigService xml;
//ส่วนเรียก Processing หลัก
public void runObjectProcessing(String outputProcess, XmlConfigService xmlConfig, String object_name, String criteria_xml, String criteria_xml_1, String criteria_xml_2)
{
this.xml = xmlConfig;
initProcessing(outputProcess, object_name, criteria_xml, criteria_xml_1, criteria_xml_2);
if (object_name == "runPostProcess")
{
WriteLine("runPostProcess");
runPostProcess();
}
closeProcessing("Main");
}
//สร้าง Custom method สำหรับ ประมวล
public void runPostProcess()
{
int allStep = 2;//กำหนด Step ทั้งหมด
//ดึงค่าจาก XML criteria_xml = FIELD payrate_code
string[] coop_id = getXMLStringValue(criteria_xml, "coop_id").Split(',');
string slip_date = getXMLStringValue(criteria_xml, "slip_date");
string log_file = getXMLStringValue(criteria_xml, "log_file");
bool logssql_flag = getXMLStringValue(criteria_xml, "logssql_flag") == "1";
bool post_flag = getXMLStringValue(criteria_xml, "post_flag") == "1";
DateTime process_date = DateTime.Now;
DateTime slip_date_dt = DateTime.ParseExact(slip_date, "MM/dd/yyyy", new CultureInfo("en-US"));
String slip_date_str = slip_date_dt.ToString("yyyy/MM/dd");
String slip_yymm = (Convert.ToInt32(slip_date_dt.ToString("yyyy")) + 543 - 2500) + slip_date_dt.ToString("MM") + "0"; //yyMM in Thai
String slip_yymm_new = "S" + (Convert.ToInt32(slip_date_dt.ToString("yyyy")) + 543 - 2500) + slip_date_dt.ToString("MM") + ""; //yyMM in Thai
String results = "", results_sql = "";
startProcessMsgLog("กำลังดำเนินการเริ่มประมวลผล ดึงข้อมูล " + slip_date + ":" + coop_id.ToString(), allStep);
//Thread.Sleep(5000);
String coopid = "", msg = "";
String sql = "", sql_where = "";
Sta taSrc = null;
Sta taSrcQuery = null;
//เขียนงานที่ต้องทำ ตรงนี้
bool status = false;
String msg_err = "";
String system_code = "SHR", source_code = "WFS";
logResultTextFile(logsFilePath, ref results, ref results_sql, true, post_flag, true);
try
{
taSrc = new Sta(state.SsConnectionString);
taSrcQuery = new Sta(state.SsConnectionString);
taSrc.Transection();
results += (post_flag ? "ประมวลข้อมูล" : "ตรวจสอบประมวลข้อมูล") + " \r\n";
msg = "STEP:" + 1 + ":เตรียมประมวลข้อมูล ";
updateProcessMsgLog(msg, 1);
sql = "";//ดึงค่าคงที่ มาเพื่อทำการ Loop ประมวลตั้งยอด
results += msg + " \r\n";
results_sql += sql + " ;\r\n";
logResultTextFile(logsFilePath, ref results, ref results_sql, true, post_flag, false);
//1.ประมวลดึงรายการตาม Table ตั้งยอดรายเดือนของสมาชิกแต่ละคนมายิงสร้าง SLTRANSPAYIN ไว้
msg = "STEP:" + 1 + ":กำลังคำนวณ ด/บ และค่าปรับพึง ";
updateProcessMsgLog(msg, 1);//Update processing message
results += msg + " \r\n";
sql = "";
results_sql += sql + " ;\r\n";
logResultTextFile(logsFilePath, ref results, ref results_sql, true, post_flag, false);
//ดึงจากรายการ Table ตั้ง วงเงินชำระหุ้นต่อเดือน มา Loop insert
// loop sltranmaster
try
{
string format = "MMddyyyy";
String process_tdate = Hdprocess.Value;
process_tdate = process_tdate.Replace("/", "");
int years = Convert.ToInt32(process_tdate.Substring(4));
if (years > 2500)
{
years = years - 543;
process_tdate = process_tdate.Substring(0, 4) + years;
format = "ddMMyyyy";
}
DateTime process_dates = DateTime.ParseExact(process_tdate, format, CultureInfo.InvariantCulture);
String s_date = Hdsdate.Value;
s_date = s_date.Replace("/", "");
int years2 = Convert.ToInt32(s_date.Substring(4));
if (years2 > 2500)
{
years2 = years2 - 543;
s_date = s_date.Substring(0, 4) + years2;
format = "ddMMyyyy";
}
DateTime s_dates = DateTime.ParseExact(s_date, format, CultureInfo.InvariantCulture);
// DateTime s_dates = DateTime.ParseExact(s_date, "mmddyyyy", null).AddMonths(month3 - 1).AddYears(-543);
int year = Convert.ToInt32(Hdyear_no.Value);
int months = Convert.ToInt32(Hdmonth_no.Value);
string select_lnloanpaymentsummary = @"select lnloanpaymentsummary.loancontract_no,intinyear_amt,lnfineinyear_amt from lnloanpaymentsummary
inner join lncontmaster on trim(lnloanpaymentsummary.loancontract_no) = trim(lncontmaster.loancontract_no)
where (lnloanpaymentsummary.year_no={2} and lnloanpaymentsummary.month_no={3} ) and (lncontmaster.lastcalint_date between {1} and {0} )
group by lnloanpaymentsummary.loancontract_no,intinyear_amt,lnfineinyear_amt ";
select_lnloanpaymentsummary = WebUtil.SQLFormat(select_lnloanpaymentsummary, process_dates, s_dates.AddDays(-1)
, year, months);
// Sdt d = WebUtil.QuerySdt(select_lnloanpaymentsummary);
Sdt d = new Sdt();
d = taSrcQuery.Query(select_lnloanpaymentsummary);
while (d.Next())
{
string loancontract_no = d.GetString("loancontract_no");
decimal int_new = WebUtil.Calint_bar(loancontract_no, process_dates, state.SsCoopId, taSrc, taSrcQuery);
decimal fine_new = WebUtil.calfine_new_bar(loancontract_no, process_dates, state.SsCoopId, taSrc, taSrcQuery);
decimal interest = d.GetDecimal("intinyear_amt") + int_new;
decimal lnfineinyear_amt = d.GetDecimal("lnfineinyear_amt") + fine_new;
string update = @"update LNLOANPAYMENTSUMMARY set intinyear_amt = {0},lnfineinyear_amt = {1},intarrforward_amt=intarrforward_amt+{5},lnfineforward_amt=lnfineforward_amt+{6} where loancontract_no={2} and (year_no={3} and month_no={4} ) ";
update = WebUtil.SQLFormat(update, interest, lnfineinyear_amt, loancontract_no, year, months, int_new, fine_new);
// WebUtil.QuerySdt(update);
taSrcQuery.Query(update);
msg = " เลขสัญญา " + loancontract_no + " [" + d.GetRowIndex() + "/" + d.GetRowCount() + "]";
if ((d.GetRowIndex() % 10) == 2)
{
taSrcQuery.Query("update cmprocessing set runtime_message='" + msg + "' where process_id='" + process_id + "'");
// execAutoCommit(sql);
}
results += msg + " \r\n";
logResultTextFile(logsFilePath, ref results, ref results_sql, true, post_flag, false);
}
// taSrcQuery.Commit();
status = true;
// d.Clear();
// LtServerMessage.Text = WebUtil.CompleteMessage("คำนวน ด/บ - ค่าปรับ พึง เสร็จเรียบร้อย");
}
catch (SoapException ex)
{
LtServerMessage.Text = WebUtil.ErrorMessage(WebUtil.SoapMessage(ex));
}
}
catch (Exception e)
{
WriteLine(e.Message);
//กรณีทำรายการ และ Error
msg_err = e.Message;
status = false;
}
finally
{
try
{
taSrc.Close();
taSrcQuery.Close();
wcf.Close();
}
catch { }
}
//Thread.Sleep(3000);
//กรณีทำรายการเสร็จสมบรูณ์
if (status)
{
finishProcessMsgLog("ทำรายการสำเร็จ", allStep);
}
else
{
cancelProcessMsgLog("ทำรายการไม่สำเร็จ" + msg_err + " ");
}
}
public double getPricipalBalance(String loancontract)
{
double principal_balance = 0;
string sql = @"select principal_balance from lncontmaster where loancontract_no='" + loancontract + "' ";
sql = WebUtil.SQLFormat(sql, loancontract);
Sdt dt = WebUtil.QuerySdt(sql);
while (dt.Next())
{
principal_balance = dt.GetDouble("principal_balance"); // เงินต้น คงเหลือ
}
return principal_balance;
}
}
}