示例#1
0
        public static string ToStringNotFormatted4(this double input)
        {
            string result = string.Empty;

            result = input.ToString("N", BaseHelper.GetNumberFormatInfo("", ",", 4));

            return(result);
        }
示例#2
0
        public static string ToStringNotFormatted4(this double?input)
        {
            string result = string.Empty;

            if (input.HasValue)
            {
                result = input.Value.ToString("N", BaseHelper.GetNumberFormatInfo("", ",", 4));
            }

            return(result);
        }
        public CallContext ImportProductivityAndScrapCostData(string fileFullName, int idEntity, CallContext resultContext)
        {
            try
            {
                resultContext.ResultCode = ETEMEnums.ResultEnum.Error;

                FileInfo excelFile = new FileInfo(fileFullName);

                using (ExcelPackage package = new ExcelPackage(excelFile))
                {
                    int currRow = 1;
                    int currCol = 0;

                    bool    res;
                    decimal resultParseDecimal;
                    int     resultParseInt;

                    ExcelWorksheet workSheet = package.Workbook.Worksheets[1];

                    if (workSheet == null)
                    {
                        resultContext.Message = "Error! No Excel work sheet with `Productivity and Scrap` data!";
                        return(resultContext);
                    }

                    ProductivityAndScrap productivityAndScrap = this.GetEntityById(idEntity);

                    if (productivityAndScrap == null)
                    {
                        resultContext.Message = "Entity `ProductivityAndScrap` not found by ID (`" + idEntity + "`)!";
                        return(resultContext);
                    }

                    List <KeyValue>       listKeyValuesToCostCenterPresses = new List <KeyValue>();
                    List <ProfileSetting> listProfileSettings = new List <ProfileSetting>();

                    List <string> listKeyTypeIntCodes = new List <string>()
                    {
                        ETEMEnums.KeyTypeEnum.CostCenter.ToString()
                    };

                    listKeyValuesToCostCenterPresses = (from kv in this.dbContext.KeyValues
                                                        join kt in this.dbContext.KeyTypes on kv.idKeyType equals kt.idKeyType
                                                        where listKeyTypeIntCodes.Contains(kt.KeyTypeIntCode)
                                                        select kv).ToList <KeyValue>();

                    listProfileSettings = (from ps in this.dbContext.ProfileSettings
                                           select ps).ToList();

                    List <ProductivityAndScrapDetail> listProductivityAndScrapDetailOld = new List <ProductivityAndScrapDetail>();
                    List <ProductivityAndScrapDetail> listProductivityAndScrapDetailNew = new List <ProductivityAndScrapDetail>();

                    listProductivityAndScrapDetailOld = (from psd in this.dbContext.ProductivityAndScrapDetails
                                                         where psd.idProductivityAndScrap == productivityAndScrap.idProductivityAndScrap
                                                         select psd).ToList <ProductivityAndScrapDetail>();

                    Dictionary <string, string> dictErrorsProductivityAndScrapDetails = new Dictionary <string, string>();

                    ProductivityAndScrapDetail newProductivityAndScrapDetail = new ProductivityAndScrapDetail();

                    bool hasNotErrorInRow = true;

                    string rangeValueStr = string.Empty;

                    ExcelRange range;

                    for (; ;)
                    {
                        currRow++;

                        hasNotErrorInRow = true;

                        if (string.IsNullOrWhiteSpace(workSheet.Cells[currRow, 1].Text) &&
                            string.IsNullOrWhiteSpace(workSheet.Cells[currRow, 2].Text) &&
                            string.IsNullOrWhiteSpace(workSheet.Cells[currRow, 3].Text) &&
                            string.IsNullOrWhiteSpace(workSheet.Cells[currRow, 4].Text) &&
                            string.IsNullOrWhiteSpace(workSheet.Cells[currRow, 5].Text) &&
                            string.IsNullOrWhiteSpace(workSheet.Cells[currRow, 6].Text))
                        {
                            break;
                        }

                        newProductivityAndScrapDetail = new ProductivityAndScrapDetail();

                        newProductivityAndScrapDetail.idProductivityAndScrap = idEntity;

                        currCol       = 1;
                        range         = workSheet.Cells[currRow, currCol];
                        rangeValueStr = (range.Value != null ? range.Value.ToString().Replace(" ", "") : string.Empty);

                        var kvPress = listKeyValuesToCostCenterPresses.Where(w => w.DefaultValue4 != null && w.DefaultValue4.Replace(" ", "").Trim().ToUpper() == rangeValueStr.Trim().ToUpper()).FirstOrDefault();

                        if (kvPress != null)
                        {
                            newProductivityAndScrapDetail.idCostCenter = kvPress.idKeyValue;
                        }
                        else
                        {
                            hasNotErrorInRow = false;
                            //if (dictErrorsProductivityAndScrapDetails.ContainsKey("CostCenterPresses"))
                            //{
                            //    dictErrorsProductivityAndScrapDetails["CostCenterPresses"] += "," + currRow;
                            //}
                            //else
                            //{
                            //    dictErrorsProductivityAndScrapDetails.Add("CostCenterPresses", currRow.ToString());
                            //}
                        }

                        currCol       = 2;
                        range         = workSheet.Cells[currRow, currCol];
                        rangeValueStr = (range.Value != null ? range.Value.ToString().Replace(" ", "") : string.Empty);

                        var profileSetting = listProfileSettings.Where(w => w.ProfileNameSAP != null && w.ProfileNameSAP.Replace(" ", "").Trim().ToUpper() == rangeValueStr.Trim().ToUpper()).FirstOrDefault();

                        if (profileSetting != null)
                        {
                            newProductivityAndScrapDetail.idProfileSetting = profileSetting.idProfileSetting;
                        }
                        else
                        {
                            hasNotErrorInRow = false;
                            //if (dictErrorsProductivityAndScrapDetails.ContainsKey("ProfileSetting"))
                            //{
                            //    dictErrorsProductivityAndScrapDetails["ProfileSetting"] += "," + currRow;
                            //}
                            //else
                            //{
                            //    dictErrorsProductivityAndScrapDetails.Add("ProfileSetting", currRow.ToString());
                            //}
                        }

                        currCol       = 3;
                        range         = workSheet.Cells[currRow, currCol];
                        rangeValueStr = (range.Value != null ? range.Value.ToString() : string.Empty);

                        res = Decimal.TryParse(rangeValueStr, NumberStyles.Any, BaseHelper.GetNumberFormatInfo("", ".", 9), out resultParseDecimal);
                        if (res)
                        {
                            newProductivityAndScrapDetail.SumOfHours = resultParseDecimal;
                        }
                        else
                        {
                            newProductivityAndScrapDetail.SumOfHours = decimal.Zero;
                        }

                        currCol       = 4;
                        range         = workSheet.Cells[currRow, currCol];
                        rangeValueStr = (range.Value != null ? range.Value.ToString() : string.Empty);

                        res = Decimal.TryParse(rangeValueStr, NumberStyles.Any, BaseHelper.GetNumberFormatInfo("", ".", 9), out resultParseDecimal);
                        if (res)
                        {
                            newProductivityAndScrapDetail.SumOfConsumption = resultParseDecimal;
                        }
                        else
                        {
                            newProductivityAndScrapDetail.SumOfConsumption = decimal.Zero;
                        }

                        currCol       = 5;
                        range         = workSheet.Cells[currRow, currCol];
                        rangeValueStr = (range.Value != null ? range.Value.ToString() : string.Empty);

                        res = Decimal.TryParse(rangeValueStr, NumberStyles.Any, BaseHelper.GetNumberFormatInfo("", ".", 9), out resultParseDecimal);
                        if (res)
                        {
                            newProductivityAndScrapDetail.SumOfProduction = resultParseDecimal;
                        }
                        else
                        {
                            newProductivityAndScrapDetail.SumOfProduction = decimal.Zero;
                        }

                        currCol       = 7;
                        range         = workSheet.Cells[currRow, currCol];
                        rangeValueStr = (range.Value != null ? range.Value.ToString() : string.Empty);

                        res = Decimal.TryParse(rangeValueStr, NumberStyles.Any, BaseHelper.GetNumberFormatInfo("", ".", 9), out resultParseDecimal);
                        if (res)
                        {
                            newProductivityAndScrapDetail.ProductivityKGh = resultParseDecimal;
                        }
                        else
                        {
                            newProductivityAndScrapDetail.ProductivityKGh = decimal.Zero;
                        }

                        currCol       = 8;
                        range         = workSheet.Cells[currRow, currCol];
                        rangeValueStr = (range.Value != null ? range.Value.ToString() : string.Empty);

                        res = Decimal.TryParse(rangeValueStr, NumberStyles.Any, BaseHelper.GetNumberFormatInfo("", ".", 9), out resultParseDecimal);
                        if (res)
                        {
                            newProductivityAndScrapDetail.ScrapRate = resultParseDecimal;
                        }
                        else
                        {
                            newProductivityAndScrapDetail.ScrapRate = decimal.Zero;
                        }

                        var checkProductivityAndScrapDetail = listProductivityAndScrapDetailOld.Where(w => w.idCostCenter == newProductivityAndScrapDetail.idCostCenter &&
                                                                                                      w.idProfileSetting == newProductivityAndScrapDetail.idProfileSetting).ToList();

                        if (checkProductivityAndScrapDetail.Count > 0)
                        {
                            hasNotErrorInRow = false;
                            if (dictErrorsProductivityAndScrapDetails.ContainsKey("DuplicateProductivityAndScrapDetail"))
                            {
                                dictErrorsProductivityAndScrapDetails["DuplicateProductivityAndScrapDetail"] += "," + currRow;
                            }
                            else
                            {
                                dictErrorsProductivityAndScrapDetails.Add("DuplicateProductivityAndScrapDetail", currRow.ToString());
                            }
                        }

                        if (hasNotErrorInRow)
                        {
                            listProductivityAndScrapDetailNew.Add(newProductivityAndScrapDetail);
                        }
                    }

                    if (dictErrorsProductivityAndScrapDetails.Count == 0)
                    {
                        resultContext = new ProductivityAndScrapDetailBL().EntitySave <ProductivityAndScrapDetail>(listProductivityAndScrapDetailNew, resultContext);
                        if (resultContext.ResultCode == ETEMEnums.ResultEnum.Success)
                        {
                            resultContext.ResultCode = ETEMEnums.ResultEnum.Success;
                            resultContext.Message    = "The productivity and scrap cost data have been imported successfully!";
                        }
                        else
                        {
                            resultContext.ResultCode = ETEMEnums.ResultEnum.Error;
                            resultContext.Message    = "Error import the productivity and scrap cost data!";
                        }
                    }
                    else
                    {
                        List <string> listErrors = new List <string>();

                        if (dictErrorsProductivityAndScrapDetails.ContainsKey("CostCenterPresses"))
                        {
                            listErrors.Add("Error! The field `PRESS - Short Text` is missing or in wrong format, Rows (" + dictErrorsProductivityAndScrapDetails["CostCenterPresses"] + ")!");
                        }
                        if (dictErrorsProductivityAndScrapDetails.ContainsKey("ProfileSetting"))
                        {
                            listErrors.Add("Error! The field `SHAPE` is missing or in wrong format, Rows (" + dictErrorsProductivityAndScrapDetails["ProfileSetting"] + ")!");
                        }
                        if (dictErrorsProductivityAndScrapDetails.ContainsKey("DuplicateProductivityAndScrapDetail"))
                        {
                            listErrors.Add("Error! The selected file includes productivity and scrap cost data with duplicate data in the database, Rows (" + dictErrorsProductivityAndScrapDetails["DuplicateProductivityAndScrapDetail"] + ")!");
                        }

                        resultContext.Message = string.Join(Constants.ERROR_MESSAGES_SEPARATOR, listErrors);
                    }
                }
            }
            catch (Exception ex)
            {
                resultContext.Message = "Error import the productivity and scrap cost data!";

                BaseHelper.Log("Error import entities `ProductivityAndScrapDetail`!");
                BaseHelper.Log(ex.Message);
                BaseHelper.Log(ex.StackTrace);
            }

            return(resultContext);
        }
