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;
namespace DSWeb.Settlements
{
public partial class DoSettlement : PageBase
{
private string strBillNO;
private string strUserID;
private string strCompanyID;//公司GID
private string strShowName;//用户显示名
private string strDeptName;//部门名称
private string strFeeCache;
private string strCacheName;
private string strSettleCurrency;
private decimal usdToRmb;
private decimal rmbToUsd;
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 (!IsPostBack)
{
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;
}
}
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(Round(double.Parse( Request.QueryString["uex"].ToString()),2,"").ToString());
h_rmbtousd.Value = rmbToUsd.ToString();
}
if (Request.QueryString["rex"] != null)
{
usdToRmb = decimal.Parse(Round(double.Parse( Request.QueryString["rex"].ToString()),2,"").ToString());
h_usdtormb.Value = usdToRmb.ToString();
}
}
else
{
RecordSettleFee();
}
}
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 = "锁定";
}
///
/// 正数四舍五入,负数是五舍六入。(个人以为第一种方法合适处置货泉盘算)
///
/// 四舍五入的数
/// 表示要保存的小数点后为数
///
private double Round(double value, int decimals, string money)
{
if (value < 0)
{
return Math.Round(value + 5 / Math.Pow(10, decimals + 1), decimals, MidpointRounding.AwayFromZero);
}
else
{
return Math.Round(value, decimals, MidpointRounding.AwayFromZero);
}
}
private int RecordSettleFee()
{
int billtype = 0;//1收费2付费
int iResult = 0;
string settleFee = "";
string settleTotal = "";
string customerName = "";
string strFeePaySettleID = "";
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 feeDoEntities = new List();
//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());
billtype = feeDoEntity.FeeType;
break;
case "cur":
feeDoEntity.Currency = strArg[1].ToString().Replace("\"", "").Trim();
break;
case "stl":
string tmp = Round(double.Parse(strArg[1].ToString().Replace("\"", "").Trim()), 2, "").ToString();
feeDoEntity.Amount = decimal.Parse(tmp);
break;
case "cstl":
string tmp1 = Round(double.Parse(strArg[1].ToString().Replace("\"", "").Trim()), 2, "").ToString();
feeDoEntity.DoAmount = decimal.Parse(tmp1);
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(Round(Double.Parse( strArg[1].ToString().Replace("\"", "").Trim()),2,"").ToString());
}
break;
default:
break;
}
}
//feeDoEntity.BSNO = "";
T_ALL_DA T_ALL_DA = new T_ALL_DA();
feeDoEntity.MBLNO = T_ALL_DA.GetStrSQL("MBLNO", "select MBLNO from (select bsno,mblno from VW_Settlement) as a where bsno='" + feeDoEntity.BSNO.ToString().Trim() + "'");
feeDoEntity.HBLNO = T_ALL_DA.GetStrSQL("HBLNO", "select HBLNO from (select bsno,HBLNO from VW_Settlement) as a where bsno='" + feeDoEntity.BSNO.ToString().Trim() + "'");
//feeDoEntity.CustomerName = "";
//feeDoEntity.FeeName = "";
feeDoEntity.BillNO = strBillNO;
feeDoEntity.GID = Guid.NewGuid().ToString();
feeDoEntity.Category = 2;
feeDoEntity.BSType = 1;
feeDoEntity.BillStatus = 1;
feeDoEntity.Remark = "";
feeDoEntities.Add(feeDoEntity);
}
}
}
if (feeDoEntities.Count > 0)
{
IList feeSettlementEntities = new List();
FeeSettlementEntity feeSettlementEntity = new FeeSettlementEntity();
feeSettlementEntity.GID = Guid.NewGuid().ToString();
strFeePaySettleID += "," + feeSettlementEntity.GID.ToString().Trim();
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(Round(double.Parse(strArg[1].ToString().Replace("\"", "").Trim()),2,"").ToString());
}
else if (strCurrency.Equals("USD"))
{
feeSettlementEntity.AmountUSD = decimal.Parse(Round(double.Parse( strArg[1].ToString().Replace("\"", "").Trim()),2,"").ToString());
}
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.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.BillNO = strBillNO;
feeSettlementEntity.BillType = billtype;//由feeDoEntity.FeeType决定。
feeSettlementEntity.VoucherNO = txt_voucherNO.Value.Trim();
feeSettlementEntity.SettleUser = strUserID;//txt_settleUser.Value.Trim();
if (this.txt_settleTime.Value.Trim() != "" && this.txt_settleTime.Value.Trim().IndexOf("0001") < 0)
{
feeSettlementEntity.SettleTime = DateTime.Parse(this.txt_settleTime.Value.Trim());
}
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 = "";
feeSettlementEntity.ChequeNoUsd = "";
feeSettlementEntity.CompanyID = strCompanyID;
feeSettlementEntities.Add(feeSettlementEntity);
FeeDoDA feeDoDA = new FeeDoDA();
iResult = feeDoDA.InsertFeeDo(feeDoEntities, feeSettlementEntities);
if (iResult == 1)
{
txt_billNO.Value = feeSettlementEntities[0].BillNO;
h_billno.Value = feeSettlementEntities[0].BillNO;
this.btn_enter.Disabled = true;
h_enter.Value = "";
}
}
}
if (strFeePaySettleID.Trim().Length > 0)
{
strFeePaySettleID = strFeePaySettleID.Trim().Substring(1);
//判断是否自动生成凭证
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", "");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "key1", "");
}
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "key1", "");
}
}
return iResult;
}
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+"DR";
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";
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;
}
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;
}
}
}
}