<?php
if(!function_exists(ValAR)){
  function ValAR() {
    if(RetRstField("SELECT fnid FROM tlcustomer WHERE ftcustomer_code='{$_REQUEST['ftcustomer_code']}'")) {
      echo"<script>alert('Customer tidak valid...'); window.history.back();</script>";
      exit;
    }
  }
}

switch($_REQUEST[mode]) {
case "batal":
  $ssql="SELECT
            a.fnid
        FROM
            txsales_invoice_hdr a
            INNER JOIN txdelivery_order_hdr b ON a.ftdo_no=b.ftdo_no
        WHERE
            a.ftdo_no='{$_REQUEST['ftdo_no']}' AND
            a.fnstatus<>9";

    $rs=mysql_query($ssql);
     if(RetRstField($ssql)) {
           echo "<script>
                alert('No. SO sudah terlampir pada DO, DO harus dibatal kan terlebih dahulu');
                window.history.back();
            </script>";
            die("");
     }
  break;
}

switch($_REQUEST[mode]) {
    case "simpan":
        break;
    case "batal":
        $ssql="select ftsales_invoice_no,ftdo_no
            from txsales_invoice_hdr
            where fnstatus=1 and ftdo_no='{$_REQUEST['ftdo_no']}'";
        $rs=mysql_query($ssql);
        $r=mysql_fetch_assoc($rs);
        if(RetRstField($ssql)) {
                $id=RetRstField("select fnid from txdelivery_order_hdr  where ftdo_no ='{$_REQUEST['ftdo_no']}'");
                $url = $_SERVER["PHP_SELF"];
                $form = $_REQUEST["form"];
                echo "<script>
                    alert('No. Invoice : {$r['ftsales_invoice_no']} harus dibatalkan dulu ...');
                   window.location='{$url}?form={$form}&mode=edit&id=$id';
                    //window.history.back(-2);
                </script>";
                die("");
         }

        break;
    case "simpan_revisi":
        $ssql="select ftsales_invoice_no,ftdo_no
            from txsales_invoice_hdr
            where fnstatus=1 and ftdo_no='{$_REQUEST['ftdo_no']}'";
        $rs=mysql_query($ssql);
        $r=mysql_fetch_assoc($rs);
         if(RetRstField($ssql)) {
              $id=RetRstField("select fnid from txdelivery_order_hdr  where ftdo_no ='{$_REQUEST['ftdo_no']}'");
              $url = $_SERVER["PHP_SELF"];
              $form = $_REQUEST["form"];
               echo "<script>
                    alert('No. Invoice : {$r['ftsales_invoice_no']} harus revisi atau dibatalkan dulu ...');
                   window.location='{$url}?form={$form}&mode=edit&id=$id';
                    //window.history.back(-2);
                </script>";
                die("");
         }

        break;

    case "final":
        for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
            // cek qty available sebelum simpan
            $ssql = "SELECT IFNULL(SUM(a.fnqty_available),0) as fnqty_available FROM tlstock_det a
                        WHERE  a.ftwarehouse_code='{$_REQUEST['ftwarehouse_codeD0'][$j]}' AND a.ftlocation_code='{$_REQUEST['ftlocation_codeD0'][$j]}'
                        AND a.ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' ";

            if(CCur($_REQUEST['fnqty_baseD0'][$j]) > RetRstField($ssql)) {
                echo "<script>
                        alert('Qty utk {$_REQUEST['ftitem_codeD0'][$j]} tidak mencukupi. Kemungkinan ada transaksi yang baru menggunakan item ini. Harap dicek kembali. ');
                        window.location='{$url}?form={$form}&mode=edit&id={$_REQUEST['id']}';
                      </script>";
                die("");
            }

            // cek qty terima sebelum simpan (mencegah input transaksi bersamaan)
            $ssql2 = "SELECT IFNULL(SUM(a.fnqty_base),0) - IFNULL(SUM(a.fnqty_accept_base),0) as fnqty_selisih FROM txsales_order_det a
                        WHERE  a.ftso_no = '{$_REQUEST['ftso_no']}' AND  a.ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' ";

            if(CCur($_REQUEST['fnqty_baseD0'][$j]) > RetRstField($ssql2)) {
                echo "<script>
                        alert('Qty utk {$_REQUEST['ftitem_codeD0'][$j]} sudah melebihi qty SO. Kemungkinan ada transaksi yang baru menggunakan SO ini. Harap dicek kembali. ');
                        window.location='{$url}?form={$form}&mode=edit&id={$_REQUEST['id']}';
                      </script>";
                die("");
            }
        }
        break;
}

 function SaveData(){
    global $objAxpDB;
    $strSQL=array();
    $objAxpDB->Allow_Save(false);
    switch($_REQUEST[mode]) {
    case "simpan":
      $_REQUEST["fdtrans_date"]=getDateYmd($_REQUEST["fdtrans_date"]);
      $strSQL['Insert DO Hdr'][]="
              INSERT INTO txdelivery_order_hdr(
              ftbranch_code,
              ftdo_no, ftdo_type, ftso_no,
              ftcustomer_code, ftship_address, fdtrans_date,
              ftdelivery_method, ftreference, ftnotes,
              fftotal_qty, fnstatus,
              ftcreated_user, fdcreated_date
              ) SELECT
              '{$_SESSION['strbranch']}' AS ftbranch_code,
              '{$_REQUEST["ftdo_no"]}' AS ftdo_no, '{$_REQUEST["ftdo_type"]}' AS ftdo_type, '{$_REQUEST["ftso_no"]}' AS ftso_no,
              '{$_REQUEST["ftcustomer_code"]}' AS ftcustomer_code, '{$_REQUEST["ftship_address"]}' AS ftship_address, '{$_REQUEST["fdtrans_date"]}' AS fdtrans_date,
              '{$_REQUEST["ftdelivery_method"]}' AS ftdelivery_method, '{$_REQUEST["ftreference"]}' AS ftreference, '{$_REQUEST["ftnotes"]}' AS ftnotes,
              '".CCur($_REQUEST["fftotal_qty"])."' AS fftotal_qty, '".CCur($_REQUEST["fnstatus"])."' AS fnstatus,
              '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date";

      for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
          if((float)CCur($_REQUEST['fnqtyD0'][$j])>0){
               $strSQL['Insert DO Det'][]="
                       INSERT INTO txdelivery_order_det
                          (ftbranch_code, ftdo_no, fnindex, ftitem_code,
                          ftwarehouse_code, ftlocation_code,
                          fnqty_available, fnqty_so,
                          fnqty, ftuom_code,  fdtrans_date,
                          ftdescription, ftstock_category_code,
                          fnqty_base,ffformula,
                          ftcreated_user, fdcreated_date,
                          ftmodified_user, fdmodified_date
                          )
                        SELECT
                          '{$_SESSION['strbranch']}' AS ftbranch_code, '{$_REQUEST['ftdo_no']}' AS ftdo_no, '{$j}' AS fnindex, '{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code,
                          '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code,
                          '".CCur($_REQUEST['fnqty_availableD0'][$j])."' AS fnqty_available, '".CCur($_REQUEST['fnqty_soD0'][$j])."' AS fnqty_so,
                          '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty, '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code, '{$_REQUEST["fdtrans_date"]}' AS  fdtrans_date,
                          '{$_REQUEST['ftdescriptionD0'][$j]}' AS ftdescription, '{$_REQUEST['ftstock_category_codeD0'][$j]}' AS ftstock_category_code,
                          '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base, '".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                          '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date,
                          '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date";

              $rs=mysql_query("select ftitem_name,ftprint_name from tlstock_hdr where ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'");
              $r=mysql_fetch_assoc($rs);
              $strSQL["n"][]="update txdelivery_order_det set ftitem_name='".$r['ftitem_name']."',ftprint_name='".$r['ftprint_name']."' where  ftdo_no='".$_REQUEST['ftdo_no']."' and ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'";
          }
      }

      break;
    }

    return $strSQL;
 }


