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

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

/*
callAf835s
*/
function callAf835s($dbutil, $data){
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
$logger->info("callAf835s>>",$data);
    $ret_array = array(
      "ret" => "0", //0:OK, -9:プリンタ登録エラー
      "msg" => "",
      "wseq" => "",
    );
    $ret = '0';

    try {
      $cnt = 0;
      $ins_cnt = 0;

      $header = $data["header"][0];
      //ヘッダー情報(検索条件)
      $ktktcd = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
      $chgcd = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"];

      $wseq = $dbutil->nextVal("innerwseq");


      $cnt = 0;
      $sql = file_get_contents("./sql/insertAfw43.sql");
      foreach($data["data"] as $detail) {
        //チェックがついている
        if($detail["chkval"] == "1") {
          $cnt = $cnt + 1;

          $whereParams = array();
          $whereParams["wseq"] = $wseq;
          $whereParams["chgcd"]  = $chgcd;
          $whereParams["ktktcd"] = $ktktcd;
          $whereParams["cucd"]   = $detail["cucd"];
          $whereParams["cuscd"]  = $detail["cuscd"];
          $whereParams["sbat"]   = $detail["sbat"];

$logger->info("b>>".$sql, $whereParams);
          $result = $dbutil->insert($sql, $whereParams);
        }
      }

      if($cnt > 0) {
        $dbutil->commit();

        $sql = file_get_contents("./sql/callAf835s.sql");
        $whereParams = array();
        $whereParams["ktktcd"]["in"] = $ktktcd;
        $whereParams["wseq"]["in"]  = $wseq;
        $whereParams["chgcd"]["in"] = $chgcd;
        $whereParams["cudt"]["in"]  = $header["cudt"];
        $whereParams["ret"]["out"]  = "";
        $result = $dbutil->call($sql,$whereParams);
  $logger->info("retAf835s",$result);

        $ret = $result["ret"];
        
        if($ret != '0') {
          if($ret == '-1') {
            $msg = "前処理が未処理のデータがあります。";
          } else if($ret == '-8') {
            $msg = "他の端末で実行中です。";
          } else {
            $msg = "エラーが発生しました。(".$ret.")";
          }
        }
      }



    } catch(userException $ue) {
      $ret = '-999';
      $msg = $ue->getErrorMessage();
    } catch(Exception $e) {
      $ret = '-999';
      $msg = $e->getMessage();
    }

    $ret_array["ret"] = $ret;
    $ret_array["msg"] = $msg;
    $ret_array["wseq"] = $wseq;

    return $ret_array;
  }


?>
