<?php
   switch($_REQUEST[mode]) {
    case "hapus":
        if(RetRstField("SELECT SUM(fnid) AS fnid FROM (
                SELECT fnid FROM tlstock_det_sparepart WHERE ftitem_code = '{$_REQUEST['ftitem_code']}' AND (fnqty_on_hand <> 0 OR fnqty_available <> 0)
                UNION ALL SELECT fnid FROM txinnitial_stock_det_sparepart WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'
                UNION ALL SELECT fnid FROM txstock_adjustment_det_sparepart WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'
                UNION ALL SELECT fnid FROM txstock_mutation_det_sparepart WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'
                UNION ALL SELECT fnid FROM txstock_out_det_sparepart WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'

                UNION ALL SELECT fnid FROM tlstock_package WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'
                UNION ALL SELECT fnid FROM tlstock_set WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'
                UNION ALL SELECT fnid FROM txdelivery_order_set WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'
                UNION ALL SELECT fnid FROM txdelivery_order_det WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'
                UNION ALL SELECT fnid FROM txestimation_det WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'
                UNION ALL SELECT fnid FROM txestimation_poduksi_det WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'
                UNION ALL SELECT fnid FROM txproduction_det WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'
                UNION ALL SELECT fnid FROM txproduction_order_request_det WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'
                UNION ALL SELECT fnid FROM txpurchase_order_det WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'
                UNION ALL SELECT fnid FROM txpurchase_request_det WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'
                UNION ALL SELECT fnid FROM txpurchase_invoice_det WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'
                UNION ALL SELECT fnid FROM txpurchase_return_det WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'
                UNION ALL SELECT fnid FROM txsales_order_det WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'
                UNION ALL SELECT fnid FROM txsales_invoice_det WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'
                UNION ALL SELECT fnid FROM txsales_return_det WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'
                UNION ALL SELECT fnid FROM txpurchase_order_det WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'
                UNION ALL SELECT fnid FROM txstock_in_det WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'
                ) X ")) {

                echo "<script>
                    alert('data tidak dapat di hapus karena sudah digunakan dalam transaksi ...');
                    window.history.back();
                </script>";
            die("");
         }

     break;
	case "update":
		$i=count($_REQUEST['fnindexD1']);
		$m= '';	

		for($j=1;$j<$i;$j++)
		{
			for( $k=1;$k<$i;$k++)
			{
				if ($j!=$k)
					{
						if ($_REQUEST['ftuom_code_multiD1'][$j]==$_REQUEST['ftuom_code_multiD1'][$k])
						{
							$m.=$j.$_REQUEST['ftuom_code_multiD1'][$j] .'   ' .$k.$_REQUEST['ftuom_code_multiD1'][$k]  ."\\n";
						}
					}
				
			}

		}
		if ($m!='') 
		{
		echo "<script>
				alert('Satuan Multi Tidak Boleh Sama');
				window.history.back();
			</script>";
			die("");
		}
    break;	
   }

    function UpdateData() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "update":
        break;
    }

    return $strSQL;
 }
 
   function UpdateDataLast() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "update":
         $strSQL["n"][]="DELETE FROM  tlstock_multi_det_sparepart
                     where  ftitem_code='".$_REQUEST['ftitem_code']."'";	
	
         $strSQL["n"][]="update tlstock_hdr_sparepart
                     set ftstock_group_name='".$_REQUEST['hidden_ftstock_group_code']."'
                        where  ftitem_code='".$_REQUEST['ftitem_code']."'";
			                
         for($j=1; $j<count($_REQUEST['fnindexD1']); $j++) {
         $strSQL["tlstock_multi_det_sparepart"][]=" INSERT INTO tlstock_multi_det_sparepart
                             (ftitem_code, ftitem_name, 
                                ftuom_code_multi, ffformula, ftketerangan,
                                ftcreated_user,fdcreated_date, ftmodified_user,fdmodified_date
								)
                        SELECT '{$_REQUEST['ftitem_code']}' AS ftitem_code ,'{$_REQUEST['ftitem_name']}' AS ftitem_name,
                                 '{$_REQUEST['ftuom_code_multiD1'][$j]}' AS ftuom_code_multi,'{$_REQUEST['ffformulaD1'][$j]}' AS  ffformula, '{$_REQUEST['ftketeranganD1'][$j]}' AS ftketerangan,
								 '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date";
            }

        break;

    }

    return $strSQL;
 }

?>
