<?php
$updatexxx=0;
if($_REQUEST[mode]=='update')  {
    $_REQUEST[mode]="simpan";
    $updatexxx=1;
}

switch($_REQUEST[mode]) {
    case "batal":
        /*if($_REQUEST['ftcategory_code']=="2") {
             $ssql="select ftstock_out_no,ftdocument_no from  txstock_out_hdr where fnstatus<>9 and ftdocument_no='{$_REQUEST['ftstock_in_no']}'";
             $rs=mysql_query($ssql);
             $r=mysql_fetch_assoc($rs);
             if(RetRstField($ssql)) {
                echo "<script>
                    alert('No. Stock Keluar : {$r['ftstock_out_no']} harus dibatalkan dulu ...');
                    window.history.back();
                </script>";
                die("");
             }
        }*/

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

        break;
}

function SaveData() {
    global $objAxpDB;
    global $updatexxx;
    $strSQL=array();
    $objAxpDB->Allow_Save(false);
    
    switch($_REQUEST[mode]) {
        case "simpan":
            $fdschedule=getDateYmd($_REQUEST['fdschedule_delivery_date']);
            $fdtrans=getDateYmd($_REQUEST['fdtrans_date']);
            $strSQL['Insert txstock_in_hdr'][]="INSERT INTO txstock_in_hdr(
                    ftbranch_code,
                    ftstock_in_no, ftcategory_code, ftstock_in_company,
                    fdtrans_date, ftdocument_no, fdschedule_delivery_date,
                    ftreff_no, ftchecked_user, ftnotes,
                    fntotal_qty, fnstatus, ftstock_type_code,
                    ftcreated_user, fdcreated_date, ftmodified_user, fdmodified_date
                    ) SELECT
                    '{$_SESSION['strbranch']}' AS ftbranch_code,
                    '{$_REQUEST['ftstock_in_no']}' AS ftstock_in_no, '{$_REQUEST['ftcategory_code']}' AS ftcategory_code, '{$_REQUEST['ftstock_in_company']}' AS ftstock_in_company,
                    '{$fdtrans}' AS fdtrans_date, '{$_REQUEST['ftdocument_no']}' AS ftdocument_no, '{$fdschedule}' AS fdschedule_delivery_date,
                    '{$_REQUEST['ftreff_no']}' AS ftreff_no, '{$_REQUEST['ftchecked_user']}' AS ftchecked_user, '{$_REQUEST['ftnotes']}' AS ftnotes,
                    '".CCur($_REQUEST['fntotal_qty'])."' AS fntotal_qty, '{$_REQUEST['fnstatus']}' AS fnstatus, '{$_REQUEST['ftstock_type_code']}' AS ftstock_type_code,
                    '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date, '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date";

            for($i=1; $i < count($_REQUEST['fnindexD0']); $i++){
              if($_REQUEST['fnqtyD0'][$i] > 0){
                  $strSQL['Insert txstock_in_det'][]="INSERT INTO txstock_in_det (
                        fnid, fnindex, ftbranch_code, ftstock_in_no,
                        ftitem_code, fdexp_date, ftwarehouse_code, ftlocation_code,
                        fnqtypo, fnqtysisapo, fnqty, fnqty_base, fnqty_broken, fcprice,
                        ftuom_code, ffformula, ftcreated_user, fdcreated_date, ftmodified_user, fdmodified_date
                        ) SELECT
                        '' AS fnid, '{$i}' AS fnindex, '{$_SESSION['strbranch']}' AS ftbranch_code, '{$_REQUEST['ftstock_in_no']}' AS ftstock_in_no,
                        '{$_REQUEST['ftitem_codeD0'][$i]}' AS ftitem_code, '{$_REQUEST['fdexp_dateD0'][$i]}' AS fdexp_date, '{$_REQUEST['ftwarehouse_codeD0'][$i]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$i]}' AS ftlocation_code,
                        '".CCur($_REQUEST['fnqtypoD0'][$i])."' AS fnqtypo, '".CCur($_REQUEST['fnqtysisapoD0'][$i])."' AS fnqtysisapo, '".CCur($_REQUEST['fnqtyD0'][$i])."' AS fnqty,
                        '".CCur($_REQUEST['fnqty_baseD0'][$i])."' AS fnqty_base,'".CCur($_REQUEST['fnqty_brokenD0'][$i])."' AS fnqty_broken, '".CCur($_REQUEST['fcpriceD0'][$i])."' AS fcprice,
                        '{$_REQUEST['ftuom_codeD0'][$i]}' AS ftuom_code, '".CCur($_REQUEST['ffformulaD0'][$i])."' AS ffformula,'{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date,
                        '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date";

                // next phase : cek cara kerja yang lebih baik utk terima hasil outsource link dgn PO.    
                /*        
                  if($_REQUEST["ftcategory_code"] == 6){
                       $ssql="SELECT ftdocument_no, ftstock_out_no FROM (";
                       $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr UNION ALL ";
                       $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr_bahan_baku UNION ALL ";
                       $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr_sparepart UNION ALL ";
                       $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr_setengah_jadi";
                       $ssql.=") txstock WHERE ftstock_out_no='".$_REQUEST['ftdocument_no']."'";
                       $mysql=mysql_query($ssql);
                       $row=mysql_fetch_assoc($mysql);
                       $ftpo_no=$row["ftdocument_no"];

                       $strSQL['Update txpurchase_order_det'][]="UPDATE txpurchase_order_det SET
                              fnqty_accept=IFNULL(fnqty_accept, 0) + ".(float)CCur($_REQUEST['fnqtyD0'][$i]).",
                              fnqty_accept_base=IFNULL(fnqty_accept_base, 0) + ".(float)CCur($_REQUEST['fnqty_baseD0'][$i])."
                              WHERE ftpo_no='{$ftpo_no}' AND ftitem_code='".$_REQUEST['ftitem_codeD0'][$i]."'";
                  }
                  */
                  $m=SaveStock('+',$_REQUEST['ftitem_codeD0'][$i],'',$_REQUEST['ftwarehouse_codeD0'][$i],$_REQUEST['ftlocation_codeD0'][$i],$_REQUEST['fdexp_dateD0'][$i],CCur($_REQUEST['fnqtyD0'][$i]),CCur($_REQUEST['fnqtyD0'][$i]));
                  $strSQL=array_merge_recursive($strSQL,$m);

              }
            }
             break;
        }

        return $strSQL;
 }

 function SaveDataLast(){
    global $objAxpDB;
    $objAxpDB->Allow_Save(true);
    $strSQL=array();
    switch($_REQUEST[mode]) {
      case "simpan":
         /*
        if($_REQUEST["ftcategory_code"] == 6){
          $ssql="SELECT ftdocument_no, ftstock_out_no FROM (";
          $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr UNION ALL ";
          $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr_bahan_baku UNION ALL ";
          $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr_sparepart UNION ALL ";
          $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr_setengah_jadi";
          $ssql.=") txstock WHERE ftstock_out_no='".$_REQUEST['ftdocument_no']."'";
          $mysql=mysql_query($ssql);
          $row=mysql_fetch_assoc($mysql);
          $ftpo_no=$row["ftdocument_no"];
          for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
              $strSQL["Update txpurchase_order_det last"][]="UPDATE txpurchase_order_det SET fnstatus=5 WHERE ftpo_no='{$ftpo_no}' AND ftitem_code='{$_REQUEST["ftitem_codeD0"][$j]}' AND fnqty_accept >= fnqty";
          }
          
        }*/
        break;
    }
    return $strSQL;
 }


 function SaveDataAfter(){
    global $objAxpDB;
    $objAxpDB->Allow_Save(true);
    $strSQL=array();
    switch($_REQUEST[mode]) {
      case "simpan":
        /*  
        if($_REQUEST["ftcategory_code"] == 6){

            $ssql="SELECT ftdocument_no, ftstock_out_no FROM (";
            $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr UNION ALL ";
            $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr_bahan_baku UNION ALL ";
            $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr_sparepart UNION ALL ";
            $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr_setengah_jadi";
            $ssql.=") txstock WHERE ftstock_out_no='".$_REQUEST['ftdocument_no']."'";
            $mysql=mysql_query($ssql);
            $row=mysql_fetch_assoc($mysql);
            $ftpo_no=$row["ftdocument_no"];

            $ssql="SELECT IFNULL(SUM(fnqty),0) - IFNULL(SUM(fnqty_accept),0) AS fnqty
                    FROM txpurchase_order_det WHERE ftpo_no = '{$ftpo_no}' GROUP BY ftpo_no ";
            $fnqty_outstanding = RetRstField($ssql);

            if ($fnqty_outstanding <= 0) {
                $fnreceive_status = 3;
            } else {
                $fnreceive_status = 2;
            }
            echo $fnqty_outstanding;
            $strSQL['Update txpurchase_order_hdr after'][] =  "UPDATE txpurchase_order_hdr set fnreceipt_status = {$fnreceive_status} WHERE ftpo_no='{$ftpo_no}'  ";

        } */
        break;
    }
    return $strSQL;
 }


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

    switch($_REQUEST[mode]) {
    case "batal":
        if($_REQUEST['fnstatus']=='1') {
            for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                /*
                if($_REQUEST["ftcategory_code"] == 6){
                     $ssql="SELECT ftdocument_no, ftstock_out_no FROM (";
                     $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr UNION ALL ";
                     $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr_bahan_baku UNION ALL ";
                     $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr_sparepart UNION ALL ";
                     $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr_setengah_jadi";
                     $ssql.=") txstock WHERE ftstock_out_no='".$_REQUEST['ftdocument_no']."'";
                     $mysql=mysql_query($ssql);
                     $row=mysql_fetch_assoc($mysql);
                     $ftpo_no=$row["ftdocument_no"];

                     $strSQL['Update txpurchase_order_det'][]="UPDATE txpurchase_order_det SET
                            fnstatus=1, fnqty_accept=IFNULL(fnqty_accept, 0) - ".(float)CCur($_REQUEST['fnqtyD0'][$j]).",
                            fnqty_accept_base=IFNULL(fnqty_accept_base, 0) - ".(float)CCur($_REQUEST['fnqty_baseD0'][$j])."
                            WHERE ftpo_no='{$ftpo_no}' AND ftitem_code='".$_REQUEST['ftitem_codeD0'][$j]."'";
                  } */
                  
                     $m=SaveStock('-',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j], $_REQUEST['fdexp_dateD0'][$j],CCur($_REQUEST['fnqtyD0'][$j]),CCur($_REQUEST['fnqtyD0'][$j]));
                     $strSQL=array_merge_recursive($strSQL,$m);
                }
        }
