<?php

    function ValBatalPI() {

        $ssql="select a.ftcvs_no,b.ftpi_no
                from txrecap_bank_voucher_hdr a, txrecap_bank_voucher_det b
                where a.ftcvs_no=b.ftcvs_no and a.fnstatus<>9 and a.fnstatus<>3
                        and b.ftpi_no='{$_REQUEST['ftpurchase_invoice_no']}'";

        $rs=mysql_query($ssql);
        $str="No. Transaksi Pembayaran Hutang ini harus dibatal kan dahulu :";
        while($r=mysql_fetch_assoc($rs)) {
            $str.=$r['ftcvs_no'];
        }

        if(RetRstField($ssql)) {
            echo "<script> alert('$str'); window.history.back(); </script>";
            die("");
        }

        $ssql="select ftpurchase_return_no 
                from txpurchase_return_hdr
                where  fnstatus<>9 and fnstatus<>3
                        AND ftpurchase_invoice_no='{$_REQUEST['ftpurchase_invoice_no']}'";

        $rs=mysql_query($ssql) or die ("query error {$ssql}");
        $ftpurchase_return_no="";
        while($r=mysql_fetch_assoc($rs)) {
           $ftpurchase_return_no.="- {$r['ftpurchase_return_no']}\\n";
           
        }
        if($ftpurchase_return_no!="") {
             $massage=" Purchase Return berikut harus dibatalkan terlebih dahulu \\n";
             $massage.="---------------------------------------------------------\\n";
             $massage.=$ftpurchase_return_no;
        }
        if($massage!="") {
          echo "<script>
                alert('{$massage}');
                window.history.back();
            </script>";
            die("");
        }
        
    }

    
    function SaveData() {
        global $objAxpDB;
        // $objAxpDB->Allow_Save(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
            case "simpan":
                $ssql="SELECT fnid, ftdocument_no, ftstock_in_company, ftcategory_code FROM txstock_in_hdr
                        WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}'";
                $rs=mysql_query($ssql) or die($ssql);
                $r=mysql_fetch_assoc($rs);
                if(isset($r["fnid"])):
                    $strSQL["Update txstock_in_hdr"][] = "UPDATE txstock_in_hdr SET fnstatus=2 WHERE fnid='{$r["fnid"]}'";
                else:
                    $strSQL["Update txpurchase_order_hdr"][] = "UPDATE txpurchase_order_hdr SET fnstatus=2, fnreceipt_status = CASE WHEN (fnreceipt_status = 1 AND fnhdr_category=2) THEN 3 ELSE fnreceipt_status END WHERE ftpo_no='{$_REQUEST['ftstock_in_no']}'";
                endif;
                
                break;
      }

      return $strSQL;
    }
    

    function SaveDataLast() {
        global $objAxpDB;
        $strSQL=array();
        switch($_REQUEST[mode]) {
            case "simpan":
                $strSQL["Update Supplier Name"][]="UPDATE txpurchase_invoice_hdr a, tlsupplier b SET a.ftsupplier_name=b.ftsupplier_name WHERE a.ftsupplier_code = b.ftsupplier_code AND a.ftpurchase_invoice_no='{$_REQUEST['ftpurchase_invoice_no']}'";

            break;
        }

        return $strSQL;
    }

    function CancelData() {
      ValBatalPI();
      global $objAxpDB;
      // $objAxpDB->Allow_Cancel(false);
      $strSQL=array();
      switch($_REQUEST[mode]) {
        case "batal":
          $strSQL["Batal txpurchase_invoice_hdr"][] =
                "update txpurchase_invoice_hdr
                    set fnpayment_status=9
                    WHERE ftpurchase_invoice_no='{$_REQUEST['ftpurchase_invoice_no']}'";

          $ssql="SELECT fnid, ftdocument_no, ftstock_in_company, ftcategory_code FROM txstock_in_hdr
                  WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}'";
          $rs=mysql_query($ssql) or die($ssql);
          $r=mysql_fetch_assoc($rs);

          if(isset($r["fnid"])):
              $strSQL["Update txstock_in_hdr"][] = "UPDATE txstock_in_hdr SET fnstatus=1 WHERE fnid='{$r["fnid"]}'";
          else:
              $strSQL["Update txpurchase_order_hdr"][] = "UPDATE txpurchase_order_hdr SET fnstatus=1, fnreceipt_status = CASE WHEN (fnreceipt_status = 3 AND fnhdr_category=2) THEN 1 ELSE fnreceipt_status END WHERE ftpo_no='{$_REQUEST['ftstock_in_no']}'";
          endif;

        break;
      }

      return $strSQL;
    }


function RevisiData() {
    
    global $objAxpDB;
    $objAxpDB->Allow_Revisi(true);
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "revisi":

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

             $ftpurchase_invoice_no_new="{$_REQUEST['ftpurchase_invoice_no']}{$str1}";

             $strSQL["batal"][]="update txpurchase_invoice_hdr
                                set fnstatus=3,ftrevisi_user='{$_SESSION['username']}',
                                     ftpurchase_invoice_no='{$ftpurchase_invoice_no_new}'
                                where ftpurchase_invoice_no='{$_REQUEST['ftpurchase_invoice_no']}'";

             $strSQL["batal"][]="update txpurchase_invoice_det
                                set ftpurchase_invoice_no='{$ftpurchase_invoice_no_new}'
                                where ftpurchase_invoice_no='{$_REQUEST['ftpurchase_invoice_no']}'";

             $ssql="select a.ftcvs_no,b.ftpurchase_invoice_no
                    from txrecap_bank_voucher_hdr a,
                            txrecap_bank_voucher_det b
                    where a.ftcvs_no=b.ftcvs_no and
                             a.fnstatus<> and a.fnstatus2=1
                      and b.ftpurchase_invoice_no='{$_REQUEST['ftpurchase_invoice_no']}'";

            $rs=mysql_query($ssql) or die ($ssql) ;
            while($r=mysql_fetch_assoc($rs)) {
                 $strSQL['Batal txrecap_bank_voucher_det'][]="DELETE FROM txrecap_bank_voucher_det
                                            WHERE ftpurchase_invoice_no='{$_REQUEST['ftpurchase_invoice_no']}' AND
                                                ftcvs_no='{$r['ftcvs_no']}'";
            }

         //--------------------------------------------

         $strSQL['Batal txrecap_bank_voucher_det'][]="UPDATE txrecap_bank_voucher_det
                                            SET ftpurchase_invoice_no='{$ftpurchase_invoice_no_new}'
                                            WHERE ftpurchase_invoice_no='{$_REQUEST['ftpurchase_invoice_no']}'";

        // Call Function Revisi General Ledger
        if(function_exists("RevisiGL")) {
            $m = RevisiGL($ftpurchase_invoice_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($_REQUEST['fnreceipt_status']==1) {
                $strSQL["Insert txpurchase_order_hdr Delivery"][] ="update txpurchase_order_hdr set fddelivery_date=fdtrans_date where ftpo_no='{$_REQUEST['ftpo_no']}'";
                $strSQL["Update txpurchase_order_det 1"][] ="update txpurchase_order_det set fdclosing_date='".CDate($_REQUEST['fdtrans_date'])."',fnstatus=1 where ftpo_no='{$_REQUEST['ftpo_no']}'";
            }
            break;
    }
    return $strSQL;
}
