<?php
switch($_REQUEST[mode]) {
    case "hapus":
        if(RetRstField("SELECT SUM(fnid) AS fnid FROM (
                SELECT fnid FROM tlstock_det WHERE ftlocation_code = '{$_REQUEST['ftlocation_code']}' AND (fnqty_available <> 0)
                UNION ALL SELECT fnid FROM txinnitial_stock_det WHERE ftlocation_code='{$_REQUEST['ftlocation_code']}'
                UNION ALL SELECT fnid FROM txstock_adjustment_det WHERE ftlocation_code = '{$_REQUEST['ftlocation_code']}'
                UNION ALL SELECT fnid FROM txstock_mutation_det WHERE ftlocation_from = '{$_REQUEST['ftlocation_code']}'
                UNION ALL SELECT fnid FROM txstock_mutation_det WHERE ftlocation_to = '{$_REQUEST['ftlocation_code']}'
                UNION ALL SELECT fnid FROM txstock_out_det WHERE ftlocation_code = '{$_REQUEST['ftlocation_code']}'
                UNION ALL SELECT fnid FROM txstock_in_det WHERE ftlocation_code = '{$_REQUEST['ftlocation_code']}'
                ) X ")) {

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

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

        $strSQL["tlstock_det"][]=" INSERT INTO tlstock_det(ftBranch_Code,ftWarehouse_Code,ftLocation_Code,ftItem_Code,ftitem_name,
                                        ftBatch_Code,fnqty_available,ftCreated_User,fdCreated_Date,ftModified_User,fdModified_Date)
                                        SELECT '{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftwarehouse_code']}' AS ftwarehouse_code,'{$_REQUEST['ftlocation_code']}' AS ftlocation_code ,ftitem_code,ftitem_name,
                                        '' AS ftBatch_Code,'0' AS fnqty_available,'{$_SESSION['username']}' AS ftcreated_user,now() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,now() AS fdmodified_date
                                        FROM tlstock_hdr";

        $strSQL["tlstock_det_setengah_jadi"][]=" INSERT INTO tlstock_det_setengah_jadi(ftBranch_Code,ftWarehouse_Code,ftLocation_Code,ftItem_Code,ftitem_name,
                                        ftBatch_Code,fnqty_available,ftCreated_User,fdCreated_Date,ftModified_User,fdModified_Date)
                                        SELECT '{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftwarehouse_code']}' AS ftwarehouse_code,'{$_REQUEST['ftlocation_code']}' AS ftlocation_code ,ftitem_code,ftitem_name,
                                        '' AS ftBatch_Code,'0' AS fnqty_available,'{$_SESSION['username']}' AS ftcreated_user,now() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,now() AS fdmodified_date
                                        FROM tlstock_hdr_setengah_jadi";

        $strSQL["tlstock_det_bahan_baku"][]=" INSERT INTO tlstock_det_bahan_baku(ftBranch_Code,ftWarehouse_Code,ftLocation_Code,ftItem_Code,ftitem_name,
                                        ftBatch_Code,fnqty_available,ftCreated_User,fdCreated_Date,ftModified_User,fdModified_Date)
                                        SELECT '{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftwarehouse_code']}' AS ftwarehouse_code,'{$_REQUEST['ftlocation_code']}' AS ftlocation_code ,ftitem_code,ftitem_name,
                                        '' AS ftBatch_Code,'0' AS fnqty_available,'{$_SESSION['username']}' AS ftcreated_user,now() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,now() AS fdmodified_date
                                        FROM tlstock_hdr_bahan_baku";

        $strSQL["tlstock_det_sparepart"][]=" INSERT INTO tlstock_det_sparepart(ftBranch_Code,ftWarehouse_Code,ftLocation_Code,ftItem_Code,ftitem_name,
                                        ftBatch_Code,fnqty_available,ftCreated_User,fdCreated_Date,ftModified_User,fdModified_Date)
                                        SELECT '{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftwarehouse_code']}' AS ftwarehouse_code,'{$_REQUEST['ftlocation_code']}' AS ftlocation_code ,ftitem_code,ftitem_name,
                                        '' AS ftBatch_Code,'0' AS fnqty_available,'{$_SESSION['username']}' AS ftcreated_user,now() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,now() AS fdmodified_date
                                        FROM tlstock_hdr_sparepart";

        break;

    }

    return $strSQL;
}


function DeleteData() {
    global $objAxpDB;
    $objAxpDB->Allow_Delete(true);
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "hapus":

        $strSQL["tlstock_det"][]=" DELETE FROM tlstock_det WHERE ftlocation_code = '{$_REQUEST['ftlocation_code']}' ";

        $strSQL["tlstock_det_setengah_jadi"][]=" DELETE FROM tlstock_det_setengah_jadi WHERE ftlocation_code = '{$_REQUEST['ftlocation_code']}' ";

        $strSQL["tlstock_det_bahan_baku"][]=" DELETE FROM tlstock_det_bahan_baku WHERE ftlocation_code = '{$_REQUEST['ftlocation_code']}' ";

        $strSQL["tlstock_det_sparepart"][]=" DELETE FROM tlstock_det_sparepart WHERE ftlocation_code = '{$_REQUEST['ftlocation_code']}' ";

        break;

    }

    return $strSQL;
}

   
?>