function SaveDataLast() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "simpan":
		if(!RetRstField("select fnid from tlcustomer where ftfiktif='1' and   ftcustomer_code='".$_REQUEST['ftcustomer_code']."'")) {
			for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
				$rs=mysql_query("select ftitem_name,ftprint_name from tlstock_hdr where ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'");
				$r=mysql_fetch_assoc($rs);
			   $strSQL["n"][]="update txdelivery_order_det set ftitem_name='".$r['ftitem_name']."',ftprint_name='".$r['ftprint_name']."' where  ftdo_no='".$_REQUEST['ftdo_no']."' and ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'";
			}
		}
		else {
			for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
				$rs=mysql_query("select ftitem_name,ftprint_name from tlstock_hdr where ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'");
				$r=mysql_fetch_assoc($rs);
				$strSQL["n"][]="update txdelivery_order_det set ftitem_name='".$r['ftitem_name']."',ftprint_name='".$_REQUEST['ftdescriptionD0'][$j]."' where  ftdo_no='".$_REQUEST['ftdo_no']."' and fnindex='".$_REQUEST['fnindexD0'][$j]."'";
			}
		}
        break;

    }

    return $strSQL;
 }

 function UpdateData(){
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "update":
      $strSQL['Delete DO Det'][]="DELETE FROM txdelivery_order_det WHERE ftdo_no='{$_REQUEST["ftdo_no"]}'";

      $dt=getDateYmd($_REQUEST['fdtrans_date']);
      for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
          if((float)CCur($_REQUEST['fnqtyD0'][$j])>0){
             $strSQL['Insert DO Det'][]="
                     INSERT INTO txdelivery_order_det
                        (ftbranch_code, ftdo_no, fnindex, ftitem_code,
                        ftwarehouse_code, ftlocation_code,
                        fnqty_available, fnqty_so,
                        fnqty, ftuom_code,  fdtrans_date,
                        ftdescription, ftstock_category_code,
                        fnqty_base,ffformula,
                        ftcreated_user, fdcreated_date,
                        ftmodified_user, fdmodified_date
                        )
                      SELECT
                        '{$_SESSION['strbranch']}' AS ftbranch_code, '{$_REQUEST['ftdo_no']}' AS ftdo_no, '{$j}' AS fnindex, '{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code,
                        '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code,
                        '".CCur($_REQUEST['fnqty_availableD0'][$j])."' AS fnqty_available, '".CCur($_REQUEST['fnqty_soD0'][$j])."' AS fnqty_so,
                        '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty, '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code, '{$dt}' AS  fdtrans_date,
                        '{$_REQUEST['ftdescriptionD0'][$j]}' AS ftdescription, '{$_REQUEST['ftstock_category_codeD0'][$j]}' AS ftstock_category_code,
                        '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base, '".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                        '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date,
                        '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date";

            $rs=mysql_query("select ftitem_name,ftprint_name from tlstock_hdr where ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'");
            $r=mysql_fetch_assoc($rs);
            $strSQL["n"][]="update txdelivery_order_det set ftitem_name='".$r['ftitem_name']."',ftprint_name='".$r['ftprint_name']."' where  ftdo_no='".$_REQUEST['ftdo_no']."' and ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'";
          }
      }

      break;
    }

    return $strSQL;
 }


 function FinalData() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "final":
      $strSQL['Delete DO Det'][]="DELETE FROM txdelivery_order_det WHERE ftdo_no='{$_REQUEST["ftdo_no"]}'";

      $ftstock_out_no=getAutoNumber($_SESSION['strbranch'],'inv005','txstock_out_hdr','ftstock_out_no',$_REQUEST['fdtrans_date']);
      $dt=getDateYmd($_REQUEST['fdtrans_date']);
        // if(!($_REQUEST['ftdo_type']==4) && !($_REQUEST['ftdo_type']==3)) {
            $strSQL['Insert Stock Out Hdr'][]="
                      INSERT INTO txstock_out_hdr
                          (ftbranch_code, ftstock_out_no, ftcategory_code,
                          ftstock_out_company, ftdocument_no, fdtrans_date,
                          fntotal_qty, ftnotes, fnstatus, ftcreated_user,
                          fdcreated_date, ftmodified_user, fdmodified_date, ftstock_type_code)
                      SELECT
                     '{$_SESSION['strbranch']}' AS ftbranch_code, '{$ftstock_out_no}' AS ftstock_out_no, '4' AS ftcategory_code,
                         '{$_REQUEST['ftcustomer_code']}' AS  ftstock_out_company, '{$_REQUEST['ftdo_no']}' AS ftdocument_no,'{$dt}' AS  fdtrans_date,
                          '".CCur($_REQUEST['fftotal_qty'])."' AS fntotal_qty,'{$_REQUEST['ftnotes']}' AS  ftnotes, '1' AS fnstatus,  '{$_SESSION['username']}' AS ftcreated_user,
                          now() AS fdcreated_date,  '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date, 'inv' as ftstock_type_code";


                    $m=0;
                    for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                       $m=1;
                       $strSQL['Insert DO Det'][]="
                               INSERT INTO txdelivery_order_det
                                  (ftbranch_code, ftdo_no, fnindex, ftitem_code,
                                  ftwarehouse_code, ftlocation_code,
                                  fnqty_available, fnqty_so,
                                  fnqty, ftuom_code, fdtrans_date,
                                  ftdescription, ftstock_category_code,
                                  fnqty_base,ffformula,
                                  ftcreated_user, fdcreated_date,
                                  ftmodified_user, fdmodified_date
                                  )
                                SELECT
                                  '{$_SESSION['strbranch']}' AS ftbranch_code, '{$_REQUEST['ftdo_no']}' AS ftdo_no, '{$j}' AS fnindex, '{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code,
                                  '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code,
                                  '".CCur($_REQUEST['fnqty_availableD0'][$j])."' AS fnqty_available, '".CCur($_REQUEST['fnqty_soD0'][$j])."' AS fnqty_so,
                                  '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty, '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code, '{$dt}' AS  fdtrans_date,
                                  '{$_REQUEST['ftdescriptionD0'][$j]}' AS ftdescription, '{$_REQUEST['ftstock_category_codeD0'][$j]}' AS ftstock_category_code,
                                  '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base, '".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                                  '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date,
                                  '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date";

                       $rs=mysql_query("select ftitem_name,ftprint_name from tlstock_hdr where ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'");
                       $r=mysql_fetch_assoc($rs);
                       $strSQL["n"][]="update txdelivery_order_det set ftitem_name='".$r['ftitem_name']."',ftprint_name='".$r['ftprint_name']."' where  ftdo_no='".$_REQUEST['ftdo_no']."' and ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'";

                       $strSQL['Insert Stock Out Det'][]="
                             INSERT INTO txstock_out_det
                                  (ftbranch_code, ftstock_out_no, fnindex, ftitem_code,
                                  ftwarehouse_code, ftlocation_code, fnqty_available,
                                  fnqty, ftuom_code, fnqty_base,ffformula,
                                  ftcreated_user,
                                  fdcreated_date, ftmodified_user
                                  )
                                  SELECT
                                  '{$_SESSION['strbranch']}' AS ftbranch_code,'{$ftstock_out_no}' AS ftstock_out_no, '{$j}' AS fnindex, '{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code,
                                  '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code, '".CCur($_REQUEST['fnqty_availableD0'][$j])."' AS fnqty_available,
                                  '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty,  '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code, '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base, '".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                                  '{$_SESSION['username']}' AS ftcreated_user,
                                  now() AS fdcreated_date,  '{$_SESSION['username']}' AS ftmodified_user";

                        $strSQL['Update txsales_order_det'][]="
                            UPDATE txsales_order_det
                                SET fnqty_accept_base=IFNULL(fnqty_accept_base,0)+ ".(double)CCur($_REQUEST['fnqty_baseD0'][$j])."
                                    ,fnqty_accept= IFNULL(fnqty_accept,0)+ ".(double)CCur($_REQUEST['fnqtyD0'][$j])."
                                WHERE ftso_no='{$_REQUEST['ftso_no']}' AND
                                    ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}'  ";

                        $m=SaveStock('-',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                        $strSQL=array_merge_recursive($strSQL,$m);

                }

                if($m==0) {
                    unset($strSQL['Insert Stock Out Det']);
                }

                $strSQL['Update DO Hdr'][]="UPDATE txdelivery_order_hdr SET fnstatus=1 WHERE ftdo_no='{$_REQUEST["ftdo_no"]}'";

                // }

        //$_REQUEST["fdtrans_date"]=CDate($_REQUEST["fdtrans_date"]);
        //pr("DO Process:".$_REQUEST["fdtrans_date"]);
        break;

    }

    return $strSQL;
 }

 function FinalDataLast(){
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
      case "final":
            $ssql="SELECT IFNULL(SUM(fnqty),0) - IFNULL(SUM(fnqty_accept),0) AS fnqty
                    FROM txsales_order_det WHERE ftso_no = '{$_REQUEST['ftso_no']}' GROUP BY ftso_no ";
            $fnqty_outstanding = RetRstField($ssql);

            if ($fnqty_outstanding <= 0) {
                $fnreceive_status = 3;
            } else {
                $fnreceive_status = 2;
            }

            $strSQL['Update txsales_order_hdr last'][] =  "UPDATE txsales_order_hdr set fnreceive_status = {$fnreceive_status} WHERE ftso_no='{$_REQUEST['ftso_no']}'  ";
   
        break;
    }

    return $strSQL;
 }


