<?php
     if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
              if(RetRstField("SELECT fnid FROM txstock_out_hdr_setengah_jadi WHERE ftstock_out_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
              }
              die("");
            break;
            case "gudang":
                $ssql="SELECT ftlocation_code,ftlocation_name FROM tllocation WHERE ftwarehouse_code='{$_REQUEST['ftwarehouse_code']}'";
                $rs=mysql_query($ssql);
                   ?>

                        var ObjSelect=document.getElementById('<?php echo $_REQUEST['ObjLokasi']; ?>');

                            var len = ObjSelect.length;

                            for (var idx=0; idx < len; idx++)
                            {
                                    if (ObjSelect.options[ObjSelect.length-1].value!='') {
                                          ObjSelect.remove(ObjSelect.length-1);
                                    }
                            }

                    <?php
                         $i=0;
                        while($r=mysql_fetch_assoc($rs)) {
                            $i++;
                              echo " try {
                                       ObjSelect.add(new Option('{$r['ftlocation_code']}--{$r['ftlocation_name']}','{$r['ftlocation_code']}'),null,null);
                                       }
                                      catch(ex) {
                                        ObjSelect.add(new Option('{$r['ftlocation_code']}--{$r['ftlocation_name']}','{$r['ftlocation_code']}'),$i,$i);

                                   } ";

                        }
                        ?>
                       document.getElementById('<?php echo $_REQUEST['objQty']; ?>').value='<?php echo number_format(0,2);?>';
                    <?php
                 die("");
            break;

            case "formula":
                $ssql="SELECT IFNULL(SUM(ffformula),1) as ffformula
                        FROM tlstock_multi_det_setengah_jadi
                        WHERE ftitem_code='{$_REQUEST['ftitem_code']}'
                        and ftuom_code_multi='{$_REQUEST['ftuom_code']}'
                        ";
                $ffformula=RetRstField($ssql);
                $ffQtyMulti=(double)$ffformula*(double)CCur($_REQUEST['fnqty']);
                ?>
                        document.getElementById('ffformulaD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ffformula; ?>';
                        document.getElementById('fnqty_baseD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ffQtyMulti; ?>';
                <?php

                die("");
            break;
        }

    }

    Getsecurity('inventorysj_barang_keluar');
    $objAxpDB->ViewName("Daftar Stock Keluar Setengah Jadi");
    //$objAxpDB->ViewTable("vIPStock_Keluar");
    $ssql="SELECT A.fnid, A.ftstock_out_no,  c.fttrans_status_name as ftcategory_code, d.ftsupplier_name as ftstock_out_company,
            A.fdtrans_date, A.ftdocument_no, A.fntotal_qty, B.fttrans_status_name
           FROM txstock_out_hdr_setengah_jadi A INNER JOIN tstransaction_status B ON a.fnstatus=b.fntrans_status_code AND B.fntrans_group_code=2
           inner join tstransaction_status c on A.ftcategory_code=c.fntrans_status_code AND c.fntrans_group_code=4
           left JOIN tlsupplier d on A.ftstock_out_company=d.ftsupplier_code
            ORDER BY A.ftstock_out_no DESC, A.fdtrans_date DESC";
    $objAxpDB->ViewSQL($ssql);

    $objAxpDB->ViewCaption("ID,No. Stock Keluar, Kategori, Supplier , Tgl. Masuk, No. Dokumen, Total Qty,  Status");
    $objAxpDB->ViewPeriod(TRUE);

    if(!$_REQUEST['id']) {
        $strAutoNumber1="ajax_do('query2.php?code=2&ftmodule_code=sjtn002&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftstock_out_no&no_trans=ftstock_out_no&table_name=txstock_out_hdr_setengah_jadi&ftbranch_code={$_SESSION['strbranch']}');";
        $strAutoNumber0="function OnChangeDate_fdtrans_date() {
                                       {$strAutoNumber1}

                         }
                         OnChangeDate_fdtrans_date();
                         ";
        $objAxpDB->jsPageOnLoad="OnChangeDate_fdtrans_date();
                                   ";
        $objAxpDB->FormJS($strAutoNumber0);

    }


    $objAxpDB->FormName("Stock Keluar Setengah Jadi");
    $objAxpDB->FormSQL("SELECT ftstock_out_no, ftcategory_code, fdtrans_date, ftpos_no, ftspb_no, ftdocument_no, ftstock_out_company,  ftnotes, fntotal_qty, fnstatus,ftstock_type_code FROM txstock_out_hdr_setengah_jadi");
    $objAxpDB->FormCaption("No. Stock Keluar, Kategori, Tgl. Keluar, No. POS, No. SPB, No. Dokumen,  Supplier, Catatan,  Total Qty, Status, Tipe Stock");
    $objAxpDB->FormPosition("TL, TL, TR, HD, HD, TR,TL, BL, BR, BR, TR");

    //$objAxpDB->FormScript(1, "onChange=\"document.getElementById('ftstock_out_company').value='';document.getElementById('hidden_ftstock_out_company').value='';\"");
    $objAxpDB->FormScript(1, "onChange=\"ajax_do('query2.php?code=15&value=' + this.value + '&ftstock_type_code=' + document.getElementById('ftstock_type_code').value);\"");

    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, fdexp_date, ftwarehouse_code, ftlocation_code, fnqty_available, fnqty, ftuom_code, fnqty_base, ffformula, '' as ftactive FROM txstock_out_det_setengah_jadi");
    $objAxpDB->GridCaption(0, "No., Kode Barang, Exp. Date, Gudang, Lokasi, Qty Avail, Qty, Satuan, Qty Base, Formula, Exp. Date Status");

    $ajaxOUM_Code="ajax_do('query2.php?code=uom_setengah_jadi&obj1=' + this.value + '&obj2=ftuom_codeD0_N_');";
    $ajaxExp_Date="ajax_do('query2.php?code=exp_datesj&obj1=' + this.value + '&obj2=ftactiveD0_N_');";
    $ajaxQtyAvail=" onChange=\"ajax_do('query2.php?code=qty_setengah_jadi&ftwarehouse_code=' + document.getElementById('ftwarehouse_codeD0_N_').value + '&ftlocation_code=' + document.getElementById('ftlocation_codeD0_N_').value+'&ftcategory_code=' + document.getElementById('ftcategory_code').value + '&ftitem_code=' + document.getElementById('ftitem_codeD0_N_').value+ '&fdexp_date=' + document.getElementById('fdexp_dateD0_N_').value+ '&objQty=fnqty_availableD0_N_');\"";
    $ajaxOUM_CodeAndQtyAvail=" onChange=\"{$ajaxOUM_Code}{$ajaxExp_Date};ajax_do('query2.php?code=qty_setengah_jadi&ftwarehouse_code=' + document.getElementById('ftwarehouse_codeD0_N_').value + '&ftlocation_code=' + document.getElementById('ftlocation_codeD0_N_').value+'&ftitem_code=' + document.getElementById('ftitem_codeD0_N_').value+ '&objQty=fnqty_availableD0_N_');\"";
    $ajaxGudang="OnChange=\"ajax_do('inventory_setengah_jadi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=gudang&ftwarehouse_code='+this.value+'&ObjLokasi=ftlocation_codeD0_N_&objQty=fnqty_availableD0_N_');\"";
    $ajaxFormula="ajax_do('inventory_setengah_jadi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=formula&index=_N_&index=_N_&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value+'&ftuom_code='+ document.getElementById('ftuom_codeD0_N_').value+'&fnqty='+ document.getElementById('fnqtyD0_N_').value);";

    $objAxpDB->GridScript(0,1, $ajaxOUM_CodeAndQtyAvail);
    $objAxpDB->GridScript(0,3, $ajaxGudang);
    $objAxpDB->GridScript(0,4, $ajaxQtyAvail);
    $objAxpDB->GridScript(0,6,"onChange=\"{$ajaxFormula}StockKeluarRecalQtyBahanBaku();\"");
    $objAxpDB->GridDelJS(0,"StockKeluarRecalQtyBahanBaku();");

    $objAxpDB->SetStatus("id, fnstatus,ftcategory_code");

    if($_REQUEST['id']=='0')  {
        $fntrans_group_code=" AND fntrans_status_code=1 ";
        $m="  WHERE fnstatus=1  ORDER BY ftitem_code ";
        $objAxpDB->Lookup("ftcategory_code", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 4 AND fnactive_status=1  AND fntrans_status_code <> 0 AND fntrans_status_code <> 4 ", "fntrans_status_code", "fttrans_status_name", -1,true);
    
    } else {
        $objAxpDB->Lookup("ftcategory_code", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 4 AND fnactive_status=1 ", "fntrans_status_code", "fttrans_status_name", -1,true);

    }

    //if(($objAxpDB->GetStatus("ftcategory_code")=='0') or ($objAxpDB->GetStatus("ftcategory_code")=='6') or ($objAxpDB->GetStatus("ftcategory_code")=='7')) {
        $objAxpDB->Lookup("ftstock_out_company", "SELECT ftsupplier_code, ftsupplier_name FROM tlsupplier", "ftsupplier_code", "ftsupplier_name", -1,false);
    //} else {
    //    $objAxpDB->Lookup("ftstock_out_company", "SELECT ftcustomer_code, ftcustomer_name FROM tlcustomer", "ftcustomer_code", "ftcustomer_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", 1,true);
    $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr_setengah_jadi  $m", "ftitem_code", "ftitem_name", -1,false);
    $objAxpDB->Lookup("ftwarehouse_code", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse where fnwarehouse_type = 0 or fnwarehouse_type = 2 ", "ftwarehouse_code", "ftwarehouse_name",-1,true,"' AND a.ftwarehouse_code=\''+ document.getElementById('ftwarehouse_code').value + '\''");
    $objAxpDB->Lookup("ftlocation_code", "SELECT ftlocation_code, ftlocation_name FROM tllocation", "ftlocation_code", "ftlocation_name", -1,true);
    $objAxpDB->Lookup("ftstock_type_code", "SELECT ftstock_type_code, ftstock_type_name FROM tlstock_type where ftstock_type_code='invsj' ", "ftstock_type_code", "ftstock_type_name", 0,true);

    $objAxpDB->SetStatus("id, fnstatus");

    $ftcategory_code=RetRstField("select ftcategory_code from txstock_out_hdr_setengah_jadi where fnid='{$_REQUEST['id']}'");

   if ($objAxpDB->GetStatus("id") == "")
            $objAxpDB->ButtonShow("baru, cetak_daftar, export_excel , tutup");

    elseif ($objAxpDB->GetStatus("id") == 0) {            
            $objAxpDB->GridAllowAddNew(0,true);
            $objAxpDB->GridAllowDelete(0,true);
            $objAxpDB->GridLocked(0,'1,0,0,0,0,1,0,1,1,1');
            $objAxpDB->ButtonShow("reset, simpan, tutup");
            $objAxpDB->FormLocked("0, 0, 0, 0,0, 0, 0, 0, 1, 1, 1");
        
        $objAxpDB->GridSQL(1, "SELECT REPLACE(fbaccess,1,'aktif') AS fbaccess  FROM tluser_access_setting #");
        $objAxpDB->GridCaption(1, "Access");
        $objAxpDB->GridAutoAdd(1, FALSE);
	
    }
    elseif ($objAxpDB->GetStatus("fnstatus") == 0) {
            $objAxpDB->GridAllowAddNew(0,true);
            $objAxpDB->GridAllowDelete(0,true);
            $objAxpDB->GridLocked(0,'1,0,0,0,0,1,0,1,1,1');
            $objAxpDB->ButtonShow("cetak, baru, update, batal, final, tutup");
            $objAxpDB->FormLocked("1, 0, 0, 0,0, 0, 0, 0, 1, 1, 1");
  
    } elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
            $objAxpDB->ButtonShow("cetak, baru, batal, tutup");
            $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
            $objAxpDB->GridLocked(0,'1,1,1,1,1,1,1,1,1,1');
    // Status approved tidak bisa batal karena ada sales invoice
    } elseif ($objAxpDB->GetStatus("fnstatus") == 2) {
            $objAxpDB->ButtonShow("cetak, tutup");
            $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
            $objAxpDB->GridLocked(0,'1,1,1,1,1,1,1,1,1,1');
    } elseif ($objAxpDB->GetStatus("fnstatus") == 9) {
            $objAxpDB->GridLocked(0,'1,1,1,1,1,1,1,1,1,1');
            $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
            $objAxpDB->ButtonShow("tutup");
    }

   $objAxpDB->GridHidden(0,'0,0,0,0,0,0,0,0,1,1,1');
   $objAxpDB->GridHidden(1,'1');

   if($_REQUEST['ajax']) {
           switch($_REQUEST['code']) {
           /*case "return":
               ob_start();
                          $ssql="SELECT b.fnindex,b.ftitem_code AS ftitem_code_change,b.ftitem_code AS ftitem_code,
                                    '' AS  ftwarehouse_code, '' AS ftlocation_code, b.fnqty-IFNULL(c.fnqty,0) as fnqty_sisa, 0.0 AS fnqty_available,  0.0 AS fnqty, '' AS ftuom_code
                                FROM txstock_in_hdr a INNER JOIN txstock_in_det b ON  a.ftstock_in_no=b.ftstock_in_no
                                    LEFT JOIN (SELECT a.ftdocument_no,b.ftitem_code,IFNULL(SUM(b.fnqty),0) AS fnqty
                                            FROM txstock_out_hdr_setengah_jadi a,txstock_out_det_setengah_jadi b
                                            WHERE a.ftstock_out_no=b.ftstock_out_no
                                                AND a.fnstatus=1 AND
                                                 a.ftdocument_no='{$_REQUEST['ftdocument_no']}' AND a.ftcategory_code='1'
                                            GROUP BY a.ftdocument_no,b.ftitem_code
                                        ) c
                                        ON a.ftstock_in_no=c.ftdocument_no AND b.ftitem_code=c.ftitem_code
                                WHERE
                                    a.ftstock_in_no='{$_REQUEST['ftdocument_no']}'  AND
                                    a.fnstatus=1 AND
                                    a.ftcategory_code='2' AND
                                    b.fnqty-IFNULL(c.fnqty,0)>0 #";

                          $objAxpDB->GridSQL(0,$ssql);
                          $objAxpDB->GridCaption(0, "No., Kode Barang ,Kode Barang Pengganti, Gudang, Lokasi,Qty Sisa, Qty Avail, Qty, Satuan");
                          $objAxpDB->GridLocked(0,'1,1,0,0,0,1,1,0,1');
                          $objAxpDB->GridScript(0,3, $ajaxGudang);
                            $objAxpDB->GridScript(0,2, $ajaxOUM_CodeAndQtyAvail);
                            #$objAxpDB->GridScript(0,2, $ajaxQtyAvail);
                            $objAxpDB->GridScript(0,4, $ajaxQtyAvail);
                            $objAxpDB->GridScript(0,7,"onChange='StockKeluarRecalQtyBahanBaku();'");
                            $objAxpDB->GridDelJS(0,"StockKeluarRecalQty();");
                              $objAxpDB->GridAllowAddNew(0,FALSE);
                            $objAxpDB->GridAllowDelete(0,true);

               $objAxpDB->Process();
               $objAxpDB->setGrid2(0);
               $html = ob_get_contents();
               ob_end_clean();
               ?>
                    div = document.getElementById('txtHint0');
                    var m='<?php echo js_esc($html); ?>';
                    m= m.replace(/^\s*|\s*$/g,"");


                    div.innerHTML =m;

                    var i=document.getElementsByName('fnindexD0[]').length;





                    RowBuild0();
                     document.getElementById("TransactionDetail0").deleteRow(i+1);
                    reSize();
               <?

               break; */
               
               case "outsource":
                ob_start();

                $ssql="SELECT b.fnindex,b.ftitem_code AS ftitem_code,'' as fdexp_date,
                        '' AS  ftwarehouse_code, '' AS ftlocation_code,CAST(0 AS DECIMAL(19,4)) AS fnqty_available, CAST(0 AS DECIMAL(19,4)) AS fnqty, b.ftuom_code AS ftuom_code,
                        CAST(0 AS DECIMAL(19,4)) AS fnqty_base, 1 AS ffformula, '' as ftactive
                        FROM txpurchase_order_hdr a INNER JOIN txpurchase_order_det b ON  a.ftpo_no=b.ftpo_no
                        WHERE a.ftpo_no='{$_REQUEST['ftdocument_no']}'  AND
                            a.fnstatus=1 AND a.fnreceipt_status <> 3 and fnhdr_category=1 # ";

                $objAxpDB->GridSQL(0,$ssql);
                $objAxpDB->GridCaption(0, "No., Kode Barang, Exp. Date, Gudang, Lokasi, Qty Avail, Qty, Satuan, Qty Base, Formula, Exp. Date Status");
                $objAxpDB->GridLocked(0,'1,1,0,0,0,1,0,1,1,1');
                $objAxpDB->GridScript(0,1, $ajaxOUM_CodeAndQtyAvail);
                $objAxpDB->GridScript(0,3, $ajaxGudang);
                $objAxpDB->GridScript(0,4, $ajaxQtyAvail);
                $objAxpDB->GridScript(0,6,"onChange=\"{$ajaxFormula}StockKeluarRecalQty();\"");
                $objAxpDB->GridDelJS(0,"StockKeluarRecalQty();");
                $objAxpDB->LookupGrid(0, "ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr", "ftitem_code", "ftitem_name", -1,false);
                //$objAxpDB->GridAllowAddNew(0,FALSE);
                $objAxpDB->GridAllowDelete(0,true);

                $objAxpDB->Process();
                $objAxpDB->setGrid2(0);
                $html = ob_get_contents();
                ob_end_clean();
                ?>
                    div = document.getElementById('txtHint0');
                    var m='<?php echo js_esc($html); ?>';
                    m= m.replace(/^\s*|\s*$/g,"");


                    div.innerHTML =m;

                    var i=document.getElementsByName('fnindexD0[]').length;

                    RowIndex0();
                    reSize();
               <?php

               break;
           }
           die("");

    }
    $objAxpDB->Process();

    $objAxpDB->WriteHtmlTable();