//            $ssql1="SELECT IFNULL(MAX(ftstock_in_no),'')  AS M
//                      FROM txstock_in_hdr
//                      WHERE ftstock_in_no LIKE '{$_REQUEST['ftstock_in_no']}-%'";
//            $no=RetRstField($ssql1);
//            if($no!="") {
//                $no=$no+1;
//                $str1="-{$no}";
//            }
//            else {
//                $str1="-1";
//            }
//
//             $ftstock_in_no_new="{$_REQUEST['ftstock_in_no']}{$str1}";
//
//             $strSQL["batal"][]="update txstock_in_hdr
//                                set fnstatus=9,ftstock_in_no='{$ftstock_in_no_new}'
//                                where ftstock_in_no='{$_REQUEST['ftstock_in_no']}'";
//
//             $strSQL["batal"][]="update txstock_in_det
//                                set ftstock_in_no='{$ftstock_in_no_new}'
//                                where ftstock_in_no='{$_REQUEST['ftstock_in_no']}'";

        break;
    }

     return $strSQL;
 }

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

    switch($_REQUEST[mode]) {
      case "batal":
        /*
        if($_REQUEST["ftcategory_code"] == 6){
          $ssql="SELECT ftdocument_no, ftstock_out_no FROM (";
          $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr UNION ALL ";
          $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr_bahan_baku UNION ALL ";
          $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr_sparepart UNION ALL ";
          $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr_setengah_jadi";
          $ssql.=") txstock WHERE ftstock_out_no='".$_REQUEST['ftdocument_no']."'";
          $mysql=mysql_query($ssql);
          $row=mysql_fetch_assoc($mysql);
          $ftpo_no=$row["ftdocument_no"];

          $ssql="SELECT SUM(fnqty_accept) AS jml FROM txpurchase_order_det WHERE ftpo_no='{$ftpo_no}'";
          $mysql=mysql_query($ssql);
          $r=mysql_fetch_assoc($mysql);
          if($r["jml"] == 0)
            $strSQL["Update txpurchase_order_hdr"][]="UPDATE txpurchase_order_hdr SET fnreceipt_status=1 WHERE ftpo_no='{$ftpo_no}'";
          else
            $strSQL["Update txpurchase_order_hdr"][]="UPDATE txpurchase_order_hdr SET fnreceipt_status=2 WHERE ftpo_no='{$ftpo_no}'";
        } */
      break;
    }

    return $strSQL;
  }
  
    
