You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

575 lines
21 KiB

using BWP.B3Frameworks;
using BWP.B3Frameworks.BO.MoneyTemplate;
using BWP.B3Frameworks.Utils;
using BWP.B3Sale.BO;
using BWP.B3Sale.Utils;
using BWP.Web.Layout;
using BWP.Web.Utils;
using BWP.Web.WebControls;
using Forks.EnterpriseServices.BusinessInterfaces;
using Forks.EnterpriseServices.DataForm;
using Forks.EnterpriseServices.DomainObjects2;
using Forks.EnterpriseServices.DomainObjects2.DQuery;
using Forks.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using TSingSoft.WebControls2;
using TSingSoft.WebPluginFramework;
using BWP.B3_YunKen;
namespace BWP.Web.Pages.B3YunKen.Dialogs
{
class SelectBillDialog : AppBasePage
{
private DFBrowseGrid _browseGrid;
string Ids
{
get { return Request.QueryString["ids"]; }
}
string CusID
{
get { return Request.QueryString["cusID"]; }
}
private bool _someError;
private Label _moneyLabel;
private Label _selectMoneyLabel;
long CustomerID
{
get
{
if (ViewState["CustomerID"] != null)
return (long)ViewState["CustomerID"];
return 0;
}
set { ViewState["CustomerID"] = value; }
}
long AccountingUnit_ID
{
get
{
if (ViewState["AccountingUnit_ID"] != null)
return (long)ViewState["AccountingUnit_ID"];
return 0;
}
set { ViewState["AccountingUnit_ID"] = value; }
}
decimal ThisMoney
{
get
{
if (ViewState["thisMoney"] != null)
return (decimal)ViewState["thisMoney"];
return 0;
}
set { ViewState["thisMoney"] = value; }
}
// 选中核销的金额
decimal SelectMoney
{
get
{
if (ViewState["SelectMoney"] != null)
return (decimal)ViewState["SelectMoney"];
return 0;
}
set { ViewState["SelectMoney"] = value; }
}
private readonly B3SaleOnlineConfiguration _config = new B3SaleOnlineConfiguration();
protected override void InitForm(HtmlForm form)
{
var zone = new TitlePanelZone();
_moneyLabel = new Label();
_selectMoneyLabel = new Label();
_someError = !Before();
if (_someError)
return;
form.Controls.Add(zone);
AddQueryControls(zone);
CreateQueryGrid(zone);
}
private DFDateInput _minInput, _maxInput;
private void AddQueryControls(TitlePanelZone zone)
{
var titlePanel = new TitlePanel(B3FrameworksConsts.PageLayouts.QueryConditions, B3FrameworksConsts.PageLayouts.QueryConditions_DisplayName);
titlePanel.Style.Add(HtmlTextWriterStyle.BackgroundColor, "white");
titlePanel.SetPageLayoutSetting(mPageLayoutManager, B3FrameworksConsts.PageLayouts.QueryConditions);
zone.Add(titlePanel);
var vbox = AddChild(titlePanel, new VLayoutPanel());
var layoutManager = new LayoutManager("");
_minInput = new DFDateInput();
_maxInput = new DFDateInput();
_maxInput.DefaultTime = DateInputDefaultTime.maxValue;
_minInput.DefaultTime = DateInputDefaultTime.minValue;
var panel = new Panel();
panel.Controls.Add(_minInput);
panel.Controls.Add(new LiteralControl("→"));
panel.Controls.Add(_maxInput);
layoutManager.Add("单据日期", new SimpleLabel("单据日期"), panel);
var bt = new TSButton("开始查询", delegate { StartQuery(); });
layoutManager.Add("开始查询", bt, false, true);
var config = new AutoLayoutConfig();
config.Cols = 6;
config.Add("单据日期");
layoutManager.Config = config;
vbox.Add(layoutManager.CreateLayout(), new VLayoutOption(HorizontalAlign.Left));
var hPanel = vbox.Add(new HLayoutPanel(), new VLayoutOption(HorizontalAlign.Left));
hPanel.Add(new TSButton("开始查询", delegate {
StartQuery();
}));
var label1 = new Label();
label1.Text = "金额:";
_moneyLabel.ID = "moneyLabel";
_moneyLabel.Font.Size = 12;
_moneyLabel.Font.Bold = true;
var label2 = new Label();
label2.Text = "本次核销金额:";
_selectMoneyLabel.ID = "selectMoneyLabel";
_selectMoneyLabel.Font.Size = 12;
_selectMoneyLabel.Font.Bold = true;
var verficationbt = hPanel.Add(new TSButton("核销"));
verficationbt.Click += delegate {
_browseGrid.GetFromUI();
using (var context = new TransactionContext())
{
var selectItems = _browseGrid.GetSelectedItems();
var money = selectItems.Sum(x => (((decimal?)x["本次核销金额"]) ?? 0));
if (money > ThisMoney)
throw new ApplicationException("本次核销金额大于收款单金额");
var ids = Ids.Split(',').Where(x => !string.IsNullOrEmpty(x)).Select(long.Parse).ToList();
var nextID = VerificationUtil.GetNextID(context);
var onlineConfig = new B3YunKenOnlineConfiguration();
if (onlineConfig.UseOneGathering && ids.Count == 1) {
var id = ids[0];
var gatheringMoney = InnerBLUtil.GetDmoPropertyByID<Gathering, Money<>?>(context.Session, "GatheringMoney", id);
var verificationMoney = InnerBLUtil.GetDmoPropertyByID<Gathering, Money<>?>(context.Session, "VerificationMoney", id);
var tMoney = (gatheringMoney ?? 0) - (verificationMoney ?? 0);
var gathering = new Gathering();
gathering.ID = id;
foreach (var item in selectItems) {
var billMoney = ((decimal?)item["本次核销金额"]) ?? 0;
if (tMoney <= 0)
continue;
if (billMoney <= 0)
continue;
if (billMoney <= tMoney) {
tMoney -= billMoney;
var billType = item["单据类型"] == null ? "销售出库单" : item["单据类型"].ToString();
short billTypeId;
if (billType == "销售发票") {
billTypeId = DmoTypeIDAttribute.GetID(typeof(Invoice));
} else if (billType == "客户验收单") {
billTypeId = DmoTypeIDAttribute.GetID(typeof(CustomerAccept));
} else if (billType == "其他应收款") {
billTypeId = DmoTypeIDAttribute.GetID(typeof(OtherReceivables));
} else {
billTypeId = DmoTypeIDAttribute.GetID(typeof(SaleOutStore));
}
var billId = (long)item["ID"];
var customer_Detail = new InvoiceInfo();
customer_Detail.Gathering_ID = gathering.ID;
customer_Detail.BillID = billId;
customer_Detail.BillMoney = (Money<>?)item["金额"];
customer_Detail.DmoTypeID = billTypeId;
customer_Detail.Date = (DateTime?)item["单据日期"];
customer_Detail.AlreadyMoney = (Money<>?)item["VerificationMoney"];
customer_Detail.Customer_ID = CustomerID;
customer_Detail.Money = billMoney;
customer_Detail.VerificationTable_ID = nextID;
context.Session.Insert(customer_Detail);
}
}
VerificationUtil.Insert(context, gathering, money, nextID);
} else {
foreach (var id in ids) {
var gathering = new Gathering();
var gatheringMoney = InnerBLUtil.GetDmoPropertyByID<Gathering, Money<>?>(context.Session, "GatheringMoney", id);
var verificationMoney = InnerBLUtil.GetDmoPropertyByID<Gathering, Money<>?>(context.Session, "VerificationMoney", id);
var tMoney = (gatheringMoney ?? 0) - (verificationMoney ?? 0);
if (tMoney <= 0)
continue;
if (money <= 0)
continue;
if (money < tMoney) {
tMoney = money;
}
money -= tMoney;
gathering.ID = id;
VerificationUtil.Insert(context, gathering, tMoney, nextID);
}
}
foreach (var row in _browseGrid.GetSelectedItems())
{
BWP.B3Frameworks.BO.Bill dmo;
var billType = row["单据类型"] == null ? "销售出库单" : row["单据类型"].ToString();
if (billType == "销售发票")
{
dmo = new Invoice();
}
else if (billType == "客户验收单")
{
dmo = new CustomerAccept();
}
else if (billType == "其他应收款")
{
dmo = new OtherReceivables();
}
else
{
dmo = new SaleOutStore();
}
dmo.ID = (long)row["ID"];
var billMoney = (Money<>?)row["金额"];
var verMoney = (Money<>?)row["VerificationMoney"];
var thisMoney = ((decimal?)row["本次核销金额"]) ?? 0m;
var tMoney = (billMoney ?? 0) - (verMoney ?? 0);
if (thisMoney == 0)
continue;
if (billMoney > 0 && thisMoney > tMoney)
throw new ApplicationException("单据No." + dmo.ID + " 本次核销金额大于未核销金额");
if (billMoney < 0 && thisMoney < tMoney)
throw new ApplicationException("单据No." + dmo.ID + " 本次核销金额小于未核销金额(本单据金额为负)");
if (billType == "销售发票")
{
VerificationUtil.Insert(context, (Invoice)dmo, thisMoney, nextID);
}
else if (billType == "客户验收单")
{
VerificationUtil.Insert(context, (CustomerAccept)dmo, thisMoney, nextID);
}
else if (billType == "其他应收款")
{
VerificationUtil.Insert(context, (OtherReceivables)dmo, thisMoney, nextID);
}
else
{
VerificationUtil.Insert(context, (SaleOutStore)dmo, thisMoney, nextID);
}
}
context.Commit();
}
DialogUtil.SetCachedObj(this, "", "核销成功");
};
hPanel.Add(new LiteralControl("&nbsp;&nbsp;"));
hPanel.Add(label1);
hPanel.Add(_moneyLabel);
hPanel.Add(new LiteralControl("&nbsp;&nbsp;"));
hPanel.Add(label2);
hPanel.Add(_selectMoneyLabel);
}
//private bool IsExistInvoiceInfo(TransactionContext context,short billTypeId,long billId,long gatheringId)
//{
// var query = new DQueryDom(new JoinAlias(typeof(InvoiceInfo)));
// query.Columns.Add(DQSelectColumn.Field("ID"));
// query.Where.Conditions.Add(DQCondition.EQ("DmoTypeID",billTypeId));
// query.Where.Conditions.Add(DQCondition.EQ("BillID", billId));
// query.Where.Conditions.Add(DQCondition.EQ("Gathering_ID", gatheringId));
// return query.EExecuteScalar<long>(context.Session) != null?true:false;
//}
private void CreateQueryGrid(TitlePanelZone zone)
{
var result = new TitlePanel(B3FrameworksConsts.PageLayouts.QueryResult, B3FrameworksConsts.PageLayouts.QueryResult_DisplayName);
result.Style.Add(HtmlTextWriterStyle.BackgroundColor, "#ebebeb");
result.SetPageLayoutSetting(mPageLayoutManager, B3FrameworksConsts.PageLayouts.QueryResult);
var editor = new DFDataTableEditor();
_browseGrid = new DFBrowseGrid(editor);
result.Controls.Add(_browseGrid);
_browseGrid.DFGridSetEnabled = false;
_browseGrid.IgnoreItemsCount = true;
_browseGrid.EnableRowID = true;
_browseGrid.MultiSelectionEnabled = true;
_browseGrid.Width = Unit.Percentage(100);
_browseGrid.Columns.Add(new DFBrowseGridColumn("单据类型"));
_browseGrid.Columns.Add(new DFBrowseGridColumn("ID"));
_browseGrid.Columns.Add(new DFBrowseGridColumn("AccountingUnit_Name"));
_browseGrid.Columns.Add(new DFBrowseGridColumn("结账客户"));
_browseGrid.Columns.Add(new DFBrowseGridColumn("单据日期"));
_browseGrid.Columns.Add(new DFBrowseGridColumn("金额") { SumMode = SumMode.Sum });
_browseGrid.Columns.Add(new DFBrowseGridColumn("VerificationMoney") { SumMode = SumMode.Sum });
var col = new DFEditGridColumn<DFTextBox>("本次核销金额");
col.InitEditControl += (sender, e) => {
var str = e.Control.Attributes["onchange"] ?? string.Empty;
e.Control.Attributes["onchange"] = str + "_SumMoney();";
};
_browseGrid.Columns.Add(col);
_browseGrid.OnDetailDataBound = (row, obj, index) =>
{
if (!IsPostBack)
{
var dataRow = obj as DFDataRow;
if (dataRow == null)
{
return;
}
var = ThisMoney - SelectMoney;
if ( > 0)
{
var item = row.Cells[0];
var re = item.Controls[0] as HtmlInputCheckBox;
re.Checked = true;
var = Convert.ToDecimal(dataRow["本次核销金额"]);
var = - ;
if ( > 0)
{
SelectMoney += ;
}
else
{
var = + ;
SelectMoney += ;
dataRow["本次核销金额"] = ;
}
}
_selectMoneyLabel.Text = (SelectMoney).ToString();
}
};
var hbox = new HLayoutPanel();
hbox.CssClass += " LeftPaddingWrapper";
result.Controls.Add(hbox);
zone.Add(result);
}
void StartQuery()
{
_browseGrid.Query = GetQueryDom();
_browseGrid.Reset();
_browseGrid.DataBind();
}
private DQueryDom GetQueryDom()
{
var doms = _config.VerificationBillTypeSet.Value.Select(x => GetQueryDom(x));
if (doms == null)
{
return GetQueryDom("销售出库单");
}
var dom = doms.ElementAt(0);
var current = dom;
for (int i = 1; i < doms.Count(); i++)
{
var next = doms.ElementAt(i);
current.UnionNext.Select = next;
current = next;
}
return dom;
}
private DQueryDom GetQueryDom(string billType)
{
DQueryDom dom;
string customerField;
string moneyField;
string customerNameField;
string dateField;
if (billType == "销售发票")
{
customerField = "Customer_ID";
moneyField = "Money";
customerNameField = "Customer_Name";
dom = new DQueryDom(new JoinAlias(typeof(Invoice)));
dom.Columns.Add(DQSelectColumn.Create(DQExpression.ConstValue("销售发票"), "单据类型"));
dom.Columns.Add(DQSelectColumn.Create(DQExpression.Field("Date"), "单据日期"));
dateField = "Date";
}
else if (billType == "客户验收单")
{
customerField = "Customer_ID";
moneyField = "Money";
customerNameField = "Customer_Name";
dom = new DQueryDom(new JoinAlias(typeof(CustomerAccept)));
dom.Columns.Add(DQSelectColumn.Create(DQExpression.ConstValue("客户验收单"), "单据类型"));
dom.Columns.Add(DQSelectColumn.Create(DQExpression.Field("BillDate"), "单据日期"));
dateField = "BillDate";
}
else if (billType == "其他应收款")
{
customerField = "AccountCustomer_ID";
moneyField = "OtherMoney";
customerNameField = "AccountCustomer_Name";
dom = new DQueryDom(new JoinAlias(typeof(OtherReceivables)));
dom.Columns.Add(DQSelectColumn.Create(DQExpression.ConstValue("其他应收款"), "单据类型"));
dom.Columns.Add(DQSelectColumn.Create(DQExpression.Field("OtherReceivablesTime"), "单据日期"));
dateField = "OtherReceivablesTime";
}
else
{
customerField = "Customer_ID";
moneyField = "Money";
customerNameField = "Customer_Name";
dom = new DQueryDom(new JoinAlias(typeof(SaleOutStore)));
dom.Columns.Add(DQSelectColumn.Create(DQExpression.ConstValue("销售出库单"), "单据类型"));
dom.Columns.Add(DQSelectColumn.Create(DQExpression.Field("LoadTime"), "单据日期"));
dateField = "LoadTime";
}
dom.Columns.Add(DQSelectColumn.Field("ID"));
dom.Columns.Add(DQSelectColumn.Field("AccountingUnit_Name"));
dom.Columns.Add(DQSelectColumn.Field("VerificationMoney"));
dom.Columns.Add(DQSelectColumn.Create(DQExpression.Field("AccountCustomer_ID"), "AccountCustomer_ID"));
dom.Columns.Add(DQSelectColumn.Create(DQExpression.Field(customerNameField), "结账客户"));
dom.Columns.Add(DQSelectColumn.Create(DQExpression.Field(moneyField), "金额"));
dom.Columns.Add(DQSelectColumn.Create(DQExpression.Field(moneyField).ESubtract(DQExpression.Field("VerificationMoney")), "本次核销金额"));
dom.Where.Conditions.Add(DQCondition.EQ(customerField, CustomerID));
if (AccountingUnit_ID > 0)
dom.Where.Conditions.Add(DQCondition.EQ("AccountingUnit_ID", AccountingUnit_ID));
dom.EAddCheckedCondition(dom.From.RootSource.Alias);
if (_minInput.Value.HasValue)
{
dom.Where.Conditions.Add(DQCondition.GreaterThanOrEqual(dateField, _minInput.Value));
}
if (_maxInput.Value.HasValue)
{
dom.Where.Conditions.Add(DQCondition.LessThanOrEqual(dateField, _maxInput.Value));
}
dom.OrderBy.Expressions.Add(DQOrderByExpression.Create("ID",false));
dom.Where.Conditions.Add(DQCondition.Or(DQCondition.And(DQCondition.LessThan(moneyField, 0), DQCondition.LessThan(DQExpression.Field(moneyField), DQExpression.IfNull(DQExpression.Field("VerificationMoney"), DQExpression.ConstValue(0)))), DQCondition.GreaterThan(DQExpression.Field(moneyField), DQExpression.IfNull(DQExpression.Field("VerificationMoney"), DQExpression.ConstValue(0)))));
return dom;
}
private bool Before()
{
if (string.IsNullOrEmpty(Ids))
{
if (string.IsNullOrEmpty(CusID))
{
AspUtil.Alert(this, "请选择收款单据或者结转客户");
return false;
}
CustomerID = long.Parse(CusID);
ThisMoney = 0;
_moneyLabel.Text = (0).ToString();
return true;
}
var dom = new DQueryDom(new JoinAlias(typeof(Gathering)));
dom.Columns.Add(DQSelectColumn.Field("AccountCustomer_ID"));
dom.Columns.Add(DQSelectColumn.Field("AccountCustomer_Name"));
dom.Columns.Add(DQSelectColumn.Create(DQExpression.Sum(DQExpression.Field("GatheringMoney").ESubtract(DQExpression.Field("VerificationMoney"))), "money"));
dom.Columns.Add(DQSelectColumn.Field("AccountingUnit_ID"));
dom.GroupBy.Expressions.Add(DQExpression.Field("AccountCustomer_ID"));
dom.GroupBy.Expressions.Add(DQExpression.Field("AccountCustomer_Name"));
dom.GroupBy.Expressions.Add(DQExpression.Field("AccountingUnit_ID"));
var ids = Ids.Split(',').Select(long.Parse).ToList();
dom.Where.Conditions.EFieldInList("ID", ids);
var tuples = dom.EExecuteList<long?, string, decimal?, long?>();
if (tuples.Count > 1)
{
AspUtil.Alert(this, "选择的客户不一致");
return false;
}
CustomerID = tuples[0].Item1 ?? 0;
ThisMoney = (tuples[0].Item3 ?? 0);
AccountingUnit_ID = (tuples[0].Item4 ?? 0);
_moneyLabel.Text = (tuples[0].Item3 ?? 0).ToString();
return true;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!IsPostBack && !_someError)
{
StartQuery();
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (!_someError)
{
string script = @"var defaultGrid=document.all." + _browseGrid.ClientID + @";
$(function(){
var grid = defaultGrid.behind;
var itemEndIndex = grid.mItemEndIndex - 1;
for (i = 0; i <= itemEndIndex; i++) {
var row = grid.front.rows[i];
var chkbox = row.cells[0].getElementsByTagName(""INPUT"")[0];
if (chkbox) {
if(i==0)
$(chkbox).change(function() {
_SumMoney(this.checked?'1':'0');
});
else
$(chkbox).change(function() {
_SumMoney();
});
}
}
});
function _SumMoney(allCheck)
{
var grid = defaultGrid.behind;
var itemEndIndex = grid.mItemEndIndex - 1;
var sumM = 0.0;
for (i = 1; i <= itemEndIndex; i++) {
var row = grid.front.rows[i];
var chkbox = row.cells[0].getElementsByTagName(""INPUT"")[0];
if (!chkbox)
continue;
if (allCheck){
if(allCheck == '0')
continue;
} else{
if (!chkbox.checked)
continue;
}
var m = row.dfContainer.getValue('本次核销金额');
if (m != null && m != '') {
sumM = sumM + 1 * m;
}
}
var label = document.getElementById('{selectMoneyLabel}')
label.innerText = sumM.toFixed(2);
if( sumM > {thisMoney}){
label.style.color = 'red';
} else {
label.style.color = 'black';
}
} ";
script = script.Replace("{thisMoney}", ThisMoney.ToString());
script = script.Replace("{selectMoneyLabel}", _selectMoneyLabel.ClientID);
Page.ClientScript.RegisterStartupScript(GetType(), "Startup", script, true);
}
}
}
}