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

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

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

    $dats = date('Y/m/d H:i:s'); $logger->info("dats>>",$dats); //afs73用

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

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

      $valutil = new getValueUtil();


      // ロック生成
      $handle = null;
      $result = lockRequestAP();
      if ($result['ret'] != 0) {
        $ret = $result["ret"];
        $msg = "登録に失敗しました。";

        $bat = $dbutil->nextVal("innerbat");
        $wsid = "AP".$ktktcd;
        $pid = "AF254X";
        $datt = date('Y/m/d H:i:s');
        $pnm = "入庫＆仕入入力登録";
        $inf1 = "入庫＆仕入入力登録　異常終了: ".date('Y/m/d H:i:s');
        $inf2 = "【".$ktktcd."】"."ロックエラー";
        $valutil->insertAfs73($bat,$wsid,$pid,$dats,$datt,$pnm,$inf1,$inf2);

        goto endReg;
      } else {
        $handle = $result['handle'];
        $logger->info("lock_handle",$handle);
      }


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

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


        //入荷予定日 or 発注日に変更あれば afr3111を更新するらしい
        if($stringUtil->nvl($header["dat"],'NULL') != $stringUtil->nvl($header["old_dat"],'NULL')
        || $stringUtil->toNumber($stringUtil->nvl($detail["pno"],'-999999999')) != $stringUtil->toNumber($stringUtil->nvl($detail["old_pno"],'-999999999'))
        ) {

          //afw53 Insert
          $whereParams = array();
          $whereParams["iseq3111"] = $detail["iseq3111"];
          $whereParams["jan"]      = $detail["jan"];
          $whereParams["ktktcd"]   = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
          $whereParams["spktcd1"]  = $header["spktcd1"];
          $whereParams["pno"]      = $detail["pno"];
          $whereParams["chgcd"]    = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"];
          $whereParams["dat"]      = $header["dat"];
          $whereParams["spktcd"]   = $detail["spktcd"];

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

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

      endReg:

      //ロック解除
      if (!$stringUtil->isEmpty($handle)) {
        lockReleaseAP($handle); //lock解除
      }

      $dbutil->commit();

    } catch(userException $ue) {
      $ret = '-999';
      $msg = $ue->getErrorMessage();

      //ロック解除
      if (!$stringUtil->isEmpty($handle)) {
        lockReleaseAP($handle); //lock解除
      }
    } catch(Exception $e) {
      $ret = '-999';
      $msg = $e->getMessage();

      //ロック解除
      if (!$stringUtil->isEmpty($handle)) {
        lockReleaseAP($handle); //lock解除
      }
    }

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

    return $ret_array;
  }

?>
