F_MST_0001.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:F_MST_0001.cs
  5. * 2.功能描述:帐套维护页面
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 张国印 2014/08/27 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Data;
  12. using System.Reflection;
  13. using System.Windows.Forms;
  14. using Dongke.IBOSS.PRD.Basics.BaseControls;
  15. using Dongke.IBOSS.PRD.Basics.BaseResources;
  16. using Dongke.IBOSS.PRD.Basics.Library;
  17. using Dongke.IBOSS.PRD.Client.CommonModule;
  18. using Dongke.IBOSS.PRD.Client.DataModels;
  19. using Dongke.IBOSS.PRD.WCF.Proxys;
  20. namespace Dongke.IBOSS.PRD.Client.SystemModule
  21. {
  22. /// <summary>
  23. /// 帐套维护页面
  24. /// </summary>
  25. public partial class F_MST_0001 : FormBase
  26. {
  27. #region 成员变量
  28. // 窗体的单例模式
  29. private static F_MST_0001 _instance;
  30. // 帐套数据源
  31. private DataTable _dtSourse;
  32. #endregion
  33. #region 构造函数
  34. /// <summary>
  35. /// 构造函数
  36. /// </summary>
  37. public F_MST_0001()
  38. {
  39. InitializeComponent();
  40. this.SetFromTitleInfo();
  41. }
  42. #endregion
  43. #region 事件
  44. /// <summary>
  45. /// 窗体关闭事件
  46. /// </summary>
  47. /// <param name="sender"></param>
  48. /// <param name="e"></param>
  49. private void F_MST_0001_FormClosed(object sender, FormClosedEventArgs e)
  50. {
  51. _instance = null;
  52. }
  53. /// <summary>
  54. /// 页面加载
  55. /// </summary>
  56. /// <param name="sender"></param>
  57. /// <param name="e"></param>
  58. private void F_MST_0001_Load(object sender, EventArgs e)
  59. {
  60. try
  61. {
  62. FormPermissionManager.FormPermissionControl(this.Name, this,
  63. LogInUserInfo.CurrentUser.CurrentUserEntity.UserRightData,
  64. LogInUserInfo.CurrentUser.CurrentUserEntity.FunctionData);
  65. // 设置“保存”按钮不可用
  66. this.btnSave.Enabled = false;
  67. // 设置表格不自动创建列
  68. this.dgvAccount.AutoGenerateColumns = false;
  69. // 窗体启动立即加载全部帐套信息
  70. this.GetAllAccount();
  71. this.RefreshDataGridViewData();
  72. this.SetAccountCodeEnable();
  73. this.dgvAccount.IsSetInputColumnsColor = true;
  74. this.dgvAccount.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
  75. }
  76. catch (Exception ex)
  77. {
  78. // 对异常进行共通处理
  79. ExceptionManager.HandleEventException(this.ToString(),
  80. MethodBase.GetCurrentMethod().Name, this.Text, ex);
  81. }
  82. }
  83. /// <summary>
  84. /// 新增一行时,给有效属性设置选中状态
  85. /// </summary>
  86. /// <param name="sender"></param>
  87. /// <param name="e"></param>
  88. private void dgvAccount_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
  89. {
  90. e.Row.Cells["ValueFlag"].Value = 1;
  91. }
  92. /// <summary>
  93. /// 行校验,帐套代码、帐套名称不能为空
  94. /// </summary>
  95. /// <param name="sender"></param>
  96. /// <param name="e"></param>
  97. private void dgvAccount_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
  98. {
  99. try
  100. {
  101. // 按Esc时不进行校验
  102. if (!this.dgvAccount.IsCurrentRowDirty)
  103. {
  104. this.dgvAccount.IsSetInputColumnsColor = true;
  105. return;
  106. }
  107. if (this.ActiveControl != null && "btnCancel".Equals(this.ActiveControl.Name))
  108. {
  109. // 清除单元格的错误消息
  110. this.dgvAccount.CurrentRow.ErrorText = string.Empty;
  111. return;
  112. }
  113. DataGridViewRow row = this.dgvAccount.CurrentRow;
  114. if (!row.IsNewRow)
  115. {
  116. // 帐套代码不能为空
  117. if (row.Cells["AccountCode"].Value == null
  118. || string.IsNullOrEmpty(row.Cells["AccountCode"].Value.ToString().Trim()))
  119. {
  120. // 增加单元格的错误消息
  121. this.dgvAccount.CurrentRow.ErrorText =
  122. string.Format(Messages.MSG_CMN_W005, "帐套编码");
  123. e.Cancel = true;
  124. return;
  125. }
  126. // 帐套名称不能为空
  127. if (row.Cells["AccountName"].Value == null
  128. || string.IsNullOrEmpty(row.Cells["AccountName"].Value.ToString().Trim()))
  129. {
  130. // 增加单元格的错误消息
  131. this.dgvAccount.CurrentRow.ErrorText =
  132. string.Format(Messages.MSG_CMN_W005, "帐套名称");
  133. e.Cancel = true;
  134. return;
  135. }
  136. }
  137. // 清除单元格的错误消息
  138. this.dgvAccount.CurrentRow.ErrorText = string.Empty;
  139. }
  140. catch (Exception ex)
  141. {
  142. // 对异常进行共通处理
  143. ExceptionManager.HandleEventException(this.ToString(),
  144. MethodBase.GetCurrentMethod().Name, this.Text, ex);
  145. }
  146. }
  147. /// <summary>
  148. /// 输入验证,帐套代码不能重复输入
  149. /// </summary>
  150. /// <param name="sender"></param>
  151. /// <param name="e"></param>
  152. private void dgvAccount_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
  153. {
  154. try
  155. {
  156. if (this.ActiveControl != null && "btnCancel".Equals(this.ActiveControl.Name))
  157. {
  158. return;
  159. }
  160. if (!this.dgvAccount.CurrentRow.IsNewRow)
  161. {
  162. DataGridViewRow rowItem = this.dgvAccount.Rows[e.RowIndex];
  163. DataGridViewColumn columnItem = this.dgvAccount.Columns[e.ColumnIndex];
  164. object value = rowItem.Cells[columnItem.Name].EditedFormattedValue;
  165. // 帐套代码
  166. if ("AccountCode".Equals(columnItem.Name))
  167. {
  168. if (value != null || !string.IsNullOrEmpty(value.ToString().Trim()))
  169. {
  170. bool isUnique = Utility.IsUnique(value.ToString(),
  171. this.dgvAccount.CurrentRow.Index,
  172. this.dgvAccount, "AccountCode");
  173. if (!isUnique)
  174. {
  175. // 增加单元格的错误消息
  176. this.dgvAccount.CurrentRow.ErrorText =
  177. string.Format(Messages.MSG_CMN_W006, "帐套编码");
  178. e.Cancel = true;
  179. this.btnSave.Enabled = false;
  180. return;
  181. }
  182. }
  183. }
  184. // 清除单元格的错误消息
  185. this.dgvAccount.CurrentRow.ErrorText = string.Empty;
  186. }
  187. }
  188. catch (Exception ex)
  189. {
  190. // 对异常进行共通处理
  191. ExceptionManager.HandleEventException(this.ToString(),
  192. MethodBase.GetCurrentMethod().Name, this.Text, ex);
  193. }
  194. }
  195. /// <summary>
  196. /// 根据数据变化,设置保存按钮的状态
  197. /// </summary>
  198. /// <param name="sender"></param>
  199. /// <param name="e"></param>
  200. private void dgvAccount_RowValidated(object sender, DataGridViewCellEventArgs e)
  201. {
  202. try
  203. {
  204. this.RefreshSaveBtnStatus();
  205. }
  206. catch (Exception ex)
  207. {
  208. // 对异常进行共通处理
  209. ExceptionManager.HandleEventException(this.ToString(),
  210. MethodBase.GetCurrentMethod().Name, this.Text, ex);
  211. }
  212. }
  213. /// <summary>
  214. /// 根据数据有没有改变,设置"保存"按钮是否可用
  215. /// </summary>
  216. /// <param name="sender"></param>
  217. /// <param name="e"></param>
  218. private void dgvAccount_CellValidated(object sender, DataGridViewCellEventArgs e)
  219. {
  220. try
  221. {
  222. this.RefreshSaveBtnStatus();
  223. }
  224. catch (Exception ex)
  225. {
  226. // 对异常进行共通处理
  227. ExceptionManager.HandleEventException(this.ToString(),
  228. MethodBase.GetCurrentMethod().Name, this.Text, ex);
  229. }
  230. }
  231. /// <summary>
  232. /// 关闭窗体
  233. /// </summary>
  234. /// <param name="sender"></param>
  235. /// <param name="e"></param>
  236. private void btnCancel_Click(object sender, EventArgs e)
  237. {
  238. this.Close();
  239. }
  240. /// <summary>
  241. /// 窗体关闭
  242. /// </summary>
  243. /// <param name="sender"></param>
  244. /// <param name="e"></param>
  245. private void F_MST_0001_FormClosing(object sender, FormClosingEventArgs e)
  246. {
  247. try
  248. {
  249. // 判断数据是否有变化
  250. if (DataJudge.IsChange((DataTable)this.dgvAccount.DataSource))
  251. {
  252. // 判断单元格是否有错误提示
  253. if (string.IsNullOrEmpty(this.dgvAccount.CurrentRow.ErrorText))
  254. {
  255. DialogResult dialogResult = MessageBox.Show(Messages.MSG_CMN_Q001,
  256. this.Text, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
  257. // 保存数据
  258. if (dialogResult == DialogResult.Yes)
  259. {
  260. this.SaveAccountInfo();
  261. }
  262. // 点击取消,不关闭窗体
  263. else if (dialogResult == DialogResult.Cancel)
  264. {
  265. e.Cancel = true;
  266. return;
  267. }
  268. }
  269. }
  270. }
  271. catch (Exception ex)
  272. {
  273. // 对异常进行共通处理
  274. ExceptionManager.HandleEventException(this.ToString(),
  275. MethodBase.GetCurrentMethod().Name, this.Text, ex);
  276. }
  277. }
  278. /// <summary>
  279. /// 保存按钮
  280. /// </summary>
  281. /// <param name="sender"></param>
  282. /// <param name="e"></param>
  283. private void btnSave_Click(object sender, EventArgs e)
  284. {
  285. try
  286. {
  287. // 判断数据是否有变化
  288. if (DataJudge.IsChange((DataTable)this.dgvAccount.DataSource))
  289. {
  290. // 判断单元格是否有错误提示
  291. if (string.IsNullOrEmpty(this.dgvAccount.CurrentRow.ErrorText))
  292. {
  293. this.SaveAccountInfo();
  294. this.SetAccountCodeEnable();
  295. }
  296. else
  297. {
  298. MessageBox.Show(string.Format(Messages.MSG_CMN_W007, "列表中数据存在错误。"),
  299. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  300. }
  301. }
  302. else
  303. {
  304. MessageBox.Show(string.Format(Messages.MSG_CMN_W007, "列表中数据没有变化。"),
  305. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  306. }
  307. }
  308. catch (Exception ex)
  309. {
  310. // 对异常进行共通处理
  311. ExceptionManager.HandleEventException(this.ToString(),
  312. MethodBase.GetCurrentMethod().Name, this.Text, ex);
  313. }
  314. }
  315. /// <summary>
  316. /// 是否停用被选中后 刷新页面
  317. /// </summary>
  318. /// <param name="sender"></param>
  319. /// <param name="e"></param>
  320. private void chkDisplayDisabledData_CheckedChanged(object sender, EventArgs e)
  321. {
  322. try
  323. {
  324. this.RefreshDataGridViewData();
  325. this.SetAccountCodeEnable();
  326. this.dgvAccount.IsSetInputColumnsColor = true;
  327. this.btnSave.Enabled = true;
  328. }
  329. catch (Exception ex)
  330. {
  331. // 对异常进行共通处理
  332. ExceptionManager.HandleEventException(this.ToString(),
  333. MethodBase.GetCurrentMethod().Name, this.Text, ex);
  334. }
  335. }
  336. /// <summary>
  337. /// 排序后维持表格状态
  338. /// </summary>
  339. /// <param name="sender"></param>
  340. /// <param name="e"></param>
  341. private void dgvAccount_Sorted(object sender, EventArgs e)
  342. {
  343. this.SetAccountCodeEnable();
  344. this.dgvAccount.IsSetInputColumnsColor = true;
  345. }
  346. /// <summary>
  347. /// 添加行事件
  348. /// </summary>
  349. /// <param name="sender"></param>
  350. /// <param name="e"></param>
  351. private void dgvAccount_UserAddedRow(object sender, DataGridViewRowEventArgs e)
  352. {
  353. this.dgvAccount.IsSetInputColumnsColor = true;
  354. }
  355. #endregion
  356. #region 私有方法
  357. /// <summary>
  358. /// 设置窗体按钮的文本信息
  359. /// </summary>
  360. private void SetFromTitleInfo()
  361. {
  362. // 窗口标题
  363. this.Text = FormTitles.F_MST_0001;
  364. // 设置按钮文本
  365. this.btnCancel.Text = ButtonText.BTN_CLOSE;
  366. this.btnSave.Text = ButtonText.BTN_SAVE;
  367. }
  368. /// <summary>
  369. /// 得到帐套信息
  370. /// </summary>
  371. /// <returns></returns>
  372. private void RefreshDataGridViewData()
  373. {
  374. try
  375. {
  376. if (this.chkDisplayDisabledData.Checked == false)
  377. {
  378. _dtSourse.DefaultView.RowFilter = "ValueFlag=1";
  379. }
  380. else
  381. {
  382. _dtSourse.DefaultView.RowFilter = null;
  383. }
  384. this.dgvAccount.DataSource = _dtSourse;
  385. this.btnSave.Enabled = false;
  386. }
  387. catch (Exception ex)
  388. {
  389. throw ex;
  390. }
  391. }
  392. /// <summary>
  393. /// 将已经存在的帐套代码单元格置成只读
  394. /// </summary>
  395. private void SetAccountCodeEnable()
  396. {
  397. for (int i = 0; i < this.dgvAccount.Rows.Count; i++)
  398. {
  399. if (!this.dgvAccount.Rows[i].IsNewRow)
  400. {
  401. if (string.IsNullOrEmpty(this.dgvAccount.Rows[i].Cells["AccountID"].Value.ToString()))
  402. {
  403. this.dgvAccount.Rows[i].Cells["AccountCode"].ReadOnly = false;
  404. }
  405. else
  406. {
  407. this.dgvAccount.Rows[i].Cells["AccountCode"].ReadOnly = true;
  408. }
  409. }
  410. }
  411. }
  412. /// <summary>
  413. /// 获取到帐套信息
  414. /// </summary>
  415. /// <returns></returns>
  416. private DataSet GetAccountInfo()
  417. {
  418. try
  419. {
  420. //获取是否停用选项的值
  421. bool valueFlag = chkDisplayDisabledData.Checked;
  422. DataSet dsResultAccount = (DataSet)DoAsync(new BaseAsyncMethod(() =>
  423. {
  424. return SystemModuleProxy.Service.GetAccountInfo(valueFlag);
  425. }));
  426. return dsResultAccount;
  427. }
  428. catch (Exception ex)
  429. {
  430. throw ex;
  431. }
  432. }
  433. /// <summary>
  434. /// 设置保存按钮的可用状态
  435. /// </summary>
  436. private void RefreshSaveBtnStatus()
  437. {
  438. if (DataJudge.IsChange((DataTable)this.dgvAccount.DataSource))
  439. {
  440. this.btnSave.Enabled = true;
  441. }
  442. else
  443. {
  444. this.btnSave.Enabled = false;
  445. }
  446. }
  447. /// <summary>
  448. /// 保存数据
  449. /// </summary>
  450. private void SaveAccountInfo()
  451. {
  452. bool bolIsValidCheck = this.IsValidCheck();
  453. if (!bolIsValidCheck)
  454. {
  455. return;
  456. }
  457. int resultState = this.SaveAccountData();
  458. // 存在相同的帐套代码 1
  459. if (Constant.INT_IS_ONE == resultState)
  460. {
  461. MessageBox.Show(string.Format(Messages.MSG_CMN_W006, "帐套编码"),
  462. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  463. return;
  464. }
  465. // 保存成功 2表示有数据被修改 3表示没有数据被修改
  466. if (Constant.INT_IS_TWO == resultState)
  467. {
  468. this.GetAllAccount();
  469. this.btnSave.Enabled = false;
  470. MessageBox.Show(string.Format(Messages.MSG_CMN_I001, "帐套管理", "保存"),
  471. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  472. }
  473. else
  474. {
  475. MessageBox.Show(string.Format(Messages.MSG_CMN_W001, "帐套管理", "保存"),
  476. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  477. return;
  478. }
  479. this.RefreshDataGridViewData();
  480. this.SetAccountCodeEnable();
  481. this.dgvAccount.IsSetInputColumnsColor = true;
  482. }
  483. /// <summary>
  484. /// 调用服务保存帐套数据
  485. /// </summary>
  486. /// <returns></returns>
  487. private int SaveAccountData()
  488. {
  489. // 取得系统默认密码
  490. string defaultPassword = Constant.S_PASSWORD_DEFAULT; ;
  491. DataSet dtSetting = CommonModuleProxy.Service.GetSysSettingBySettingType("S_CMN_0001");
  492. if (dtSetting != null)
  493. {
  494. if (dtSetting.Tables.Count > Constant.INT_IS_ZERO)
  495. {
  496. if (dtSetting.Tables[0].Rows.Count > Constant.INT_IS_ZERO)
  497. {
  498. defaultPassword = dtSetting.Tables[0].Rows[0]["SETTINGVALUE"].ToString();
  499. }
  500. }
  501. }
  502. defaultPassword = Encryption.GetMD5String(defaultPassword);
  503. DataTable AccountData = (DataTable)this.dgvAccount.DataSource;
  504. int result = (int)DoAsync(new BaseAsyncMethod(() =>
  505. {
  506. return SystemModuleProxy.Service.SaveAccountData(AccountData, defaultPassword);
  507. }));
  508. return result;
  509. }
  510. /// <summary>
  511. /// 验证是否可以进行帐套保存 True为可以保存,False为不可保存
  512. /// </summary>
  513. /// <returns>True为验证通过,False为验证不通过</returns>
  514. private bool IsValidCheck()
  515. {
  516. // 初始帐套不能设置成无效校验
  517. bool isDefaultAccountDisable = isDefaultAccountToDisable();
  518. if (isDefaultAccountDisable)
  519. {
  520. MessageBox.Show(string.Format(Messages.MSG_CMN_W007, "系统默认的帐套不能置成无效状态"),
  521. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  522. return false;
  523. }
  524. // 帐套数不能超过授权的帐套数
  525. bool isExceedLicenseAccountNumber = this.IsExceedLicenseAccountNumber();
  526. if (isExceedLicenseAccountNumber)
  527. {
  528. MessageBox.Show(string.Format(Messages.MSG_CMN_W007, "所创建的有效帐套数超出了授权许可的帐套数"),
  529. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  530. return false;
  531. }
  532. return true;
  533. }
  534. /// <summary>
  535. /// 判断是否将默认帐套设置成无效了
  536. /// </summary>
  537. /// <returns>
  538. /// true:将默认帐套设置成无效
  539. /// false:未将默认用户设置成无效
  540. /// </returns>
  541. private bool isDefaultAccountToDisable()
  542. {
  543. for (int i = 0; i < this.dgvAccount.Rows.Count; i++)
  544. {
  545. if (!this.dgvAccount.Rows[i].IsNewRow)
  546. {
  547. if (LogInUserInfo.CurrentUser.CurrentUserEntity.AccountCode.ToUpper() ==
  548. this.dgvAccount.Rows[i].Cells["AccountCode"].Value.ToString().ToUpper()
  549. && !Constant.ValueFlag.Effective.GetHashCode().ToString().Equals(
  550. this.dgvAccount.Rows[i].Cells["valueFlag"].Value.ToString()))
  551. {
  552. return true;
  553. }
  554. }
  555. }
  556. return false;
  557. }
  558. /// <summary>
  559. /// 判断当前的有效帐套数是否超过了授权帐套数
  560. /// </summary>
  561. /// <returns>
  562. /// true:超出
  563. /// false:未超出
  564. /// </returns>
  565. private bool IsExceedLicenseAccountNumber()
  566. {
  567. int accountNumber = 0;
  568. for (int i = 0; i < this.dgvAccount.Rows.Count; i++)
  569. {
  570. if (!this.dgvAccount.Rows[i].IsNewRow)
  571. {
  572. if (Constant.ValueFlag.Effective.GetHashCode().ToString().Equals(
  573. this.dgvAccount.Rows[i].Cells["valueFlag"].Value.ToString()))
  574. {
  575. accountNumber += 1;
  576. }
  577. }
  578. }
  579. int licenseCount = 999; //目前设置为最大值,正式使用后需要修改为0
  580. if (LogInUserInfo.CurrentUser.CurrentLicenseInfo != null)
  581. {
  582. if (LogInUserInfo.CurrentUser.CurrentLicenseInfo.Tables.Count > 0)
  583. {
  584. if (LogInUserInfo.CurrentUser.CurrentLicenseInfo.Tables.Contains("BaseInfoTable"))
  585. {
  586. licenseCount = (int)LogInUserInfo.CurrentUser.CurrentLicenseInfo.
  587. Tables["BaseInfoTable"].Rows[0]["AccountNumber"];
  588. }
  589. }
  590. }
  591. if (accountNumber > licenseCount)
  592. {
  593. return true;
  594. }
  595. return false;
  596. }
  597. /// <summary>
  598. /// 获取全部帐套信息
  599. /// </summary>
  600. private void GetAllAccount()
  601. {
  602. try
  603. {
  604. DataSet dsResultAccount = (DataSet)DoAsync(new BaseAsyncMethod(() =>
  605. {
  606. return SystemModuleProxy.Service.GetAllAccountInfo();
  607. }));
  608. this._dtSourse = dsResultAccount.Tables[0];
  609. }
  610. catch (Exception ex)
  611. {
  612. throw ex;
  613. }
  614. }
  615. #endregion
  616. }
  617. }