示例#4
0
        public CallContext ImportSAPDataExpensesAndQuantities(string fileFullName, int idEntity, CallContext resultContext)
        {
            try
            {
                resultContext.ResultCode = ETEMEnums.ResultEnum.Error;

                FileInfo excelFile = new FileInfo(fileFullName);

                using (ExcelPackage package = new ExcelPackage(excelFile))
                {
                    int currRow = 1;
                    int currCol = 0;

                    bool    res;
                    decimal resultParseDecimal;
                    int     resultParseInt;

                    ExcelWorksheet workSheetExpenses = package.Workbook.Worksheets[1];
                    ExcelWorksheet workSheetQuantity = package.Workbook.Worksheets[2];

                    if (workSheetExpenses == null && workSheetQuantity == null)
                    {
                        resultContext.Message = "Error! No Excel work sheet `Expenses` and `Quantity`!";
                        return(resultContext);
                    }
                    else if (workSheetExpenses == null)
                    {
                        resultContext.Message = "Error! No Excel work sheet `Expenses`!";
                        return(resultContext);
                    }
                    else if (workSheetQuantity == null)
                    {
                        resultContext.Message = "Error! No Excel work sheet `Quantity`!";
                        return(resultContext);
                    }

                    SAPData _SAPData = this.GetEntityById(idEntity);

                    if (_SAPData == null)
                    {
                        resultContext.Message = "Entity `SAPData` not found by ID (`" + idEntity + "`)!";
                        return(resultContext);
                    }

                    List <string> listKeyTypeIntCodes = new List <string>()
                    {
                        ETEMEnums.KeyTypeEnum.CostCenter.ToString(),
                                  ETEMEnums.KeyTypeEnum.ExpensesType.ToString(),
                                  ETEMEnums.KeyTypeEnum.QuantityType.ToString()
                    };

                    List <KeyValueDataView> listKeyValuesToSAPDataExpensesAndQuantity = new List <KeyValueDataView>();
                    List <KeyValueDataView> listKeyValueCostCenter   = new List <KeyValueDataView>();
                    List <KeyValueDataView> listKeyValueExpensesType = new List <KeyValueDataView>();
                    List <KeyValueDataView> listKeyValueQuantityType = new List <KeyValueDataView>();

                    listKeyValuesToSAPDataExpensesAndQuantity = (from kv in this.dbContext.KeyValues
                                                                 join kt in this.dbContext.KeyTypes on kv.idKeyType equals kt.idKeyType
                                                                 where listKeyTypeIntCodes.Contains(kt.KeyTypeIntCode)
                                                                 select new KeyValueDataView
                    {
                        idKeyValue = kv.idKeyValue,
                        Name = kv.Name,
                        NameEN = kv.NameEN,
                        DefaultValue1 = kv.DefaultValue1,
                        KeyValueIntCode = kv.KeyValueIntCode,
                        KeyTypeIntCode = kt.KeyTypeIntCode
                    }
                                                                 ).ToList <KeyValueDataView>();

                    listKeyValueCostCenter   = listKeyValuesToSAPDataExpensesAndQuantity.Where(w => w.KeyTypeIntCode == ETEMEnums.KeyTypeEnum.CostCenter.ToString()).ToList();
                    listKeyValueExpensesType = listKeyValuesToSAPDataExpensesAndQuantity.Where(w => w.KeyTypeIntCode == ETEMEnums.KeyTypeEnum.ExpensesType.ToString()).ToList();
                    listKeyValueQuantityType = listKeyValuesToSAPDataExpensesAndQuantity.Where(w => w.KeyTypeIntCode == ETEMEnums.KeyTypeEnum.QuantityType.ToString()).ToList();

                    List <SAPDataExpense>  listSAPDataExpenseOld  = new List <SAPDataExpense>();
                    List <SAPDataQuantity> listSAPDataQuantityOld = new List <SAPDataQuantity>();
                    List <SAPDataExpense>  listSAPDataExpenseNew  = new List <SAPDataExpense>();
                    List <SAPDataQuantity> listSAPDataQuantityNew = new List <SAPDataQuantity>();

                    listSAPDataExpenseOld = (from sde in this.dbContext.SAPDataExpenses
                                             where sde.idSAPData == _SAPData.idSAPData
                                             select sde).ToList <SAPDataExpense>();

                    listSAPDataQuantityOld = (from sdq in this.dbContext.SAPDataQuantities
                                              where sdq.idSAPData == _SAPData.idSAPData
                                              select sdq).ToList <SAPDataQuantity>();

                    Dictionary <string, string> dictErrorsExpenses   = new Dictionary <string, string>();
                    Dictionary <string, string> dictErrorsQuantities = new Dictionary <string, string>();

                    SAPDataExpense  newSAPDataExpense  = new SAPDataExpense();
                    SAPDataQuantity newSAPDataQuantity = new SAPDataQuantity();

                    bool hasNotErrorInRow = true;

                    string rangeValueStr = string.Empty;

                    ExcelRange range;

                    for (int i = 4; i < listKeyValueCostCenter.Count + 4; i++)
                    {
                        for (int j = 2; j < listKeyValueExpensesType.Count + 2; j++)
                        {
                            hasNotErrorInRow = true;

                            currRow       = 1;
                            range         = workSheetExpenses.Cells[currRow, i];
                            rangeValueStr = (range.Value != null ? range.Value.ToString() : string.Empty);

                            var kvCostCenter = listKeyValueCostCenter.Where(w => w.Name.Trim().ToUpper() == rangeValueStr.Trim().ToUpper()).FirstOrDefault();

                            newSAPDataExpense = new SAPDataExpense();

                            newSAPDataExpense.idSAPData = idEntity;
                            if (kvCostCenter != null)
                            {
                                newSAPDataExpense.idCostCenter = kvCostCenter.idKeyValue;
                            }
                            else
                            {
                                hasNotErrorInRow = false;
                                if (dictErrorsExpenses.ContainsKey("CostCenterExpenses"))
                                {
                                    dictErrorsExpenses["CostCenterExpenses"] += "," + i;
                                }
                                else
                                {
                                    dictErrorsExpenses.Add("CostCenterExpenses", i.ToString());
                                }
                            }

                            currCol       = 2;
                            range         = workSheetExpenses.Cells[j, currCol];
                            rangeValueStr = (range.Value != null ? range.Value.ToString() : string.Empty);

                            var kvExpensesType = listKeyValueExpensesType.Where(w => w.KeyValueIntCode.Trim().ToUpper() == rangeValueStr.Trim().ToUpper()).FirstOrDefault();

                            if (kvExpensesType != null)
                            {
                                newSAPDataExpense.idExpensesType = kvExpensesType.idKeyValue;
                            }
                            else
                            {
                                hasNotErrorInRow = false;
                                if (dictErrorsExpenses.ContainsKey("ExpensesType"))
                                {
                                    dictErrorsExpenses["ExpensesType"] += "," + j;
                                }
                                else
                                {
                                    dictErrorsExpenses.Add("ExpensesType", j.ToString());
                                }
                            }

                            range         = workSheetExpenses.Cells[j, i];
                            rangeValueStr = (range.Value != null ? range.Value.ToString() : string.Empty);

                            res = Decimal.TryParse(rangeValueStr, NumberStyles.Any, BaseHelper.GetNumberFormatInfo("", ".", 4), out resultParseDecimal);
                            if (res)
                            {
                                newSAPDataExpense.ValueData = resultParseDecimal;
                            }
                            else
                            {
                                newSAPDataExpense.ValueData = decimal.Zero;
                            }

                            var checkSAPDataExpenses = listSAPDataExpenseOld.Where(w => w.idCostCenter == newSAPDataExpense.idCostCenter &&
                                                                                   w.idExpensesType == newSAPDataExpense.idExpensesType).ToList();

                            if (checkSAPDataExpenses.Count > 0)
                            {
                                hasNotErrorInRow = false;
                                if (dictErrorsExpenses.ContainsKey("DuplicateOldExpenses"))
                                {
                                    dictErrorsExpenses["DuplicateOldExpenses"] += "," + j + "-" + i;
                                }
                                else
                                {
                                    dictErrorsExpenses.Add("DuplicateOldExpenses", j + "-" + i);
                                }
                            }

                            if (hasNotErrorInRow)
                            {
                                listSAPDataExpenseNew.Add(newSAPDataExpense);
//                                this.dbContext.SAPDataExpenses.AddObject(newSAPDataExpense);
                            }
                        }

                        for (int j = 2; j < listKeyValueQuantityType.Count + 2; j++)
                        {
                            hasNotErrorInRow = true;

                            currRow       = 1;
                            range         = workSheetQuantity.Cells[currRow, i];
                            rangeValueStr = (range.Value != null ? range.Value.ToString() : string.Empty);

                            var kvCostCenter = listKeyValueCostCenter.Where(w => w.Name.Trim().ToUpper() == rangeValueStr.Trim().ToUpper()).FirstOrDefault();

                            newSAPDataQuantity = new SAPDataQuantity();

                            newSAPDataQuantity.idSAPData = idEntity;
                            if (kvCostCenter != null)
                            {
                                newSAPDataQuantity.idCostCenter = kvCostCenter.idKeyValue;
                            }
                            else
                            {
                                hasNotErrorInRow = false;
                                if (dictErrorsQuantities.ContainsKey("CostCenterQuantity"))
                                {
                                    dictErrorsQuantities["CostCenterQuantity"] += "," + i;
                                }
                                else
                                {
                                    dictErrorsQuantities.Add("CostCenterQuantity", i.ToString());
                                }
                            }

                            currCol       = 2;
                            range         = workSheetQuantity.Cells[j, currCol];
                            rangeValueStr = (range.Value != null ? range.Value.ToString() : string.Empty);

                            var kvQuantityType = listKeyValueQuantityType.Where(w => w.KeyValueIntCode.Trim().ToUpper() == rangeValueStr.Trim().ToUpper()).FirstOrDefault();

                            if (kvQuantityType != null)
                            {
                                newSAPDataQuantity.idQuantityType = kvQuantityType.idKeyValue;
                            }
                            else
                            {
                                hasNotErrorInRow = false;
                                if (dictErrorsQuantities.ContainsKey("QuantityType"))
                                {
                                    dictErrorsQuantities["QuantityType"] += "," + j;
                                }
                                else
                                {
                                    dictErrorsQuantities.Add("QuantityType", j.ToString());
                                }
                            }

                            range         = workSheetQuantity.Cells[j, i];
                            rangeValueStr = (range.Value != null ? range.Value.ToString() : string.Empty);

                            res = Decimal.TryParse(rangeValueStr, NumberStyles.Any, BaseHelper.GetNumberFormatInfo("", ".", 4), out resultParseDecimal);
                            if (res)
                            {
                                newSAPDataQuantity.ValueData = resultParseDecimal;
                            }
                            else
                            {
                                newSAPDataQuantity.ValueData = decimal.Zero;
                            }

                            var checkSAPDataQuantity = listSAPDataQuantityOld.Where(w => w.idCostCenter == newSAPDataQuantity.idCostCenter &&
                                                                                    w.idQuantityType == newSAPDataQuantity.idQuantityType).ToList();

                            if (checkSAPDataQuantity.Count > 0)
                            {
                                hasNotErrorInRow = false;
                                if (dictErrorsQuantities.ContainsKey("DuplicateOldQuantity"))
                                {
                                    dictErrorsQuantities["DuplicateOldQuantity"] += "," + j + "-" + i;
                                }
                                else
                                {
                                    dictErrorsQuantities.Add("DuplicateOldQuantity", j + "-" + i);
                                }
                            }

                            if (hasNotErrorInRow)
                            {
                                listSAPDataQuantityNew.Add(newSAPDataQuantity);
//                                this.dbContext.SAPDataQuantities.AddObject(newSAPDataQuantity);
                            }
                        }
                    }

                    if (dictErrorsExpenses.Count == 0 && dictErrorsQuantities.Count == 0)
                    {
                        resultContext = new SAPDataExpensesBL().EntitySave <SAPDataExpense>(listSAPDataExpenseNew, resultContext);
                        if (resultContext.ResultCode == ETEMEnums.ResultEnum.Success)
                        {
                            resultContext = new SAPDataQuantityBL().EntitySave <SAPDataQuantity>(listSAPDataQuantityNew, resultContext);
                            if (resultContext.ResultCode == ETEMEnums.ResultEnum.Success)
                            {
                                resultContext.ResultCode = ETEMEnums.ResultEnum.Success;
                                resultContext.Message    = "The SAP data expenses and quantities by cost centers have been imported successfully!";
                            }
                            else
                            {
                                resultContext.ResultCode = ETEMEnums.ResultEnum.Error;
                                resultContext.Message    = "Error import the SAP data expenses and quantities by cost centers!";
                            }
                        }
                        else
                        {
                            resultContext.ResultCode = ETEMEnums.ResultEnum.Error;
                            resultContext.Message    = "Error import the SAP data expenses and quantities by cost centers!";
                        }
                    }
                    else
                    {
                        List <string> listErrors = new List <string>();

                        if (dictErrorsExpenses.ContainsKey("CostCenterExpenses"))
                        {
                            listErrors.Add("Error! The field `Cost Center` for expenses is missing or in wrong format, Cols (" + dictErrorsExpenses["CostCenterExpenses"] + ")!");
                        }
                        if (dictErrorsExpenses.ContainsKey("ExpensesType"))
                        {
                            listErrors.Add("Error! The field `Expenses Type` is missing or in wrong format, Rows (" + dictErrorsExpenses["ExpensesType"] + ")!");
                        }
                        if (dictErrorsExpenses.ContainsKey("DuplicateOldExpenses"))
                        {
                            listErrors.Add("Error! The selected file includes expenses with duplicate data in the database, Rows-Cols (" + dictErrorsExpenses["DuplicateOldExpenses"] + ")!");
                        }
                        if (dictErrorsQuantities.ContainsKey("CostCenterQuantity"))
                        {
                            listErrors.Add("Error! The field `Cost Center` for quantities is missing or in wrong format, Cols (" + dictErrorsQuantities["CostCenterQuantity"] + ")!");
                        }
                        if (dictErrorsQuantities.ContainsKey("QuantityType"))
                        {
                            listErrors.Add("Error! The field `Quantity Type` is missing or in wrong format, Rows (" + dictErrorsQuantities["QuantityType"] + ")!");
                        }
                        if (dictErrorsQuantities.ContainsKey("DuplicateOldQuantity"))
                        {
                            listErrors.Add("Error! The selected file includes quantities with duplicate data in the database, Rows-Cols (" + dictErrorsQuantities["DuplicateOldQuantity"] + ")!");
                        }

                        resultContext.Message = string.Join(Constants.ERROR_MESSAGES_SEPARATOR, listErrors);
                    }
                }
            }
            catch (Exception ex)
            {
                resultContext.Message = "Error import the SAP data expenses and quantities by cost centers!";

                BaseHelper.Log("Error import entities `SAPDataExpenses`, `SAPDataQuantity`!");
                BaseHelper.Log(ex.Message);
                BaseHelper.Log(ex.StackTrace);
            }

            return(resultContext);
        }
