using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DSWeb.Models;
using DSWeb.EntityDA;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
using DSWeb.WorkFlow;
using DSWeb.Log;
namespace DSWeb.Settlements
{
public partial class DoRecvApplication : System.Web.UI.Page
{
private string strBillNO;
private string strUserID;
private string strCacheName;
private string strSettleCurrency;
private decimal usdToRmb;
private decimal rmbToUsd;
private string strModuleName;//模块名称
private string strCompanyID;//公司GID
private string strDeptName;//部门名称
private string strShowName;
private string stroplb;//业务类别
protected void Page_Load(object sender, EventArgs e)
{
if (Session["USERID"] != null)
{
this.strUserID = Session["USERID"].ToString();
}
if (Session["SHOWNAME"] != null)
{
this.strShowName = Session["SHOWNAME"].ToString();
}
if (Session["COMPANYID"] != null)
{
strCompanyID = Session["COMPANYID"].ToString();
}
if (Session["DEPTNAME"] != null)
{
strDeptName = Session["DEPTNAME"].ToString();
}
if (Request.QueryString["oplb"] != null)
{
stroplb = Request.QueryString["oplb"].ToString();
}
if (Request.QueryString["mod"] != null)
{
strModuleName = Request.QueryString["mod"].ToString();
}
if (Request.QueryString["obj"] != null)
{
strCacheName = Request.QueryString["obj"].ToString();
}
if (Request.QueryString["currency"] != null)
{
strSettleCurrency = Request.QueryString["currency"].ToString();
h_settlecurrency.Value = strSettleCurrency;
}
if (Request.QueryString["uex"] != null)
{
rmbToUsd = decimal.Parse(Request.QueryString["uex"].ToString());
h_rmbtousd.Value = rmbToUsd.ToString();
}
if (Request.QueryString["rex"] != null)
{
usdToRmb = decimal.Parse(Request.QueryString["rex"].ToString());
h_usdtormb.Value = usdToRmb.ToString();
}
InitDoSettlement();
if(ViewState["enter"] == null)
{
ResovlePostSessionTable(strCacheName);
ViewState["enter"] = "0";//已经完成初始化
}
else
{
if (h_enter.Value.Trim().Equals("2"))
{
if (ViewState["enter"] != null)
{
PostPaySettleRequest(strCacheName);
}
}
}
}
#region 提交收费申请
///
/// 提交收费申请
///
private void PostPaySettleRequest(string tempPostSessionName)
{
DataTable bsnoTable = null;
DataTable feeTable = null;
DataTable totalTable = null;
DataSet postSet = null;
if (Session[tempPostSessionName] != null)
{
postSet = (DataSet)Session[tempPostSessionName];
}
bsnoTable = (DataTable)postSet.Tables[0];
feeTable = (DataTable)postSet.Tables[1];
totalTable = (DataTable)postSet.Tables[2];
if (bsnoTable == null || feeTable == null || totalTable == null || bsnoTable.Rows.Count < 1 || feeTable.Rows.Count < 1 || totalTable.Rows.Count < 1)
{
ViewState["enter"] = "2";
Page.ClientScript.RegisterStartupScript(this.GetType(), "key2", "");
return;
}
int result = 0;//为0表示只有一种业务类型,大于0表示有多个
string bsnotypeName = GetOplbnameByBsno(bsnoTable.Rows[0]["BSNO"].ToString().Trim());
for (int i = 1; i < bsnoTable.Rows.Count; i++)
{
if (bsnotypeName != GetOplbnameByBsno(bsnoTable.Rows[i]["BSNO"].ToString().Trim()))
{
result++;
break;
}
else
{
continue;
}
}
string strFeePayApplicationID = Guid.NewGuid().ToString();
string strCustomerName = "";
//收费申请费用明细信息
IList feeDoEntities = new List();
for (int j = 0; j < bsnoTable.Rows.Count; j++)
{
for (int i = 0; i < feeTable.Rows.Count; i++)
{
if (i == 0)
{
strCustomerName = feeTable.Rows[i]["CUSTOMERNAME"].ToString();
}
if (bsnoTable.Rows[j]["BSNO"].ToString().Trim() != feeTable.Rows[i]["BSNO"].ToString().Trim())
{
continue;
}
FeeDoEntity feeDoEntity = new FeeDoEntity();
feeDoEntity.FeeID = feeTable.Rows[i]["GID"].ToString();
feeDoEntity.FeeType = int.Parse(feeTable.Rows[i]["FEETYPE"].ToString());
feeDoEntity.Currency = feeTable.Rows[i]["CURRENCY"].ToString();
feeDoEntity.Amount = decimal.Parse(feeTable.Rows[i]["AMOUNT"].ToString());
feeDoEntity.DoAmount = decimal.Parse(feeTable.Rows[i]["CTL"].ToString());
feeDoEntity.UnsettleAmount = decimal.Parse(feeTable.Rows[i]["UCTL"].ToString());
feeDoEntity.CustomerName = feeTable.Rows[i]["CUSTOMERNAME"].ToString();
feeDoEntity.FeeName = feeTable.Rows[i]["FEENAME"].ToString();
feeDoEntity.OriginalFeeName = feeTable.Rows[i]["FEENAME"].ToString();
feeDoEntity.OriginalFeeCSTL = decimal.Parse(feeTable.Rows[i]["CTL"].ToString());
if (strSettleCurrency == "1")//RMB
{
feeDoEntity.ExchangeRate = usdToRmb;
}
else if (strSettleCurrency == "2")//USD
{
if (rmbToUsd == 0 || rmbToUsd.ToString().Trim().Equals("0.00000") || rmbToUsd == 1 || rmbToUsd.ToString().Trim().Equals("1.00000"))
{
feeDoEntity.ExchangeRate = decimal.Parse(feeTable.Rows[i]["EXCHANGERATE"].ToString());
}
else
{
feeDoEntity.ExchangeRate = rmbToUsd;
}
}
else
{
feeDoEntity.ExchangeRate = decimal.Parse(feeTable.Rows[i]["EXCHANGERATE"].ToString());
}
feeDoEntity.BSNO = bsnoTable.Rows[j]["BSNO"].ToString();
feeDoEntity.MBLNO = bsnoTable.Rows[j]["MBLNO"].ToString();
feeDoEntity.HBLNO = bsnoTable.Rows[j]["HBLNO"].ToString();
feeDoEntity.BillNO = "";
feeDoEntity.GID = Guid.NewGuid().ToString();
feeDoEntity.Category = 4;
feeDoEntity.BSType = 1;
feeDoEntity.BillStatus = 2;
feeDoEntity.Remark = "";
feeDoEntity.CompanyID = strCompanyID;
feeDoEntities.Add(feeDoEntity);
}
}
//收费申请表信息
ChFeeRecvApplicationEntity ChFeeRecvApplicationEntity = new ChFeeRecvApplicationEntity();
ChFeeRecvApplicationEntity.GID = strFeePayApplicationID;
decimal totalRmb = decimal.Parse(totalTable.Rows[0]["TOTALRMB"].ToString());
decimal totalUsd = decimal.Parse(totalTable.Rows[0]["TOTALUSD"].ToString());
decimal FinalRmd = 0;
decimal FinalUsd = 0;
if (strSettleCurrency == "1")
{
//人民币结算
FinalRmd = (StatisticSettleAmount(totalUsd, usdToRmb) + totalRmb);
//折算汇率
ChFeeRecvApplicationEntity.RATE = totalUsd != 0 ? usdToRmb : 1;
}
else if (strSettleCurrency == "2")
{
//美元结算
FinalUsd = (StatisticSettleAmount(totalRmb, rmbToUsd) + totalUsd);
//折算汇率
ChFeeRecvApplicationEntity.RATE = totalRmb != 0 ? rmbToUsd : 1;
}
else if (strSettleCurrency == "3")
{
FinalRmd = totalRmb;
FinalUsd = totalUsd;
ChFeeRecvApplicationEntity.RATE = 1;
}
ChFeeRecvApplicationEntity.AMOUNTRMB = FinalRmd;
ChFeeRecvApplicationEntity.AMOUNTUSD = FinalUsd;
ChFeeRecvApplicationEntity.BILLSTATUS = 0;
ChFeeRecvApplicationEntity.INVOICETITLE = txt_chequeNo.Value.Trim();
ChFeeRecvApplicationEntity.APPLICANT = strUserID;
ChFeeRecvApplicationEntity.APPLYTIME = DateTime.Parse(txt_applyTime.Value.Trim());
ChFeeRecvApplicationEntity.RECVTIME = txt_payableDate.Value.Trim().Equals("") ? System.DateTime.MinValue : DateTime.Parse(txt_payableDate.Value.Trim());
ChFeeRecvApplicationEntity.CUSTOMERNAME = strCustomerName;//结算单位
ChFeeRecvApplicationEntity.SETTLETYPE = int.Parse(this.sel_settlemode.Value);
ChFeeRecvApplicationEntity.REMARK = txt_remark.Value.Trim();
ChFeeRecvApplicationEntity.COMPANYID = strCompanyID;
ChFeeRecvApplicationEntity.INVOICENUMREMARK = this.chequenumermark.Text.Trim();//发票号备注
ChFeeRecvApplicationDA ChFeeRecvApplicationDA = new ChFeeRecvApplicationDA();
int iResult = ChFeeRecvApplicationDA.InsertFeeRecvApplication(feeDoEntities, ChFeeRecvApplicationEntity);
if (iResult == 1)
{
ViewState["enter"] = "1";
this.btn_enter.Disabled = true;
LogEntity logEntity = new LogEntity();
logEntity.GID = Guid.NewGuid().ToString();
logEntity.Name = "添加收费申请信息";
logEntity.LogType = "INSERT 插入操作";
logEntity.CreateUserID = strUserID;
logEntity.LogContent = strShowName + String.Format(Logger.Instance.GetLogContentTemplate(Logger.LogTypes.INSERT), "收费申请", "申请编号:" + strBillNO);
Logger.Instance.WriteLog(logEntity);
ViewState["PaySettleAppID"] = strFeePayApplicationID;
Page.ClientScript.RegisterStartupScript(this.GetType(), "key1", "");
}
else
{
ViewState["enter"] = "2";
Page.ClientScript.RegisterStartupScript(this.GetType(), "key2", "");
}
}
#endregion
///
/// 获取业务类型
///
///
///
protected string GetOplbnameByBsno(string bsno)
{
T_ALL_DA tallda = new T_ALL_DA();
return tallda.GetStrSQL("OPLBNAME", "SELECT OPLBNAME FROM VW_Settlement WHERE BSNO='" + bsno.Trim() + "'");
}
private void InitDoSettlement()
{
txt_applicant.Value = strShowName;
txt_applyTime.Value = DateTime.Now.ToString();
txt_billStatus.Value = "锁定";
}
#region 获取缓存收费申请合计信息
///
/// 获取缓存收费申请合计信息
///
/// 缓存名称
private void ResovlePostSessionTable(string tempPostSessionName)
{
DataTable bsnoTable = null;
DataTable feeTable = null;
DataTable totalTable = null;
DataSet postSet = null;
if (Session[tempPostSessionName] != null)
{
postSet = (DataSet)Session[tempPostSessionName];
}
bsnoTable = (DataTable)postSet.Tables[0];
feeTable = (DataTable)postSet.Tables[1];
totalTable = (DataTable)postSet.Tables[2];
StringBuilder totalBuilder = new StringBuilder();
totalBuilder.Append("{");
totalBuilder.Append("\"totals\":");
totalBuilder.Append("[");
totalBuilder.Append("{");
decimal totalRmb = decimal.Parse(totalTable.Rows[0]["TOTALRMB"].ToString());
decimal totalUsd = decimal.Parse(totalTable.Rows[0]["TOTALUSD"].ToString());
decimal FinalRmd = 0;
decimal FinalUsd = 0;
string currency = "";
if (strSettleCurrency == "1")
{
//人民币结算
FinalRmd = (StatisticSettleAmount(totalUsd, usdToRmb) + totalRmb);
currency = "RMB";
}
else if (strSettleCurrency == "2")
{
//美元结算
FinalUsd = (StatisticSettleAmount(totalRmb, rmbToUsd) + totalUsd);
currency = "USD";
}
else if (strSettleCurrency == "3")
{
FinalRmd = totalRmb;
FinalUsd = totalUsd;
}
totalBuilder.Append("\"recvrmb\":" + totalTable.Rows[0]["RECVRMB"].ToString() + ",");
totalBuilder.Append("\"recvusd\":" + totalTable.Rows[0]["RECVUSD"].ToString() + ",");
totalBuilder.Append("\"payrmb\":" + totalTable.Rows[0]["PAYRMB"].ToString() + ",");
totalBuilder.Append("\"payusd\":" + totalTable.Rows[0]["PAYUSD"].ToString() + ",");
totalBuilder.Append("\"totalrmb\":" + FinalRmd.ToString() + ",");
totalBuilder.Append("\"totalusd\":" + FinalUsd.ToString() + ",");
totalBuilder.Append("\"currency\":\"" + currency + "\"");
totalBuilder.Append("}");
totalBuilder.Append("]");
totalBuilder.Append("}");
h_total.Value = totalBuilder.ToString();
}
#endregion
#region 不同币别金额换算
///
/// 不同币别金额换算
///
/// 原币别金额
/// 汇率
/// 返回折算后的金额
private decimal StatisticSettleAmount(decimal tempOrigAmount,decimal tempRate)
{
decimal result = 0;
result = decimal.Parse(C1Round((double.Parse((tempOrigAmount * tempRate).ToString())),2).ToString());
return result;
}
#endregion
#region 金额四舍五入
///
/// 金额四舍五入
///
/// 金额值
/// 小数点后位数
/// 返回Double型四舍五入金额
public double C1Round(double value, int digit)
{
double vt = Math.Pow(10, digit);
double vx = value * vt;
vx += 0.5;
return (Math.Floor(vx) / vt);
}
#endregion
private string GetSettleTypeName(int iSettleType)
{
string strResult = "";
switch (iSettleType)
{
case 1:
strResult = "现金";
break;
case 2:
strResult = "发票";
break;
case 3:
strResult = "电汇";
break;
case 4:
strResult = "转账";
break;
case 5:
strResult = "承兑汇票";
break;
}
return strResult;
}
}
}