<?php
//共通
require_once '../../base/common.php';

/**
 * af9031x共通カスタム関数
 */

/*
  重複チェック
*/
function chkRegBefore($dbutil, $data){
$logger = new logger(basename(__FILE__));
  global $stringUtil;
$logger->info("chkRegBefore>>",$data);
  $ret_array = array(
    "ret" => "0", //0:OK, -1:マスタと重複
  );
  $ret = '0';

  $valutil = new getValueUtil();

  $header = $data["header"][0];
  $dataList = $data["data"];

  foreach($dataList as $detail) {
    $whereParams = array();
      
    $whereParams["cucd"]=$header["cucd"];
    $whereParams["cugdcd"]=$detail["cugdcd"];
    $whereParams["medcd"]=$detail["medcd"];
    $whereParams["cugdde"]=$detail["cugdde"];

    $sql = file_get_contents("./sql/chkAfm031.sql");
    
    $ret_cnt = $dbutil->select($sql, $whereParams);

    if($ret_cnt[0]["cnt"] != '0') {
      $ret = '-1';
      $dupliCugdcd=$detail["cugdcd"];
      $dupliMedcd=$detail["medcd"];
      $dupliCugdde=$detail["cugdde"];
      $orgCugdcd=$detail["orgCugdcd"];
      $orgMedcd=$detail["orgMedcd"];
      $orgCugdde=$detail["orgCugdde"];
      break;
    }
  }

  $ret_array["ret"] = $ret;
  $ret_array["dupliCugdcd"] = $dupliCugdcd;
  $ret_array["dupliMedcd"] = $dupliMedcd;
  $ret_array["dupliCugdde"] = $dupliCugdde;
  $ret_array["orgCugdcd"] = $orgCugdcd;
  $ret_array["orgMedcd"] = $orgMedcd;
  $ret_array["orgCugdde"] = $orgCugdde;

  return $ret_array;
}


function getPsum($dbutil, $data){
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
  $logger->info("getPsum>>",$data);
  $ret_array = array(
    "ret" => "0",
  );

  $valutil = new getValueUtil();
  global $stringUtil;

  try {

    $whereParams = array();
    $whereParams["jan"] = $data["jan"];
    $sql = file_get_contents("./sql/getIjan.sql");
    $logger->info($sql, $whereParams);
    $result = $dbutil->select($sql, $whereParams);
    $logger->info("getIjan_result",$result);
    if($result != null) { 
      $ret_array["ijan"] = $result[0]["ijan"];
$logger->info("getIjan_ret_array",$ret_array);
      $cnt = 0;
      $psum = 0;
      foreach($ret_array as $key => $val){
        $cnt++;
$logger->info("getIjan_val",$val);
        //CP在庫取得
        $whereParams = array();
        $whereParams["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
        $whereParams["spktcd"] = $data["spktcd"];
        $whereParams["ijan"] = $val;
        $whereParams["cls1"] = $data["cls1"];
        $sql = file_get_contents("./sql/getPsum.sql");
        $logger->info($sql, $whereParams);
        $retPsum = $dbutil->select($sql, $whereParams);
        $logger->info("retPsum",$retPsum);
        $ret_array["psum"] = $stringUtil->nvl0($retPsum[0]["psum"]);
      }

    } else {
      $ret_array["ret"] = '-1';
      $ret_array["msg"] = "入力されたJANは未登録です";
    }

  } catch(userException $ue) {
    $retData["code"]  = $ue->getErrorCode();
    $retData["msg"]   = $ue->getErrorMessage();
    $response["status"] = "NG";
    $ret_array["ret"] = '-9';
    $ret_array["msg"] = $ue->getErrorMessage();
  } catch(Exception $e) {
    $response["status"] = "NG";
    $response["data"]   = $e->getMessage();
    $ret_array["ret"] = '-9';
    $ret_array["msg"]   = $e->getMessage();
  }

  return $ret_array;
}

?>
