<?php
//共通
require_once '../../base/common.php';
require_once './allways.php';
$logger = new logger(basename(__FILE__));
$logger->info("===af902x_search_start===");

$response["status"] = "NG";

try {
  global $dbutil;
  global $stringUtil;
  $valutil = new getValueUtil();
  $listutil = new getListUtil();

  $requestBody = file_get_contents('php://input');
  $data = json_decode($requestBody, true);

  $logger->info($data);
  $header = $data["header"][0];
  $ret_array = array();


  if($stringUtil->isEmpty($header["ijan"])) {
    $gInit_array = getDefaultData();
    array_push($ret_array, $gInit_array);
  } else {
    // afm02reqのセレクト
    $whereParams=array();
    $whereParams["ijan"] = $header["ijan"];
    $sql = file_get_contents("./sql/search.sql");
    $result = $dbutil->select($sql, $whereParams);
    $logger->info("result",$result);

    //あるはず
    if ($result == null) {
      $gInit_array = getDefaultData();
      array_push($ret_array, $gInit_array);
    } else {
      $ret_array = $result;
    }
  }


  //分類コンボボックス
  $ret_array["listGdcls2"] = $listutil->getlistGdcls2($ret_array[0]["gdcls1"]);
  $ret_array["listGdcls3"] = $listutil->getlistGdcls3($ret_array[0]["gdcls1"],$ret_array[0]["gdcls2"]);

  if(!$stringUtil->isEmpty($ret_array[0]["ijan"])) {

    //afm02eのselect
    $whereParams=array();
    $whereParams["ijan"] = $ret_array[0]["ijan"];
    $sql02e = file_get_contents("./sql/search02e.sql");
    $result02e = $dbutil->select($sql02e, $whereParams);
    if($result02e == null) {
      $ret_array["afm02e_cnt"] = 0;
      $ret_array["array_afm02e"] = array();
    } else {
      
      $cnt02e=0;
      $wmcd = "";
      foreach($result02e as $detail) {
        $cnt02e = $cnt02e + 1;
        if($detail["rkbn"] == 'ON') {
          //$ret_array[0]["mcd"] = $detail["mcd"];
          $wmcd = $detail["mcd"];
        }
      }
  
      if($stringUtil->isEmpty($ret_array[0]["mcd"]) && !$stringUtil->isEmpty($wmcd)) {
        $ret_array[0]["mcd"] = $wmcd;
      }

      $ret_array["afm02e_cnt"] = 1;
      $ret_array["array_afm02e"] = $result02e;
    }

    //afm02dのセレクト
    $whereParams=array();
    $whereParams["ijan"] = $ret_array[0]["ijan"];
    $sql02d = file_get_contents("./sql/search02d.sql");
    $result02d = $dbutil->select($sql02d, $whereParams);
    if($result02d == null) {
      $ret_array["afm02d_cnt"] = 0;
      $ret_array["array_afm02d"] = array();
    } else {
      
      $cnt02d=0;
      $wmcd = "";
      foreach($result02d as $detail) {
        $cnt02d = $cnt02d + 1;
        if($detail["rkbn"] == 'ON') {
          //$ret_array[0]["mcd"] = $detail["mcd"];
          $wmcd = $detail["mcd"];
        }
      }
  
      if($stringUtil->isEmpty($ret_array[0]["mcd"]) && !$stringUtil->isEmpty($wmcd)) {
        $ret_array[0]["mcd"] = $wmcd;
      }

      $ret_array["afm02d_cnt"] = 1;
      $ret_array["array_afm02d"] = $result02d;
    }

  } else {
    $ret_array["afm02e_cnt"] = 0;
    $ret_array["array_afm02e"] = array();
    $ret_array["afm02d_cnt"] = 0;
    $ret_array["array_afm02d"] = array();
  }


  //消費税表示用
  $dataToCreateTax = array();
  $dataToCreateTax["taxrtd"] = $ret_array[0]["taxrtd"];
  $dataToCreateTax["gdtax"] = $ret_array[0]["gdtax"];
  $dataToCreateTax["gdtaxn"] = $ret_array[0]["gdtaxn"];
  $dataToCreateTax["pmode"] = "0"; 
  $ret_tax = dspTaxCombo($dbutil, $dataToCreateTax);
  $response["listGdtax"]   = json_encode($ret_tax['listGdtax'],true);
  $response["listGdtaxn"]   = json_encode($ret_tax['listGdtaxn'],true);
  $logger->info("taxinfo",$ret_tax);

  $ret_array[0]["gdcls6"] = $ret_tax["ret_array"]["gdcls6"];
  $ret_array[0]["gdclsg"] = $ret_tax["ret_array"]["gdclsg"];
  $ret_array[0]["gdcls6nm"] = $ret_tax["ret_array"]["gdcls6nm"];
  $ret_array[0]["gdclsgnm"] = $ret_tax['ret_array']["gdclsgnm"];

  $response["status"] = "OK";
  $response["data"]   = json_encode($ret_array, true);

} catch(userException $ue) {
  $retData["code"]  = $ue->getErrorCode();
  $retData["msg"]   = $ue->getErrorMessage();

  $response["status"] = "NG";
  $response["data"]   = $retData;

  $dbutil->rollback();
} catch(Exception $e) {
  $response["status"] = "NG";
  $response["data"]   = $e->getMessage();

  $dbutil->rollback();

} finally {
  print(json_encode($response));
}


$logger->info("===af902x_search_end===", $response);

?>
