Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.18/5 (3 votes)
See more:
<?php
// Your code here!
$jsonAsString = '   {
    "header": {
        "dateTimeIssued": "2022-12-28T00:34:00Z",
        "receiptNumber": "INV-2204577",
        "uuid": "",
        "previousUUID": "",
        "referenceOldUUID": "",
        "currency": "EGP",
        "exchangeRate": 0,
        "sOrderNameCode": "sOrderNameCode",
        "orderdeliveryMode": "FC"
    },
    "documentType": {
        "receiptType": "SC",
        "typeVersion": "1.2"
    },
    "seller": {
        "rin": "619013583",
        "companyTradeName": "شركة الصوفى",
        "branchCode": "0",
        "branchAddress": {
            "country": "EG",
            "governate": "cairo",
            "regionCity": "city center",
            "street": "16 street",
            "buildingNumber": "14BN",
            "postalCode": "74235",
            "floor": "1F",
            "room": "3R",
            "landmark": "tahrir square",
            "additionalInformation": "talaat harb street"
        },
        "deviceSerialNumber": "Soft2010",
        "activityCode": "5610"
    },
    "buyer": {
        "type": "F",
        "id": "313717919",
        "name": "taxpayer 1",
        "mobileNumber": "+201020567462",
        "paymentNumber": "987654"
    },
    "itemData": [
        {
            "internalCode": "880609",
            "description": "Samsung A02 32GB_LTE_BLACK_DS_SM-A022FZKDMEB_A022 _ A022_SM-A022FZKDMEB",
            "itemType": "GS1",
            "itemCode": "037000401629",
            "unitType": "EA",
            "quantity": 35,
            "unitPrice":  247.96000 ,
            "netSale":  7810.74000 ,
            "totalSale":  8678.60000 ,
            "total":  8887.04360 ,
            "commercialDiscountData": [
                 {
                     "amount": 867.86000, 
                     "description": "XYZ"
                 }
            ],
            "itemDiscountData": [
                {
                    "amount": 10,
                    "description":"ABC"
                },
                {
                    "amount": 10,
                    "description": "XYZ"
                }
            ],
            "valueDifference": 20,
            "taxableItems": [
                {
                        "taxType": "T1",
                        "amount":  1096.30360 ,
                        "subType": "V009",
                        "rate": 14
                }
            ]
        }
    ],
    "totalSales": 8678.60000,
    "totalCommercialDiscount": 867.86000,
    "totalItemsDiscount": 20,
    "extraReceiptDiscountData": [
       {
           "amount": 0,
           "description": "ABC"
       }
    ],
    "netAmount": 7810.74000,
    "feesAmount": 0,
    "totalAmount": 8887.04360,
    "taxTotals": [
            {
                "taxType": "T1",
                "amount": 1096.30360
            }
    ],
    "paymentMethod": "C",
    "adjustment": 0
}';

$jsonAsArray = json_decode($jsonAsString);

function Serialize11($documentStructure): string
{
    if (is_float($documentStructure)) {
        return '"' . number_format($documentStructure, 5, '.', '') . '"';
    }
    
    if (!is_object($documentStructure) and !is_array($documentStructure)) {
        return '"' . $documentStructure . '"';
    }
    
    $serializedString = '';
    

    foreach ($documentStructure as $key => $value) {
        if (! is_array($value)) {
            $serializedString .= '"' . strtoupper($key) . '"';
            $serializedString .= Serialize11($value);
        }
        
        if (is_array($value)) {
            $serializedString .= '"' . strtoupper($key) . '"';
            foreach ($value as $elementItem) {
                $serializedString .= '"' . strtoupper($key) . '"';
                $serializedString .= Serialize11($elementItem);
            }
        }
        
    }
    
    return $serializedString;
}

$serializedString = Serialize11($jsonAsArray);

$uuid = hash('sha256', $serializedString);


echo strtoupper ($uuid);
?>


What I have tried:

