<?php
if($_REQUEST['ajax']) {
    switch($_REQUEST['code']) {

      case "transno":
      if(RetRstField("SELECT fnid FROM txstock_mutation_hdr WHERE ftstock_mutation_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;

    case "gudang":
        $ssql="SELECT a.ftlocation_code,a.ftlocation_name FROM tllocation a
                inner join tlwarehouse b on a.ftwarehouse_code = b.ftwarehouse_code
                WHERE a.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);

                           } ";
                }
          if($_REQUEST['t']==1) {
                ?>
               document.getElementById('<?php echo $_REQUEST['objQty']; ?>').value=0;
            <?php
          }

    break;

    case "formula":
        $ssql="SELECT IFNULL(SUM(ffformula),1) as ffformula
                FROM tlstock_multi_det
                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;

    case "lokasi_from":
        $ssql="SELECT ftwarehouse_code FROM tllocation
                WHERE ftlocation_code='{$_REQUEST['ftlocation_from']}'
                ";
        $ftwarehouse_code=RetRstField($ssql);

        ?>
        document.getElementById('ftwarehouse_fromD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ftwarehouse_code; ?>';

        ajax_do('query2.php?code=qty&ftwarehouse_code=' + document.getElementById('ftwarehouse_fromD0<?php echo $_REQUEST['index']; ?>').value + '&ftlocation_code=' + document.getElementById('ftlocation_fromD0<?php echo $_REQUEST['index']; ?>').value+'&ftitem_code=' + document.getElementById('ftitem_codeD0<?php echo $_REQUEST['index']; ?>').value+ '&fdexp_date=' + document.getElementById('fdexp_dateD0<?php echo $_REQUEST['index']; ?>').value+ '&objQty=fnqty_availableD0<?php echo $_REQUEST['index']; ?>');
        <?php

        die("");
        break;

    case "lokasi_to":
        $ssql="SELECT ftwarehouse_code FROM tllocation
                WHERE ftlocation_code='{$_REQUEST['ftlocation_to']}'
                ";
        $ftwarehouse_code=RetRstField($ssql);

        ?>
        document.getElementById('ftwarehouse_toD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ftwarehouse_code; ?>';
        <?php

        die("");
        break;

    case "lokasi_revisi":
        $ssql="SELECT ftwarehouse_code FROM tllocation
                WHERE ftlocation_code='{$_REQUEST['ftlocation_from']}'
                ";
        $ftwarehouse_code=RetRstField($ssql);

        $ssql="SELECT ifnull(fnqty,0) as fnqty FROM txstock_mutation_det
                WHERE ftstock_mutation_no='{$_REQUEST['ftstock_mutation_no']}'  AND ftitem_code='{$_REQUEST['ftitem_code']}' AND ftlocation_from='{$_REQUEST['ftlocation_from']}'
                ";
        $fnqty=RetRstField($ssql);
        if ($fnqty=='') { $fnqty = 0; }

        ?>
        document.getElementById('ftwarehouse_fromD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ftwarehouse_code; ?>';
        ajax_do('query2.php?code=qty&ftwarehouse_code=<?php echo $ftwarehouse_code; ?>&ftlocation_code=<?php echo $_REQUEST['ftlocation_from']; ?>&ftitem_code=' + document.getElementById('ftitem_codeD0<?php echo $_REQUEST['index']; ?>').value+ '&objQty=fnqty_availableD0<?php echo $_REQUEST['index']; ?>&qty_revisi=<?php echo $fnqty; ?>');
        <?php

        die("");
        break;

    case "qty_available_revisi":
        ?>
        var i=document.getElementsByName('fnindexD0[]').length;

        for(var j=0;j < i;j++) {
            ajax_do('inventory.php?form=<?php echo $_REQUEST['form']; ?>&mode=<?php echo $_REQUEST['mode']; ?>&id=0&ajax=1&code=lokasi_revisi&index='+j+'&ftlocation_from='+document.getElementsByName('ftlocation_fromD0[]')[j].value + '&ftstock_mutation_no=' + document.getElementById('ftstock_mutation_no').value + '&ftitem_code='+ document.getElementsByName('ftitem_codeD0[]')[j].value);
        }

        <?php
        break;

    }
}

    Getsecurity('mutasi_konsinyasi');
    $objAxpDB->ViewName("Daftar Mutasi Konsinyasi");
    $objAxpDB->ViewSQL("SELECT A.fnid,
                               A.fdtrans_date,
                               A.ftstock_mutation_no,
                               A.ftreference,
                               A.ftnotes,
                               B.fttrans_status_name
                        FROM txstock_mutation_hdr A
                        INNER JOIN tstransaction_status B ON a.fnstatus=b.fntrans_status_code AND B.fntrans_group_code=2
                        WHERE (('{$_SESSION['strbranch']}' <> 'PUSAT'
                        AND a.ftbranch_code = '{$_SESSION['strbranch']}') OR '{$_SESSION['strbranch']}' = 'PUSAT')
                        AND a.ftcategory=2
                        ORDER BY
                        A.ftstock_mutation_no DESC, A.fdtrans_date DESC");

    $objAxpDB->ViewCaption("ID, Tgl. Mutasi, No. Mutasi, No Referensi, Catatan, Status");
    $objAxpDB->ViewPeriod(TRUE);

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

        $objAxpDB->FormJS($strAutoNumber0);
      	$m=" WHERE fnstatus=1 and ftstock_category_code='inv' ORDER BY ftitem_name,ftpart_no";

        //ajax grid
        // $ajaxGrid="ajax_do_grid0('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid1&ftso_no='+ document.getElementById('ftdocument_no').value);";
        // $objAxpDB->FormScript(2, "onChange=\"{$ajaxGrid}\"");
        //



    } else {
        $m=" WHERE ftstock_category_code='inv' ORDER BY ftitem_name,ftpart_no";

        if(isset($_REQUEST['revisi'])) {
            // reload qty available
            $ajax_qty="ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=qty_available_revisi');";
            $objAxpDB->FormJS($ajax_qty);
        }
    }

    $access=RetRstField("SELECT fbaccess FROM tluser_access_setting WHERE ftusername='admin' and ftsecurity_code='exp_date'");
    if($access==1){
        $show='TR';
    } else {
        $show='HD';
    }

    $objAxpDB->FormName("Mutasi Konsinyasi");
    $objAxpDB->FormSQL("SELECT ftstock_mutation_no,
                       ftcategory,
                       ftdocument_no,
                       fdtrans_date,
                       ftreference,
                       ftnotes, fnstatus,fftotal_qty FROM txstock_mutation_hdr");

    $objAxpDB->FormCaption("No. Mutasi, Kategori, No. SO,Tgl. Mutasi, No Referensi, Catatan, Status,Total Quantity");
    $objAxpDB->FormPosition("TL,TL,TL, TR, TR, BL, BR,HD");

    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, fdexp_date, ftlocation_from, ftwarehouse_from, ftlocation_to, ftwarehouse_to, fnqty_available, fnqty, fnqty_so, ftuom_code, fnqty_base, ffformula, '' as ftactive, ftso_no FROM TXStock_Mutation_Det");
    $objAxpDB->GridCaption(0, "No., Kode Barang, Exp. Date, Lokasi Asal, Gudang Asal, Lokasi Tujuan, Gudang Tujuan, Qty Avail, Qty,Qty SO, Satuan, Qty Base, Formula, Exp. Date Status, ftdocument_no");
     $objAxpDB->GridHidden(0,'0,0,1,0,1,0,1,0,0,0,0,0,1,1,1');
    $objAxpDB->GridHidden(1,'1');
    $objAxpDB->GridSize(0, ",,,,10,,10,10,10,10,,,");

    $ajaxOUM_Code="ajax_do('query2.php?code=uom&obj1=' + this.value + '&obj2=ftuom_codeD0_N_');";
    //$ajaxExp_Date="ajax_do('query2.php?code=exp_date&obj1=' + this.value + '&obj2=ftactiveD0_N_');";
    //$ajaxQtyAvail="ajax_do('query2.php?code=qty&ftwarehouse_code=' + document.getElementById('ftwarehouse_fromD0_N_').value + '&ftlocation_code=' + document.getElementById('ftlocation_fromD0_N_').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&ftwarehouse_code=' + document.getElementById('ftwarehouse_fromD0_N_').value + '&ftlocation_code=' + document.getElementById('ftlocation_fromD0_N_').value+'&ftitem_code=' + document.getElementById('ftitem_codeD0_N_').value+ '&objQty=fnqty_availableD0_N_');\"";
    //$ajaxGudang1="OnChange=\"ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=gudang&ftwarehouse_code='+this.value+'&ObjLokasi=ftlocation_fromD0_N_&t=1&objQty=fnqty_availableD0_N_');\"";
    $ajaxLokasi1="OnChange=\"ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=lokasi_from&index=_N_&ftlocation_from='+this.value);\"";
    $objAxpDB->GridScript(0,1, $ajaxOUM_CodeAndQtyAvail);
    $objAxpDB->GridScript(0,3, $ajaxLokasi1);
    //$objAxpDB->GridScript(0,4, $ajaxQtyAvail);
    //$ajaxGudang2="OnChange=\"ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=gudang&ftwarehouse_code='+this.value+'&ObjLokasi=ftlocation_toD0_N_&t=2');\"";
    $ajaxLokasi2="OnChange=\"ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=lokasi_to&index=_N_&ftlocation_to='+this.value);\"";
    $objAxpDB->GridScript(0,5, $ajaxLokasi2);
    $ajaxFormula="OnChange=\"ajax_do('inventory.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);MutasiKonsinyasiRecalDetail(_N_);\"";
    $objAxpDB->GridScript(0,8, $ajaxFormula);

    $objAxpDB->LookupMagicQuotes(TRUE);
    $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, CONCAT(TRIM(ftitem_name),CASE WHEN TRIM(ftpart_no) != '' THEN ' PART NO: ' ELSE '' END,ftpart_no) AS ftitem_name FROM tlstock_hdr $m ", "ftitem_code", "ftitem_name",-1,false);
    //$objAxpDB->Lookup("ftwarehouse_from", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse where fnwarehouse_type = 0 or fnwarehouse_type = 1 ", "ftwarehouse_code", "ftwarehouse_name", -1,true);
    //$objAxpDB->Lookup("ftwarehouse_to", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse where fnwarehouse_type = 0 or fnwarehouse_type = 1 ", "ftwarehouse_code", "ftwarehouse_name", -1,true);
    $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("ftlocation_from", "SELECT ftlocation_code, ftlocation_name FROM tllocation where 0=0 ", "ftlocation_code", "ftlocation_name", -1,true," and ftwarehouse_code='{$_REQUEST['ftwarehouse_code']}' ");
    $objAxpDB->Lookup("ftlocation_to", "SELECT ftlocation_code, ftlocation_name FROM tllocation where 0=0 ", "ftlocation_code", "ftlocation_name", -1,true," and ftwarehouse_code='{$_REQUEST['ftwarehouse_code']}' ");
    $objAxpDB->Lookup("ftuom_code", "SELECT ftuom_code, ftuom_name FROM tluom WHERE fnstatus = 1 order by ftuom_code", "ftuom_code", "ftuom_name", -1,true);

    //kategori
    $objAxpDB->Lookup("ftcategory", "SELECT `fntrans_status_code`,`fttrans_status_name` FROM `tstransaction_status` WHERE `fntrans_group_code` = 77", "fntrans_status_code", "fttrans_status_name", 1,true);
    //


  $ajaxGrid="ajax_do_grid0('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=2&code=grid123&ftdocument_no='+ document.getElementById('ftdocument_no').value + '&fdtrans_date='+ document.getElementById('fdtrans_date').value);";
  $objAxpDB->FormScript(2, "onChange=\"{$ajaxGrid}\"");



    $objAxpDB->SetStatus("id, fnstatus");
    if ($objAxpDB->GetStatus("id") == "")
        $objAxpDB->ButtonShow("baru");
    elseif ($objAxpDB->GetStatus("id") == 0) {
        $objAxpDB->FormLocked("1, 1,0,0, 0, 0, 1,1");
        $objAxpDB->GridLocked(0, "1, 0, 0, 0, 1, 0, 1, 1, 0,1, 0, 1, 1");
        $objAxpDB->GridAllowAddNew(0,true);
        $objAxpDB->GridAllowDelete(0,true);
        $objAxpDB->ButtonShow("reset, simpan, tutup");

        $objAxpDB->GridSQL(1, "SELECT REPLACE(fbaccess,1,'aktif') AS fbaccess  FROM tluser_access_setting #");
        $objAxpDB->GridCaption(1, "Access");
        $objAxpDB->GridAutoAdd(1, FALSE);
        //no so
        $objAxpDB->Lookup( "ftdocument_no", "SELECT a.ftso_no,a.fdtrans_date,  COALESCE(b.fnqty_base,0) AS fnqty_base, COALESCE(b.fnqty_accept_base,0) AS  fnqty_accept_base
        FROM `txsales_order_hdr` A JOIN  txsales_order_det B ON a.ftso_no=b.ftso_no
        WHERE a.`ftsales_type_code` = 2 AND a.`ftbranch_code`='{$_SESSION['strbranch']}' AND a.fnstatus=1 and a.fnreceive_status != 3  AND COALESCE(b.fnqty_base,0)!= COALESCE(b.fnqty_accept_base,0) GROUP BY ftso_no", "ftso_no", "fdtrans_date", -1,true);


    } elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
        // revisi
        if(isset($_REQUEST['revisi'])) {
            $objAxpDB->FormLocked("1, 1, 1, 0, 1,1,1,1");
            $objAxpDB->GridLocked(0, "1, 0, 0, 0, 1, 0, 1, 1, 0,1, 0, 1, 1");
            $objAxpDB->GridAllowAddNew(0,true);
            $objAxpDB->GridAllowDelete(0,true);

            $ajaxLokasiRevisi="OnChange=\"ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=lokasi_revisi&index=_N_&ftlocation_from='+this.value + '&ftstock_mutation_no=' + document.getElementById('ftstock_mutation_no').value + '&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value);\"";
            $objAxpDB->GridScript(0,3, $ajaxLokasiRevisi);
            $ajaxFormula="OnChange=\"ajax_do('inventory.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);MutasiKonsinyasiRecalDetail(_N_);\"";
            $objAxpDB->GridScript(0,8, $ajaxFormula);

            $objAxpDB->ButtonShow("simpan_revisi,tutup");


        } else {
            $objAxpDB->ButtonShow("baru, cetak, revisi, batal, tutup");
            $objAxpDB->FormLocked("1, 1, 1, 1, 1,1,1,1");
            $objAxpDB->GridLocked(0, "1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
        }

    } elseif (($objAxpDB->GetStatus("fnstatus") == 3) || ($objAxpDB->GetStatus("fnstatus") == 9)) {
      $objAxpDB->FormLocked("1, 1, 1, 1, 1,1,1,1");
        $objAxpDB->GridLocked(0, "1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
        $objAxpDB->ButtonShow("tutup");
    }


    if($_REQUEST['ajax']) {

		switch($_REQUEST['code']) {
			case "grid123":
              ob_start();

                                $ssql="SELECT `ftso_no`
                                      FROM `txsales_order_hdr`
                                      WHERE `ftsales_type_code` = 2
                                      AND `ftbranch_code`='{$_SESSION['strbranch']}'
                                      AND  fnstatus=1
                                      AND ftso_no='{$_REQUEST['ftdocument_no']}'";
				$ftso_no=RetRstField($ssql);

                                $ssql1="SELECT txsales_order_det.`fnindex` ,
                                         txsales_order_det.`ftitem_code`,
                                         '' AS fdexp_date,
                                         '' AS ftlocation_from,
                                         '' AS ftwarehouse_from,
                                         '' AS ftlocation_to,
                                         '' AS ftwarehouse_to,
                                         tlstock_det.`fnqty_available`,
                                         (txsales_order_det.fnqty_base-txsales_order_det.fnqty_base) AS fnqty,
                                         round(txsales_order_det.fnqty_base-IFNULL(txsales_order_det.fnqty_accept_base,0),2) as fnqty_so,
                                         txsales_order_det.ftuom_code,
                                         txsales_order_det.fnqty_base,
                                         txsales_order_det.ffformula,
                                         '' AS ftactive,
                                         txsales_order_det.ftso_no
                                         FROM `txsales_order_hdr`
                                         JOIN txsales_order_det ON txsales_order_det.`ftso_no`=txsales_order_hdr.`ftso_no`
                                         JOIN tlstock_det ON txsales_order_det.ftitem_code=tlstock_det.ftitem_code
                                         WHERE txsales_order_hdr.ftso_no='{$_REQUEST['ftdocument_no']}'
                                         AND (txsales_order_det.fnqty_base-IFNULL(txsales_order_det.fnqty_accept_base,0)>0)
                                         GROUP BY ftitem_code
                                         ORDER BY txsales_order_det.fnindex#";

                                         $objAxpDB->GridSQL(0, $ssql1);
                                         $objAxpDB->GridCaption(0, "No., Kode Barang, Exp. Date, Lokasi Asal, Gudang Asal, Lokasi Tujuan, Gudang Tujuan, Qty Avail, Qty,Qty SO, Satuan, Qty Base, Formula, Exp. Date Status, NO SO");
                                         // $objAxpDB->GridHidden(0,'0,0,1,0,1,0,1,0,0,0,0,1,1,1,1');
                                         $objAxpDB->GridHidden(1,'1');
                                         $objAxpDB->GridSize(0, ",,,,10,,10,6,10,10,,,");


                                        $objAxpDB->Lookup("ftlocation_from", "SELECT ftlocation_code, ftlocation_name FROM tllocation where 0=0 ", "ftlocation_code", "ftlocation_name", -1,true," and ftwarehouse_code='{$_REQUEST['ftwarehouse_code']}' ");
                                        $objAxpDB->Lookup("ftlocation_to", "SELECT ftlocation_code, ftlocation_name FROM tllocation where 0=0 ", "ftlocation_code", "ftlocation_name", -1,true," and ftwarehouse_code='{$_REQUEST['ftwarehouse_code']}' ");
                                        $objAxpDB->Lookup("ftuom_code", "SELECT ftuom_code, ftuom_name FROM tluom WHERE fnstatus = 1 order by ftuom_code", "ftuom_code", "ftuom_name", -1,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;
        RowIndex0();
        reSize();
        ElementCurrencyALL();
        <?php


			break;
		}
            die("");
            exit;
    }




    $objAxpDB->Process();

    $objAxpDB->WriteHtmlTable();

?>
