<?php
    if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
              if(RetRstField("SELECT fnid FROM txproduction_monitoring_por_hdr WHERE ftmpor_no='{$_REQUEST['fttrans_no']}'")) {
                  ?>

                  alert('No. transaksi sudah dipakai...\n System akan mengenerate No. transaksi kembali ...\n Silahkan ulangi proses penyimpanan...  ');
                  OnChangeDate_fdtrans_date();
            <?php
              }
              else {
                    ?>


                  submitbutton('Simpan','','','','');

            <?php
              }
            break;

        }
        die("");
    }

     Getsecurity(31);

     $fnstatus=0;
    if($_REQUEST['id']) {
        $ssql="SELECT fnstatus FROM txproduction_monitoring_por_hdr WHERE fnid='{$_REQUEST['id']}'";
        $rs=mysql_query($ssql);
        $r=mysql_fetch_assoc($rs);
        $fnstatus=$r['fnstatus'];
    }

     function GetPOR($fnstatus,$Where1,$Where2="1=1") {
            $html="";
         if(isset($_REQUEST['id'])) {
         if($_REQUEST['id']=="0") {

                           $ssql1="select distinct fdtrans_date from txproduction_outstanding_weekly_hdr  where  fnstatus='1'";
                           $trans_date=RetRstField( $ssql1);
                            $ssql="SELECT  a0.ftstock_group_code,a0.ftstock_subgroup_code,a0.ftdecal_code,a0.ftcolor_code ,
                                             a0.ftstock_group_name, a0.ftstock_subgroup_name, a0.ftdecal_name,a0.ftcolor_name ,
                                                        A2.fnqty_on_hand AS fnqty_available,0  as fnqty_painting,0 as fnqty_neto ,
                                                       A2.fnqty_on_hand AS fnqty_total_por,sum(b.ffqty_luarkota) AS fnqty_rekap,
                                                        sum(b.ffqty_conntainer) as fnqty_container ,sum(b.ffqty_jakarta) as fnqty_jakarta,
                                                        sum( b.ffqty) as fnqty_total_order,
                                                         A2.fnqty_on_hand+0+0-sum(b.ffqty) AS fnqty_result_temp,
                                                        0 as fnminim_stock,
                                                        A2.fnqty_on_hand+0+0-sum(b.ffqty) AS fnqty_result
                                            FROM

                                        tlstock_hdr a0 left join
						(select b.* from txproduction_outstanding_weekly_det  b,txproduction_outstanding_weekly_hdr a
							where a.ftpow_no=b.ftpow_no and a.fnstatus=1
                                               ) b on
                                        b.ftitem_code=a0.ftitem_code


                                            inner JOIN (
                    select  left(l.ftitem_code,12) as fttype,
                        ifnull(sum(m.fnmasuk),0)-ifnull(sum(m.fnkeluar),0) as fnqty_on_hand
                            from tlstock_hdr l
                            left join (
                            select b.ftitem_code,sum(fnqty) as fnmasuk,0 as fnkeluar
                            from txinnitial_stock_hdr a,txinnitial_stock_det b
                            where a.ftstock_in_no=b.ftstock_in_no
                                and a.fnstatus<>9
                            group by b.ftitem_code
                            union all

                            select b.ftitem_code,sum(fnqty) as fnmasuk,0 as fnkeluar
                            from txstock_in_hdr a,txstock_in_det b
                            where a.ftstock_in_no=b.ftstock_in_no
                                and a.fnstatus<>9 and
                                a.fdtrans_date<'$trans_date'
                            group by b.ftitem_code

                            union all
                            select b.ftitem_code,0 as fnmasuk,sum(fnqty)  as fnkeluar
                            from txstock_out_hdr a,txstock_out_det b
                            where a.ftstock_out_no=b.ftstock_out_no
                                and a.fnstatus<>9 and
                                a.fdtrans_date<'$trans_date'
                            group by b.ftitem_code

                            union all
                            select b.ftitem_code,0 as fnmasuk,sum(fnqty) as fnkeluar
                            from txstock_mutation_hdr a,txstock_mutation_det b
                            where a.ftstock_mutation_no=b.ftstock_mutation_no
                                and a.fnstatus<>9 and
                                a.fdtrans_date<'$trans_date'
                            group by  b.ftitem_code

                           union all
                            select b.ftitem_code,
                                case when sum(fnqty_opname)>sum(fnqty_available) then sum(fnqty_opname)-sum(fnqty_available) else 0 end as fnmasuk,
                                case when sum(fnqty_opname)<sum(fnqty_available) then sum(fnqty_available)-sum(fnqty_opname) else 0 end as fnkeluar
                            from txstock_adjustment_hdr a,txstock_adjustment_det b
                            where a.ftstock_adjustment_no=b.ftstock_adjustment_no and a.fnstatus=2 and
                                a.fdtrans_date<'$trans_date'
                            group by  b.ftitem_code

                            union all
                            select b.ftitem_code,sum(fnqty)  as fnmasuk,0 as fnkeluar
                            from txstock_mutation_hdr a,txstock_mutation_det b
                            where a.ftstock_mutation_no=b.ftstock_mutation_no
                                and a.fnstatus<>9 and
                                a.fdtrans_date<'$trans_date'
                            group by b.ftitem_code

                          )
                            m
                            on l.ftitem_code=m.ftitem_code
                            group by left(l.ftitem_code,12)
                    )

                     a2 on left(a0.ftitem_code,12)=a2.fttype
                                        where a0.ftstock_category_code='inv'  and a0.fnstatus=1 
                                            GROUP BY  a0.ftstock_group_code,a0.ftstock_subgroup_code,a0.ftdecal_code,a0.ftcolor_code
                   , a0.ftstock_group_name, a0.ftstock_subgroup_name, a0.ftdecal_name,a0.ftcolor_name";
               #pr($ssql);die("suhut");
        }
        else {

             $ssql="SELECT b.fnid, b.ftstock_group_code,b.ftstock_subgroup_code,b.ftdecal_code,b.ftcolor_code ,
                            b.ftstock_group_name,b.ftstock_subgroup_name,b.ftdecal_name,b.ftcolor_name ,
                            b.fnqty_available AS fnqty_available,b.fnqty_painting  as fnqty_painting,b.fnqty_neto as fnqty_neto ,
                            b.fnqty_total_por AS fnqty_total_por,b.fnqty_rekap AS fnqty_rekap,
                            b.fnqty_container as fnqty_container ,b.fnqty_jakarta as fnqty_jakarta,
                           b.fnqty_total_order as fnqty_total_order,
                           b.fnqty_result_temp AS fnqty_result_temp,
                           b.fnminim_stock,
                           b.fnqty_result AS fnqty_result
            FROM
            txproduction_monitoring_por_hdr a
            INNER JOIN txproduction_monitoring_por_det b ON  a.ftmpor_no=b.ftmpor_no

            WHERE    $Where1
            ORDER BY b.ftstock_group_name,b.ftstock_subgroup_name,b.ftdecal_name,b.ftcolor_name


            "; 
        }
         }

//	pr($ssql);die("");
        $rs=mysql_query($ssql) or pr($ssql);

        $html.="<table class='ewTable' id='TransactionDetail' name='TransactionDetail' border='0' width='100%' cellspacing='0' celpadding='0'>
                <tr class='ewTableHeader'>
                <td valign='top'>No</th>
                <td valign='top'>Type Barang</th>
                <td valign='top'>Warna</th>
                <td valign='top'>Stock Gudang </th>
                <td valign='top'>SPK Painting</th>
                <td valign='top'>Barang 1/2Jadi</th>
                <td valign='top'>Total</th>
                <td valign='top'>Rekap Mingguan Luar Kota</th>
                <td valign='top'>OST Container</th>
                <td valign='top'>OST Jakarta</th>
                <td valign='top'>Total Order</th>
                <td valign='top'>Hasil Akhir(1)</th>
                <td valign='top'>Safety Stock</th>
                <td valign='top'>Hasil Akhir(2)</th>
                </tr>";

        $i=0;
        if($fnstatus==0) {
            $m=" ";
        }
        else {
             $m=" readonly ";
        }
        while($r = mysql_fetch_assoc($rs))
         {
            if ($i%2) {
                    $class1="ewTableRow";
                    $class2="ISGridTDeven";
            } else {
                    $class1="ewTableRow";
                    $class2="ISGridTDodd";
            }
            $j=$i+1;
            $html.= "<tr class='$class1'>
                    <td class='$class2'><input readonly style='background-color: #E0E0E0' type='text' name='fnindexD[]' id='fnindexD$i' value='" . $j . "' size=3>{$r['flag']}</td>
					<td class='$class2'><input type='hidden' name='ftstock_group_codeD[]' id='ftstock_group_codeD$i' value='{$r['ftstock_group_code']}'>
										<input type='hidden' name='ftstock_subgroup_codeD[]' id='ftstock_subgroup_codeD$i'  value='{$r['ftstock_subgroup_code']}'>
										<input type='hidden' name='ftdecal_codeD[]' id='ftdecal_codeD$i' value='{$r['ftdecal_code']}'>
                                        <input type='hidden' name='ftstock_group_nameD[]' id='ftstock_group_nameD$i' value='{$r['ftstock_group_name']}'>
										<input type='hidden' name='ftstock_subgroup_nameD[]' id='ftstock_subgroup_nameD$i'  value='{$r['ftstock_subgroup_name']}'>
										<input type='hidden' name='ftdecal_nameD[]' id='ftdecal_nameD$i' value='{$r['ftdecal_name']}'>

										{$r['ftstock_group_name']} {$r['ftstock_subgroup_name']} {$r['ftdecal_name']} </td>
					<td class='$class2'><input type='hidden' name='ftcolor_codeD[]' id='ftcolor_codeD$i' value='{$r['ftcolor_code']}'>
                                        <input type='hidden' name='ftcolor_nameD[]' id='ftcolor_nameD$i' value='{$r['ftcolor_name']}'>
									{$r['ftcolor_name']}</td>
                    <td class='$class2'><input style='background-color: #E0E0E0' readonly onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: right;\" type='text' name='fnqty_availableD[]' id='fnqty_availableD$i' value='" . $r['fnqty_available'] . "'   size=10></td>
                    <td class='$class2'><input $m onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: right;\"  type='text' name='fnqty_paintingD[]'  onChange=\"MonitoringPORRecalQty('$i');\" id='fnqty_paintingD$i' value='" . $r['fnqty_painting'] . "'   size=10></td>
                   <td class='$class2'><input  $m onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: right;\"  type='text' name='fnqty_netoD[]'   onChange=\"MonitoringPORRecalQty('$i');\"  id='fnqty_netoD$i' value='" . $r['fnqty_neto'] . "'   size=10></td>
                   <td class='$class2'><input style='background-color: #E0E0E0' readonly type='text' onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: right;\"  name='fnqty_total_porD[]' id='fnqty_total_porD$i' value='" . $r['fnqty_total_por'] . "'   size=10></td>
                    <td class='$class2'><input style='background-color: #E0E0E0' readonly type='text' onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: right;\"  name='fnqty_rekapD[]'  onChange=\"MonitoringPORRecalQty('$i');\"  id='fnqty_rekapD$i' value='" . $r['fnqty_rekap'] . "'   size=10></td>
                    <td class='$class2'><input style='background-color: #E0E0E0' readonly type='text' onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: right;\"  name='fnqty_containerD[]'  onChange=\"MonitoringPORRecalQty('$i');\"  id='fnqty_containerD$i' value='" . $r['fnqty_container'] . "'   size=10></td>
                    <td class='$class2'><input style='background-color: #E0E0E0' readonly type='text' onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: right;\"  name='fnqty_jakartaD[]'  onChange=\"MonitoringPORRecalQty('$i');\"  id='fnqty_jakartaD$i' value='" . $r['fnqty_jakarta'] . "'   size=10></td>
                    <td class='$class2'><input style='background-color: #E0E0E0' readonly type='text' onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: right;\"  name='fnqty_total_orderD[]' id='fnqty_total_orderD$i' value='" . $r['fnqty_total_order'] . "'   size=10></td>
                     <td class='$class2'><input style='background-color: #E0E0E0' readonly type='text' onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: right;\"  name='fnqty_result_tempD[]' id='fnqty_result_tempD$i' value='" . $r['fnqty_result_temp'] . "'   size=10></td>
                    <td class='$class2'><input $m    onChange=\"MonitoringPORRecalQty('$i');\"   type='text' onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: right;\"  name='fnminim_stockD[]' id='fnminim_stockD$i' value='" . $r['fnminim_stock'] . "'   size=10></td>
                    <td class='$class2'><input style='background-color: #E0E0E0' readonly type='text' onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: right;\"  name='fnqty_resultD[]' id='fnqty_resultD$i' value='" . $r['fnqty_result'] . "'   size=10></td>
                    </tr>";

             $i++;
         }
        $html.= "</table>";
        $html.= "<input id='TransactionDetailCount' name='TransactionDetailCount' value='$num_rows' type='hidden'>";

        return $html;
 }


