Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zeva 574 - adds ldv sales to supplier page #714

Merged
merged 7 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions backend/api/serializers/model_year_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from api.models.model_year_report_address import ModelYearReportAddress
from api.models.model_year_report_make import ModelYearReportMake
from api.models.model_year_report_statuses import ModelYearReportStatuses
from api.models.model_year_report_ldv_sales import ModelYearReportLDVSales
from api.serializers.model_year_report_ldv_sales import ModelYearReportLDVSalesSerializer
from api.models.user_profile import UserProfile
from api.serializers.model_year_report_address import \
ModelYearReportAddressSerializer
Expand All @@ -34,6 +36,29 @@ class ModelYearReportSerializer(ModelSerializer):
confirmations = SerializerMethodField()
statuses = SerializerMethodField()
ldv_sales_updated = SerializerMethodField()
ldv_sales_previous = SerializerMethodField()
avg_sales = SerializerMethodField()

def get_ldv_sales_previous(self, obj):
ldv_sales = ModelYearReportLDVSales.objects.filter(
model_year_report=obj
)
serializer = ModelYearReportLDVSalesSerializer(ldv_sales, many=True)
return serializer.data

def get_avg_sales(self, obj):
rows = ModelYearReportLDVSales.objects.filter(
model_year_report_id=obj.id
).values_list(
'ldv_sales', flat=True
)[:3]

avg_sales = 0
if rows.count() < 3:
return None
avg_sales = sum(list(rows)) / 3
return avg_sales
ldv_sales_updated = SerializerMethodField()

def get_create_user(self, obj):
user_profile = UserProfile.objects.filter(username=obj.create_user)
Expand Down Expand Up @@ -81,10 +106,11 @@ def get_statuses(self, obj):
class Meta:
model = ModelYearReport
fields = (
'organization_name', 'supplier_class', 'ldv_sales', 'model_year',
'organization_name', 'supplier_class', 'model_year',
'model_year_report_addresses', 'makes', 'validation_status',
'create_user', 'model_year_report_history', 'confirmations',
'statuses', 'ldv_sales_updated',
'statuses', 'ldv_sales_updated','statuses',
'ldv_sales_previous', 'avg_sales'
)


Expand Down Expand Up @@ -146,6 +172,7 @@ def create(self, validated_data):
makes = validated_data.pop('makes')
model_year = validated_data.pop('model_year')
confirmations = request.data.get('confirmations')
ldv_sales = request.user.organization.ldv_sales

report = ModelYearReport.objects.create(
model_year_id=model_year.id,
Expand All @@ -154,8 +181,14 @@ def create(self, validated_data):
**validated_data,
create_user=request.user.username,
update_user=request.user.username,
supplier_class=request.user.organization.supplier_class
)

for each in ldv_sales:
ModelYearReportLDVSales.objects.create(
model_year=each.model_year,
ldv_sales=each.ldv_sales,
model_year_report=report
)
for confirmation in confirmations:
ModelYearReportConfirmation.objects.create(
create_user=request.user.username,
Expand Down
4 changes: 2 additions & 2 deletions backend/api/serializers/model_year_report_ldv_sales.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rest_framework.serializers import ModelSerializer, SlugRelatedField
from api.serializers.model_year_report import ModelYearReportSerializer
# from api.serializers.model_year_report import ModelYearReportSerializer
from api.models.model_year import ModelYear

from api.models.model_year_report_ldv_sales import \
Expand All @@ -11,7 +11,7 @@ class ModelYearReportLDVSalesSerializer(ModelSerializer):
slug_field='name',
queryset=ModelYear.objects.all()
)
model_year_report = ModelYearReportSerializer
# model_year_report = ModelYearReportSerializer

class Meta:
model = ModelYearReportLDVSales
Expand Down
3 changes: 1 addition & 2 deletions backend/api/viewsets/model_year_report_consumer_sales.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
ModelYearReportHistorySerializer
from api.serializers.model_year_report_vehicle import \
ModelYearReportVehicleSerializer, ModelYearReportVehicleSaveSerializer
from api.serializers.model_year_report_ldv_sales import \
ModelYearReportLDVSalesSerializer

