<?php

    Getsecurity("sales_invoice_pameran");

    if($_REQUEST['ajax']) {

        switch($_REQUEST['code']) {

          // Set Formula dan Qty Base multi satuan
          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']);
              $ffTotalQty = $ffTotalQty + $ffQtyMulti;
              ?>
                      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 "item":

                      $ssql="SELECT fcselling_price,ftitem_name  FROM tlselling_price
                        WHERE ftitem_code='{$_REQUEST['ftitem_code']}'";

                        $rs=mysql_query($ssql) ;
                        $r=mysql_fetch_assoc($rs);

                    ?>

                      document.getElementById('fcpriceD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $r['fcselling_price']; ?>';
                      document.getElementById('ftitem_nameD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $r['ftitem_name']; ?>';
                      ElementCurrencyALL();
                   <?php

                  die("");
                  break;

          case "lokasi":

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

              ?>


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

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

              <?php

              die("");
              break;

          case "customer":

              $ssql="SELECT ftcustomer_code, ftcustomer_name, fttax_code ,IFNULL(ftaddress,'') AS ftship_address, ftterm_payment, ftsales_code, fnstatus
                        FROM tlcustomer
                        WHERE ftcustomer_code='{$_REQUEST['ftcustomer_code']}' ";

              $rs=mysql_query($ssql) ;
              $r=mysql_fetch_assoc($rs);

              $term_payment = $r['ftterm_payment'];
              $fnstatus = $r['fnstatus'];

              if ($fnstatus == '') {
                  // get no customer code, do nothing
              // Check if customer status is NOT AKTIF OR PENDING ?
              } else if (($fnstatus != 1) && ($fnstatus != '')) {
                  ?>
                      alert('Status Customer TIDAK AKTIF atau PENDING. \nHarap cek dengan bagian Finance. ');
                      document.getElementById('ftcustomer_code').value='';
                      document.getElementById('hidden_ftcustomer_code').value='';
                      document.getElementById('span_ftcustomer_code').textContent='';
                  <?php
              } else {
                  if ($term_payment == '') {$term_payment = 0; }
                  $ssql="select date_add('".CDate($_REQUEST['tgl'])."',INTERVAL {$term_payment} DAY)";
                  $date=RetRstField($ssql);

                  ?>

                      document.getElementById('ftship_address').value='<?php echo js_esc($r['ftship_address']); ?>';
                      document.getElementById('ftcustomer_name').value='<?php echo js_esc($r['ftcustomer_name']); ?>';
                      SetValueSelected('<?php echo $r['fttax_code']; ?>',document.getElementById('fntax_code'));

                      if (document.getElementById('fntax_code').value == 0) {
                          document.getElementById('ffvat').value = 10.00;
                      } else if (document.getElementById('fntax_code').value == 1) {
                          document.getElementById('ffvat').value = 0;
                      } else if (document.getElementById('fntax_code').value == 2) {
                          document.getElementById('ffvat').value = 10;
                      }

                      // KHUSUS ABRINDO : untuk sementara tidak pake pajak/non ppn, set fttax_code = kosong
                      document.getElementById('fntax_code').value = "";
                      document.getElementById('ffvat').value = 0;

                      document.getElementById('fddue_date').value='<?php echo CDateRev($date); ?>';
                  <?php

                  // term payment
                  $ssql="SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 75 AND (( fntrans_status_code = 0) OR (fntrans_status_code='{$term_payment}')) ORDER BY fntrans_status_code ";
                  $rs=mysql_query($ssql);

                  ?>

                      var ObjSelect=document.getElementById('ftterm_payment');
                      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['fntrans_status_code']}--{$r['fttrans_status_name']}','{$r['fntrans_status_code']}'),null,null);
                              } catch(ex) {
                                  ObjSelect.add(new Option('{$r['fntrans_status_code']}--{$r['fttrans_status_name']}','{$r['fntrans_status_code']}'),$i,$i);
                              } ";
                  }

                  ?>
                      document.getElementById('ftterm_payment').value = <?php echo $term_payment; ?>;
                  <?php
              }

              break;


            case "transno":
                if(RetRstField("SELECT fnid FROM txsales_invoice_hdr WHERE ftsales_invoice_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 "tgl":
                if ($_REQUEST['termpayment'] == '') {$_REQUEST['termpayment'] = 0; }
                $ssql="select date_add('".CDate($_REQUEST['tgl'])."',INTERVAL ".intval($_REQUEST['ftterm_payment'])." DAY)";
                $date=RetRstField($ssql);
                ?>
                document.getElementById('fddue_date').value='<?php echo CDateRev($date); ?>';
                <?php
                break;

            case "tgltempo":
                if ($_REQUEST['termpayment'] == '') {$_REQUEST['termpayment'] = 0; }
                $ssql="select date_add('".CDate($_REQUEST['fdtrans_date'])."',INTERVAL {$_REQUEST['ftterm_payment']} DAY)";
                $date=RetRstField($ssql);
                ?>
                document.getElementById('fddue_date').value='<?php echo CDateRev($date); ?>';
                <?php
                break;
           case "invoice":
                $ssql="SELECT  IFNULL(ftcustomer_code,'') AS ftcustomer_code  ,IFNULL(ftship_address,'') AS ftship_address
                          FROM `txdelivery_order_hdr`
                          WHERE ftdo_no='{$_REQUEST['ftdo_no']}'";

                $rs=mysql_query($ssql) ;
                $r=mysql_fetch_assoc($rs);

                $rs1=mysql_query("SELECT ftcustomer_code,ftcustomer_name,fttax_code,
                                    IFNULL(ftaddress,'') AS ftaddress,IFNULL(ftcity,'') AS ftcity,
                                    IFNULL(ftzip_code,'') AS ftzip_code,IFNULL(ftstate,'') AS ftstate,
                                    IFNULL(ftcountry,'') AS ftcountry FROM tlcustomer WHERE
                                    ftcustomer_code='{$r['ftcustomer_code']}'");

                $r1=mysql_fetch_assoc($rs1);
                $str=(!empty($r1['ftaddress']))? $r1['ftaddress']: "";

                if(!empty($r1['ftcity'])){
                    if(!empty($str)) $str.=" - ";
                        $str.=$r1['ftcity'];
                }

                ?>
                    document.getElementById("DivCtl_ftcustomer_code").innerHTML='<input type=hidden name=ftcustomer_code value=\'<?php echo $r['ftcustomer_code']; ?>\'><input readonly  style="background-color: #E0E0E0" size="30" type=text value=\'<?php echo "{$r['ftcustomer_code']}"; ?>\'>&nbsp;<font color="red" SIZE="2">-</font>&nbsp;&nbsp;<span id="span_ftcustomer_code" name="span_ftcustomer_code"> <?php echo $r1['ftcustomer_name']; ?></span><input type="hidden" id="hidden_ftcustomer_code" name="hidden_ftcustomer_code" size="100"  value="<?php echo $r1['ftcustomer_name']; ?>" readonly style="background-color: #E0E0E0" >';
                    document.getElementById('ftship_address').value='<?php echo js_esc($str);?>';
                    SetValueSelected('<?php echo $r1['fttax_code']; ?>',document.getElementById('fntax_code'));
                <?php

                $ssql="SELECT b.fddue_date, b.ftterm_payment, b.fntax_code, b.ftreff_no, b.ffdiscount,
                          b.fcdiscount, b.fcother_cost, b.ffvat, b.ftcurrency_code, b.fcrate,
                          b.fcvat, b.fcamount, a.fdtrans_date
                          FROM txdelivery_order_hdr a INNER JOIN txsales_order_hdr b
                          ON a.ftso_no=b.ftso_no
                          WHERE a.ftdo_no='{$_REQUEST['ftdo_no']}'
                          GROUP BY b.ftso_no";

                $rs=mysql_query($ssql) ;
                $r=mysql_fetch_assoc($rs);

                ?>
                    document.getElementById('fdtrans_date').value='<?php echo CDateRev($r['fdtrans_date']); ?>';
                    document.getElementById('fdvat_date').value='<?php echo CDateRev($r['fdtrans_date']); ?>';
                    document.getElementById('fddue_date').value='<?php echo CDateRev($r['fddue_date']); ?>';

                    document.getElementById('ftreff_no').value='<?php echo $r['ftreff_no']; ?>';
                    document.getElementById('ftterm_payment').value='<?php echo $r['ftterm_payment']; ?>';
                    document.getElementById('fntax_code').value='<?php echo $r['fntax_code']; ?>';
                    document.getElementById('ffdiscount').value='<?php echo number_format($r['ffdiscount'], 2); ?>';
                    document.getElementById('fcdiscount').value='<?php echo number_format($r['fcdiscount'], 2); ?>';
                    document.getElementById('fcother_cost').value='<?php echo number_format($r['fcother_cost'], 2); ?>';
                    document.getElementById('ftcurrency_code').value='<?php echo $r['ftcurrency_code']; ?>';
                    document.getElementById('fcrate').value='<?php echo number_format($r['fcrate'], 2); ?>';
                    document.getElementById('ffvat').value='<?php echo number_format($r['ffvat'], 2); ?>';
                    document.getElementById('fcvat').value='<?php echo number_format($r['fcvat'], 2); ?>';
                    document.getElementById('fcamount').value='<?php echo number_format($r['fcamount'], 2); ?>';
                <?php

                $html = GetInvoice();

                ?>
                    div = document.getElementById('txtHint0');
                    var m='<?php echo js_esc($html); ?>';
                    m= m.replace(/^\s*|\s*$/g,"");
                    div.innerHTML =m;
                    <!--reSize();-->
                    ElementCurrencyALL();

                    SalesInvoiceRecalQty0();
                    OnChangeDate_fdtrans_date();
                <?php

                $ajax_due="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&code=tgltempo&ajax=1&fdtrans_date=' + document.getElementById('fdtrans_date').value + '&ftterm_payment=' + document.getElementById('ftterm_payment').value + '&fddue_date=fddue_date');";
                echo $ajax_due;


               break;

               case "tipeinvoice":
                   $do=$_REQUEST['do_no'];
                   $do_1=substr($do,0,3);
                   if($do_1=='DOB')
                   {
                       ?>
                        document.getElementById('ftitem_type').value=0;
                        <?php
                   }
                   else
                   {
                       ?>
                        document.getElementById('ftitem_type').value=1;
                        <?php
                   }
              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);
                $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;
           }
           die("");
           exit;
    }

    function GetInvoice() {
        $x="";
        $ssql="SELECT a.fnindex as fnindex,a.ftitem_code,a.ftitem_name,a.ftprint_name,a.ftuom_code,a.ftstock_category_code,a.ftwarehouse_code,a.ftlocation_code,a.fnqty,
                0 as fcprice,0 as fcDisc,0 as fcamount,a.fnqty_base,a.ffformula
                FROM txdelivery_order_det a
                WHERE a.ftdo_no='{$_REQUEST['ftdo_no']}' ";

        $rs=mysql_query($ssql);

        $x.= "<table class='ewTable' id='TransactionDetail' name='TransactionDetail' border='0' width='100%' cellspacing='0' celpadding='0'>
                <tr class='ewTableHeader'>
                <td valign='top'>No</th>
                <td  valign='top'>Kode Barang</th>
                <td  valign='top'>Qty</th>
                <td  valign='top'>Satuan</th>
                <td  valign='top'>Kategori</th>
                <td  valign='top'>Harga</th>
                <td  valign='top'>Disc(%)</th>
                <td  valign='top'>Disc(Rp)</th>
                <td th valign='top'>Jml. Total</th>
                </tr>";

            $ssql="select ftcustomer_code
                  from  txdelivery_order_hdr
                  where ftdo_no='{$_REQUEST['ftdo_no']}'";
            $ftcustomer_code=RetRstField($ssql);

            $ssql="select ftso_no
                  from  txdelivery_order_hdr
                  where ftdo_no='{$_REQUEST['ftdo_no']}'";
            $ftso_no=RetRstField($ssql);

            $ssql=" select ftcustomer_type_code
                    from tlcustomer
                    where ftcustomer_code='{$ftcustomer_code}'";
            $ftcustomer_type_code=RetRstField($ssql);

            $ssql=" select ftharga_potong_pajak
                    from tlcustomer
                    where ftcustomer_code='{$ftcustomer_code}'";
            $ftharga_potong_pajak=RetRstField($ssql);

            if(!RetRstField("select fnid from tlcustomer where ftfiktif='1' and   ftcustomer_code='{$ftcustomer_code}'")) {
                $mmm="0";
            }
            else {
                $mmm="1";
            }


        $i=0;
        while($r = mysql_fetch_assoc($rs))
         {
            if($mmm=="1") {
                $ftitem_name=$r['ftprint_name'];
                $ftprint_name=$r['ftprint_name'];
            }
            else {
                 $ftitem_name=$r['ftitem_name'];
                 $ftprint_name=$r['ftprint_name'];
            }

            if ($i%2) {
                    $class1="ewTableRow";
                    $class2="ISGridTDeven";
            } else {
                    $class1="ewTableRow";
                    $class2="ISGridTDodd";
            }

            /*$ssql="select fcselling_price
                   from tlselling_price
                   where ftitem_code='{$r[ftitem_code]}'
                   and ftcustomer_type_code='{$ftcustomer_type_code}'";

            $fcselling_price=RetRstField($ssql);
            if( $ftharga_potong_pajak=="1") {
                 #$fcselling_price=$fcselling_price- $fcselling_price*0.1;
                 $fcselling_price=round($fcselling_price/(1.1),2);
            }
            else {
                 $fcselling_price=$fcselling_price;
            }

            $ssql="select ffselling_discount
                   from tlselling_price
                   where ftitem_code='{$r[ftitem_code]}'
                   and ftcustomer_type_code='{$ftcustomer_type_code}'";

            $ffselling_discount=RetRstField($ssql);*/

            $ssql="select fcprice, ffdiscount, fcdiscount
                   from txsales_order_det
                   where ftso_no='{$ftso_no}'
                   AND ftitem_code='{$r[ftitem_code]}'";
            $mysql2=mysql_query($ssql);
            $r2=mysql_fetch_assoc($mysql2);
            $fcselling_price=$r2["fcprice"];
            $ffselling_discount=$r2["ffdiscount"];
            $fcselling_discount=(($r['fnqty'] * $fcselling_price) * $ffselling_discount) / 100;

             $x.= "<tr class='$class1'>
             <td class='$class2'><input readonly style='background-color: #E0E0E0'  type='text' name='fnindexD[]' id='fnindexD$i' value='" . $r['fnindex'] . "' size=3></td>
             <td class='$class2'><input  type='hidden' name='ftlocation_codeD[]' id='ftlocation_codeD$i' value='" . $r['ftlocation_code'] . "'>
                                <input   type='hidden' name='ftwarehouse_codeD[]' id='ftwarehouse_codeD$i' value='" . $r['ftwarehouse_code'] . "' >
                                <input readonly style='background-color: #E0E0E0'  type='text'  size='25'  name='ftitem_codeD[]' id='ftitem_codeD$i' value='" . $r['ftitem_code'] . "'  size=16>
                                <input  type='hidden' name='ftprint_nameD[]' id='ftprint_nameD$i' value='" .$ftprint_name . "'>
                                <input  type='hidden' name='ftitem_nameD[]' id='ftitem_nameD$i' value='" .$ftitem_name . "'>{$ftitem_name}</td>
             <td class='$class2'><input readonly style='background-color: #E0E0E0'  onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: right;\" type='text' name='fnqtyD[]' id='fnqtyD$i' value='" . $r['fnqty'] . "'   size=10>
                                <input type='hidden' name='fnqty_baseD[]' id='fnqty_baseD' value='" . $r['fnqty_base'] . "'   size=10>
                                <input type='hidden' name='ffformulaD[]' id='ffformulaD' value='" . $r['ffformula'] . "'   size=10></td>
             <td class='$class2'><input readonly style='background-color: #E0E0E0'  type='text' name='ftuom_codeD[]' id='ftuom_codeD$i' value='" . $r['ftuom_code'] . "'   size=3></td>
             <td class='$class2'><input readonly style='background-color: #E0E0E0'  type='text' name='ftstock_category_codeD[]' id='ftstock_category_codeD$i' value='" . $r['ftstock_category_code'] . "'   size=10></td>
             <td class='$class2'><input type='text' onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: right;\"  onChange=\"SalesInvoiceRecalPrice();\" name='fcpriceD[]' id='fcpriceD$i' value='" . $fcselling_price . "'  size=15><input type=hidden name='fcpriceListD[]'  id='fcpriceListD$i' value='{$fcselling_price}'>  </td>
             <td class='$class2'><input type='text' onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: right;\"  onChange=\"SalesInvoiceRecalDiscDetailPercent($i);\"   name='ffdiscountD[]' id='ffdiscountD$i' value='" . $ffselling_discount . "' size=15></td>
             <td class='$class2'><input readonly  onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: right;\" type='text'  onChange=\"SalesInvoiceRecalDiscDetailRp($i);\"  name='fcdiscountD[]' id='fcdiscountD$i' value='". $fcselling_discount ."'  size=15></td>
             <td class='$class2'><input type='text' readonly style='background-color: #E0E0E0;text-align: right;' onblur=\"ElementCurrencyOnBlur(this);\" onfocus=\"ElementCurrencyOnBlur(this); this.select();\"  name='fcamountD[]' id='fcamountD$i' value='0'></td>
             </tr>";

             $i++;
         }
        $x.= "</table>
        <input id='TransactionDetailCount' name='TransactionDetailCount' value='$num_rows' type='hidden'>
       ";
        return $x;
    }

    // filter only sales transaction if login by user
    $ftsales_code = RetRstField("Select ifnull(ftsales_code,'') FROM tluser WHERE ftusername = '{$_SESSION['username']}' ");
    if ($ftsales_code != "") { $sql_sales = " AND a.ftsales_code = '{$ftsales_code}' "; }

    // filter warehouse code based on login user
    $ftwarehouse_code = RetRstField("Select ifnull(ftwarehouse_code,'') FROM tluser WHERE ftusername = '{$_SESSION['username']}' ");
    if ($ftwarehouse_code != "") {
        $str_warehouse = explode(",", $ftwarehouse_code);
        $sql_where = "''";

        foreach($str_warehouse as $item) {
            $sql_where = $sql_where . ",'{$item}'";
        }
        $sql_where = " AND e.ftwarehouse_code IN ({$sql_where})";
    }

    $objAxpDB->ViewName("Daftar Sales Invoice Pameran");
    //$objAxpDB->ViewTable("vSASalesInvoice");
    // $objAxpDB->ViewSQL("SELECT DISTINCT
    //                     A.fnid,
    //                     A.fdtrans_date,
    //                     A.ftsales_invoice_no,
    //                     CONCAT(A.ftcustomer_code,' - ',d.ftcustomer_name) as ftcustomer_name,
    //                     A.fddue_date,
    //                     A.fcamount,
    //                     A.fcoutstanding_amount,
    //                     C.fttrans_status_name AS fnpayment_status,
    //                     e.ftwarehouse_code,
    //                     B.fttrans_status_name
    //                     FROM txsales_invoice_hdr  A
    //                     inner join txsales_invoice_det e on a.ftsales_invoice_no = e.ftsales_invoice_no
    //                     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.fnpayment_status=C.fntrans_status_code AND C.fntrans_group_code=1
    //                     INNER JOIN tlcustomer d ON a.ftcustomer_code = d.ftcustomer_code
    //                     WHERE A.fnsaldo_awal=0
    //                     AND (('{$_SESSION['strbranch']}' <> 'PUSAT' AND a.ftbranch_code = '{$_SESSION['strbranch']}') OR '{$_SESSION['strbranch']}' = 'PUSAT')
    //                     ORDER BY  A.fdtrans_date DESC,ftsales_invoice_no DESC");

                    $objAxpDB->ViewSQL("SELECT DISTINCT
                          A.fnid,
                          A.fdtrans_date,
                          A.ftsales_invoice_no,
                          CONCAT(A.ftcustomer_code,' - ',a.ftcustomer_name) as ftcustomer_name,
                          IF(ftsales_type_code='3','Pameran',''),
                          A.fddue_date,
                          A.fcamount,
                          A.fcoutstanding_amount,
                          C.fttrans_status_name AS fnpayment_status,
                          B.fttrans_status_name
                          FROM txsales_invoice_hdr  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.fnpayment_status=C.fntrans_status_code AND C.fntrans_group_code=1
                          INNER JOIN tlcustomer d ON a.ftcustomer_code = d.ftcustomer_code
                          WHERE A.fnsaldo_awal=0  AND A.ftsales_type_code=3
                          AND (('{$_SESSION['strbranch']}' <> 'PUSAT' AND a.ftbranch_code = '{$_SESSION['strbranch']}') OR '{$_SESSION['strbranch']}' = 'PUSAT')
                          ORDER BY  A.fdtrans_date DESC,ftsales_invoice_no DESC");


    $objAxpDB->ViewCaption("ID, Tgl. Invoice, No. Invoice Pameran, Nama Customer,Tipe Penjualan, Tgl. Jatuh Tempo, Total Invoice, Sisa Pembayaran, Status Pembayaran, Status , Status");
    $objAxpDB->ViewPeriod(TRUE);

    $ajaxinvoice="ajax_do_grid0('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=invoice&ftdo_no='+ document.getElementById('ftdo_no').value);";
    $ajaxKurs="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=idr&ftcurrency_code='+ document.getElementById('ftcurrency_code').value);";

    if(!$_REQUEST['id']) {

            $strAutoNumber1=" ajax_do('query2.php?code=2&ftdo_no=' + document.getElementById(\"ftdo_no\").value + '&ftmodule_code=TN006&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftsales_invoice_no&no_trans=ftsales_invoice_no&table_name=txsales_invoice_hdr&ftbranch_code={$_SESSION['strbranch']}');";
            $strAutoNumber2=" ajax_do('query2.php?code=2&ftdo_no=' + document.getElementById(\"ftdo_no\").value + '&ftmodule_code=TN021&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftsales_invoice_no&no_trans=ftsales_invoice_no&table_name=txsales_invoice_hdr&ftbranch_code={$_SESSION['strbranch']}');";
            $strAutoNumber3=" ajax_do('query2.php?code=2&ftdo_no=' + document.getElementById(\"ftdo_no\").value + '&ftmodule_code=TN021&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftsales_invoice_no&no_trans=ftsales_invoice_no&table_name=txsales_invoice_hdr&ftbranch_code={$_SESSION['strbranch']}');";

            // $ajaxTransDate="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=tgl&tgl=' + document.getElementById(\"fdtrans_date\").value + '&ftterm_payment=+ document.getElementById(\"ftterm_payment\").value +');";
            $ajax_tipe="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=tipeinvoice&do_no=' + document.getElementById(\"ftdo_no\").value + '');";
            $ajax_due="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&code=tgltempo&ajax=1&fdtrans_date=' + document.getElementById('fdtrans_date').value + '&ftterm_payment=' + document.getElementById('ftterm_payment').value + '&fddue_date=fddue_date');";

            $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                     if(document.getElementById(\"fntax_code\").value==0) {
                                           {$strAutoNumber1}
                                     }
                                     else if(document.getElementById(\"fntax_code\").value==1) {
                                           {$strAutoNumber2}
                                     }
                                     else if(document.getElementById(\"fntax_code\").value==2) {
                                           {$strAutoNumber3}
                                     }

                                     document.getElementById(\"fdvat_date\").value=document.getElementById(\"fdtrans_date\").value;
                                     ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=tgl&tgl=' + document.getElementById('fdtrans_date').value + '&ftterm_payment=' + document.getElementById('ftterm_payment').value);

                                     {$ajaxTransDate}{$ajax_tipe}

                                }
                                 OnChangeDate_fdtrans_date();
                                 document.getElementById('ftcurrency_code').value='IDR';
                                 {$ajaxKurs}";
            $objAxpDB->FormJS($strAutoNumber0);
            $objAxpDB->Lookup("ftdo_no", "SELECT ftdo_no,ftreference FROM txdelivery_order_hdr WHERE fnstatus=1", "ftdo_no", "ftreference", -1,true);

    } else {
            $objAxpDB->FormJS("ElementCurrencyALL();");
    }

    $objAxpDB->FormName("Sales Invoice Pameran");
    $objAxpDB->FormSQL("SELECT ftsales_invoice_no,  ftdo_no, ftcustomer_code, ftship_address, ftitem_type, fdtrans_date,  ftterm_payment, fddue_date, ftcurrency_code, fcrate, fntax_code,ftreff_no, fnpayment_status, ftsales_type_code,fdvat_date,ftvat_no,ftnotes, fcpayment_amount, fcoutstanding_amount, fcsub_amount, ffdiscount, fcdiscount, fcother_cost, ffvat, fcvat, fcamount, fnstatus, fcreturn_amount, ftcustomer_name FROM txsales_invoice_hdr");
    $objAxpDB->FormCaption("No. Invoice,  No. DO, Customer, Alamat, Tipe Invoice, Tgl. Invoice,  Syarat Pembayaran, Tgl. Jatuh Tempo, Mata Uang, Kurs, Kode Pajak, Referensi, Status Pembayaran,Tipe Penjualan,Tgl. Faktur Pajak, No. Faktur Pajak, Catatan, Jumlah Terbayar, Sisa Pembayaran,   Sub Total, Discount (%), Discount(Rp), Biaya Lain-lain, Pajak (%), Pajak (Rp), Grand Total, Status, Jumlah Retur");
    $objAxpDB->FormPosition("TL, HD, TL, TL, HD, TR, TR, TR, TR, TR, TR, TL, TL,TL, TR, TR, BL, BL, BL, BR, BR, BR, BR, BR, BR, BR, BR, BL, HD");
    //  //
    // $objAxpDB->FormName("Sales Invoice");
    // $objAxpDB->FormSQL("SELECT ftsales_invoice_no,  ftdo_no, ftcustomer_code, ftship_address, ftitem_type, fdtrans_date,  ftterm_payment, fddue_date, ftcurrency_code, fcrate, fntax_code,ftreff_no, fnpayment_status, fdvat_date, ftvat_no,ftnotes, fcpayment_amount, fcoutstanding_amount, fcsub_amount, ffdiscount, fcdiscount, fcother_cost, ffvat, fcvat, fcamount, fnstatus, fcreturn_amount FROM txsales_invoice_hdr");
    // $objAxpDB->FormCaption("No. Invoice,  No. DO, Customer, Alamat, Tipe Invoice, Tgl. Invoice,  Syarat Pembayaran, Tgl. Jatuh Tempo, Mata Uang, Kurs, Kode Pajak, Referensi, Status Pembayaran,Tgl. Faktur Pajak, No. Faktur Pajak, Catatan, Jumlah Terbayar, Sisa Pembayaran,   Sub Total, Discount (%), Discount(Rp), Biaya Lain-lain, Pajak (%), Pajak (Rp), Grand Total, Status, Jumlah Retur");
    // $objAxpDB->FormPosition("TL, HD, TL, TL, HD, TR, TR, TR, TR, TR, TR, TL, TL, TR, TR, BL, BL, BL, BR, BR, BR, BR, BR, BR, BR, BR, BL");


    $objAxpDB->FormScript(20, "onChange=\"SalesInvoiceRecalDiscPercent();\"");
    $objAxpDB->FormScript(21, "onChange=\"SalesInvoiceRecalDiscRp();\"");
    $objAxpDB->FormScript(22, "onChange=\"SalesInvoiceRecalQty0();\"");
    $objAxpDB->FormScript(23, "onChange=\"SalesInvoiceRecalQty0();\"");
    $objAxpDB->FormScript(24, "onChange=\"SalesInvoiceRecalQty0();\"");
    $objAxpDB->FormScript(10, "onChange=\"OnChangeDate_fdtrans_date();\"");
    $objAxpDB->FormScript(5, "onChange=\"{$ajax_due}\"");
    $objAxpDB->FormScript(6, "onChange=\"{$ajax_due}\"");
    $objAxpDB->FormScript(8, "OnChange=\"{$ajaxKurs}\"");


    //ajax customer dan alamat
    $ajaxcustomer="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=customer&ftcustomer_code='+ document.getElementById('ftcustomer_code').value+'&tgl='+ document.getElementById('fdtrans_date').value);";
    $objAxpDB->FormScript(2, "onChange=\"{$ajaxcustomer}\"");
    //

    // $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code,ftprint_name,fnqty, ftuom_code, fcprice, ffdiscount, fcdiscount, fcamount, fnqty_base, ffformula FROM txsales_invoice_det");
    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code,ftlocation_code,ftwarehouse_code, fnqty_available, fnqty,ftuom_code,ftprint_name, fcprice ,fcamount,fnqty_base,ffformula,ftitem_name  FROM txsales_invoice_det");
    $objAxpDB->GridCaption(0, "No., Kode Barang, Lokasi, Gudang, Qty Tersedia, Qty, Satuan, Keterangan Barang,Harga, Jumlah Total ,  Qty Base, Formula");
    $objAxpDB->GridDelJS(0,"SalesInvoiceRecalQty0();");
    $objAxpDB->GridSize(0, "1,10,5,10,5,5,5,,,,5,5");


    // ajax formula dan quantity base / menkalkulasi
    $ajaxFormula="ajax_do('penjualan.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);";
    $ajax1="onChange=\"SalesInvoiceRecalQty0;SalesInvoiceRecalDiscDetailPercent(_N_);{$ajaxFormula}\"  ";
    $objAxpDB->GridScript(0,5, $ajax1);

    // KLIK SATUAN
    // ajax formula dan quantity base / menkalkulasi
    $ajaxFormula="ajax_do('penjualan.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);";
    $ajax1="onChange=\"SalesInvoiceRecalQty0;SalesInvoiceRecalDiscDetailPercent(_N_);{$ajaxFormula}\"  ";
    $objAxpDB->GridScript(0,6, $ajax1);

    //
    $ajaxItem="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=item&ftitem_code=' + this.value + '&index=_N_&ftcustomer_code='+ document.getElementById('ftcustomer_code').value);";
    $objAxpDB->GridScript(0,1," onChange=\"{$ajaxItem}\"");

    //

    //gudang dan lokasi
    $ajaxLokasi="OnChange=\"ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=lokasi&index=_N_&ftlocation_code='+this.value);{$ajaxQtyAvail}\"";
    $objAxpDB->GridScript(0,2, $ajaxLokasi);
    //

    //
    // $ajaxSellingPrice="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=selling_price&index=_N_&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value);";
    $ajax1=" onChange=\"SalesInvoiceRecalDiscDetailPercent(_N_);\"  ";
    $objAxpDB->GridScript(0,8, $ajax1);
    //

    $objAxpDB->Lookup("ftcurrency_code", "SELECT  ftsimbol, ftdescription  FROM tlcurrency_hdr ORDER BY ftsimbol", "ftsimbol", "ftdescription", 0, True);
    $objAxpDB->Lookup("ftpayment_method", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 8", "fntrans_status_code", "fttrans_status_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("fntax_code", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 15", "fntrans_status_code", "fttrans_status_name", -1,true);
    $objAxpDB->Lookup("fnpayment_status", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 1", "fntrans_status_code", "fttrans_status_name", 1,true);
    $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr", "ftitem_code", "ftitem_name", -1,false);
    $objAxpDB->Lookup("ftitem_type","SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fttrans_group_name='tipe barang invoice'","fntrans_status_code","fttrans_status_name",1,true);
    $objAxpDB->Lookup("ftcustomer_code","SELECT ftcustomer_code, ftcustomer_name FROM tlcustomer","ftcustomer_code","ftcustomer_name", -1,false);
    $objAxpDB->Lookup("ftterm_payment", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 75 ORDER BY fntrans_status_code ", "fntrans_status_code", "fttrans_status_name", 1,true);
    $objAxpDB->Lookup("ftlocation_code", "SELECT ftlocation_code, ftlocation_name FROM tllocation WHERE fnstatus=1 ", "ftlocation_code", "ftlocation_name", -1,true);
    $objAxpDB->Lookup("ftuom_code", "SELECT ftuom_code, ftuom_name FROM tluom", "ftuom_code", "ftuom_name", -1,true);
    //tipe penjualan
    $objAxpDB->Lookup("ftsales_type_code", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 56 AND fnactive_status = 1 ", "fntrans_status_code", "fttrans_status_name", 2,true);
    //

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

    if ($objAxpDB->GetStatus("id") == "") {

           $objAxpDB->ButtonShow("baru");

    } elseif ($objAxpDB->GetStatus("id") == 0) {

      $objAxpDB->FormLocked("1,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,1,0,0,0,0,0,1,1,1");
      $objAxpDB->GridLocked(0, "1, 0, 0, 1, 1, 0, 0, 0, 0, 1,1,1");
      $objAxpDB->GridHidden(0, "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,1");
      $objAxpDB->GridAllowAddNew(0,true);
      $objAxpDB->GridAllowDelete(0,true);
      $objAxpDB->ButtonShow("reset, simpan, tutup");

    } elseif (($objAxpDB->GetStatus("fnstatus") == 1) OR ($objAxpDB->GetStatus("fnstatus") == 2)) {

      if(isset($_REQUEST['revisi']))
      {
          // $price_history = " <select id='price_historyD0_N_' name='price_historyD0_N_' readyonly style='background-color:silver;'> </select>";
          // $objAxpDB->GridLayout(0,9, "$price_history");

          $objAxpDB->FormLocked("1,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,1,0,0,0,0,0,1,1,1");
          $objAxpDB->GridLocked(0, "1, 0, 0, 1, 1, 0, 0, 0, 0, 1,1,1");
          $objAxpDB->GridHidden(0, "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,1");
          $objAxpDB->GridAllowAddNew(0, TRUE);
          $objAxpDB->GridAllowDelete(0, TRUE);

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

          $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftdisplay_name AS ftitem_name FROM vsitem_part WHERE fnstatus = 1 ORDER BY ftdisplay_name ", "ftitem_code", "ftitem_name", -1,false);

      } else {

            $objAxpDB->FormLocked("1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1");

            $objAxpDB->GridLocked(0, "1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
            $objAxpDB->GridHidden(0, "0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0,1");
            $objAxpDB->Lookup("ftcustomer_code", "SELECT ftcustomer_code, ftcustomer_name FROM tlcustomer", "ftcustomer_code", "ftcustomer_name", 1,false);
            $objAxpDB->ButtonShow("baru,cetak,revisi,batal,tutup");

     }

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

    $objAxpDB->Process();

    $objAxpDB->WriteHtmlTable();