function RevisiData() {
    global $objAxpDB;
    //$objAxpDB->Allow_Revisi(false);
     $strSQL=array();
    switch($_REQUEST[mode]) {
    case "simpan_revisi":
        //-----------------------------
        // yang lama di balikin
        // ---------------------------
         if(!($_REQUEST['ftdo_type']==3)) {
            $rs=mysql_query("SELECT * FROM txdelivery_order_det WHERE ftdo_no='{$_REQUEST['ftdo_no']}'");
            while($r=mysql_fetch_assoc($rs)) {
                 if($r['ftstock_category_code']=="inv") {
                         $m=SaveStock('+',$r['ftitem_code'],'',$r['ftwarehouse_code'],$r['ftlocation_code'],'',CCur($r['fnqty']),CCur($r['fnqty']));
                         $strSQL=array_merge_recursive($strSQL,$m);
                  }
            }
         }

         //-----------------------------------------------------
         // batal -%
         $ssql1="SELECT IFNULL(MAX(ftdo_no),'')  AS M
                      FROM txdelivery_order_hdr
                      WHERE ftdo_no LIKE '{$_REQUEST['ftdo_no']}-%'";
            $no=RetRstField($ssql1);
            if($no!="") {
                $no1=explode("-",$no);
                $no=(int)$no1[1]+1;
                $str1="-{$no}";
            }
            else {
                $str1="-1";
            }

             $ftdo_no_new="{$_REQUEST['ftdo_no']}{$str1}";

             $strSQL["Revisi"][]="update txdelivery_order_hdr
                                set fdrevisi_date=now(),ftrevisi_user='{$_SESSION['username']}',
                                     fnstatus=3,ftdo_no='{$ftdo_no_new}'
                                where ftdo_no='{$_REQUEST['ftdo_no']}'";

             $strSQL["Revisi"][]="update txdelivery_order_det
                                set ftdo_no='{$ftdo_no_new}'
                                where ftdo_no='{$_REQUEST['ftdo_no']}'";
         //--------------------------------------------
          if(!($_REQUEST['ftdo_type']==3)) {
                      $strSQL['Batal txstock_out_hdr'][]="UPDATE txstock_out_hdr
                                                        SET fnstatus=9,ftdocument_no='{$ftdo_no_new}'
                                                        WHERE ftdocument_no='{$_REQUEST['ftdo_no']}'";

                      $strSQL['Batal txsales_invoice_hdr'][]="UPDATE txsales_invoice_hdr
                                                    SET ftdo_no='{$ftdo_no_new}'
                                                    WHERE ftdo_no='{$_REQUEST['ftdo_no']}'";


                    //############################
                    $ftstock_out_no=getAutoNumber($_SESSION['strbranch'],'TN008','txstock_out_hdr','ftstock_out_no',$_REQUEST['fdtrans_date']);
                    $dt=getDateYmd($_REQUEST['fdtrans_date']);
                    $strSQL['Insert Stock Out Hdr'][]="
                              INSERT INTO txstock_out_hdr
                                  (ftbranch_code, ftstock_out_no, ftcategory_code,
                                  ftstock_out_company, ftdocument_no, fdtrans_date,
                                  fntotal_qty, ftnotes, fnstatus, ftcreated_user,
                                  fdcreated_date, ftmodified_user)
                              SELECT
                             '{$_SESSION['strbranch']}' AS ftbranch_code, '{$ftstock_out_no}' AS ftstock_out_no, '3' AS ftcategory_code,
                                 '{$_REQUEST['ftcustomer_code']}' AS  ftstock_out_company, '{$_REQUEST['ftdo_no']}' AS ftdocument_no,'{$dt}' AS  fdtrans_date,
                                  '".CCur($_REQUEST['fftotal_qty'])."' AS fntotal_qty,'{$_REQUEST['ftnotes']}' AS  ftnotes, '1' AS fnstatus,  '{$_SESSION['username']}' AS ftcreated_user,
                                  now() AS fdcreated_date,  '{$_SESSION['username']}' AS ftmodified_user";

                    $m=0;
                    for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                        if($_REQUEST['ftstock_category_codeD0'][$j]=="inv") {
                             $m=1;
                             $strSQL['Insert Stock Out Det'][]="
                             INSERT INTO txstock_out_det
                                  (ftbranch_code, ftstock_out_no, fnindex, ftitem_code,
                                  ftwarehouse_code, ftlocation_code, fnqty_available,
                                  fnqty, ftuom_code, fnqty_base, ffformula,
                                  ftcreated_user,
                                  fdcreated_date, ftmodified_user
                                  )
                                  SELECT
                                  '{$_SESSION['strbranch']}' AS ftbranch_code, '{$ftstock_out_no}' AS ftstock_out_no, '{$j}' AS fnindex, '{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code,
                                  '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code, '".CCur($_REQUEST['fnqty_availableD0'][$j])."' AS fnqty_available,
                                  '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty,  '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code,  '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base, '".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                                  '{$_SESSION['username']}' AS ftcreated_user,
                                  now() AS fdcreated_date,  '{$_SESSION['username']}' AS ftmodified_user";

                              $m=SaveStock('-',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                              $strSQL=array_merge_recursive($strSQL,$m);
                        }
                    }

          }

            // Call Function Revisi General Ledger
            if(function_exists("RevisiGL")) {
                $m = RevisiGL($ftdo_no_new);
                if ($m != '') { $strSQL=array_merge_recursive($strSQL,$m); }
            }
        break;
    }



    return $strSQL;
 }

