89584bd0fd9626c221fe40053c0cdd9f285a2fa5.svn-base 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. 
  2. BemsWebApplication.BudgetPlan2 = function (params, viewInfo) {
  3. "use strict";
  4. var eq = BWA.DataUtil.constructEqualFilter,
  5. and = BWA.DataUtil.andFilter;
  6. var initialized = false;
  7. var isNewBudget = ko.observable(false);
  8. var currentYear = (new Date()).getFullYear();
  9. var modifiableBudgetPlanDataGridOptions = undefined;
  10. var budgetsInView = ko.observableArray(),
  11. budgets = ko.observableArray()
  12. ;
  13. var selectedBudgetYearName = ko.observable();
  14. var budgetGridView = undefined;
  15. var disabledButtons = [ko.observable(), ko.observable(), ko.observable(), ko.observable()];
  16. var budgetDataSource = BemsWebApplication.db.createDataSource('FmsBudget', true, false, true);
  17. var budgetCodeDataSourceForTree = BWA.DataUtil.createDataSource({
  18. dataSourceOptions: {
  19. paginate: false,
  20. store: BemsWebApplication.odata.FmsBudgetCodeClass,
  21. select: ['SiteId', 'BudgetSeq', 'BudgetClassId',
  22. 'RootBudgetClassId', 'ParentBudgetClassId',
  23. 'RootBudgetName', 'ParentBudgetName',
  24. 'Depth', 'Name', 'Abbreviation', 'IsUse', 'YearlyBudget', 'MonthlyBudget'],
  25. }
  26. });
  27. var newBudgetCodeDataSource = BWA.DataUtil.createDataSource({
  28. dataSourceOptions: {
  29. paginate: false,
  30. store: BemsWebApplication.odata.FmsBudgetCodeClass,
  31. select: ['SiteId', 'BudgetSeq', 'BudgetClassId',
  32. 'RootBudgetClassId', 'ParentBudgetClassId',
  33. 'RootBudgetName','ParentBudgetName',
  34. 'Depth', 'Name', 'Abbreviation', 'IsUse', 'YearlyBudget', 'MonthlyBudget'],
  35. }
  36. } );
  37. var existingBudgetCodeDataSource = BWA.DataUtil.createDataSource({
  38. dataSourceOptions: {
  39. paginate: false,
  40. store: BemsWebApplication.odata.FmsBudgetDetail,
  41. select: ['SiteId', 'Year', 'BudgetSeq', 'BudgetClassId',
  42. 'RootBudgetClassId', 'ParentBudgetClassId',
  43. 'RootBudgetName', 'ParentBudgetName',
  44. 'Depth', 'Name', 'Abbreviation', 'IsUse', 'YearlyBudget', 'MonthlyBudget'], //, 'MonthlyExecution'
  45. }
  46. });
  47. // }, 'FmsBudgetDetail');
  48. modifiableBudgetPlanDataGridOptions = utils.datagrid.defaultOptions({
  49. pager: {
  50. showPageSizeSelector: false,
  51. allowedPageSizes: []
  52. },
  53. //groupPanel: {
  54. // visible: true
  55. //},
  56. dataSource: budgetsInView,
  57. columns: [
  58. // { dataField: 'BudgetSeq', caption: 'Seq', width: '15%', alignment: 'left'}, , //sortOrder: 'asc'
  59. //{ dataField: 'BudgetClassId', caption: 'BudgetClassId', width: '10%', alignment: 'center' },
  60. // { dataField: 'RootBudgetClassId', caption: 'RootBudgetClassId', width: '10%', alignment: 'center' },
  61. { dataField: 'RootBudgetName', caption: '대분류', alignment: 'left', groupIndex: 0 },
  62. { dataField: 'ParentBudgetName', caption: '중분류', alignment: 'left', groupIndex: 1 },
  63. // { dataField: 'ParentBudgetClassId', caption: 'ParentBudgetClassId', width: '10%', alignment: 'center'},
  64. { dataField: 'Name', caption: '명칭', alignment: 'left' },
  65. {
  66. dataField: 'YearlyBudget',
  67. caption: '연간예산', alignment: 'center'
  68. , customizeText: function (cellInfo) {
  69. return 0;
  70. }
  71. , cellTemplate: utils.datagrid.getNumberBoxDataGridTemplateFunc({
  72. min: 0,
  73. max: Number.MAX_SAFE_INTEGER,
  74. dataField: 'YearlyBudget'
  75. , convertValueWhenValueChanged: function (data, value) {
  76. var monthlyValue = value / 12;
  77. data.MonthlyBudget(Number(monthlyValue.toFixed(1)));
  78. reCalculateSummaryValue();
  79. return Math.max(0, value);
  80. }
  81. })
  82. },
  83. {
  84. dataField: 'MonthlyBudget', caption: '월간예산', alignment: 'center'
  85. , cellTemplate: utils.datagrid.getNumberBoxDataGridTemplateFunc({
  86. min: 0,
  87. max: Number.MAX_SAFE_INTEGER,
  88. dataField: 'MonthlyBudget'
  89. , convertValueWhenValueChanged: function (data, value) {
  90. var yearlyValue = value * 12;
  91. data.YearlyBudget(Number(yearlyValue.toFixed(1)));
  92. reCalculateSummaryValue();
  93. return Math.max(0, value);
  94. }
  95. })
  96. },
  97. ],
  98. sortByGroupSummaryInfo: [{
  99. summaryItem: 'count'
  100. }],
  101. summary: {
  102. groupItems: [
  103. {
  104. column: 'YearlyBudget',
  105. summaryType: 'sum',
  106. valueFormat: "currency",
  107. showInGroupFooter: false,
  108. alignByColumn: true
  109. },
  110. {
  111. column: 'MonthlyBudget',
  112. summaryType: 'sum',
  113. valueFormat: "currency",
  114. showInGroupFooter: false,
  115. alignByColumn: true
  116. }
  117. ]
  118. },
  119. cellClick: function (e) {
  120. if (e.rowType == 'data' && e.data.Depth() === 2) {
  121. if (e.column.dataField === 'YearlyBudget' || e.column.dataField === 'MonthlyBudget') {
  122. utils.datagrid.cellClickEventForDataGridTemplate(e, e.column.dataField);
  123. }
  124. }
  125. },
  126. rowPrepared: function (rowElement, rowInfo) {
  127. if (rowInfo.rowType == 'data') {
  128. var data = rowInfo.data;
  129. if (data.Depth() === 0) {
  130. var color = 'rgba(100,100,100,0.2)';
  131. rowElement.css('background-color', color);
  132. }
  133. else if ( data.Depth() === 1) {
  134. var color = 'rgba(150,150,150,0.2)';
  135. rowElement.css('background-color', color);
  136. }
  137. else if ( data.Depth() === 2) {
  138. }
  139. }
  140. },
  141. cellPrepared: function (cellElement, cellInfo) {
  142. if (cellInfo.rowType == 'data') {
  143. switch (cellInfo.column.dataField) {
  144. case 'Name':
  145. //if (cellInfo.data.Depth() === 1) {
  146. // cellElement.css('padding-left', '30px');
  147. //}
  148. //else
  149. if (cellInfo.data.Depth() === 2) {
  150. cellElement.css('padding-left', '40px');
  151. }
  152. break;
  153. }
  154. }
  155. },
  156. contentReadyAction: function (e) {
  157. budgetGridView = e.component;
  158. },
  159. });
  160. var viewModel,
  161. popup = null,
  162. virtualFacilityPopup = null,
  163. selectedTreeData = ko.observable(null),
  164. selectedTreeItem = ko.observable(null);
  165. var commandButtonOptions = [{
  166. icon: 'add',
  167. id: 'create1',
  168. title: '신규',
  169. //disabled: disabledButtons[BID_INSERT_VIRTUAL_FACILITY],
  170. action: newBudget
  171. },
  172. {
  173. icon: 'add',
  174. id: 'create3',
  175. title: '불러오기',
  176. //disabled: disabledButtons[BID_GENERATE_POINT],
  177. //action: generatePoints
  178. action: getBudgetYear
  179. },
  180. {
  181. icon: 'edit',
  182. id: 'create2',
  183. title: '저장',
  184. //disabled: disabledButtons[BID_EDIT_VIRTUAL_FACILITY],
  185. action: saveBudget
  186. }, {
  187. id: 'create4',
  188. title: '초기화',
  189. //disabled: disabledButtons[BID_GENERATE_POINT],
  190. //action: generatePoints
  191. action: resetBudget
  192. },
  193. {
  194. icon: 'remove',
  195. id: 'delete',
  196. title: '삭제',
  197. //disabled: disabledButtons[BID_DELETE_POINT],
  198. //action: handleRemoveSelectedPoints
  199. action: deleteBudget
  200. }];
  201. function handleClickTreeItem(element, data) {
  202. selectedTreeItem(data);
  203. var filter;
  204. var depth = data.depth;
  205. var parentData = data.parentData;
  206. data = data.data;
  207. selectedTreeData(data);
  208. if (depth == 1) {
  209. filter = [
  210. ['SiteId', '=', BWA.UserInfo.SiteId()],
  211. 'and',
  212. ['Depth', '=', 0]
  213. ];
  214. }
  215. else if (depth === 2) {
  216. // FirstCodeClass 에 해당하는 모든 항목
  217. filter = [
  218. ['SiteId', '=', BWA.UserInfo.SiteId()],
  219. 'and',
  220. ['BudgetSeq', 'startswith', data.BudgetSeq()]
  221. ];
  222. }
  223. else if (depth === 3) {
  224. // SecondCodeClass 에 해당하는 모든 항목
  225. filter = [
  226. ['SiteId', '=', BWA.UserInfo.SiteId()],
  227. 'and',
  228. [
  229. ['BudgetClassId', '=', data.ParentBudgetClassId()],
  230. 'or',
  231. ['BudgetClassId', '=', data.BudgetClassId()],
  232. 'or',
  233. ['ParentBudgetClassId', '=', data.BudgetClassId()]
  234. ]
  235. ];
  236. }
  237. budgetGridView.filter(filter);
  238. }
  239. function handleViewShown() {
  240. //setTreeview();
  241. if (initialized == false) {
  242. selectedBudgetYearName('대상 항목');
  243. viewModel.popup.onSelectedBudgets(null);
  244. }
  245. initialized = true;
  246. }
  247. viewModel = BWA.CommonView.create(params, viewInfo, null, null, handleViewShown);
  248. viewModel.modifiableBudgetPlanDataGridOptions = modifiableBudgetPlanDataGridOptions;
  249. viewModel.commandButtonOptions = commandButtonOptions;
  250. viewModel.budgets = budgets;
  251. viewModel.selectedBudgetYearName = selectedBudgetYearName;
  252. popup = viewModel.popup = BWA.Popup.BudgetPlan2.create(viewModel, {
  253. onSelectedBudgets: function (selectedBudgets) {
  254. var now = new Date();
  255. if (selectedBudgets == null)
  256. existingBudgetCodeDataSource.filter([
  257. eq('SiteId', BWA.UserInfo.SiteId()),
  258. and,
  259. eq('Year', now.getFullYear()),
  260. and,
  261. eq('Depth', 2),
  262. ]);
  263. else
  264. existingBudgetCodeDataSource.filter([
  265. eq('SiteId', BWA.UserInfo.SiteId()),
  266. and,
  267. eq('Year', selectedBudgets.Year()),
  268. and,
  269. eq('Depth', 2),
  270. ]);
  271. $.when(existingBudgetCodeDataSource.load()).done(function (budgetCodeResult) {
  272. budgetsInView(budgetCodeResult);
  273. });
  274. isNewBudget(false);
  275. if (selectedBudgets == null)
  276. currentYear = now.getFullYear();
  277. else
  278. currentYear = selectedBudgets.Year();
  279. selectedBudgetYearName(currentYear + ' 년도 대상 항목');
  280. }
  281. });
  282. function reCalculateSummaryValue() {
  283. _.each(budgetsInView(), function (x) {
  284. if (x.Depth() === 0) {
  285. var yearlySum = 0;
  286. var monthlySum = 0;
  287. _.each(budgetsInView(), function (y) {
  288. if (y.Depth() === 2 && y.BudgetSeq().indexOf(x.BudgetSeq()) == 0) {
  289. yearlySum += Number(y.YearlyBudget());
  290. monthlySum += Number(y.MonthlyBudget());
  291. }
  292. });
  293. x.YearlyBudget(yearlySum);
  294. x.MonthlyBudget(monthlySum);
  295. }
  296. else if (x.Depth() === 1) {
  297. var yearlySum = 0;
  298. var monthlySum = 0;
  299. _.each(budgetsInView(), function (y) {
  300. if (y.Depth() === 2 && y.BudgetSeq().indexOf(x.BudgetSeq()) == 0) {
  301. yearlySum += Number(y.YearlyBudget());
  302. monthlySum += Number(y.MonthlyBudget());
  303. }
  304. });
  305. x.YearlyBudget(yearlySum);
  306. x.MonthlyBudget(monthlySum);
  307. }
  308. });
  309. budgetGridView.refresh();
  310. }
  311. function newBudget() {
  312. isNewBudget(true);
  313. newBudgetCodeDataSource.filter([
  314. eq('SiteId', BWA.UserInfo.SiteId()),
  315. and,
  316. eq('Depth', 2),
  317. ]);
  318. $.when(
  319. newBudgetCodeDataSource.load(),
  320. budgetDataSource.load()
  321. ).done(function (budgetCodeResult, budgetResult) {
  322. budgetsInView(budgetCodeResult[0]);
  323. budgets(budgetResult[0]);
  324. // currentYear 설정 (최신년도 설정 으로...)
  325. if (_.isEmpty(budgets())) {
  326. currentYear = (new Date()).getFullYear();
  327. }
  328. else {
  329. var lastBudget = _.last(budgets());
  330. currentYear = lastBudget.Year() + 1;
  331. }
  332. selectedBudgetYearName(currentYear + ' 년도 대상 항목');
  333. });
  334. }
  335. function deleteBudget() {
  336. DevExpress.ui.dialog.confirm($G('deleteConfirmMsg'), "삭제").then(function (result) {
  337. if (result) {
  338. var array = budgetsInView();
  339. if (_.isEmpty(array)) {
  340. utils.toast.show('저장할 예산편성이 없습니다.', 'error');
  341. return;
  342. }
  343. var siteId = BWA.UserInfo.SiteId();
  344. var year = currentYear;
  345. var parameters = {
  346. SiteId: siteId,
  347. Year: year
  348. };
  349. BWA.api.post('FmsBudgetDetail/Delete', null, parameters).done(function () {
  350. utils.toast.show($G('successDatabaseDeleteMsg'));
  351. resetBudget();
  352. });
  353. }
  354. });
  355. }
  356. function getBudgetYear() {
  357. popup.show();
  358. }
  359. function saveBudget() {
  360. var array = budgetsInView();
  361. if (_.isEmpty(array)) {
  362. utils.toast.show('저장할 예산편성이 없습니다.', 'error');
  363. return;
  364. }
  365. var siteId = BWA.UserInfo.SiteId();
  366. var year = currentYear;
  367. var postData;
  368. var parameters = {
  369. SiteId: siteId,
  370. Year: year
  371. };
  372. postData = _.map(array, function (f) {
  373. return {
  374. SiteId: siteId,
  375. Year: year,
  376. BudgetClassId: f.BudgetClassId(),
  377. BudgetSeq: f.BudgetSeq(),
  378. RootBudgetClassId: f.RootBudgetClassId(),
  379. ParentBudgetClassId: f.ParentBudgetClassId(),
  380. Depth: f.Depth(),
  381. Name: f.Name(),
  382. Abbreviation: f.Abbreviation(),
  383. IsUse: f.IsUse(),
  384. YearlyBudget: f.YearlyBudget(),
  385. MonthlyBudget: f.MonthlyBudget(),
  386. //MonthlyExecution: 0,
  387. //YearlyExecution: 0,
  388. };
  389. });
  390. if (isNewBudget()) {
  391. BWA.api.post('FmsBudgetDetail/Save', postData, parameters).done(function () {
  392. utils.toast.show($G('successDatabaseProcessMsg'));
  393. isNewBudget(false);
  394. });
  395. }
  396. else {
  397. BWA.api.post('FmsBudgetDetail/Update', postData, parameters).done(function () {
  398. utils.toast.show($G('successDatabaseProcessMsg'));
  399. });
  400. }
  401. }
  402. function resetBudget() {
  403. isNewBudget(false);
  404. budgetsInView([]);
  405. budgets([]);
  406. budgetGridView.refresh();
  407. selectedBudgetYearName('대상 항목');
  408. //clearTreeview();
  409. }
  410. function clearTreeview() {
  411. $('#facilityTreeView').cwTreeView({
  412. width: '30%',
  413. height: 'auto',
  414. onClickTreeItem: handleClickTreeItem,
  415. delegateDataSource: getDataSourceDelegateForTreeView(budgetCodeDataSourceForTree)
  416. });
  417. }
  418. function setTreeview() {
  419. $('#facilityTreeView').cwTreeView({
  420. width: '30%',
  421. height: 'auto',
  422. onClickTreeItem: handleClickTreeItem,
  423. delegateDataSource: getDataSourceDelegateForTreeView(budgetCodeDataSourceForTree)
  424. });
  425. }
  426. function getDataSourceDelegateForTreeView(dataSource) {
  427. return function (data, alterObj) {
  428. var id = data.id;
  429. var depth = data.depth;
  430. var promise = null;
  431. switch (depth) {
  432. case 0:
  433. var typeDeferred = new $.Deferred();
  434. promise = typeDeferred.promise();
  435. alterObj.IsFunction = false;
  436. typeDeferred.resolve([{
  437. Id: 0,
  438. Name: '관리항목'
  439. }]);
  440. //rootTreeData(data);
  441. break;
  442. case 1:
  443. dataSource.filter([
  444. eq('SiteId', BWA.UserInfo.SiteId()),
  445. 'and',
  446. eq('Depth', 0)
  447. ]);
  448. promise = dataSource.load();
  449. break;
  450. case 2:
  451. dataSource.filter([
  452. eq('SiteId', BWA.UserInfo.SiteId()),
  453. 'and',
  454. eq('ParentBudgetClassId', data.data.BudgetClassId()),
  455. 'and',
  456. eq('Depth', 1)
  457. ]);
  458. promise = dataSource.load();
  459. break;
  460. }
  461. return promise;
  462. };
  463. }
  464. return viewModel;
  465. };