<?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.ftar_no,b.ftsales_invoice_no
                from txar_hdr a,
                        txar_det b
                where a.ftar_no=b.ftar_no and
                         a.fnstatus<>9 and a.fnstatus<>3
                  and b.ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}'";

        $rs=mysql_query($ssql);
        $str="No. Transaksi Penerimaan piutang ini harus dibatal kan dahulu :";
        while($r=mysql_fetch_assoc($rs)) {
            $str.=$r['ftar_no'];
        }
    //   die($str);
         if(RetRstField($ssql)) {
               echo "<script>
                    alert('$str');
                    window.history.back();
                </script>";
                die("");
         }

         $ssql="SELECT fnid FROM txsales_return_hdr where fnstatus<>9 AND ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}'";
         $rs=mysql_query($ssql);
         $str="No. Transaksi ini sudah digunakan untuk retur penjualan.";
         if(RetRstField($ssql)) {
               echo "<script>
                    alert('{$str}');
                    window.history.back();
                </script>";
                die("");
         }



          break;
    case "revisi":
         $ssql="SELECT ftsales_invoice_no, ftsales_return_no FROM txsales_return_hdr where fnstatus<>9";
         $rs=mysql_query($ssql);
          $str="No. Transaksi ini sudah digunakan untuk retur penjualan : ";
         while( $r=mysql_fetch_assoc($rs))
            {
             $str.=$r['ftsales_return_no'];
                if($r['ftsales_invoice_no']==$_REQUEST['ftsales_invoice_no'])
                {
                    echo "<script>
                    alert('$str');
                    window.history.back();
                </script>";

                die("");
                }
            }

        $ssql="select a.ftar_no,b.ftsales_invoice_no
                from txar_hdr a,
                        txar_det b
                where a.ftar_no=b.ftar_no and
                         a.fnstatus<>9 and a.fnstatus2<>1
                  and b.ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}'";

        $rs=mysql_query($ssql);
        $str="No. Transaksi Penerimaan piutang ini harus dibatal kan dahulu :";
        while($r=mysql_fetch_assoc($rs)) {
            $str.=$r['ftar_no'];
        }
    //   die($str);
         if(RetRstField($ssql)) {
               echo "<script>
                    alert('$str');
                    window.history.back();
                </script>";
                die("");
         }

          break;

    case "simpan":
        $ssql = "SELECT ftdo_no FROM txsales_invoice_hdr a
                    WHERE a.ftdo_no='{$_REQUEST['ftdo_no']}' AND a.fnstatus <> 3 and a.fnstatus <> 9 ";

        if(RetRstField($ssql)) {
            echo "<script>
                    alert('Invoice utk No DO : {$_REQUEST['ftdo_no']} sudah terbuat. 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(true);
    switch($_REQUEST[mode]) {

    case "simpan":


    // nomor otomatis Do
    $ftdo_code = RetRstField("SELECT ftcode FROM tstransaction_number WHERE ftmodule_code = 'TN005' and ftbranch_code = '{$_SESSION['strbranch']}'");
    $ftdo_no=getAutoNumber($_SESSION['strbranch'],'TN005','txdelivery_order_hdr','ftdo_no',$_REQUEST['fdtrans_date']);

    // nama customer
    $ssql = mysql_query("SELECT ftcustomer_name FROM tlcustomer WHERE ftcustomer_code='{$_REQUEST["ftcustomer_code"]}'");
    $r = mysql_fetch_assoc($ssql);

    // tanggal
    $fdtrans_date=getDateYmd($_REQUEST["fdtrans_date"]);

    // menghitung total di grid
    for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {

      $ffquantity = $ffquantity + $_REQUEST['fnqtyD0'][$j];

    }


    //nomor stock out
    $ftstock_out_no=getAutoNumber($_SESSION['strbranch'],'inv005','txstock_out_hdr','ftstock_out_no',$_REQUEST['fdtrans_date']);


    // $strSQL["Revisi"][]="UPDATE txsales_invoice_hdr
    // SET ftdo_no = '{$ftdo_no}',
    //     fntax_code='{$_REQUEST['fntax_code']}'
    // WHERE ftsales_invoice_no='{$_REQUEST["ftsales_invoice_no"]}'";


    // insert DELIVERY ORDER HEADER
    $strSQL['Insert DO Hdr'][]="
        INSERT INTO txdelivery_order_hdr(
            ftbranch_code,
            ftdo_no, ftdo_type, ftso_no,
            ftcustomer_code, ftcustomer_name,ftship_address, fdtrans_date,
            ftdelivery_method, ftreference, ftnotes,
            fftotal_qty, fnstatus,ftsales_type_code, ftsales_code,
            ftcreated_user, fdcreated_date )
        SELECT
            '{$_SESSION['strbranch']}' AS ftbranch_code,
            '{$ftdo_no}' AS ftdo_no,
             '0' AS ftdo_type,
             '{$_REQUEST["ftsales_invoice_no"]}' AS ftso_no,
            '".js_esc($_REQUEST["ftcustomer_code"])."' AS ftcustomer_code,
            '".js_esc($r['ftcustomer_name'])."' AS ftcustomer_name,
            '".js_esc($_REQUEST["ftship_address"])."' AS ftship_address,
             '{$fdtrans_date}' AS fdtrans_date,
            '{$_REQUEST["ftdelivery_method"]}' AS ftdelivery_method,
             '".js_esc($_REQUEST["ftreference"])."' AS ftreference,
              '".js_esc($_REQUEST["ftnotes"])."' AS ftnotes,
            '".CCur($ffquantity)."' AS fftotal_qty,
            '".CCur($_REQUEST["fnstatus"])."' AS fnstatus,
            '".CCur($_REQUEST["ftsales_type_code"])."' AS ftsales_type_code,
            '{$_REQUEST['ftsales_code']}',
            '{$_SESSION['username']}' AS ftcreated_user,
             now() AS fdcreated_date";

             // insert STOCK OUT HEADER
             $strSQL['Insert Stock Out Hdr'][]="
                     INSERT INTO txstock_out_hdr
                         (ftbranch_code, ftstock_out_no,
                          ftcategory_code,
                          ftreff_no,
                          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,
                         '".js_esc($_REQUEST['ftreference'])."',
                         '{$_REQUEST['ftcustomer_code']}' AS  ftstock_out_company, '{$ftdo_no}' AS ftdocument_no,'{$_REQUEST['fdtrans_date']}' AS  fdtrans_date,
                         '".CCur($ffquantity)."' AS fntotal_qty,
                         '".js_esc($_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";

                //FORLOOP GRID
                for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {

                  //MENGAMBIL NAMA BARANG
                  $rs=mysql_query("select ftitem_name from tlstock_hdr where ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'");
                  $r=mysql_fetch_assoc($rs);

                    $ftprint_name = $_REQUEST['ftprint_nameD0'][$j];
                    // echo $_REQUEST['ftitem_nameD0'][$j];
                    // die;
                  //INSERT DO DETAIL
                  $strSQL['Insert DO Det'][]="
                          INSERT INTO txdelivery_order_det
                              (ftbranch_code, ftdo_no, fnindex,
                              ftitem_code, ftitem_name, ftprint_name,
                              ftwarehouse_code, ftlocation_code,
                              fnqty_available, fnqty_so,
                              fnqty, ftuom_code, fdtrans_date,
                              ftdescription,
                              fnqty_base,ffformula,
                              ftcreated_user, fdcreated_date,
                              ftmodified_user, fdmodified_date)
                          SELECT
                              '{$_SESSION['strbranch']}' AS ftbranch_code,
                              '{$ftdo_no}' AS ftdo_no,
                              '{$j}' AS fnindex,
                              '".js_esc($_REQUEST['ftitem_codeD0'][$j])."' AS ftitem_code,
                              '".js_esc($r['ftitem_name'])."' AS ftitem_name,
                              '".js_esc($ftprint_name)."' AS ftprint_name,
                              '{$_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_so,
                              '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty,
                               '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code,
                                '{$fdtrans_date}' AS  fdtrans_date,
                              '".js_esc($_REQUEST['ftdescriptionD0'][$j])."' AS ftdescription,
                              '".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";

                    $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, fdmodified_date,
                      ftprint_name,ftdescription)
                      SELECT
                      '{$_SESSION['strbranch']}' AS ftbranch_code,
                      '{$ftstock_out_no}' AS ftstock_out_no,
                      '{$j}' AS fnindex, '".js_esc($_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,
                      now() AS fdmodified_date,
                      '".js_esc($ftprint_name)."',
                      '".js_esc($_REQUEST['ftdescriptionD0'][$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);

                }
                //
    
       break;

    }
    // echo "<pre>";
    // print_r($strSQL);
    // echo "</pre>";
    // die;
    #pr($strSQL);die("mm");

    return $strSQL;
 }


 function SaveDataLast() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "simpan":
        $strSQL["Update Customer Name"][]="UPDATE txsales_invoice_hdr a, tlcustomer b SET a.ftcustomer_name=b.ftcustomer_name WHERE a.ftcustomer_code = b.ftcustomer_code AND  a.ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}'";

    break;

    }

    return $strSQL;
 }


 function CancelData() {

    global $objAxpDB;
   // $objAxpDB->Allow_Cancel(false);
     $strSQL=array();

    switch($_REQUEST[mode]) {

        case "batal":

            $strSQL["Update Status Posted DO"][]="UPDATE txdelivery_order_hdr SET fnstatus=9,  ftmodified_user = '{$_SESSION['username']}', fdmodified_date = now() WHERE ftso_no='{$_REQUEST['ftsales_invoice_no']}'";

              for($j=1; $j<count($_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);

              }

              $ftdo_no = RetRstField("SELECT ftdo_no FROM txdelivery_order_hdr WHERE ftso_no = '{$_REQUEST['ftsales_invoice_no']}' and ftbranch_code = '{$_SESSION['strbranch']}'");

            if ($ftdo_no != "") {

              $strSQL['Batal txstock_out_hdr'][]="UPDATE txstock_out_hdr SET fnstatus=9, ftmodified_user = '{$_SESSION['username']}', fdmodified_date = now() WHERE ftdocument_no='{$ftdo_no}' ";

            }

        break;
    }

    // echo "<pre>";
    // print_r($strSQL);
    // echo "</pre>";
    //  die;
     return $strSQL;
 }