function RevisiDataLast() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "simpan_revisi":
		if(!RetRstField("select fnid from tlcustomer where ftfiktif='1' and  ftcustomer_code='".$_REQUEST['ftcustomer_code']."'")) {
			for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
				$rs=mysql_query("select ftitem_name,ftprint_name from tlstock_hdr where ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'");
				$r=mysql_fetch_assoc($rs);
			   $strSQL["n"][]="update txdelivery_order_det set ftitem_name='".$r['ftitem_name']."',ftprint_name='".$r['ftprint_name']."' where  ftdo_no='".$_REQUEST['ftdo_no']."' and ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'";
			}
		}
		else {
			for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
				$rs=mysql_query("select ftitem_name,ftprint_name from tlstock_hdr where ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'");
				$r=mysql_fetch_assoc($rs);
				$strSQL["n"][]="update txdelivery_order_det set ftitem_name='".$r['ftitem_name']."',ftprint_name='".$_REQUEST['ftdescriptionD0'][$j]."' where  ftdo_no='".$_REQUEST['ftdo_no']."' and fnindex='".$_REQUEST['fnindexD0'][$j]."'";
			}
		}
        break;

    }

    return $strSQL;
 }


 function CancelData() {
    global $objAxpDB;
    //$objAxpDB->Allow_Cancel(false);
     $strSQL=array();
    switch($_REQUEST[mode]) {
    case "batal":
        if ($_REQUEST["fnstatus"] != 0) {
            for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                $strSQL['Update txsales_order_det'][]="
                            UPDATE txsales_order_det
                                SET fnqty_accept_base=IFNULL(fnqty_accept_base,0)- ".(double)CCur($_REQUEST['fnqty_baseD0'][$j])."
                                    ,fnqty_accept= IFNULL(fnqty_accept,0)- ".(double)CCur($_REQUEST['fnqtyD0'][$j])."
                                WHERE ftso_no='{$_REQUEST['ftso_no']}' AND
                                    ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}'  ";

                $m=SaveStock('+',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                $strSQL=array_merge_recursive($strSQL,$m);
            }
        }

        $strSQL['Batal txstock_out_hdr'][]="UPDATE txstock_out_hdr
                                                SET fnstatus=9
                                                WHERE ftdocument_no='{$_REQUEST['ftdo_no']}'";

        break;
    }

     return $strSQL;
 }


function CancelDataLast() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "batal":

        $ssql="SELECT IFNULL(SUM(fnqty_accept),0) AS fnqty_accept
                FROM txsales_order_det WHERE ftso_no = '{$_REQUEST['ftso_no']}'
                GROUP BY ftso_no";
        $fnqty_accept = RetRstField($ssql);

        if ($fnqty_accept <= $_REQUEST['fftotal_qty']) {
            $fnreceive_status = 1;
        } else if ($fnqty_accept > $_REQUEST['fftotal_qty']){
            $fnreceive_status = 2;
        }

        $strSQL['Update txsales_order_hdr'][] = "UPDATE txsales_order_hdr set fnreceive_status = {$fnreceive_status} WHERE ftso_no='{$_REQUEST['ftso_no']}'  ";

        break;

    }

    return $strSQL;
 }

