import React, { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { FlatList, StyleSheet, Text, View } from "react-native"; import _ from "lodash"; import { ActivityIndicator, Card, DataTable, Divider } from "react-native-paper"; import { GET_LINE_TICKET_BY_PK } from "../../graphql/jobs.queries"; import ErrorDisplay from "../error-display/error-display.component"; import { useQuery } from "@apollo/client"; import { connect } from "react-redux"; import { createStructuredSelector } from "reselect"; import { selectBodyshop } from "../../redux/user/user.selectors"; import { selectCurrentEmployee } from "../../redux/employee/employee.selectors"; import { CalculateAllocationsTotals } from "../../util/labor-allocations-table.utility"; const mapStateToProps = createStructuredSelector({ bodyshop: selectBodyshop, technician: selectCurrentEmployee, }); export function LaborAllocationsTable({ jobId, bodyshop, technician,costCenterDiff,selectedCostCenter, style, shouldRefresh}) { const { t } = useTranslation(); const onRefresh = async () => { return refetch(); }; const { loading, error, data, refetch } = useQuery(GET_LINE_TICKET_BY_PK, { variables: { id: jobId }, skip: !!!jobId, fetchPolicy: "network-only", nextFetchPolicy: "network-only", }); if (error) return ; const [totals, setTotals] = useState([]); useEffect(() => { if (!!data?.joblines && !!data?.timetickets && !!bodyshop){ let temptotals = CalculateAllocationsTotals( bodyshop, data?.joblines, data?.timetickets, data?.adjustments ); if(!!selectedCostCenter){ let tempCostCenterDiff = Math.round( temptotals.find( (total) => total["cost_center"] === selectedCostCenter.label )?.difference * 10 ) / 10; costCenterDiff.current= tempCostCenterDiff; } setTotals( temptotals ); } if (!jobId) setTotals([]); }, [data?.joblines, data?.timetickets, bodyshop, data?.adjustments, jobId, selectedCostCenter]); useEffect(() => { !!shouldRefresh && shouldRefresh ? onRefresh() : null },[shouldRefresh]); const summary = totals && totals.reduce( (acc, val) => { acc.hrs_total += val.total; acc.hrs_claimed += val.claimed; acc.adjustments += val.adjustments; acc.difference += val.difference; return acc; }, { hrs_total: 0, hrs_claimed: 0, adjustments: 0, difference: 0 } ); if (loading) return ; return ( {typeof data !== "undefined" ? ( {t("laborallocations.labels.costcenter")} {t("laborallocations.labels.hourstotal")} {t("laborallocations.labels.hoursclaimed")} {/* Hours Claimed */} {t("laborallocations.labels.adjustments")} {t("laborallocations.labels.difference")} item.cost_center} ItemSeparatorComponent={} renderItem={(object) => ( {object.item.cost_center} {" ("} {object.item.mod_lbr_ty} {")"} {object.item.total && object.item.total.toFixed(1)} {object.item.claimed && object.item.claimed.toFixed(1)} {object.item.adjustments && object.item.adjustments.toFixed(1)} = 0 ? "green" : "red", }} > {_.round(object.item.difference, 1)} )} /> {summary && ( {t("laborallocations.labels.totals")} {summary.hrs_total.toFixed(1)} {summary.hrs_claimed.toFixed(1)} {summary.adjustments.toFixed(1)} {summary.difference.toFixed(1)} )} ) : null} {/* use "totals" for the rows in the table */} {/* use "summary" for the totals at the bottom */} ); } const localStyles = StyleSheet.create({ headerArea: { flexDirection: "row", justifyContent: "center", alignItems: "center", margin: 1, }, footertext: { flex: 1, textAlign: "center", textAlignVertical: "center", margin: 1, paddingBottom: 8, }, headertext: { flex: 1, textAlign: "center", textAlignVertical: "center", margin: 1, paddingBottom: 8, }, headertextAdjusts: { flex: 1, textAlign: "center", textAlignVertical: "center", margin: 1, paddingBottom: 8, }, }); export default connect(mapStateToProps, null)(LaborAllocationsTable);