function RevisiData() {

    global $objAxpDB;

    $objAxpDB->Allow_Revisi(true);

    $strSQL=array();

    switch($_REQUEST[mode]) {

    case "simpan_revisi":

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

         //-----------------------------------------------------
         // batal -%
         // echo $_REQUEST['ftcustomer_name'];
         // die;
         //SALES INVOICE
         $ssql1="SELECT IFNULL(MAX(ftsales_invoice_no),'')  AS M
                      FROM txsales_invoice_hdr
                      WHERE ftsales_invoice_no LIKE '{$_REQUEST['ftsales_invoice_no']}-%'";

         $no=RetRstField($ssql1);
            if($no!="") {
                $no1=explode("-",$no);
                $no=(int)$no1[1]+1;
                $str1="-{$no}";
            }
            else {
                $str1="-1";
            }

             $ftsales_invoice_no_new="{$_REQUEST['ftsales_invoice_no']}{$str1}";

             $strSQL["Revisi"][]="update txsales_invoice_hdr
                                     set ftsales_invoice_no='{$ftsales_invoice_no_new}',fnstatus=3,ftrevisi_user='{$_SESSION['username']}'
                                     where ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}'";

             $strSQL["Revisi"][]="update txsales_invoice_det
                                set ftsales_invoice_no='{$ftsales_invoice_no_new}'
                                where ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}'";

            // Auto Revisi DO, Get No Revisi
            $ftdo_no=RetRstField("SELECT ftdo_no FROM txdelivery_order_hdr WHERE ftso_no = '{$_REQUEST['ftsales_invoice_no']}' ");

              $ssql1="SELECT IFNULL(MAX(ftdo_no),'')  AS M
                      FROM txdelivery_order_hdr
                      WHERE ftdo_no LIKE '{$ftdo_no}-%'";

              $no=RetRstField($ssql1);

                  if($no!=""){

                      $no1=explode("-",$no);
                      $no=(int)$no1[1]+1;
                      $str1="-{$no}";

                   }else{

                      $str1="-1";

                   }

                   $ftdo_no_new="{$ftdo_no}{$str1}";

                  //Pengembalian Stock transaksi lama
                  $rs=mysql_query("SELECT * FROM txdelivery_order_det WHERE ftdo_no='{$ftdo_no}'");

                  while($r=mysql_fetch_assoc($rs)) {

                  $m=SaveStock('+',$r['ftitem_code'],'',$r['ftwarehouse_code'],$r['ftlocation_code'],'',$r['fnqty_base'],$r['fnqty_base']);
                  $strSQL=array_merge_recursive($strSQL,$m);

                  }

                  $strSQL["Revisi"][]="UPDATE txdelivery_order_hdr
                                       SET fnstatus=3,
                                       ftso_no = '{$ftsales_invoice_no_new}',
                                       ftdo_no = '{$ftdo_no_new}',
                                       ftmodified_user='{$_SESSION['username']}',
                                       fdmodified_date=now()
                                       WHERE ftdo_no='{$ftdo_no}'";

                  $strSQL["Revisi"][]="UPDATE txdelivery_order_det
                                       SET ftdo_no = '{$ftdo_no_new}',
                                       ftmodified_user='{$_SESSION['username']}',
                                       fdmodified_date=now()
                                       WHERE ftdo_no='{$ftdo_no}'";

                //STOCK OUT!
                // Auto Revisi Stock Out, Get No Revisi
                $ftstock_out_no=RetRstField("SELECT ftstock_out_no FROM txstock_out_hdr WHERE ftdocument_no = '{$ftdo_no}'");

                if ($ftdo_no != "") {

                    $ssql1="SELECT IFNULL(MAX(ftstock_out_no),'')  AS M
                    FROM txstock_out_hdr
                    WHERE ftstock_out_no LIKE '{$ftstock_out_no}-%'";

                    $no=RetRstField($ssql1);

                    if($no!=""){

                     $no1=explode("-",$no);
                     $no=(int)$no1[1]+1;
                     $str1="-{$no}";

                    }else{

                     $str1="-1";

                    }
                    $ftstock_out_no_new="{$ftstock_out_no}{$str1}";

                     $strSQL["Revisi"][]="UPDATE txstock_out_hdr
                     SET fnstatus=3,ftstock_out_no='{$ftstock_out_no_new}',ftdocument_no = '{$ftdo_no_new}',
                         ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                     WHERE ftstock_out_no='{$ftstock_out_no}'";

                     $strSQL["Revisi"][]="UPDATE txstock_out_det
                     SET ftstock_out_no = '{$ftstock_out_no_new}',
                         ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                     WHERE ftstock_out_no='{$ftstock_out_no}'";

                    }

            // MEMBUAT DATA BARU !!!
            // nama customer
            $ssql = mysql_query("SELECT ftcustomer_name FROM tlcustomer WHERE ftcustomer_code='{$_REQUEST["ftcustomer_code"]}'");
            $r = mysql_fetch_assoc($ssql);

            // tanggal
            $fdtrans_date=getDateYmd($_REQUEST["fdtrans_date"]);

            //MENGHITUNG TOTAL QTY DI GRID
            for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {

              $ffquantity = $ffquantity + $_REQUEST['fnqtyD0'][$j];

            }


            // insert DELIVERY ORDER HEADER
            $strSQL['Insert DO Hdr'][]="
                INSERT INTO txdelivery_order_hdr(
                    ftbranch_code,
                    ftdo_no, ftdo_type, ftso_no,
                    ftcustomer_code, ftcustomer_name,ftship_address, fdtrans_date,
                    ftdelivery_method, ftreference, ftnotes,
                    fftotal_qty, fnstatus,ftsales_type_code, ftsales_code,
                    ftcreated_user, fdcreated_date )
                SELECT
                    '{$_SESSION['strbranch']}' AS ftbranch_code,
                    '{$ftdo_no}' AS ftdo_no,
                     '0' AS ftdo_type,
                     '{$_REQUEST["ftsales_invoice_no"]}' AS ftso_no,
                    '".js_esc($_REQUEST["ftcustomer_code"])."' AS ftcustomer_code,
                    '".js_esc($r['ftcustomer_name'])."' AS ftcustomer_name,
                    '".js_esc($_REQUEST["ftship_address"])."' AS ftship_address,
                     '{$fdtrans_date}' AS fdtrans_date,
                    '{$_REQUEST["ftdelivery_method"]}' AS ftdelivery_method,
                     '".js_esc($_REQUEST["ftreff_no"])."' AS ftreference,
                      '".js_esc($_REQUEST["ftnotes"])."' AS ftnotes,
                    '".CCur($ffquantity)."' AS fftotal_qty,
                    '".CCur($_REQUEST["fnstatus"])."' AS fnstatus,
                    '".CCur($_REQUEST["ftsales_type_code"])."' AS ftsales_type_code,
                    '{$_REQUEST['ftsales_code']}',
                    '{$_SESSION['username']}' AS ftcreated_user,
                     now() AS fdcreated_date";

             //insert STOCK OUT!
             $strSQL['Insert Stock Out Hdr'][]="
                     INSERT INTO txstock_out_hdr
                         (ftbranch_code, ftstock_out_no,
                          ftcategory_code,
                          ftreff_no,
                          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,
                         '".js_esc($_REQUEST['ftreference'])."',
                         '{$_REQUEST['ftcustomer_code']}' AS  ftstock_out_company, '{$ftdo_no}' AS ftdocument_no,'{$_REQUEST['fdtrans_date']}' AS  fdtrans_date,
                         '".CCur($ffquantity)."' AS fntotal_qty,
                         '".js_esc($_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";

          //FORLOOP GRID
          for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {

          //MENGAMBIL NAMA BARANG
            $rs=mysql_query("select ftitem_name from tlstock_hdr where ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'");
            $r=mysql_fetch_assoc($rs);

            $ftprint_name = $_REQUEST['ftprint_nameD0'][$j];

              //INSERT DO DETAIL
              $strSQL['Insert DO Det'][]="
              INSERT INTO txdelivery_order_det
              (ftbranch_code, ftdo_no, fnindex,
              ftitem_code, ftitem_name, ftprint_name,
              ftwarehouse_code, ftlocation_code,
              fnqty_available, fnqty_so,
              fnqty, ftuom_code, fdtrans_date,
              ftdescription,
              fnqty_base,ffformula,
              ftcreated_user, fdcreated_date,
              ftmodified_user, fdmodified_date)
              SELECT
              '{$_SESSION['strbranch']}' AS ftbranch_code,
              '{$ftdo_no}' AS ftdo_no,
              '{$j}' AS fnindex,
              '".js_esc($_REQUEST['ftitem_codeD0'][$j])."' AS ftitem_code,
              '".js_esc($r['ftitem_name'])."' AS ftitem_name,
              '".js_esc($ftprint_name)."' AS ftprint_name,
              '{$_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_so,
              '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty,
              '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code,
              '{$fdtrans_date}' AS  fdtrans_date,
              '".js_esc($_REQUEST['ftdescriptionD0'][$j])."' AS ftdescription,
              '".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";

              $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, fdmodified_date,
              ftprint_name,ftdescription)
              SELECT
              '{$_SESSION['strbranch']}' AS ftbranch_code,
              '{$ftstock_out_no}' AS ftstock_out_no,
              '{$j}' AS fnindex, '".js_esc($_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,
              now() AS fdmodified_date,
              '".js_esc($ftprint_name)."',
              '".js_esc($_REQUEST['ftdescriptionD0'][$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);

            }





        break;
    }

      // echo "<pre>";
      // print_r($strSQL);
      // echo "</pre>";
      //
      // die;
      return $strSQL;

    //  mysql_query($query);

 }