from api.serializers.model_year_report import ModelYearReportSerializer
from api.serializers.vehicle import ModelYearSerializer
from api.services.vehicle import vehicles_sales
Expand Down
32 changes: 15 additions & 17 deletions frontend/src/compliance/ConsumerSalesContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const ConsumerSalesContainer = (props) => {
validationStatus,
} = consumerSalesResponse.data;


if (vehicleList.length > 0) {
setVehicles(vehicleList);
}
Expand Down Expand Up @@ -94,7 +93,7 @@ const ConsumerSalesContainer = (props) => {
const handleCheckboxClick = (event) => {
if (!event.target.checked) {
const checked = checkboxes.filter(
(each) => Number(each) !== Number(event.target.id)
(each) => Number(each) !== Number(event.target.id),
);
setCheckboxes(checked);
}
Expand All @@ -106,21 +105,20 @@ const ConsumerSalesContainer = (props) => {
};

const handleSave = () => {
axios.post(ROUTES_COMPLIANCE.CONSUMER_SALES, {
data: vehicles,
modelYearReportId: id,
confirmation: checkboxes,
}).then(() => {
history.push(ROUTES_COMPLIANCE.REPORTS);
history.replace(ROUTES_COMPLIANCE.REPORT_CONSUMER_SALES.replace(':id', id));
}).catch((error) => {
const { response } = error;
if (response.status === 400) {
setErrorMessage(error.response.data.status);
}
});
}

axios.post(ROUTES_COMPLIANCE.CONSUMER_SALES, {
data: vehicles,
modelYearReportId: id,
confirmation: checkboxes,
}).then(() => {
history.push(ROUTES_COMPLIANCE.REPORTS);
history.replace(ROUTES_COMPLIANCE.REPORT_CONSUMER_SALES.replace(':id', id));
}).catch((error) => {
const { response } = error;
if (response.status === 400) {
setErrorMessage(error.response.data.status);
}
});
};

useEffect(() => {
refreshDetails(true);
Expand Down
41 changes: 38 additions & 3 deletions frontend/src/compliance/SupplierInformationContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const SupplierInformationContainer = (props) => {
const [checkboxes, setCheckboxes] = useState([]);
const [disabledCheckboxes, setDisabledCheckboxes] = useState('');
const [details, setDetails] = useState({});
const [previousYearsList, setPreviousYearsList] = useState([{}]);
const [modelYear, setModelYear] = useState(CONFIG.FEATURES.MODEL_YEAR_REPORT.DEFAULT_YEAR);
const [statuses, setStatuses] = useState({
supplierInformation: {
Expand Down Expand Up @@ -57,6 +58,9 @@ const SupplierInformationContainer = (props) => {
event.preventDefault();

const data = {
class: user.organization.supplierClass,
averageLdvSales: details.organization.avgLdvSales,
previousLdvSales: details.organization.ldvSales,
makes,
modelYear,
confirmations: checkboxes,
Expand Down Expand Up @@ -87,6 +91,27 @@ const SupplierInformationContainer = (props) => {
}
};

const getClassDescriptions = (supplierClass) => {
let supplierClassString = {};
if (supplierClass === 'L') {
supplierClassString = {
class: 'Large',
secondaryText: '(5,000 or more total LDV sales)',
};
} else if (supplierClass === 'M') {
supplierClassString = {
class: 'Medium',
secondaryText: '(1,000 to 4,999 total LDV sales)',
};
} else if (supplierClass === 'S') {
supplierClassString = {
class: 'Small',
secondaryText: '(less than 1,000 total LDV sales)',
};
}
return supplierClassString;
}

const handleCancelConfirmation = () => {
const data = {
delete_confirmations: true,
Expand All @@ -103,6 +128,9 @@ const SupplierInformationContainer = (props) => {
if (id && id !== 'new') {
axios.get(ROUTES_COMPLIANCE.REPORT_DETAILS.replace(/:id/g, id)).then((response) => {
const {
avgSales,
ldvSalesPrevious,
supplierClass,
makes: modelYearReportMakes,
modelYearReportAddresses,
modelYearReportHistory,
Expand All @@ -112,17 +140,21 @@ const SupplierInformationContainer = (props) => {
confirmations,
statuses: reportStatuses,
} = response.data;

setPreviousYearsList([{ id: 0, previousSales: 323, modelYear: 2020 }]);
setModelYear(parseInt(reportModelYear.name, 10));

if (modelYearReportMakes) {
const currentMakes = modelYearReportMakes.map((each) => (each.make));

setMakes(currentMakes);
}

const supplierClassString = getClassDescriptions(supplierClass);
console.log(ldvSalesPrevious)
setDetails({
supplierClassString,
organization: {
avgLdvSales: avgSales,
ldvSales: ldvSalesPrevious,
name: organizationName,
organizationAddress: modelYearReportAddresses,
},
Expand All @@ -140,9 +172,11 @@ const SupplierInformationContainer = (props) => {
} else {
axios.get(ROUTES_VEHICLES.LIST).then((response) => {
const { data } = response;

const previousSales = user.organization.ldvSales
const supplierClassString = getClassDescriptions(user.organization.supplierClass)
setMakes([...new Set(data.map((vehicle) => vehicle.make.toUpperCase()))]);
setDetails({
supplierClassString,
organization: user.organization,
supplierInformation: {
history: [],
Expand Down Expand Up @@ -194,6 +228,7 @@ const SupplierInformationContainer = (props) => {
details={details}
statuses={statuses}
id={id}
previousYearsList={previousYearsList}
/>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Modal from '../../app/components/Modal';
import history from '../../app/History';
import CustomPropTypes from '../../app/utilities/props';
import ROUTES_COMPLIANCE from '../../app/routes/Compliance';

import FormatNumeric from '../../app/utilities/formatNumeric';
import ComplianceReportAlert from './ComplianceReportAlert';
import ComplianceReportSignOff from './ComplianceReportSignOff';

Expand All @@ -31,12 +31,12 @@ const SupplierInformationDetailsPage = (props) => {
modelYear,
statuses,
id,
previousYearsList,
avgSales,
} = props;

const [showModal, setShowModal] = useState(false);
let disabledCheckboxes = propsDisabledCheckboxes;
let disabledInputs = false;

if (loading) {
return <Loading />;
}
Expand Down Expand Up @@ -143,6 +143,45 @@ const SupplierInformationDetailsPage = (props) => {
)
))}
</div>
<div className="mt-1 row">
<div className="col-sm-12 col-md-6">
<div className="mt-2 row">
<span className="col-4">
<h4 className="d-inline">Vehicle Supplier Class: </h4>
</span>
<span className="col-6">
<b>{details.supplierClassString.class} Volume Supplier</b>
<br />
{details.supplierClassString.secondaryText}
</span>
</div>
<div className="mt-2 row">
<span className="col-4">
<h4 className="d-inline">3 Year Average LDV Sales: </h4>
</span>
<span className="col-6">{FormatNumeric(details.organization.avgLdvSales)}</span>
</div>
</div>
<div className="col-sm-12 col-md-5">
<div className="supplier-information p-3">
{details.organization.ldvSales
&& (
<div className="previous-ldv-sales mt-2 p-3">
{details.organization.ldvSales.map((yearSale) => (
<div className="model-year-ldv" key={yearSale.id}>
<label className="text-blue mr-4 font-weight-bold">
{yearSale.modelYear} Model Year LDV Sales\Leases:
</label>
<label className="sales-numbers">
{yearSale.ldvSales}
</label>
</div>
))}
</div>
)}
</div>
</div>
</div>
<div className="d-block mt-3">
If there is an error in any of the information above, please contact: <a href="mailto:[email protected]">[email protected]</a>
</div>
Expand Down