pragma solidity ^0.4.18; contract LogisticsContract { struct Shipment { uint shipmentId; // Unique ID for the shipment address sender; // Address of the sender address receiver; // Address of the receiver bytes32 certificate; // Certificate related to the shipment uint timestamp; // Timestamp of the shipment creation mapping(string => address) entities; // Mapping to store the addresses of different entities involved } Shipment[] public shipments; mapping(uint => bool) public shipmentExists; mapping(uint => bool) public shipmentReceived; event ShipmentCreated(uint shipmentId, address sender, address receiver, bytes32 certificate, uint timestamp); event ShipmentReceived(uint shipmentId, address receiver, uint timestamp); event EntityInvolved(uint shipmentId, string role, address entityAddress); function createShipment(uint shipmentId, address receiver, bytes32 certificate) public { require(!shipmentExists[shipmentId]); require(receiver != address(0)); require(certificate != bytes32(0)); Shipment memory newShipment = Shipment({ shipmentId: shipmentId, sender: msg.sender, receiver: receiver, certificate: certificate, timestamp: block.timestamp }); shipments.push(newShipment); shipmentExists[shipmentId] = true; ShipmentCreated(shipmentId, msg.sender, receiver, certificate, block.timestamp); } function addEntity(uint shipmentId, string role, address entityAddress) public { require(shipmentExists[shipmentId]); Shipment storage shipment = shipments[shipmentId]; shipment.entities[role] = entityAddress; EntityInvolved(shipmentId, role, entityAddress); } function receiveShipment(uint shipmentId) public { require(shipmentExists[shipmentId]); require(!shipmentReceived[shipmentId]); shipmentReceived[shipmentId] = true; ShipmentReceived(shipmentId, msg.sender, block.timestamp); } function getShipmentCount() public view returns (uint) { return shipments.length; } function getShipmentDetails(uint shipmentId) public view returns (address sender, address receiver, bytes32 certificate, uint timestamp, bool received) { require(shipmentExists[shipmentId]); Shipment memory shipment = shipments[shipmentId]; return (shipment.sender, shipment.receiver, shipment.certificate, shipment.timestamp, shipmentReceived[shipmentId]); } function getEntityAddress(uint shipmentId, string role) public view returns (address) { require(shipmentExists[shipmentId]); Shipment storage shipment = shipments[shipmentId]; return shipment.entities[role]; } }
0.4.18