Skip to content

Commit

Permalink
Merge #6765
Browse files Browse the repository at this point in the history
6765: More shipment fixes and improvements r=mbayopanda a=jmcameron

Updates
      - Round weights for shipment document/manifest
      - Enforce uniqueness of shipment container labels

Closes #6762

**TESTING** 
- Use bhima_test
- Create a shipment and try to create two containers with the same label
- Create/edit a container and try entering a negative weight.


Co-authored-by: Jonathan Cameron <jmcameron@gmail.com>
  • Loading branch information
bors[bot] and jmcameron authored Aug 5, 2022
2 parents 087621c + f1fe2d0 commit 43a5586
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 13 deletions.
11 changes: 9 additions & 2 deletions client/src/modules/shipment/create-shipment.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,10 @@ function CreateShipmentController(
};

vm.newContainer = function newContainer() {
ShipmentModal.openEditContainerModal({ action : 'create' })
ShipmentModal.openEditContainerModal({
action : 'create',
known : vm.containers.map(c => c.label),
})
.then((result) => {
if (!result) { return; }

Expand Down Expand Up @@ -274,7 +277,11 @@ function CreateShipmentController(
};

vm.editContainer = function editContainer(container) {
ShipmentModal.openEditContainerModal({ action : 'edit', container })
ShipmentModal.openEditContainerModal({
action : 'edit',
container,
known : vm.containers.map(c => c.label),
})
.then((result) => {
if (!result) { return; }

Expand Down
11 changes: 8 additions & 3 deletions client/src/modules/shipment/modals/edit-container.modal.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<form name="ActionForm" bh-submit="$ctrl.submit(ActionForm)" novalidate>
<form name="ContainerForm" bh-submit="$ctrl.submit(ContainerForm)" novalidate>
<div class="modal-header">
<ol class="headercrumb text-bold">
<li class="static" translate>SHIPMENT.SHIPMENTS</li>
Expand All @@ -16,6 +16,7 @@
text-value="$ctrl.container.label"
label="FORM.LABELS.LABEL"
autocomplete="off"
on-change="$ctrl.checkLabel(key, value)"
required="true">
</bh-input-text>
</div>
Expand All @@ -33,14 +34,18 @@
</ui-select>
</div>

<div class="form-group">
<label class="control-label" for="containerType" translate>SHIPMENT.CONTAINER_WEIGHT</label>
<div class="form-group" ng-class="{ 'has-error' : ContainerForm.weight.$invalid }">
<label class="control-label" for="weight" translate>SHIPMENT.CONTAINER_WEIGHT</label>
<input
name="weight"
type="number"
min="0"
class="form-control"
ng-model="$ctrl.container.weight">
<div class="help-block"
ng-messages="ContainerForm.weight.$error" ng-show="ContainerForm.$submitted">
<div ng-messages-include="modules/templates/messages.tmpl.html"></div>
</div>
</div>

</div> <!-- end of modal-body -->
Expand Down
43 changes: 39 additions & 4 deletions client/src/modules/shipment/modals/edit-container.modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@ function ContainerEditModalController(Data, Containers, Notify, Instance) {
vm.isCreate = Data.action === 'create';
vm.existingContainer = Data.action === 'edit' && !(NOT_CREATED in Data.container);

vm.knownContainers = new Set();
if (Data.known) {
Data.known.forEach(lab => {
vm.knownContainers.add(lab.toLowerCase());
});
}

vm.loading = false;

vm.container = {};

vm.cancel = () => Instance.close(null);
vm.submit = submit;

vm.checkLabel = checkLabel;

function startup() {
vm.loading = true;

Expand All @@ -32,16 +41,42 @@ function ContainerEditModalController(Data, Containers, Notify, Instance) {
})
.then(() => {
if (Data.container) {
// This means we are editing
vm.container = Data.container;
vm.loading = false;
} else {
vm.loading = false;

// Don't complain if we re-use the same label while editing
vm.knownContainers.delete(vm.container.label.toLowerCase());
}
})
.finally(() => {
vm.loading = false;
});
}

/* tslint: disable:no-unused-variable */
function checkLabel(key, value) {
if (vm.form) {
// If we have entered a duplicate label before, reset the field and form when it is okay
const valid = value ? !vm.knownContainers.has(value.toLowerCase()) : true;
vm.form.textValueForm.inputTextElement.$invalid = !valid;
vm.form.textValueForm.inputTextElement.$valid = valid;
vm.form.$valid = valid;
vm.form.$invalid = !valid;
}
}

function submit(form) {
if (form.$invalid) { return 0; }
if (form.$invalid) { return false; }

// Make sure the proposed container label is unique
// @TODO: Add a validator to bhInputText component to avoid these hacks
if (vm.knownContainers.has(vm.container.label.toLowerCase())) {
form.textValueForm.inputTextElement.$invalid = true;
form.textValueForm.inputTextElement.$valid = false;
form.textValueForm.inputTextElement.$setValidity('unique', false);
vm.form = form; // save a reference for resetting the form when the label is corrected
return false;
}

// NOTE: If the container did not already exist, do not create a new one.
// Let the parent controller (create_shipment) do that when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,20 @@ async function getShipmentDocument(req, res, next) {
sortItems(containerItems);
contents.push({
containerName : cntr.label,
containerEmptyWeight : cntr.weight,
containerEmptyWeight : _.round(cntr.weight, 2),
containerType : `SHIPMENT.CONTAINER_TYPES.${cntr.container_type}`,
containerWeight : cntr.weight + containerItems.reduce((agg, row) => agg + row.weight, 0),
containerWeight : _.round(cntr.weight + containerItems.reduce((agg, row) => agg + row.weight, 0), 2),
containerValue : containerItems.reduce((agg, row) => agg + row.cost, 0),
items : containerItems,
});
shipment.totalWeight += cntr.weight;
});
// Round weights for display
shipment.totalWeight = _.round(shipment.totalWeight, 2);
shipmentItems.forEach(row => {
row.weight = _.round(row.quantity_sent * row.unit_weight, 2);
row.unit_weight = _.round(row.unit_weight, 2);
});
} else {
sortItems(shipmentItems);
contents = [{
Expand Down
6 changes: 4 additions & 2 deletions server/models/migrations/next/migrate.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ CREATE TABLE `shipment_container` (
`date_sent` DATETIME,
`date_received` DATETIME,
PRIMARY KEY (`uuid`),
UNIQUE KEY `shipment_container__unique_label` (`label`),
CONSTRAINT `shipment_container__type` FOREIGN KEY (`container_type_id`) REFERENCES `shipment_container_types` (`id`),
CONSTRAINT `shipment_container__shipment` FOREIGN KEY (`shipment_uuid`) REFERENCES `shipment` (`uuid`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARACTER SET = utf8mb4 DEFAULT COLLATE = utf8mb4_unicode_ci;

CALL add_column_if_missing('shipment_item', 'container_uuid', 'BINARY(16) NULL');

INSERT IGNORE INTO `shipment_container_types`
(`id`, `text`, `predefined`)
VALUES
Expand All @@ -49,3 +48,6 @@ INSERT IGNORE INTO `shipment_container_types`
(15, 'REAM', 1),
(16, 'SACK', 1),
(17, 'SET', 1);

CALL add_column_if_missing('shipment_item', 'container_uuid', 'BINARY(16) NULL');
CALL add_constraint_if_missing('shipment_item', 'shipment_item__shipment', 'FOREIGN KEY (`shipment_uuid`) REFERENCES `shipment` (`uuid`) ON DELETE CASCADE');
1 change: 1 addition & 0 deletions server/models/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2757,6 +2757,7 @@ CREATE TABLE `shipment_container` (
`date_sent` DATETIME,
`date_received` DATETIME,
PRIMARY KEY (`uuid`),
UNIQUE KEY `shipment_container__unique_label` (`label`),
CONSTRAINT `shipment_container__type` FOREIGN KEY (`container_type_id`) REFERENCES `shipment_container_types` (`id`),
CONSTRAINT `shipment_container__shipment` FOREIGN KEY (`shipment_uuid`) REFERENCES `shipment` (`uuid`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARACTER SET = utf8mb4 DEFAULT COLLATE = utf8mb4_unicode_ci;
Expand Down

0 comments on commit 43a5586

Please sign in to comment.