LR-20210131IOQH\Administrator
2021-09-13 1435b5b13b3857a20292d5c869f95e0c2ae5ffea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**  
* OrderMoneyReceive.aspx.cs
*
* 功 能: 订单收款
* 类 名: OrderMoneyReceive
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2013-5-28 13:44      吴崎均    初版
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CY.BLL.EC;
using CY.Infrastructure.Common;
using CY.Model;
using CY.BLL.OA;
using CY.BLL.Sys;
using System.Text;
using CY.BLL;
using System.Transactions;
 
namespace CY.WebForm.Pages.financial
{
    public partial class OrderMoneyBatchReceive : BasePage
    {
        private EC_OrderBasicBLL _eC_OrderBasicBLL = null;
       
        private OA_CorporateClientsBLL bll_OA_CorporateClientsBLL = null;
 
        /// <summary>
        /// 初始化构造 
        /// </summary>
        public OrderMoneyBatchReceive()
        {
            bll_OA_CorporateClientsBLL = new OA_CorporateClientsBLL();
        }
        /// <summary>
        /// 界面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            _eC_OrderBasicBLL = new EC_OrderBasicBLL();
            btnSubmit.ServerClick += new EventHandler(btnSubmit_ServerClick);
 
            LoadOrder();
            if (IsPostBack || IsCallback)
                return;
            InitPage();
 
        }
 
        /// <summary>
        /// 提交按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnSubmit_ServerClick(object sender, EventArgs e)
        {
            string[] orderIds = Request["ids"].Split(',');
            string receiveWay = Request["rdoReceiveWay"];
            bool isWin = false;
            using (var scope = new TransactionScope(TransactionScopeOption.Required,
     new TransactionOptions
     {
         IsolationLevel = IsolationLevel.ReadCommitted,
         Timeout = TransactionManager.MaximumTimeout
     }
 ))
            {
                foreach (string orderId in orderIds)
                {
 
                    EC_OrderBasic _orderBasic = _eC_OrderBasicBLL.SelectModelById(orderId.ToInt32().Value);
                    OA_CorporateClients m_OA_CorporateClients = bll_OA_CorporateClientsBLL.SelectModelByFirmIdandMemberId(CurrentUser.MemberId, _orderBasic.BuyerId);
                    if (receiveWay == "0" && m_OA_CorporateClients != null)
                    {
                        if (this.txtReceiveMoney.Value.ToInt32() > (m_OA_CorporateClients.Prepayments + m_OA_CorporateClients.Credit))
                        {
                            JavaScript.MessageBox("预付款不足!", this, "window.location='/Pages/financial/OrderMoneyBatchReceive.aspx?id=" + Request["ids"] + "'");
                            //Response.Redirect("/Pages/financial/OrderMoneyReceive.aspx?id=" + orderId);
                            return;
                        }
                    }
                    if (string.IsNullOrEmpty(orderId) || string.IsNullOrEmpty(receiveWay))
                    {
                        JavaScript.MessageBox("传入参数错误!", this);
                        return;
                    }
                    if (null == _orderBasic)
                    {
                        JavaScript.MessageBox("订单不存在或已删除!", this);
                        return;
                    }
                    isWin = "0".Equals(receiveWay) ? UsePrepare(_orderBasic) : ToAccount(_orderBasic.Keyid.Value, _orderBasic.BuyerName, _orderBasic.UnPayedMoney);
                    if (!isWin)
                    {
                        break;
                    }
 
                }
                scope.Complete();
            }
             
 
            if (isWin)
            {
                JavaScript.MessageBox("操作成功", this, true, true);
            }
            else
            {
                JavaScript.MessageBox("操作失败", this);
            }
            //JavaScript.MessageBox(isWin ? "收款成功!" : "收款失败!", this, "top.frmright.ReLoad();top.Dialog.close();");
            Request.RequestContext.RouteData.DataTokens.Add("IsWin", isWin ? "1" : "");
            if (!isWin)
                InitPage();
            else
                ;
 
 
        }
 
        /// <summary>
        /// 加载页面数据
        /// </summary>
        private void InitPage()
        {
            try
            {
                Sys_DictionaryBLL _Sys_DictionaryBLL = new Sys_DictionaryBLL();
                OA_SubjectSetBLL _OA_SubjectSetBLL = new OA_SubjectSetBLL();
                OA_FirmAccountBLL oA_FirmAccountBLL = new OA_FirmAccountBLL();
 
                //科目名称
                this.selSubjects.DataSource = _OA_SubjectSetBLL.getAllSubject(CurrentUser.MemberId, "借");
                this.selSubjects.DataBind();
                this.selSubjects.Items.Insert(0, new ListItem("请选择", ""));
 
                int i = -1;
                //账户类型
                List<Sys_Dictionary> accountsType = _Sys_DictionaryBLL.GetDataByType("账户类型") as List<Sys_Dictionary>;
                Request.RequestContext.RouteData.DataTokens.Add("accountsTypes", null == accountsType || accountsType.Count == 0 ? "[]" : JsonHelper.GetJsonStringByObject(accountsType));
 
                List<OA_FirmAccount> firmAccounts = oA_FirmAccountBLL.getAllSubject(CurrentUser.MemberId, "") as List<OA_FirmAccount>;
 
                const string ACCOUNTSJSONKEY = "accounts";
                if (null == firmAccounts)
                {
                    Request.RequestContext.RouteData.DataTokens.Add(ACCOUNTSJSONKEY, "[]");
                    return;
                }
 
                IEnumerable<IGrouping<string, OA_FirmAccount>> groupings = firmAccounts.GroupBy(fa => fa.AccountType);
                StringBuilder builder = new StringBuilder();
 
                foreach (IGrouping<string, OA_FirmAccount> groupitem in groupings)
                {
                    OA_FirmAccount[] items = groupitem.ToArray();
                    builder.AppendFormat("{0}\"{2}\":{3}{1}", ",", "", groupitem.Key, JsonHelper.GetJsonStringByObject(items));
                }
                builder.Append("}]");
                builder.Replace(",", "[{", 0, 1);
 
                Request.RequestContext.RouteData.DataTokens.Add(ACCOUNTSJSONKEY, null == firmAccounts ? "[]" : builder.ToString());
 
            }
            catch (Exception ex)
            {
                PAGEHandleException(ex);
            }
        }
 
