unepwcmc/SAPI — pull request #151 by agnessa (2013-12-17 15:34:19+00:00)
Title: Multiple import and origin permits

This PR allows a shipment to have multiple import permits or origin permits (previously it could have only one of those, while we allowed for multiple export permits).

The database structure has been amended to represent many to many relationships between shipments and different types of permits (while the two foreign keys previously present in the shipments table for origin permit & import permit have been removed).

We use a seperate joining table for each type of permit (trade_shipment_export_permits, trade_shipment_import_permits, trade_shipment_origin_permits), because this way each of them can have a UNIQUE index defined on shipment_id, permit_id pair, which should be more performant. Also, this way we reduce the potentially large size of a common joining table. This might lead to performance issues anyway though; once we verify how that behaves when we get the shipments data in, we could discuss whether it would make sense to cache the import / export / origin numbers in the shipments table.

How this affects existing functionality:
-> submitting shipments had to be amended to parse import & origin permit fields exactly like in the case of export permits
-> search by permit number had to be amended (+ its underlying view)
-> editing shipments one by one: a problem was discovered where new permits would be created without the geo entity; we should always try to assign a geo entity to a permit if we can. For an export permit we assign the exporter, for an import permit the importer and for origin permit the origin; only trouble there being that origin is not a required field. I added a validation to require that origin is specified if origin permit is given, so that we avoid creating a permit without geo entity in this case.