<?php
    include "../db/headerApi.php";
    include "../db/connection_main.php";

    $json = file_get_contents('php://input');
    $data = json_decode($json);

    $copy_res = [];
    foreach ($data->data as $row) {

        //Check if exists
        if (!empty($row->SKU_SIZE)) {
            $sizeWhere = " AND sku_size = '".$row->SKU_SIZE."' ";
        } else {
            $sizeWhere = "";
        }

        if (!empty($row->BRAND)) {
            $brandWhere = " AND brand = '".$row->BRAND."' ";
        } else {
            $brandWhere = "";
        }

        $sql = "SELECT COUNT(1) ctr
                  FROM apps.yty_g_prc_modifier_list_tl
                 WHERE period = :period
                   AND customer_id = :customerid
                   AND product_attr_value = :sku
                   ".$sizeWhere."
                   ".$brandWhere;

        $sqlarr = oci_parse($dbcon , $sql);
        oci_bind_by_name($sqlarr, ':period', $data->target_period->ID);
        oci_bind_by_name($sqlarr, ':customerid', $row->CUSTOMER_ID);
        oci_bind_by_name($sqlarr, ':sku', $row->PRODUCT_ATTR_VALUE);
        $r = oci_execute($sqlarr);
        while ($rw = oci_fetch_array($sqlarr)) {
            $exists = $rw['CTR'];
        }

        if ($exists == "0") {
            $sql = "SELECT to_char(to_date(:period, 'MON-RR'), 'YYYY-MM-DD') start_date,
                           to_char(last_day(to_date(:period, 'MON-RR')), 'YYYY-MM-DD') end_date
                      FROM dual";

            $sqlarr = oci_parse($dbcon , $sql);
            oci_bind_by_name($sqlarr, ':period', $data->target_period->ID);
            $r = oci_execute($sqlarr);
            oci_fetch_all($sqlarr, $row_period, null, null, OCI_FETCHSTATEMENT_BY_ROW);

            $name="YTY-QP-".$_SESSION['P_UNAME']."-".$row->CURRENCY_CODE."-".$data->target_period->ID;
            $start_date = $row_period[0]["START_DATE"];
            $end_date = $row_period[0]["END_DATE"];

            $sql = "INSERT INTO apps.yty_g_prc_modifier_list_tl
                                (name, description, currency_code, period, start_date, end_date, 
                                 product_uom_code, product_attr_value, category_id, new_price, customer_name, customer_id, created_by, 
                                 creation_date, comments, volume, bill_to, ship_to, sku_size, incoterm, brand)
                         VALUES (:name, :description, :currency_code, :period, to_date(:start_date,'YYYY-MM-DD'), to_date(:end_date,'YYYY-MM-DD'), 
                                 :product_uom_code, :product_attr_value, :category_id, :new_price, :customer_name, :customer_id, :created_by, 
                                 SYSDATE, :comments, :volume, :bill_to, :ship_to, :sku_size, :incoterm, :brand)";

            $sqlarr = oci_parse($dbcon , $sql);
            oci_bind_by_name($sqlarr, ':name', $name);
            oci_bind_by_name($sqlarr, ':description', $row->ITEM_DESCRIPTION);
            oci_bind_by_name($sqlarr, ':currency_code', $row->CURRENCY_CODE);
            oci_bind_by_name($sqlarr, ':period', $data->target_period->ID);
            oci_bind_by_name($sqlarr, ':start_date', $start_date);
            oci_bind_by_name($sqlarr, ':end_date', $end_date);
            oci_bind_by_name($sqlarr, ':product_uom_code', $row->PRODUCT_UOM_CODE);
            oci_bind_by_name($sqlarr, ':product_attr_value', $row->PRODUCT_ATTR_VALUE);
            oci_bind_by_name($sqlarr, ':category_id', $row->CATEGORY_ID);
            oci_bind_by_name($sqlarr, ':new_price', $row->NEW_PRICE);
            oci_bind_by_name($sqlarr, ':customer_name', $row->CUSTOMER_NAME);
            oci_bind_by_name($sqlarr, ':customer_id', $row->CUSTOMER_ID);
            oci_bind_by_name($sqlarr, ':created_by', $data->userid);
            oci_bind_by_name($sqlarr, ':comments', $row->COMMENTS);
            oci_bind_by_name($sqlarr, ':volume', $row->VOLUME);
            oci_bind_by_name($sqlarr, ':bill_to', $row->BILL_TO);
            oci_bind_by_name($sqlarr, ':ship_to', $row->SHIP_TO);
            oci_bind_by_name($sqlarr, ':sku_size', $row->SKU_SIZE);
            oci_bind_by_name($sqlarr, ':incoterm', $row->INCOTERM);
            oci_bind_by_name($sqlarr, ':brand', $row->BRAND);
            $r = oci_execute($sqlarr, OCI_NO_AUTO_COMMIT);

            if (!$r) {
                $e = oci_error($sqlarr);
                $errorMsg = json_encode($e['message']);
            }
            $errorMsg = "Success";

        } else {
            $errorMsg = "The price with this combination already exists.";
        }

        //Create new array
        $copy_res[] = [
            "NAME" => $name,
            "PERIOD" => $row->PERIOD,
            "CURRENCY_CODE" => $row->CURRENCY_CODE,
            "CATEGORY_ID" => $row->CATEGORY_ID,
            "PRODUCT_ATTR_VALUE" => $row->PRODUCT_ATTR_VALUE,
            "ITEM_DESCRIPTION" => $row->ITEM_DESCRIPTION,
            "SKU_SIZE" => $row->SKU_SIZE,
            "INCOTERM" => $row->INCOTERM,
            "BRAND" => $row->BRAND,
            "PRODUCT_UOM_CODE" => $row->PRODUCT_UOM_CODE,
            "CUSTOMER_ID" => $row->CUSTOMER_ID,
            "CUSTOMER_NAME" => $row->CUSTOMER_NAME,
            "VOLUME" => $row->VOLUME,
            "NEW_PRICE" => $row->NEW_PRICE,
            "BILL_TO" => $row->BILL_TO,
            "SHIP_TO" => $row->SHIP_TO,
            "COMMENTS" => $row->COMMENTS,
            "SYSTEM_MARKS" => $errorMsg
        ];
    }

    $result = [];
    oci_commit($dbcon);
    //oci_rollback($dbcon);
    $result[] = [
        "message" => "Success",
        "data" => $copy_res
    ];
    
    print json_encode($result);
?>