        /// <summary>
        /// 加载订单
        /// </summary>
        private void LoadOrder()
        {
            string[] orderIds = Request["id"].Split(',');
            this.ids.Value = Request["id"];
            List<EC_OrderBasic> eC_OrderBasics = new List<EC_OrderBasic>();
            decimal shoukuanmoney = 0;
            if (orderIds==null || orderIds.Length==0)
            {
                return;
            }
 
            try
            {
                foreach (string orderId in orderIds)
                {
                    EC_OrderBasic _orderBasicmode = _eC_OrderBasicBLL.SelectModelById(orderId.ToInt32().Value);
                    OA_CorporateClients m_OA_CorporateClients = bll_OA_CorporateClientsBLL.SelectModelByFirmIdandMemberId(CurrentUser.MemberId, _orderBasicmode.BuyerId);
                    if (m_OA_CorporateClients != null)
                    {
                        _orderBasicmode.nowCustomMoney = "¥" + (m_OA_CorporateClients.Prepayments ?? 0).ToString("0.00");
                        _orderBasicmode.nowCredit = "¥" + (m_OA_CorporateClients.Credit ?? 0).ToString("0.00");
                    }
                    else
                    {
                        _orderBasicmode.nowCustomMoney = "¥0";
                        _orderBasicmode.nowCredit = "¥0";
                    }
 
 
 
                    shoukuanmoney += _orderBasicmode.UnPayedMoney;
                    eC_OrderBasics.Add(_orderBasicmode);
                }
                this.txtReceiveMoney.Value = String.Format("{0:F}", shoukuanmoney);
                //rptOrderInfo.DataSource = eC_OrderBasics;
                //rptOrderInfo.DataBind();
            }
            catch (Exception ex)
            {
                PAGEHandleException(ex);
                JavaScript.MessageBox("订单不存在!", this);
            }
 
 
        }
 
        /// <summary>
        /// 收款到账户
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="buyerName"></param>
        /// <returns></returns>
        private bool ToAccount(int orderId, string buyerName,decimal UnPayedMoney)
        {
            OA_FirmAccountRecord oA_FirmAccountRecord = new Model.OA_FirmAccountRecord();
            oA_FirmAccountRecord.Money = UnPayedMoney;
            oA_FirmAccountRecord.LastUpdateTime = MyConvert.ConvertToDateTime(txtReceiveDate.Value);
            if (!oA_FirmAccountRecord.Money.HasValue || 0 > oA_FirmAccountRecord.Money.Value)
            {
                return false;
            }
            if (!oA_FirmAccountRecord.LastUpdateTime.HasValue)
            {
                return false;
            }
 
            oA_FirmAccountRecord.Operator = CurrentUser.ShortName;
            oA_FirmAccountRecord.Remark = txtRemark.Value;
            oA_FirmAccountRecord.PaymentUnit = buyerName;
            oA_FirmAccountRecord.SubjectId = MyConvert.ConvertToInt32(selSubjects.Value);
            oA_FirmAccountRecord.AccountId = MyConvert.ConvertToInt32("0".Equals(Request["rdoReceiveWay"]) ? "0" : Request["selAccounts"]);
            if (!oA_FirmAccountRecord.AccountId.HasValue || 0 == oA_FirmAccountRecord.AccountId.Value)
                return false;
            return _eC_OrderBasicBLL.ReceiveMoneyToAccount(orderId, oA_FirmAccountRecord);
        }
 
        /// <summary>
        /// 使用预付款(付款、收款)
        /// </summary>
        /// <returns></returns>
        private bool UsePrepare(EC_OrderBasic _orderBasic)
        {
            decimal? money = _orderBasic.UnPayedMoney;
            int operatTypeId = 3;
            string subject = "订单付款";
            switch (Request["outsourceType"])
            {
                case "1":
                    operatTypeId = 2;
                    subject = "外协付款";
                    break;
                default:
                    break;
            }
            return _eC_OrderBasicBLL.ReceiveMoney(_orderBasic.SellerId, _orderBasic.BuyerId, _orderBasic.Keyid.Value, money.Value, operatTypeId, subject, CurrentUser.ShortName);
        }
    }
}