demo20230512/Browser_Local/js/CRUD.js
2023-05-12 10:20:28 +08:00

149 lines
6.1 KiB
JavaScript

/*
Logic Solution
WeeeCarbonFootprint
Data access CRUD from API
*/
function Query(MyLCAdata, controller, action, LCAID, id, pa3, pa4) {
return MyLCAdata.query({ Controller: controller, Action: action, pa1: LCAID, pa2: id, pa3: pa3, pa4: pa4 }).$promise;
}
function Get(MyLCAdata, isArray, controller, action, LCAID, id, pa3, pa4) {
return MyLCAdata.get({ Controller: controller, Action: action, pa1: LCAID, pa2: id, pa3: pa3, pa4: pa4 }, { isArray: isArray }).$promise;
}
function Save(MyLCAdata, data, controller, action, LCAID, id, pa3, pa4) {
return MyLCAdata.save({ Controller: controller, Action: action, pa1: LCAID, pa2: id, pa3: pa3, pa4: pa4 }, data).$promise;
}
function Delete(MyLCAdata, data, controller, action, LCAID, id, pa3, pa4) {
return MyLCAdata.delete({ Controller: controller, Action: action, pa1: LCAID, pa2: id, pa3: pa3, pa4: pa4 }, data).$promise;
}
function getCertificationCompany(MyLCAdata, scope) {
return Query(MyLCAdata, 'Company', 'getCertificationCompany').then(function (response) {
scope.product_survey.Verification_Unit = response;
});
}
function getPublicPCR(MyLCAdata, scope) {
return Query(MyLCAdata, 'PCR', 'getPublicPCR').then(function (response) {
scope.product_survey.LCA = {};
scope.product_survey.PCR = response;
return response;
});
}
function getProduct(MyLCAdata, scope) {
return Query(MyLCAdata, 'Product', 'Get').then(function (response) {
scope.product_survey.Product = response;
});
}
function getFab(MyLCAdata, scope) {
return Query(MyLCAdata, 'Fab', 'Get').then(function (response) {
scope.product_survey.Fab = response;
});
}
function createLCA(MyLCAdata, cookieStore, type, LCA) {
var isProductLCA = true;
if (type == 'Organization') isProductLCA = false;
return Save(MyLCAdata, LCA, 'LCA', 'Create' + type + 'LCA').then(function (response) {
cookieStore.put('LCAID', mergeNumber(response));
cookieStore.put('LCA_Status_Now', 0);
cookieStore.put('isProductLCA', isProductLCA);
});
}
function getReplyLCA(MyLCAdata, scope) {
return Query(MyLCAdata, 'LCA', 'GetMyLCA').then(function (response) {
for (var r = 0; r < response.length; r++) scope.Reply_LCA.push(response[r]);
});
}
function getComment(MyLCAdata, scope, LCAID) {
return Query(MyLCAdata, 'LCA', 'GetComments', LCAID).then(function (response) {
for (var sb = 0, _sb = scope.sidebar; sb < scope.sidebar.length; sb++) {
_sb[sb].comment_item = [];
for (var r = 0; r < response.length; r++) {
if (response[r].Category.indexOf(_sb[sb].Name) > -1) {
if (_sb[sb].Name.indexOf('Waste') > -1 || _sb[sb].Name.indexOf('Transport') > -1) {
if (response[r].Category.indexOf('_') > -1) {
_sb[sb].comment_item.push(response[r].CommentText);
} else if (_sb[sb].Name == response[r].Category) {
_sb[sb].comment_item.push(response[r].CommentText);
}
} else if ((_sb[sb].Name == 'Vehicle' || _sb[sb].Name == 'Kitchen') && response[r].Category.indexOf('_') > -1) {
_sb[sb].comment_item.push(response[r].CommentText);
} else {
_sb[sb].comment_item.push(response[r].CommentText);
}
}
}
}
});
}
function getSimaproEncodingAndValues(MyLCAdata, _dr, encodings) {
return Save(MyLCAdata, encodings, 'Parameter', 'GetSimaproEncodingsAndValues').then(function (response) {
for (var d = 0; d < _dr.length; d++) {
if (typeof _dr[d].ParameterID != 'undefined' && _dr[d].ParameterID != null) {
var encoding_value = splitValue(response[_dr[d].ParameterID]);
_dr[d].Encoding = encoding_value.first;
_dr[d].ParameterValue = encoding_value.second;
}
for (var dre = 0, _dre = _dr[d].ChildMaterials; dre < _dr[d].ChildMaterials.length; dre++) {
if (typeof _dre[dre].ParameterID != 'undefined' && _dre[dre].ParameterID != null) {
var encoding_value = splitValue(response[_dre[dre].ParameterID]);
_dre[dre].Encoding = encoding_value.first;
_dre[dre].ParameterValue = encoding_value.second;
}
}
}
});
}
function getSupplier(MyLCAdata, _dr, scope) {
return Query(MyLCAdata, 'Company', 'getSupplier').then(function (response) {
scope.Supplier = {};
var sps = [];
for (var r = 0; r < response.length; r++) {
sps.push(response[r].Name);
scope.Supplier[response[r].Name] = { Email: response[r].Email, ID: response[r].ID, Name: response[r].Name };
}
for (var d = 0; d < _dr.length; d++) {
_dr[d].SupplierCompanyselect = sps;
for (var r = 0; r < response.length; r++) {
if (_dr[d].SupplierCompanyID == response[r].ID) {
_dr[d].SupplierCompany = response[r].Name;
if (_dr[d].SupplierCompanyEmail == null) {
_dr[d].SupplierCompanyEmail = response[r].Email;
}
}
}
}
});
}
function getSimaproAndVersion(MyLCAdata, scope) {
return Query(MyLCAdata, 'PCR', 'GetVersionCategory').then(function (response) {
scope.Simapro = [];
scope.Simapro_Version = [];
for (var v = 0; v < response.length; v++) {
scope.Simapro.push(response[v]);
scope.Simapro_Version.push({ Version: response[v].Version });
}
});
}
function organizationLCANewToProccess(MyLCAdata, scope, cookieStore) {
return Save(MyLCAdata, null, 'LCA', 'OrganizationNewProcessing', cookieStore.get('LCAID')).then(function (response) {
if (mergeNumber(response)) {
resetLCAState(scope);
scope.LCA_Status_Now = 1;
cookieStore.put('LCA_Status_Now', 1);
}
});
}