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

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

$gInit_array = array( //初期値

);

try {
  global $dbutil;

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

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

  // SQL実行
  $whereParams=array();
  $whereParams["spktcd"] = $header["spktcd"];
  $whereParams["ijan"] = $header["ijan"];

  if($header["cls"]=='0') { //良品
    $sql = file_get_contents("./sql/getRyohin.sql");
  } else {
    $sql = file_get_contents("./sql/getFuryohin.sql");
  }
  
  $logger->info($sql,$whereParams);
  $result = $dbutil->select($sql, $whereParams);
  $logger->info("result",$result);
  
  if ($result == null) {
    array_push($ret_array, $gInit_array);
    $ret_array[0]["orgpno"] = '0';
    $ret_array[0]["newpno"] = '0';
  } else {
    $ret_array = $result;
    $ret_array[0]["orgpno"] = $result[0]["psum"];
    $ret_array[0]["newpno"] = $result[0]["psum"];
  }

  $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("===af292x_search_end===", $response);

?>
