<?php
        function GetEstimasi($ftitem_code="") {

        $limit = 1;
        $page=$_REQUEST['page'];
        if(empty($page) || $page==0)
        {
                $offset = 0;
                $page = 1;
        }
        else
        {
                $offset = (($page * $limit) - $limit);
        }
        
        // Tabel 1
        $ssql="SELECT a.fnid, a.ftitem_code,a.ftitem_name , b.ftdepartment_code ,a.fnqty_bom_formula, a.fnqty_bom_estimasi,a.fnqty_estimasi, a.fnqty_spk,a.fnqty_produksi,a.ftuom_code
               FROM txestimation_poduksi_det a INNER JOIN tldepartment_ppic b ON a.ftdepartment_code = b.ftdepartment_code
               WHERE a.ftestimation_no= '{$ftestimation_no}'";

        $rs=mysql_query($ssql) or pr($ssql);

        $html.="<input type=hidden name='page' id='page' value='{$_REQUEST['page']}'>
                <br><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'>Kode Barang</th>
                    <td valign='top'>Department</th>
                    <td valign='top'>Qty Estimasi</th>
                    <td valign='top'>Qty SPK</th>
                    <td valign='top'>Qty Produksi</th>
                    <td valign='top'>Satuan</th>
                </tr>";

        $j=0;
        while($r = mysql_fetch_assoc($rs))
         {
            if ($i%2) {
                    $class1="ewTableRow";
                    $class2="ISGridTDeven";
            } else {
                    $class1="ewTableRow";
                    $class2="ISGridTDodd";
            }
            $j++;
            $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 readonly style='background-color: #E0E0E0'  type='text'  size='25'  name='ftitem_codeD[]' id='ftitem_codeD$i' value='" . $r['ftitem_code'] . "'  size=16>
                                <input type='hidden' name='ftitem_nameD[]' id='ftitem_nameD$i'value='{$r['ftitem_name']}'>{$r['ftitem_name']}
                        </td>
                     <td class='$class2'>
                                <input readonly style='background-color: #E0E0E0'  type='text'  size='25'  name='ftdepartment_codeD[]' id='ftdepartment_codeD$i' value='" . $r['ftdepartment_code'] . "'  size=16>
                        </td>
                        <td class='$class2'>
                            <input style='background-color: #E0E0E0' readonly type='hidden' onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: left;\"  name='fnqty_bom_formulaD[]' id='fnqty_bom_formulaD$i' value='" . $r['fnqty_bom_formula'] . "'   size=10>
                            <input style='background-color: #E0E0E0' readonly type='hidden' onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: left;\"  name='fnqty_bom_estimasiD[]' id='fnqty_bom_estimasiD$i' value='" . $r['fnqty_bom_estimasi'] . "'   size=10>
                            <input style='background-color: #E0E0E0' readonly type='text' onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: left;\"  name='fnqty_estimasiD[]' id='fnqty_estimasiD$i' value='" . $r['fnqty_estimasi'] . "'   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: left;\"  name='fnqty_spkD[]' id='fnqty_spkD$i' value='" . $r['fnqty_spk'] . "'   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: left;\"  name='fnqty_produksiD[]' id='fnqty_produksiD$i' value='" . $r['fnqty_produksi'] . "'   size=10></td>
                        <td class='$class2'>
                            <input style='background-color: #E0E0E0' name='ftuom_codeD[]' id='ftuom_code' value='" . $r['ftuom_code'] . "'   size=15>
                        </td>
                    </tr>";
             $i++;
         }
        $html.= "</table><br>";
        
        // Tabel 2
        $ssql="SELECT  fnid,ftitemdetail_code,ftitemdetail_name,fnqty_bom_formula,fnqty_bom_estimasi, fnqty_estimasi, fnqty_spk,fnqty_avail,fnqty_used,ftuom_code
	      FROM txestimation_det
              WHERE ftestimation_no='{$ftestimation_no}' and fnindex = $offset";

        $rs=mysql_query($ssql) or pr($ssql);
        $html_page=pageDataGridEtimasi();

        $html.="<input type=hidden name='page' id='page' value='{$_REQUEST['page']}'>
                {$html_page}<br><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'>Kode Barang</th>
                    <td valign='top'>Qty Estimasi</th>
                    <td valign='top'>Qty SPK</th>
                    <td valign='top'>Qty Avail</th>
                    <td valign='top'>Qty Used</th>
                    <td valign='top'>Satuan</th>
                </tr>";

        $j=0;
        while($r = mysql_fetch_assoc($rs))
         {
            if ($i%2) {
                    $class1="ewTableRow";
                    $class2="ISGridTDeven";
            } else {
                    $class1="ewTableRow";
                    $class2="ISGridTDodd";
            }
            $j++;
            $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 readonly style='background-color: #E0E0E0'  type='text'  size='25'  name='ftitemdetail_codeD[]' id='ftitemdetail_codeD$i' value='" . $r['ftitemdetail_code'] . "'  size=16>
                                <input type='hidden' name='ftitem_nameD[]' id='ftitem_nameD$i'value='{$r['ftitem_name']}'>{$r['ftitem_name']}
                        </td>
                        <td class='$class2'>

                            <input style='background-color: #E0E0E0' readonly type='hidden' onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: left;\"  name='fnqty_bom_formulaD[]' id='fnqty_bom_formulaD$i' value='" . $r['fnqty_bom_formula'] . "'   size=10>
                            <input style='background-color: #E0E0E0' readonly type='hidden' onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: left;\"  name='fnqty_bom_estimasiD[]' id='fnqty_bom_estimasiD$i' value='" . $r['fnqty_bom_estimasi'] . "'   size=10>
                            <input style='background-color: #E0E0E0' readonly type='text' onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: left;\"  name='fnqty_estimasiD[]' id='fnqty_estimasiD$i' value='" . $r['fnqty_estimasi'] . "'   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: left;\"  name='fnqty_spkD[]' id='fnqty_spkD$i' value='" . $r['fnqty_spk'] . "'   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: left;\"  name='fnqty_availD[]' id='fnqty_availD$i' value='" . $r['fnqty_avail'] . "'   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: left;\"  name='fnqty_usedD[]' id='fnqty_usedD$i' value='" . $r['fnqty_used'] . "'   size=10></td>
                        <td class='$class2'>
                            <input style='background-color: #E0E0E0' name='ftuom_codeD[]' id='ftuom_code' value='" . $r['ftuom_code'] . "'   size=15>
                        </td>
                    </tr>";
             $i++;
         }
        $html.= "</table>{$html_page}<br>";
        $html.= "<input id='TransactionDetailCount' name='TransactionDetailCount' value='$num_rows' type='hidden'>";

        return $html;
 }
        function insert_POR()

        {
            $_REQUEST['fdtrans_date']=CDate($_REQUEST['fdtrans_date']);
                            $strSQL['transaksiSQL'][]= "INSERT INTO txestimation_hdr
                                         (
                                            ftbranch_code,ftestimation_no,
                                            ftpor_no,fdtrans_date,fnstatus,
                                            ftitem_code,fnqty,
                                            ftcreated_user,fdcreated_date,ftmodified_user,fdmodified_date
                                         )
                                         VALUES
                                         (
                                             '{$_SESSION["strbranch"]}','{$_REQUEST['ftestimation_no']}',
                                             '{$_REQUEST['ftpor_no']}','{$_REQUEST['fdtrans_date']}','0',
                                             '{$_REQUEST['ftitem_coded']}','{$_REQUEST['fnqty']}',
                                             '{$_SESSION['username']}',now(),'{$_SESSION['username']}',now()
                                        )";
                            $ssql1="SELECT ff_create_estimasi('{$_SESSION["strbranch"]}','{$_REQUEST['ftestimation_no']}','{$_REQUEST['ftitem_coded']}','{$_REQUEST['fnqty']}','{$_SESSION['username']}')";
                                    $trans_date=RetRstField( $ssql1);
                            $ssq2="SELECT ff_create_estimasi_produksi('{$_SESSION["strbranch"]}','{$_REQUEST['ftestimation_no']}','{$_REQUEST['ftitem_coded']}','{$_REQUEST['fnqty']}')";
                                    $trans_date=RetRstField( $ssq2);
                            $ssql3="SELECT ff_fnindex_estimasi('{$_REQUEST['ftestimation_no']}')";
                                    $trans_date=RetRstField( $ssql3);

                    Query_SqlArr($strSQL);
        }

     if($_REQUEST['ajax']) {
            switch($_REQUEST['code'])
            {
            case "save_grid":
                $id=0;

                if(($_REQUEST['ftpor_no'])&&($_REQUEST['ftitem_coded']))
                {
                    $ssql="SELECT a.* FROM txproduction_order_request_det a
                                INNER JOIN tlbom_header b ON a.ftitem_code = b.ftitem_code
                           WHERE a.ftpor_no = '{$_REQUEST['ftpor_no']}' AND a.ftitem_code  = '{$_REQUEST['ftitem_coded']}'";
                    $barang=RetRstField($ssql);
                    if(($barang==''))
                    {
                         ?>
                            alert(' Kode Barang Tidak Ada Dalam Daftar Bom Atau Kode Barang Tidak Valid');
                            OnChangeDate_fdtrans_date();
                        <?php
                    }
                    else
                    {
                         insert_POR();
                         $ssql="SELECT fnid FROM txestimation_hdr
                                WHERE  ftestimation_no='{$_REQUEST['ftestimation_no']}'";
                         $id=RetRstField($ssql);
                    }
                }
                else
                {
                    ?>
                        alert(' No. POR Atau Kode Barang Tidak Valid ');
                        OnChangeDate_fdtrans_date();
                    <?php
                }
                echo " window.location='ppic.php?form=estimasi&mode=edit&id={$id}';";
                break;
            case "show_grid":
                $html=GetEstimasi($_REQUEST['ftitem_code']);
                ?>
                    div = document.getElementById('txtHint0');
                    var m='<?php echo js_esc($html); ?>';
                    m= m.replace(/^\s*|\s*$/g,"");
                    div.innerHTML =m;
                <?php
                break;
            }

            die("");
     }

     if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
              if(RetRstField("SELECT fnid FROM txestimation_hdr WHERE ftestimation_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("estimasi");

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


    $objAxpDB->ViewName("Daftar Estimasi");
    $objAxpDB->ViewSQL("SELECT A.fnid,A.ftestimation_no, a.ftpor_no,A.fdtrans_date,a.ftitem_code, c.ftitem_name, a.fnqty, B.fttrans_status_name
                        FROM txestimation_hdr A
                            INNER JOIN tstransaction_status B ON a.fnstatus=b.fntrans_status_code AND B.fntrans_group_code=2
                            INNER JOIN tlstock_hdr c ON a.ftitem_code = c.ftitem_code");
    $objAxpDB->ViewCaption("ID, No. Estimasi, No. POR, Tgl. transaksi, Kode Barang, Nama Barang, Qty , Status");
    $objAxpDB->ViewPeriod(TRUE);

    // Deklarasi Detail

    $objAxpDB->FormName("Estimasi");
    $objAxpDB->FormSQL("SELECT  ftestimation_no, ftitem_code,fnqty,fdtrans_date, ftnotes,fnstatus FROM txestimation_hdr");

    $objAxpDB->FormCaption(" No. Estimasi, Kode Barang, QTY Estimasi, Tgl. transaksi,Catatan , Status ");
    $objAxpDB->FormPosition("TL,TL,TL,TR,BL,BR");

    $Clear.="document.getElementById('fnqty').value='0';";
    $Clear.="$ajaxGrid";
    $objAxpDB->FormScript(1, "onChange=\"{$Clear}\"");

    $ajaxinvoice="ajax_do('ppic.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=show_grid&ftitem_code='+ document.getElementById('ftitem_code').value);";
    $objAxpDB->FormScript(2, "onChange=\"{$ajaxinvoice}\"");

    // $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM vBomDetail", "ftitem_code", "ftitem_name", -1,false);
    $objAxpDB->Lookup("ftitem_code", "SELECT a.ftitem_code, b.ftitem_name FROM tlbom_header a INNER JOIN tlstock_hdr b ON a.ftitem_code = b.ftitem_code WHERE a.fnstatus = 1 ", "ftitem_code", "ftitem_name",0,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",1,true);

    $objAxpDB->GridSQL(0, "SELECT  fnid,ftitemdetail_code,ftuom_code,  fnqty FROM txestimation_det");
    $objAxpDB->GridCaption(0, "No, Kode Barang, Satuan, Jumlah ");
    
    $objAxpDB->SetStatus("id, fnstatus");

    $ssql="SELECT ftestimation_no FROM txestimation_hdr
           WHERE fnid='{$_REQUEST['id']}' ";
    $ftestimation_no=RetRstField($ssql);
    $_REQUEST['ftestimation_no']=$ftestimation_no;

    // DRAFT
    if(($_REQUEST['id'])&&($fnstatus==0))
    {
        $html=GetEstimasi($ftestimation_no);
        $objAxpDB->GridReplace(0,$html);
    }
    // POSTED
    else if(($_REQUEST['id'])&&($fnstatus=1))
    {
        $html=GetEstimasi($ftestimation_no);
        $objAxpDB->GridReplace(0,$html);
    }
    // BATAL
    else if(($_REQUEST['id'])&&($fnstatus==9))
    {
        $html=GetEstimasi($ftestimation_no);
        $objAxpDB->GridReplace(0,$html);
    }
    else
    {
         $ajax1=" OnClick=\"ajax_do_grid0('ppic.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=save_grid&ftestimation_no='+ document.getElementById('ftestimation_no').value+'&fdtrans_date='+ document.getElementById('fdtrans_date').value+'&ftpor_no='+ document.getElementById('ftpor_no').value+'&ftitem_coded='+ document.getElementById('ftitem_coded').value+'&fnqty='+ document.getElementById('fnqty').value); \"";
         $button="&nbsp;&nbsp;<input type='button' id='cmd_generate' name='cmd_generate' value='Hitung Stock' {$ajax1}>";
         $objAxpDB->FormLayout(2,$button);
         $html=GetEstimasi("");
         $objAxpDB->GridReplace(0,$html);

         $where=" 0=1 ";

         $strAutoNumber1=" ajax_do('query2.php?code=2&ftmodule_code=ppic_02&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftestimation_no&no_trans=ftestimation_no&table_name=txestimation_hdr&ftbranch_code={$_SESSION['strbranch']}');";
         $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                       {$strAutoNumber1}
                                 }
                             OnChangeDate_fdtrans_date();
                             {$getGrid}
                            ";
        $objAxpDB->FormJS($strAutoNumber0);
        $objAxpDB->GridReplace(0,$html);
    }
    if ($objAxpDB->GetStatus("id") == "")
    {
            $objAxpDB->ButtonShow("baru, cetak_daftar,tutup");
    }
    elseif ($objAxpDB->GetStatus("id") == 0)
    {
            $objAxpDB->FormLocked("0,0,0,0,0, 1");
            $objAxpDB->ButtonShow("baru, tutup");
    }
    elseif ($objAxpDB->GetStatus("id")&&$objAxpDB->GetStatus("fnstatus") == 0)
    {
            $objAxpDB->FormLocked("1,1,1,1,1,1");
            $objAxpDB->ButtonShow("baru, batal, cetak, tutup");
    }
    elseif ($objAxpDB->GetStatus("fnstatus") == 1)
    {
            $objAxpDB->ButtonShow("cetak, batal, tutup");
            $objAxpDB->FormLocked("1,1,1,1,1,1");
    }
    elseif ($objAxpDB->GetStatus("fnstatus") == 9)
    {
            $objAxpDB->FormLocked("1,1,1,1,1,1");
            $objAxpDB->ButtonShow("tutup");
    }
    $objAxpDB->Process();
    $objAxpDB->WriteHtmlTable();
?>