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

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

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

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

      $header = $data["header"][0];
      
      //元のwseqのデータは消しておく
      if(!$stringUtil->isEmpty($header["wseq"])) {
        delAfw85($dbutil, $data);
      }

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

      $sql = file_get_contents("./sql/insert.sql");   //afw85 insert (チェック行)
      $wseq = $dbutil->nextVal("innerwseq");

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

        if($detail["chkval"] == "1") {          //チェック行をafw85にセット
          //afw53 Insert
          $whereParams = array();
          $whereParams["wseq"]   = $wseq;
          $whereParams["ktktcd"] = $ktktcd;
          $whereParams["cucd"]   = $detail["cucd"];
          $whereParams["bal"]    = $detail["bal"];
          $whereParams["sbat"]   = $detail["sbat"];

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

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

      if ($ins_cnt > 0 || !$stringUtil->isEmpty($header["wseq"])) {
        $dbutil->commit();
      }

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

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

    try {
      $cnt = 0;
      $del_cnt = 0;

      $header = $data["header"][0];
      $sql = file_get_contents("./sql/delete.sql"); 

      //afw53 Insert
      $whereParams = array();
      $whereParams["wseq"] = $header["wseq"];
      $result = $dbutil->delete($sql, $whereParams);

      //commitは外でする

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


?>
