<?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("");
        }
    }
}

    function SaveData() {
        global $objAxpDB;
        $objAxpDB->Allow_Save(true);
        $strSQL=array();
        ValCheckLocation();
        
        switch($_REQUEST[mode]) {
        case "simpan":

            //update qty retur di pi
            //-------------------------------
            for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                 $strSQL['Update qty retur txpurchase_invoice_det'][]="UPDATE txpurchase_invoice_det
                                    SET fnqty_return=IFNULL(fnqty_return,0)+".CCur($_REQUEST['fnqtyD0'][$j])."
                                    WHERE ftpurchase_invoice_no='{$_REQUEST['ftpurchase_invoice_no']}' AND
                                            ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}'";
            }

            $strSQL['Update qty retur txpurchase_invoice_hdr'][]="UPDATE txpurchase_invoice_hdr
                                SET fcreturn_amount= IFNULL(fcreturn_amount,0) + ".CCur($_REQUEST['fcamount'])."
                                WHERE ftpurchase_invoice_no='{$_REQUEST['ftpurchase_invoice_no']}'";
                                
            // transaksi stock keluar
            /*$ftstock_out_no=getAutoNumber('tn108','txstock_out_hdr_bahan_baku','ftstock_out_no',$_REQUEST['fdtrans_date']);
            $dt=getDateYmd($_REQUEST['fdtrans_date']);
            $strSQL['Insert txstock_out_hdr_bahan_baku'][]="
                      INSERT INTO txstock_out_hdr_bahan_baku 
                          (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, '0' AS ftcategory_code,
                         '{$_REQUEST['ftsupplier_code']}' AS  ftstock_out_company, '{$_REQUEST['ftpurchase_return_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";

           
            for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {              
               
                 $strSQL['Insert Stock Out Det'][]="
                 INSERT INTO txstock_out_det_bahan_baku 
                      (ftstock_out_no, fnindex, ftitem_code,
                      ftwarehouse_code, ftlocation_code, fnqty_available,
                      fnqty, ftuom_code, ftcreated_user,
                      fdcreated_date, ftmodified_user
                      )
                      SELECT
                      '{$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,  '{$_SESSION['username']}' AS ftcreated_user,
                      now() AS fdcreated_date,  '{$_SESSION['username']}' AS ftmodified_user";
               
            }
              $strSQL['Insert Stock Out Det'][]="UPDATE  txstock_out_hdr_bahan_baku
                                                    SET fntotal_qty=(SELECT SUM(fnqty) AS fnqty FROM txstock_out_det_bahan_baku  WHERE ftstock_out_no='{$ftstock_out_no}')
                                                  WHERE ftstock_out_no='{$ftstock_out_no}'";

              //update qty
             for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                 $m=SaveStock_bahan_baku('-',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],CCur($_REQUEST['fnqtyD0'][$j]),CCur($_REQUEST['fnqtyD0'][$j]));
                 $strSQL=array_merge_recursive($strSQL,$m);
            }*/

            break;
    }
    return $strSQL;

   }
    
 function SaveDataLast() {
        global $objAxpDB;
        $objAxpDB->Allow_Save(true);
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "simpan":

        $ssql="DELETE FROM txpurchase_return_det WHERE fnqty=0 AND ftpurchase_return_no='{$_REQUEST['ftpurchase_return_no']}' ";
        $strSQL['txpurchase_return_det Index'][]= $ssql;
        
        $ssql=" update txpurchase_return_det a,
                        (select c.fnid,(select ifnull(count(d.fnid),0)+1
                                        from txpurchase_return_det d
                                        where d.fnid<c.fnid and d.ftpurchase_return_no='{$_REQUEST['ftpurchase_return_no']}') as m
                                from  txpurchase_return_det c
                                where c.ftpurchase_return_no='{$_REQUEST['ftpurchase_return_no']}'
                                ) b
                        set a.fnindex=b.m
                        where a.ftpurchase_return_no='{$_REQUEST['ftpurchase_return_no']}' and a.fnid=b.fnid";

         $strSQL['txpurchase_return_det Index'][]= $ssql;
        

            break;
    }
    return $strSQL;

   }




 function CancelData() {
     
        global $objAxpDB;
        $objAxpDB->Allow_Cancel(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
          case "batal":
              $strSQL["Batal txpurchase_return_hdr"][] =
                    "update txpurchase_return_hdr 
                            set ftmodified_user='{$_SESSION['username']}',fdmodified_date=now(), 
                                fnstatus=9
                                WHERE ftpurchase_return_no='{$_REQUEST['ftpurchase_return_no']}'";


               //update qty retur di pi
            //-------------------------------
            for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                 $strSQL['Update qty retur txpurchase_invoice_det'][]="UPDATE txpurchase_invoice_det
                                                            SET fnqty_return=IFNULL(fnqty_return,0)-".CCur($_REQUEST['fnqtyD0'][$j])."
                                                            WHERE ftpurchase_invoice_no='{$_REQUEST['ftpurchase_invoice_no']}' AND
                                                                    ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}'";
            }

            $strSQL['Update qty retur txpurchase_invoice_hdr'][]="UPDATE txpurchase_invoice_hdr
                                SET fcreturn_amount= IFNULL(fcreturn_amount,0) - ".CCur($_REQUEST['fcamount'])."
                                WHERE ftpurchase_invoice_no='{$_REQUEST['ftpurchase_invoice_no']}'";

            /*  $strSQL['Insert Stock Out Det'][]="UPDATE  txstock_out_hdr_bahan_baku
                                                    SET fnstatus='9' 
                                                  WHERE ftdocument_no='{$_REQUEST['ftpurchase_return_no']}'";

             //update qty
             for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                 $m=SaveStock_bahan_baku('+',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],CCur($_REQUEST['fnqtyD0'][$j]),CCur($_REQUEST['fnqtyD0'][$j]));
                 $strSQL=array_merge_recursive($strSQL,$m);
            }*/
        break;
        }

        return $strSQL;
      }
?>
