You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/DSWeb/Settlements/DoPaySettlement.aspx.cs

1228 lines
57 KiB
C#

3 years ago
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.Log;
namespace DSWeb.Settlements
{
public partial class DoPaySettlement : System.Web.UI.Page
{
private string strUserID;//登录操作人GID
private string strShowName;//操作人显示名称
//private string strFeeCache;
private string strCacheName;
private string strSettleCurrency;
private decimal usdToRmb;
private decimal rmbToUsd;
private string strCompanyID;//公司GID
private string strDeptName;//部门名称
protected void Page_Load(object sender, EventArgs e)
{
if (Session["USERID"] != null)
{
strUserID = Session["USERID"].ToString();
}
if (Session["SHOWNAME"] != null)
{
strShowName = Session["SHOWNAME"].ToString();
}
if (Session["COMPANYID"] != null)
{
strCompanyID = Session["COMPANYID"].ToString();
}
if (Session["DEPTNAME"] != null)
{
strDeptName = Session["DEPTNAME"].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();
}
#region ####
//if (!h_enter.Value.Trim().Equals("1"))
//{
// if (Request.QueryString["obj"] != null)
// {
// strCacheName = Request.QueryString["obj"].ToString();
// if (strCacheName != null)
// {
// if (Session[strCacheName] != null)
// {
// strFeeCache = Session[strCacheName].ToString();
// Session.Remove(strCacheName);
// }
// }
// h_feecache.Value = strFeeCache;
// InitDoSettlement();
// }
// 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();
// }
//}
//else
//{
// RecordSettleFee();
//}
#endregion
InitDoSettlement();
if (ViewState["enter"] == null)
{
ResovlePostSessionTable(strCacheName);
ViewState["enter"] = "0";//已经完成初始化
}
else
{
if (h_enter.Value.Trim().Equals("1"))
{
SavePaySettle(strCacheName);
}
else if (h_enter.Value.Trim().Equals("2"))
{
if (ViewState["enter"] != null)
{
PostPaySettle(strCacheName);
}
}
}
}
#region 获取缓存付费结算合计信息
/// <summary>
/// 获取缓存付费结算合计信息
/// </summary>
/// <param name="tempPostSessionName">缓存名称</param>
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;
if (strSettleCurrency == "1")
{
//人民币结算
FinalRmd = (StatisticSettleAmount(totalUsd, usdToRmb) + totalRmb);
}
else if (strSettleCurrency == "2")
{
//美元结算
FinalUsd = (StatisticSettleAmount(totalRmb, rmbToUsd) + totalUsd);
}
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("}");
totalBuilder.Append("]");
totalBuilder.Append("}");
h_total.Value = totalBuilder.ToString();
}
#endregion
private void InitDoSettlement()
{
this.txt_settleUser.Value = strShowName;
if (this.txt_settleTime.Value.Trim() == "")
{
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
string rq = T_ALL_DA.GetStrSQL("rq", "select getdate() as rq");//convert(char(10),getdate(),20)
this.txt_settleTime.Value = rq;
}
this.txt_billStatus.Value = "锁定";
}
#region 保存付费结算
/// <summary>
/// 保存付费结算
/// </summary>
private void SavePaySettle(string tempPostSessionName)
{
int billtype = 0;//1收费2付费
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];
RunFeeDoDA runFeeDoDA = new RunFeeDoDA();
if (ViewState["SaveState"] == null)
{
string strCustomerName = "";
string strRunSettleID = Guid.NewGuid().ToString();
//付费申请费用明细信息
IList<RunFeeDoEntity> runFeeDoEntities = new List<RunFeeDoEntity>();
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;
}
RunFeeDoEntity runFeeDoEntity = new RunFeeDoEntity();
runFeeDoEntity.FeeID = feeTable.Rows[i]["GID"].ToString();
runFeeDoEntity.FeeType = int.Parse(feeTable.Rows[i]["FEETYPE"].ToString());
billtype = runFeeDoEntity.FeeType;
runFeeDoEntity.Currency = feeTable.Rows[i]["CURRENCY"].ToString();
runFeeDoEntity.Amount = decimal.Parse(feeTable.Rows[i]["AMOUNT"].ToString());
runFeeDoEntity.DoAmount = decimal.Parse(feeTable.Rows[i]["CTL"].ToString());
runFeeDoEntity.UnsettleAmount = decimal.Parse(feeTable.Rows[i]["UCTL"].ToString());
runFeeDoEntity.CustomerName = feeTable.Rows[i]["CUSTOMERNAME"].ToString();
runFeeDoEntity.FeeName = feeTable.Rows[i]["FEENAME"].ToString();
runFeeDoEntity.OriginalFeeName = feeTable.Rows[i]["CUSTOMERNAME"].ToString();
runFeeDoEntity.OriginalFeeCSTL = decimal.Parse(feeTable.Rows[i]["CTL"].ToString());
if (strSettleCurrency == "1")
{
runFeeDoEntity.ExchangeRate = usdToRmb;
}
else if (strSettleCurrency == "2")
{
runFeeDoEntity.ExchangeRate = rmbToUsd;
}
runFeeDoEntity.BSNO = bsnoTable.Rows[j]["BSNO"].ToString();
runFeeDoEntity.MBLNO = bsnoTable.Rows[j]["MBLNO"].ToString();
runFeeDoEntity.HBLNO = bsnoTable.Rows[j]["HBLNO"].ToString();
runFeeDoEntity.GID = Guid.NewGuid().ToString();
runFeeDoEntity.Category = 4;
runFeeDoEntity.BSType = 1;
runFeeDoEntity.BillStatus = 2;
runFeeDoEntity.Remark = "";
runFeeDoEntity.OrigCurrency = "";
runFeeDoEntity.RunSettleID = strRunSettleID;
runFeeDoEntity.InvoiceSettleNO = "";
runFeeDoEntity.CreateUser = strUserID;
runFeeDoEntity.CompanyID = strCompanyID;
runFeeDoEntities.Add(runFeeDoEntity);
}
}
RunFeeSettlementEntity runFeeSettlementEntity = new RunFeeSettlementEntity();
runFeeSettlementEntity.GID = strRunSettleID;
runFeeSettlementEntity.BillType= billtype;//由 runFeeDoEntity.FeeType决定
runFeeSettlementEntity.BillStatus = 1;
runFeeSettlementEntity.CreateUser = strUserID;
if (this.txt_settleTime.Value.Trim() != "" && this.txt_settleTime.Value.Trim().IndexOf("0001") < 0)
{
runFeeSettlementEntity.CreateTime = DateTime.Parse(this.txt_settleTime.Value.Trim());
}
runFeeSettlementEntity.PayableTime = System.DateTime.MinValue;
runFeeSettlementEntity.CustomerName = strCustomerName;//结算单位
runFeeSettlementEntity.SettleType = int.Parse(this.sel_settlemode.Value);
runFeeSettlementEntity.Remark = txt_remark.Value.Trim();
runFeeSettlementEntity.Type = 2;//付费结算
runFeeSettlementEntity.VoucherNO = "";//凭证号
runFeeSettlementEntity.FinancialVoucher = "";
runFeeSettlementEntity.CompanyID = strCompanyID;
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);
//折算汇率
runFeeSettlementEntity.Rate = totalUsd != 0 ? usdToRmb : 1;
}
else if (strSettleCurrency == "2")
{
//美元结算
FinalUsd = (StatisticSettleAmount(totalRmb, rmbToUsd) + totalUsd);
//折算汇率
runFeeSettlementEntity.Rate = totalRmb != 0 ? rmbToUsd : 1;
}
else if (strSettleCurrency == "3")
{
FinalRmd = totalRmb;
FinalUsd = totalUsd;
//if (FinalRmd != 0)
//{
// //折算汇率
// runFeeSettlementEntity.Rate = totalUsd != 0 ? usdToRmb : 1;
// usdToRmb = totalUsd != 0 ? usdToRmb : 1;
//}
//else if (FinalUsd != 0)
//{
// //折算汇率
// runFeeSettlementEntity.Rate = decimal.Parse(feeTable.Rows[0]["EXCHANGERATE"].ToString());
// rmbToUsd = decimal.Parse(feeTable.Rows[0]["EXCHANGERATE"].ToString());
//}
//else
//{
runFeeSettlementEntity.Rate = 1;
// usdToRmb = 1;
//}
}
runFeeSettlementEntity.AmountRMB = FinalRmd;
runFeeSettlementEntity.AmountUSD = FinalUsd;
runFeeSettlementEntity.AccountRMB = this.h_usdaccount.Value.Trim();
runFeeSettlementEntity.AccountUSD = this.h_rmbaccount.Value.Trim();
runFeeSettlementEntity.ChequeNo = this.txt_chequeno.Value.Trim();
runFeeSettlementEntity.ChequeNoUsd = this.txt_chequenousd.Value.Trim();
runFeeSettlementEntity.CHEQUENUMREMARK = "";//支票号备注
int iResult = runFeeDoDA.InsertRunSettle(runFeeDoEntities, runFeeSettlementEntity,strUserID,strShowName,"UNPAYSETTLE");
if (iResult == 1)
{
ViewState["enter"] = "1";//已经保存成功
ViewState["SaveState"] = "1";//已经保存成功
ViewState["RunSettleID"] = runFeeSettlementEntity.GID;//保存运行结算表GID
this.txt_customerName.Value = runFeeSettlementEntity.CustomerName;//结算单位
//Page.ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>alert('付费结算保存成功');refresh();window.parent.opener.location.href = window.parent.opener.location.href;</script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>alert('付费结算保存成功');window.parent.opener.location.href = window.parent.opener.location.href;window.close();</script>");
}
else
{
ViewState["enter"] = "1";
ViewState.Remove("SaveState");
Page.ClientScript.RegisterStartupScript(this.GetType(), "key2", "<script>alert('付费结算保存失败');</script>");
}
}
else
{
string strRunSettleID = "";
RunFeeSettlementEntity origRunFeeSettlementEntity = new RunFeeSettlementEntity();
RunFeeSettlementEntity newRunFeeSettlementEntity = new RunFeeSettlementEntity();
if (ViewState["RunSettleID"] != null)
{
strRunSettleID = ViewState["RunSettleID"].ToString();
RunFeeSettlementDA runFeeSettlementDA = new RunFeeSettlementDA();
origRunFeeSettlementEntity = runFeeSettlementDA.GetRunFeeSettlementByGID(strRunSettleID);
if (origRunFeeSettlementEntity != null)
{
if (origRunFeeSettlementEntity.GID != null)
{
LogEntity logEntity = new LogEntity();
logEntity.GID = Guid.NewGuid().ToString();
logEntity.Name = "更新付费结算保存信息";
logEntity.LogType = "UPDATE 更新操作";
logEntity.CreateUserID = strUserID;
int iSettleType = int.Parse(this.sel_settlemode.Value);
if (origRunFeeSettlementEntity.SettleType != int.Parse(this.sel_settlemode.Value))
{
logEntity.LogContent += String.Format(Logger.Instance.GetLogContentTemplate(Logger.LogTypes.UPDATE), "结算方式", GetSettleTypeName(origRunFeeSettlementEntity.SettleType), GetSettleTypeName(iSettleType));
}
string strChequeNo = this.txt_chequeno.Value.Trim();
if ((origRunFeeSettlementEntity.ChequeNo ?? "") != strChequeNo)
{
logEntity.LogContent += String.Format(Logger.Instance.GetLogContentTemplate(Logger.LogTypes.UPDATE), "支票号码RMB", origRunFeeSettlementEntity.ChequeNo, strChequeNo);
}
string strChequeNoUsd = this.txt_chequenousd.Value.Trim();
if ((origRunFeeSettlementEntity.ChequeNoUsd ?? "") != strChequeNoUsd)
{
logEntity.LogContent += String.Format(Logger.Instance.GetLogContentTemplate(Logger.LogTypes.UPDATE), "支票号码USD", origRunFeeSettlementEntity.ChequeNoUsd, strChequeNoUsd);
}
string strVoucherNO = this.txt_voucherNO.Value.Trim();
if ((origRunFeeSettlementEntity.VoucherNO ?? "") != strVoucherNO)
{
logEntity.LogContent += String.Format(Logger.Instance.GetLogContentTemplate(Logger.LogTypes.UPDATE), "凭证号码", origRunFeeSettlementEntity.VoucherNO, strVoucherNO);
}
int iResult = runFeeSettlementDA.UpdateRunSettleByGID(strRunSettleID, iSettleType, strChequeNo, strChequeNoUsd, System.DateTime.MinValue, strUserID, strVoucherNO);
if (logEntity.LogContent != null)
{
if (iResult == 1)
{
logEntity.LogContent = strShowName + " " + logEntity.LogContent;
Logger.Instance.WriteLog(logEntity);
}
}
if (iResult == 1)
{
this.txt_customerName.Value = origRunFeeSettlementEntity.CustomerName;//结算单位
Page.ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>alert('付费结算保存成功');refresh();</script>");
}
}
}
}
}
}
#endregion
#region #####
//private int RecordSettleFee()
//{
// int iResult = 0;
// string settleFee = "";
// string settleTotal = "";
// string customerName = "";
// settleTotal = h_settleinfo.Value;
// strFeeCache = h_feecache.Value;
// if (strFeeCache.IndexOf("[") >= 0 && strFeeCache.IndexOf("]") > 0)
// {
// strBillNO = CreateSettleCode();
// settleFee = strFeeCache.Substring(strFeeCache.IndexOf("[") + 1, strFeeCache.IndexOf("]") - strFeeCache.IndexOf("[") - 1);
// IList<FeeDoEntity> feeDoEntities = new List<FeeDoEntity>();
// //settleFee = settleFee.Replace(, "#");
// string[] strFees = settleFee.Split(new string[] { "},{" }, System.StringSplitOptions.RemoveEmptyEntries);
// if (strFees.Length > 0)
// {
// for (int i = 0; i < strFees.Length; i++)
// {
// strFees[i] = strFees[i].ToString().Replace("{", "");
// strFees[i] = strFees[i].ToString().Replace("}", "");
// string[] strCell = strFees[i].Split(new char[] { ',' });
// if (strCell.Length > 0)
// {
// FeeDoEntity feeDoEntity = new FeeDoEntity();
// for (int j = 0; j < strCell.Length; j++)
// {
// string[] strArg = strCell[j].Split(new char[] { ':' });
// switch (strArg[0].Replace("\"", ""))
// {
// case "id":
// feeDoEntity.FeeID = strArg[1].ToString().Replace("\"", "").Trim();
// break;
// case "type":
// feeDoEntity.FeeType = int.Parse(strArg[1].ToString().Replace("\"", "").Trim());
// break;
// case "cur":
// feeDoEntity.Currency = strArg[1].ToString().Replace("\"", "").Trim();
// break;
// case "stl":
// feeDoEntity.Amount = decimal.Parse(strArg[1].ToString().Replace("\"", "").Trim());
// break;
// case "cstl":
// feeDoEntity.DoAmount = decimal.Parse(strArg[1].ToString().Replace("\"", "").Trim());
// break;
// case "ustl":
// break;
// case "cus":
// if (customerName.Equals(""))
// {
// customerName = UnicodeToGB(strArg[1].ToString().Replace("\"", "").Trim());
// this.txt_customerName.Value = customerName;
// }
// feeDoEntity.CustomerName = UnicodeToGB(strArg[1].ToString().Replace("\"", "").Trim());
// break;
// case "fname":
// feeDoEntity.FeeName = UnicodeToGB(strArg[1].ToString().Replace("\"", "").Trim());
// break;
// case "bsno":
// feeDoEntity.BSNO = strArg[1].ToString().Replace("\"", "").Trim();
// break;
// case "orig":
// if (!strArg[1].ToString().Replace("\"", "").Trim().Equals(""))
// {
// feeDoEntity.Orig = int.Parse(strArg[1].ToString().Replace("\"", "").Trim());
// }
// break;
// case "oname":
// if (!strArg[1].ToString().Replace("\"", "").Trim().Equals(""))
// {
// feeDoEntity.OriginalFeeName = UnicodeToGB(strArg[1].ToString().Replace("\"", "").Trim());
// }
// break;
// case "ocstl":
// if (!strArg[1].ToString().Replace("\"", "").Trim().Equals(""))
// {
// feeDoEntity.OriginalFeeCSTL = decimal.Parse(strArg[1].ToString().Replace("\"", "").Trim());
// }
// break;
// default:
// break;
// }
// }
// //feeDoEntity.BSNO = "";
// feeDoEntity.MBLNO = "";
// feeDoEntity.HBLNO = "";
// //feeDoEntity.CustomerName = "";
// //feeDoEntity.FeeName = "";
// feeDoEntity.BillNO = strBillNO;
// feeDoEntity.GID = Guid.NewGuid().ToString();
// feeDoEntity.Category = 1;
// feeDoEntity.BSType = 1;
// feeDoEntity.BillStatus = 1;
// feeDoEntity.Remark = "";
// feeDoEntities.Add(feeDoEntity);
// }
// }
// }
// if (feeDoEntities.Count > 0)
// {
// IList<FeeSettlementEntity> feeSettlementEntities = new List<FeeSettlementEntity>();
// FeeSettlementEntity feeSettlementEntity = new FeeSettlementEntity();
// feeSettlementEntity.GID = Guid.NewGuid().ToString();
// if (settleTotal != "")
// {
// string tempSettle = settleTotal.Substring(settleTotal.IndexOf("[") + 1, settleTotal.IndexOf("]") - settleTotal.IndexOf("[") - 1);
// string[] tempSettleFees = tempSettle.Split(new string[] { "},{" }, System.StringSplitOptions.RemoveEmptyEntries);
// if (tempSettleFees.Length > 0)
// {
// for (int i = 0; i < tempSettleFees.Length; i++)
// {
// tempSettleFees[i] = tempSettleFees[i].ToString().Replace("{", "");
// tempSettleFees[i] = tempSettleFees[i].ToString().Replace("}", "");
// string[] strCell = tempSettleFees[i].Split(new char[] { ',' });
// if (strCell.Length > 0)
// {
// string strCurrency = "";
// for (int j = 0; j < strCell.Length; j++)
// {
// string[] strArg = strCell[j].Split(new char[] { ':' });
// switch (strArg[0].Replace("\"", ""))
// {
// case "id":
// //feeSettlementEntity.GID = strArg[1].ToString().Replace("\"", "").Trim();
// break;
// case "cur":
// strCurrency = strArg[1].ToString().Replace("\"", "").Trim();
// break;
// case "amount":
// if (strCurrency.Equals("RMB"))
// {
// feeSettlementEntity.AmountRMB = decimal.Parse(strArg[1].ToString().Replace("\"", "").Trim());
// }
// else if (strCurrency.Equals("USD"))
// {
// feeSettlementEntity.AmountUSD = decimal.Parse(strArg[1].ToString().Replace("\"", "").Trim());
// }
// break;
// case "account":
// if (strCurrency.Equals("RMB"))
// {
// feeSettlementEntity.AccountRMB = strArg[1].ToString().Replace("\"", "").Trim();
// }
// else if (strCurrency.Equals("USD"))
// {
// feeSettlementEntity.AccountUSD = strArg[1].ToString().Replace("\"", "").Trim();
// }
// break;
// case "bank":
// break;
// default:
// break;
// }
// }
// }
// if (tempSettleFees.Length > 1)
// {
// feeSettlementEntity.Rate = 1;
// }
// else
// {
// if (feeSettlementEntity.AmountRMB > 0)
// {
// feeSettlementEntity.Rate = decimal.Parse(h_usdtormb.Value);
// }
// else
// {
// feeSettlementEntity.Rate = decimal.Parse(h_rmbtousd.Value);
// }
// }
// }
// }
// }
// if (feeSettlementEntity.AccountUSD == null)
// {
// feeSettlementEntity.AccountUSD = "";
// }
// if (feeSettlementEntity.AccountRMB == null)
// {
// feeSettlementEntity.AccountRMB = "";
// }
// feeSettlementEntity.BillNO = strBillNO;
// feeSettlementEntity.BillType = 1;
// feeSettlementEntity.VoucherNO = txt_voucherNO.Value.Trim();
// feeSettlementEntity.SettleUser = strUserID;
// feeSettlementEntity.BillStatus = 1;
// feeSettlementEntity.SettleMode = 2;//自由结算
// feeSettlementEntity.FinancialVoucher = "";
// feeSettlementEntity.CustomerName = customerName;//结算单位
// feeSettlementEntity.SettleType = int.Parse(this.sel_settlemode.Value);
// feeSettlementEntity.Remark = txt_remark.Value.Trim();
// feeSettlementEntity.ChequeNo = txt_chequeno.Value.Trim();
// feeSettlementEntities.Add(feeSettlementEntity);
// FeeDoDA feeDoDA = new FeeDoDA();
// //IList<FeeEntity> feeEntities = new List<FeeEntity>();
// //FeeDA feeDA = new FeeDA();
// //foreach (FeeDoEntity tempFeeDoEntity in feeDoEntities)
// //{
// // FeeEntity feeEntity = new FeeEntity();
// // feeEntity = feeDA.GetFeeByID(tempFeeDoEntity.FeeID);
// // feeEntities.Add(feeEntity);
// //}
// iResult = feeDoDA.InsertFeeDo(feeDoEntities, feeSettlementEntities);
// if (iResult == 1)
// {
// txt_billNO.Value = feeSettlementEntities[0].BillNO;
// this.btn_enter.Disabled = true;
// this.txt_chequeno.Disabled = true;
// h_enter.Value = "0";
// }
// }
// }
// return iResult;
//}
#endregion
private void PostPaySettle(string tempPostSessionName)
{
int billtype = 0;//1收费2付费
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];
string strFeePaySettleID = Guid.NewGuid().ToString();
FeePaySettleDA feePaySettleDA = new FeePaySettleDA();
if (ViewState["RunSettleID"] == null && ViewState["PaySettleID"] == null)
{
string strCustomerName = "";
//付费申请表信息
FeeSettlementEntity feeSettlementEntity = new FeeSettlementEntity();
feeSettlementEntity.GID = strFeePaySettleID;
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);
//折算汇率
feeSettlementEntity.Rate = totalUsd != 0 ? usdToRmb : 1;
}
else if (strSettleCurrency == "2")
{
//美元结算
FinalUsd = (StatisticSettleAmount(totalRmb, rmbToUsd) + totalUsd);
//折算汇率
feeSettlementEntity.Rate = totalRmb != 0 ? rmbToUsd : 1;
}
else if (strSettleCurrency == "3")
{
FinalRmd = totalRmb;
FinalUsd = totalUsd;
//if (FinalRmd != 0)
//{
// //折算汇率
// feeSettlementEntity.Rate = totalUsd != 0 ? usdToRmb : 1;
// usdToRmb = totalUsd != 0 ? usdToRmb : 1;
//}
//else if (FinalUsd != 0)
//{
// //折算汇率
// feeSettlementEntity.Rate = decimal.Parse(feeTable.Rows[0]["EXCHANGERATE"].ToString());
// rmbToUsd = decimal.Parse(feeTable.Rows[0]["EXCHANGERATE"].ToString());
//}
//else
//{
feeSettlementEntity.Rate = 1;
// usdToRmb = 1;
//}
}
//付费申请费用明细信息
IList<FeeDoEntity> feeDoEntities = new List<FeeDoEntity>();
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());
billtype = feeDoEntity.FeeType;
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 == "RMB")
{
feeDoEntity.ExchangeRate = usdToRmb;
}
else if (strSettleCurrency == "USD")
{
feeDoEntity.ExchangeRate = rmbToUsd;
}
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 = 8;
feeDoEntity.BSType = 1;
feeDoEntity.BillStatus = 2;
feeDoEntity.Remark = "";
if(feeDoEntity.Currency == "USD")
{
feeDoEntity.ExchangeRate = usdToRmb;
}
else if (feeDoEntity.Currency == "RMB")
{
feeDoEntity.ExchangeRate = rmbToUsd;
}
else
{
feeDoEntity.ExchangeRate = 1;
}
feeDoEntity.CompanyID = strCompanyID;
feeDoEntities.Add(feeDoEntity);
}
}
feeSettlementEntity.AmountRMB = FinalRmd;
feeSettlementEntity.AmountUSD = FinalUsd;
feeSettlementEntity.AccountRMB = this.h_rmbaccount.Value.Trim();
feeSettlementEntity.AccountUSD = this.h_usdaccount.Value.Trim();
//预收预付自由结算时,赋值
feeSettlementEntity.ACCOUNTS_CURRENCY = (feeSettlementEntity.AmountUSD == 0) ? "RMB" : "USD";//记账资料_币别
feeSettlementEntity.ACCOUNTS_RATE = feeSettlementEntity.Rate;//记账资料_汇率
feeSettlementEntity.ACCOUNTS_MONEY = (feeSettlementEntity.AmountUSD == 0) ? feeSettlementEntity.AmountRMB : feeSettlementEntity.AmountUSD;//记账资料_金额
feeSettlementEntity.PREPAY_CURRENCY = (feeSettlementEntity.AmountUSD == 0) ? "RMB" : "USD";//预付支资料_币别
feeSettlementEntity.PREPAY_RATE = feeSettlementEntity.Rate;//预付支资料_汇率
feeSettlementEntity.PREPAY_MONEY = 0;//预付支资料_金额
feeSettlementEntity.AHSR_CURRENCY = (feeSettlementEntity.AmountUSD == 0) ? "RMB" : "USD";//实付支资料_币别
feeSettlementEntity.AHSR_RATE = feeSettlementEntity.Rate;//实付支资料_汇率
feeSettlementEntity.AHSR_MONEY = (feeSettlementEntity.AmountUSD == 0) ? feeSettlementEntity.AmountRMB : feeSettlementEntity.AmountUSD;//实付支资料_金额
feeSettlementEntity.FINANCIAL_CURRENCY = (feeSettlementEntity.AmountUSD == 0) ? "RMB" : "USD";//财务费用_币别
feeSettlementEntity.FINANCIAL_RATE = feeSettlementEntity.Rate;//财务费用_汇率
feeSettlementEntity.FINANCIAL_MONEY = 0;//财务费用_金额
feeSettlementEntity.ADVANCE_CURRENCY = (feeSettlementEntity.AmountUSD == 0) ? "RMB" : "USD";//预付支取用资料_币别
feeSettlementEntity.ADVANCE_RATE = feeSettlementEntity.Rate;//预付支取用资料_汇率
feeSettlementEntity.ADVANCE_MONEY = 0;//预付支取用资料_金额
//
feeSettlementEntity.BillType = billtype; //由feeDoEntity.FeeType决定
feeSettlementEntity.BillStatus = 1;
feeSettlementEntity.SettleMode = 2;//自由结算
feeSettlementEntity.ChequeNo = txt_chequeno.Value.Trim();
feeSettlementEntity.ChequeNoUsd = txt_chequenousd.Value.Trim();
feeSettlementEntity.SettleUser = strUserID;
feeSettlementEntity.CustomerName = strCustomerName;//结算单位
feeSettlementEntity.SettleType = int.Parse(this.sel_settlemode.Value);
feeSettlementEntity.Remark = txt_remark.Value.Trim();
feeSettlementEntity.CompanyID = strCompanyID;
feeSettlementEntity.FinancialVoucher = "";
feeSettlementEntity.VoucherNO = "";
if (this.txt_settleTime.Value.Trim() != "" && this.txt_settleTime.Value.Trim().IndexOf("0001") < 0)
{
feeSettlementEntity.SettleTime = DateTime.Parse(this.txt_settleTime.Value.Trim());
}
int iResult = feePaySettleDA.InsertFeePaySettle(feeDoEntities, feeSettlementEntity);
if (iResult == 1)
{
ViewState["enter"] = "1";
ViewState["PaySettleID"] = feeSettlementEntity.GID;
this.btn_enter.Disabled = true;
this.btn_post.Disabled = true;
this.txt_customerName.Value = feeSettlementEntity.CustomerName;
//判断是否自动生成凭证
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
string strISSETTLEMENTS = T_ALL_DA.GetStrSQL("ISSETTLEMENTS", "SELECT TOP 1 ISSETTLEMENTS FROM cw_design");
if (strISSETTLEMENTS.Trim() == "True")
{
string strISSETTLEMENTSOPEN = T_ALL_DA.GetStrSQL("ISSETTLEMENTSOPEN", "SELECT TOP 1 ISSETTLEMENTSOPEN FROM cw_design");
if (strISSETTLEMENTSOPEN.Trim() == "True")
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>alert('付费结算提交成功');refresh();cwvouchersadd('" + strFeePaySettleID + "');</script>");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>alert('付费结算提交成功');refresh();</script>");
}
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>alert('付费结算提交成功');refresh();</script>");
}
}
else
{
ViewState["enter"] = "2";
Page.ClientScript.RegisterStartupScript(this.GetType(), "key2", "<script>alert('付费结算提交失败');refresh()</script>");
}
}
else if (ViewState["RunSettleID"] != null && ViewState["PaySettleID"] == null)
{
//是已保存未结算的付费,则将结算运行表中的结算信息进行结算
string strRunSettleID = "";
string strBillNO = "";
if (ViewState["RunSettleID"] != null)
{
strRunSettleID = ViewState["RunSettleID"].ToString();
string strRemark = this.txt_remark.Value.Trim();
RunFeeSettlementDA runFeeSettlementDA = new RunFeeSettlementDA();
RunFeeSettlementEntity runFeeSettlementEntity = runFeeSettlementDA.GetRunFeeSettlementByGID(strRunSettleID);
if (runFeeSettlementEntity != null)
{
int iResult = feePaySettleDA.PostSavePaySettle(strFeePaySettleID, strRunSettleID, strUserID, strRemark, strCompanyID);
if (iResult == 1)
{
strBillNO = feePaySettleDA.GetBillNoByPaySettleID(strFeePaySettleID);
ViewState["enter"] = "1";
this.btn_enter.Disabled = true;
this.btn_post.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), "付费结算", "由编号:" + runFeeSettlementEntity.SerialNO + " 生成 付费结算 编号:" + strBillNO);
Logger.Instance.WriteLog(logEntity);
ViewState["PaySettleID"] = strFeePaySettleID;
this.txt_customerName.Value = runFeeSettlementEntity.CustomerName;
//判断是否自动生成凭证
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
string strISSETTLEMENTS = T_ALL_DA.GetStrSQL("ISSETTLEMENTS", "SELECT TOP 1 ISSETTLEMENTS FROM cw_design");
if (strISSETTLEMENTS.Trim() == "True")
{
string strISSETTLEMENTSOPEN = T_ALL_DA.GetStrSQL("ISSETTLEMENTSOPEN", "SELECT TOP 1 ISSETTLEMENTSOPEN FROM cw_design");
if (strISSETTLEMENTSOPEN.Trim() == "True")
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>alert('结算提交成功');refresh();cwvouchersadd('" + strFeePaySettleID + "');</script>");//window.parent.opener.location.href = window.parent.opener.location.href;
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>alert('结算提交成功');refresh();</script>");//window.parent.opener.location.href = window.parent.opener.location.href;
}
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>alert('结算提交成功');refresh();</script>");//window.parent.opener.location.href = window.parent.opener.location.href;
}
}
else
{
ViewState["enter"] = "2";
Page.ClientScript.RegisterStartupScript(this.GetType(), "key2", "<script>alert('结算提交失败');refresh()</script>");
}
}
}
}
}
private string CreateSettleCode()
{
int CodeLength = 4;
string strSettleCode = "";
//获取分公司的票号头字符
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
string tempBANKSHEAD = T_ALL_DA.GetStrSQL("BANKSHEAD", "SELECT TOP 1 BANKSHEAD FROM company WHERE GID='" + strCompanyID.Trim() + "'");
string strFeeType = tempBANKSHEAD+"CR";
FeeDoDA feeDoDA = new FeeDoDA();
DateTime nowDateTime = feeDoDA.getServerDateTime();
string strYear = nowDateTime.Year.ToString();
string strMonth = nowDateTime.Month.ToString().Length < 2 ? "0" + nowDateTime.Month.ToString() : nowDateTime.Month.ToString();
//string strDay = nowDateTime.Day.ToString();
string strDateTime = strYear + strMonth;
string strCodePrefix = strFeeType + strDateTime;
//string strSql = "SELECT TOP 1 BILLNO FROM ch_fee_do WHERE BILLNO LIKE '" + strFeeType + strDateTime + "%' ORDER BY BILLNO DESC";
string strSql = " SELECT TOP 1 BILLNO FROM "
+ " (SELECT BILLNO FROM ch_fee_do WHERE BILLNO LIKE '" + strFeeType + strDateTime + "%' UNION "
+ " SELECT BILLNO FROM ch_fee_payapplication WHERE BILLNO LIKE '" + strFeeType + strDateTime + "%') as A ORDER BY BILLNO DESC";
DataTable billTable = feeDoDA.GetExcuteSql(strSql).Tables[0];
ArrayList codeArg = new ArrayList();
for (int i = 0; i < CodeLength; i++)
{
codeArg.Add(0);
}
string strCode = "";//编号
if (billTable.Rows.Count > 0)
{
string oldCode = billTable.Rows[0][0].ToString();
if (oldCode.IndexOf(strCodePrefix) >= 0)
{
oldCode = oldCode.Substring(strCodePrefix.Length, oldCode.Length - strCodePrefix.Length);
if (oldCode.Length == CodeLength)
{
for (int i = 0; i < oldCode.Length; i++)
{
if (oldCode[i].ToString().IndexOf("0") == 0)
{
oldCode = oldCode.Remove(i, 1);
i = -1;
continue;
}
else
{
break;
}
}
}
}
int newNum = int.Parse(oldCode);
newNum = newNum + 1;
string strNewNum = newNum.ToString();
int codeArgCount = codeArg.Count - 1;
for (int i = strNewNum.Length - 1; i >= 0; i--)
{
codeArg[codeArgCount] = strNewNum[i];
codeArgCount--;
}
//for (int i = 0; i < strNewNum.Length; i++)
//{
// codeArg[i] = strNewNum[i];
//}
}
else
{
int iOld = int.Parse(codeArg[codeArg.Count - 1].ToString());
codeArg[codeArg.Count - 1] = (iOld + 1).ToString();
}
for (int i = 0; i < codeArg.Count; i++)
{
strCode += codeArg[i].ToString();
}
strSettleCode = strFeeType + strDateTime + strCode;
return strSettleCode;
}
#region Unicode转换成GB2312
/// <summary>
/// Unicode转换成GB2312
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public string UnicodeToGB(string text)
{
MatchCollection mc = Regex.Matches(text, "([\\w]+)|(\\\\u([\\w]{4}))");
if (mc != null && mc.Count > 0)
{
StringBuilder sb = new StringBuilder();
foreach (Match m2 in mc)
{
string v = m2.Value;
if (v.IndexOf("\\") >= 0)
{
string word = v.Substring(2);
byte[] codes = new byte[2];
int code = Convert.ToInt32(word.Substring(0, 2), 16);
int code2 = Convert.ToInt32(word.Substring(2), 16);
codes[0] = (byte)code2;
codes[1] = (byte)code;
sb.Append(Encoding.Unicode.GetString(codes));
}
else
{
sb.Append(v);
}
}
return sb.ToString();
}
else
{
return text;
}
}
#endregion
#region 不同币别金额换算
/// <summary>
/// 不同币别金额换算
/// </summary>
/// <param name="tempOrigAmount">原币别金额</param>
/// <param name="tempRate">汇率</param>
/// <returns>返回折算后的金额</returns>
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 金额四舍五入
/// <summary>
/// 金额四舍五入
/// </summary>
/// <param name="value">金额值</param>
/// <param name="digit">小数点后位数</param>
/// <returns>返回Double型四舍五入金额</returns>
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;
}
protected void btn_cancelsettle_Click(object sender, EventArgs e)
{
string strPaySettleID = "";
if (ViewState["PaySettleID"] != null)
{
strPaySettleID = ViewState["PaySettleID"].ToString().Trim();
FeePaySettleDA feePaySettleDA = new FeePaySettleDA();
FeeSettlementDA feeSettlementDA = new FeeSettlementDA();
FeeSettlementEntity feeSettlementEntity = new FeeSettlementEntity();
feeSettlementEntity = feeSettlementDA.GetFeeSettlementByGID(strPaySettleID);
if (feeSettlementEntity != null)
{
if (feeSettlementEntity.GID != null)
{
int iResult = feePaySettleDA.CacelPaySettle(strPaySettleID, feeSettlementEntity.BillNO, strUserID, feeSettlementEntity.CompanyID);
if (iResult == 1)
{
LogEntity logEntity = new LogEntity();
logEntity.GID = Guid.NewGuid().ToString();
logEntity.Name = "撤销付费结算信息";
logEntity.LogType = "CANCEL 撤销操作";
logEntity.CreateUserID = strUserID;
logEntity.LogContent = strShowName + String.Format(Logger.Instance.GetLogContentTemplate(Logger.LogTypes.CANCEL), "付费结算", "结算单号:" + feeSettlementEntity.BillNO);
Logger.Instance.WriteLog(logEntity);
this.btn_enter.Disabled = true;
this.btn_cancelsettle.Enabled = false;
this.btn_post.Disabled = true;
Page.ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>alert('撤销结算成功');closeAction();</script>");
}
}
}
}
}
}
}