$objAxpDB->ViewName("Daftar Monitoring POR");
$objAxpDB->ViewSQL("SELECT A.fnid,A.ftmpor_no,A.fdtrans_date, sum(c.fnqty_available) as fnqty_available, B.fttrans_status_name FROM txproduction_monitoring_por_hdr
                          A INNER JOIN tstransaction_status B ON a.fnstatus=b.fntrans_status_code AND B.fntrans_group_code=2
                            INNER JOIN txproduction_monitoring_por_det c ON A.ftmpor_no=c.ftmpor_no
                        group by A.fnid,A.ftmpor_no,A.fdtrans_date, B.fttrans_status_name
                          ORDER BY A.ftmpor_no DESC,A.fdtrans_date DESC");

$objAxpDB->ViewCaption("ID, No. Monitoring POR, Tgl. transaksi, Qty Akhir, Status");
$objAxpDB->ViewPeriod(TRUE);
$objAxpDB->FormName("Monitoring POR");
$objAxpDB->FormSQL("SELECT ftmpor_no,fdtrans_date,fnstatus FROM txproduction_monitoring_por_hdr");

$objAxpDB->FormCaption("No. Monitoring POR, Tgl. transaksi, Status ");
$objAxpDB->FormPosition("TL, TL, BR");




// DRAFF
if(($_REQUEST['id'])&&($fnstatus==0)) {
    $where="A.fnid='".$_REQUEST['id']."'";
    $html=GetPOR($fnstatus,$where);
    $objAxpDB->GridReplace(0,$html);

}
// POSTED
else if(($_REQUEST['id'])&&($fnstatus=1)) {
    $where="A.fnid='".$_REQUEST['id']."'";
    $html=GetPOR($fnstatus,$where,"0=1");
   $objAxpDB->GridReplace(0,$html);

}
// BATAL
else if(($_REQUEST['id'])&&($fnstatus==9)) {
    $where="A.fnid='".$_REQUEST['id']."'";
    $where3="BB.fnid='".$_REQUEST['id']."'";
    $html=GetPOR($fnstatus,$where,'1=0');
    $objAxpDB->GridReplace(0,$html);
}
else {

    $where=" 0=1 ";

	if(isset($_REQUEST['id']))
		 $html=GetPOR(0,$where);
	else
		 $html="";

    $objAxpDB->GridReplace(0,$html);

     $strAutoNumber1=" ajax_do('query2.php?code=2&ftmodule_code=TN102&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftmpor_no&no_trans=ftmpor_no&table_name=txproduction_monitoring_por_hdr');";
     $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                   {$strAutoNumber1}
                             }
                         OnChangeDate_fdtrans_date();
                         {$getGrid}
                        ";
    $objAxpDB->FormJS($strAutoNumber0);
    $objAxpDB->GridReplace(0,$html);

}