示例#5
0
        public CallContext ImportDiePriceListDetails(string fileFullName, int idEntity, CallContext resultContext)
        {
            try
            {
                resultContext.ResultCode = ETEMEnums.ResultEnum.Error;

                FileInfo excelFile = new FileInfo(fileFullName);

                using (ExcelPackage package = new ExcelPackage(excelFile))
                {
                    int currRow = 1;
                    int currCol = 0;

                    bool    res;
                    decimal resultParseDecimal;
                    int     resultParseInt;

                    ExcelWorksheet workSheet = package.Workbook.Worksheets.FirstOrDefault();

                    if (workSheet == null)
                    {
                        resultContext.Message = "Error! No Excel work sheet!";
                        return(resultContext);
                    }

                    DiePriceList diePriceList = this.GetEntityById(idEntity);

                    if (diePriceList == null)
                    {
                        resultContext.Message = "Entity `DiePriceList` not found by ID (" + idEntity + ")!";
                        return(resultContext);
                    }

                    List <string> listKeyTypeIntCodes = new List <string>()
                    {
                        ETEMEnums.KeyTypeEnum.NumberOfCavities.ToString(),
                                  ETEMEnums.KeyTypeEnum.ProfileCategory.ToString(),
                                  ETEMEnums.KeyTypeEnum.ProfileComplexity.ToString()
                    };

                    List <KeyValueDataView> listKeyValuesToDiePriceListDetail = new List <KeyValueDataView>();
                    List <KeyValueDataView> listKeyValueNumberOfCavities      = new List <KeyValueDataView>();
                    List <KeyValueDataView> listKeyValueProfileCategory       = new List <KeyValueDataView>();
                    List <KeyValueDataView> listKeyValueProfileComplexity     = new List <KeyValueDataView>();

                    listKeyValuesToDiePriceListDetail = (from kv in this.dbContext.KeyValues
                                                         join kt in this.dbContext.KeyTypes on kv.idKeyType equals kt.idKeyType
                                                         where listKeyTypeIntCodes.Contains(kt.KeyTypeIntCode)
                                                         select new KeyValueDataView
                    {
                        idKeyValue = kv.idKeyValue,
                        Name = kv.Name,
                        NameEN = kv.NameEN,
                        DefaultValue1 = kv.DefaultValue1,
                        KeyValueIntCode = kv.KeyValueIntCode,
                        KeyTypeIntCode = kt.KeyTypeIntCode
                    }
                                                         ).ToList <KeyValueDataView>();

                    listKeyValueNumberOfCavities  = listKeyValuesToDiePriceListDetail.Where(w => w.KeyTypeIntCode == ETEMEnums.KeyTypeEnum.NumberOfCavities.ToString()).ToList();
                    listKeyValueProfileCategory   = listKeyValuesToDiePriceListDetail.Where(w => w.KeyTypeIntCode == ETEMEnums.KeyTypeEnum.ProfileCategory.ToString()).ToList();
                    listKeyValueProfileComplexity = listKeyValuesToDiePriceListDetail.Where(w => w.KeyTypeIntCode == ETEMEnums.KeyTypeEnum.ProfileComplexity.ToString()).ToList();

                    List <DiePriceListDetail> listDiePriceListDetailsOld = new List <DiePriceListDetail>();
                    List <DiePriceListDetail> listDiePriceListDetailsNew = new List <DiePriceListDetail>();

                    listDiePriceListDetailsOld = (from dpld in this.dbContext.DiePriceListDetails
                                                  where dpld.idDiePriceList == diePriceList.idDiePriceList
                                                  select dpld).ToList <DiePriceListDetail>();

                    Dictionary <string, string> dictErrorsRows = new Dictionary <string, string>();

                    DiePriceListDetail newDiePriceListDetail = new DiePriceListDetail();

                    bool hasNotErrorInRow = true;

                    string rangeValueStr = string.Empty;

                    ExcelRange range;

                    for (; ;)
                    {
                        currRow++;

                        hasNotErrorInRow = true;

                        currCol = 1;
                        range   = workSheet.Cells[currRow, currCol];
                        if (string.IsNullOrWhiteSpace(workSheet.Cells[currRow, 1].Text) &&
                            string.IsNullOrWhiteSpace(workSheet.Cells[currRow, 2].Text) &&
                            string.IsNullOrWhiteSpace(workSheet.Cells[currRow, 3].Text) &&
                            string.IsNullOrWhiteSpace(workSheet.Cells[currRow, 4].Text) &&
                            string.IsNullOrWhiteSpace(workSheet.Cells[currRow, 5].Text) &&
                            string.IsNullOrWhiteSpace(workSheet.Cells[currRow, 6].Text))
                        {
                            break;
                        }

                        newDiePriceListDetail = new DiePriceListDetail();

                        newDiePriceListDetail.idDiePriceList = idEntity;

                        rangeValueStr = (range.Value != null ? range.Value.ToString() : string.Empty);

                        var numberOfCavities = listKeyValueNumberOfCavities.Where(w => w.DefaultValue1.Trim().ToUpper() == rangeValueStr.Trim().ToUpper()).FirstOrDefault();

                        if (numberOfCavities != null)
                        {
                            newDiePriceListDetail.idNumberOfCavities = numberOfCavities.idKeyValue;
                        }
                        else
                        {
                            hasNotErrorInRow = false;
                            if (dictErrorsRows.ContainsKey("NumberOfCavities"))
                            {
                                dictErrorsRows["NumberOfCavities"] += "," + currRow;
                            }
                            else
                            {
                                dictErrorsRows.Add("NumberOfCavities", currRow.ToString());
                            }
                        }

                        currCol++;
                        range = workSheet.Cells[currRow, currCol];

                        rangeValueStr = (range.Value != null ? range.Value.ToString() : string.Empty);

                        string[] arrRangeValue = rangeValueStr.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);

                        string profileComplexityVal = (arrRangeValue.Length > 0 ? arrRangeValue[0] : string.Empty);
                        string profileCategoryVal   = (arrRangeValue.Length > 1 ? arrRangeValue[1] : string.Empty);

                        var profileComplexity = listKeyValueProfileComplexity.Where(w => w.Name.Trim().ToUpper() == profileComplexityVal.Trim().ToUpper()).FirstOrDefault();

                        if (profileComplexity != null)
                        {
                            newDiePriceListDetail.idProfileComplexity = profileComplexity.idKeyValue;
                        }
                        else
                        {
                            hasNotErrorInRow = false;
                            if (dictErrorsRows.ContainsKey("ProfileComplexity"))
                            {
                                dictErrorsRows["ProfileComplexity"] += "," + currRow;
                            }
                            else
                            {
                                dictErrorsRows.Add("ProfileComplexity", currRow.ToString());
                            }
                        }

                        var profileCategory = listKeyValueProfileCategory.Where(w => w.Name.Trim().ToUpper() == profileCategoryVal.Trim().ToUpper()).FirstOrDefault();

                        if (profileCategory != null)
                        {
                            newDiePriceListDetail.idProfileCategory = profileCategory.idKeyValue;
                        }
                        else
                        {
                            hasNotErrorInRow = false;
                            if (dictErrorsRows.ContainsKey("ProfileCategory"))
                            {
                                dictErrorsRows["ProfileCategory"] += "," + currRow;
                            }
                            else
                            {
                                dictErrorsRows.Add("ProfileCategory", currRow.ToString());
                            }
                        }

                        currCol++;

                        currCol++;
                        range = workSheet.Cells[currRow, currCol];

                        rangeValueStr = (range.Value != null ? range.Value.ToString() : string.Empty);

                        res = Decimal.TryParse(rangeValueStr, NumberStyles.Any, BaseHelper.GetNumberFormatInfo("", ".", 2), out resultParseDecimal);
                        if (res)
                        {
                            newDiePriceListDetail.Price = resultParseDecimal;
                        }
                        else
                        {
                            hasNotErrorInRow            = false;
                            newDiePriceListDetail.Price = decimal.MinValue;

                            if (dictErrorsRows.ContainsKey("Price"))
                            {
                                dictErrorsRows["Price"] += "," + currRow;
                            }
                            else
                            {
                                dictErrorsRows.Add("Price", currRow.ToString());
                            }
                        }

                        currCol++;
                        range = workSheet.Cells[currRow, currCol];

                        rangeValueStr = (range.Value != null ? range.Value.ToString() : string.Empty);

                        res = Int32.TryParse(rangeValueStr, NumberStyles.Any, BaseHelper.GetNumberFormatInfo("", ".", 0), out resultParseInt);
                        if (res)
                        {
                            newDiePriceListDetail.DimensionA = resultParseInt;
                        }
                        else
                        {
                            hasNotErrorInRow = false;
                            newDiePriceListDetail.DimensionA = int.MinValue;

                            if (dictErrorsRows.ContainsKey("DimensionA"))
                            {
                                dictErrorsRows["DimensionA"] += "," + currRow;
                            }
                            else
                            {
                                dictErrorsRows.Add("DimensionA", currRow.ToString());
                            }
                        }

                        currCol++;
                        range = workSheet.Cells[currRow, currCol];

                        rangeValueStr = (range.Value != null ? range.Value.ToString() : string.Empty);

                        res = Int32.TryParse(rangeValueStr, NumberStyles.Any, BaseHelper.GetNumberFormatInfo("", ".", 0), out resultParseInt);
                        if (res)
                        {
                            newDiePriceListDetail.DimensionB = resultParseInt;
                        }
                        else
                        {
                            hasNotErrorInRow = false;
                            newDiePriceListDetail.DimensionB = int.MinValue;

                            if (dictErrorsRows.ContainsKey("DimensionB"))
                            {
                                dictErrorsRows["DimensionB"] += "," + currRow;
                            }
                            else
                            {
                                dictErrorsRows.Add("DimensionB", currRow.ToString());
                            }
                        }

                        newDiePriceListDetail.Lifespan = decimal.Zero;

                        var checkDiePriceListDetail = listDiePriceListDetailsOld.Where(w => w.idNumberOfCavities == newDiePriceListDetail.idNumberOfCavities &&
                                                                                       w.idProfileComplexity == newDiePriceListDetail.idProfileComplexity &&
                                                                                       w.idProfileCategory == newDiePriceListDetail.idProfileCategory &&
                                                                                       w.Price == newDiePriceListDetail.Price &&
                                                                                       w.DimensionA == newDiePriceListDetail.DimensionA &&
                                                                                       w.DimensionB == newDiePriceListDetail.DimensionB).ToList();

                        if (checkDiePriceListDetail.Count > 0)
                        {
                            hasNotErrorInRow = false;
                            if (dictErrorsRows.ContainsKey("DuplicateOld"))
                            {
                                dictErrorsRows["DuplicateOld"] += "," + currRow;
                            }
                            else
                            {
                                dictErrorsRows.Add("DuplicateOld", currRow.ToString());
                            }
                        }

                        checkDiePriceListDetail = listDiePriceListDetailsNew.Where(w => w.idNumberOfCavities == newDiePriceListDetail.idNumberOfCavities &&
                                                                                   w.idProfileComplexity == newDiePriceListDetail.idProfileComplexity &&
                                                                                   w.idProfileCategory == newDiePriceListDetail.idProfileCategory &&
                                                                                   w.Price == newDiePriceListDetail.Price &&
                                                                                   w.DimensionA == newDiePriceListDetail.DimensionA &&
                                                                                   w.DimensionB == newDiePriceListDetail.DimensionB).ToList();

                        if (checkDiePriceListDetail.Count > 0)
                        {
                            hasNotErrorInRow = false;
                            if (dictErrorsRows.ContainsKey("DuplicateNew"))
                            {
                                dictErrorsRows["DuplicateNew"] += "," + currRow;
                            }
                            else
                            {
                                dictErrorsRows.Add("DuplicateNew", currRow.ToString());
                            }
                        }

                        if (hasNotErrorInRow)
                        {
                            listDiePriceListDetailsNew.Add(newDiePriceListDetail);
//                            this.dbContext.DiePriceListDetails.AddObject(newDiePriceListDetail);
                        }
                    }

                    if (dictErrorsRows.Count == 0)
                    {
                        resultContext = new DiePriceListDetailBL().EntitySave <DiePriceListDetail>(listDiePriceListDetailsNew, resultContext);

                        if (resultContext.ResultCode == ETEMEnums.ResultEnum.Success)
                        {
                            resultContext.ResultCode = ETEMEnums.ResultEnum.Success;
                            resultContext.Message    = "The details for current `Die Price List by Vendor` have been imported successfully!";
                        }
                        else
                        {
                            resultContext.ResultCode = ETEMEnums.ResultEnum.Error;
                            resultContext.Message    = "Error import details for current `Die Price List by Vendor`!";
                        }
                    }
                    else
                    {
                        List <string> listErrors = new List <string>();

                        if (dictErrorsRows.ContainsKey("NumberOfCavities"))
                        {
                            listErrors.Add("Error! The field `cavities` is missing or in wrong format, Rows (" + dictErrorsRows["NumberOfCavities"] + ")!");
                        }
                        if (dictErrorsRows.ContainsKey("ProfileComplexity"))
                        {
                            listErrors.Add("Error! The field `complexity` is missing or in wrong format, Rows (" + dictErrorsRows["ProfileComplexity"] + ")!");
                        }
                        if (dictErrorsRows.ContainsKey("ProfileCategory"))
                        {
                            listErrors.Add("Error! The field `category` is missing or in wrong format, Rows (" + dictErrorsRows["ProfileCategory"] + ")!");
                        }
                        if (dictErrorsRows.ContainsKey("Price"))
                        {
                            listErrors.Add("Error! The field `price` is missing or in wrong NUMBER format, Rows (" + dictErrorsRows["Price"] + ")!");
                        }
                        if (dictErrorsRows.ContainsKey("DimensionA"))
                        {
                            listErrors.Add("Error! The field `dimensiona` is missing or in wrong INTEGER NUMBER format, Rows (" + dictErrorsRows["DimensionA"] + ")!");
                        }
                        if (dictErrorsRows.ContainsKey("DimensionB"))
                        {
                            listErrors.Add("Error! The field `dimensionb` is missing or in wrong INTEGER NUMBER format, Rows (" + dictErrorsRows["DimensionB"] + ")!");
                        }
                        if (dictErrorsRows.ContainsKey("DuplicateNew"))
                        {
                            listErrors.Add("Error! The selected file includes die price list details with duplicate values, Rows (" + dictErrorsRows["DuplicateNew"] + ")!");
                        }
                        if (dictErrorsRows.ContainsKey("DuplicateOld"))
                        {
                            listErrors.Add("Error! The selected file includes die price list details with duplicate values in the database, Rows (" + dictErrorsRows["DuplicateOld"] + ")!");
                        }

                        resultContext.Message = string.Join(Constants.ERROR_MESSAGES_SEPARATOR, listErrors);
                    }
                }
            }
            catch (Exception ex)
            {
                resultContext.Message = "Error import details for current `Die Price List by Vendor`!";

                BaseHelper.Log("Error import entities `DiePriceListDetail`!");
                BaseHelper.Log(ex.Message);
                BaseHelper.Log(ex.StackTrace);
            }

            return(resultContext);
        }
        public Dictionary <string, TableRow[]> LoadOfferDataCostCenterAndExpensesType(int idOffer, ETEMEnums.CalculationType calculationType, CallContext resultContext)
        {
            Dictionary <string, TableRow[]> result = new Dictionary <string, TableRow[]>();

            try
            {
                List <string> listKeyTypeIntCodes = new List <string>()
                {
                    ETEMEnums.KeyTypeEnum.CostCenter.ToString(),
                              ETEMEnums.KeyTypeEnum.ExpensesTypeGroup.ToString(),
                };

                List <KeyValueDataView> listKeyValuesToSAPDataCostCenterAndExpensesTypeGroup = new List <KeyValueDataView>();
                List <KeyValueDataView> listKeyValueCostCenter        = new List <KeyValueDataView>();
                List <KeyValueDataView> listKeyValueExpensesTypeGroup = new List <KeyValueDataView>();

                listKeyValuesToSAPDataCostCenterAndExpensesTypeGroup = (from kv in this.dbContext.KeyValues
                                                                        join kt in this.dbContext.KeyTypes on kv.idKeyType equals kt.idKeyType
                                                                        where listKeyTypeIntCodes.Contains(kt.KeyTypeIntCode)
                                                                        orderby kv.V_Order ascending


                                                                        select new KeyValueDataView
                {
                    idKeyValue = kv.idKeyValue,
                    Name = kv.Name,
                    NameEN = kv.NameEN,
                    DefaultValue1 = kv.DefaultValue1,
                    KeyValueIntCode = kv.KeyValueIntCode,
                    KeyTypeIntCode = kt.KeyTypeIntCode
                }
                                                                        ).ToList <KeyValueDataView>();

                List <OfferDataExpenseGroupView> listGroupExpense = GetAllOfferDataExpenseGroupByOffer(idOffer);
                Offer offer = new OfferBL().GetEntityById(idOffer);
                OfferProducitivity offerProducitivity = new OfferProducitivityBL().GetOfferProducitivityByOfferID(idOffer);

                List <int> listIdCostCenter = listGroupExpense.DistinctBy(k => k.idCostCenter).Select(k => k.idCostCenter).ToList();

                listKeyValueCostCenter = listKeyValuesToSAPDataCostCenterAndExpensesTypeGroup.Where(w => listIdCostCenter.Contains(w.idKeyValue)).ToList();

                listKeyValueExpensesTypeGroup = listKeyValuesToSAPDataCostCenterAndExpensesTypeGroup.Where(w => w.KeyTypeIntCode == ETEMEnums.KeyTypeEnum.ExpensesTypeGroup.ToString()).ToList();

                int countRows = listKeyValueExpensesTypeGroup.Count + 3;

                if (calculationType == ETEMEnums.CalculationType.EUR_TON)
                {
                    countRows = listKeyValueExpensesTypeGroup.Count + 4;
                }


                TableRow[] arrExpensesTypeGroup = new TableRow[countRows];

                TableRow        tableRow        = new TableRow();
                TableCell       tableCell       = new TableCell();
                TableHeaderRow  tableHeaderRow  = new TableHeaderRow();
                TableHeaderCell tableHeaderCell = new TableHeaderCell();
                int             rowIndex        = 0;

                #region Expenses by Cost Centers (EUR/MH)

                string eurTonMHbr = string.Empty;
                string eurTonMH   = string.Empty;
                int    columnSpan = 2;

                if (calculationType == ETEMEnums.CalculationType.EUR_MH)
                {
                    eurTonMHbr = "<br/>(EUR/MH)";
                    eurTonMH   = "(EUR/MH)";
                    columnSpan--;
                }
                else
                {
                    eurTonMHbr = "<br/>(EUR/ton)";
                    eurTonMH   = "(EUR/ton)";
                }

                tableHeaderRow  = new TableHeaderRow();
                tableHeaderCell = new TableHeaderCell();

                tableHeaderCell.Text       = "Expenses by Cost Centers " + eurTonMH;
                tableHeaderCell.ColumnSpan = listIdCostCenter.Count + columnSpan;
                tableHeaderCell.CssClass   = "GridExpenses_tr_th_Main";
                tableHeaderRow.Cells.Add(tableHeaderCell);

                arrExpensesTypeGroup[rowIndex++] = tableHeaderRow;

                tableHeaderRow  = new TableHeaderRow();
                tableHeaderCell = new TableHeaderCell();

                if (calculationType == ETEMEnums.CalculationType.EUR_TON)
                {
                    tableHeaderCell.Text = "Expenses " + eurTonMHbr;
                }
                else
                {
                    tableHeaderCell.Text = "Expenses";
                }

                tableHeaderCell.Width = Unit.Pixel(320);
                tableHeaderRow.Cells.Add(tableHeaderCell);

                arrExpensesTypeGroup[rowIndex] = tableHeaderRow;


                #region Add header and footer columns and rows

                //Calculate tableHeaderCell width
                int headerWidth;

                if (calculationType == ETEMEnums.CalculationType.EUR_MH)
                {
                    headerWidth = (int)(100 / (listKeyValueCostCenter.Count + 1));
                }
                else
                {
                    headerWidth = (int)(100 / (listKeyValueCostCenter.Count + 2));
                }


                // Add header columns
                foreach (KeyValueDataView kvCostCenter in listKeyValueCostCenter)
                {
                    if (kvCostCenter.DefaultValue1 == "Packaging" && calculationType == ETEMEnums.CalculationType.EUR_TON)
                    {
                        //add total extrusion column
                        if (calculationType == ETEMEnums.CalculationType.EUR_TON)
                        {
                            //TotalColumn
                            tableHeaderCell      = new TableHeaderCell();
                            tableHeaderCell.Text = "Total Extrusion " + eurTonMHbr;
                            //В офертата да направим колоните в таблиците с разходите с еднакъв размер
                            tableHeaderCell.Width = new Unit(headerWidth.ToString() + "%");

                            arrExpensesTypeGroup[rowIndex].Cells.Add(tableHeaderCell);
                        }
                    }

                    tableHeaderCell      = new TableHeaderCell();
                    tableHeaderCell.Text = kvCostCenter.Name;

                    //В офертата да направим колоните в таблиците с разходите с еднакъв размер
                    tableHeaderCell.Width = new Unit(headerWidth.ToString() + "%");


                    if (kvCostCenter.DefaultValue1 == "DIES" && calculationType == ETEMEnums.CalculationType.EUR_MH)
                    {
                        tableHeaderCell.Text += "<br/>" + "(EUR/kg)";
                    }

                    arrExpensesTypeGroup[rowIndex].Cells.Add(tableHeaderCell);
                }

                //add total extrusion column
                //if (calculationType == ETEMEnums.CalculationType.EUR_TON)
                //{
                //    //TotalColumn
                //    tableHeaderCell = new TableHeaderCell();
                //    tableHeaderCell.Text = "Total Extrusion " + eurTonMH;
                //    //В офертата да направим колоните в таблиците с разходите с еднакъв размер
                //    tableHeaderCell.Width = new Unit(headerWidth.ToString() + "%");

                //    arrExpensesTypeGroup[rowIndex].Cells.Add(tableHeaderCell);
                //}

                rowIndex++;
                // Add header rows
                foreach (KeyValueDataView kvExpensesTypeGroup in listKeyValueExpensesTypeGroup)
                {
                    tableRow  = new TableRow();
                    tableCell = new TableCell();

                    tableCell.Text = kvExpensesTypeGroup.Name.Replace("group", "");

                    tableRow.Cells.Add(tableCell);

                    arrExpensesTypeGroup[rowIndex++] = tableRow;
                }

                int i = 0;
                if (calculationType == ETEMEnums.CalculationType.EUR_TON)
                {
                    TableRow tableFooterRow = new TableRow();
                    tableCell      = new TableCell();
                    tableCell.Text = "Die depreciation";
                    tableFooterRow.Cells.Add(tableCell);
                    arrExpensesTypeGroup[rowIndex] = tableFooterRow;

                    rowIndex++;
                    i++;
                }

                TableRow tableFooterTotalRow = new TableRow();
                tableCell           = new TableCell();
                tableCell.Text      = "Total " + eurTonMH;
                tableCell.Font.Bold = true;
                tableCell.CssClass  = "MainGrid_td_item_right GridExpenses_td_background_yellow";
                tableFooterTotalRow.Cells.Add(tableCell);

                arrExpensesTypeGroup[rowIndex] = tableFooterTotalRow;

                #endregion

                Dictionary <string, decimal> dictTotalByColl = new Dictionary <string, decimal>();

                decimal totalByRowTon   = decimal.Zero;
                decimal currValue       = decimal.Zero;
                decimal totalByTotalRow = decimal.Zero;

                rowIndex = rowIndex - (listKeyValueExpensesTypeGroup.Count + i);

                foreach (KeyValueDataView kvExpensesTypeGroup in listKeyValueExpensesTypeGroup)
                {
                    totalByRowTon = decimal.Zero;

                    foreach (KeyValueDataView kvCostCenter in listKeyValueCostCenter)
                    {
                        tableCell = new TableCell();
                        tableCell.HorizontalAlign = HorizontalAlign.Right;
                        tableCell.CssClass        = "MainGrid_td_item_right";

                        var currGroupExpense = listGroupExpense.Where(w => w.idExpensesType == kvExpensesTypeGroup.idKeyValue &&
                                                                      w.idCostCenter == kvCostCenter.idKeyValue).FirstOrDefault();

                        if (currGroupExpense != null)
                        {
                            currValue = Convert.ToDecimal(currGroupExpense.ValueData);

                            if (dictTotalByColl.ContainsKey(kvCostCenter.KeyValueIntCode))
                            {
                                if (calculationType == ETEMEnums.CalculationType.EUR_MH)
                                {
                                    dictTotalByColl[kvCostCenter.KeyValueIntCode] += currValue;
                                }
                                else if (calculationType == ETEMEnums.CalculationType.EUR_TON)
                                {
                                    #region EUR_TON
                                    if (kvCostCenter.DefaultValue1 == "Press")
                                    {
                                        if (offerProducitivity != null && offerProducitivity.PressProducitivity_TON_MH != decimal.Zero)
                                        {
                                            dictTotalByColl[kvCostCenter.KeyValueIntCode] += currValue / offerProducitivity.PressProducitivity_TON_MH;
                                            currValue = currValue / offerProducitivity.PressProducitivity_TON_MH;
                                        }
                                        else
                                        {
                                            dictTotalByColl[kvCostCenter.KeyValueIntCode] += currValue;
                                        }

                                        totalByRowTon += currValue;
                                    }
                                    else if (kvCostCenter.DefaultValue1 == "COMETAL")
                                    {
                                        if (offerProducitivity != null && offerProducitivity.COMetalProducitivity_TON_MH != decimal.Zero)
                                        {
                                            dictTotalByColl[kvCostCenter.KeyValueIntCode] += currValue / offerProducitivity.COMetalProducitivity_TON_MH;

                                            currValue = currValue / offerProducitivity.COMetalProducitivity_TON_MH;
                                        }
                                        else
                                        {
                                            dictTotalByColl[kvCostCenter.KeyValueIntCode] += currValue;
                                        }

                                        totalByRowTon += currValue;
                                    }
                                    else if (kvCostCenter.DefaultValue1 == "QualityControl")
                                    {
                                        if (offerProducitivity != null && offerProducitivity.QCProducitivity_TON_MH != decimal.Zero)
                                        {
                                            dictTotalByColl[kvCostCenter.KeyValueIntCode] += currValue / offerProducitivity.QCProducitivity_TON_MH;
                                            currValue = currValue / offerProducitivity.QCProducitivity_TON_MH;
                                        }
                                        else
                                        {
                                            dictTotalByColl[kvCostCenter.KeyValueIntCode] += currValue;
                                        }

                                        totalByRowTon += currValue;
                                    }
                                    else if (kvCostCenter.DefaultValue1 == "Packaging")
                                    {
                                        ///
                                        //fill total in each row

                                        TableCell tableCellTotal = new TableCell();

                                        tableCellTotal.Text = totalByRowTon.ToString("N", BaseHelper.GetNumberFormatInfo(".", ",", 4));
                                        totalByTotalRow    += totalByRowTon;

                                        tableCellTotal.Font.Bold = true;
                                        tableCellTotal.CssClass  = "MainGrid_td_item_right GridExpenses_td_background_yellow";
                                        arrExpensesTypeGroup[rowIndex].Cells.Add(tableCellTotal);


                                        ///

                                        if (offerProducitivity != null && offerProducitivity.PackagingProducitivity_TON_MH != decimal.Zero)
                                        {
                                            dictTotalByColl[kvCostCenter.KeyValueIntCode] += currValue / offerProducitivity.PackagingProducitivity_TON_MH;
                                            currValue = currValue / offerProducitivity.PackagingProducitivity_TON_MH;
                                        }
                                        else
                                        {
                                            dictTotalByColl[kvCostCenter.KeyValueIntCode] += currValue;
                                        }
                                    }
                                    else if (kvCostCenter.DefaultValue1 == "DIES")
                                    {
                                        //for Direct depreciation Expenses
                                        //offer.CostOfDie

                                        if (kvExpensesTypeGroup.KeyValueIntCode != "DirectDepreciationGroup")
                                        {
                                            currValue = currValue * 1000;
                                            dictTotalByColl[kvCostCenter.KeyValueIntCode] += currValue;   //For DIES Department formula is different: '=Value (EUR/kg)*1000
                                        }
                                        else
                                        {
                                            dictTotalByColl[kvCostCenter.KeyValueIntCode] += decimal.Zero;
                                            currValue = decimal.Zero;
                                        }

                                        totalByRowTon += currValue;
                                    }
                                    #endregion
                                }
                                else
                                {
                                    dictTotalByColl[kvCostCenter.KeyValueIntCode] += decimal.Zero;
                                }
                            }
                            else
                            {
                                if (calculationType == ETEMEnums.CalculationType.EUR_MH)
                                {
                                    dictTotalByColl.Add(kvCostCenter.KeyValueIntCode, currValue);
                                }
                                else if (calculationType == ETEMEnums.CalculationType.EUR_TON)
                                {
                                    #region EUR_TON
                                    if (kvCostCenter.DefaultValue1 == "Press")
                                    {
                                        if (offerProducitivity != null && offerProducitivity.PressProducitivity_TON_MH != decimal.Zero)
                                        {
                                            dictTotalByColl.Add(kvCostCenter.KeyValueIntCode, currValue / offerProducitivity.PressProducitivity_TON_MH);
                                            currValue = currValue / offerProducitivity.PressProducitivity_TON_MH;
                                        }
                                        else
                                        {
                                            dictTotalByColl.Add(kvCostCenter.KeyValueIntCode, currValue);
                                        }

                                        totalByRowTon += currValue;
                                    }
                                    else if (kvCostCenter.DefaultValue1 == "COMETAL")
                                    {
                                        if (offerProducitivity != null && offerProducitivity.COMetalProducitivity_TON_MH != decimal.Zero)
                                        {
                                            dictTotalByColl.Add(kvCostCenter.KeyValueIntCode, currValue / offerProducitivity.COMetalProducitivity_TON_MH);
                                            currValue = currValue / offerProducitivity.COMetalProducitivity_TON_MH;
                                        }
                                        else
                                        {
                                            dictTotalByColl.Add(kvCostCenter.KeyValueIntCode, currValue);
                                        }

                                        totalByRowTon += currValue;
                                    }
                                    else if (kvCostCenter.DefaultValue1 == "QualityControl")
                                    {
                                        if (offerProducitivity != null && offerProducitivity.QCProducitivity_TON_MH != decimal.Zero)
                                        {
                                            dictTotalByColl.Add(kvCostCenter.KeyValueIntCode, currValue / offerProducitivity.QCProducitivity_TON_MH);
                                            currValue = currValue / offerProducitivity.QCProducitivity_TON_MH;
                                        }
                                        else
                                        {
                                            dictTotalByColl.Add(kvCostCenter.KeyValueIntCode, currValue);
                                        }

                                        totalByRowTon += currValue;
                                    }
                                    else if (kvCostCenter.DefaultValue1 == "Packaging")
                                    {
                                        ///
                                        //fill total in each row

                                        TableCell tableCellTotal = new TableCell();

                                        tableCellTotal.Text = totalByRowTon.ToString("N", BaseHelper.GetNumberFormatInfo(".", ",", 4));
                                        totalByTotalRow    += totalByRowTon;

                                        tableCellTotal.Font.Bold = true;
                                        tableCellTotal.CssClass  = "MainGrid_td_item_right GridExpenses_td_background_yellow";
                                        arrExpensesTypeGroup[rowIndex].Cells.Add(tableCellTotal);

                                        ///

                                        if (offerProducitivity != null && offerProducitivity.PackagingProducitivity_TON_MH != decimal.Zero)
                                        {
                                            dictTotalByColl.Add(kvCostCenter.KeyValueIntCode, currValue / offerProducitivity.PackagingProducitivity_TON_MH);
                                            currValue = currValue / offerProducitivity.PackagingProducitivity_TON_MH;
                                        }
                                        else
                                        {
                                            dictTotalByColl.Add(kvCostCenter.KeyValueIntCode, currValue);
                                        }
                                    }
                                    else if (kvCostCenter.DefaultValue1 == "DIES")
                                    {
                                        //for Direct depreciation Expenses
                                        //offer.CostOfDie
                                        //dictTotalByColl[kvCostCenter.KeyValueIntCode] = currValue * 1000; //For DIES Department formula is different: '=Value (EUR/kg)*1000
                                        if (kvExpensesTypeGroup.KeyValueIntCode != "DirectDepreciationGroup")
                                        {
                                            dictTotalByColl.Add(kvCostCenter.KeyValueIntCode, currValue * 1000); //For DIES Department formula is different: '=Value (EUR/kg)*1000
                                            currValue = currValue * 1000;
                                        }
                                        else
                                        {
                                            currValue = decimal.Zero;
                                            dictTotalByColl.Add(kvCostCenter.KeyValueIntCode, currValue);
                                        }

                                        totalByRowTon += currValue;
                                    }
                                    #endregion
                                }
                                else
                                {
                                    dictTotalByColl.Add(kvCostCenter.KeyValueIntCode, decimal.Zero);
                                }
                            }

                            tableCell.Text = currValue.ToString("N", BaseHelper.GetNumberFormatInfo(".", ",", 4));
                            arrExpensesTypeGroup[rowIndex].Cells.Add(tableCell);
                        }
                    }

                    ////fill total in each row
                    //if (calculationType == ETEMEnums.CalculationType.EUR_TON)
                    //{
                    //    tableCell = new TableCell();


                    //    tableCell.Text  = totalByRowTon.ToString("N", BaseHelper.GetNumberFormatInfo(".", ",", 4));
                    //    totalByTotalRow += totalByRowTon;


                    //    tableCell.Font.Bold = true;
                    //    tableCell.CssClass  = "MainGrid_td_item_right";
                    //    arrExpensesTypeGroup[rowIndex].Cells.Add(tableCell);
                    //}

                    rowIndex++;
                }

                string dieDepreciation = decimal.Zero.ToString("N", BaseHelper.GetNumberFormatInfo(".", ",", 4));

                //Fill cell in additional row 'Die depreciation'
                if (calculationType == ETEMEnums.CalculationType.EUR_TON)
                {
                    foreach (KeyValueDataView kvCostCenter in listKeyValueCostCenter)
                    {
                        tableCell = new TableCell();
                        tableCell.HorizontalAlign = HorizontalAlign.Right;
                        tableCell.CssClass        = "MainGrid_td_item_right";

                        if (kvCostCenter.DefaultValue1 == "DIES")
                        {
                            if (offer.CostOfDieEUR_Per_TON_Computable.HasValue)
                            {
                                dictTotalByColl[kvCostCenter.KeyValueIntCode] += offer.CostOfDieEUR_Per_TON_Computable.Value;
                                tableCell.Text   = offer.CostOfDieEUR_Per_TON_Computable.Value.ToString("N", BaseHelper.GetNumberFormatInfo(".", ",", 4));                                //dictTotalByColl[kvCostCenter.KeyValueIntCode].ToString("N", BaseHelper.GetNumberFormatInfo(".", ",", 4));
                                totalByTotalRow += offer.CostOfDieEUR_Per_TON_Computable.Value;
                            }
                            else
                            {
                                dictTotalByColl[kvCostCenter.KeyValueIntCode] += decimal.Zero;
                                tableCell.Text = decimal.Zero.ToString("N", BaseHelper.GetNumberFormatInfo(".", ",", 4));
                            }

                            dieDepreciation = tableCell.Text;
                        }
                        else if (kvCostCenter.DefaultValue1 == "Packaging")
                        {
                            //fill total in Die depreciation row
                            TableCell tableCellTotal = new TableCell();
                            tableCellTotal.Font.Bold = true;
                            tableCellTotal.CssClass  = "MainGrid_td_item_right GridExpenses_td_background_yellow";
                            tableCellTotal.Text      = dieDepreciation;
                            arrExpensesTypeGroup[rowIndex].Cells.Add(tableCellTotal);

                            tableCell.Text = decimal.Zero.ToString("N", BaseHelper.GetNumberFormatInfo(".", ",", 4));
                        }
                        else
                        {
                            tableCell.Text = decimal.Zero.ToString("N", BaseHelper.GetNumberFormatInfo(".", ",", 4));
                        }

                        arrExpensesTypeGroup[rowIndex].Cells.Add(tableCell);
                    }


                    //fill total in Die depreciation row
                    //tableCell               = new TableCell();
                    //tableCell.Font.Bold     = true;
                    //tableCell.CssClass      = "MainGrid_td_item_right";
                    //tableCell.Text          = dieDepreciation;
                    //arrExpensesTypeGroup[rowIndex].Cells.Add(tableCell);

                    rowIndex++;
                }

                foreach (KeyValueDataView kvCostCenter in listKeyValueCostCenter)
                {
                    if (kvCostCenter.DefaultValue1 == "Packaging" && calculationType == ETEMEnums.CalculationType.EUR_TON)
                    {
                        TableCell tableCellTotal = new TableCell();
                        tableCellTotal.Font.Bold = true;
                        tableCellTotal.CssClass  = "MainGrid_td_item_right GridExpenses_td_background_yellow";
                        tableCellTotal.Text      = totalByTotalRow.ToString("N", BaseHelper.GetNumberFormatInfo(".", ",", 4));
                        arrExpensesTypeGroup[rowIndex].Cells.Add(tableCellTotal);
                    }

                    tableCell = new TableCell();
                    tableCell.HorizontalAlign = HorizontalAlign.Right;
                    tableCell.CssClass        = "MainGrid_td_item_right GridExpenses_td_background_yellow";
                    tableCell.Font.Bold       = true;
                    tableCell.Text            = dictTotalByColl[kvCostCenter.KeyValueIntCode].ToString("N", BaseHelper.GetNumberFormatInfo(".", ",", 4));
                    arrExpensesTypeGroup[rowIndex].Cells.Add(tableCell);
                }

                //fill cell total on Total Extrusion
                //if (calculationType == ETEMEnums.CalculationType.EUR_TON)
                //{
                //    tableCell               = new TableCell();
                //    tableCell.Font.Bold     = true;
                //    tableCell.CssClass      = "MainGrid_td_item_right";
                //    tableCell.Text          = totalByTotalRow.ToString("N", BaseHelper.GetNumberFormatInfo(".", ",", 4));
                //    arrExpensesTypeGroup[rowIndex].Cells.Add(tableCell);
                //}

                #endregion

                result.Add("Expenses", arrExpensesTypeGroup);
            }
            catch (Exception ex)
            {
                resultContext.Message = "Error load SAP data expenses and quantities!";

                BaseHelper.Log("Error load SAP data expenses and quantities!");
                BaseHelper.Log(ex.Message);
                BaseHelper.Log(ex.StackTrace);
            }

            return(result);
        }