invoice/lib/ui/reports/client_report.dart

301 lines
9.0 KiB
Dart

import 'dart:math';
import 'package:built_collection/built_collection.dart';
import 'package:invoiceninja_flutter/utils/enums.dart';
import 'package:invoiceninja_flutter/constants.dart';
import 'package:invoiceninja_flutter/data/models/client_model.dart';
import 'package:invoiceninja_flutter/data/models/company_model.dart';
import 'package:invoiceninja_flutter/data/models/models.dart';
import 'package:invoiceninja_flutter/redux/reports/reports_state.dart';
import 'package:invoiceninja_flutter/redux/static/static_state.dart';
import 'package:invoiceninja_flutter/ui/reports/reports_screen.dart';
import 'package:invoiceninja_flutter/utils/formatting.dart';
import 'package:memoize/memoize.dart';
enum ClientReportFields {
name,
website,
currency,
language,
private_notes,
public_notes,
industry,
size,
address_1,
address_2,
city,
state,
post_code,
phone,
country,
shipping_address_1,
shipping_address_2,
shipping_city,
shipping_state,
shipping_postal_code,
shipping_country,
custom_value_1,
custom_value_2,
custom_value_3,
custom_value_4,
created_by,
assigned_to,
balance,
credit_balance,
paid_to_date,
id_number,
vat_number,
contact_full_name,
contact_email,
contact_phone,
contact_custom_value_1,
contact_custom_value_2,
contact_custom_value_3,
contact_custom_value_4,
contact_last_login,
is_active,
created_at,
updated_at,
}
var memoizedClientReport = memo5((
UserCompanyEntity userCompany,
ReportsUIState reportsUIState,
BuiltMap<String, ClientEntity> clientMap,
BuiltMap<String, UserEntity> userMap,
StaticState staticState,
) =>
clientReport(userCompany, reportsUIState, clientMap, userMap, staticState));
ReportResult clientReport(
UserCompanyEntity userCompany,
ReportsUIState reportsUIState,
BuiltMap<String, ClientEntity> clientMap,
BuiltMap<String, UserEntity> userMap,
StaticState staticState,
) {
final List<List<ReportElement>> data = [];
BuiltList<ClientReportFields> columns;
final reportSettings = userCompany.settings.reportSettings;
final clientReportSettings =
reportSettings != null && reportSettings.containsKey(kReportClient)
? reportSettings[kReportClient]
: ReportSettingsEntity();
if (clientReportSettings.columns.isNotEmpty) {
columns = BuiltList(clientReportSettings.columns
.map((e) => EnumUtils.fromString(ClientReportFields.values, e))
.toList());
} else {
columns = BuiltList(<ClientReportFields>[
ClientReportFields.name,
ClientReportFields.contact_email,
ClientReportFields.id_number,
ClientReportFields.vat_number,
ClientReportFields.currency,
ClientReportFields.balance,
ClientReportFields.paid_to_date,
ClientReportFields.country,
]);
}
for (var clientId in clientMap.keys) {
final client = clientMap[clientId];
final contact = client.primaryContact;
if (client.isDeleted) {
continue;
}
bool skip = false;
final List<ReportElement> row = [];
for (var column in columns) {
dynamic value = '';
switch (column) {
case ClientReportFields.name:
value = client.name;
break;
case ClientReportFields.website:
value = client.website;
break;
case ClientReportFields.currency:
value =
staticState.currencyMap[client.currencyId]?.listDisplayName ?? '';
break;
case ClientReportFields.language:
value =
staticState.languageMap[client.languageId]?.listDisplayName ?? '';
break;
case ClientReportFields.private_notes:
value = client.privateNotes;
break;
case ClientReportFields.public_notes:
value = client.publicNotes;
break;
case ClientReportFields.industry:
value =
staticState.industryMap[client.industryId]?.listDisplayName ?? '';
break;
case ClientReportFields.size:
value = staticState.sizeMap[client.sizeId]?.listDisplayName ?? '';
break;
case ClientReportFields.custom_value_1:
value = client.customValue1;
break;
case ClientReportFields.custom_value_2:
value = client.customValue2;
break;
case ClientReportFields.custom_value_3:
value = client.customValue3;
break;
case ClientReportFields.custom_value_4:
value = client.customValue4;
break;
case ClientReportFields.address_1:
value = client.address1;
break;
case ClientReportFields.address_2:
value = client.address2;
break;
case ClientReportFields.city:
value = client.city;
break;
case ClientReportFields.state:
value = client.state;
break;
case ClientReportFields.post_code:
value = client.postalCode;
break;
case ClientReportFields.country:
value =
staticState.countryMap[client.countryId]?.listDisplayName ?? '';
break;
case ClientReportFields.shipping_address_1:
value = client.shippingAddress1;
break;
case ClientReportFields.shipping_address_2:
value = client.shippingAddress2;
break;
case ClientReportFields.shipping_city:
value = client.shippingCity;
break;
case ClientReportFields.shipping_state:
value = client.shippingState;
break;
case ClientReportFields.shipping_postal_code:
value = client.shippingPostalCode;
break;
case ClientReportFields.shipping_country:
value = staticState
.countryMap[client.shippingCountryId]?.listDisplayName ??
'';
break;
case ClientReportFields.phone:
value = client.phone;
break;
case ClientReportFields.id_number:
value = client.idNumber;
break;
case ClientReportFields.vat_number:
value = client.vatNumber;
break;
case ClientReportFields.assigned_to:
value = userMap[client.assignedUserId]?.listDisplayName ?? '';
break;
case ClientReportFields.created_by:
value = userMap[client.createdUserId]?.listDisplayName ?? '';
break;
case ClientReportFields.contact_full_name:
value = contact.fullName;
break;
case ClientReportFields.contact_email:
value = contact.email;
break;
case ClientReportFields.contact_phone:
value = contact.phone;
break;
case ClientReportFields.contact_custom_value_1:
value = contact.customValue1;
break;
case ClientReportFields.contact_custom_value_2:
value = contact.customValue2;
break;
case ClientReportFields.contact_custom_value_3:
value = contact.customValue3;
break;
case ClientReportFields.contact_custom_value_4:
value = contact.customValue4;
break;
case ClientReportFields.contact_last_login:
value = convertTimestampToDateString(contact.lastLogin);
break;
case ClientReportFields.balance:
value = client.balance;
break;
case ClientReportFields.credit_balance:
value = client.creditBalance;
break;
case ClientReportFields.paid_to_date:
value = client.paidToDate;
break;
case ClientReportFields.is_active:
value = client.isActive;
break;
case ClientReportFields.updated_at:
value = convertTimestampToDateString(client.updatedAt);
break;
case ClientReportFields.created_at:
value = convertTimestampToDateString(client.createdAt);
break;
}
if (!ReportResult.matchField(
value: value,
userCompany: userCompany,
reportsUIState: reportsUIState,
column: EnumUtils.parse(column),
)) {
skip = true;
}
if (value.runtimeType == bool) {
row.add(client.getReportBool(value: value));
} else if (value.runtimeType == double) {
row.add(client.getReportNumber(
value: value, currencyId: client.settings.currencyId));
} else {
row.add(client.getReportString(value: '$value'));
}
}
if (!skip) {
data.add(row);
}
}
data.sort((rowA, rowB) {
if (rowA.length <= clientReportSettings.sortIndex ||
rowB.length <= clientReportSettings.sortIndex) {
return 0;
}
final String valueA = rowA[clientReportSettings.sortIndex].value;
final String valueB = rowB[clientReportSettings.sortIndex].value;
if (clientReportSettings.sortAscending) {
return valueA.compareTo(valueB);
} else {
return valueB.compareTo(valueA);
}
});
return ReportResult(
allColumns:
ClientReportFields.values.map((e) => EnumUtils.parse(e)).toList(),
columns: columns.map((item) => EnumUtils.parse(item)).toList(),
data: data,
);
}