|
| 1 | +// SPDX-License-Identifier: ISC |
| 2 | +pragma solidity ^0.8.24; |
| 3 | + |
| 4 | +contract ServiceReceipt { |
| 5 | + struct Receipt { |
| 6 | + uint256 id; |
| 7 | + address customer; |
| 8 | + string productCode; |
| 9 | + string deliveryState; |
| 10 | + string metadataUri; |
| 11 | + uint256 createdAt; |
| 12 | + bool active; |
| 13 | + } |
| 14 | + |
| 15 | + address public owner; |
| 16 | + uint256 public nextReceiptId = 1; |
| 17 | + mapping(uint256 => Receipt) public receipts; |
| 18 | + |
| 19 | + event ReceiptCreated(uint256 indexed id, address indexed customer, string productCode); |
| 20 | + event DeliveryStateUpdated(uint256 indexed id, string deliveryState); |
| 21 | + event ReceiptDeactivated(uint256 indexed id); |
| 22 | + |
| 23 | + modifier onlyOwner() { |
| 24 | + require(msg.sender == owner, "Only owner can perform this action"); |
| 25 | + _; |
| 26 | + } |
| 27 | + |
| 28 | + constructor() { |
| 29 | + owner = msg.sender; |
| 30 | + } |
| 31 | + |
| 32 | + function createReceipt( |
| 33 | + address customer, |
| 34 | + string memory productCode, |
| 35 | + string memory deliveryState, |
| 36 | + string memory metadataUri |
| 37 | + ) external onlyOwner returns (uint256) { |
| 38 | + uint256 receiptId = nextReceiptId; |
| 39 | + receipts[receiptId] = Receipt({ |
| 40 | + id: receiptId, |
| 41 | + customer: customer, |
| 42 | + productCode: productCode, |
| 43 | + deliveryState: deliveryState, |
| 44 | + metadataUri: metadataUri, |
| 45 | + createdAt: block.timestamp, |
| 46 | + active: true |
| 47 | + }); |
| 48 | + |
| 49 | + nextReceiptId += 1; |
| 50 | + emit ReceiptCreated(receiptId, customer, productCode); |
| 51 | + return receiptId; |
| 52 | + } |
| 53 | + |
| 54 | + function updateDeliveryState(uint256 receiptId, string memory deliveryState) external onlyOwner { |
| 55 | + require(receipts[receiptId].active, "Receipt is not active"); |
| 56 | + receipts[receiptId].deliveryState = deliveryState; |
| 57 | + emit DeliveryStateUpdated(receiptId, deliveryState); |
| 58 | + } |
| 59 | + |
| 60 | + function deactivateReceipt(uint256 receiptId) external onlyOwner { |
| 61 | + require(receipts[receiptId].active, "Receipt already inactive"); |
| 62 | + receipts[receiptId].active = false; |
| 63 | + emit ReceiptDeactivated(receiptId); |
| 64 | + } |
| 65 | +} |
0 commit comments