<?php

function ValCheckLocation() {

    for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
        if ((CCur($_REQUEST['fnqty_baseD0'][$j]) > 0) && (CCur($_REQUEST['ftlocation_codeD0'][$j])=="")) {
            echo "<script>
                    alert('Lokasi harus diisi. ');
                    window.history.back();
                  </script>";
            die("");
        }
    }
}

switch($_REQUEST[mode]) {
case "batal":
    $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);
    ValCheckLocation();

    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, ftwarehouse_code, ftlocation_code, fdexp_date,
                    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['ftwarehouse_codeD0'][$i]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$i]}' AS ftlocation_code, '{$_REQUEST['fdexp_dateD0'][$i]}' AS fdexp_date,
                    '".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 ";

              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_sparepart('+',$_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":

              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_sparepart('-',$_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);
                }

        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;
  }