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

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

/*
insertAfw851
*/
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];
      $footer = $data["footer"][0];
      //ヘッダー情報(検索条件)
      $ktktcd = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];

      $sql = file_get_contents("./sql/insert851.sql");
      $sql_h = file_get_contents("./sql/insert851h.sql");
      $wseq = $dbutil->nextVal("innerwseq");

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

        //Afw851 Insert
        $whereParams = array();
        $whereParams["wseq"] = $wseq;
        $whereParams["vlin"] = $cnt;
        $whereParams["h_ijan"] = $header["ijan"];
        $whereParams["h_mcd"]  = $header["mcd"];
        $whereParams["h_chgcd"] = $header["chgcd"];
        $whereParams["h_adats"] = $header["adats"];
        $whereParams["h_adate"] = $header["adate"];
        $whereParams["adat"] = $detail["adat"];
        $whereParams["mcd"]  = $detail["mcd"];
        $whereParams["amt01"] = $stringUtil->toNumber($detail["amt01"]);
        $whereParams["amt02"] = $stringUtil->toNumber($detail["amt02"]);
        $whereParams["amt03"] = $stringUtil->toNumber($detail["amt03"]);
        $whereParams["amt"] = $stringUtil->toNumber($detail["amt"]);
        $whereParams["rt02"] = $stringUtil->toNumber($detail["rt02"]);
        $whereParams["rt03"] = $stringUtil->toNumber($detail["rt03"]);
        $whereParams["h_spktcd"] = $header["spktcd"];
$logger->info("ins851>>".$sql, $whereParams);
        $result = $dbutil->insert($sql, $whereParams);

        if ($cnt == 1) {  //1行目だけ
          $whereParams = array();
          $whereParams["wseq"] = $wseq;
          $whereParams["vlin"] = "99999";
          $whereParams["h_ijan"] = $header["ijan"];
          $whereParams["h_mcd"]  = $header["mcd"];
          $whereParams["h_chgcd"] = $header["chgcd"];
          $whereParams["h_adats"] = $header["adats"];
          $whereParams["h_adate"] = $header["adate"];
          $whereParams["tamt01"] = $stringUtil->toNumber($footer["tamt01"]);
          $whereParams["tamt02"] = $stringUtil->toNumber($footer["tamt02"]);
          $whereParams["tamt03"] = $stringUtil->toNumber($footer["tamt03"]);
          $whereParams["tamt"] = $stringUtil->toNumber($footer["tamt"]);
          $whereParams["trt02"] = $stringUtil->toNumber($footer["trt02"]);
          $whereParams["trt03"] = $stringUtil->toNumber($footer["trt03"]);
          $whereParams["h_spktcd"] = $header["spktcd"];
$logger->info("ins851h>>".$sql, $whereParams);
          $result = $dbutil->insert($sql_h, $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;
    $ret_array["wseq"] = $wseq;

    return $ret_array;
  }


?>
