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

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

/*請求書種類*/
/*出力対象の請求書かのチェックに使う*/
/*とりあえずafm90.cls(P)*/
function getTyp($dbutil, $data){
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
$logger->info("getTyp>>",$data);
  
  $listutil = new getListUtil();
  $listAfm90 = $listutil->getListAfm90('P');
  $ret_array = array();

  foreach($listAfm90 as $key => $val) {
    $logger->info($val);
    array_push($ret_array,$val["cd"]);
  }

  return $ret_array;
}

/*締日コード → 締日*/
function getCdat($dbutil, $data){
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
$logger->info("getCdat>>",$data);
    $ret_array = array(
      "ret" => "0", //0:OK
      "wseq" => "", //afw43.wseq
    );
    $ret = '0';

    try {

      $sql = file_get_contents("./sql/getCdat.sql");
      $whereParams = array();
      $whereParams["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
      $whereParams["cudt"] = $data["cudt"];
      $logger->info($sql,$whereParams);
      $result = $dbutil->select($sql, $whereParams);
      $logger->info("retGetCdat",$result);
      $ret_array["cdat"] = $result[0]["cdat"];

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

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

    return $ret_array;
  }

/*
請求書発行
*/
function doSeikyu($dbutil, $data){
  $logger = new logger(basename(__FILE__));
    global $stringUtil;
$logger->info("insertAfw43>>",$data);
    $ret_array = array(
      "ret" => "0", //0:OK
      "wseq" => "", //afw43.wseq
    );
    $ret = '0';

    $header = $data["header"][0];

    try {
      $arrTyp =  getTyp($dbutil, $data);

      $cnt = 0;
      $cnt_01 = 0;
      $cnt_02 = 0;
      $cnt_OP = 0; //イシザワ用
      $wseq = $dbutil->nextVal("innerwseq");

      //チェック行をfaw43にセット
      $sql = file_get_contents("./sql/insertAfw43.sql");
      foreach($data["data"] as $detail) {
        //チェックがついている&&請求書種別が妥当
        if($detail["chkval"] == "1" && in_array($detail["typ"],$arrTyp)) {
          $cnt = $cnt + 1;

          //請求書JAVA発行有無などのチェックのため、件数カウント
          if($detail["typ"] == '01') {
            $cnt_01 = $cnt_01 + 1;
          } else if($detail["typ"] == '02') {
            $cnt_02 = $cnt_02 + 1;
          } else if($detail["typ"] == 'OP') {
            $cnt_OP = $cnt_OP + 1;
          }

          $whereParams = array();
          $whereParams["wseq"] = $wseq;
          $whereParams["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
          $whereParams["cucd"] = $detail["cucd"];
          $whereParams["cuscd"] = $detail["cuscd"];
          $whereParams["sbat"] = $detail["sbat"];
          $whereParams["typ"] = $detail["typ"];
          $whereParams["cpnum"] = $detail["cpnum"]; //部数
          $whereParams["cudt"] = $header["cudt"];
          $whereParams["cdat"] = $detail["cdat"];
          $whereParams["pdat"] = $detail["pdat"];
          $whereParams["pamt"] = $detail["pamt"];
          $whereParams["cuedi"] = $detail["cuedi"];

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

      //専用処理あれば
      if($cnt_OP > 0) {
        //専用処理
      }


      $dbutil->commit();


      //請求書作成用のplsqlのコール
      $whereParams = array();
      $whereParams["wseq"]["in"] = $wseq;
      $whereParams["ktktcd"]["in"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
      $whereParams["chgcd"]["in"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"];
      $whereParams["ret"]["out"] = "";
      $sql = file_get_contents("./sql/callFmAf461xGet.sql");
      $ret_plsql = $dbutil->call($sql, $whereParams);
      $logger->info("retplsql",$ret_plsql);
      $ret = $ret_plsql["ret"];


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

    $ret_array["ret"] = $ret;
    $ret_array["msg"] = $msg;
    $ret_array["wseq"] = $wseq;
    $ret_array["cnt_01"] = $cnt_01;
    $ret_array["cnt_02"] = $cnt_02;
    $ret_array["cnt_OP"] = $cnt_OP;

    return $ret_array;
  }


?>