$objAxpDB->GridSQL(0, "SELECT fnid FROM txproduction_monitoring_por_hdr");
$objAxpDB->GridCaption(0, "No. POW, Type Barang, Kode Barang, Nama Barang, Warna, Qty Avail, SPK Painting, Barang 1/2 Jadi, Total, Rekap Mingguan Luar Kota, OST Container, OST Jakarta, Total Order, Hasil Akhir ");
$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("baru,  cetak_daftar,tutup");
    }
    elseif ($objAxpDB->GetStatus("id") == 0) {
            $objAxpDB->FormLocked("0,  0, 1");
            $objAxpDB->ButtonShow("reset, simpan, tutup");
    }
    elseif ($objAxpDB->GetStatus("id")&&$objAxpDB->GetStatus("fnstatus") == 0) {
          $objAxpDB->FormLocked("1, 0, 1");
          $objAxpDB->ButtonShow("cetak,update,final, batal, tutup");
    }
    elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
              $objAxpDB->ButtonShow("cetak, batal, tutup");
             $objAxpDB->FormLocked("1,  1, 1");
    } elseif ($objAxpDB->GetStatus("fnstatus") == 9) {
            $objAxpDB->FormLocked("1,  1, 1");
            $objAxpDB->ButtonShow("tutup");
    }
    $objAxpDB->Process();

    $objAxpDB->WriteHtmlTable();


     ?>