Skip to content

Commit

Permalink
Merge branch 'dev' into feature/AB#26819-custom-field-reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreGAot authored Jan 30, 2025
2 parents 8fe1c8b + 2e173df commit 4a90da9
Show file tree
Hide file tree
Showing 21 changed files with 610 additions and 446 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using System;
using System.Globalization;
using Unity.Flex.Worksheets;

namespace Unity.Flex
{
public static class DataGridExtensions
{
public static string ApplyPresentationFormatting(this string value, string columnType, string? format)
{
if (value == null) return string.Empty;

return columnType switch
{
var ct when IsDateColumn(ct) && TryParseDate(value, format, out string formattedDate) => formattedDate,
var ct when IsDateTimeColumn(ct) && TryParseDateTime(value, format, out string formattedDateTime) => formattedDateTime,
var ct when IsCurrencyColumn(ct) && TryParseCurrency(value, format, out string formattedCurrency) => formattedCurrency,
var ct when IsYesNoColumn(ct) && TryFormatYesNo(value, out string formattedYesNo) => formattedYesNo,
var ct when IsCheckBoxColumn(ct) && TryFormatCheckbox(value, out string formattedCheckbox) => formattedCheckbox,
_ => value
};
}


public static string ApplyStoreFormatting(this string value, string columnType)
{
return columnType switch
{
var ct when IsDateColumn(ct) => ValueConverterHelpers.ConvertDate(value),
var ct when IsDateTimeColumn(ct) => ValueConverterHelpers.ConvertDateTime(value),
var ct when IsCurrencyColumn(ct) => ValueConverterHelpers.ConvertDecimal(value),
var ct when IsYesNoColumn(ct) => ValueConverterHelpers.ConvertYesNo(value),
var ct when IsCheckBoxColumn(ct) => ValueConverterHelpers.ConvertCheckbox(value),
_ => value
};
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Style",
"IDE0060:Remove unused parameter",
Justification = "We ignore the format provided from CHEFS for datetime as this does not display correctly")]
private static bool TryParseDateTime(string value, string? format, out string formattedDateTime)
{
const string fixedFormat = "yyyy-MM-dd hh:mm:ss tt";
if (DateTime.TryParse(value, new CultureInfo("en-CA"), DateTimeStyles.None, out DateTime dateTime))
{
// The format that CHEFS provides vs the provided value don't format correctly
format = fixedFormat;
var appliedFormat = !string.IsNullOrEmpty(format) ? format : fixedFormat;
formattedDateTime = dateTime.ToString(appliedFormat, CultureInfo.InvariantCulture);
return true;
}
formattedDateTime = string.Empty;
return false;
}

private static bool IsDateTimeColumn(string columnType)
{
return columnType == CustomFieldType.DateTime.ToString();
}

private static bool TryFormatCheckbox(string value, out string formattedCheckbox)
{
if (value.IsTruthy()) formattedCheckbox = "true";
else
formattedCheckbox = "false";
return true;
}

private static bool IsCheckBoxColumn(string columnType)
{
return columnType == CustomFieldType.Checkbox.ToString();
}

private static bool TryFormatYesNo(string value, out string formattedYesNo)
{
if (string.IsNullOrEmpty(value) || value == "Please choose...")
{
formattedYesNo = string.Empty;
return true;
}
formattedYesNo = value;
return false;
}

private static bool IsYesNoColumn(string columnType)
{
return columnType == CustomFieldType.YesNo.ToString();
}

private static bool IsDateColumn(string columnType)
{
return columnType == CustomFieldType.Date.ToString();
}

private static bool IsCurrencyColumn(string columnType)
{
return columnType == CustomFieldType.Currency.ToString();
}

private static bool TryParseDate(string value, string? format, out string formattedDate)
{
if (DateTime.TryParse(value, new CultureInfo("en-CA"), DateTimeStyles.None, out DateTime dateTime))
{
var appliedFormat = !string.IsNullOrEmpty(format) ? format : "yyyy-MM-dd";
formattedDate = dateTime.ToString(appliedFormat, CultureInfo.InvariantCulture);
return true;
}
formattedDate = string.Empty;
return false;
}

private static bool TryParseCurrency(string value, string? format, out string formattedCurrency)
{
if (decimal.TryParse(value, out decimal number))
{
var currencyCode = !string.IsNullOrEmpty(format) ? format : "CAD";
var culture = GetCultureInfoByCurrencyCode(currencyCode);
formattedCurrency = number.ToString("C", culture);
return true;
}
formattedCurrency = string.Empty;
return false;
}

private static CultureInfo GetCultureInfoByCurrencyCode(string currencyCode)
{
foreach (var culture in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
var region = new RegionInfo(culture.Name);
if (region.ISOCurrencySymbol == currencyCode)
{
return culture;
}
}

throw new ArgumentException("Invalid or unsupported currency code.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"Worksheet:Configuration:AddCheckboxOptionText": "Add Option",
"Worksheet:Configuration:ExportWorksheetButtonText": "Export Worksheet",
"Worksheet:Configuration:AddSelectListOptionText": "Add Option",
"Worksheet:Configuration:AddColumnOptionText": "Add Column"
"Worksheet:Configuration:AddColumnOptionText": "Add Column",
"DataGrids:DynamicColumnsHeader": "Dynamic Columns",
"DataGrids:CustomColumnsHeader": "Custom Columns"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ internal static Dictionary<string, string> ApplyPresentationFormat(
return formattedKeyValuePairs;
}

internal async Task<List<WorksheetFieldViewModel>?> GetPropertiesAsync(RowInputData dataProps)
internal async Task<(KeyValuePair<string, string>[] dynamicFields, List<WorksheetFieldViewModel>? customFields)> GetPropertiesAsync(RowInputData dataProps)
{
if (IsFirstRow(dataProps))
{
return await GetFirstRowAsync(dataProps);
return ([], await GetFirstRowAsync(dataProps));
}

if (AddNewRow(dataProps))
{
return await GetNewRowAsync(dataProps);
return ([], await GetNewRowAsync(dataProps));
}

return await GetExistingRowAsync(dataProps);
Expand All @@ -62,7 +62,7 @@ private async Task<List<WorksheetFieldViewModel>> GetFirstRowAsync(RowInputData
var datagridDefinition = await customFieldAppService.GetAsync(dataProps.FieldId);
if (datagridDefinition == null) return [];

return DataGridServiceUtils.ConvertDataGridValue(new DataGridValue(), datagridDefinition, 0, true);
return DataGridServiceUtils.ExtractCustomColumnsValues(new DataGridValue(), datagridDefinition, 0, true);
}

private async Task<List<WorksheetFieldViewModel>> GetNewRowAsync(RowInputData dataProps)
Expand All @@ -76,21 +76,22 @@ private async Task<List<WorksheetFieldViewModel>> GetNewRowAsync(RowInputData da

var dataGridValue = JsonSerializer.Deserialize<DataGridValue>(customFieldValue.CurrentValue ?? "{}");

return DataGridServiceUtils.ConvertDataGridValue(dataGridValue, datagridDefinition, dataProps.Row, true);
return DataGridServiceUtils.ExtractCustomColumnsValues(dataGridValue, datagridDefinition, dataProps.Row, true);
}

private async Task<List<WorksheetFieldViewModel>> GetExistingRowAsync(RowInputData dataProps)
private async Task<(KeyValuePair<string, string>[] dynamicFields, List<WorksheetFieldViewModel> customFields)> GetExistingRowAsync(RowInputData dataProps)
{
if (dataProps.ValueId == null) throw new ArgumentNullException(nameof(dataProps));
var customFieldValue = await customFieldValueAppService.GetAsync(dataProps.ValueId.Value);
if (customFieldValue.CurrentValue == null) return [];
if (customFieldValue.CurrentValue == null) return ([], []);

var datagridDefinition = await customFieldAppService.GetAsync(dataProps.FieldId);
if (datagridDefinition == null) return [];
if (datagridDefinition == null) return ([], []);

var dataGridValue = JsonSerializer.Deserialize<DataGridValue>(customFieldValue.CurrentValue ?? "{}");

return DataGridServiceUtils.ConvertDataGridValue(dataGridValue, datagridDefinition, dataProps.Row, false);
return (DataGridServiceUtils.ExtractDynamicColumnsPairs(dataGridValue, dataProps.Row),
DataGridServiceUtils.ExtractCustomColumnsValues(dataGridValue, datagridDefinition, dataProps.Row, false));
}

private static bool AddNewRow(RowInputData dataProps)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Unity.Flex.Web.Pages.Flex
{
public static class DataGridServiceUtils
{
internal static List<WorksheetFieldViewModel> ConvertDataGridValue(DataGridValue? value,
internal static List<WorksheetFieldViewModel> ExtractCustomColumnsValues(DataGridValue? value,
CustomFieldDto datagridDefinition,
uint rowNumber,
bool isNew)
Expand Down Expand Up @@ -124,6 +124,28 @@ internal static List<DataGridRowCell> SetRowCells(List<Tuple<string, string, Cus
}
return cells;
}

internal static KeyValuePair<string, string>[] ExtractDynamicColumnsPairs(DataGridValue? dataGridValue, uint rowNumber)
{
var keyValues = new List<KeyValuePair<string, string>>();
var gridValue = DeserializeDataGridValue(dataGridValue?.Value?.ToString());
if (gridValue == null) return [];
var gridRowsValue = DeserializeDataGridRowsValue(dataGridValue?.Value?.ToString());
if (gridRowsValue == null) return [];
var row = gridRowsValue.Rows[(int)rowNumber];

foreach (var column in dataGridValue?.Columns ?? [])
{
var cell = row.Cells.Find(s => s.Key == column.Key);

if (cell != null)
{
keyValues.Add(new(column.Name, cell.Value.ApplyPresentationFormatting(column.Type, null)));
}
}

return [.. keyValues];
}
}

public class RowInputData
Expand Down
Loading

0 comments on commit 4a90da9

Please sign in to comment.