<pre><?php
// Your code here!
$jsonAsString = '   {
    "header": {
        "dateTimeIssued": "2022-12-28T00:34:00Z",
        "receiptNumber": "INV-2204577",
        "uuid": "",
        "previousUUID": "",
        "referenceOldUUID": "",
        "currency": "EGP",
        "exchangeRate": 0,
        "sOrderNameCode": "sOrderNameCode",
        "orderdeliveryMode": "FC"
    },
    "documentType": {
        "receiptType": "SC",
        "typeVersion": "1.2"
    },
    "seller": {
        "rin": "619013583",
        "companyTradeName": "شركة الصوفى",
        "branchCode": "0",
        "branchAddress": {
            "country": "EG",
            "governate": "cairo",
            "regionCity": "city center",
            "street": "16 street",
            "buildingNumber": "14BN",
            "postalCode": "74235",
            "floor": "1F",
            "room": "3R",
            "landmark": "tahrir square",
            "additionalInformation": "talaat harb street"
        },
        "deviceSerialNumber": "Soft2010",
        "activityCode": "5610"
    },
    "buyer": {
        "type": "F",
        "id": "313717919",
        "name": "taxpayer 1",
        "mobileNumber": "+201020567462",
        "paymentNumber": "987654"
    },
    "itemData": [
        {
            "internalCode": "880609",
            "description": "Samsung A02 32GB_LTE_BLACK_DS_SM-A022FZKDMEB_A022 _ A022_SM-A022FZKDMEB",
            "itemType": "GS1",
            "itemCode": "037000401629",
            "unitType": "EA",
            "quantity": 35,
            "unitPrice":  247.96000 ,
            "netSale":  7810.74000 ,
            "totalSale":  8678.60000 ,
            "total":  8887.04360 ,
            "commercialDiscountData": [
                 {
                     "amount": 867.86000, 
                     "description": "XYZ"
                 }
            ],
            "itemDiscountData": [
                {
                    "amount": 10,
                    "description":"ABC"
                },
                {
                    "amount": 10,
                    "description": "XYZ"
                }
            ],
            "valueDifference": 20,
            "taxableItems": [
                {
                        "taxType": "T1",
                        "amount":  1096.30360 ,
                        "subType": "V009",
                        "rate": 14
                }
            ]
        }
    ],
    "totalSales": 8678.60000,
    "totalCommercialDiscount": 867.86000,
    "totalItemsDiscount": 20,
    "extraReceiptDiscountData": [
       {
           "amount": 0,
           "description": "ABC"
       }
    ],
    "netAmount": 7810.74000,
    "feesAmount": 0,
    "totalAmount": 8887.04360,
    "taxTotals": [
            {
                "taxType": "T1",
                "amount": 1096.30360
            }
    ],
    "paymentMethod": "C",
    "adjustment": 0
}';

$jsonAsArray = json_decode($jsonAsString);

function Serialize11($documentStructure): string
{
    if (is_float($documentStructure)) {
        return '"' . number_format($documentStructure, 5, '.', '') . '"';
    }
    
    if (!is_object($documentStructure) and !is_array($documentStructure)) {
        return '"' . $documentStructure . '"';
    }
    
    $serializedString = '';
    

    foreach ($documentStructure as $key => $value) {
        if (! is_array($value)) {
            $serializedString .= '"' . strtoupper($key) . '"';
            $serializedString .= Serialize11($value);
        }
        
        if (is_array($value)) {
            $serializedString .= '"' . strtoupper($key) . '"';
            foreach ($value as $elementItem) {
                $serializedString .= '"' . strtoupper($key) . '"';
                $serializedString .= Serialize11($elementItem);
            }
        }
        
    }
    
    return $serializedString;
}

$serializedString = Serialize11($jsonAsArray);

$uuid = hash('sha256', $serializedString);


echo strtoupper ($uuid);
?>
Posted
Updated 4-Jan-23 20:01pm
Comments

1 solution

This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it'll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900