<?php
    Getsecurity('bom');

    if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
          
            case "transno":
              if(RetRstField("SELECT fnid FROM tlbom_header WHERE ftitem_code='{$_REQUEST['ftitem_code']}'")) {
                  
				  ?>

                  alert('Kode barang sudah dipakai... ');
            <?php
              }
              else {
                    ?>

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

            <?php
              }
            break;
              case "item":
                  $ssql="SELECT ftuom_code FROM vBomTypeDetail WHERE ftitem_code='{$_REQUEST['ftitemdetail_code']}' ";
				  $ftuom_code=RetRstField($ssql); 
                  ?> 
					  document.getElementById('ftoumdetail_codeD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ftuom_code ; ?>';
   				  <?php
				die("");
			break;
              case "itemtype":
                  //$objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name,ftuom_code,ftitem_type FROM vBomTypeHeader ", "ftitem_name", "ftitem_code",-1,false,"' WHERE ftitem_type =\''+ {$_REQUEST['ftbom_type']} + '\''");
                  //$objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name,ftuom_code,ftitem_type FROM vBomTypeHeader ", "ftitem_name", "ftitem_code",-1,false);
                  die("");
                  break;
              case "uom":
                  $ssql="SELECT UPPER(ftuom_code) as ftuom_code FROM vBomTypeHeader WHERE ftitem_code='{$_REQUEST['ftitem_code']}' ";
                  $uom_code=RetRstField($ssql);
                  ?>
                      document.getElementById('ftuom_code').value='<?php echo ($uom_code); ?>';
                  <?php
                 
                  die("");
            break;
        }
        die("");
    }

    $objAxpDB->LookupMagicQuotes(True);
    $objAxpDB->ViewName("Daftar BOM");
    $objAxpDB->ViewSQL("SELECT a.fnid, a.ftitem_code, b.ftitem_name, a.ftuom_code,c.ftdepartment_name,
                              CASE WHEN fnstatus=0 THEN 'tidak aktif'
                              ELSE 'aktif'
                              END AS STATUS
                              FROM tlbom_header a
                              INNER JOIN vBomTypeHeader b ON a.ftitem_code=b.ftitem_code
                              INNER JOIN tldepartment_ppic c ON a.ftdepartment_code=c.ftdepartment_code
                              ORDER BY c.ftdepartment_name");
    $objAxpDB->ViewCaption("ID, Kode BOM, Nama Bom, Satuan, Status");
    $objAxpDB->FormName("BOM");
    $objAxpDB->FormSQL("select ftbom_type,ftitem_code,ftitem_name,ftuom_code,ftdepartment_code,ftdescription,ffnetto,ffbruto,fnstatus
                        from tlbom_header");
    $objAxpDB->FormCaption("Type Bom, Kode Barang,Nama Barang, Satuan, Department,Keterangan,Berat Bersih, Berat Kotor, Status");
    $objAxpDB->FormPosition("TL,TL,HD,TL,TR, BL, BR,BR,BR,HD");
    $objAxpDB->FormLayout(3,"&nbsp<input type='hidden' id='ftuom_code_grid0' name='ftuom_code_grid0'> <input type='hidden' id='fnindex_grid0' name='fnindex_grid0'>");

    // Deklarasi Detail
    $objAxpDB->GridSQL(0, "SELECT fnindex,ftitem_type, ftitemdetail_code,fnqty,ftoumdetail_code ,ftuomdetail_multi_code,ffqty_2 FROM tlbom_detail");
    $objAxpDB->GridCaption(0,"No.,Type Barang, Kode Barang, Qty, Satuan, Satuan Multi,Calc" );
    $ajax1="&no='+ document.getElementById('fnindexD0_N_').value +'&ftuomdetail_code='+ document.getElementById('ftoumdetail_codeD0_N_').value +'&fnqty='+ document.getElementById('fnqtyD0_N_').value";
    $ajax1="document.getElementById('ftuom_code_grid0').value=document.getElementById('ftoumdetail_codeD0_N_').value;document.getElementById('fnindex_grid0').value='_N_';window.open('?form=calc&mode=edit&id=0{$ajax1},'mywindow','width=540,height=200,resizable=no,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no,screenX=100,screenY=2')";
    $ajax1=" OnClick=\"{$ajax1}\"";
    $objAxpDB->GridLayout(0,6,"<input type='button' id='cmd_grid_list' name='cmd_grid_list' value='Calc' {$ajax1}>");
    $objAxpDB->GridSize(0, " , , , , , ,1");

    $objAxpDB->Lookup('ftbom_type', "SELECT 0 AS A,'Setengah Jadi' AS B UNION ALL SELECT 1 AS A,'Barang Jadi' AS B", "A", "B", 0, true);
    $objAxpDB->Lookup('ftitem_type', "SELECT 0 AS A,'Bahan Baku' AS B UNION ALL SELECT 1 AS A,'Setengah Jadi' AS B", "A", "B", -1, true);
    //$objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name,ftuom_code,ftitem_type FROM vBomTypeHeader ", "ftitem_name", "ftitem_code",-1,false,"' WHERE ftitem_type =\''+ document.getElementById('ftbom_type').value + '\''");
    $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name,ftuom_code,ftitem_type FROM vBomTypeHeader WHERE 1=1 ", "ftitem_code", "ftitem_name",-1,false, "' AND ftitem_type=\''+ document.getElementById('ftbom_type').value + '\''");
    //$objAxpDB->Lookup("ftitemdetail_code", "SELECT ftitem_code, ftitem_name,ftuom_code,ftitem_type FROM vBomTypeDetail ", "ftitem_name", "ftitem_code",-1,false,"' AND ftitem_type =\''+ document.getElementById('ftitem_typeD0_N_').value + '\''");
    $objAxpDB->Lookup("ftitemdetail_code", "SELECT ftitem_code, ftitem_name,ftuom_code,ftitem_type FROM vBomTypeDetail WHERE 1=1 ", "ftitem_code", "ftitem_name",-1,false, "' AND ftitem_type=\''+ document.getElementById('ftitem_typeD0_N_').value + '\''");

    $objAxpDB->Lookup('fnstatus', 'SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status where fntrans_status_code<>2 and fntrans_group_code=9', 'fntrans_status_code', 'fttrans_status_name', 1,true);
    $objAxpDB->Lookup('ftuomdetail_multi_code', 'SELECT ftuom_code, ftuom_name FROM vi_uom_code_multi', 'ftuom_code', 'ftuom_name', -1,true);
    $objAxpDB->Lookup("ftdepartment_code", "SELECT ftdepartment_code, ftdepartment_name FROM tldepartment_ppic", "ftdepartment_code", "ftdepartment_name", -1, True);

    //$ajaxGudang="OnChange=\"ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=gudang&ftwarehouse_code='+this.value+'&ObjLokasi=ftlocation_codeD0_N_');\"";
	
    //$objAxpDB->FormScript(0," onChange=\"{$ajaxItem}\"");
    $x="document.getElementById('ftitem_code').value='';";
    $x.="document.getElementById('ftuom_code').value='';";
    $x.="ajax_do('ppic.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=itemtype&ftbom_type='+ this.value);";
    $objAxpDB->FormScript(0," onChange=\"{$x}\"");
    $ajaxItem="ajax_do('ppic.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=uom&ftitem_code='+ this.value)";
    $objAxpDB->FormScript(1," onChange=\"{$ajaxItem}\"");

    $xD="document.getElementById('ftitemdetail_codeD0_N_').value='';";
    $xD.="document.getElementById('ftoumdetail_codeD0_N_').value='';";
    $objAxpDB->GridScript(0,1," onChange=\"{$xD}\"");
	
    $ajaxItemDetail="ajax_do('ppic.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=item&ftitemdetail_code=' + this.value + '&index=_N_');";
    $objAxpDB->GridScript(0,2," onChange=\"{$ajaxItemDetail}\"");

    $objAxpDB->SetStatus("id, fnStatus");
    if ($objAxpDB->GetStatus("id") == "")
            $objAxpDB->ButtonShow("baru, cetak_daftar, export_excel , tutup");
    elseif ($objAxpDB->GetStatus("id") == 0) {
            $objAxpDB->FormLocked("0, 0, 0, 1, 0,0,0,0,0 ");
            $objAxpDB->ButtonShow("reset, simpan, tutup");
            $objAxpDB->GridLocked(0, "1, 0, 0,0, 1,0");
            $objAxpDB->GridAllowAddNew(0, TRUE);
            $objAxpDB->GridAllowDelete(0, TRUE);
            $objAxpDB->GridHidden(0, "0, 0, 0,0, 0,1");

    } elseif ($objAxpDB->GetStatus("id") != 0) {
            $objAxpDB->FormLocked("1, 1, 1,1, 1,0,0,0,0 ");
            $objAxpDB->ButtonShow("baru, update, cetak, hapus, tutup");
            $objAxpDB->GridLocked(0, "1, 0,0, 0,1");
            $objAxpDB->GridHidden(0, "0, 0, 0,0, 0,1");
                $objAxpDB->GridAllowAddNew(0, TRUE);
            $objAxpDB->GridAllowDelete(0, TRUE);
    }
    $objAxpDB->Process();
    $objAxpDB->WriteHtmlTable();