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

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


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

try {
  global $dbutil;

  $detail = $data["data"][0];
  $logger->info("data", $detail );

  insertUpdateAfm05( $data["header"], $detail, $dbutil, $logger );

  $dbutil->commit();

} 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 {
  $logger->info("response", $response );
  print( json_encode($response) );
}

$logger->info("=== af2300x_updata_end ===");



function insertUpdateAfm05($header, $data, $dbutil, $logger) {
  global $stringUtil;

  $valueData["param1"] = $header["param1"];
  $valueData['ktktcd'] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $valueData['spktcd'] = $header['spktcd'];
  $valueData['ijan'] = $header['ijan'];

  if($header["param1"] == '32') { //良品⇒不良品　振替
    $valueData['pno'] = $stringUtil->toNumber($data['pno'])*-1;
  } else {                         //不良品⇒良品　振替
    $valueData['pno'] = $stringUtil->toNumber($data['pno']);
  }

  $valueData['com'] = $data['com'];
  $valueData['chgcd'] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"];
  
  $logger->info("valuData",$valueData);

  $sql = file_get_contents("./sql/insert.sql");
  $result = $dbutil->insert($sql, $valueData);
  $logger->info("did_insert");

  return $reuslt;
}

?>
