<?php

    Getsecurity('debit_memo');

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

        }

    }

    $objAxpDB->ViewName("Daftar Debit Memo");
    #$objAxpDB->ViewTable("vKUDebitMemo");
    $objAxpDB->ViewSQL("SELECT  A.fnid, A.ftdm_no, A.fdtrans_date, c.ftsupplier_name, D.fttrans_status_name AS fntrans_type, A.fcinvoice_amount, A.fcoutstanding_amount, A.fcdebit_amount, A.ftnotes,  B.fttrans_status_name FROM txdebit_memo_hdr
                      A left JOIN tstransaction_status B ON a.fnstatus=b.fntrans_status_code AND fntrans_group_code=2
                      left JOIN tlsupplier C ON A.ftsupplier_code=c.ftsupplier_code
                      left JOIN tstransaction_status D ON  A.fntrans_type=D.fntrans_status_code AND D.fntrans_group_code = 6
                      WHERE (('{$_SESSION['strbranch']}' <> 'PUSAT' AND a.ftbranch_code = '{$_SESSION['strbranch']}') OR '{$_SESSION['strbranch']}' = 'PUSAT')
                      ORDER BY A.ftdm_no DESC, A.fdtrans_date DESC");
    $objAxpDB->ViewCaption("ID, No. transaksi, Tgl. transaksi, Supplier, Jenis transaksi,  Total Debit Memo, Sisa Debit Memo,  Total Pemb. Debit Memo, Catatan,  Status");
    $objAxpDB->ViewPeriod(TRUE);
    
    $objAxpDB->FormName("Debit Memo");
    $objAxpDB->FormSQL("SELECT ftdm_no, ftsupplier_code, fdtrans_date, fntrans_type,ftcurrency_code,fcrate, ftnotes,  fcinvoice_amount, fcoutstanding_amount, fcdebit_amount, fnstatus FROM txdebit_memo_hdr");
    $objAxpDB->FormCaption("No. transaksi, Supplier, Tgl. transaksi, Jenis transaksi,Mata Uang, Kurs, Catatan,  Total Debit Memo, Sisa Debit Memo, Total Pemb. Debit Memo, Status");
    $objAxpDB->FormPosition("TL, TL, TR, TR, TR, TR, BL, BR, BR, BR, BR");
    
    $ajaxKurs="ajax_do('keuangan.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}\"");

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

            $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                           {$strAutoNumber1}
                              }
                              OnChangeDate_fdtrans_date();
                              document.getElementById('ftcurrency_code').value='IDR';
                              {$ajaxKurs}
                              document.getElementById('fcrate').value=1;
                              ";

            $objAxpDB->FormJS($strAutoNumber0);
            $ajaxGrid="ajax_do('keuangan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&ftsupplier_code='+ document.getElementById('ftsupplier_code').value +'&fntrans_type='+  document.getElementById('fntrans_type').value );";

            $objAxpDB->FormScript(1, "onChange=\"{$ajaxGrid}\"");
            $objAxpDB->FormScript(3, "onChange=\"{$ajaxGrid}\"");


            $ftdue_date="&ftdue_date=ftdue_dateD0_N_";
            $ftpp_no="";
            $ftpo_no="";
            $ftreceipt_no="";
            $fcoutstanding_debit_memo="&fcoutstanding_debit_memo=fcoutstanding_debit_memoD0_N_";
            $fcinvoice_amount="&fcinvoice_amount=fcinvoice_amountD0_N_";
            $fcoutstanding_amount="&fcoutstanding_amount=fcoutstanding_amountD0_N_";
            $fcinvoice_amountUSD="&fcinvoice_amountUSD=fcinvoice_amountUSDD0_N_";
            $fcoutstanding_amountUSD="&fcoutstanding_amountUSD=fcoutstanding_amountUSDD0_N_";
            $obj="{$fcoutstanding_debit_memo}{$ftdue_date}{$ftpp_no}{$ftpo_no}{$ftreceipt_no}{$fcinvoice_amount}{$fcoutstanding_amount}{$fcinvoice_amountUSD}{$fcoutstanding_amountUSD}";
            $ajaxcv="ajax_do('keuangan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&ajax=1&code=cv&ftpurchase_invoice_no='+ document.getElementById('ftcv_noD0_N_').value +'{$obj}');";
            $objAxpDB->GridScript(0,1,"OnChange=\"$ajaxcv\"");
    }

    $objAxpDB->GridSQL(0, "SELECT fnindex, ftcv_no,ftdue_date,ftdescription,fcinvoice_amount, fcoutstanding_amount, fcdebit_amount,fcoutstanding_debit_memo FROM txdebit_memo_det");
    $objAxpDB->GridCaption(0, "No.,No. PI, Tgl. Invoice,Keterangan, Jumlah Invoice, Sisa Pembayaran, Debit Memo, Sisa Debit Memo");

    $objAxpDB->Lookup("ftcurrency_code", "SELECT  ftsimbol, ftdescription  FROM tlcurrency_hdr ORDER BY ftsimbol", "ftsimbol", "ftdescription", 0, 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("ftsupplier_code", "SELECT ftsupplier_code, ftsupplier_name FROM tlsupplier ORDER BY ftsupplier_code", "ftsupplier_code", "ftsupplier_name", -1,false);
    $objAxpDB->Lookup("fntrans_type", "SELECT  fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 6", "fntrans_status_code", "fttrans_status_name",-1,true);

    // $ssql="SELECT ftpurchase_invoice_no FROM txpurchase_invoice_hdr WHERE (fnstatus=1 OR fnstatus = 2) AND ftsupplier_code='{$_REQUEST['ftsupplier_code']}'";
    $ssql="SELECT ftpurchase_invoice_no,ftpo_no FROM txpurchase_invoice_hdr x WHERE fnstatus<>9 AND ftsupplier_code='{$_REQUEST['ftsupplier_code']}'";
    $ssql.=" and exists (select a.ftpurchase_invoice_no,fcamount-ifnull(b.fcdebit_amount,0) as fcoutstanding_debit_memo
                from txpurchase_invoice_hdr a
                left join (
                                select a2.ftcv_no, sum(a2.fcdebit_amount) as fcdebit_amount
                                from txdebit_memo_hdr a1,
                                        txdebit_memo_det a2
                                where a1.ftdm_no=a2.ftdm_no and a1.fnstatus<>9
                                group by a2.ftcv_no
                        ) b
                on a.ftpurchase_invoice_no=b.ftcv_no 
                where fcamount-ifnull(b.fcdebit_amount,0)>0
                        and x.ftpurchase_invoice_no=a.ftpurchase_invoice_no 
                        AND a.ftbranch_code='{$_SESSION['strbranch']}'
                ) ";

    $objAxpDB->Lookup( "ftcv_no",$ssql, "ftpurchase_invoice_no", "ftpo_no", -1,true);

    $objAxpDB->SetStatus("id, fnstatus,fntrans_type");
    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, 1, 0, 1, 1, 0, 1");
            $objAxpDB->GridHidden(0, "0, 0, 0, 0, 0, 0, 0, 0");
            $objAxpDB->ButtonShow("reset, simpan, tutup");
            $objAxpDB->FormLocked("0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1");
            $objAxpDB->GridScript(0,7, "onChange=\"DebitMemoRecalQty();\"");
            $objAxpDB->GridDelJS(0,"DebitMemoRecalQty();");

    } elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
            $objAxpDB->ButtonShow("baru, cetak, 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");
            $objAxpDB->GridHidden(0, "0, 0, 0, 0, 0, 0, 0, 1");
            unset($objAxpDB->lookup["ftcv_no"]);
            
    } elseif ($objAxpDB->GetStatus("fnstatus") == 9) {
            $objAxpDB->FormLocked("1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1");
            $objAxpDB->GridLocked(0, "1, 1, 1, 1, 1, 1, 1, 1");
            $objAxpDB->GridHidden(0, "0, 0, 0, 0, 0, 0, 0, 1");
            $objAxpDB->ButtonShow("tutup");
            unset($objAxpDB->lookup["ftcv_no"]);
    }
    
    if($objAxpDB->GetStatus("id")&&$objAxpDB->GetStatus("fntrans_type")>'2') {
          $objAxpDB->GridSQL(0, "SELECT fnindex,  ftdescription, fcdebit_amount FROM txdebit_memo_det");
          $objAxpDB->GridCaption(0, "No.,  Keterangan, Debit Memo");
          $objAxpDB->GridLocked(0, "1, 1, 1");
    }

    if($_REQUEST['ajax']) {
           switch($_REQUEST['code']) {
           case "grid":
               ob_start();
               if(($_REQUEST['fntrans_type']==3)||($_REQUEST['fntrans_type']==4)) {
                      $objAxpDB->GridSQL(0, "SELECT fnindex,  ftdescription,  fcdebit_amount FROM txdebit_memo_det");
                      $objAxpDB->GridCaption(0, "No.,  Keterangan, Debit Memo");
                      $objAxpDB->GridLocked(0, "1, 0, 0");
                      $objAxpDB->GridScript(0,1,"");
                      $objAxpDB->GridScript(0,2, "onChange=\"DebitMemoRecalQty();\"");
               }
               $objAxpDB->Process();
               $objAxpDB->setGrid1(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;
                    RowBuild0();
               <?php

               break;
           case "cv":
                $ssql="SELECT fdtrans_date AS ftdue_date,fcamount,fcoutstanding_amount
                        FROM txpurchase_invoice_hdr
                        WHERE ftpurchase_invoice_no='{$_REQUEST['ftpurchase_invoice_no']}'";
                $rs=mysql_query($ssql);
                $r=mysql_fetch_assoc($rs);
                ?>
                    document.getElementById('<?php echo $_REQUEST['ftdue_date']; ?>').value='<?php echo CDateRev($r['ftdue_date']); ?>';
                    document.getElementById('<?php echo $_REQUEST['fcinvoice_amount']; ?>').value='<?php echo  number_format($r['fcamount'],2) ; ?>';
                    document.getElementById('<?php echo $_REQUEST['fcoutstanding_amount']; ?>').value='<?php echo  number_format($r['fcoutstanding_amount'],2) ; ?>';
                <?php
                $sql="select a.ftpurchase_invoice_no,a.fcamount-ifnull(b.fcdebit_amount,0) as fcoutstanding_debit_memo
                        from txpurchase_invoice_det a
                        left join (
                                        select a2.ftcv_no, sum(a2.fcdebit_amount) as fcdebit_amount
                                        from txdebit_memo_hdr a1,
                                                txdebit_memo_det a2
                                        where a1.ftdm_no=a2.ftdm_no and a1.fnstatus<>9
                                        group by a2.ftcv_no
                                ) b
                        on a.ftpurchase_invoice_no=b.ftcv_no
                        where a.ftpurchase_invoice_no='{$_REQUEST['ftpurchase_invoice_no']}'";
                $rs=mysql_query($sql);
                $r=mysql_fetch_assoc($rs);

                //$fcamount=$r['fcamount'];
                //$fcoutstanding_amount=$r['fcoutstanding_amount'];
                $fcoutstanding_debit_memo=$r['fcoutstanding_debit_memo'];

                //$sql="select SUM(b.fcdebit_amount) FROM txdebit_memo_hdr a INNER JOIN txdebit_memo_det b ON a.ftcm_no=b.ftcm_no WHERE b.ftpo_no='{$_REQUEST['ftpo_no']}' AND a.fnstatus <> '9'";
                //$rs=mysql_query($sql);
                //$r=mysql_fetch_assoc($rs);
                //$fccredit_amount=$r['fcdebit_amount'];

                //$fcamount_total=$fcamount - $fcoutstanding_amount - $fcdebit_amount;
                $fcamount_total=$fcoutstanding_debit_memo;
                if($fctotal_amount < 0) $fctotal_amount=0;

                ?>
                    document.getElementById('<?php echo $_REQUEST['fcoutstanding_debit_memo']; ?>').value='<?php echo number_format($r['fcoutstanding_debit_memo'], 2); ?>';
                <?php

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

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

?>

