DS7/DSWeb/Settlements/DoRecvApplicationSettlement...

996 lines
55 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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.Text;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Data.SqlClient;
using WebSqlHelper;
namespace DSWeb.Settlements
{
public partial class DoRecvApplicationSettlement : System.Web.UI.Page
{
private string strBillNO;//申请业务编号
private string strUserID;//登录人UserID
private string strShowName;//登录人名称
private string strCompanyID;//公司GID
private string strDeptName;//部门名称
private int iSettleType;//结算类型 1-单收费结算 2-合并收费结算
private string strMergeCacheName;//合并收费申请缓存名称
private string recvJSON_2;
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["billno"] != null)
{
strBillNO = Request.QueryString["billno"].ToString().Trim();
}
if (Request.QueryString["type"] != null)
{
iSettleType = int.Parse(Request.QueryString["type"].ToString());
}
else
{
iSettleType = 0;
}
if (Request.QueryString["mergecachename"] != null)
{
strMergeCacheName = Request.QueryString["mergecachename"].ToString().Trim();
}
if (!IsPostBack)
{
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
if (this.txt_settleTime.Value.Trim() == "")
{
string rq = T_ALL_DA.GetStrSQL("rq", "select getdate() as rq");//convert(char(10),getdate(),20)
this.txt_settleTime.Value = rq;
}
//判断是否在结算页面显示预收预收信息录入
this.h_ISADVANCE.Value = T_ALL_DA.GetStrSQL("ISADVANCE", "select ISADVANCE from subcomp_seae_def where CORPID='" + strCompanyID + "'");
//是否取用预收预收默认是0 不取用
this.h_isAccess.Value = "0";
//费用的结算单据用途1-收款2-付款
this.h_feetype.Value = "1";
//
this.h_bb5.Value = "RMB";
}
#region ####
if (!h_enter.Value.Trim().Equals("1"))
{
if (strBillNO != null)
{
if (!strBillNO.Equals(""))
{
if (this.h_settleinfo.Value.ToString().Trim().Equals(""))
{
ChFeeRecvApplicationEntity ChFeeRecvApplicationEntity = new ChFeeRecvApplicationEntity();
ChFeeRecvApplicationDA feePaySettleApplication = new ChFeeRecvApplicationDA();
ChFeeRecvApplicationEntity = feePaySettleApplication.GetModel(strBillNO, strCompanyID);
if (ChFeeRecvApplicationEntity.GID != null)
{
this.h_rmbamount.Value = ChFeeRecvApplicationEntity.AMOUNTRMB.ToString().Trim();
this.h_usdamount.Value = ChFeeRecvApplicationEntity.AMOUNTUSD.ToString().Trim();
Decimal rmbamount = ChFeeRecvApplicationEntity.AMOUNTRMB - ChFeeRecvApplicationEntity.SETTLERMB;
Decimal usdamount = ChFeeRecvApplicationEntity.AMOUNTUSD - ChFeeRecvApplicationEntity.SETTLEUSD;
StringBuilder amountBuilder = new StringBuilder();
amountBuilder.Append("{\"settles\":[");
bool isExist = false;
if (ChFeeRecvApplicationEntity.CURRENCY == "RMB") //if (ChFeeRecvApplicationEntity.AMOUNTRMB != 0)
{
//header 0 1 2 3 4 5 6 7 8
//var headerArgs_do = new Array("应结币别", "申请金额", "应结金额", "本次结算", "未结", "实结币别", "实结费率", "实结金额", "出账账户");
amountBuilder.Append("{\"id\":\"" + Guid.NewGuid().ToString().Trim() + "\",");
amountBuilder.Append("\"cur\":\"RMB\",");//应结币别
amountBuilder.Append("\"applyamount\":\"" + ChFeeRecvApplicationEntity.AMOUNTRMB.ToString().Trim() + "\",");//申请金额
amountBuilder.Append("\"amount\":\"" + rmbamount.ToString().Trim() + "\",");//应结金额
amountBuilder.Append("\"ctl\":\"" + rmbamount.ToString().Trim() + "\",");//本次结算
amountBuilder.Append("\"uctl\":\"0\",");//未结
amountBuilder.Append("\"cur2\":\"RMB\",");//实结币别
amountBuilder.Append("\"rate2\":\"" + ChFeeRecvApplicationEntity.RATE.ToString().Trim() + "\",");//实结费率
amountBuilder.Append("\"amount2\":\"" + rmbamount.ToString().Trim() + "\",");//实结金额
amountBuilder.Append("\"account\":\"\",");//出账账户
amountBuilder.Append("\"bank\":\"\"}");//出账账户
isExist = true;
this.h_cur.Value = "RMB";
this.h_ctl.Value = rmbamount.ToString();
}
if (ChFeeRecvApplicationEntity.CURRENCY == "USD") //if (ChFeeRecvApplicationEntity.AMOUNTUSD != 0)
{
if (isExist)
{
amountBuilder.Append(",{\"id\":\"" + Guid.NewGuid().ToString().Trim() + "\",");
amountBuilder.Append("\"cur\":\"USD\",");
amountBuilder.Append("\"applyamount\":\"" + ChFeeRecvApplicationEntity.AMOUNTUSD.ToString().Trim() + "\",");//应结费率
amountBuilder.Append("\"amount\":\"" + usdamount.ToString().Trim() + "\",");
amountBuilder.Append("\"ctl\":\"" + usdamount.ToString().Trim() + "\",");
amountBuilder.Append("\"uctl\":\"0\",");
amountBuilder.Append("\"cur2\":\"USD\",");//实结币别
amountBuilder.Append("\"rate2\":\"" + ChFeeRecvApplicationEntity.RATE.ToString().Trim() + "\",");//实结费率
amountBuilder.Append("\"amount2\":\"" + usdamount.ToString().Trim() + "\",");//实结金额
amountBuilder.Append("\"account\":\"\",");
amountBuilder.Append("\"bank\":\"\"}");
}
else
{
amountBuilder.Append("{\"id\":\"" + Guid.NewGuid().ToString().Trim() + "\",");
amountBuilder.Append("\"cur\":\"USD\",");
amountBuilder.Append("\"applyamount\":\"" + ChFeeRecvApplicationEntity.AMOUNTUSD.ToString().Trim() + "\",");//申请金额
amountBuilder.Append("\"amount\":\"" + usdamount.ToString().Trim() + "\",");
amountBuilder.Append("\"ctl\":\"" + usdamount.ToString().Trim() + "\",");
amountBuilder.Append("\"uctl\":\"0\",");
amountBuilder.Append("\"cur2\":\"USD\",");//实结币别
amountBuilder.Append("\"rate2\":\"" + ChFeeRecvApplicationEntity.RATE.ToString().Trim() + "\",");//实结费率
amountBuilder.Append("\"amount2\":\"" + usdamount.ToString().Trim() + "\",");//实结金额
amountBuilder.Append("\"account\":\"\",");
amountBuilder.Append("\"bank\":\"\"}");
}
this.h_cur.Value = "USD";
this.h_ctl.Value = usdamount.ToString().Trim();
}
amountBuilder.Append("]}");
h_settleinfo.Value = amountBuilder.ToString().Trim();
this.txt_customerName.Value = ChFeeRecvApplicationEntity.CUSTOMERNAME.ToString().Trim();
this.txt_customerName.Disabled = true;//结算单位不可编辑
this.txt_remark.Value = ChFeeRecvApplicationEntity.REMARK.ToString().Trim();
}
}
}
}
}
else
{
RecordSettleFee();//
}
#endregion
if (strMergeCacheName != null && iSettleType == 2)
{
MergeRecvApplicationInfo();
}
//if (h_enter.Value.Trim() == "1")
//{
// //提交收费申请结算
// PostRecvApplicationSettle();
//}
}
#region 获取合并收费申请的汇总费用信息
/// <summary>
/// 获取合并收费申请的汇总费用信息
/// </summary>
private void MergeRecvApplicationInfo()
{
StringBuilder mergeBuilder = new StringBuilder();
ChFeeRecvApplicationDA ChFeeRecvApplicationDA = new ChFeeRecvApplicationDA();
DataTable mergeTable = new DataTable();
if (Session[strMergeCacheName] != null)
{
mergeTable = (DataTable)Session[strMergeCacheName];
}
mergeBuilder.Append("{");
mergeBuilder.Append("\"totals\":");
mergeBuilder.Append("[");
mergeBuilder.Append("{");
decimal rmbTotal = 0;
decimal usdTotal = 0;
for (int i = 0; i < mergeTable.Rows.Count; i++)
{
ChFeeRecvApplicationEntity ChFeeRecvApplicationEntity = ChFeeRecvApplicationDA.GetModel(mergeTable.Rows[i][0].ToString());
if (ChFeeRecvApplicationEntity != null)
{
if (ChFeeRecvApplicationEntity.GID != null)
{
if (ChFeeRecvApplicationEntity.AMOUNTUSD != 0)
{
usdTotal += ChFeeRecvApplicationEntity.AMOUNTUSD;
}
if (ChFeeRecvApplicationEntity.AMOUNTRMB != 0)
{
rmbTotal += ChFeeRecvApplicationEntity.AMOUNTRMB;
}
if (i == 0)
{
this.txt_customerName.Value = ChFeeRecvApplicationEntity.CUSTOMERNAME.ToString().Trim();
}
}
}
}
mergeBuilder.Append("\"totalrmb\":" + rmbTotal.ToString() + ",");
mergeBuilder.Append("\"totalusd\":" + usdTotal.ToString() + "");
mergeBuilder.Append("}");
mergeBuilder.Append("]");
mergeBuilder.Append("}");
h_total.Value = mergeBuilder.ToString();
}
#endregion
#region 提交收费申请结算
/// <summary>
/// 提交收费申请结算
/// </summary>
private void PostRecvApplicationSettle()
{
ChFeeRecvApplicationDA ChFeeRecvApplicationDA = new ChFeeRecvApplicationDA();
DataTable mergeTable = new DataTable();
if (Session[strMergeCacheName] != null)
{
mergeTable = (DataTable)Session[strMergeCacheName];
}
IList<ChFeeRecvApplicationEntity> feeRecvAppEntities = new List<ChFeeRecvApplicationEntity>();
FeeSettlementEntity feeSettlementEntity = new FeeSettlementEntity();
decimal rmbTotal = 0;
decimal usdTotal = 0;
for (int i = 0; i < mergeTable.Rows.Count; i++)
{
ChFeeRecvApplicationEntity ChFeeRecvApplicationEntity = ChFeeRecvApplicationDA.GetModel(mergeTable.Rows[i][0].ToString());
if (ChFeeRecvApplicationEntity != null)
{
if (ChFeeRecvApplicationEntity.GID != null)
{
feeRecvAppEntities.Add(ChFeeRecvApplicationEntity);
}
if (ChFeeRecvApplicationEntity.AMOUNTUSD != 0)
{
usdTotal += ChFeeRecvApplicationEntity.AMOUNTUSD;
}
if (ChFeeRecvApplicationEntity.AMOUNTRMB != 0)
{
rmbTotal += ChFeeRecvApplicationEntity.AMOUNTRMB;
}
}
}
feeSettlementEntity.GID = Guid.NewGuid().ToString();
feeSettlementEntity.AmountRMB = rmbTotal;
feeSettlementEntity.AmountUSD = usdTotal;
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 = 1;
feeSettlementEntity.BillStatus = 1;
feeSettlementEntity.SettleMode = 1;//收费结算
feeSettlementEntity.ChequeNo = txt_chequeno.Value.Trim();
feeSettlementEntity.ChequeNoUsd = txt_chequenousd.Value.Trim();
feeSettlementEntity.SettleUser = strUserID;
feeSettlementEntity.CustomerName = this.txt_customerName.Value;//结算单位
feeSettlementEntity.SettleType = int.Parse(this.sel_settlemode.Value);
feeSettlementEntity.Remark = txt_remark.Value.Trim();
feeSettlementEntity.CompanyID = strCompanyID;
feeSettlementEntity.FinancialVoucher = "";
feeSettlementEntity.VoucherNO = this.txt_voucherNO.Value.Trim();
FeeRecvSettleDA FeeRecvSettleDA = new FeeRecvSettleDA();
int iResult = FeeRecvSettleDA.MergeRecvApplicationSettle(feeRecvAppEntities, feeSettlementEntity, strCompanyID, strUserID);
if (iResult == 1)
{
this.btn_enter.Disabled = true;
h_enter.Value = "2";
Page.ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>alert('收费结算提交成功');refresh();window.parent.opener.location.href = window.parent.opener.location.href;</script>");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "key2", "<script>alert('收费结算提交失败');refresh()</script>");
}
}
#endregion
#region 执行结算收费申请结算
/// <summary>
/// 执行结算收费申请结算
/// </summary>
private void RecordSettleFee()
{
#region 将页面post获取的信息解析
string settleTotal = "";
int iResult = 0;
settleTotal = h_settleinfo.Value;
string strNewBillNO = CreateSettleCode();
FeeSettlementEntity feeSettlementEntity = new FeeSettlementEntity();
string strFeeRecvSettleID = Guid.NewGuid().ToString();
feeSettlementEntity.GID = strFeeRecvSettleID;
string cur="";//币别
decimal applyamount = 0;//申请金额
decimal amount = 0; //应结金额
decimal ctl = 0; //实结金额
decimal uctl = 0; //未结金额
string cur2 = "";//币别
decimal rate2 = 0;//币别
decimal amount2 = 0; //应结金额
string account = "";//入账账户GID
string bank = "";//入账账户显示信息
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();
cur = strCurrency;
break;
case "applyamount":
applyamount = decimal.Parse(strArg[1].ToString().Replace("\"", "").Trim());
break;
case "amount":
amount = decimal.Parse(strArg[1].ToString().Replace("\"", "").Trim());
break;
case "ctl"://实结金额
ctl = decimal.Parse(strArg[1].ToString().Replace("\"", "").Trim());
break;
case "uctl"://未结金额
uctl = decimal.Parse(strArg[1].ToString().Replace("\"", "").Trim());
break;
case "cur2"://币别
cur2 = strArg[1].ToString().Replace("\"", "").Trim();
break;
case "rate2":
feeSettlementEntity.Rate = decimal.Parse(strArg[1].ToString().Replace("\"", "").Trim());
rate2 = decimal.Parse(strArg[1].ToString().Replace("\"", "").Trim());
break;
case "amount2":
if (cur2.Equals("RMB"))
{
feeSettlementEntity.AmountRMB = decimal.Parse(strArg[1].ToString().Replace("\"", "").Trim());
}
else if (cur2.Equals("USD"))
{
feeSettlementEntity.AmountUSD = decimal.Parse(strArg[1].ToString().Replace("\"", "").Trim());
}
amount2 = 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();
}
account = strArg[1].ToString().Replace("\"", "").Trim();
break;
case "bank":
bank = strArg[1].ToString().Replace("\"", "").Trim();
break;
default:
break;
}
}
}
}
}
}
//
if (feeSettlementEntity.AccountUSD == null)
{
feeSettlementEntity.AccountUSD = "";
}
if (feeSettlementEntity.AccountRMB == null)
{
feeSettlementEntity.AccountRMB = "";
}
//预收
feeSettlementEntity.ACCOUNTS_CURRENCY = this.ddl_bb1.Value.Trim();
feeSettlementEntity.ACCOUNTS_RATE = this.txt_hl1.Value.Trim() == "" ? 0 : Decimal.Parse(this.txt_hl1.Value.Trim());
feeSettlementEntity.ACCOUNTS_MONEY = this.txt_je1.Value.Trim() == "" ? 0 : Decimal.Parse(this.txt_je1.Value.Trim());
feeSettlementEntity.PREPAY_CURRENCY = this.ddl_bb2.Value.Trim();
feeSettlementEntity.PREPAY_RATE = this.txt_hl2.Value.Trim() == "" ? 0 : Decimal.Parse(this.txt_hl2.Value.Trim());
feeSettlementEntity.PREPAY_MONEY = this.txt_je2.Value.Trim() == "" ? 0 : Decimal.Parse(this.txt_je2.Value.Trim());
feeSettlementEntity.AHSR_CURRENCY = this.ddl_bb3.Value.Trim();
feeSettlementEntity.AHSR_RATE = this.txt_hl3.Value.Trim() == "" ? 0 : Decimal.Parse(this.txt_hl3.Value.Trim());
feeSettlementEntity.AHSR_MONEY = this.txt_je3.Value.Trim() == "" ? 0 : Decimal.Parse(this.txt_je3.Value.Trim());
feeSettlementEntity.FINANCIAL_CURRENCY = this.ddl_bb4.Value.Trim();
feeSettlementEntity.FINANCIAL_RATE = this.txt_hl4.Value.Trim() == "" ? 0 : Decimal.Parse(this.txt_hl4.Value.Trim());
feeSettlementEntity.FINANCIAL_MONEY = this.txt_je4.Value.Trim() == "" ? 0 : Decimal.Parse(this.txt_je4.Value.Trim());
//feeSettlementEntity.ADVANCE_CURRENCY = this.ddl_bb5.Value.Trim();
feeSettlementEntity.ADVANCE_CURRENCY = this.h_bb5.Value.Trim();
feeSettlementEntity.ADVANCE_RATE = this.txt_hl5.Value.Trim() == "" ? 0 : Decimal.Parse(this.txt_hl5.Value.Trim());
feeSettlementEntity.ADVANCE_MONEY = this.txt_je5.Value.Trim() == "" ? 0 : Decimal.Parse(this.txt_je5.Value.Trim());
#endregion
#region 构建插入更新的sql语句
FeeDoDA feeDoDA = new FeeDoDA();
FeeDA feeDA = new FeeDA();
IList<FeeEntity> feeEntities = new List<FeeEntity>();
IList<FeeDoEntity> feeDoEntities2 = new List<FeeDoEntity>();
IList<FeeDoEntity> feeDoEntities = new List<FeeDoEntity>();
feeDoEntities = feeDoDA.GetFeeDoByBillNO_DelCRDR(strBillNO);
if (feeDoEntities.Count > 0)
{
ChFeeRecvApplicationEntity ChFeeRecvApplicationEntity = new ChFeeRecvApplicationEntity();
ChFeeRecvApplicationDA ChFeeRecvApplicationDA = new ChFeeRecvApplicationDA();
ChFeeRecvApplicationEntity = ChFeeRecvApplicationDA.GetModel(strBillNO, strCompanyID);
int oldBillStatus = ChFeeRecvApplicationEntity.BILLSTATUS;
if (ChFeeRecvApplicationEntity.GID != null)
{
//ch_fee_payapplication
ChFeeRecvApplicationEntity.SETTLERATE = ChFeeRecvApplicationEntity.RATE;
ChFeeRecvApplicationEntity.SETTLEUSER = strUserID;
if (this.txt_settleTime.Value.Trim() != "" && this.txt_settleTime.Value.Trim().IndexOf("0001") < 0)
{
ChFeeRecvApplicationEntity.SETTLETIME = DateTime.Parse(this.txt_settleTime.Value.Trim());
}
#region begin 2014-01-11 添加 部分结算
ArrayList alFeeDo = new ArrayList();
if ((applyamount-ctl) != 0)
{
//ch_fee_do
decimal sums = 0; //金额累加
foreach (FeeDoEntity feeDoEntity in feeDoEntities)
{
if (feeDoEntity.Amount == feeDoEntity.DoAmount)
{
sums += feeDoEntity.DoAmount;
if ((ctl - sums) == 0)
{
feeDoEntities2.Add(feeDoEntity);
break;
}
else
{
decimal doamount = feeDoEntity.Amount - (sums - ctl);
string ssql = "update [ch_fee_do] set [DOAMOUNT]=" + doamount + " where GID='" + feeDoEntity.GID.ToString().Trim() + "'";
alFeeDo.Add(ssql);
feeDoEntity.DoAmount = feeDoEntity.Amount - (sums - ctl);
feeDoEntities2.Add(feeDoEntity);
break;
}
}
else
{
sums += (feeDoEntity.Amount - feeDoEntity.DoAmount);
if ((ctl - sums) == 0)
{
decimal doamount = feeDoEntity.Amount;
string ssql = "update [ch_fee_do] set [DOAMOUNT]=" + doamount + " where GID='" + feeDoEntity.GID.ToString().Trim() + "'";
alFeeDo.Add(ssql);
feeDoEntity.DoAmount = ctl;
feeDoEntities2.Add(feeDoEntity);
break;
}
//else if ((ctl - sums) > 0)
//{
// decimal doamount = feeDoEntity.Amount;
// string ssql = "update [ch_fee_do] set [DOAMOUNT]=" + doamount + " where GID='" + feeDoEntity.GID.ToString().Trim() + "'";
// alFeeDo.Add(ssql);
// feeDoEntity.DoAmount = (feeDoEntity.Amount - feeDoEntity.DoAmount);
// feeDoEntities2.Add(feeDoEntity);
//}
else
{
decimal doamount = feeDoEntity.DoAmount + ctl;
//if ((feeDoEntity.DoAmount + ctl) >= feeDoEntity.Amount)
if ((feeDoEntity.DoAmount + ctl) == feeDoEntity.Amount)
{
doamount = feeDoEntity.Amount;
}
string ssql = "update [ch_fee_do] set [DOAMOUNT]=" + doamount + " where GID='" + feeDoEntity.GID.ToString().Trim() + "'";
alFeeDo.Add(ssql);
feeDoEntity.DoAmount = ctl;
feeDoEntities2.Add(feeDoEntity);
break;
}
}
}
//ch_fee_payapplication
if (cur.Trim() == "RMB")
{
ChFeeRecvApplicationEntity.SETTLERMB += ctl;
//
if (ChFeeRecvApplicationEntity.SETTLERMB == ChFeeRecvApplicationEntity.AMOUNTRMB)
{
ChFeeRecvApplicationEntity.BILLSTATUS = 4;
}
else
{
ChFeeRecvApplicationEntity.BILLSTATUS = 5;
}
}
else
{
ChFeeRecvApplicationEntity.SETTLEUSD += ctl;
//
if (ChFeeRecvApplicationEntity.SETTLEUSD == ChFeeRecvApplicationEntity.AMOUNTUSD)
{
ChFeeRecvApplicationEntity.BILLSTATUS = 4;
}
else
{
ChFeeRecvApplicationEntity.BILLSTATUS = 5;
}
}
}
else
{
feeDoEntities2 = feeDoEntities;
//ch_fee_payapplication
ChFeeRecvApplicationEntity.SETTLERMB = ChFeeRecvApplicationEntity.AMOUNTRMB;
ChFeeRecvApplicationEntity.SETTLEUSD = ChFeeRecvApplicationEntity.AMOUNTUSD;
ChFeeRecvApplicationEntity.BILLSTATUS = 4;
}
#endregion
#region 汇率损益
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
//是否在结算时生成“汇率损益”信息
string sISCGOL = T_ALL_DA.GetStrSQL("ISCGOL", "select ISCGOL from subcomp_seae_def where CORPID='" + strCompanyID + "'");
if (sISCGOL.Trim() == "True")
{
foreach (FeeDoEntity feeDoEntity in feeDoEntities2)
{
FeeEntity feeEntity = new Models.FeeEntity();
feeEntity = feeDA.GetFeeByID(feeDoEntity.FeeID.ToString().Trim());
//
decimal amount3 = 0;
string cur3 = feeEntity.Currency.ToString().Trim();
decimal ExchangeRate = feeEntity.ExchangeRate;
decimal rate3 = feeDoEntity.ExchangeRate;
if (cur3.Trim() != cur2.Trim())
{
if (cur3.Trim() == "RMB")
{
//amount3 = decimal.Parse((amount2 * rate2 - ctl).ToString("F2"));
//amount3 = decimal.Parse(((feeDoEntity.DoAmount / rate2 - ctl / rate3) * rate2).ToString("F2"));
amount3 = 0;
}
else if (cur3.Trim() == "USD")
{
//amount3 = decimal.Parse((amount2 - ctl * rate3).ToString("F2"));
amount3 = decimal.Parse((feeDoEntity.DoAmount * rate2 - feeDoEntity.DoAmount * rate3).ToString("F2"));
}
//
if (amount3 != 0)
{
feeEntity.GID = Guid.NewGuid().ToString();
feeEntity.FeeName = "汇率损益";
//feeEntity.CustomerName = "汇率损益";
feeEntity.UnitPrice = amount3;
feeEntity.Quantity = 1;
feeEntity.Amount = amount3;
feeEntity.Settlement = amount3;
feeEntity.Currency = "RMB";
feeEntity.ExchangeRate = 1;
feeEntity.Remark = "汇率损益";
feeEntity.FeeType = 2;
feeEntity.EnterOperator = strUserID;
feeEntity.FeeStatus = 9;
feeEntity.WMSOUTBSNO = "";
feeEntities.Add(feeEntity);
//
string ssql = "INSERT INTO [ch_fee_do]([GID],[BILLNO],[BSNO],[MBLNO],[HBLNO],[CUSTOMERNAME],[BSTYPE],[FEEID],[FEENAME],[CURRENCY],[AMOUNT],[DOAMOUNT],[FEETYPE],[CATEGORY],[BILLSTATUS],[REMARK],[ISDELETED],[CREATETIME],[EXCHANGERATE],[ORIGCURRENCY],[DELETEUSER],[DELETETIME],[ORIGAMOUNT],[INVOICESETTLENO],[COMPANYID]) "
+ " select '" + Guid.NewGuid().ToString() + "' as [GID],'" + strNewBillNO + "' as [BILLNO],[BSNO],[MBLNO],[HBLNO],'" + feeEntity.CustomerName.ToString() + "' as [CUSTOMERNAME],[BSTYPE],'" + feeEntity.GID + "' as [FEEID],'" + feeEntity.FeeName + "' as [FEENAME],'" + feeEntity.Currency + "' as [CURRENCY]," + feeEntity.Amount + " as [AMOUNT]," + feeEntity.Amount + " as [DOAMOUNT],'" + feeEntity.FeeType + "' as [FEETYPE],[CATEGORY],[BILLSTATUS],[REMARK],[ISDELETED],[CREATETIME],'" + feeEntity.ExchangeRate + "' as [EXCHANGERATE],'" + feeEntity.Currency + "' as [ORIGCURRENCY],[DELETEUSER],[DELETETIME]," + feeEntity.Amount + " as [ORIGAMOUNT],[INVOICESETTLENO],[COMPANYID] from ch_fee_do where GID='" + feeDoEntity.GID.ToString().Trim() + "'";
alFeeDo.Add(ssql);
}
}
}
}
#endregion
#region 插入 ch_fee_settlement 表
feeSettlementEntity.BillNO = strNewBillNO;
feeSettlementEntity.BillType = 1;
feeSettlementEntity.VoucherNO = txt_voucherNO.Value.Trim();
feeSettlementEntity.SettleUser = strUserID;
feeSettlementEntity.BillStatus = 1;
if (oldBillStatus == 0 || oldBillStatus == 5)//如果收费申请状态是已审批或部分结算,则表示收费结算方式为收费申请方式
{
feeSettlementEntity.SettleMode = 1;//申请结算
}
else
{
feeSettlementEntity.SettleMode = 2;//自由结算
}
feeSettlementEntity.FinancialVoucher = "";
feeSettlementEntity.BillType = 1;
feeSettlementEntity.CustomerName = this.txt_customerName.Value.Trim();//结算单位
feeSettlementEntity.SettleType = int.Parse(this.sel_settlemode.Value);
feeSettlementEntity.Remark = txt_remark.Value.Trim();
feeSettlementEntity.CompanyID = strCompanyID.Trim();
feeSettlementEntity.AuditUser = strUserID.Trim();
feeSettlementEntity.ChequeNo = txt_chequeno.Value.Trim();
feeSettlementEntity.ChequeNoUsd = txt_chequenousd.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());
}
#endregion
#region 预收预收处理
if (this.h_ISADVANCE.Value.Trim() == "True")
{
#region 插入预收预收表
if (feeSettlementEntity.PREPAY_MONEY != 0)
{
Decimal strEXCHANGERATE = 1;
if (feeSettlementEntity.PREPAY_RATE.ToString().Trim() == "")
{
strEXCHANGERATE = feeSettlementEntity.PREPAY_RATE;
if (strEXCHANGERATE <= 0)
{
strEXCHANGERATE = 1;
}
}
else
{
strEXCHANGERATE = 1;
}
//
string ssql = "INSERT INTO [ch_fee_advance_payment](GID,LINKGID,BILLNO,FEETYPE,CUSTOMERNAME,CURRENCY,AMOUNT,CREATEUSER,CREATETIME,DOAMOUNT,SETTLEUSER,REMARK,ISDELETE,DELETEUSER,ISFINISH,COMPANYID,VOUCHERNO,EXCHANGERATE) "
+ " values ('" + feeSettlementEntity.GID.ToString() + "','" + feeSettlementEntity.GID.ToString() + "','" + feeSettlementEntity.BillNO.ToString() + "',1,'" + feeSettlementEntity.CustomerName.ToString() + "','" + feeSettlementEntity.ACCOUNTS_CURRENCY.ToString() + "','" + feeSettlementEntity.PREPAY_MONEY.ToString() + "','" + strUserID.Trim() + "',getdate(),0,'','',0,'',0,'" + strCompanyID.Trim() + "','','" + strEXCHANGERATE + "')";
alFeeDo.Add(ssql);
}
#endregion
#region 预收预收取用解析post返回的值并形成插入更新的sql语句
if (this.h_isAccess.Value.Trim() == "1")
{
recvJSON_2 = "";
recvJSON_2 = this.recvContainer_2.Value.Trim();
if (!recvJSON_2.Trim().Equals(""))
{
string tempSettle = recvJSON_2.Substring(recvJSON_2.IndexOf("[") + 1, recvJSON_2.IndexOf("]") - recvJSON_2.IndexOf("[") - 1);
string[] tempSettleFees = tempSettle.Split(new string[] { "},{" }, System.StringSplitOptions.RemoveEmptyEntries);
if (tempSettleFees.Length > 0)
{
for (int i = 0; i < tempSettleFees.Length; i++)
{
// 1 2 3 4 5 6 7
//客户名称,单号,实录时间,币别,实录金额,未核销金额,本次核销");
string GID = ""; //客户名称
string CUSTOMERNAME = ""; //客户名称
string BILLNO = ""; //单号
string CREATETIME = ""; //实录时间
string CURRENCY = ""; //币别
decimal AMOUNT = 0; //实录金额
decimal DOAMOUNT2 = 0; //未核销金额
decimal DOAMOUNT = 0; //本次核销
string ISFINISH = ""; //是否全部完成核销
tempSettleFees[i] = tempSettleFees[i].ToString().Replace("{", "");
tempSettleFees[i] = tempSettleFees[i].ToString().Replace("}", "");
string[] strCell = tempSettleFees[i].Split(new char[] { ',' });
if (strCell.Length > 0)
{
for (int j = 0; j < strCell.Length; j++)
{
string[] strArg = strCell[j].Split(new char[] { ':' });
switch (strArg[0].Replace("\"", ""))
{
case "GID":
GID = strArg[1].ToString().Replace("\"", "").Trim();
break;
case "CUSTOMERNAME"://客户名称
UnicodeEncoding unicode = new UnicodeEncoding();
CUSTOMERNAME = unicode.GetString(unicode.GetBytes(Regex.Unescape(strArg[1].ToString().Replace("\"", "").Trim())));
break;
case "BILLNO"://单号
BILLNO = strArg[1].ToString().Replace("\"", "").Trim();
break;
case "CREATETIME"://实录时间
CREATETIME = strArg[1].ToString().Replace("\"", "").Trim();
break;
case "CURRENCY"://币别
CURRENCY = strArg[1].ToString().Replace("\"", "").Trim();
break;
case "AMOUNT"://实录金额
AMOUNT = decimal.Parse(strArg[1].ToString().Replace("\"", "").Trim());
break;
case "DOAMOUNT2"://未核销金额
DOAMOUNT2 = decimal.Parse(strArg[1].ToString().Replace("\"", "").Trim());
break;
case "DOAMOUNT"://本次核销
DOAMOUNT = decimal.Parse(strArg[1].ToString().Replace("\"", "").Trim());
break;
case "ISFINISH"://是否全部完成核销
ISFINISH = strArg[1].ToString().Replace("\"", "").Trim();
break;
default:
break;
}
}
}
if (DOAMOUNT != 0 && GID.Trim() != "")
{
//更新主表
string ssql = "update [ch_fee_advance_payment] set DOAMOUNT=DOAMOUNT+" + DOAMOUNT + ",ISFINISH=" + ISFINISH + " where GID='" + GID + "' and (ISDELETE=0 or ISDELETE is null)";
alFeeDo.Add(ssql);
//插入明细表
ssql = "INSERT INTO [ch_fee_advance_payment_detail](GID,LINKGID,BILLNO,SETTLELINKGID,SETTLEBILLNO,FEETYPE,CUSTOMERNAME,CURRENCY,DOAMOUNT,SETTLEUSER,SETTLETIME,REMARK,ISDELETE,DELETEUSER,COMPANYID,VOUCHERNO) "
+ " values ('" + Guid.NewGuid().ToString() + "','" + GID + "','" + BILLNO + "','" + feeSettlementEntity.GID.ToString() + "','" + feeSettlementEntity.BillNO.ToString() + "',1,'" + CUSTOMERNAME + "','" + CURRENCY + "'," + DOAMOUNT + ",'" + strUserID.Trim() + "',getdate(),'',0,'','" + strCompanyID.Trim() + "','')";
alFeeDo.Add(ssql);
}
//
}
}
//
recvJSON_2 = "";
this.recvContainer_2.Value = "";
}
}
#endregion
}
#endregion
FeeRecvSettleDA FeeRecvSettleDA = new EntityDA.FeeRecvSettleDA();
iResult = FeeRecvSettleDA.SettleFeeRecvApplication(feeSettlementEntity, ChFeeRecvApplicationEntity, feeDoEntities2, strCompanyID, strUserID, feeEntities, alFeeDo);//feeDoEntities
if (iResult == 1)
{
this.btn_enter.Disabled = true;
this.txt_voucherNO.Disabled = true;
this.sel_settlemode.Disabled = true;
this.txt_remark.Disabled = true;
}
}
}
#endregion
#region 返回操作结果
if (iResult == 1)
{
txt_billNO.Value = strNewBillNO;
txt_customerName.Disabled = true;
sel_settlemode.Disabled = true;
txt_voucherNO.Disabled = true;
//this.btn_enter.Disabled = true;
FeeSettlementDA feeSettlementDA = new FeeSettlementDA();
feeSettlementEntity = feeSettlementDA.GetFeeSettlementByBillNO(strNewBillNO);
txt_billStatus.Value = "锁定";
txt_settleTime.Value = feeSettlementEntity.SettleTime.ToString("yyyy-MM-dd HH:mm:ss");
UserDA userDA = new UserDA();
UserEntity userEntity = userDA.GetUserSignByID(feeSettlementEntity.SettleUser);
this.txt_settleUser.Value = userEntity.ShowName;
h_enter.Value = "0";
//判断是否自动生成凭证
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('收费结算成功');window.parent.opener.location.href = window.parent.opener.location.href;cwvouchersadd('" + strFeeRecvSettleID + "');window.close();</script>");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>alert('收费结算成功');window.parent.opener.location.href = window.parent.opener.location.href;window.close();</script>");
}
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>alert('收费结算成功');window.parent.opener.location.href = window.parent.opener.location.href;window.close();</script>");
}
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "key2", "<script>alert('收费结算失败');window.close();</script>");
}
#endregion
}
#endregion
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_settlement WHERE BILLNO LIKE '" + strFeeType + strDateTime + "%' ORDER BY BILLNO DESC";
string strSql = " SELECT TOP 1 BILLNO FROM "
+ " (SELECT BILLNO FROM ch_fee_payapplication WHERE BILLNO LIKE '" + strFeeType + strDateTime + "%' UNION "
+ " SELECT BILLNO FROM ch_fee_settlement 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;
}
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;
}
}
}
}