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

1279 lines
76 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
Logic Solution
WeeeCarbonFootprint
Template Directive
obsoleted not in use now
*/
var arr = window.location.href.split("/");
if (arr[3].indexOf('app') == 0)
baseUrl = '';
else
baseUrl = '/' + arr[3];
angular.module('View.Directive')
.directive('headerContent', function (CONSTANTS) {
return {
restrict: 'E',
replace: true,
controller: function ($scope, $routeParams, $resource, $route, $cookieStore, $timeout, SURVEY_TABLE_MODEL, Survey_Data_Service, Redirect, $filter) {
var MyLCAdata = Survey_Data_Service.API;
var redirect = new Redirect();
var order_items = SURVEY_TABLE_MODEL.SURVEY_FORM_CONFIG;
$scope.headerImageChange = function (sn, m) {
var k = 'header_image' + sn;
var s = 'show' + sn;
this[k] = 'url(\'' + baseUrl + '/Browser_Local/img/' + k + '_' + m + '.png\')';
if (m == 1) this[s] = false;
if (m == 2) this[s] = true;
}
for (var h = 0; h < CONSTANTS.HeaderContent.length; h++) {
$scope.headerImageChange(h + 1, 1);
}
$scope.HeaderContent = CONSTANTS.HeaderContent;
$scope.tooltipWidth = function (n) {
if (typeof CONSTANTS != 'undefined' && typeof CONSTANTS.HeaderContent != 'undefined' && n <= CONSTANTS.HeaderContent.length) {
var char_length = countLength(CONSTANTS.HeaderContent[n - 1]);
var space_length = 0;
var temp = CONSTANTS.HeaderContent[n - 1].substr(0);
while (temp.indexOf(' ') > -1) {
temp = temp.substr(temp.indexOf(' ') + 1);
space_length++;
}
// 字體 10px, 字距 2px
return char_length * 5 + (char_length - space_length - 1) * 2 + space_length * 10 + 15 + 8 + 'px';
}
}
$scope.Add = function () {
$scope.Table_Add_State = true;
$scope.show_table_edit = true;
$scope.Enable_Add_Child = false;
var roi = $routeParams.OrderItem;
if (roi != 'WorkHour' && roi != 'PowerUsage' && roi != 'WaterUsage' && roi != 'SteamUsage') {
// 橫向編輯
$scope.Table_Edit_State = true;
$scope.targetColumn = 0;
$scope.targetTable = 0;
var _st = $scope.Survey_Table[$scope.targetTable];
$scope.targetRow = typeof _st.Data_Row != 'undefined' ? _st.Data_Row.length : 0;
var _tdr = $scope.targetRow;
if (_tdr == 0) _st.Data_Row = [];
var _dr = _st.Data_Row;
_dr.push([]);
_dr[_tdr].rowDone = true;
_dr[_tdr].ID = 0;
_dr[_tdr].No = _tdr + 1;
_dr[_tdr].Data_Row_Edit = {};
if (roi == 'Vehicle') {
_dr[_tdr].Typeselect = CONSTANTS.Type;
} else if (roi.indexOf('High') > -1) {
_dr[_tdr].MaterialTypeselect = CONSTANTS.MaterialType;
}
for (var t = 0, _t = _st.Table_Header; t < _st.Table_Header.length; t++) {
var keyname = setKeyTitle(_t[t].Keys);
_dr[_tdr].Data_Row_Edit[keyname] = {};
_dr[_tdr].Data_Row_Edit[keyname].data = null;
_dr[_tdr].Data_Row_Edit[keyname].edit = true;
_dr[_tdr].Data_Row_Edit[keyname].required = false;
if (keyname == 'Unit' && (roi.indexOf('LCI') > -1 || roi.indexOf('High') > -1)) {
_dr[_tdr][keyname] = undefined;
_dr[_tdr][keyname + 'select'] = setSelectOptions($scope[roi + 'UnitOptions']);
}
}
if (typeof _dr[_tdr].Data_Row_Edit.No != 'undefined') {
_dr[_tdr].Data_Row_Edit.No.data = _tdr + 1;
}
// 非Material跟有月份之外的頁面
var option_name = roi + 'Options';
if (roi.indexOf('LCI') > -1 || roi.indexOf('High') > -1) {
setDataRowSelectOption(_st.Table_Header, _dr[_tdr], $scope[roi + 'Options'], CONSTANTS, $scope[roi + 'UnitOptions'], $scope.Supplier);
_dr[_tdr].SupplierCompanyselect = setSelectOptions($scope.Supplier);
} else if (roi != 'Refrigerant') {
setDataRowSelectOption(_st.Table_Header, _dr[_tdr], $scope[roi + 'Options'], CONSTANTS, null);
} else {
setDataRowSelectOption(_st.Table_Header, _dr[_tdr], $scope[roi + 'Options1'], CONSTANTS, $scope[roi + 'Options2']);
}
setDataRowSelectValue(_st.Table_Header, _dr[_tdr], CONSTANTS, { 1: undefined, 2: undefined }, { 1: undefined, 2: undefined }, $scope.Supplier);
var div = document.getElementsByClassName('survey_form_outer_wrap')[0];
if (typeof div.scrollHeight != 'undefined' && div.scrollHeight > 595) {
$timeout(function () {
div.scrollTop = div.scrollHeight - 595 + 29;
}, 200);
}
$scope.dr = _dr[_tdr];
}
}
$scope.AddChild = function () {
$scope.Table_Edit_State = true;
$scope.Enable_Add_Child = false;
$scope.Table_Child_Edit_State = true;
$scope.Table_Child_Add_State = true;
$scope.show_table_edit = true;
$scope.targetColumn = 0;
$scope.targetTable = 0;
var _tdr = $scope.targetRow;
var _st = $scope.Survey_Table[$scope.targetTable];
var _dr = _st.Data_Row;
var roi = $scope.current_item;
_dr[_tdr].child_expand = true;
_dr[_tdr].rowDone = true;
if (typeof _dr[_tdr].ChildMaterials == 'undefined') {
$scope.targetChildRow = 0;
_dr[_tdr].ChildMaterials = [{ ID: 0, Data_Row_Edit: {} }];
} else {
$scope.targetChildRow = _dr[_tdr].ChildMaterials.length;
_dr[_tdr].ChildMaterials.push({ ID: 0, Data_Row_Edit: {} });
}
for (var d in _dr[_tdr]) {
if (d != 'ChildMaterials' && d != 'Data_Row_Edit' && d != 'ID') {
_dr[_tdr].ChildMaterials[_dr[_tdr].ChildMaterials.length - 1][d] = null;
_dr[_tdr].ChildMaterials[_dr[_tdr].ChildMaterials.length - 1].Data_Row_Edit[d] = {};
_dr[_tdr].ChildMaterials[_dr[_tdr].ChildMaterials.length - 1].Data_Row_Edit[d].data = null;
_dr[_tdr].ChildMaterials[_dr[_tdr].ChildMaterials.length - 1].Data_Row_Edit[d].edit = true;
_dr[_tdr].ChildMaterials[_dr[_tdr].ChildMaterials.length - 1].Data_Row_Edit[d].required = false;
}
for (var t = 0, _t = _st.Table_Header; t < _st.Table_Header.length; t++) {
var keyname = setKeyTitle(_t[t].Keys);
if (keyname == 'Unit') {
_dr[_tdr].ChildMaterials[_dr[_tdr].ChildMaterials.length - 1][keyname] = undefined;
_dr[_tdr].ChildMaterials[_dr[_tdr].ChildMaterials.length - 1][keyname + 'select'] = setSelectOptions($scope[roi + 'UnitOptions']);
} else if (keyname == 'DQI') {
_dr[_tdr].ChildMaterials[_dr[_tdr].ChildMaterials.length - 1][keyname] = undefined;
_dr[_tdr].ChildMaterials[_dr[_tdr].ChildMaterials.length - 1][keyname + 'select'] = CONSTANTS.DQI;
} else if (keyname == 'ParameterType') {
_dr[_tdr].ChildMaterials[_dr[_tdr].ChildMaterials.length - 1][keyname] = undefined;
_dr[_tdr].ChildMaterials[_dr[_tdr].ChildMaterials.length - 1][keyname + 'select'] = setSelectOptions($scope[roi + 'Options']);
}
}
}
_dr[_tdr].ChildMaterials[_dr[_tdr].ChildMaterials.length - 1].isChild = true;
$scope.dr = _dr[_tdr].ChildMaterials[_dr[_tdr].ChildMaterials.length - 1];
var div = document.getElementsByClassName('survey_form_outer_wrap')[0];
if (typeof div.scrollHeight != 'undefined' && div.scrollHeight > 595) {
$timeout(function () {
div.scrollTop = div.scrollHeight - 595 + 29;
}, 200);
}
}
$scope.Edit = function () {
var _tth = typeof $scope.targetColumn != 'undefined' ? $scope.targetColumn : -1;
var _tdr = typeof $scope.targetRow != 'undefined' ? $scope.targetRow : -1;
var _tdcr = typeof $scope.targetChildRow != 'undefined' ? $scope.targetChildRow : -1;
var _st = typeof $scope.Survey_Table[$scope.targetTable] != 'undefined' ? $scope.Survey_Table[$scope.targetTable] : -1;
var roi = $routeParams.OrderItem;
if ($scope.Table_Edit_State || $scope.Table_Child_Edit_State) {
alert(CONSTANTS.AlertMessage[13]);
return false;
}
if (_tth > -1 && (_tdr > -1 || _tdcr > -1)) {
$scope.Table_Edit_State = true;
if (roi == 'WorkHour' || roi == 'PowerUsage' || roi == 'WaterUsage' || roi == 'SteamUsage') {
// 垂直向編輯
$scope.tempname = roi + 'Tmp';
$scope[$scope.tempname] = [];
var year_row = 1;
if (roi == 'PowerUsage') year_row = 5;
for (var dt = 0; dt < $scope.tempcount; dt++) {
$scope[$scope.tempname].push(typeof _st.Data_Row[dt][_st.Table_Header[_tth].Keys[0]] != 'undefined' ? _st.Data_Row[dt][_st.Table_Header[_tth].Keys[0]] : 0);
}
$scope[$scope.tempname].push(typeof _st.Data_Row[year_row][_st.Table_Header[_tth].Keys[0]] != 'undefined' ? _st.Data_Row[year_row][_st.Table_Header[_tth].Keys[0]] : null);
for (var d = 0, _dr = _st.Data_Row; d < _st.Data_Row.length; d++) {
for (var dre in _dr[d].Data_Row_Edit) {
if (dre == _st.Table_Header[_tth].Keys[0]) {
_dr[d].Data_Row_Edit[dre].edit = true;
_dr[d].Data_Row_Edit[dre].data = _dr[d][dre];
break;
}
}
}
} else {
// 橫向編輯
var _dr = _st.Data_Row;
var _td = _tdr;
if (typeof _tdcr != 'undefined' && _tdcr != -1) {
_dr = _st.Data_Row[_tdr].ChildMaterials;
_td = _tdcr;
}
for (var d = 0; d < _st.Data_Row.length; d++) {
if (d == _td) {
_dr[d].rowDone = true;
for (var dre in _dr[d].Data_Row_Edit) {
_dr[d].Data_Row_Edit[dre].edit = true;
for (var th = 0, _th = _st.Table_Header; th < _st.Table_Header.length; th++) {
_dr[d].Data_Row_Edit[dre].data = _dr[d][dre];
}
}
$scope.dr = _dr[d];
$scope.show_table_edit = true;
break;
}
}
}
}
}
$scope.Cancel = function () {
var _tth = $scope.targetColumn;
var _tdr = $scope.targetRow;
var _st = $scope.Survey_Table[$scope.targetTable];
var roi = $scope.current_item;
if ((typeof $scope.Table_Child_Add_State == 'undefined' || !$scope.Table_Child_Add_State
|| typeof $scope.Table_Child_Edit_State == 'undefined' || !$scope.Table_Child_Edit_State)
&& (typeof $scope.targetChildRow == 'undefined' || $scope.targetChildRow == -1)) {
if (typeof _tdr != 'undefined' && _tdr != -1) {
if (typeof $scope.Table_Add_State == 'undefined' || !$scope.Table_Add_State) {
for (var d = 0, _dr = _st.Data_Row; d < _st.Data_Row.length; d++) {
if (roi == 'WorkHour' || roi.indexOf('Usage') > -1) {
for (var dr in _dr[d]) {
if (dr.indexOf('select') > -1) {
_dr[d][_st.Table_Header[_tth].Keys[0]] = null;
break;
}
}
for (var dre in _dr[d].Data_Row_Edit) {
_dr[d].Data_Row_Edit[dre].edit = false;
_dr[d].rowDone = false;
}
} else {
if (d == _tdr) {
for (var dre in _dr[d].Data_Row_Edit) {
_dr[d].Data_Row_Edit[dre].edit = false;
_dr[d].rowDone = false;
}
}
}
}
} else {
_st.Data_Row.splice($scope.targetRow, 1);
}
}
} else {
if (typeof $scope.Table_Child_Add_State == 'undefined' || !$scope.Table_Child_Add_State) {
var _tdcr = $scope.targetChildRow;
for (var dre in _st.Data_Row[_tdr].ChildMaterials[_tdcr].Data_Row_Edit) {
_st.Data_Row[_tdr].ChildMaterials[_tdcr].Data_Row_Edit[dre].edit = false;
_st.Data_Row[_tdr].ChildMaterials[_tdcr].rowDone = false;
}
} else {
_st.Data_Row[$scope.targetRow].ChildMaterials.splice($scope.targetChildRow, 1);
}
}
resetLCAState($scope);
}
$scope.Delete = function () {
var _tth = typeof $scope.targetColumn != 'undefined' ? $scope.targetColumn : -1;
var _tdr = typeof $scope.targetRow != 'undefined' ? $scope.targetRow : -1;
var _st = typeof $scope.Survey_Table[$scope.targetTable] != 'undefined' ? $scope.Survey_Table[$scope.targetTable] : -1;
var roi = $scope.current_item;
if (!$scope.Table_Edit_State && _tth != -1 && _tdr != -1) {
if (roi == 'WorkHour' || roi == 'PowerUsage' || roi == 'WaterUsage' || roi == 'SteamUsage') {
// 垂直向編輯 沒有刪除功能
} else {
// 橫向編輯
loadingDisable($timeout, $scope, true);
for (var d = 0, _dr = _st.Data_Row; d < _st.Data_Row.length; d++) {
if (d == _tdr) {
if (roi.indexOf('LCI') > -1 || roi.indexOf('High') > -1) {
roi = 'Material';
var action = 'Delete' + roi;
var LCAID = typeof $cookieStore.get('LCAID') != 'undefined' ? $cookieStore.get('LCAID') : $scope.product_survey.LCA.ID;
Delete(MyLCAdata, null, roi, action, LCAID, _st.Data_Row[_tdr].ID).then(function (response) {
_st.Data_Row.splice(d, 1);
loadingDisable($timeout, $scope, false);
});
} else {
var action = 'Delete' + roi;
Delete(MyLCAdata, null, roi, action, _st.Data_Row[_tdr].ID).then(function (response) {
_st.Data_Row.splice(d, 1);
loadingDisable($timeout, $scope, false);
});
}
break;
}
}
}
}
resetLCAState($scope);
}
$scope.Done = function () {
$scope.isCheckOnce = true;
var _tth = $scope.targetColumn;
var _tdr = $scope.targetRow;
var _tdcr = $scope.targetChildRow;
var _tb = $scope.targetTable;
var _st = $scope.Survey_Table[_tb];
var roi = $scope.current_item;
var LCAID = typeof $cookieStore.get('LCAID') != 'undefined' ? $cookieStore.get('LCAID') : $scope.product_survey.LCA.ID;
var send_data = {};
if (roi == 'WorkHour' || roi == 'PowerUsage' || roi == 'WaterUsage' || roi == 'SteamUsage') {
// 垂直向編輯
// 若沒有填任何資料,則不送回 Server
// 若將數據都改成 0還是要送回 Server一樣算"有變動"
// 除數據需填以外,亦須選擇年份才算資料填寫完成
var action = 'Save' + roi;
var hasedit = false;
for (var dt = 0; dt < $scope.tempcount; dt++) {
if ($scope[$scope.tempname][dt] != _st.Data_Row[dt].Data_Row_Edit[_st.Table_Header[_tth].Keys[0]].data) {
hasedit = true;
break;
}
}
var year_row = 1;
if (roi != 'WorkHour') {
if (roi == 'PowerUsage') year_row = 5;
if (_st.Data_Row[year_row][_st.Table_Header[_tth].Keys[0]] == null) {
hasedit = false;
} else {
if ($scope[$scope.tempname][$scope[$scope.tempname].length - 1] != _st.Data_Row[year_row][_st.Table_Header[_tth].Keys[0]]) {
hasedit = true;
}
}
}
if (hasedit) {
// Front End Data
var _dr = _st.Data_Row;
for (var d = 0; d < $scope.tempcount; d++) {
_dr[d][_st.Table_Header[_tth].Keys[0]] = _dr[d].Data_Row_Edit[_st.Table_Header[_tth].Keys[0]].data;
}
for (var th = 0, _th = _st.Table_Header; th < _st.Table_Header.length - 1; th++) {
var keyname = setKeyTitle(_th[th].Keys);
for (var l = 0, lft = Setkeys(_st.Table_Left_Title) ; l < Setkeys(_st.Table_Left_Title).length; l++) {
if (lft[l] == 'WorkRatio') {
_dr[l - 1][keyname] = _dr[2][keyname] / 8;
} else if (lft[l] == 'TotalHour') {
_dr[l - 1][keyname] = _dr[0][keyname] * _dr[1][keyname] * _dr[2][keyname];
} else if (lft[l] == 'TotalPower') {
_dr[l - 1][keyname] = _dr[0][keyname] + _dr[1][keyname] + _dr[2][keyname] + _dr[3][keyname];
}
}
}
for (var d = 0; d < caculateTotal(_st).length; d++) {
if (Setkeys(_st.Table_Left_Title)[d + 1] != 'Area' && Setkeys(_st.Table_Left_Title)[d + 1] != 'Year') {
_dr[d].Total = Math.round(caculateTotal(_st)[d] * 100) / 100;
}
}
var all_data = [];
for (var s = 0; s <= _tb; s++) {
for (var d = 0; d < $scope.Survey_Table[s].Data_Row.length; d++) {
all_data.push($scope.Survey_Table[s].Data_Row[d]);
}
}
// Server Data
send_data = setMonthlyTableSendData($scope, _tb, _tth, all_data);
send_data.ID = _st.Table_Header[_tth].ID;
send_data.Area = $scope.Usage_Area_Selected;
send_data.LCAID = LCAID;
if (roi != 'WorkHour') {
send_data.Year = _st.Data_Row[year_row][_st.Table_Header[_tth].Keys[0]];
var options = $scope[roi + 'Options'];
send_data.ParameterID = parseInt(options[send_data.Area][Setkeys(options[send_data.Area])[0]][send_data.Year][0]);
}
if (typeof _st.Table_Header[_tth].ReferenceFileLink != 'undefined') {
send_data.ReferenceFileLink = _st.Table_Header[_tth].ReferenceFileLink;
}
console.log(send_data);
loadingDisable($timeout, $scope, true);
Save(MyLCAdata, send_data, roi, action, send_data.LCAID, send_data.ID).then(function (response) {
if (mergeNumber(response) != -1) {
for (var d = 0; d < _dr.length; d++) {
_dr[d].Data_Row_Edit[setKeyTitle(_st.Table_Header[_tth].Keys)].edit = false;
}
resetLCAState($scope);
loadingDisable($timeout, $scope, false);
if ($scope.MonthlyTableSelectArea) {
$scope.MonthlyTableSelectArea = false;
$route.reload();
}
}
});
} else {
_st.Data_Row[year_row].error_list = [];
for (var t = 0; t < _st.Table_Header.length; t++) {
if (t != _tth) _st.Data_Row[year_row].error_list.push(false);
else _st.Data_Row[year_row].error_list.push(true);
}
}
} else {
// 橫向編輯
var _dr = _st.Data_Row;
var _tr = -1;
var parent_material = null;
if (!$scope.Table_Child_Add_State && (typeof $scope.targetChildRow == 'undefined' || $scope.targetChildRow == -1)) {
_tr = _tdr;
if (typeof _dr[_tr].No != 'undefined') _dr[_tr].No = _tr + 1;
} else {
_dr = _st.Data_Row[_tdr].ChildMaterials;
parent_material = _st.Data_Row[_tdr];
_tr = _tdcr;
}
// 先判斷是否必填的都填了
var data_row = typeof $scope.dr != 'undefined' ? $scope.dr : _dr[_tr];
if (!checkTableEditValidation(data_row, _st.Table_Header)) return false;
send_data.ID = _dr[_tr].ID != null ? _dr[_tr].ID : 0;
for (var t = 0, _th = _st.Table_Header; t < _st.Table_Header.length; t++) {
var keyname = setKeyTitle(_th[t].Keys);
if (_th[t].Type != 'select') {
for (var dre in _dr[_tr].Data_Row_Edit) {
if (dre == keyname) {
_dr[_tr][keyname] = _dr[_tr].Data_Row_Edit[dre].data;
break;
}
}
}
}
if (roi == 'Waste' && $scope.radio_id == 0) {
if (_dr[_tr].Data_Row_Edit['WaterBOD'].data == null) _dr[_tr].Data_Row_Edit.WaterBOD.data = 0;
_dr[_tr].WaterBOD = _dr[_tr].Data_Row_Edit.WaterBOD.data;
if (_dr[_tr].Data_Row_Edit['WaterCOD'].data == null) _dr[_tr].Data_Row_Edit.WaterCOD.data = 0;
_dr[_tr].WaterCOD = _dr[_tr].Data_Row_Edit.WaterCOD.data;
}
// Server Data
for (var dre in _dr[_tr]) send_data[dre] = _dr[_tr][dre];
send_data.DQI = getCONSTANTSIndex(CONSTANTS, 'DQI', _dr[_tr].DQI);
if (parent_material != null) send_data.ParentMaterialID = parent_material.ID;
if (typeof _dr[_tr].InputParameterValue != 'undefined' && _dr[_tr].InputParameterValue != null) {
_dr[_tr].KgCO2e = _dr[_tr].Quantity * _dr[_tr].InputParameterValue * _dr[_tr].Scalar;
send_data.KgCO2e = _dr[_tr].KgCO2e;
} else {
if (_dr[_tr].ParameterID != null) {
if (_dr[_tr].ParameterValue != null) {
_dr[_tr].KgCO2e = _dr[_tr].Quantity * _dr[_tr].ParameterValue * _dr[_tr].Scalar;
send_data.KgCO2e = _dr[_tr].KgCO2e;
}
} else {
_dr[_tr].KgCO2e = 0;
send_data.KgCO2e = 0;
}
}
if (roi.indexOf('LCI') > -1 || roi.indexOf('High') > -1) send_data.HighLevelAnalyzeResult = send_data.KgCO2e;
send_data.LCAID = LCAID;
//var check_filled = checkFilled(_dr[_tr], _st.Table_Header);
//if (check_filled.all_filled) {
if (roi.indexOf('LCI') > -1 || roi.indexOf('High') > -1) {
if (roi == 'LCI_Direct_Material') {
send_data.MaterialType = 0;
} else if (roi == 'LCI_Indirect_Material') {
send_data.MaterialType = 1;
} else if (roi == 'LCI_Wrap_Material') {
send_data.MaterialType = 2;
} else if (roi.indexOf('High') > -1) {
send_data.MaterialType = getCONSTANTSIndex(CONSTANTS, 'MaterialType', send_data.MaterialType);
if (send_data.MaterialType == 1) {
if (send_data.Quantity == null) {
_dr[_tr].Quantity = 0;
send_data.Quantity = 0;
}
}
}
if (typeof _dr[_tr].InputParameterValue != 'undefined' && _dr[_tr].InputParameterValue != null && _dr[_tr].InputParameterValue != '') {
_dr[_tr].KgCO2e = _dr[_tr].Quantity * _dr[_tr].InputParameterValue * _dr[_tr].Scalar;
send_data.KgCO2e = _dr[_tr].KgCO2e;
}
if (typeof send_data.ParameterType != 'undefined' && send_data.ParameterType != '' && send_data.ParameterType != null) {
send_data.ParameterTypeID = $scope[roi + 'Options'][send_data.ParameterType][send_data.ParameterType][0];
}
if (typeof send_data.SupplierCompany != 'undefined' && send_data.SupplierCompany != '' && send_data.SupplierCompany != null) {
send_data.SupplierCompanyID = $scope.Supplier[send_data.SupplierCompany].ID;
}
console.log(send_data);
loadingDisable($timeout, $scope, true);
Save(MyLCAdata, send_data, 'Material', 'SaveMaterial', send_data.LCAID, send_data.ID).then(function (response) {
console.log(response);
if (mergeNumber(response) != -1) {
_dr[_tr].ID = mergeNumber(response);
_dr[_tr].rowDone = false;
resetLCAState($scope, _tr);
}
$scope.show_table_edit = false;
loadingDisable($timeout, $scope, false);
});
} else {
var controller = roi;
if (roi == 'Waste') {
for (var t = 0, _th = _st.Table_Header; t < _st.Table_Header.length; t++) {
if (setKeyTitle(_th[t].Keys) == 'WaterBOD') {
calculateWasteWaterScalar(_dr[_tr].Data_Row_Edit);
break;
}
}
_dr[_tr].Scalar = _dr[_tr].Data_Row_Edit.Scalar.data;
send_data.Scalar = _dr[_tr].Data_Row_Edit.Scalar.data;
} else if (roi == 'Vehicle') {
_dr[_tr].Type = getCONSTANTSIndex(CONSTANTS, 'Type', _dr[_tr].Type);
} else if (roi == 'Refrigerant') {
send_data.ParameterID = parseInt($scope[roi + 'Options1'][send_data.RefrigerantType][send_data.RefrigerantType][0]);
send_data.ParameterID2 = parseInt($scope[roi + 'Options2'][send_data.EquipmentType][send_data.EquipmentType][0]);
} else {
}
if (roi != 'Refrigerant') setParameterID(send_data, $scope);
var action = 'Save' + controller;
for (var t = 0, _th = _st.Table_Header; t < _st.Table_Header.length; t++) {
// 轉換 Select Tag 選項 string to int
var keyname = setKeyTitle(_th[t].Keys);
// Server Data
if (keyname != 'DQI') {
if (_th[t].Keys.length > 1) {
if (typeof send_data[_th[t].Keys[0]] == 'undefined') {
send_data[_th[t].Keys[0]] = {};
}
send_data[_th[t].Keys[0]][_th[t].Keys[1]] = _dr[_tr][keyname];
} else {
send_data[keyname] = _dr[_tr][keyname];
}
}
}
if (typeof $scope.radio_id != 'undefined') {
send_data.Type = $scope.radio_id;
}
setUncommonData(roi, _dr[_tr], send_data, CONSTANTS);
console.log(send_data);
if (roi != 'Refrigerant') {
var pid = -1;
for (var sda in send_data) {
if (sda.indexOf('ParameterID') > -1) {
pid = send_data[sda];
break;
}
}
loadingDisable($timeout, $scope, true);
Save(MyLCAdata, [pid], 'Parameter', 'getParameterValues').then(function (response) {
_dr[_tdr].KgCO2e = _dr[_tdr].Scalar * response.value[pid];
send_data.KgCO2e = _dr[_tdr].KgCO2e;
})
.then(function () {
Save(MyLCAdata, send_data, controller, action, send_data.LCAID, send_data.ID).then(function (response) {
if (mergeNumber(response) != -1) {
_dr[_tr].ID = mergeNumber(response);
_dr[_tr].rowDone = false;
resetLCAState($scope, _tr);
}
$scope.show_table_edit = false;
loadingDisable($timeout, $scope, false);
});
});
} else {
var pid1 = send_data.ParameterID != null ? send_data.ParameterID : -1;
var pid2 = send_data.ParameterID2 != null ? send_data.ParameterID2 : -1;
if (pid1 != -1 && pid2 != -1) {
var date_range = getDataRange($scope.product_survey.LCA, CONSTANTS);
var pv1 = 0;
var pv2 = 0;
loadingDisable($timeout, $scope, true);
Save(MyLCAdata, [pid1], 'Parameter', 'getParameterValues').then(function (response) {
if (response.value[pid1] != -1) {
pv1 = response.value[pid1];
};
return pv1;
})
.then(function () {
return Save(MyLCAdata, [pid2], 'Parameter', 'getParameterValues').then(function (response) {
if (response.value[pid2] != -1) {
pv2 = response.value[pid2];
_st.Data_Row[_tdr].KgCO2e = send_data.Scalar * pv1 * pv2 * date_range;
send_data.KgCO2e = _st.Data_Row[_tdr].KgCO2e;
}
});
})
.then(function () {
console.log(send_data);
Save(MyLCAdata, send_data, controller, action, send_data.LCAID, send_data.ID).then(function (response) {
if (mergeNumber(response) != -1) {
_dr[_tr].ID = mergeNumber(response);
_dr[_tr].rowDone = false;
resetLCAState($scope, _tr);
}
$scope.show_table_edit = false;
loadingDisable($timeout, $scope, false);
});
});
}
}
}
//} else {
// // 資料輸入不正確或未填
// _dr[_tr].error_list = check_filled.error_list;
//}
}
}
$scope.parentCantEdit = function () {
var roi = $scope.current_item;
if (typeof roi != 'undefined' && typeof $scope.LCA_Status_Now != 'undefined') {
if ((roi.indexOf('LCI') > -1 || roi.indexOf('High') > -1) && ($scope.LCA_Status_Now > 0 && $scope.LCA_Status_Now != 3)) {
if (typeof $scope.targetChildRow != 'undefined' && $scope.targetChildRow != -1) return false;
else return true;
}
}
return false;
}
$scope.undelete = function () {
var roi = $scope.current_item;
if (typeof roi != 'undefined') {
if ((roi.indexOf('LCI') > -1 || roi.indexOf('High') > -1) && ($scope.LCA_Status_Now > 0 && $scope.LCA_Status_Now != 3)) return false;
else return true;
}
}
$scope.enableAdd = function () {
var roi = $scope.current_item;
var LSN = $scope.LCA_Status_Now;
if (typeof roi != 'undefined') {
if ((roi.indexOf('LCI') > -1 || roi.indexOf('High') > -1) && LSN != 0 && LSN != 3) return false;
else if (roi.indexOf('High') > -1 && LSN > 0) return false;
else return true;
}
}
$scope.DownloadCSV = function () {
var csv = '\uFEFF';
var roi = $scope.current_item;
var data_response_name = getCacheName(roi);
var LCAID = typeof $cookieStore.get('LCAID') != 'undefined' ? $cookieStore.get('LCAID') : $scope.product_survey.LCA.ID;
var api_config = { Controller: roi, Action: 'Get' + roi + 's', pa1: LCAID };
if (roi.indexOf('LCI') > -1 || roi.indexOf('High') > -1) {
api_config.Controller = 'Material';
api_config.Action = 'GetMaterials';
}
MyLCAdata.get(api_config, { isArray: false }).$promise.then(function (response) {
setTableSelectOptionsToScope(response, $scope);
for (var st = 0, _st = $scope.Survey_Table; st < $scope.Survey_Table.length; st++) {
var all_header = $scope.oi[$scope.current_item_id].Table_Header[st];
var table_header = [];
var table_row = [];
var radio = [$scope.current_item];
if (typeof _st[st].item_radio_content != 'undefined') {
radio = [];
for (var rad in _st[st].item_radio_content[0]) {
radio.push(rad);
}
}
for (var r = 0; r < radio.length; r++) {
table_header = setRadioSingleHeader(all_header, radio, r);
if (roi == 'WorkHour' || roi.indexOf('Usage') > -1) {
table_row = setCSVSingleRow(roi, _st[st].Data_Row, table_header, r);
} else {
table_row = setCSVSingleRow(roi, $scope[data_response_name], table_header, r);
}
setSelect(roi, $scope, CONSTANTS, table_row, table_header, r);
csv += setCSVSingleTable(_st[st].Table_Left_Title, table_header, table_row, $scope.Supplier);
}
}
var downloadLink = createCSVDownloadLink(csv, $scope.current_item);
downloadLink.click();
});
}
$scope.showUpload = function () {
var lsn = typeof $scope.LCA_Status_Now != 'undefined' ? $scope.LCA_Status_Now : 0;
var roi = $scope.current_item;
if (lsn == 2) return false;
if (lsn == 1 && (roi.indexOf('LCI') > -1 || roi.indexOf('High') > -1)) return false;
if (lsn == 3 && roi.indexOf('High') > -1) return false;
return true;
}
$scope.showQuote = function () {
}
$scope.UploadCSV = function () {
var upload = createCSVUploadElement();
upload.onchange = function () {
$scope.UploadCSVFiles(this.files);
}
upload.click();
}
$scope.UploadCSVFiles = function (files) {
if (confirm(CONSTANTS.AlertMessage[15] + '\n\n ' + files[0].name + '?')) {
if (files[0].name.indexOf('.csv') > -1) {
var reader = new FileReader();
reader.onload = function (e) {
var _st = $scope.Survey_Table;
var roi = $scope.current_item;
var result = [];
// 因為 Description 會有斷行的顯示方式,因此無法使用通用型 split function
if (!$scope.Schedule_Table) {
result = splitCSVFileData(e.target.result, _st);
console.log(result);
} else {
result = e.target.result.split(/[\r\n]+/g);
}
// 從header開始解析
var result_header = !$scope.Schedule_Table ? result[0] : CSVtoArray(result[0]);
console.log(result_header);
var leftheader = checkCSVFileHeader($scope, result_header, CONSTANTS.AlertMessage[0]);
if (!leftheader && typeof leftheader == 'boolean') return false;
var LCAID = typeof $cookieStore.get('LCAID') != 'undefined' ? $cookieStore.get('LCAID') : $scope.product_survey.LCA.ID;
// 無論是否有資料列,下拉式選單必重新讀取
var api_config = setApiConfig(roi, LCAID);
var parameter_api_config = api_config.parameter_api_config
var delete_api_config = api_config.delete_api_config;
var save_all_api_config = api_config.save_all_api_config;
// 資料陣列初始化
for (var s = 0; s < _st.length; s++) _st[s].Data_Row = [];
var st = -1;
var change_table = 0;
var radio_count = 0;
var null_row = 0;
var all_data = [];
// 轉換CSV資料給 all_data
for (var r = 0; r < result.length; r++) {
var monthly_row = 0;
var table_header = setCSVSingleHeader($scope, st, radio_count);
var row = !$scope.Schedule_Table ? result[r] : CSVtoArray(result[r]);
var result_row = checkDataRowforTable(row, _st, st, $scope, leftheader, radio_count);
console.log(result_row);
var rc = getRadioTableCount(result_row, $scope.oi[$scope.current_item_id]);
radio_count += rc.count;
if (typeof result_row != 'undefined' && (result_row == null || (rc.check_header || isNullData(result_row, '')))) result_row = null;
// 重新擷取標題列
if (typeof result_row != 'undefined' && result_row != null) {
if (leftheader > 0) {
setCSVDataToDataRow(result_row, r - null_row + st + monthly_row - 1, all_data, table_header, Setkeys(_st[st].Table_Left_Title)[r - change_table], radio_count);
if (Setkeys(_st[st].Table_Left_Title)[r - change_table] == 'AverageHourPerDay' || Setkeys(_st[st].Table_Left_Title)[r - change_table] == 'OffPeak') {
for (var tlt in _st[st].Table_Left_Title) {
if (tlt == 'WorkRatio' || tlt == 'TotalHour' || tlt == 'TotalPower') {
monthly_row++;
setCSVDataToDataRow(result_row, r - null_row + st + monthly_row - 1, all_data, table_header, tlt);
}
}
}
} else {
setCSVDataToDataRow(result_row, r - change_table - null_row - 1, all_data, table_header, null, radio_count, $scope.Supplier);
}
} else if (typeof result_row == 'undefined') {
st++;
change_table = r;
} else if (result_row == null) {
null_row++;
}
}
// 將全部資料列加入編輯列,供 Server 存檔使用
// 原先存檔是用 Data_Row但因為有 Multi-Table 的問題
// 所以在匯入 BOM 表的時候改採用 All_Data
for (var s = 0; s < _st.length; s++) {
for (var d = 0, _dr = all_data; d < all_data.length; d++) {
_dr[d].Data_Row_Edit = SetEditState(_dr[d]);
for (var dre in _dr[d].Data_Row_Edit) {
_dr[d].Data_Row_Edit[dre].data = _dr[d][dre] != 'undefined' ? _dr[d][dre] : null;
}
}
}
// 重新擷取下拉式選單陣列
if (roi != 'WorkHour' && roi.indexOf('Usage') == -1) {
var dr = [];
if (typeof $scope.radio_id != 'undefined') {
for (var r = 0; r < Setkeys($scope.oi[$scope.current_item_id].Radio[0][0]).length; r++) {
dr.push([]);
for (var d = 0; d < all_data.length; d++) {
if (all_data[d].Type == r) dr[r].push(all_data[d]);
}
}
for (var k = 0; k < dr.length; k++) {
var table_header = setCSVSingleHeader($scope, 0, dr[k][0].Type);
setSelect(roi, $scope, CONSTANTS, dr[k], table_header, dr[k][0].Type);
}
} else {
setSelect(roi, $scope, CONSTANTS, all_data, table_header, $scope.radio_id);
}
}
// 將資料列轉換成送給Server儲存用的send_data_array
var send_data_array = [];
var send_data_count = 0;
var send_data_parameter_ID_array = [];
var insert_row = [];
for (var s = 0; s < _st.length; s++) {
if (roi != 'WorkHour' && roi.indexOf('Usage') == -1) {
for (var d = 0, _dr = all_data; d < all_data.length; d++) {
var table_header = setCSVSingleHeader($scope, s, _dr[d].Type);
var check_filled = checkFilled(_dr[d], table_header);
if (check_filled.all_filled) {
insert_row.push(d);
send_data_array.push({});
if (typeof all_data[d].Type != 'undefined') send_data_array[send_data_count].Type = all_data[d].Type;
for (var dre in _dr[d].Data_Row_Edit) {
if (dre == 'DQI' || dre == 'MaterialType' || (dre == 'Type' && roi == 'Vehicle')) {
send_data_array[send_data_count][dre] = getCONSTANTSIndex(CONSTANTS, dre, _dr[d].Data_Row_Edit[dre].data);
} else {
send_data_array[send_data_count][dre] = _dr[d].Data_Row_Edit[dre].data;
}
}
send_data_array[send_data_count].LCAID = LCAID;
send_data_array[send_data_count].ID = 0;
if (roi.indexOf('Direct') > -1) send_data_array[send_data_count].MaterialType = 0;
else if (roi.indexOf('Indirect') > -1) send_data_array[send_data_count].MaterialType = 1;
else if (roi.indexOf('Wrap') > -1) send_data_array[send_data_count].MaterialType = 2;
if (typeof _dr[d].ParameterType != 'undefined') {
for (var p = 0; p < _dr[d].ParameterTypeselect.length; p++) {
if (send_data_array[send_data_count].ParameterType == _dr[d].ParameterTypeselect[p]) {
send_data_array[send_data_count].ParameterTypeID = p + 1;
}
}
}
if (roi != 'Refrigerant') {
var PID = setParameterID(_dr[d], $scope);
setParameterID(send_data_array[send_data_count], $scope);
send_data_parameter_ID_array.push(PID);
} else {
_dr[d].ParameterID = $scope[roi + 'Options1'][send_data_array[send_data_count].RefrigerantType][send_data_array[send_data_count].RefrigerantType][0];
_dr[d].ParameterID2 = $scope[roi + 'Options2'][send_data_array[send_data_count].EquipmentType][send_data_array[send_data_count].EquipmentType][0];
send_data_array[send_data_count].ParameterID = _dr[d].ParameterID;
send_data_array[send_data_count].ParameterID2 = _dr[d].ParameterID2;
send_data_parameter_ID_array.push(_dr[d].ParameterID);
send_data_parameter_ID_array.push(_dr[d].ParameterID2);
}
setUncommonData(roi, _dr[d], send_data_array[send_data_count], CONSTANTS);
send_data_count++;
} else {
// 資料輸入不正確或未填
var error_list = check_filled.error_list;
}
}
} else {
var table_header = setCSVSingleHeader($scope, s);
for (var t = 0; t < table_header.length; t++) {
if (table_header[t].Name != 'Total') {
var count = 0;
for (var d = 0; d < $scope.tempcount; d++) {
var row_length = 5;
count += all_data[d + s * row_length][setKeyTitle(table_header[t].Keys)];
}
if (count > 0) {
send_data_array.push(setMonthlyTableSendData($scope, s, t, all_data));
}
}
}
}
}
loadingDisable($timeout, $scope, true);
Save(MyLCAdata, send_data_parameter_ID_array, 'Parameter', 'getParameterValues').then(function (response) {
if (Setkeys(response.value).length > 0) {
var PIDname1 = 'ParameterID';
var PIDname2 = 'ParameterID2';
var _dr = all_data;
if (roi != 'Refrigerant') {
for (var d = 0; d < insert_row.length; d++) {
if (typeof _dr[insert_row[d]].ParameterID != 'undefined') PIDname1 = 'ParameterID';
var quantity = 1;
if (typeof _dr[insert_row[d]].Quantity != 'undefined') quantity = _dr[insert_row[d]].Quantity;
_dr[insert_row[d]].KgCO2e = quantity * response.value[_dr[insert_row[d]][PIDname1]] * _dr[insert_row[d]].Scalar;
send_data_array[d].KgCO2e = _dr[insert_row[d]].KgCO2e;
}
} else {
var date_range = getDataRange($scope.product_survey.LCA, CONSTANTS);
for (var d = 0; d < insert_row.length; d++) {
_dr[insert_row[d]].KgCO2e = _dr[insert_row[d]].Scalar * response.value[_dr[insert_row[d]][PIDname1]] * response.value[_dr[insert_row[d]][PIDname2]] * date_range;
send_data_array[d].KgCO2e = _dr[insert_row[d]].KgCO2e;
}
}
}
})
.then(function () {
MyLCAdata.delete(delete_api_config).$promise.then(function (response) {
for (var d = 0; d < send_data_array.length; d++)
delete send_data_array[d].Data_Row_Edit;
})
.then(function () {
console.log(send_data_array);
MyLCAdata.save(save_all_api_config, send_data_array).$promise.then(function (response) {
console.log(response);
$route.reload();
});
});
});
}
reader.onerror = function (stuff) {
console.log('error', stuff);
console.log(stuff.getMessage());
}
reader.readAsText(files[0], 'BIG5');
} else {
alert(CONSTANTS.AlertMessage[1]);
}
} else {
}
}
$scope.showSimapro = function () {
var roi = $scope.current_item;
var _st = $scope.Survey_Table;
if (typeof roi != 'undefined' && (roi.indexOf('LCI') > -1 || roi.indexOf('High') > -1) && typeof _st != 'undefined') {
var _dr = _st[0].Data_Row;
if ($scope.Table_Edit_State && !$scope.Schedule_Table) {
return false;
} else if ($scope.LCA_Status_Now == 0 && typeof $scope.targetRow != 'undefined' && $scope.targetRow != -1) {
return true;
} else if ($scope.LCA_Status_Now == 1) {
if (typeof $scope.targetChildRow != 'undefined' && $scope.targetChildRow != -1) {
if (!_dr[$scope.targetRow].IncludedInInterrogation || _dr[$scope.targetRow].SupplierCompanyEmail == null || _dr[$scope.targetRow].SupplierCompanyEmail == '') {
return true;
}
}
} else if ($scope.LCA_Status_Now == 3) {
if (roi.indexOf('High') == -1) {
if (typeof $scope.targetChildRow != 'undefined' && $scope.targetChildRow != -1) {
if (!_dr[$scope.targetRow].IncludedInInterrogation || _dr[$scope.targetRow].SupplierCompanyEmail == null || _dr[$scope.targetRow].SupplierCompanyEmail == '') {
return true;
}
}
}
}
}
return false;
}
$scope.CollapseAllChild = function () {
for (var d = 0, _dr = $scope.Survey_Table[0].Data_Row; d < $scope.Survey_Table[0].Data_Row.length; d++) {
_dr[d].child_expand = false;
}
}
$scope.isMaterial = function () {
if (typeof $scope.current_item != 'undefined') {
if ($scope.current_item.indexOf('LCI') > -1) return true;
else return false;
}
}
$scope.PCRforHighLevelCalculate = function () {
for (var d = 0; d < $scope.Survey_Table[0].Data_Row.length; d++) {
if ($scope.Survey_Table[0].Data_Row[d].ParameterID == null) {
alert(CONSTANTS.AlertMessage[19]);
return false;
}
}
loadingDisable($timeout, $scope, true);
if (typeof $scope.selectPCRforHighLevelCalculate != 'undefined') {
$scope.selectPCRforHighLevelCalculate = !$scope.selectPCRforHighLevelCalculate;
} else {
$scope.selectPCRforHighLevelCalculate = true;
}
var _th = $scope.Survey_Table[0].Table_Header;
if ($scope.selectPCRforHighLevelCalculate) {
if (_th[0].Type != 'checkbox') {
_th.splice(0, 0, { "Name": "", "Keys": ["IncludedInInterrogation"], "Type": "checkbox", "Width": 40 });
}
$scope.Survey_Table[0].Data_Row = $filter('orderBy')($scope.Survey_Table[0].Data_Row, 'KgCO2e', true);
// 做KgCO2e的自動選擇
$scope.KgCO2e_total_count = 0;
$scope.KgCO2e_select_count = 0;
for (var d = 0, _dr = $scope.Survey_Table[0].Data_Row; d < $scope.Survey_Table[0].Data_Row.length; d++) {
$scope.KgCO2e_total_count += _dr[d].KgCO2e;
}
$scope.KgCO2e_total_count = parseFloat(Math.round($scope.KgCO2e_total_count * 100)) / 100;
if ($cookieStore.get('PCRID') != null) {
getPublicPCR(MyLCAdata, $scope).then(function (res) {
for (var r = 0; r < res.length; r++) {
if ($cookieStore.get('PCRID') == res[r].ID) {
$scope.PCR_percentage = res[r].Percentage;
loadingDisable($timeout, $scope, false);
break;
}
}
setKgCO2eCount();
});
} else {
$scope.PCR_percentage = 100;
setKgCO2eCount();
loadingDisable($timeout, $scope, false);
}
} else {
_th.splice(0, 1);
loadingDisable($timeout, $scope, false);
}
$scope.Survey_Table[0].Table_Width = SetTableTotalWidth($scope.Survey_Table[0].Table_Header, null, 0);
}
function setKgCO2eCount() {
var count_KgCO2e = 0;
var send_data_array = [];
var _dr = $scope.Survey_Table[0].Data_Row;
if (_dr.length > 0 && (typeof $scope.show_table_edit == 'undefined' || !$scope.show_table_edit)) {
count_KgCO2e += _dr[0].KgCO2e;
$scope.KgCO2e_select_count = count_KgCO2e;
_dr[0].IncludedInInterrogation = true;
send_data_array.push($scope.resetMaterial(0));
for (var d = 1; d < _dr.length; d++) {
if ((count_KgCO2e + _dr[d].KgCO2e) / $scope.KgCO2e_total_count * 100 < $scope.PCR_percentage) {
count_KgCO2e += _dr[d].KgCO2e;
$scope.KgCO2e_select_count = count_KgCO2e;
_dr[d].IncludedInInterrogation = true;
send_data_array.push($scope.resetMaterial(d));
} else {
count_KgCO2e += _dr[d].KgCO2e;
_dr[d].IncludedInInterrogation = false;
send_data_array.push($scope.resetMaterial(d));
}
}
console.log(send_data_array);
Save(MyLCAdata, send_data_array, 'Material', 'SaveMaterials').then(function (res) {
}).then(function () {
$scope.KgCO2e_select_count = parseFloat(Math.round($scope.KgCO2e_select_count * 100)) / 100;
compare($scope);
});
}
}
$scope.toNextLCAStatus = function (status) {
loadingDisable($timeout, $scope, true);
switch (status) {
case 0:
break;
case 1:
if ($scope.LCA_Status_Now != 1 && $scope.LCA_Status_Now != 3) {
loadingDisable($timeout, $scope, false);
alert(CONSTANTS.AlertMessage[2]);
return false;
}
var LCAID = typeof $cookieStore.get('LCAID') != 'undefined' ? $cookieStore.get('LCAID') : $scope.product_survey.LCA.ID;
var status_api_config = { Controller: 'LCA', Action: 'ProcessWaiting', pa1: LCAID };
if ($scope.LCA_Status_Now == 1) {
if (confirm(CONSTANTS.AlertMessage[3])) {
if (!$scope.isProductLCA) status_api_config.Action = 'OrganizationProcessWaiting';
MyLCAdata.save(status_api_config).$promise.then(function (response) {
loadingDisable($timeout, $scope, false);
if (mergeNumber(response) == 200) {
alert(CONSTANTS.AlertMessage[4]);
redirect.backToMainPage();
} else {
alert(CONSTANTS.AlertMessage[6]);
}
});
} else {
loadingDisable($timeout, $scope, false);
}
} else if ($scope.LCA_Status_Now == 3) {
if ($scope.isProductLCA) status_api_config.Action = 'RejectProcessing';
else status_api_config.Action = 'OrganizationRejectProcessing';
if (confirm(CONSTANTS.AlertMessage[3])) {
MyLCAdata.save(status_api_config).$promise.then(function (response) {
loadingDisable($timeout, $scope, false);
if (mergeNumber(response) == 200) {
alert(CONSTANTS.AlertMessage[5]);
} else {
alert(CONSTANTS.AlertMessage[6]);
}
}).then(function () {
if ($scope.isProductLCA) status_api_config.Action = 'ProcessWaiting';
else status_api_config.Action = 'OrganizationProcessWaiting';
MyLCAdata.save(status_api_config).$promise.then(function (response) {
loadingDisable($timeout, $scope, false);
if (mergeNumber(response) == 200) {
alert(CONSTANTS.AlertMessage[4]);
redirect.backToMainPage();
} else {
alert(CONSTANTS.AlertMessage[6]);
}
});
});
} else {
loadingDisable($timeout, $scope, false);
}
}
break;
case 5:
if ($scope.LCA_Status_Now != 0 && $scope.LCA_Status_Now != 4 && $scope.Survey_Table[0].Data_Row.length == 0) {
loadingDisable($timeout, $scope, false);
alert(CONSTANTS.AlertMessage[7]);
return false;
}
if (confirm(CONSTANTS.AlertMessage[14])) {
var LCAID = typeof $cookieStore.get('LCAID') != 'undefined' ? $cookieStore.get('LCAID') : $scope.product_survey.LCA.ID;
var status_api_config = { Controller: 'LCA', Action: 'NewCompleting', pa1: LCAID };
if (!$scope.isProductLCA) status_api_config.Action = 'OrganizationNewCompleting';
MyLCAdata.save(status_api_config).$promise.then(function (response) {
loadingDisable($timeout, $scope, false);
if (mergeNumber(response) == 200) {
alert(CONSTANTS.AlertMessage[8]);
// 跳轉至報表頁面
redirect.backToMainPage();
} else {
alert(CONSTANTS.AlertMessage[6]);
}
});
} else {
loadingDisable($timeout, $scope, false);
}
break;
}
}
$scope.notMonthlyTable = function () {
var roi = $scope.current_item;
if (typeof roi != 'undefined') {
if (roi == 'WorkHour' || roi.indexOf('Usage') > -1) return false;
else return true;
}
}
$scope.HighLevelCalculateDone = function () {
// 顯示已挑選之總和及全部總和
var _dr = $scope.Survey_Table[0].Data_Row;
if (_dr.length > 0) {
if ($scope.KgCO2e_select_count / $scope.KgCO2e_total_count * 100 >= $scope.PCR_percentage) {
if (confirm(CONSTANTS.AlertMessage[9])) {
var LCAID = typeof $cookieStore.get('LCAID') != 'undefined' ? $cookieStore.get('LCAID') : $scope.product_survey.LCA.ID;
Save(MyLCAdata, null, 'LCA', 'NewProcessing', LCAID).then(function (response) {
resetLCAState($scope);
$scope.LCA_Status_Now = 1;
$cookieStore.put('LCA_Status_Now', 1);
$scope.PCRforHighLevelCalculate();
$scope.sidebar = setSidebar(order_items[$scope.current_order].Item, order_items[$scope.current_order].Item.length);
loadingDisable($timeout, $scope, false);
});
} else {
loadingDisable($timeout, $scope, false);
}
} else {
loadingDisable($timeout, $scope, false);
alert(CONSTANTS.AlertMessage[10]);
}
} else {
loadingDisable($timeout, $scope, false);
alert(CONSTANTS.AlertMessage[11]);
}
}
// 選了地區以後要將所有資料的地區都替換掉,並且把年份欄位刪除
$scope.UsageTableAreaSelect = function () {
$scope.MonthlyTableSelectArea = true;
var area = $scope.Usage_Area_Selected;
var roi = $scope.current_item;
var year_row = 1;
var _st = $scope.Survey_Table[0];
var _dr = _st.Data_Row;
var _th = _st.Table_Header;
var _tth = $scope.targetColumn;
var options = $scope[roi + 'Options'];
if (roi.indexOf('Power') > -1) year_row = 5;
if (area != null) {
for (var th = 0; th < _th.length; th++) {
var keyname = setKeyTitle(_th[th].Keys);
_dr[year_row][keyname + 'select'] = setSelectOptions(options[area][Setkeys(options[area])[0]]);
}
}
var years = _dr[year_row][keyname + 'select'];
var LCAID = typeof $cookieStore.get('LCAID') != 'undefined' ? $cookieStore.get('LCAID') : $scope.product_survey.LCA.ID;
Get(MyLCAdata, false, roi, 'Get' + roi + 's', LCAID).then(function (response) {
return response[roi + 's'];
}).then(function (server_data) {
var send_data_array = [];
for (var sd = 0; sd < server_data.length; sd++) {
for (var th = 0; th < _th.length; th++) {
if (_th[th].Name != 'Total' && server_data[sd].Index == th) {
var year = parseInt(_th[th].Keys[0].substr(0, 4));
var row = {};
var new_row = true;
for (var r = 0; r < server_data.length; r++) {
if (th == server_data[r].Index) {
row = server_data[r];
new_row = false;
}
}
if (new_row) {
for (var tlt = 1, _tlt = Setkeys(_st.Table_Left_Title) ; tlt < Setkeys(_st.Table_Left_Title).length; tlt++) {
row[_tlt[tlt]] = 0;
}
row.ID = 0;
row.LCAID = LCAID;
row.Index = th;
}
row.Area = area;
for (var y = years.length - 1; y >= 0; y--) {
if (year >= years[y]) {
row.Year = years[y];
break;
}
}
row.ParameterID = parseInt(options[row.Area][Setkeys(options[row.Area])[0]][row.Year][0]);
send_data_array.push(row);
}
}
}
// 一次儲存全部
if ($scope.Table_Edit_State) {
for (var tlt = 1, _tlt = Setkeys(_st.Table_Left_Title) ; tlt < Setkeys(_st.Table_Left_Title).length; tlt++) {
if (_tlt[tlt].indexOf('Total') == -1 && _tlt[tlt] != 'Year') {
for (var sda = 0; sda < send_data_array.length; sda++) {
if (_tth == send_data_array[sda].Index) {
send_data_array[sda][_tlt[tlt]] = _st.Data_Row[tlt - 1].Data_Row_Edit[_th[$scope.targetColumn].Keys[0]].data;
}
}
}
}
var new_month = true;
for (var s = 0; s < send_data_array.length; s++) {
if (_tth == send_data_array[s].Index) new_month = false;
_dr[_dr.length - 1][_th[send_data_array[s].Index].Keys[0]] = send_data_array[s].Year;
}
if (new_month) {
if (send_data_array.length > 0) {
_dr[_dr.length - 1][_th[_tth].Keys[0]] = send_data_array[0].Year;
} else {
var year = parseInt(_th[_tth].Keys[0].substr(0, 4));
for (var y = years.length - 1; y >= 0; y--) {
if (year >= years[y]) {
_dr[_dr.length - 1][_th[_tth].Keys[0]] = years[y];
break;
}
}
}
}
for (var s = 0; s < send_data_array.length; s++) {
if (send_data_array[s].Index == $scope.targetColumn) send_data_array.splice(s, 1);
}
}
Save(MyLCAdata, send_data_array, roi, 'Save' + roi + 's').then(function (response) {
if (!$scope.Table_Edit_State) $route.reload();
})
});
}
$scope.quoteCurrentTableData = function () {
$scope.show_popout = true;
$scope.show_quote = true;
}
},
templateUrl: CONSTANTS.TB + 'header_content'
};
})