<?php

    Getsecurity("return_pembelian_import");

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

                                 } ";


                            }

                    die("");
                    break;
             case "idr":
                $ssql="SELECT fcrate FROM tlcurrency_hdr WHERE ftsimbol='{$_REQUEST["ftcurrency_code"]}'";
                $mysql=mysql_query($ssql) or die ($ssql);
                $r=mysql_fetch_assoc($mysql);
                ?>
                  document.getElementById('fcrate').value='<?php echo number_format($r["fcrate"], 2); ?>';
                  ElementCurrencyALL();
                <?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);
                if ($ffformula==0) { $ffformula=1; };
                
                $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;
            }
    }
           
    $objAxpDB->ViewName("Daftar Purchase Return Import");
    $objAxpDB->ViewSQL("SELECT a.fnid,a.ftpurchase_return_no,a.fdtrans_date,a.ftpurchase_invoice_no,
                                d.ftpo_no,a.ftsupplier_code,
                                b.ftsupplier_name,a.fcamount,c.fttrans_status_name AS ftstatus
                        FROM txpurchase_return_hdr a
                        LEFT JOIN tlsupplier b ON a.ftsupplier_code = b.ftsupplier_code
                        LEFT JOIN tstransaction_status c ON a.fnstatus = c.fntrans_status_code AND c.fntrans_group_code = 2
                        LEFT JOIN txpurchase_invoice_hdr d ON a.ftpurchase_invoice_no=d.ftpurchase_invoice_no
                        WHERE a.fnpo_type=1
                        AND (('{$_SESSION['strbranch']}' <> 'PUSAT' AND a.ftbranch_code = '{$_SESSION['strbranch']}') OR '{$_SESSION['strbranch']}' = 'PUSAT')
                        ORDER BY a.fdtrans_date DESC, a.ftpurchase_return_no desc");
    
    $objAxpDB->ViewCaption("ID, No. Return, Tgl. Return, No. PI, No. PO, Kode Supplier, Nama Supplier, Jumlah, Status");
    $objAxpDB->ViewPeriod(TRUE);
	
    // Deklarasi Header & Footer
    $objAxpDB->FormName("Purchase Return Import");
    $objAxpDB->FormSQL("SELECT ftpurchase_return_no, ftsupplier_code, ftpurchase_invoice_no,
                                    fdtrans_date,
                                    ftcurrency_code,fcrate,ftnotes,
                                    fcsub_amount,ffdiscount,fcdiscount,
                                    fcother_cost,ffvat,fcvat,
                                    fcamount,fnstatus,
                                    '0' AS fnpo_type
                            FROM txpurchase_return_hdr");
    $objAxpDB->FormCaption("No. Return, Supplier, No. PI,
                        Tgl. Return, Mata Uang, Kurs, Catatan,
                        Sub Total, Disc (%), Disc,Biaya Lain-Lain,Pajak (%), Pajak,Grand Total, Status,
                                Type PO");
    $objAxpDB->FormPosition("TL,TL, TL, TR, TR,  TR, BL,BR, BR, BR, BR, BR,BR, BR, BR,HD");
     
    // Deklarasi Detail
    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code,fnqty_pi,fnqty_outstanding_pi,fnqty_available, fnqty, ftuom_code,ftwarehouse_code,ftlocation_code, fcprice, ffdiscount, fcdiscount, fcamount, fnqty_base, ffformula FROM txpurchase_return_det");
    $objAxpDB->GridCaption(0, "No., Kode Barang, Qty PI, Qty Sisa PI, Qty Avail, Qty, Satuan, Gudang, Lokasi, Harga, Discount(%),Discount,Jumlah, Qty Base, Formula");

    if($_REQUEST['id']=='0') {
       $m=" AND fnstatus=1 ";
    }

    $objAxpDB->Lookup("ftpurchase_invoice_no", "SELECT ftpurchase_invoice_no, CONCAT(fdtrans_date, ' - ', ftpo_no) AS ftvalue_name FROM txpurchase_invoice_hdr WHERE fnpo_type=1 {$m}", "ftpurchase_invoice_no", "ftvalue_name", -1, False, "' AND ftsupplier_code=\''+ document.getElementById('ftsupplier_code').value + '\''");
    $objAxpDB->Lookup("ftsupplier_code", "SELECT ftsupplier_Code, ftsupplier_name FROM tlsupplier WHERE fnsupplier_type IN (1, 2) ORDER BY ftsupplier_Code", "ftsupplier_Code", "ftsupplier_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_bahan_baku ORDER BY ftitem_code", "ftitem_code", "ftitem_name", -1, False);
    $objAxpDB->Lookup("ftcurrency_code", "SELECT  ftsimbol, ftdescription  FROM tlcurrency_hdr ORDER BY ftsimbol", "ftsimbol", "ftdescription", 0, True);
    $objAxpDB->Lookup("ftwarehouse_code", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse", "ftwarehouse_code", "ftwarehouse_name", 0,true);
    $objAxpDB->Lookup("ftlocation_code", "SELECT ftlocation_code, ftlocation_name FROM tllocation", "ftlocation_code", "ftlocation_name", 0,true);

    $ajaxGrid="ajax_do_grid0('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&ftpurchase_invoice_no='+ document.getElementById('ftpurchase_invoice_no').value);";
    $objAxpDB->FormScript(1, "onChange=\"document.getElementById('ftpurchase_invoice_no').value='';document.getElementById('hidden_ftpurchase_invoice_no').value='';document.getElementById('span_ftpurchase_invoice_no').innerHTML='';{$ajaxGrid}\"");
    $objAxpDB->FormScript(2, "onChange=\"{$ajaxGrid}\"");
    $ajaxKurs="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=idr&ftcurrency_code='+ document.getElementById('ftcurrency_code').value);";
    $objAxpDB->FormScript(4, "onChange=\"{$ajaxKurs}\"");
    
    $ajaxFormula="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=formula&index=_N_&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value+'&ftuom_code='+ document.getElementById('ftuom_codeD0_N_').value+'&fnqty='+ document.getElementById('fnqtyD0_N_').value);";
    $ajax1=" onChange=\"{$ajaxFormula}PurchaseReturnImportRecalPrice();PurchaseReturnLocalRecalDiscPercent();\"  ";
    $ajaxItem="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=item&ftitem_code=' + this.value + '&index=_N_');";
    $objAxpDB->GridScript(0,1," onChange=\"{$ajaxItem}\"");
    $objAxpDB->GridScript(0,2," onChange=\"{$ajaxItem}\"");
    $objAxpDB->GridScript(0,5, $ajax1);
    $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_'); document.getElementById('fnqty_availableD0_N_').value='".number_format(0, 2)."';\"";
    $objAxpDB->GridScript(0,7, $ajaxGudang);
    $ajax1=" onChange=\"PurchaseReturnImportRecalDiscDetailPercent(_N_);\"   ";
    $objAxpDB->GridScript(0,10, $ajax1);
    $ajax1="   onChange=\"PurchaseReturnImportRecalDiscDetailRp(_N_);\"  ";
    $objAxpDB->GridScript(0,11, $ajax1);
    
    $objAxpDB->FormScript(8, "onChange=\"PurchaseReturnImportRecalDiscPercent();\"");
    $objAxpDB->FormScript(9, "onChange=\"PurchaseReturnImportRecalDiscRp();\"");
    $objAxpDB->FormScript(10, "onChange=\"PurchaseReturnImportRecalQty0();\"");
    $objAxpDB->FormScript(11, "onChange=\"PurchaseReturnImportRecalQty0();\"");
    
    $ajax1=" onChange=\"PurchaseReturnImportRecalPrice();\"  ";
    $objAxpDB->GridScript(0,2, $ajax1);
    $objAxpDB->SetStatus("id, fnreceipt_status, fnstatus");
       
    if(!$_REQUEST['id']) {
            $strAutoNumber1="ajax_do('query2.php?code=2&ftmodule_code=pu006&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftpurchase_return_no&no_trans=ftpurchase_return_no&table_name=txpurchase_return_hdr&ftbranch_code={$_SESSION['strbranch']}');";
          
            $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                           {$strAutoNumber1}
                                      
                                     }
                                OnChangeDate_fdtrans_date();
                                document.getElementById(\"fnpo_type\").value='1';
                                var i=document.getElementsByName('fnindexD0[]').length;
                                document.getElementById('ftcurrency_code').value='IDR';
                                {$ajaxKurs}
                                ";

            $objAxpDB->FormJS($strAutoNumber0);
    } else {

        $ssql = "Select ftpurchase_invoice_no from txpurchase_return_hdr where fnid={$_REQUEST['id']}";
        $ftpurchase_invoice_no = RetRstField($ssql);

        $ssql="SELECT a.fnpo_type3 FROM txpurchase_order_hdr a
                        left join txpurchase_invoice_hdr b ON a.ftpo_no = b.ftpo_no
                        WHERE   b.ftpurchase_invoice_no='{$ftpurchase_invoice_no}' ";
        $fnpo_type3=RetRstField($ssql);

        $tablename = "";
        if($fnpo_type3==0) {
            $tablename = "_bahan_baku";
            $fnwarehouse_type = '3';
        } elseif ($fnpo_type3==1) {
            $tablename = "_setengah_jadi";
            $fnwarehouse_type = '2';
        } elseif ($fnpo_type3==2) {
            $tablename = "_sparepart";
            $fnwarehouse_type = '4';
        } elseif ($fnpo_type3==3) {
            $tablename = "";
            $fnwarehouse_type = '1';
        }

        $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr{$tablename} ORDER BY ftitem_code", "ftitem_code", "ftitem_name", -1, False);

        $objAxpDB->FormJS("ElementCurrencyALL();");
    }
    
    if ($objAxpDB->GetStatus("id") == "") {
            $objAxpDB->ButtonShow("baru");
    } elseif ($objAxpDB->GetStatus("id") == 0) {
            $objAxpDB->ButtonShow("reset, simpan, tutup");
            $objAxpDB->FormLocked("1, 0, 0,
                                   0, 0, 0,
                                   0,
                                    1,0,0,0,0,0,1,1");
            $objAxpDB->GridHidden(0, "0, 0, 0, 0,0,0,0,0,0,0,0,0,0,1,1");
            $objAxpDB->GridLocked(0, "1, 1, 1, 1,1,0,1,1,1,1,1,1,1,1,1");

    } elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
             $objAxpDB->ButtonShow("baru, cetak, cetak_non_harga, batal, tutup");
            $objAxpDB->FormLocked("1, 1, 1,1,
                                   1, 1, 1, 1, 1,
                                   1,
                                    1,1,1,1,1,1,1,1");
           $objAxpDB->GridHidden(0, "0, 0, 0, 0,0,0,0,0,0,0,0,0,0,1,1");
           $objAxpDB->GridLocked(0, "1, 1, 1, 1,1,1,1,1,1,1,1,1,1,1,1");
          
    } elseif ($objAxpDB->GetStatus("fnstatus") == 9) {
          $objAxpDB->ButtonShow("baru, tutup");
          $objAxpDB->FormLocked("1, 1, 1,1,
                                   1, 1, 1, 1, 1,
                                   1,
                                    1,1,1,1,1,1,1,1");
          $objAxpDB->GridHidden(0, "0, 0, 0, 0,0,0,0,0,0,0,0,0,0,1,1");
          $objAxpDB->GridLocked(0, "1, 1, 1, 1,1,1,1,1,1,1,1,1,1,1,1");
    }


     if($_REQUEST['ajax']) {
           switch($_REQUEST['code']) {
           case "grid":
               ob_start();
                     $ssql="SELECT a.fnpo_type3
                        FROM txpurchase_order_hdr a left join txpurchase_invoice_hdr b
                        ON a.ftpo_no = b.ftpo_no
                        WHERE   b.ftpurchase_invoice_no='".$_REQUEST['ftpurchase_invoice_no']."' ";

                $fnpo_type3=RetRstField($ssql);

                $tablename = "";
                if($fnpo_type3==0) {
                    $tablename = "_bahan_baku";
                    $fnwarehouse_type = '3';
                } elseif ($fnpo_type3==1) {
                    $tablename = "_setengah_jadi";
                    $fnwarehouse_type = '2';
                } elseif ($fnpo_type3==2) {
                    $tablename = "_sparepart";
                    $fnwarehouse_type = '4';
                } elseif ($fnpo_type3==3) {
                    $tablename = "";
                    $fnwarehouse_type = '1';
                }

               $ssql="SELECT b.fnindex,b.ftitem_code,b.fnqty AS fnqty_pi,b.fnqty-IFNULL(b.fnqty_return,0) AS fnqty_outstanding_pi ,SUM(c.fnqty_available) AS fnqty_available, b.fnqty-b.fnqty AS fnqty, b.ftuom_code,b.ftwarehouse_code,b.ftlocation_code, b.fcprice, b.ffdiscount, '0.00' as fcdiscount, b.fcamount-b.fcamount AS fcamount,b.fnqty_base-b.fnqty_base as fnqty_base, b.ffformula
                        FROM txpurchase_invoice_hdr a INNER JOIN
                                txpurchase_invoice_det b ON  a.ftpurchase_invoice_no=b.ftpurchase_invoice_no
                                LEFT JOIN tlstock_det{$tablename} c ON b.ftitem_code=c.ftitem_code
                        WHERE   a.ftpurchase_invoice_no='".$_REQUEST['ftpurchase_invoice_no']."'
                        GROUP BY b.fnindex,b.ftitem_code,b.fnqty,b.fnqty_return, b.ftuom_code,b.ftwarehouse_code,b.ftlocation_code, b.fcprice, b.ffdiscount, b.fcdiscount, b.fcamount
                           #                               ";
               
               $objAxpDB->GridSQL(0,$ssql);

               $objAxpDB->GridCaption(0, "No., Kode Barang, Qty PI, Qty Sisa PI,Qty Avail, Qty, Satuan, Gudang, Lokasi, Harga, Discount(%),Discount,Jumlah,Qty Base, Formula");
               $objAxpDB->GridHidden(0, "0, 0, 0, 0,0,0,0,0,0,0,0,0,0,1,1");
               $objAxpDB->GridLocked(0, "1, 1, 1, 1,1,0,1,0,0,1,1,1,1,1,1");

               $ajaxFormula="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=formula&index=_N_&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value+'&ftuom_code='+ document.getElementById('ftuom_codeD0_N_').value+'&fnqty='+ document.getElementById('fnqtyD0_N_').value);";
               $ajax1=" onChange=\"{$ajaxFormula}PurchaseReturnImportRecalQty();PurchaseReturnImportRecalPrice();PurchaseReturnLocalRecalDiscPercent();\"  ";
               $objAxpDB->GridScript(0,5, $ajax1);

               $ajaxQtyAvail=" onChange=\"ajax_do('query2.php?code=qty{$tablename}&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_');\"";
               $objAxpDB->GridScript(0,8, $ajaxQtyAvail);

               $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr{$tablename} ", "ftitem_code", "ftitem_name",-1,false);
               $objAxpDB->Lookup("ftwarehouse_code", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse where fnwarehouse_type=0 or fnwarehouse_type={$fnwarehouse_type} ", "ftwarehouse_code", "ftwarehouse_name",-1,true);
               $objAxpDB->Lookup("ftlocation_code", "SELECT ftlocation_code, ftlocation_name FROM tllocation where 0=0 ", "ftlocation_code", "ftlocation_name", -1,true,"' AND ftwarehouse_code=\'' + document.getElementById('ftwarehouse_code').value + '\''");
               
               $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();
                <?php
                    $mysql=mysql_query($ssql);
                    while($r=mysql_fetch_assoc($mysql)){
                 ?>
                    document.getElementById('ftwarehouse_codeD0<?php echo $r["fnindex"];?>').value='<?php echo $r['ftwarehouse_code']; ?>';
                    document.getElementById('ftlocation_codeD0<?php echo $r["fnindex"];?>').value='<?php echo $r['ftlocation_code']; ?>';
                <?php
                      }
                      $ssql="SELECT a.ftcurrency_code, a.fcrate, a.ffdiscount, a.fcdiscount, a.ffvat, a.fcvat, b.ftwarehouse_code, b.ftlocation_code FROM txpurchase_invoice_hdr a INNER JOIN txpurchase_invoice_det b ON a.ftpurchase_invoice_no=b.ftpurchase_invoice_no WHERE a.ftpurchase_invoice_no='{$_REQUEST['ftpurchase_invoice_no']}'";
                      $rs=mysql_query($ssql) or die($ssql);
                      $r=mysql_fetch_assoc($rs);
                 ?>
                    ///                document.getElementById('ftterm_payment').value='<?php echo $r['ftterm_payment']; ?>';
                    document.getElementById('fcrate').value='<?php echo number_format($r['fcrate'], 2); ?>';
                    document.getElementById('ffdiscount').value='<?php echo number_format($r['ffdiscount'], 2); ?>';
                    document.getElementById('fcdiscount').value='<?php echo number_format($r['fcdiscount'], 2); ?>';
                    document.getElementById('ffvat').value='<?php echo number_format($r['ffvat'], 2); ?>';
                    document.getElementById('fcvat').value='<?php echo number_format($r['fcvat'], 2); ?>';

                 
                    var len1 = document.getElementById('ftcurrency_code').length;
                    for (var idx1=0; idx1 < len1; idx1++)
                    {
                        if (document.getElementById('ftcurrency_code').options[idx1].value=='<?php echo $r['ftcurrency_code']; ?>') {
                            document.getElementById('ftcurrency_code').selectedIndex=idx1;
                            break;
                        }
                    }

                    PurchaseReturnLocalRecalDiscPercent();
                    PurchaseReturnImportRecalPrice();
               <?php

               break;

           }
           die("");
           exit;
    }
    

    $objAxpDB->Process();
    $objAxpDB->WriteHtmlTable();