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

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

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

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

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

      $sql105 = file_get_contents("./sql/getAfm105.sql"); //プリンタ情報取得
      $sql_grp = file_get_contents("./sql/insert53_grp.sql"); //伝票毎でない
      $sql_nogrp = file_get_contents("./sql/insert53_nogrp.sql"); //伝票毎

      foreach($data["data"] as $detail) {
        $cnt = $cnt + 1;

        if($detail["chkval"] == "1") {          //チェック行セット
          //プリンタ情報取得
          $whereParams = array();
          $whereParams["ktktcd"] = $ktktcd;
          $whereParams["prno"] = $detail["prno"];
          $result = $dbutil->selectNocount($sql105, $whereParams);
          if ($result == null) {  //データなし:ありえない
            $ret = '-9';
            $msg = 'プリンタ情報の登録がありません。';
          } else if (count($result) > 1) {  //複数あり
            $ret = '-9';
            $msg = 'プリンタ情報の登録が不正です。';
          } else {
            $parea = $result[0]["parea"];
            $term = $result[0]["term"];
          }
          if ($ret == "-9") {
            break;
          }

          //afw53 Insert
          $whereParams = array();
          $whereParams["prno"] = $detail["prno"];
          $whereParams["parea"] = $parea;
          $whereParams["h_pkbn1"] = $header["pkbn1"];
          $whereParams["h_pkbn2"] = $header["pkbn2"];
          $whereParams["term"] = $term;
          $whereParams["cudenp"] = $detail["cudenp"];
          $whereParams["ktktcd"] = $ktktcd;
          $whereParams["ipadr"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["IPADDRESS"];

          if ($header["chk_grpkbn"] == "1") { //伝票毎
            $sql = $sql_nogrp;
            $whereParams["viseq"] = $detail["viseq"];
          } else {  //Not 伝票毎
            $sql = $sql_grp;
            $whereParams["tcls"] = $detail["tcls"];
            $whereParams["psts"] = $detail["psts"];
            $whereParams["cucd"] = $detail["cucd"];
            $whereParams["cuscd"] = $detail["cuscd"];
            $whereParams["h_chgcd"] = $header["chgcd"];
            $whereParams["h_sadat"] = $header["sadat"];
            $whereParams["h_eadat"] = $header["eadat"];
            $whereParams["hvseq"] = $detail["hvseq"];
          }
$logger->info("b>>".$sql, $whereParams);
          $result = $dbutil->insert($sql, $whereParams);

          $ins_cnt = $ins_cnt + 1;  //Insert カウント
        }
      }

      if ($ins_cnt > 0) {
        if ($ret == "0") {
          $dbutil->commit();
        } else {
          $dbutil->rollback();
        }
      }


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

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

    return $ret_array;
  }

  /*
  afw851 Insert
  */
  function insertAfw851($dbutil, $data){
    $logger = new logger(basename(__FILE__));
      global $stringUtil;
  $logger->info("insertAfw851>>",$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"];
        $wseq = $dbutil->nextVal("innerWseq");  //afw851.wseq

        $sql_grp = file_get_contents("./sql/insert851_grp.sql"); //伝票毎でない
        $sql_nogrp = file_get_contents("./sql/insert851_nogrp.sql"); //伝票毎
        $sql_upd = file_get_contents("./sql/updateAur32Psts.sql"); //ステータス更新

        foreach($data["data"] as $detail) {
          $cnt = $cnt + 1;

          if($detail["chkval"] == "1") {          //チェック行セット
            //afw851 Insert
            $whereParams = array();
            $whereParams["wseq"] = $wseq;
            $whereParams["ktktcd"] = $ktktcd;

            if ($header["chk_grpkbn"] == "1") { //伝票毎
              $sql = $sql_nogrp;
              $whereParams["cucd"] = $detail["cucd"];
              $whereParams["adat"] = $detail["adat"];
              $whereParams["vseq"] = $detail["vseq"];
              $whereParams["viseq"] = $detail["viseq"];
            } else {  //Not 伝票毎
              $sql = $sql_grp;
              $whereParams["tcls"] = $detail["tcls"];
              $whereParams["psts"] = $detail["psts"];
              $whereParams["cucd"] = $detail["cucd"];
              $whereParams["cuscd"] = $detail["cuscd"];
              $whereParams["h_chgcd"] = $header["chgcd"];
              $whereParams["h_sadat"] = $header["sadat"];
              $whereParams["h_eadat"] = $header["eadat"];
              $whereParams["hvseq"] = $detail["hvseq"];
            }
  $logger->info("b>>".$sql, $whereParams);
            $result = $dbutil->insert($sql, $whereParams);

            $ins_cnt = $ins_cnt + 1;  //Insert カウント
          }
        }

        if ($ins_cnt > 0) {
          //aur32.psts更新
          $whereParams = array();
          $whereParams["wseq"] = $wseq;
          $result = $dbutil->update($sql_upd, $whereParams);

          if ($ret == "0") {
            $dbutil->commit();
          } else {
            $dbutil->rollback();
          }
        }


      } 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;
    }


?>
