I am working on a use case of "Purchase Order" wherein there will be multiple Products listed/ordered under one Purchase Order(PO) by a buyer.
1 PO : N Products
struct Product{
bytes32 product_code;
bytes32 product_name;
uint quantity;
uint unit_price;
}
struct PurchaseOrder{
bytes32 po_number;
string po_creation_date;
// next line is the question
mapping( bytes32 => Product[]) products; // or Products[] products
}
//mapping to store Purchase Orders
mapping(uint => PurchaseOrder) purchaseOrders;
uint po_number = 100;
function createPO(string memory po_creation_date,bytes32[] memory products) public returns(bool){
require(msg.sender == buyerAddress);
po_number++;
purchaseOrders[po_number].po_creation_date = po_creation_date;
for (uint256 i = 0; i < products.length; i++) {
bytes32[] memory arr=new bytes32[](products.length);
arr[0]= products[i];
//bytes32 product_code = arr[0].product_code;
purchaseOrders[po_number].products[product_code].push(Product(arr)); // how do i need to proceed ??
}
}
parameter bytes32[] memory products comes from nodejs app and as a array of objects.
I must also be able to retrieve all the products under a PO for particular buyer.
Improve this code or if this code does not make sense and can be rewritten to achieve same, please feel free to do so or guide me.
TiA