7b1c11402333d05510b1611c50034e73fced4b78.svn-base 18 KB

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