<?php

function SaveData() {
    global $objAxpDB;
    $strSQL=array();
       // $objAxpDB->Allow_save(false);
    switch($_REQUEST[mode]) {
        case "simpan":


          for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {

            $ssql="UPDATE txsales_order_det
                    SET fnqty_accept_base=IFNULL(fnqty_accept_base,0) + ".(double)CCur($_REQUEST['fnqty_baseD0'][$j]).",
                        fnqty_accept= IFNULL(fnqty_accept,0) + (".(double)CCur($_REQUEST['fnqty_baseD0'][$j])."/ffformula)
                    WHERE ftso_no='{$_REQUEST['ftso_noD0'][$j]}' AND
                        ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}'";

            $ambil_total_masuk = $ambil_total_masuk + $_REQUEST['fnqty_baseD0'][$j];

            $strSQL['Update txsales_order_det'][]=$ssql;

          }


            $cek_data_so = "SELECT SUM(fnqty_base) AS total_so
            FROM txsales_order_det WHERE `ftso_no` = '{$_REQUEST['ftdocument_no']}'";

            $cek_tot_mutasi = "SELECT SUM(`fftotal_qty`) AS total_qty_hdr FROM `txstock_mutation_hdr` WHERE `ftdocument_no` = '{$_REQUEST['ftdocument_no']}' AND fnstatus = 1";

            $rss=mysql_query($cek_data_so);
            $ra=mysql_fetch_assoc($rss);

            $rss2=mysql_query($cek_tot_mutasi);
            $ra2=mysql_fetch_assoc($rss2);

            $replace = $ra2['total_qty_hdr'] + $ambil_total_masuk;

            // $replace = $ra['total_so'];

            if ($replace >= $ra['total_so']) {

               $ssql="UPDATE txsales_order_hdr
                        SET fnreceive_status = '3'
                        WHERE ftso_no='{$_REQUEST['ftdocument_no']}'";

              $strSQL['Update txsales_order_hdr'][]=$ssql;

            }else{


              $ssql="UPDATE txsales_order_hdr
                      SET fnreceive_status = '2'
                      WHERE ftso_no='{$_REQUEST['ftdocument_no']}'";

              $strSQL['Update txsales_order_hdr'][]=$ssql;

            }


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

function SaveDataAfter() {
    global $objAxpDB;
    $strSQL=array();

    switch($_REQUEST[mode]) {
        case "simpan":
            $strSQL['Update tlstock_det1'][]= "
                UPDATE tlstock_det a
                INNER JOIN (SELECT ftitem_code,ftwarehouse_from,ftlocation_from, fdexp_date, SUM(fnqty) AS fnqty
                                FROM txstock_mutation_det
                                WHERE ftstock_mutation_no = '".$_REQUEST['ftstock_mutation_no']."'
                                GROUP BY ftitem_code,ftwarehouse_from,ftlocation_from,fdexp_date) b
                       ON a.ftitem_code=b.ftitem_code AND
                          a.ftWarehouse_Code=b.ftwarehouse_from AND
                          a.ftlocation_code=b.ftlocation_from AND
                          a.fdexp_date=b.fdexp_date
                SET a.fnqty_available=a.fnqty_available - b.fnqty";

            $strSQL['Update tlstock_det2'][]= "
                UPDATE tlstock_det a
		INNER JOIN (SELECT ftitem_code,ftwarehouse_to,ftlocation_to,fdexp_date, SUM(fnqty) AS fnqty
				FROM txstock_mutation_det
				WHERE ftstock_mutation_no = '".$_REQUEST['ftstock_mutation_no']."'
				GROUP BY ftitem_code,ftwarehouse_to,ftlocation_to,fdexp_date) b
		       ON a.ftitem_code=b.ftitem_code AND
			  a.ftWarehouse_Code=b.ftwarehouse_to AND
			  a.ftlocation_code=b.ftlocation_to AND
                          a.fdexp_date=b.fdexp_date
                SET a.fnqty_available=a.fnqty_available + b.fnqty";

        break;
    }

 return $strSQL;
}

function RevisiData() {
    global $objAxpDB;
     // $objAxpDB->Allow_Revisi(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {

        case "simpan_revisi":


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

            //
            $strSQL["Revisi"][]="UPDATE txstock_mutation_hdr
                                    SET fdmodified_date=now(),ftmodified_user='{$_SESSION['username']}',
                                    fnstatus=3,ftstock_mutation_no='{$fttrans_no_new}'
                                    WHERE ftstock_mutation_no='{$_REQUEST['ftstock_mutation_no']}'";
            $strSQL["Revisi"][]="UPDATE txstock_mutation_det
                                    SET ftstock_mutation_no='{$fttrans_no_new}'
                                    where ftstock_mutation_no='{$_REQUEST['ftstock_mutation_no']}'";


            // Pengembalian Stock transaksi lama
            $rs=mysql_query("SELECT * FROM txstock_mutation_det WHERE ftstock_mutation_no='{$_REQUEST['ftstock_mutation_no']}'");

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


                $m=SaveStock('+',$r['ftitem_code'],'',$r['ftwarehouse_from'],$r['ftlocation_from'],'',$r['fnqty_base'],$r['fnqty_base']);

                $strSQL=array_merge_recursive($strSQL,$m);

                $m=SaveStock('-',$r['ftitem_code'],'',$r['ftwarehouse_to'],$r['ftlocation_to'],'',$r['fnqty_base'],$r['fnqty_base']);

                $strSQL=array_merge_recursive($strSQL,$m);

            }


            // Save Stock transaksi baru
            for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {

                $m=SaveStock('-',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_fromD0'][$j],$_REQUEST['ftlocation_fromD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                $strSQL=array_merge_recursive($strSQL,$m);

                $m=SaveStock('+',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_toD0'][$j],$_REQUEST['ftlocation_toD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                $strSQL=array_merge_recursive($strSQL,$m);

          }


      $ambil =  "SELECT `ftitem_code`,`ftitem_name`,`fnindex`,
       `fnqty_base` AS fnqty_base_m
        FROM `txstock_mutation_det` WHERE `ftstock_mutation_no` = '{$_REQUEST['ftstock_mutation_no']}'";

        $rs=mysql_query($ambil);

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

          $ssql="UPDATE txsales_order_det
                  SET fnqty_accept_base=IFNULL(fnqty_accept_base,0) - ".(double)CCur($r['fnqty_base_m']).",
                      fnqty_accept= IFNULL(fnqty_accept,0) - (".(double)CCur($r['fnqty_base_m'])."/ffformula)
                  WHERE ftso_no='{$_REQUEST['ftdocument_no']}' AND
                      ftitem_code='{$r['ftitem_code']}' AND
                      fnindex='{$r['fnindex']}' ";

          $ambil_total_batal = $ambil_total_batal + $r['fnqty_base_m'];

          $strSQL['Update txsales_order_det'][]=$ssql;

      }

      for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {

        $ssql="UPDATE txsales_order_det
                SET fnqty_accept_base=IFNULL(fnqty_accept_base,0) + ".(double)CCur($_REQUEST['fnqty_baseD0'][$j]).",
                    fnqty_accept= IFNULL(fnqty_accept,0) + (".(double)CCur($_REQUEST['fnqty_baseD0'][$j])."/ffformula)
                WHERE ftso_no='{$_REQUEST['ftdocument_no']}' AND
                    ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}'";

        $strSQL['Update txsales_order_det'][]=$ssql;

        $ambil_total_masuk = $ambil_total_masuk + $_REQUEST['fnqty_baseD0'][$j];

      }

       $cek_data_so = "SELECT SUM(fnqty_base) AS total_so
                   FROM txsales_order_det WHERE `ftso_no` = '{$_REQUEST['ftdocument_no']}'";

       $cek_tot_mutasi = "SELECT SUM(`fftotal_qty`) AS total_qty_hdr FROM `txstock_mutation_hdr` WHERE `ftdocument_no` = '{$_REQUEST['ftdocument_no']}' AND fnstatus = 1";

       $rss=mysql_query($cek_data_so);
       $ra=mysql_fetch_assoc($rss);

       $rss2=mysql_query($cek_tot_mutasi);
       $ra2=mysql_fetch_assoc($rss2);

       //penjumlahan
      $replace = $ra2['total_qty_hdr'] + $ambil_total_masuk - $ambil_total_batal;
       //

       if ($replace == $ra['total_so']) {

          $ssql="UPDATE txsales_order_hdr
                   SET fnreceive_status = '3'
                   WHERE ftso_no='{$_REQUEST['ftdocument_no']}'";

         $strSQL['Update txsales_order_hdr'][]=$ssql;

       }else{

         $ssql="UPDATE txsales_order_hdr
                 SET fnreceive_status = '2'
                 WHERE ftso_no='{$_REQUEST['ftdocument_no']}'";

         $strSQL['Update txsales_order_hdr'][]=$ssql;

       }
            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++) {
                $m=SaveStock('+',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_fromD0'][$j],$_REQUEST['ftlocation_fromD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                $strSQL=array_merge_recursive($strSQL,$m);
                $m=SaveStock('-',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_toD0'][$j],$_REQUEST['ftlocation_toD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                $strSQL=array_merge_recursive($strSQL,$m);

              }

                for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {

                  $ssql="UPDATE txsales_order_det
                          SET fnqty_accept_base=IFNULL(fnqty_accept_base,0) - ".(double)CCur($_REQUEST['fnqty_baseD0'][$j]).",
                              fnqty_accept= IFNULL(fnqty_accept,0) - (".(double)CCur($_REQUEST['fnqty_baseD0'][$j])."/ffformula)
                          WHERE ftso_no='{$_REQUEST['ftdocument_no']}' AND
                              ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' AND
                              fnindex='{$_REQUEST['fnindexD0'][$j]}' ";

                  $ambil_total_batal = $ambil_total_batal + $_REQUEST['fnqty_baseD0'][$j];

                  $strSQL['Update txsales_order_det'][]=$ssql;

                }

                $cek_tot_mutasi = "SELECT SUM(`fftotal_qty`) AS total_qty_hdr FROM `txstock_mutation_hdr` WHERE `ftdocument_no` = '{$_REQUEST['ftdocument_no']}' AND fnstatus = 1";

                $rss2=mysql_query($cek_tot_mutasi);
                $ra2=mysql_fetch_assoc($rss2);

                $result = $ra2['total_qty_hdr'] - $ambil_total_batal;

                if ($result == 0) {

                   $ssql="UPDATE txsales_order_hdr
                            SET fnreceive_status = '1'
                            WHERE ftso_no='{$_REQUEST['ftdocument_no']}'";

                  $strSQL['Update txsales_order_hdr'][]=$ssql;

                }else {

                  $ssql="UPDATE txsales_order_hdr
                          SET fnreceive_status = '2'
                          WHERE ftso_no='{$_REQUEST['ftdocument_no']}'";

                  $strSQL['Update txsales_order_hdr'][]=$ssql;

                }



        }
        break;
    }

     return $strSQL;
}
// function RevisiDataAfter() {
//     global $objAxpDB;
//     // $objAxpDB->Allow_Revisi(false);
//     $strSQL=array();
//     switch($_REQUEST[mode]) {
//         case "simpan_revisi":
//         // die;
//             // Save Stock transaksi baru
//             for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
//                 $m=SaveStock('-',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_fromD0'][$j],$_REQUEST['ftlocation_fromD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
//                 $strSQL=array_merge_recursive($strSQL,$m);
//                 $m=SaveStock('+',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_toD0'][$j],$_REQUEST['ftlocation_toD0'][$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;
// }


?>
