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

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

 /*
 insertAfw83
 */
 function insertAfw83($dbutil, $data){
   $logger = new logger(basename(__FILE__));
     global $stringUtil;
 $logger->info("insertAfw83>>",$data);
     $ret_array = array(
       "ret" => "0", //0:OK
       "msg" => "",
       "wseq" => "",
     );
     $ret = '0';

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

       $header = $data["header"][0];
       $footer = $data["footer"][0];
       //ヘッダー情報(検索条件)
       $ktktcd = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
       $sdday =  $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["SDDAY"];
       $wseq = $dbutil->nextVal("innerwseq");

       $sqlIns83 = file_get_contents("./sql/insert83.sql");
       $sqlDel83 = file_get_contents("./sql/delete83.sql");
       $sqlAf808s = file_get_contents("./sql/callAf808s.sql");
       $sqlIns85_0 = file_get_contents("./sql/insert85_0.sql"); //未発行
       $sqlIns85_1 = file_get_contents("./sql/insert85_1.sql"); //発行済み

       if ($header["rdo_cls"] == "0") { //未発行の場合
         foreach($data["data"] as $detail) {
           if($detail["chkval"] == "1" && !$stringUtil->isEmpty($detail["spktcd"])) {
             $whereParams = array();
             $whereParams["wseq"] = $wseq;
             $whereParams["ktktcd"] = $ktktcd;
             $whereParams["spktcd"] = $detail["spktcd"];
             $whereParams["dat"] = $sdday;
   $logger->info("ins83>>".$sqlIns83, $whereParams);
             $result = $dbutil->insert($sqlIns83, $whereParams);
             $cnt = $cnt + 1;
           }
         }
         if ($cnt > 0) {  //念のため
           $whereParams = array();
           $whereParams["ktktcd"]["in"] = $ktktcd;
           $whereParams["wseq"]["in"] = $wseq;
           $whereParams["ret"]["out"] = "";
 $logger->info("call808>>".$sqlAf808s, $whereParams);
           $result = $dbutil->call($sqlAf808s, $whereParams);
           $ret808 = $result["ret"];
           if (intval($ret808) > 0) {
             $obat = $result["ret"];
             $whereParams = array();
             $whereParams["wseq"] = $wseq;
             $whereParams["ktktcd"] = $ktktcd;
             $whereParams["obat"] = $obat;
   $logger->info("ins85_0>>".$sqlIns85_0, $whereParams);
             $result = $dbutil->insert($sqlIns85_0, $whereParams);
           } else {
             $ret = "-1";
             if ($ret808 == "-8") {
               $msg = "他の端末で実行中です。";
             } else {
               $msg = "エラーが発生しました。(".$ret808.")";
             }
           }
           $whereParams = array();
           $whereParams["wseq"] = $wseq;
 $logger->info("del83>>".$sqlDel83, $whereParams);
           $result = $dbutil->delete($sqlDel83, $whereParams);
         }
       } else { //発行済み
         foreach($data["data"] as $detail) {
           if($detail["chkval"] == "1" && !$stringUtil->isEmpty($detail["spktcd"])) {
             $whereParams = array();
             $whereParams["wseq"] = $wseq;
             $whereParams["ktktcd"] = $ktktcd;
             $whereParams["spktcd"] = $detail["spktcd"];
             $whereParams["vseq"] = $detail["vseq"];
   $logger->info("ins85_1>>".$sqlIns85_1, $whereParams);
             $result = $dbutil->insert($sqlIns85_1, $whereParams);
             $cnt = $cnt + 1;
           }
         }
       }

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

?>
