<?php
 Getsecurity(33);
$objAxpDB->ViewName("Daftar Production Order Sheet Custom");
$objAxpDB->ViewSQL("SELECT a.fnid, a.ftpos_no, a.fdtrans_date, a.fdforecasted_date, a.ftpor_no, a.fftotal_qty, a.ftnotes, B.fttrans_status_name FROM txproduction_order_sheet_hdr
                    A INNER JOIN tstransaction_status B ON a.fnstatus=b.fntrans_status_code AND B.fntrans_group_code=2 
                    WHERE IFNULL(A.fntype,0)=1
                    ORDER BY  a.ftpos_no DESC, a.fdtrans_date DESC");


$objAxpDB->ViewCaption("ID, No. POS, Date, Forecasted Production Time, No. POR, Quantity, Note, Status");
$objAxpDB->ViewPeriod(TRUE);

$objAxpDB->FormName("Production Order Sheet Custom");
$objAxpDB->FormSQL("SELECT ftpos_no, fdtrans_date,fdforecasted_date, ftpor_no, fftotal_qty, ftnotes, fnstatus FROM txproduction_order_sheet_hdr");
$objAxpDB->FormCaption(" No. POS, Date,Forecasted Production Time, No. POR,  Quantity, Note, Status");
$objAxpDB->FormPosition(" TL, TL,TL, TR,  BR, BL, BR");
 
if($_REQUEST['id']) {
    $objAxpDB->GridSQL(0, "SELECT fnindex,fttype_barang_code,fttype_barang_name,ftcolor_code,ftsize_code,fnqty_por ,ffqty
                            FROM (
                            SELECT a.ftpos_no,a.fnindex, concat(a.ftstock_group_code,a.ftstock_subgroup_code,a.ftdecal_code) as fttype_barang_code,
                            concat(a4.ftstock_group_name,' ',a5.ftstock_subgroup_name,' ',a6.ftdecal_name) as fttype_barang_name,
                            a.ftcolor_code, a.ftsize_code,a.fnqty_por,a.ffqty
                            FROM txproduction_order_sheet_det a
                            INNER JOIN tlstock_group a4 on a.ftstock_group_code=a4.ftstock_group_code
                            INNER JOIN tlstock_subgroup a5 on a.ftstock_subgroup_code=a5.ftstock_subgroup_code
                            INNER JOIN tldecal a6 on a.ftdecal_code=a6.ftdecal_code

                            ORDER BY A.fnindex) m");
}

$objAxpDB->GridCaption(0, "No, Code Type Barang, Type Barang, Kode Warna,Kode Size, Qty POR,Qty");


 $objAxpDB->Lookup("ftcolor_code", "SELECT ftcolor_code, ftcolor_name FROM tlcolor", "ftcolor_code", "ftcolor_name", -1,false);
 $objAxpDB->Lookup("ftsize_code", "SELECT ftsize_code, ftsize_name FROM tlsize", "ftsize_code", "ftsize_name", -1,false);

$objAxpDB->Lookup( "fnstatus", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 2", "fntrans_status_code", "fttrans_status_name", 0,true);
$objAxpDB->SetStatus("id, fnstatus");
if ($objAxpDB->GetStatus("id") == "") {
       $objAxpDB->ButtonShow(" cetak_daftar, export_excel ,tutup");
} elseif ($objAxpDB->GetStatus("fnstatus") == 0) {
        $objAxpDB->ButtonShow("final,tutup");
        $objAxpDB->FormLocked("1, 1, 0,1, 1,0,  1, 1");
        $objAxpDB->GridLocked(0, "1,1, 1, 1,1,1");
        $objAxpDB->FormJS(" ValPOS(); ");
        $objAxpDB->GridDelJS(0,"ValPOS();");
        $objAxpDB->GridScript(0,6,"onChange='ValPOS();'");
}
 elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
        $objAxpDB->ButtonShow("cetak,tutup");
        $objAxpDB->FormLocked("1, 1, 1,1, 1,1,  1, 1");
        $objAxpDB->GridLocked(0, "1,1, 1, 1,1,1,1");
}
$objAxpDB->Process();

$ssql="SELECT fnstatus
        FROM txproduction_order_sheet_hdr
        WHERE fnid='".$_REQUEST['id']."'";
#die("$ssql");
if(RetRstField($ssql)==0) {
    
  $ssql="SELECT ftpos_no FROM txproduction_order_sheet_hdr WHERE fnid='".$_REQUEST['id']."'";
    $ftpos_no=RetRstField($ssql);
    $ssql="SELECT fdtrans_date FROM txproduction_order_sheet_hdr WHERE fnid='".$_REQUEST['id']."'";
    $date=RetRstField($ssql);

    $ssql="SELECT SUM(IFNULL(ffqty,fnqty_por)) AS fftotal_qty FROM txproduction_order_sheet_det WHERE ftpos_no='{$ftpos_no}' ";

    $rs=mysql_query($ssql);
    $r=mysql_fetch_assoc($rs);
    if($r['fftotal_qty']>=2000) {
        $n=0;
        while($n!=60) {
            $ssql_1="SELECT date_add('{$date}',INTERVAL 1 DAY)";
            $date=RetRstField($ssql_1);
            $ssql_2="SELECT  DATE_FORMAT('{$date}','%W')";
            $day=RetRstField($ssql_2);
            if(($day=="Saturday")||($day=="Sunday")) {

            }
            else {
                $n++;
            }
        }

    }
    else {
         $n=0;
        while($n!=30) {
            $ssql_1="SELECT date_add('{$date}',INTERVAL 1 DAY)";
            $date=RetRstField($ssql_1);
            $ssql_2="SELECT  DATE_FORMAT('{$date}','%W')";
            $day=RetRstField($ssql_2);
            if(($day=="Saturday")||($day=="Sunday")) {

            }
            else {
                $n++;
            }
        }

    }


    $strAutoNumber0="  ValPOS(); document.getElementById('fdforecasted_date').value='".CdateRev($date)."';";

    $objAxpDB->FormJS($strAutoNumber0);

}


$objAxpDB->WriteHtmlTable();

?>