function RevisiData() {
    global $objAxpDB;
    // $objAxpDB->Allow_Revisi(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
        case "simpan_revisi":
            
            $ssql1="SELECT IFNULL(MAX(ftstock_in_no),'')  AS M
                                    FROM txstock_in_hdr
                            WHERE ftstock_in_no LIKE '{$_REQUEST['ftstock_in_no']}-%'";
            $no=RetRstField($ssql1);
            if($no!="") 
            {
                    $no1=explode("-",$no);
                    $no=(int)$no1[1]+1;
                    $str1="-{$no}";
            }
            else 
            {
                    $str1="-1";
            }
            $fttrans_no_new="{$_REQUEST['ftstock_in_no']}{$str1}";

            // Pengembalian Stock transaksi lama              
            $rs=mysql_query("SELECT * FROM txstock_in_det WHERE ftstock_in_no='{$_REQUEST['ftstock_in_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 txstock_in_hdr
                                    SET fdmodified_date=now(),ftmodified_user='{$_SESSION['username']}',
                                    fnstatus=3,ftstock_in_no='{$fttrans_no_new}'
                                    WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}'";
            $strSQL["Revisi"][]="UPDATE txstock_in_det
                                    SET ftstock_in_no='{$fttrans_no_new}'
                                    where ftstock_in_no='{$_REQUEST['ftstock_in_no']}'";

            // Save Stock transaksi baru                        
            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);
            }
            
            // Call Function Revisi General Ledger
            if(function_exists("RevisiGL")) {
                $m = RevisiGL($fttrans_no_new);
                if ($m != '') { $strSQL=array_merge_recursive($strSQL,$m); }
            }
          
            break;
    }
    return $strSQL;
}
