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

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

/*
afw82の削除
*/


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

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

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

      $sql_3111  = file_get_contents("./sql/updateAfr3111.sql"); //afr3111 update
      $sql_ins83 = file_get_contents("./sql/insertAfw83.sql");   //afw83 insert (チェック行)
      $sql_plsql = file_get_contents("./sql/callAf804s.sql");    //確定plsql
      $sql_del83 = file_get_contents("./sql/deleteAfw83.sql");   //afw83 delete (plsqlが終わったら消す)

      $wseq = $dbutil->nextVal("innerwseq");

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


        //入荷予定日 or 発注日に変更あれば afr3111を更新するらしい
        if(!$stringUtil->isEmpty($detail["mcd"])
        && ($stringUtil->nvl($detail["dat"],'9999/99/99') != $stringUtil->nvl($detail["ddat"],'9999/99/99')
            || $stringUtil->nvl($detail["dat1"],'9999/99/99') != $stringUtil->nvl($detail["ddat1"],'9999/99/99')
        )) {
          $whereParams = array();
          $whereParams["ddat"]   = $detail["ddat"];
          $whereParams["ddat1"]  = $detail["ddat1"];
          $whereParams["ktktcd"] = $ktktcd;
          $whereParams["mcd"]    = $detail["mcd"];
          $whereParams["vseq"]   = $detail["vseq"];
          $whereParams["dat"]    = $detail["dat"];
          $whereParams["dat1"]   = $detail["dat1"];
$logger->info("upd_afr3111>>".$sql_3111, $whereParams);
          $result = $dbutil->update($sql_3111, $whereParams);
          
        }

        if($detail["chkval"] == "1") {          //チェック行をafw83にセット
          //afw53 Insert
          $whereParams = array();
          $whereParams["wseq"]   = $wseq;
          $whereParams["ktktcd"] = $ktktcd;
          $whereParams["spktcd"] = $detail["spktcd"];
          $whereParams["mcd"]    = $detail["mcd"];
          $whereParams["vseq"]   = $detail["vseq"];

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

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

      //チェック行があればplsqlのコール -> ワークの削除
      if ($ins_cnt > 0) {
        $whereParams = array();
        $whereParams["ktktcd"]["in"] = $ktktcd;
        $whereParams["wseq"]["in"]   = $wseq;
        $whereParams["chgcd"]["in"]  = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"];
        $whereParams["wret"]["out"]  = "";
        $whereParams["wmsg"]["out"]  = "";
$logger->info("call_af804s>>".$sql_plsql, $whereParams);
        $ret_plsql = $dbutil->call($sql_plsql, $whereParams);
$logger->info("ret_af804s>>",$ret_plsql);

        $ret = $ret_plsql["wret"];
        if((int)$ret < 0 ) {
          $msg = "発注確定に失敗しました。(".$ret_plsql["wmsg"].")";
        }

        $whereParams = array();
        $whereParams["wseq"] = $wseq;
$logger->info("del_afw83>>".$sql_del83, $whereParams);
        $result = $dbutil->delete($sql_del83,$whereParams);

        $dbutil->commit();
      }

      //retは確定obatsが入る。ret>0はちゃんと確定できたってこと
      if((int)$ret>0) {
        //liv用(iReport用)に追加。発注データをaw851にinsertするplsqlのコール
        $pwseq = $dbutil->nextVal("innerwseq");
        $whereParams = array();
        $whereParams["wseq"]["in"]   = $pwseq;
        $whereParams["ktktcd"]["in"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
        $whereParams["chgcd"]["in"]  = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"];
        $whereParams["dat1"]["in"]   = "";
        $whereParams["mcd"]["in"]    = "";
        $whereParams["vseqs"]["in"]  = "";
        $whereParams["vseqe"]["in"]  = "";
        $whereParams["obats"]["in"]  = $ret; //確定plsqlのretにobatsが入っている
        $whereParams["ret"]["out"]   = "";
        $sql = file_get_contents("./sql/call_Fm_AF412X_Get.sql");
        $pret_plsql = $dbutil->call($sql,$whereParams);

        //liv用(iReport用)に追加。発注データをaw851にinsertするplsqlの結果を返す
        $ret_array["pret"]  = $pret_plsql["ret"];
        $ret_array["pwseq"] = $pwseq;
      }


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

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

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

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

      $sql = file_get_contents("./sql/deleteAfr3111.sql"); 

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

        if($detail["chkval"] == "1") {          //チェック削除

          //afw53 Insert
          $whereParams = array();
          $whereParams["spktcd"] = $detail["spktcd"];
          $whereParams["mcd"]    = $detail["mcd"];
          $whereParams["vseq"]   = $detail["vseq"];

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

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

      if ($del_cnt > 0) {
        $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;

    return $ret_array;
  }


?>
