Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.17 KB | None | 0 0
  1. sap.ui.define([
  2. "sap/ui/core/mvc/Controller",
  3. "../model/formatter",
  4. "sap/m/MessageToast",
  5. "sap/ui/model/Filter"
  6. ], function(Controller, formatter, MessageToast, Filter) {
  7. "use strict";
  8.  
  9. return Controller.extend("ZPDT_PARAM.controller.Main", {
  10.  
  11. formatter: formatter,
  12.  
  13. onInit: function() {
  14. this.sProfil = null;
  15.  
  16. this.oFileReader = new FileReader();
  17. this.oFileReader.onload = this._readFile;
  18.  
  19. this.oView.setModel(new sap.ui.model.json.JSONModel({
  20. busyDelay: 0
  21. }), "paramAppli");
  22.  
  23. this.oView.setModel(new sap.ui.model.json.JSONModel({
  24. MarqueSet: [],
  25. VerrerieSet: []
  26. }), "transverseTrie");
  27.  
  28. this.oView.setModel(new sap.ui.model.json.JSONModel({
  29. items: []
  30. }), "article");
  31.  
  32. this.oView.setModel(new sap.ui.model.json.JSONModel({
  33. items: []
  34. }), "grpQualite");
  35.  
  36. this.oView.setModel(new sap.ui.model.json.JSONModel({
  37. items: []
  38. }), "magasins");
  39.  
  40. this.oView.setModel(new sap.ui.model.json.JSONModel({
  41. items: []
  42. }), "flaconnage");
  43.  
  44. this.oView.setModel(new sap.ui.model.json.JSONModel({
  45. items: []
  46. }), "groupementQualiteFlaconnage");
  47.  
  48. this.oView.setModel(new sap.ui.model.json.JSONModel({
  49. items: []
  50. }), "groupeQualite");
  51.  
  52. this.oView.setModel(new sap.ui.model.json.JSONModel({
  53. valeur: 0
  54. }), "seuil");
  55. },
  56.  
  57. onBeforeRendering: function() {
  58. this.oView.getModel("transverse").read("/ProfilSet('parametrage')", {
  59. success: function(oDataProfil) {
  60. this.sProfil = oDataProfil.profil;
  61. //On défini les listes en fonction du profil
  62. //La table des articles et lots exclu
  63. this._refreshOdata("articlesLotsSet");
  64.  
  65. //La table des groupes de qualités exclu
  66. this._refreshOdata("groupeQualiteSet");
  67.  
  68. //La table des magasins exclu
  69. this._refreshOdata("magasinSet");
  70.  
  71. //La table des flaconnages
  72. this._refreshOdata("flaconnageSet");
  73.  
  74. //La table des groupement de qualité/flaconnage
  75. this._refreshOdata("groupementQualiteFlaconnageSet");
  76.  
  77. //La table des groupes de qualité
  78. this._refreshOdata("groupeGroupementQualiteSet");
  79.  
  80. //Le champ de tvarvc pour le seuil mini
  81. this.oView.getModel().read("/seuilSet(profil='" + oDataProfil.profil + "')", {
  82. success: function(oDataSeuil) {
  83. var oData = new sap.ui.model.json.JSONModel({
  84. valeur: oDataSeuil.valeur
  85. });
  86. this.oView.setModel(oData, "seuil");
  87. }.bind(this),
  88. error: function(oError) {
  89.  
  90. }.bind(this)
  91. });
  92.  
  93. //On update les listes
  94. this.oView.getModel("transverse").read("/VerrerieSet", {
  95. filters: [new Filter("profil", sap.ui.model.FilterOperator.EQ, oDataProfil.profil)],
  96. success: function(oDataVerrerie) {
  97. this.oView.getModel("transverseTrie").setProperty("/VerrerieSet", oDataVerrerie.results);
  98. }.bind(this),
  99. error: function(oError) {
  100.  
  101. }.bind(this)
  102. });
  103. this.oView.getModel("transverse").read("/MarqueSet", {
  104. filters: [new Filter("profil", sap.ui.model.FilterOperator.EQ, oDataProfil.profil)],
  105. success: function(oDataMarques) {
  106. this.oView.getModel("transverseTrie").setProperty("/MarqueSet", oDataMarques.results);
  107. }.bind(this),
  108. error: function(oError) {
  109.  
  110. }.bind(this)
  111. });
  112. }.bind(this),
  113. error: function(oError) {
  114.  
  115. }.bind(this)
  116. });
  117. },
  118.  
  119. onChangeSeuil: function(oEvent) {
  120. if (oEvent.getParameter("value")) {
  121. var oDataInput = {
  122. profil: this.sProfil,
  123. valeur: oEvent.getParameter("value")
  124. };
  125. this.oView.getModel().create("/seuilSet", oDataInput, {
  126. success: function(oDataSeuil) {
  127. this.oView.byId("seuilGlobal").setValue();
  128. var oData = new sap.ui.model.json.JSONModel({
  129. valeur: oDataSeuil.valeur
  130. });
  131. this.oView.setModel(oData, "seuil");
  132. }.bind(this),
  133. error: function(oError) {}.bind(this)
  134. });
  135. }
  136. },
  137.  
  138. onChangeQuantite: function(oEvent) {
  139. var oData = this.oView.getModel("article").getData(),
  140. sContext = oEvent.getSource().getBindingInfo("value").binding.getContext().getPath(),
  141. lineIndex = sContext.substring(sContext.lastIndexOf("/") + 1),
  142. oValues = this._deleteUselessData(oData.items[lineIndex], "articlesLots", true);
  143.  
  144. oValues.quantiteIgnore = oValues.quantiteIgnore.toString();
  145.  
  146. this.oView.getModel().create("/articlesLotsSet", oValues, {
  147. success: function() {
  148. this._refreshOdata("articlesLotsSet");
  149. }.bind(this),
  150. error: function(oError) {}.bind(this)
  151. });
  152. },
  153.  
  154. onPressDelete: function(oEvent) {
  155. var oTable = oEvent.getSource().getParent().getParent(),
  156. aLines = oTable.getSelectedItems(),
  157. lineIndex = 0,
  158. oData = {},
  159. functionSuccess,
  160. functionError;
  161.  
  162. switch (oEvent.getParameter("id")) {
  163. case "__xmlview0--buttonDeleteLineArticle":
  164. oData = this.oView.getModel("article").getData();
  165. functionSuccess = function() {
  166. this._refreshOdata("articlesLotsSet");
  167. }.bind(this);
  168. break;
  169. case "__xmlview0--buttonDeleteLineQualite":
  170. oData = this.oView.getModel("grpQualite").getData();
  171. functionSuccess = function() {
  172. this._refreshOdata("groupeQualiteSet");
  173. }.bind(this);
  174. break;
  175. case "__xmlview0--buttonDeleteLineMagasin":
  176. oData = this.oView.getModel("magasins").getData();
  177. functionSuccess = function() {
  178. this._refreshOdata("magasinSet");
  179. }.bind(this);
  180. break;
  181. case "__xmlview0--buttonDeleteLineFlaconnage":
  182. oData = this.oView.getModel("flaconnage").getData();
  183. functionSuccess = function() {
  184. this._refreshOdata("flaconnageSet");
  185. }.bind(this);
  186. break;
  187. case "__xmlview0--buttonDeleteLineGroupementQualFlac":
  188. oData = this.oView.getModel("groupementQualiteFlaconnage").getData();
  189. functionSuccess = function() {
  190. this._refreshOdata("groupementQualiteFlaconnageSet");
  191. }.bind(this);
  192. break;
  193. case "__xmlview0--buttonDeleteLineGroupeQualite":
  194. oData = this.oView.getModel("groupeQualite").getData();
  195. functionSuccess = function() {
  196. this._refreshOdata("groupeGroupementQualiteSet");
  197. }.bind(this);
  198. break;
  199. default:
  200. }
  201.  
  202. functionError = function(oError) {
  203. sap.m.MessageToast.show("Le groupement flaconnage/qualité est utilisé dans la table de flaconnage/qualité. Suppression impossible", {
  204. duration: 2000 || 3000
  205. });
  206. }.bind(this);
  207.  
  208. //Pour chaque lignes on pousse la suppression à SAP
  209. for (var i = 0; i < aLines.length; i++) {
  210. lineIndex = aLines[i].getBindingContextPath().substring(aLines[i].getBindingContextPath().lastIndexOf("/") + 1);
  211.  
  212. //On mets à jour l'oData depuis SAP
  213. switch (oEvent.getParameter("id")) {
  214. case "__xmlview0--buttonDeleteLineArticle":
  215. this.oView.getModel().remove("/articlesLotsSet(profil='" + oData.items[lineIndex].profil + "',article='" + oData.items[lineIndex]
  216. .article + "',lot='" + oData.items[lineIndex].lot + "')", {
  217. success: functionSuccess,
  218. error: functionError
  219. });
  220. break;
  221. case "__xmlview0--buttonDeleteLineQualite":
  222. this.oView.getModel().remove("/groupeQualiteSet(profil='" + oData.items[lineIndex].profil + "',brand='" + oData.items[lineIndex]
  223. .brand + "',grpQualite='" + oData.items[lineIndex].grpQualite + "',action='EXCLUDE')", {
  224. success: functionSuccess,
  225. error: functionError
  226. });
  227. break;
  228. case "__xmlview0--buttonDeleteLineMagasin":
  229. this.oView.getModel().remove("/magasinSet(profil='" + oData.items[lineIndex].profil + "',mag='" + oData.items[lineIndex]
  230. .mag + "',action='EXCLUDE')", {
  231. success: functionSuccess,
  232. error: functionError
  233. });
  234. break;
  235. case "__xmlview0--buttonDeleteLineFlaconnage":
  236. this.oView.getModel().remove("/flaconnageSet(profil='" + oData.items[lineIndex].profil + "',flcnage='" + oData.items[lineIndex]
  237. .flcnage + "',specificiteVerrerie='" + oData.items[lineIndex].specificiteVerrerie + "',marque='" + oData.items[lineIndex].marque +
  238. "')", {
  239. success: functionSuccess,
  240. error: functionError
  241. });
  242. break;
  243. case "__xmlview0--buttonDeleteLineGroupementQualFlac":
  244. this.oView.getModel().remove("/groupementQualiteFlaconnageSet(profil='" + oData.items[lineIndex].profil +
  245. "',groupementQualite='" + oData.items[
  246. lineIndex]
  247. .groupementQualite + "',groupementFlaconnage='" + oData.items[lineIndex].groupementFlaconnage + "')", {
  248. success: functionSuccess,
  249. error: functionError
  250. });
  251. break;
  252. case "__xmlview0--buttonDeleteLineGroupeQualite":
  253. this.oView.getModel().remove("/groupeGroupementQualiteSet(profil='" + oData.items[lineIndex].profil + "',groupeQualite='" +
  254. oData.items[
  255. lineIndex]
  256. .groupeQualite + "')", {
  257. success: functionSuccess,
  258. error: functionError
  259. });
  260. break;
  261. default:
  262. }
  263. }
  264. },
  265.  
  266. onPressAdd: function(oEvent) {
  267. //On défini les paramètre pour savoir quelle form on fait apparaitre dans la popup
  268. var oDialog = this._getDialog("AddLine"),
  269. oData = {
  270. profil: this.sProfil,
  271. bArticle: false,
  272. bQualite: false,
  273. bMagasin: false,
  274. bFlaconnage: false,
  275. bGroupementQualFlac: false,
  276. bGroupeQualite: false
  277. };
  278.  
  279. switch (oEvent.getParameter("id")) {
  280. case "__xmlview0--buttonAddLineArticle":
  281. oData.bArticle = true;
  282. break;
  283. case "__xmlview0--buttonAddLineQualite":
  284. oData.bQualite = true;
  285. break;
  286. case "__xmlview0--buttonAddLineMagasin":
  287. oData.bMagasin = true;
  288. break;
  289. case "__xmlview0--buttonAddLineFlaconnage":
  290. oData.bFlaconnage = true;
  291. break;
  292. case "__xmlview0--buttonAddLineGroupementQualFlac":
  293. oData.bGroupementQualFlac = true;
  294. break;
  295. case "__xmlview0--buttonAddLineGroupeQualite":
  296. oData.bGroupeQualite = true;
  297. break;
  298. default:
  299. return;
  300. }
  301.  
  302. oDialog.setModel(new sap.ui.model.json.JSONModel(oData), "AddLine");
  303.  
  304. oDialog.open();
  305. },
  306.  
  307. onSearch: function(oEvent) {
  308. var oControl = oEvent.getSource().getParent().getParent();
  309.  
  310. // Get the search query, regardless of the triggered event ('query' for the search event, 'newValue' for the liveChange one).
  311. var sQuery = oEvent.getParameter("query") || oEvent.getParameter("newValue"),
  312. sSourceId = oEvent.getSource().getId(),
  313. aFinalFilters = [];
  314.  
  315. var aFilters = [];
  316. // 1) Search filters (with OR)
  317. if (sQuery && sQuery.length > 0) {
  318. aFilters.push(new sap.ui.model.Filter("designationArticle", sap.ui.model.FilterOperator.Contains, sQuery));
  319. aFilters.push(new sap.ui.model.Filter("article", sap.ui.model.FilterOperator.Contains, sQuery));
  320. aFilters.push(new sap.ui.model.Filter("lot", sap.ui.model.FilterOperator.Contains, sQuery));
  321. aFilters.push(new sap.ui.model.Filter("quantiteIgnore", sap.ui.model.FilterOperator.Contains, sQuery));
  322. }
  323.  
  324. aFinalFilters = aFilters.length > 0 ? [new sap.ui.model.Filter(aFilters, false)] : [];
  325. var oBindingOptions = this._updateBindingOptions(oControl.getId(), {
  326. filters: aFinalFilters
  327. }, sSourceId);
  328. var oBindingInfo = oControl.getBindingInfo("items");
  329. oControl.bindAggregation("items", {
  330. model: oBindingInfo.model,
  331. path: oBindingInfo.path,
  332. parameters: oBindingInfo.parameters,
  333. template: oBindingInfo.template,
  334. sorter: oBindingOptions.sorters,
  335. filters: oBindingOptions.filters
  336. });
  337.  
  338. },
  339.  
  340. _deleteUselessData: function(aItems, sEntityTypeName, fFlat) {
  341. fFlat = fFlat || false;
  342.  
  343. if (!sEntityTypeName) {
  344. return false;
  345. }
  346. var oMetadata = this.oView.getModel().getServiceMetadata();
  347.  
  348. //on test le metadata pour ne pas avoir d'erreur
  349. if (oMetadata && oMetadata.dataServices && oMetadata.dataServices.schema && oMetadata.dataServices.schema[0] && oMetadata.dataServices
  350. .schema[0].entityType) {
  351. var oEntityType = oMetadata.dataServices.schema[0].entityType;
  352. //on boucle sur les entityTypes pour avoir le bon
  353. for (var i = 0; i < oEntityType.length; i++) {
  354. if (oEntityType[i].name === sEntityTypeName) {
  355. var oProperties = oEntityType[i].property,
  356. oItems = [],
  357. oLine = {};
  358.  
  359. if (!fFlat) {
  360. for (var j = 0; j < aItems.length; j++) {
  361. oLine = {};
  362. for (var k = 0; k < oProperties.length; k++) {
  363. // if (aItems[j][oProperties[k].name] !== undefined) {
  364. oLine[oProperties[k].name] = aItems[j][oProperties[k].name];
  365. // }
  366. }
  367. oItems.push(oLine);
  368. }
  369. //Une fois qu'on a trouvé on quitte la fonction
  370. return oItems;
  371. } else {
  372. oLine = {};
  373. for (var l = 0; l < oProperties.length; l++) {
  374. oLine[oProperties[l].name] = aItems[oProperties[l].name];
  375. }
  376. return oLine;
  377. }
  378. }
  379. }
  380. }
  381. },
  382.  
  383. _refreshOdata: function(sOdataName) {
  384. var oProfilFilter = new Filter("profil", sap.ui.model.FilterOperator.EQ, this.sProfil);
  385.  
  386. switch (sOdataName) {
  387. case "articlesLotsSet":
  388. //La table des articles et lots exclu
  389. this.oView.getModel().read("/articlesLotsSet", {
  390. filters: [oProfilFilter],
  391. success: function(oDataArticle) {
  392. var oData = new sap.ui.model.json.JSONModel({
  393. items: oDataArticle.results
  394. });
  395. this.oView.setModel(oData, "article");
  396. }.bind(this),
  397. error: function(oError) {
  398.  
  399. }.bind(this)
  400. });
  401. break;
  402.  
  403. case "groupeQualiteSet":
  404. //La table des groupes de qualités exclu
  405. this.oView.getModel().read("/groupeQualiteSet", {
  406. filters: [
  407. oProfilFilter,
  408. new Filter("action", sap.ui.model.FilterOperator.EQ, "EXCLUDE")
  409. ],
  410. success: function(oDataGrpQualite) {
  411. var oData = new sap.ui.model.json.JSONModel({
  412. items: oDataGrpQualite.results
  413. });
  414. this.oView.setModel(oData, "grpQualite");
  415. }.bind(this),
  416. error: function(oError) {
  417.  
  418. }.bind(this)
  419. });
  420. break;
  421.  
  422. case "magasinSet":
  423. //La table des magasins exclu
  424. this.oView.getModel().read("/magasinSet", {
  425. filters: [oProfilFilter],
  426. success: function(oDataGrpQualite) {
  427. var oData = new sap.ui.model.json.JSONModel({
  428. items: oDataGrpQualite.results
  429. });
  430. this.oView.setModel(oData, "magasins");
  431. }.bind(this),
  432. error: function(oError) {
  433.  
  434. }.bind(this)
  435. });
  436. break;
  437.  
  438. case "flaconnageSet":
  439. //La table des flaconnages
  440. this.oView.getModel().read("/flaconnageSet", {
  441. filters: [oProfilFilter],
  442. success: function(oDataFlaconnage) {
  443. var oData = new sap.ui.model.json.JSONModel({
  444. items: oDataFlaconnage.results
  445. });
  446. this.oView.setModel(oData, "flaconnage");
  447. }.bind(this),
  448. error: function(oError) {
  449.  
  450. }.bind(this)
  451. });
  452. break;
  453.  
  454. case "groupementQualiteFlaconnageSet":
  455. //La table des groupement de qualité/flaconnage
  456. this.oView.getModel().read("/groupementQualiteFlaconnageSet", {
  457. filters: [oProfilFilter],
  458. success: function(oDataGroupementQualiteFlaconnage) {
  459. var oData = new sap.ui.model.json.JSONModel({
  460. items: oDataGroupementQualiteFlaconnage.results
  461. });
  462. this.oView.setModel(oData, "groupementQualiteFlaconnage");
  463. }.bind(this),
  464. error: function(oError) {
  465.  
  466. }.bind(this)
  467. });
  468. break;
  469.  
  470. case "groupeGroupementQualiteSet":
  471. //La table des groupes de qualité
  472. this.oView.getModel().read("/groupeGroupementQualiteSet", {
  473. filters: [oProfilFilter],
  474. success: function(oDataGroupeQualite) {
  475. var oData = new sap.ui.model.json.JSONModel({
  476. items: oDataGroupeQualite.results
  477. });
  478. this.oView.setModel(oData, "groupeQualite");
  479. }.bind(this),
  480. error: function(oError) {
  481.  
  482. }.bind(this)
  483. });
  484. break;
  485. }
  486. },
  487.  
  488. _updateBindingOptions: function(sCollectionId, oBindingData, sSourceId) {
  489. this.mBindingOptions = this.mBindingOptions || {};
  490. this.mBindingOptions[sCollectionId] = this.mBindingOptions[sCollectionId] || {};
  491.  
  492. var aSorters = this.mBindingOptions[sCollectionId].sorters;
  493. var oGroupby = this.mBindingOptions[sCollectionId].groupby;
  494.  
  495. // If there is no oBindingData parameter, we just need the processed filters and sorters from this function
  496. if (oBindingData) {
  497. if (oBindingData.sorters) {
  498. aSorters = oBindingData.sorters;
  499. }
  500. if (oBindingData.groupby) {
  501. oGroupby = oBindingData.groupby;
  502. }
  503. // 1) Update the filters map for the given collection and source
  504. this.mBindingOptions[sCollectionId].sorters = aSorters;
  505. this.mBindingOptions[sCollectionId].groupby = oGroupby;
  506. this.mBindingOptions[sCollectionId].filters = this.mBindingOptions[sCollectionId].filters || {};
  507. this.mBindingOptions[sCollectionId].filters[sSourceId] = oBindingData.filters || [];
  508. }
  509.  
  510. // 2) Reapply all the filters and sorters
  511. var aFilters = [];
  512. for (var key in this.mBindingOptions[sCollectionId].filters) {
  513. aFilters = aFilters.concat(this.mBindingOptions[sCollectionId].filters[key]);
  514. }
  515.  
  516. // Add the groupby first in the sorters array
  517. if (oGroupby) {
  518. aSorters = aSorters ? [oGroupby].concat(aSorters) : [oGroupby];
  519. }
  520.  
  521. var aFinalFilters = aFilters.length > 0 ? [new sap.ui.model.Filter(aFilters, true)] : undefined;
  522. return {
  523. filters: aFinalFilters,
  524. sorters: aSorters
  525. };
  526.  
  527. },
  528.  
  529. handleUploadComplete: function(oEvent) {
  530. var file = oEvent.getParameter("files") && oEvent.getParameter("files")[0];
  531.  
  532. if (this.oFileReader) {
  533. this.oFileReader.readAsText(file);
  534. }
  535. },
  536.  
  537. onTableUpdateStart: function(oEvent) {
  538. oEvent.getSource().setBusy(true);
  539. },
  540.  
  541. onTableUpdateFinished: function(oEvent) {
  542. oEvent.getSource().setBusy(false);
  543. },
  544.  
  545. _readFile: function(oEvent) {
  546. var strCSV = oEvent.target.result; //string in CSV
  547. // var otListJSON = self.csvJSON(strCSV).replace("\\n", "");
  548. debugger;
  549. },
  550.  
  551. _onCloseDialog: function() {
  552. this.getParent().getParent().getModel("article").refresh();
  553. this.getParent().getParent().getModel("grpQualite").refresh();
  554. this.getParent().getParent().getModel("magasins").refresh();
  555. this.getParent().getParent().getModel("flaconnage").refresh();
  556. this.getParent().getParent().getModel("groupementQualiteFlaconnage").refresh();
  557. this.getParent().getParent().getModel("groupeQualite").refresh();
  558. },
  559.  
  560. _getDialog: function(sDialogName) {
  561.  
  562. this.mDialogs = this.mDialogs || {};
  563. var oDialog = this.mDialogs[sDialogName];
  564. // var oBindingContext = oSource.getBindingContext();
  565. // var sPath = (oBindingContext) ? oBindingContext.getPath() : null;
  566. var oView;
  567. if (!oDialog) {
  568. this.getOwnerComponent().runAsOwner(function() {
  569. oView = sap.ui.xmlview({
  570. viewName: "ZPDT_PARAM.view.Dialog." + sDialogName
  571. });
  572. this.getView().addDependent(oView);
  573. oView.getController().setRouter(this.oRouter);
  574. oDialog = oView.getContent()[0];
  575. this.mDialogs[sDialogName] = oDialog;
  576. }.bind(this));
  577. }
  578.  
  579. // oDialog.attachEventOnce("afterClose", null, this._onCloseDialog);
  580.  
  581. return oDialog;
  582. }
  583.  
  584. });
  585. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement