<?php
/**
 * Copyright 2018 Huawei Technologies Co.,Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License.  You may obtain a copy of the
 * License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * sdk reference linking：https://support.huaweicloud.com/sdkreference-ocr/ocr_04_0016.html
 */


require_once "./HWOcrClient/HWOcrClientAKSK.php";
require_once "./HWOcrClient/HWOcrClientToken.php";


function TokenRequest() {
    $domainName = "xxx";
    $username = "xxx";
    $password = "xxx";
    $regionName = "cn-north-4";
    $uri = "/v2/{project_id}/ocr/auto-classification";
    $imagePath = __DIR__."/data/vat-invoice-demo.jpg"; // File path or URL of the image to be recognized.

    $ocrServer = new HWOcrClientToken(
        $username,
        $password,
        $domainName,
        $regionName,
        $uri
    );

    $option = array(
        "type_list" => array("vat_invoice", "id_card_protrait_side")
    );

    $result =  $ocrServer->RequestOcrResult($imagePath, $option);
    echo "response = {$result}<br>";

    /*
     * data analysis
     */
    $responseDict = json_decode($result, true);
    DecodeResponse($responseDict);
}

function AkskRequst() {
    $appKey = "xxx";
    $appSecret = "xxx";
    $regionName = "cn-north-4";
    $uri = "/v2/{project_id}/ocr/auto-classification";
    $imagePath = __DIR__."/data/vat-invoice-demo.jpg"; // File path or URL of the image to be recognized.

    $akskAuth = new HWOcrClientAKSK(
        $appKey,
        $appSecret,
        $regionName,
        $uri
    );
    $option = array(
        "type_list" => array("vat_invoice", "id_card_protrait_side")
    );

    $result =  $akskAuth->RequestOcrAkSkService($imagePath, $option);
    echo "response = {$result}<br>";

    /*
     * data analysis
     */
    $responseDict = json_decode($result, true);
    DecodeResponse($responseDict);
}

function DecodeResponse($responseDict) {
    $resultArr = $responseDict["result"];
    foreach ($resultArr as $key => $item) {
        echo "$item = " . json_encode($item) ."<br>";
        $type = $item["type"];
        $content = $item["content"];
        echo "$type = ". $type ."<br>";
        if ("id_card_portrait_side" == $type) {
            $name = $content["name"];
            $sex = $content["sex"];
            $ethnicity = $content["ethnicity"];
            $birth = $content["ethnicity"];
            $address = $content["address"];
            $number = $content["number"];
            echo "$name = " . $name . "<br>$sex = " . $sex . "<br>$ethnicity = " . $ethnicity . "<br>";
        } elseif ("vat_invoice" == $type) {
            $itemtype = $content["type"];
            $serial_number = $content["serial_number"];
            $attribution = $content["attribution"];
            $code = $content["code"];
            $check_code = $content["check_code"];
            $machine_number = $content["machine_number"];
            $print_number = $content["print_number"];
            $number = $content["number"];
            $issue_date = $content["issue_date"];
            $encryption_block = $content["encryption_block"];
            $buyer_name = $content["buyer_name"];
            $buyer_id = $content["buyer_id"];
            $buyer_address = $content["buyer_address"];
            $buyer_bank = $content["buyer_bank"];
            $seller_name = $content["seller_name"];
            $seller_id = $content["seller_id"];
            $seller_address = $content["seller_address"];
            $seller_bank = $content["seller_bank"];
            $subtotal_amount = $content["subtotal_amount"];
            $subtotal_tax = $content["subtotal_tax"];
            $total = $content["total"];
            $total_in_words = $content["total_in_words"];
            $remarks = $content["remarks"];
            $receiver = $content["receiver"];
            $reviewer = $content["reviewer"];
            $issuer = $content["issuer"];
            echo "$itemtype = " . $itemtype . "<br>$serial_number = " . $serial_number . "<br>";

            $supervision_seal = $content["supervision_seal"];
            echo "$supervision_seal = " . json_encode($supervision_seal) . "<br>";
            $seller_seal = $content["seller_seal"];
            echo "$seller_seal = " . json_encode($seller_seal) . "<br>";
            $item_list = $content["item_list"];
            foreach ($item_list as $key2 => $listItem) {
                echo "$listItem = " . json_encode($listItem) . "<br>";
                $name = $listItem["name"];
                $specification = $listItem["specification"];
                $unit = $listItem["unit"];
                $quantity = $listItem["quantity"];
                $unit_price = $listItem["unit_price"];
                $license_plate_number = $listItem["license_plate_number"];
                $vehicle_type = $listItem["vehicle_type"];
                $start_date = $listItem["start_date"];
                $end_date = $listItem["end_date"];
                $amount = $listItem["amount"];
                $tax_rate = $listItem["tax_rate"];
                $tax = $listItem["tax"];
                echo "$name = " . $name ."<br>$unit = " . $unit . "<br>$quantity = " . $quantity;
            }
        }
    }
}

TokenRequest();
AkskRequst();