Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sap.ui.define([
- "sap/ui/core/mvc/Controller",
- "../model/formatter",
- "sap/m/MessageToast",
- "sap/ui/model/Filter"
- ], function(Controller, formatter, MessageToast, Filter) {
- "use strict";
- return Controller.extend("ZPDT_PARAM.controller.Main", {
- formatter: formatter,
- onInit: function() {
- this.sProfil = null;
- this.oFileReader = new FileReader();
- this.oFileReader.onload = this._readFile;
- this.oView.setModel(new sap.ui.model.json.JSONModel({
- busyDelay: 0
- }), "paramAppli");
- this.oView.setModel(new sap.ui.model.json.JSONModel({
- MarqueSet: [],
- VerrerieSet: []
- }), "transverseTrie");
- this.oView.setModel(new sap.ui.model.json.JSONModel({
- items: []
- }), "article");
- this.oView.setModel(new sap.ui.model.json.JSONModel({
- items: []
- }), "grpQualite");
- this.oView.setModel(new sap.ui.model.json.JSONModel({
- items: []
- }), "magasins");
- this.oView.setModel(new sap.ui.model.json.JSONModel({
- items: []
- }), "flaconnage");
- this.oView.setModel(new sap.ui.model.json.JSONModel({
- items: []
- }), "groupementQualiteFlaconnage");
- this.oView.setModel(new sap.ui.model.json.JSONModel({
- items: []
- }), "groupeQualite");
- this.oView.setModel(new sap.ui.model.json.JSONModel({
- valeur: 0
- }), "seuil");
- },
- onBeforeRendering: function() {
- this.oView.getModel("transverse").read("/ProfilSet('parametrage')", {
- success: function(oDataProfil) {
- this.sProfil = oDataProfil.profil;
- //On défini les listes en fonction du profil
- //La table des articles et lots exclu
- this._refreshOdata("articlesLotsSet");
- //La table des groupes de qualités exclu
- this._refreshOdata("groupeQualiteSet");
- //La table des magasins exclu
- this._refreshOdata("magasinSet");
- //La table des flaconnages
- this._refreshOdata("flaconnageSet");
- //La table des groupement de qualité/flaconnage
- this._refreshOdata("groupementQualiteFlaconnageSet");
- //La table des groupes de qualité
- this._refreshOdata("groupeGroupementQualiteSet");
- //Le champ de tvarvc pour le seuil mini
- this.oView.getModel().read("/seuilSet(profil='" + oDataProfil.profil + "')", {
- success: function(oDataSeuil) {
- var oData = new sap.ui.model.json.JSONModel({
- valeur: oDataSeuil.valeur
- });
- this.oView.setModel(oData, "seuil");
- }.bind(this),
- error: function(oError) {
- }.bind(this)
- });
- //On update les listes
- this.oView.getModel("transverse").read("/VerrerieSet", {
- filters: [new Filter("profil", sap.ui.model.FilterOperator.EQ, oDataProfil.profil)],
- success: function(oDataVerrerie) {
- this.oView.getModel("transverseTrie").setProperty("/VerrerieSet", oDataVerrerie.results);
- }.bind(this),
- error: function(oError) {
- }.bind(this)
- });
- this.oView.getModel("transverse").read("/MarqueSet", {
- filters: [new Filter("profil", sap.ui.model.FilterOperator.EQ, oDataProfil.profil)],
- success: function(oDataMarques) {
- this.oView.getModel("transverseTrie").setProperty("/MarqueSet", oDataMarques.results);
- }.bind(this),
- error: function(oError) {
- }.bind(this)
- });
- }.bind(this),
- error: function(oError) {
- }.bind(this)
- });
- },
- onChangeSeuil: function(oEvent) {
- if (oEvent.getParameter("value")) {
- var oDataInput = {
- profil: this.sProfil,
- valeur: oEvent.getParameter("value")
- };
- this.oView.getModel().create("/seuilSet", oDataInput, {
- success: function(oDataSeuil) {
- this.oView.byId("seuilGlobal").setValue();
- var oData = new sap.ui.model.json.JSONModel({
- valeur: oDataSeuil.valeur
- });
- this.oView.setModel(oData, "seuil");
- }.bind(this),
- error: function(oError) {}.bind(this)
- });
- }
- },
- onChangeQuantite: function(oEvent) {
- var oData = this.oView.getModel("article").getData(),
- sContext = oEvent.getSource().getBindingInfo("value").binding.getContext().getPath(),
- lineIndex = sContext.substring(sContext.lastIndexOf("/") + 1),
- oValues = this._deleteUselessData(oData.items[lineIndex], "articlesLots", true);
- oValues.quantiteIgnore = oValues.quantiteIgnore.toString();
- this.oView.getModel().create("/articlesLotsSet", oValues, {
- success: function() {
- this._refreshOdata("articlesLotsSet");
- }.bind(this),
- error: function(oError) {}.bind(this)
- });
- },
- onPressDelete: function(oEvent) {
- var oTable = oEvent.getSource().getParent().getParent(),
- aLines = oTable.getSelectedItems(),
- lineIndex = 0,
- oData = {},
- functionSuccess,
- functionError;
- switch (oEvent.getParameter("id")) {
- case "__xmlview0--buttonDeleteLineArticle":
- oData = this.oView.getModel("article").getData();
- functionSuccess = function() {
- this._refreshOdata("articlesLotsSet");
- }.bind(this);
- break;
- case "__xmlview0--buttonDeleteLineQualite":
- oData = this.oView.getModel("grpQualite").getData();
- functionSuccess = function() {
- this._refreshOdata("groupeQualiteSet");
- }.bind(this);
- break;
- case "__xmlview0--buttonDeleteLineMagasin":
- oData = this.oView.getModel("magasins").getData();
- functionSuccess = function() {
- this._refreshOdata("magasinSet");
- }.bind(this);
- break;
- case "__xmlview0--buttonDeleteLineFlaconnage":
- oData = this.oView.getModel("flaconnage").getData();
- functionSuccess = function() {
- this._refreshOdata("flaconnageSet");
- }.bind(this);
- break;
- case "__xmlview0--buttonDeleteLineGroupementQualFlac":
- oData = this.oView.getModel("groupementQualiteFlaconnage").getData();
- functionSuccess = function() {
- this._refreshOdata("groupementQualiteFlaconnageSet");
- }.bind(this);
- break;
- case "__xmlview0--buttonDeleteLineGroupeQualite":
- oData = this.oView.getModel("groupeQualite").getData();
- functionSuccess = function() {
- this._refreshOdata("groupeGroupementQualiteSet");
- }.bind(this);
- break;
- default:
- }
- functionError = function(oError) {
- sap.m.MessageToast.show("Le groupement flaconnage/qualité est utilisé dans la table de flaconnage/qualité. Suppression impossible", {
- duration: 2000 || 3000
- });
- }.bind(this);
- //Pour chaque lignes on pousse la suppression à SAP
- for (var i = 0; i < aLines.length; i++) {
- lineIndex = aLines[i].getBindingContextPath().substring(aLines[i].getBindingContextPath().lastIndexOf("/") + 1);
- //On mets à jour l'oData depuis SAP
- switch (oEvent.getParameter("id")) {
- case "__xmlview0--buttonDeleteLineArticle":
- this.oView.getModel().remove("/articlesLotsSet(profil='" + oData.items[lineIndex].profil + "',article='" + oData.items[lineIndex]
- .article + "',lot='" + oData.items[lineIndex].lot + "')", {
- success: functionSuccess,
- error: functionError
- });
- break;
- case "__xmlview0--buttonDeleteLineQualite":
- this.oView.getModel().remove("/groupeQualiteSet(profil='" + oData.items[lineIndex].profil + "',brand='" + oData.items[lineIndex]
- .brand + "',grpQualite='" + oData.items[lineIndex].grpQualite + "',action='EXCLUDE')", {
- success: functionSuccess,
- error: functionError
- });
- break;
- case "__xmlview0--buttonDeleteLineMagasin":
- this.oView.getModel().remove("/magasinSet(profil='" + oData.items[lineIndex].profil + "',mag='" + oData.items[lineIndex]
- .mag + "',action='EXCLUDE')", {
- success: functionSuccess,
- error: functionError
- });
- break;
- case "__xmlview0--buttonDeleteLineFlaconnage":
- this.oView.getModel().remove("/flaconnageSet(profil='" + oData.items[lineIndex].profil + "',flcnage='" + oData.items[lineIndex]
- .flcnage + "',specificiteVerrerie='" + oData.items[lineIndex].specificiteVerrerie + "',marque='" + oData.items[lineIndex].marque +
- "')", {
- success: functionSuccess,
- error: functionError
- });
- break;
- case "__xmlview0--buttonDeleteLineGroupementQualFlac":
- this.oView.getModel().remove("/groupementQualiteFlaconnageSet(profil='" + oData.items[lineIndex].profil +
- "',groupementQualite='" + oData.items[
- lineIndex]
- .groupementQualite + "',groupementFlaconnage='" + oData.items[lineIndex].groupementFlaconnage + "')", {
- success: functionSuccess,
- error: functionError
- });
- break;
- case "__xmlview0--buttonDeleteLineGroupeQualite":
- this.oView.getModel().remove("/groupeGroupementQualiteSet(profil='" + oData.items[lineIndex].profil + "',groupeQualite='" +
- oData.items[
- lineIndex]
- .groupeQualite + "')", {
- success: functionSuccess,
- error: functionError
- });
- break;
- default:
- }
- }
- },
- onPressAdd: function(oEvent) {
- //On défini les paramètre pour savoir quelle form on fait apparaitre dans la popup
- var oDialog = this._getDialog("AddLine"),
- oData = {
- profil: this.sProfil,
- bArticle: false,
- bQualite: false,
- bMagasin: false,
- bFlaconnage: false,
- bGroupementQualFlac: false,
- bGroupeQualite: false
- };
- switch (oEvent.getParameter("id")) {
- case "__xmlview0--buttonAddLineArticle":
- oData.bArticle = true;
- break;
- case "__xmlview0--buttonAddLineQualite":
- oData.bQualite = true;
- break;
- case "__xmlview0--buttonAddLineMagasin":
- oData.bMagasin = true;
- break;
- case "__xmlview0--buttonAddLineFlaconnage":
- oData.bFlaconnage = true;
- break;
- case "__xmlview0--buttonAddLineGroupementQualFlac":
- oData.bGroupementQualFlac = true;
- break;
- case "__xmlview0--buttonAddLineGroupeQualite":
- oData.bGroupeQualite = true;
- break;
- default:
- return;
- }
- oDialog.setModel(new sap.ui.model.json.JSONModel(oData), "AddLine");
- oDialog.open();
- },
- onSearch: function(oEvent) {
- var oControl = oEvent.getSource().getParent().getParent();
- // Get the search query, regardless of the triggered event ('query' for the search event, 'newValue' for the liveChange one).
- var sQuery = oEvent.getParameter("query") || oEvent.getParameter("newValue"),
- sSourceId = oEvent.getSource().getId(),
- aFinalFilters = [];
- var aFilters = [];
- // 1) Search filters (with OR)
- if (sQuery && sQuery.length > 0) {
- aFilters.push(new sap.ui.model.Filter("designationArticle", sap.ui.model.FilterOperator.Contains, sQuery));
- aFilters.push(new sap.ui.model.Filter("article", sap.ui.model.FilterOperator.Contains, sQuery));
- aFilters.push(new sap.ui.model.Filter("lot", sap.ui.model.FilterOperator.Contains, sQuery));
- aFilters.push(new sap.ui.model.Filter("quantiteIgnore", sap.ui.model.FilterOperator.Contains, sQuery));
- }
- aFinalFilters = aFilters.length > 0 ? [new sap.ui.model.Filter(aFilters, false)] : [];
- var oBindingOptions = this._updateBindingOptions(oControl.getId(), {
- filters: aFinalFilters
- }, sSourceId);
- var oBindingInfo = oControl.getBindingInfo("items");
- oControl.bindAggregation("items", {
- model: oBindingInfo.model,
- path: oBindingInfo.path,
- parameters: oBindingInfo.parameters,
- template: oBindingInfo.template,
- sorter: oBindingOptions.sorters,
- filters: oBindingOptions.filters
- });
- },
- _deleteUselessData: function(aItems, sEntityTypeName, fFlat) {
- fFlat = fFlat || false;
- if (!sEntityTypeName) {
- return false;
- }
- var oMetadata = this.oView.getModel().getServiceMetadata();
- //on test le metadata pour ne pas avoir d'erreur
- if (oMetadata && oMetadata.dataServices && oMetadata.dataServices.schema && oMetadata.dataServices.schema[0] && oMetadata.dataServices
- .schema[0].entityType) {
- var oEntityType = oMetadata.dataServices.schema[0].entityType;
- //on boucle sur les entityTypes pour avoir le bon
- for (var i = 0; i < oEntityType.length; i++) {
- if (oEntityType[i].name === sEntityTypeName) {
- var oProperties = oEntityType[i].property,
- oItems = [],
- oLine = {};
- if (!fFlat) {
- for (var j = 0; j < aItems.length; j++) {
- oLine = {};
- for (var k = 0; k < oProperties.length; k++) {
- // if (aItems[j][oProperties[k].name] !== undefined) {
- oLine[oProperties[k].name] = aItems[j][oProperties[k].name];
- // }
- }
- oItems.push(oLine);
- }
- //Une fois qu'on a trouvé on quitte la fonction
- return oItems;
- } else {
- oLine = {};
- for (var l = 0; l < oProperties.length; l++) {
- oLine[oProperties[l].name] = aItems[oProperties[l].name];
- }
- return oLine;
- }
- }
- }
- }
- },
- _refreshOdata: function(sOdataName) {
- var oProfilFilter = new Filter("profil", sap.ui.model.FilterOperator.EQ, this.sProfil);
- switch (sOdataName) {
- case "articlesLotsSet":
- //La table des articles et lots exclu
- this.oView.getModel().read("/articlesLotsSet", {
- filters: [oProfilFilter],
- success: function(oDataArticle) {
- var oData = new sap.ui.model.json.JSONModel({
- items: oDataArticle.results
- });
- this.oView.setModel(oData, "article");
- }.bind(this),
- error: function(oError) {
- }.bind(this)
- });
- break;
- case "groupeQualiteSet":
- //La table des groupes de qualités exclu
- this.oView.getModel().read("/groupeQualiteSet", {
- filters: [
- oProfilFilter,
- new Filter("action", sap.ui.model.FilterOperator.EQ, "EXCLUDE")
- ],
- success: function(oDataGrpQualite) {
- var oData = new sap.ui.model.json.JSONModel({
- items: oDataGrpQualite.results
- });
- this.oView.setModel(oData, "grpQualite");
- }.bind(this),
- error: function(oError) {
- }.bind(this)
- });
- break;
- case "magasinSet":
- //La table des magasins exclu
- this.oView.getModel().read("/magasinSet", {
- filters: [oProfilFilter],
- success: function(oDataGrpQualite) {
- var oData = new sap.ui.model.json.JSONModel({
- items: oDataGrpQualite.results
- });
- this.oView.setModel(oData, "magasins");
- }.bind(this),
- error: function(oError) {
- }.bind(this)
- });
- break;
- case "flaconnageSet":
- //La table des flaconnages
- this.oView.getModel().read("/flaconnageSet", {
- filters: [oProfilFilter],
- success: function(oDataFlaconnage) {
- var oData = new sap.ui.model.json.JSONModel({
- items: oDataFlaconnage.results
- });
- this.oView.setModel(oData, "flaconnage");
- }.bind(this),
- error: function(oError) {
- }.bind(this)
- });
- break;
- case "groupementQualiteFlaconnageSet":
- //La table des groupement de qualité/flaconnage
- this.oView.getModel().read("/groupementQualiteFlaconnageSet", {
- filters: [oProfilFilter],
- success: function(oDataGroupementQualiteFlaconnage) {
- var oData = new sap.ui.model.json.JSONModel({
- items: oDataGroupementQualiteFlaconnage.results
- });
- this.oView.setModel(oData, "groupementQualiteFlaconnage");
- }.bind(this),
- error: function(oError) {
- }.bind(this)
- });
- break;
- case "groupeGroupementQualiteSet":
- //La table des groupes de qualité
- this.oView.getModel().read("/groupeGroupementQualiteSet", {
- filters: [oProfilFilter],
- success: function(oDataGroupeQualite) {
- var oData = new sap.ui.model.json.JSONModel({
- items: oDataGroupeQualite.results
- });
- this.oView.setModel(oData, "groupeQualite");
- }.bind(this),
- error: function(oError) {
- }.bind(this)
- });
- break;
- }
- },
- _updateBindingOptions: function(sCollectionId, oBindingData, sSourceId) {
- this.mBindingOptions = this.mBindingOptions || {};
- this.mBindingOptions[sCollectionId] = this.mBindingOptions[sCollectionId] || {};
- var aSorters = this.mBindingOptions[sCollectionId].sorters;
- var oGroupby = this.mBindingOptions[sCollectionId].groupby;
- // If there is no oBindingData parameter, we just need the processed filters and sorters from this function
- if (oBindingData) {
- if (oBindingData.sorters) {
- aSorters = oBindingData.sorters;
- }
- if (oBindingData.groupby) {
- oGroupby = oBindingData.groupby;
- }
- // 1) Update the filters map for the given collection and source
- this.mBindingOptions[sCollectionId].sorters = aSorters;
- this.mBindingOptions[sCollectionId].groupby = oGroupby;
- this.mBindingOptions[sCollectionId].filters = this.mBindingOptions[sCollectionId].filters || {};
- this.mBindingOptions[sCollectionId].filters[sSourceId] = oBindingData.filters || [];
- }
- // 2) Reapply all the filters and sorters
- var aFilters = [];
- for (var key in this.mBindingOptions[sCollectionId].filters) {
- aFilters = aFilters.concat(this.mBindingOptions[sCollectionId].filters[key]);
- }
- // Add the groupby first in the sorters array
- if (oGroupby) {
- aSorters = aSorters ? [oGroupby].concat(aSorters) : [oGroupby];
- }
- var aFinalFilters = aFilters.length > 0 ? [new sap.ui.model.Filter(aFilters, true)] : undefined;
- return {
- filters: aFinalFilters,
- sorters: aSorters
- };
- },
- handleUploadComplete: function(oEvent) {
- var file = oEvent.getParameter("files") && oEvent.getParameter("files")[0];
- if (this.oFileReader) {
- this.oFileReader.readAsText(file);
- }
- },
- onTableUpdateStart: function(oEvent) {
- oEvent.getSource().setBusy(true);
- },
- onTableUpdateFinished: function(oEvent) {
- oEvent.getSource().setBusy(false);
- },
- _readFile: function(oEvent) {
- var strCSV = oEvent.target.result; //string in CSV
- // var otListJSON = self.csvJSON(strCSV).replace("\\n", "");
- debugger;
- },
- _onCloseDialog: function() {
- this.getParent().getParent().getModel("article").refresh();
- this.getParent().getParent().getModel("grpQualite").refresh();
- this.getParent().getParent().getModel("magasins").refresh();
- this.getParent().getParent().getModel("flaconnage").refresh();
- this.getParent().getParent().getModel("groupementQualiteFlaconnage").refresh();
- this.getParent().getParent().getModel("groupeQualite").refresh();
- },
- _getDialog: function(sDialogName) {
- this.mDialogs = this.mDialogs || {};
- var oDialog = this.mDialogs[sDialogName];
- // var oBindingContext = oSource.getBindingContext();
- // var sPath = (oBindingContext) ? oBindingContext.getPath() : null;
- var oView;
- if (!oDialog) {
- this.getOwnerComponent().runAsOwner(function() {
- oView = sap.ui.xmlview({
- viewName: "ZPDT_PARAM.view.Dialog." + sDialogName
- });
- this.getView().addDependent(oView);
- oView.getController().setRouter(this.oRouter);
- oDialog = oView.getContent()[0];
- this.mDialogs[sDialogName] = oDialog;
- }.bind(this));
- }
- // oDialog.attachEventOnce("afterClose", null, this._onCloseDialog);
- return oDialog;
- }
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement