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

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

 /*
確定処理
 */
 function doKakutei($dbutil, $data){
   $logger = new logger(basename(__FILE__));
     global $stringUtil;
 $logger->info("doKakutei>>",$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");
       $sqlAf809s = file_get_contents("./sql/callAf809s.sql");
       $sqlDel83 = file_get_contents("./sql/delete83.sql");

       foreach($data["data"] as $detail) {
         if($detail["chkval"] == "1" && !$stringUtil->isEmpty($detail["vseq"])) {
           $whereParams = array();
           $whereParams["wseq"] = $wseq;
           $whereParams["ktktcd"] = $ktktcd;
           $whereParams["spktcd"] = $detail["spktcd"];
           $whereParams["vseq"] = $detail["vseq"];
           $whereParams["adat"] = $header["adat"];
 $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["adat"]["in"] = $header["adat"];
         $whereParams["ret"]["out"] = "";
$logger->info("call809>>".$sqlAf809s, $whereParams);
         $result = $dbutil->call($sqlAf809s, $whereParams);
         $ret809 = $result["ret"];
         if ($ret809 == "0") {
           $ret = "0";
         } else {
           $ret = "-1";
           if ($ret809 == "-8") {
             $msg = "他の端末で実行中です。";
           } else if ($ret809 == "-4") {
             $msg = "マイナス在庫になるものがあります。";
           } else {
             $msg = "エラーが発生しました。(".$ret809.")";
           }
         }
       }
       $whereParams = array();
       $whereParams["wseq"] = $wseq;
$logger->info("del83>>".$sqlDel83, $whereParams);
       $result = $dbutil->delete($sqlDel83, $whereParams);

       $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 chkRegBefore($dbutil, $data){
   $logger = new logger(basename(__FILE__));
     global $stringUtil;
   $logger->info("chkRegBeforeInfo>>",$data);
   $ret_array = array(
     "ret" => "0",
     "ret1" => "0",
     "msg" => "",
   );
   $ret = "0";
   $ret1 = "0";  //計上日

   $valutil = new getValueUtil();

   $header = $data["header"];
   $ktktcd = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
   try {
     $result = $valutil->chk_adat("3", $ktktcd,"", "", $header["adat"]);
     if ($result != "0") {
       $ret1 = "-1";
       $msg = "計上日が不正です。";
     }
 $logger->info("chk_adat:".$result);

     if ($ret1 != "0") { //エラー有
       $ret = "-1";
     }

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

   return $ret_array;
 }


?>
