<?php       
    // Script Ajax di form bersangkutan
    if($_REQUEST['ajax']) {
       switch($_REQUEST['code']) {
            // cek dan validasi no transaksi sebelum simpan    
            case "transno":
                // Validasi gudang dan lokasi untuk settingan SI Auto DO
                if($_SESSION["SOAutoDOPrint"] || $_SESSION["SOAutoDOSave"]) {
                    for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                        if ((CCur($_REQUEST['fnqty_baseD0'][$j]) > 0) && (CCur($_REQUEST['ftwarehouse_codeD0'][$j])=="")) {
                            echo "<script>
                                    alert('Gudang harus diisi. ');
                                    document.getElementsByName('ftwarehouse_codeD0[]')[j].focus();
                                  </script>";
                            die("");
                        }

                        if ((CCur($_REQUEST['fnqty_baseD0'][$j]) > 0) && (CCur($_REQUEST['ftlocation_codeD0'][$j])=="")) {
                            echo "<script>
                                    alert('Lokasi harus diisi. ');
                                    document.getElementsByName('ftlocation_codeD0[]')[j].focus();
                                  </script>";
                            die("");
                        }
                    }   
                }    
                // echo "alert('".count($_REQUEST['fnindexD0'])."');";
                 
                // Validasi tidak boleh qty minus
                if (($_SESSION["SOAutoDOPrint"] || $_SESSION["SOAutoDOSave"]) && (!$_SESSION["QtyMinus"])) {
                    for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                        if ((CCur($_REQUEST['fnqtyD0'][$j]) > CCur($_REQUEST['fnqty_availableD0'][$j])) && ($_REQUEST['ftstock_category_codeD0'][$j] != 'srv')) {
                            echo "<script>
                                    alert('Qty tidak boleh lebih dari qty tersedia. ');
                                    document.getElementsByName('fnqtyD0[]')[j].focus();
                                  </script>";
                            die("");
                        }    
                    }  
                }
                
                if(RetRstField("SELECT fnid FROM txsales_order_hdr WHERE ftso_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 {
                    // cek tanggal transaksi tidak boleh lebih besar dari hari ini   
                    $date_today=RetRstField("SELECT DATE_FORMAT(NOW(), '%Y-%m-%d')");
                    $date_tomorrow=RetRstField("SELECT DATE_FORMAT(CURDATE() + INTERVAL 1 DAY, '%d')");
                    
                    // if tomorrow is first day of the month
                    /* if ($date_tomorrow == "01") {
                        $day=RetRstField("WEEKDAY(CURDATE())");
                        // if friday
                        if ($day == "4") {
                            $next_monday_date = RetRstField("SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL (9 - IF(DAYOFWEEK(CURDATE())=1, 8, DAYOFWEEK(CURDATE()))) DAY), '%d') AS NEXTMONDAY");
                        }
                        
                    } */
                    
                    ?>
                    var date1 = new Date("<?php echo $date_today; ?>");
                    var date2 = new Date("<?php echo CDate($_REQUEST['fdtrans_date']); ?>");
                    var date_next = new Date("<?php echo $date_tomorrow; ?>");
                    <!-- var timeDiff = Math.abs(date2.getTime() - date1.getTime()); -->
                    var timeDiff = date2.getTime() - date1.getTime();
                    var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));  
                    
                    <!-- hanya boleh simpan di tgl hari ini atau tgl 1 di awal bulan berikutnya -->
                    if ((diffDays > 1) && (date_next != '01'))  {
                        alert('Tanggal Transaksi tidak boleh lebih dari hari ini');
                        document.getElementById('fdtrans_date').value='<?php echo CDateRev($date_today); ?>';
                    } else {
                        submitbutton('Simpan','','','','');
                    }
                    <?php
                }
                break;
            
            // cek credit limit
            case "creditlimit":
                $ssql="SELECT SUM(a.fcdebit) AS fcdebit, SUM(a.fccredit) AS fccredit
                        FROM (
                            SELECT 0 AS fcdebit, SUM(a.fcdebit - a.fccredit) AS fccredit
                            FROM (
                                SELECT SUM(fcamount) AS fcdebit, 0 AS fccredit
                                FROM txsales_order_hdr
                                WHERE fnstatus=1 AND ftcustomer_code='{$_REQUEST["ftcustomer_code"]}'
                                UNION ALL
                                SELECT 0 AS fcdebit, SUM(a.fcpayment_amount) AS fccredit
                                FROM txsales_invoice_hdr a
                                INNER JOIN txdelivery_order_hdr b ON a.ftdo_no=b.ftdo_no
                                INNER JOIN txsales_order_hdr c ON b.ftso_no=c.ftso_no
                                WHERE a.fnstatus=1 AND c.fnstatus=1 AND c.ftcustomer_code='{$_REQUEST["ftcustomer_code"]}'
                                GROUP BY b.ftcustomer_code
                            ) a
                            UNION ALL
                            SELECT fccredit_limit AS fcdebit, 0 AS fccredit FROM tlcustomer WHERE ftcustomer_code='{$_REQUEST["ftcustomer_code"]}'
                        ) a";
                // execute sql            
                $rs=mysql_query($ssql);
                $r=mysql_fetch_assoc($rs);

                if(((float)$r["fcdebit"] < ((float)$r["fccredit"] + (float)CCur($_REQUEST["fcamount"]))) && (float)$r["fcdebit"] > 0) {
                    $outstanding=$r["fcdebit"] - $r["fccredit"];
                    if($outstanding < 0) $outstanding=0;                    
                        ?>
                        alert('Customer sudah melebihi batas kredit,\nsaat ini credit yang masih dimiliki sebesar\n<?php echo $outstanding; ?>.');
                        <?php
                } else {
                    ?>
                    submitbutton('Final','','','','');
                    <?php
                }
                break;
                    
            // set tanggal due date    
            case "tgl":
                if ($_REQUEST['termpayment'] == '') { $_REQUEST['termpayment'] = 0; }
                $ssql="select date_add('".CDate($_REQUEST['tgl'])."',INTERVAL {$_REQUEST['termpayment']} DAY)";
                $date=RetRstField($ssql);
                ?>
                document.getElementById('fddue_date').value='<?php echo CDateRev($date); ?>';
                <?php

                break;

            // set tanggal jatuh tempo    
            case "term_payment":
                if ($_REQUEST['termpayment'] == '') {$_REQUEST['termpayment'] = 0; }
                $ssql="select date_add('".CDate($_REQUEST['tgl'])."',INTERVAL {$_REQUEST['termpayment']} DAY)";
                $date=RetRstField($ssql);
                ?>
                document.getElementById('fddue_date').value='<?php echo CDateRev($date); ?>';
                <?php

                break;

            // Set keterangan dan data customer    
            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('ftsales_code').value='<?php echo js_esc($r['ftsales_code']); ?>';
                        document.getElementById('ftship_address').value='<?php echo js_esc($r['ftship_address']); ?>';
                        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 = "1";
                        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 = 7) OR (fntrans_status_code='{$term_payment}')) ORDER BY fntrans_status_code ";
                    $ssql="SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 75 AND fnactive_status=1 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;

            // Set keterangan Pajak   
            case "pajak":
                ?>
                    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.00";
                    } else if (document.getElementById('fntax_code').value == 2) {
                        document.getElementById('ffvat').value = "10.00";
                    }
                    SalesOrderRecalQty0();

                <?php

                break;
            
            // Set Nilai Kurs
            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();
                  ElementCurrencyOnBlur(document.getElementById('fcdiscount'));

                <?php
                break;
            
            // Set Satuan, Location, Qty OnHand dan Harga Jual
            case "item":
                ?>
                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);
                        }
                }
                
                var ObjSelect2=document.getElementById('<?php echo $_REQUEST['ObjLokasi2']; ?>');
                var len2 = ObjSelect2.length;
                for (var idx=0; idx < len2; idx++)
                {
                        if (ObjSelect2.options[ObjSelect2.length-1].value!='') {
                                  ObjSelect2.remove(ObjSelect2.length-1);
                        }
                }
                
                <?php
                // set pulldown satuan
                $ftUOM_Code="SELECT UCASE(a.ftuom_code) AS ftuom_code,UCASE(b.ftuom_name) AS ftuom_name FROM tlstock_hdr a
                                    INNER JOIN tluom b ON a.ftuom_code=b.ftuom_code
                                    WHERE a.ftitem_code='{$_REQUEST['ftitem_code']}'
                                    GROUP BY a.ftuom_code
                                    UNION ALL
                                    SELECT UCASE(a.ftuom_code_multi) AS ftuom_code,UCASE(b.ftuom_name) AS ftuom_name FROM tlstock_multi_det a
                                            INNER JOIN tluom b ON a.ftuom_code_multi=b.ftuom_code
                                    WHERE a.ftitem_code='{$_REQUEST['ftitem_code']}'
                                    GROUP BY a.ftuom_code_multi
                                        ";
                $rs=mysql_query($ftUOM_Code);
                $i=0;
                while($r=mysql_fetch_assoc($rs))
                {
                        $i++;
                        echo " try {
                                         ObjSelect.add(new Option('{$r['ftuom_code']}--{$r['ftuom_name']}','{$r['ftuom_code']}'),null,null);
                                  } catch(ex) {
                                         ObjSelect.add(new Option('{$r['ftuom_code']}--{$r['ftuom_name']}','{$r['ftuom_code']}'),$i,$i);
                                  } ";
                }

                // normal warehouse
                $fnwarehouse_type = "1 ";
                
                // System Setting
                if($_SESSION["SOAutoDOPrint"] || $_SESSION["SOAutoDOSave"] ) { 
                    // set dan tampilkan pulldown Lokasi & Qty Available               
                    $ftlocation="   SELECT UCASE(a.ftlocation_code) AS ftlocation_code,UCASE(a.ftlocation_name) AS ftlocation_name, b.fnqty_available, IFNULL(SUM(c.fnqty),0) AS fnqty,
                                        (b.fnqty_available - IFNULL(SUM(c.fnqty),0)) AS fnqty_onhand
                                    FROM tllocation a LEFT JOIN tlstock_det b ON a.ftlocation_code=b.ftlocation_code
                                    LEFT JOIN ( SELECT SUM(fnqty) AS fnqty, c.ftlocation_code, c.ftitem_code FROM txsales_order_det c 
                                                    INNER JOIN txsales_order_hdr d ON c.ftso_no = d.ftso_no AND d.fnstatus = 1
                                                    WHERE c.ftitem_code='{$_REQUEST['ftitem_code']}'
                                              ) c ON c.ftitem_code = b.ftitem_code AND c.ftlocation_code = a.ftlocation_code
                                    LEFT JOIN tlwarehouse d ON d.ftwarehouse_code = a.ftwarehouse_code 
                                    WHERE b.ftitem_code='{$_REQUEST['ftitem_code']}' AND d.fnwarehouse_type = '{$fnwarehouse_type}' AND a.fnstatus = 1 
                                    GROUP BY a.ftlocation_code, a.ftlocation_name         
                                    HAVING b.fnqty_available > 0 
                                ";
                    // echo $ftlocation;                
                    $rs=mysql_query($ftlocation);
                    $i=0;
                    while($r=mysql_fetch_assoc($rs))
                    {
                            $i++;

                            if ($r['fnqty'] != 0) { $strpending = " ,Hold: " . $r['fnqty']; }

                            echo " try {
                                             ObjSelect2.add(new Option('{$r['ftlocation_code']}--{$r['ftlocation_name']} (Qty: {$r['fnqty_onhand']} $strpending)','{$r['ftlocation_code']}'),null,null);
                                      } catch(ex) {
                                             ObjSelect2.add(new Option('{$r['ftlocation_code']}--{$r['ftlocation_name']} (Qty: {$r['fnqty_onhand']} $strpending)','{$r['ftlocation_code']}'),$i,$i);
                                      } ";
                    }
                } 
                
                $ftUOM_Code1=RetRstField("SELECT UCASE(ftUOM_Code) FROM tlstock_hdr WHERE ftitem_code='{$_REQUEST['ftitem_code']}'");
                $ftstock_category_code=RetRstField("SELECT ftstock_category_code FROM tlstock_hdr WHERE ftitem_code='{$_REQUEST['ftitem_code']}'");
                $fcprice=RetRstField("SELECT fcselling_price FROM tlselling_price WHERE ftitem_code='{$_REQUEST['ftitem_code']}' AND ftcurrency_code='{$_REQUEST['ftcurrency_code']}'");
                $ftdiscount=RetRstField("SELECT ffselling_discount FROM tlselling_price WHERE ftitem_code='{$_REQUEST['ftitem_code']}' AND ftcurrency_code='{$_REQUEST['ftcurrency_code']}'");
                //$ftcurrency_code=RetRstField("SELECT ftcurrency_code FROM tlselling_price  WHERE ftitem_code='{$_REQUEST['ftitem_code']}'");
                $ffdefault='1';
                
                ?>
                    document.getElementById('fnqtyD0<?php echo $_REQUEST['index']; ?>').value='0';
                    document.getElementById('fnqty_baseD0<?php echo $_REQUEST['index']; ?>').value='0';
                    document.getElementById('ftuom_codeD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ftUOM_Code1; ?>';
                    document.getElementById('ftstock_category_codeD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ftstock_category_code; ?>';
                    document.getElementById('fcold_priceD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $fcprice; ?>';
                    document.getElementById('fcpriceD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $fcprice; ?>';
                    document.getElementById('ftdiscountD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ftdiscount; ?>';
                    ElementCurrencyALL();    
                    
                <?php
                die("");
                break;
            
            // 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 "gudang":
                $ssql="SELECT ftlocation_code,ftlocation_name FROM tllocation WHERE ftwarehouse_code='{$_REQUEST['ftwarehouse_code']}'";
//                $ssql="SELECT ftlocation_code,ftlocation_name FROM tllocation WHERE ftlocation_code='GBJ'";
                $rs=mysql_query($ssql);
                ?>

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

                    var len = ObjSelect.length;

                    for (var idx=0; idx < len; idx++)
                    {
                            if (ObjSelect.options[ObjSelect.length-1].value!='') {
                                  ObjSelect.remove(ObjSelect.length-1);
                            }
                    }
                <?php
                $i=0;
                while($r=mysql_fetch_assoc($rs)) {
                    $i++;
                    echo " try {
                                ObjSelect.add(new Option('{$r['ftlocation_code']}--{$r['ftlocation_name']}','{$r['ftlocation_code']}'),null,null);
                            } catch(ex) {
                                ObjSelect.add(new Option('{$r['ftlocation_code']}--{$r['ftlocation_name']}','{$r['ftlocation_code']}'),$i,$i);
                            } ";
                }

                ?>
                    document.getElementById('<?php echo $_REQUEST['objQty']; ?>').value=0;
                <?php
                die("");
                break;
                
            case "lokasi":
                $ssql="SELECT ftwarehouse_code FROM tllocation 
                        WHERE ftlocation_code='{$_REQUEST['ftlocation_code']}'
                        ";
                $ftwarehouse_code=RetRstField($ssql);

                ?>
                if (<?php echo $_REQUEST['index']; ?> != 1) {
                    // make sure variable index - 1 is exists , effect from delete row 
                    var i;
                    for (i = <?php echo $_REQUEST['index']; ?> - 1; i >= 0 ; i--) {
                        if (document.getElementById('ftwarehouse_codeD0' + i) !== null) { break; }         
                    }
       
                    if (document.getElementById('ftwarehouse_codeD0' + i).value != '<?php echo $ftwarehouse_code; ?>') {
                        alert('Tidak bisa memilih dari Gudang yang berbeda.');
                        document.getElementById('ftlocation_codeD0<?php echo $_REQUEST['index']; ?>').value='';
                        document.getElementById('ftwarehouse_codeD0<?php echo $_REQUEST['index']; ?>').value='';
                    } else {
                        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']; ?>');                
                    }
                } else {    
                    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 "selling_price":
                /* Untuk sementara dibuka dahulu karena masa trial
                $ssql="SELECT fcselling_price FROM tlselling_price 
                 
                        WHERE ftitem_code='{$_REQUEST['ftitem_code']}' AND ftcurrency_code='IDR' ";
                $fcselling_price=RetRstField($ssql);
        
                ?>        
                    fcprice=document.getElementsByName('fcpriceD0[]')[<?php echo $_REQUEST['index']; ?>].value.replace(/,/g,'')*1;

                    if (fcprice < <?php echo $fcselling_price; ?> ) {
                        document.getElementById('fcpriceD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $fcselling_price; ?>';

                        alert('Harga tidak bisa lebih kecil dari Price List');
                    }
                <?php
                */
                die("");
                break;  
            
            case "pricehistory":
                // set pulldown price history
                $fcprice_history="SELECT DATE_FORMAT(a.fdtrans_date,'%d-%m-%Y') AS fdtrans_date, FORMAT(b.fcprice,2) as fcprice, b.fnqty FROM txsales_order_hdr a INNER JOIN txsales_order_det b ON a.ftso_no=b.ftso_no
                                    WHERE b.ftitem_code='{$_REQUEST['ftitem_code']}' AND a.fnstatus != 3 AND a.fnstatus !=9 AND a.ftcustomer_code = '{$_REQUEST['ftcustomer_code']}'   
                                    ORDER BY a.ftso_no DESC
                                    LIMIT 5
                            ";


                             
                $rs=mysql_query($fcprice_history);
                $info="";
                $infoHeader = $_REQUEST['ftitem_code']." : \\n";
                
                while($r=mysql_fetch_assoc($rs)) {
                    $info.="Tgl : " . $r['fdtrans_date']." ; Harga : ".$r['fcprice']." \\n";
                }
                
                if(!$info){
                    $info= "Tidak ada history harga untuk item ini";
                }
                
                ?>
                alert('<?php echo $infoHeader . $info ?>');
                <?php


                //var_dump($_REQUEST['ftitem_code']);  

                break;    
            }
           
           die("");
           exit;
    }
    
    // Security Hak Akses
    Getsecurity("sales_order");

    // 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_where = " AND a.ftsales_code = '{$ftsales_code}' "; } 
        
    // -- Section Daftar Form --
    // Judul Form
    $objAxpDB->ViewName("Daftar Sales Order");
    
    // Query untuk Form Daftar . Bisa menggunakan View (ViewTable) atau direct SQL (ViewSQL)
    //$objAxpDB->ViewTable("vSASalesOrder");
    $objAxpDB->ViewSQL("SELECT a.fnid AS fnid,a.fdtrans_date AS fdtrans_date,a.ftso_no AS ftso_no, e.fttrans_status_name AS fttax_code, 
                                CONCAT(a.ftcustomer_code,' - ',c.ftcustomer_name) AS ftcustomer_code,
                                a.ftsales_code, a.fcamount AS fcamount,b.fttrans_status_name AS ftstatus, 
                                d.fttrans_status_name AS ftreceive_status	  
                        FROM txsales_order_hdr A LEFT JOIN tstransaction_status B ON 
                                (a.fnstatus = b.fntrans_status_code) AND (b.fntrans_group_code = 2) 
                        LEFT JOIN tlcustomer C ON a.ftcustomer_code = c.ftcustomer_code
                        LEFT JOIN tstransaction_status d ON 
                                (a.fnreceive_status = d.fntrans_status_code) AND (d.fntrans_group_code = 18) 
                        LEFT JOIN tstransaction_status e ON
                                (a.fntax_code = e.fntrans_status_code) AND (e.fntrans_group_code = 15)         
                        WHERE (('{$_SESSION['strbranch']}' <> 'PUSAT' AND a.ftbranch_code = '{$_SESSION['strbranch']}') OR '{$_SESSION['strbranch']}' = 'PUSAT') {$sql_where} 
                        ORDER BY a.ftso_no DESC");
    
    // Judul Kolom                    
    $objAxpDB->ViewCaption("ID, Tgl. Sales Order, No. Sales Order, Tipe, Customer, Salesman, Grand Total, Status, Status Pengiriman");
    // Tampilkan Filter Periode Tanggal
    $objAxpDB->ViewPeriod(TRUE);

    // -- Section Detail Form --
    // Judul Detail Form
    $objAxpDB->FormName("Sales Order");
    $sql = "SELECT ftso_no, ftcustomer_code, ftship_address, ftsales_code, ftdelivery_method, ftreference, 
                    fdtrans_date, ftterm_payment, fddue_date, ftcurrency_code, fcrate, 
                    fntax_code, fnpayment_status, fdvat_date, ftvat_no, ftreff_no, 
                    ftnotes, fnstatus, fnreceive_status, fcpayment_amount, fcoutstanding_amount, 
                    fcreturn_amount,
                    fftotal_qty, fcsub_amount, ffdiscount, fcdiscount, fcother_cost, ffvat, 
                    fcvat, fcamount,fnhdr_print
            FROM txsales_order_hdr";
    $objAxpDB->FormSQL($sql);
    $objAxpDB->FormCaption("No. Sales Order, Customer, Alamat, Salesman, Metode Pengiriman, Referensi, Tgl. Transaksi,  Syarat Pembayaran, Tgl. Jatuh Tempo, Mata Uang, Kurs, Tipe Penjualan, Status Pembayaran, Tgl. Faktur Pajak, No. Faktur Pajak, No. Reff, Catatan, Status, Status Pengiriman, Jumlah Terbayar, Sisa Pembayaran, Jumlah Retur, Total Qty, Sub Total, Discount (%), Discount(Rp), Biaya Lain-lain, Pajak (%), Pajak (Rp), Grand Total, Header Cetak");

    // Posisi Layout Kolom : TL = Top Left, TR = Top Right, BL = Bottom Left, BR = Bottom Right, HD = Hidden
    $objAxpDB->FormPosition("TL, TL, TL, TL, HD, TL, TR, TR, TR, TR, TR, TR, TR, HD, HD, HD, BL, BL, BL, BL, BL, BL, BR, BR, BR, BR, HD, BR, BR, BR, TR");

    // Ajax Script
    $ajaxKurs="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=idr&ftcurrency_code='+ document.getElementById('ftcurrency_code').value);";
    $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);";    
    // Trigger Script Kolom
    $objAxpDB->FormScript(1, "onChange=\"{$ajaxcustomer}\"");
    $ajaxGrid="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=term_payment&ftso_no='+ document.getElementById('ftso_no').value +'&tgl='+ document.getElementById('fdtrans_date').value + '&termpayment='+ document.getElementById('ftterm_payment').value);";
    $objAxpDB->FormScript(7, "onChange=\"{$ajaxGrid}\"");
    $objAxpDB->FormScript(9, "OnChange=\"{$ajaxKurs}\"");
    // $ajaxGrid="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=pajak&ftso_no='+ document.getElementById('ftso_no').value);";
    $objAxpDB->FormScript(11, "onChange=\"{$ajaxGrid}\"");
    $objAxpDB->FormScript(24, "onChange=\"SalesOrderRecalDiscPercent();\"");
    $objAxpDB->FormScript(25, "onChange=\"SalesOrderRecalDiscRp();\"");
    $objAxpDB->FormScript(26, "onChange=\"SalesOrderRecalQty0();\"");
    $objAxpDB->FormScript(27, "onChange=\"SalesOrderRecalQty0();\"");
    $objAxpDB->FormScript(28, "onChange=\"SalesOrderRecalQty0();\"");
    
    // Tampilkan Grid
    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, ftlocation_code, ftwarehouse_code, fnqty_available, fnqty, ftuom_code, ftprint_name, fcold_price, fcprice, ffdiscount, fcdiscount, fcamount, fnqty_base, ffformula,ftstock_category_code FROM txsales_order_det");
    $objAxpDB->GridCaption(0, "No., Kode Barang, Lokasi, Gudang, Qty Tersedia, Qty, Satuan, Keterangan Barang, Harga Lama, Harga, Disc(%), Disc(Rp), Jml Total, Qty Base, Formula, Kategori Stock");
    $objAxpDB->GridSize(0, ",,,5,10,10,5,,1,10,10,10,10,,,");
                
    // System Setting
    if($_SESSION["SOAutoDOPrint"] || $_SESSION["SOAutoDOSave"]) { 
        // Tampilkan Kolom Gudang & Lokasi
        // Setting Kolom di Grid dihide / tidak . 1 = Aktif, 0 = Tidak Aktif
        $objAxpDB->GridHidden(0, "0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1");
        $cetak = "cetak_so"; // Fungsi Cetak otomatis buat DO dan SI diaktifkan
    } else {
        // Tidak perlu tampilkan Kolom Gudang & Lokasi
        $objAxpDB->GridHidden(0, "0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1");
        $cetak = "cetak";
    }   

    // Trigger Script utk Kolom di Grid
    $ajaxItem="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=item&ObjLokasi=ftuom_codeD0_N_&ObjLokasi2=ftlocation_codeD0_N_&ftitem_code=' + this.value + '&index=_N_&ftcustomer_code='+ document.getElementById('ftcustomer_code').value + '&fntax_code='+ document.getElementById('fntax_code').value + '&ftcurrency_code='+ document.getElementById('ftcurrency_code').value);SalesOrderRecalDiscDetailPercent(_N_);SalesOrderRecalDiscPercent();";
    $objAxpDB->GridScript(0,1," onChange=\"if (document.getElementById('fntax_code').value == '') { alert('Pilih Tipe Penjualan terlebih dahulu'); } else { {$ajaxItem} } \"");
    //$ajaxGudang="OnChange=\"ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=gudang&ftwarehouse_code='+this.value+'&ObjLokasi=ftlocation_codeD0_N_&objQty=fnqty_availableD0_N_');\"";
    //$ajaxQtyAvail=" ajax_do('query2.php?code=qty&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_');";
    $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);
    //$ajaxQtyAvail=" onChange=\"ajax_do('query2.php?code=qty&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,3, $ajaxQtyAvail);
    $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=\"SalesOrderRecalDiscDetailPercent(_N_);SalesOrderRecalDiscPercent();{$ajaxFormula}\"  ";
    $objAxpDB->GridScript(0,5, $ajax1);
    $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=\"{$ajaxSellingPrice}SalesOrderRecalDiscDetailPercent(_N_);SalesOrderRecalDiscPercent();\"  ";
    $objAxpDB->GridScript(0,9, $ajax1);
    $price_history_temp = "  <div id='myModal' style='display: none;position: fixed;z-index: 1;left: 0;top: 0;width: 50%;height: 50%;overflow: auto;background-color: rgb(0,0,0);background-color: rgba(0,0,0,0.4);'>

                            <!-- Modal content -->
                            <div style='background-color: #fefefe;margin: 15% auto;padding: 20px;border: 1px solid #888;width: 80%;'>
                              <span id='spanCloseModal' style='color: #aaa;float: right;font-weight: bold;'>&times;</span>
                              <p>Some text in the Modal..</p>
                            </div>

                        </div>
                        <input id='btnPrice' name='btnPrice' type='button' value='Check' onclick=\"document.getElementById('myModal').style.display = 'block';\">
                        ";
    $ajax1=" onChange=\"SalesOrderRecalDiscDetailPercent(_N_);SalesOrderRecalDiscPercent();\"   ";
    $objAxpDB->GridScript(0,10, $ajax1);
    $ajax1=" onChange=\"SalesOrderRecalDiscDetailRp(_N_);SalesOrderRecalDiscPercent();\"  ";
    $objAxpDB->GridScript(0,11, $ajax1);
    
    // Tambahan Button di Grid
    $ajaxPriceHistory="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=pricehistory&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value + '&ftcustomer_code='+ document.getElementById('ftcustomer_code').value);";
    $button_PriceHistory="<input type='button' id='PriceHistoryD0_N_' name='PriceHistoryD0_N_' value='?' OnClick=\"{$ajaxPriceHistory}\">";
    $objAxpDB->GridLayout(0,9,"{$button_PriceHistory}");
    

//var_dump(document.getElementById('ftitem_codeD0_N_').value);

    // Script Ketika Hapus Baris Grid
    $objAxpDB->GridDelJS(0,"SalesOrderRecalQty0();SalesOrderRecalDiscPercent();");

    // Tampilkan Lookup atau Pulldown Menu
    $objAxpDB->Lookup("ftsales_code", "SELECT  ftsales_code, ftsales_name  FROM tlsales WHERE fnstatus=1 ORDER BY ftsales_code", "ftsales_code", "ftsales_name", -1, true);
    $objAxpDB->Lookup("ftdelivery_method", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 7", "fntrans_status_code", "fttrans_status_name", -1,true);
    $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 AND fnactive_status = 1 ", "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_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 WHERE fnstatus != 0 ", "ftcustomer_code", "ftcustomer_name", 1,false);
    $objAxpDB->Lookup("ftuom_code", "SELECT ftuom_code, ftuom_name FROM tluom", "ftuom_code", "ftuom_name", -1,true);
    $objAxpDB->Lookup("fnreceive_status", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 18", "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("ftwarehouse_code", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse WHERE fnstatus=1 ", "ftwarehouse_code", "ftwarehouse_name", -1,true);
    $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("fnhdr_print", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 57 and fnactive_status = 1", "fntrans_status_code", "fttrans_status_name",0, True);
            
    
    // Script utk dijalankan apabila id=0 (Transaksi Baru)
    // if(!$_REQUEST['id']) {
    if($_REQUEST['id']=='0') {
            // Ajax Script untuk memanggil penomoran auto number transaksi
            $strAutoNumber1=" ajax_do('query2.php?code=2&ftmodule_code=sls001&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftso_no&no_trans=ftso_no&table_name=txsales_order_hdr&ftbranch_code={$_SESSION['strbranch']}');";
            
            $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                    {$strAutoNumber1}

                                    ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=tgl&tgl=' + document.getElementById('fdtrans_date').value + '&termpayment=' + document.getElementById('ftterm_payment').value);
                                    
                                }
                                OnChangeDate_fdtrans_date();
                                document.getElementById('ftcurrency_code').value='IDR';
                                document.getElementById('ftsales_code').value='{$ftsales_code}';
                                {$ajaxKurs}
                                ElementCurrencyALL();    
                            ";        
   
            $objAxpDB->FormJS($strAutoNumber0);
            
            $ajaxInquiry="window.open('penjualan.php?form=sales_inquiry&mode=".$_REQUEST['mode']."&id=0','win_inquiry','height='+450+',width='+700+',top='+100+',left='+200+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen=1');";
            $button_inquiry = "<input type='button' value='INQUIRY' OnClick=\"{$ajaxInquiry}\">";
            $objAxpDB->FormLayout(0, $button_inquiry);
            
            //$objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, CONCAT(TRIM(ftitem_name),CASE WHEN TRIM(IFNULL(ftpart_no,\'\')) != \'\' THEN \' ,PART NO: \' ELSE \'\' END,IFNULL(ftpart_no,\'\')) AS ftitem_name FROM tlstock_hdr WHERE fnstatus=1 ORDER BY ftitem_code ", "ftitem_code", "ftitem_name",-1,false);
            $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, CONCAT(TRIM(ftitem_name),CASE WHEN TRIM(IFNULL(ftpart_no,'')) != '' THEN ' ,PART NO: ' ELSE '' END,IFNULL(ftpart_no,'')) AS ftitem_name FROM tlstock_hdr WHERE fnstatus=1 ORDER BY ftitem_code ", "ftitem_code", "ftitem_name",-1,false);
            
    } else {
            if(isset($_REQUEST['revisi'])) 
            {   // reset term payment option
                $strScript =  "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); ";                
                
                // recalculate qty available
                $strScript =  $strScript . "var i=document.getElementsByName('fnindexD0[]').length;
                                            for(var j=1;j<i;j++) {
                                                ajax_do('query2.php?code=qty_onhand' + '&ftwarehouse_code=' + document.getElementsByName('ftwarehouse_codeD0[]')[j].value + '&ftlocation_code=' + document.getElementsByName('ftlocation_codeD0[]')[j].value + '&ftitem_code=' + document.getElementsByName('ftitem_codeD0[]')[j].value + '&qty_revisi=' + document.getElementsByName('fnqtyD0[]')[j].value + '&objQty=fnqty_availableD0' + j  + '&ftso_no=' + document.getElementById('ftso_no').value);
                                            }
                                            ";                
                
                $objAxpDB->lookup_magic_quotes = true;
                $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, CONCAT(TRIM(ftitem_name),CASE WHEN TRIM(IFNULL(ftpart_no,'')) != '' THEN ' ,PART NO: ' ELSE '' END,IFNULL(ftpart_no,'')) AS ftitem_name FROM tlstock_hdr WHERE fnstatus=1 ORDER BY ftitem_code ", "ftitem_code", "ftitem_name",-1,false);
                
            // refresh qty available if status = 1    
            } else {
                $ssql="SELECT fnstatus FROM txsales_order_hdr WHERE fnid='{$_REQUEST['id']}' ";
                $fnstatus = RetRstField($ssql);                

                if ($fnstatus == 1) {
                    // recalculate qty available
                    $strScript =  $strScript . "var i=document.getElementsByName('fnindexD0[]').length;
                                                for(var j=1;j<i;j++) {
                                                    ajax_do('query2.php?code=qty_onhand' + '&ftwarehouse_code=' + document.getElementsByName('ftwarehouse_codeD0[]')[j].value + '&ftlocation_code=' + document.getElementsByName('ftlocation_codeD0[]')[j].value + '&ftitem_code=' + document.getElementsByName('ftitem_codeD0[]')[j].value + '&qty_revisi=' + document.getElementsByName('fnqtyD0[]')[j].value + '&objQty=fnqty_availableD0' + j  + '&ftso_no=' + document.getElementById('ftso_no').value);
                                                }
                                                ";       
                }   
                
                $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, CONCAT(TRIM(ftitem_name),CASE WHEN TRIM(IFNULL(ftpart_no,'')) != '' THEN ' ,PART NO: ' ELSE '' END,IFNULL(ftpart_no,'')) AS ftitem_name FROM tlstock_hdr", "ftitem_code", "ftitem_name",-1,false);
            
            }   
            
            $strScript = $strScript . " ElementCurrencyALL(); ";
            $objAxpDB->FormJS($strScript);
    }

    // Set variable untuk menampung kolom data tertentu
    $objAxpDB->SetStatus("id, fnstatus, fnreceive_status");
    
    // Set Tombol utk daftar dimana id= ""
    if ($objAxpDB->GetStatus("id") == "")
            $objAxpDB->ButtonShow("baru");
    // Set Tombol utk transaksi baru , id=0
    elseif ($objAxpDB->GetStatus("id") == 0) {
            // Tombol Tambah dan Hapus Baris di Grid di aktifkan
            $objAxpDB->GridAllowAddNew(0,true);
            $objAxpDB->GridAllowDelete(0,true);
            // Setting Kolom di Form dan Grid dikunci / tidak . 1 = Aktif, 0 = Tidak Aktif
            $objAxpDB->FormLocked("1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1");
            $objAxpDB->GridLocked(0, "1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1");
            $objAxpDB->ButtonShow("reset, simpan, tutup");

    // Set Tombol utk transaksi dengan status 1 (posted)                    
    } elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
        // revisi
        if(isset($_REQUEST['revisi'])) 
        {
            $objAxpDB->FormLocked("1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1");
            $objAxpDB->GridLocked(0, "1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1");
            $objAxpDB->GridAllowAddNew(0, TRUE);
            $objAxpDB->GridAllowDelete(0, TRUE); 
                
            $objAxpDB->ButtonShow("simpan_revisi,tutup");
            
        } else {    
            $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 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, 1, 1, 1, 1");
            
            if($objAxpDB->GetStatus("fnreceive_status") == 3) {
                $objAxpDB->ButtonShow("baru, {$cetak}, revisi, batal, tutup");
            } else {
                // $objAxpDB->ButtonShow("baru, cetak, cetak_do, update, close_so, batal, tutup");
                $objAxpDB->ButtonShow("baru, {$cetak}, revisi, batal, sales_invoice, tutup");
            }
            
        }
        
    // Set Tombol utk transaksi dengan status 2 (approved)                    
    } elseif ($objAxpDB->GetStatus("fnstatus") == 2) {
        // revisi
        if(isset($_REQUEST['revisi'])) 
        {
            $objAxpDB->FormLocked("1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1");
            $objAxpDB->GridLocked(0, "1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1");
            $objAxpDB->GridAllowAddNew(0, TRUE);
            $objAxpDB->GridAllowDelete(0, TRUE); 
                
            $objAxpDB->ButtonShow("simpan_revisi,tutup");
            
        } else {
        
            $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 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, 1, 1, 1, 1");
            // $objAxpDB->ButtonShow("baru, cetak_do, close_so, batal, tutup");
            $objAxpDB->ButtonShow("baru, cetak, revisi, batal, tutup");
            // $objAxpDB->ButtonShow("baru, close_so, batal, tutup");
            
        }
        
    } elseif (($objAxpDB->GetStatus("fnstatus") == 3) || ($objAxpDB->GetStatus("fnstatus") == 5) || ($objAxpDB->GetStatus("fnstatus") == 9)) {
            $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, 1, 1, 1");
            $objAxpDB->GridLocked(0, "1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
            $objAxpDB->ButtonShow("baru, tutup");
            
    } 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, 1, 1, 1");
            $objAxpDB->GridLocked(0, "1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
            $objAxpDB->ButtonShow("baru, batal, tutup");
            
    }

    
    $objAxpDB->Process();

    $objAxpDB->WriteHtmlTable();

            
?>  
                    
<script language="JavaScript" type="text/javascript">
    // coding pending sementara
    // Get the modal
    var modal = document.getElementById('myModal');

    // Get the button that opens the modal
    //var btn = document.getElementById('btnPrice');

    // When the user clicks on the button, open the modal
    // btn.onclick = function() {
    //    modal.style.display = 'block';
    //}

    // When the user clicks on <span> (x), close the modal
    //spanCloseModal.onclick = function() {
    //    modal.style.display = 'none';
    //}

    // When the user clicks anywhere outside of the modal, close it
    window.onclick = function(event) {
        if (event.target == modal) {
            modal.style.display = 'none';
        }
    } 
</script>                    