<!---- 
Author  : Ferry / Tanggal : 02-02-2015  
-->
    
    <?php
            //---------Untuk privellege jika dia ada tombol save----------
                if($group_user->SavePrivillage == 1){
                    $cek_autority_save  = "";
                }
                else{
                    $cek_autority_save  = "disabled";
                }
                //-------------end----------------------------
    ?>
    
<div class="easyui-layout" data-options="fit:true,border:false">
    <!-- top view-->
    <div id="form-header" data-options="region:'north',border:false" >
        <div id="form-header-wrapper">
            <table width="100%" cellpadding="0" cellspacing="0">
            <tr>
                <td align="left" >
                    <a href="#" class="easyui-linkbutton" iconCls="icon-document" plain="true" onclick="parent.tab_add('New Purchase Payment', '<?=$menu_link?>', 'icon-add', 'add');">New</a>                          
                    <a href="#" class="easyui-linkbutton" id="refresh-linkbutton" data-options="plain:true,iconCls:'icon-reload'" onclick="location.reload();">Refresh</a>
                </td>                           
                <td align="right">                              
                    <span class="form-title"><?=$menu_label?></span>
                </td>
            </tr>
            </table>                
        </div>          
    </div>
    <!-- end top view -->
    
    <!-- center view -->
    <div id="form-body" data-options="region:'center',border:false">
        <div id="form-wrapper" class="easyui-panel" fit="true" style="padding:5px;">
            <form id="fm_purchase_payment" name="fm_purchase_payment" method="post">
                <input type="hidden" name="detail" id="detail"/>
                <input type="hidden" name="hdn_vend_saldo_dp" id="hdn_vend_saldo_dp" class="easyui-numberbox" value="0" style="text-align:right;" readonly
                                            data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'"/>
                                            
                <input type="hidden" name="hdn_bank_number" id="hdn_bank_number"/>
                
                <input type="hidden" name="hdn_vend_curr_code" id="hdn_vend_curr_code"/>
                <input type="hidden" name="hdn_bank_curr_code" id="hdn_bank_curr_code"/>
                <input type="hidden" name="hdn_payment_status" id="hdn_payment_status"/>
                
                <input type="hidden" name="txt_vendor_rate" id="txt_vendor_rate" class="easyui-numberbox" value="0" style="text-align:right;" readonly
                                            data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'"/>
                                            
                <input type="hidden" name="hdn_total_currency" id="hdn_total_currency" class="easyui-numberbox" value="0" style="text-align:right;" readonly
                                            data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'"/>
                
                <input type="hidden" name="hdn_PInvTotalAmount" id="hdn_PInvTotalAmount" class="easyui-numberbox" value="0" style="text-align:right;" readonly
                                            data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'"/>
                                            
                <input type="hidden" name="hdn_PPayTotalOwing" id="hdn_PPayTotalOwing" class="easyui-numberbox" value="0" style="text-align:right;" readonly
                                            data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'"/>
                                            
              <input type="hidden" name="hdn_PPayTotalDPUsed" id="hdn_PPayTotalDPUsed" class="easyui-numberbox" value="0" style="text-align:right;" readonly
                                            data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'"/>
                
                <input type="hidden" name="hdn_PPayTotalDiscount" id="hdn_PPayTotalDiscount" class="easyui-numberbox" value="0" style="text-align:right;" readonly
                                            data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'"/>
                                            
                <input type="hidden" name="hdn_lock_status"     id="hdn_lock_status"/>
                <input type="hidden" name="hdn_branch_for"      id="hdn_branch_for" value="<?= $this->session->userdata("branch") ?>"/>
                
                <input type="hidden" name="hdn_po_status"       class="easyui-numberbox" id="hdn_po_status" value="0"/>
                <input type="hidden" name="hdn_pi_status"       class="easyui-numberbox" id="hdn_pi_status" value="0"/>
                <input type="hidden" name="hdn_pgir_number"       id="hdn_pgir_number"/>

                <!-- Form Header -->
                <table width="100%" height="141" cellpadding="0" cellspacing="0">
                    <tr>
                        <td width="50%" valign="top">
                            <table width="100%" cellpadding="0" cellspacing="1">
                                <tr height="20">
                                    <td width="120"><b>Branch Name</b></td>
                                    <td width="10"><b>:</b></td>
                                    
                                    <td>
                                    	<?php
											if($txt_cheque_no == ""){
										?>
                                        <select class="combo-grid" name="cbg_branch" id="cbg_branch" style="width:252px;height:22px;" required <?=(($this->session->userdata("branch_status")==1)?"disabled":'') ?> editable="false"
                                            data-options="
                                                panelWidth:400,
                                                value:'<?= $this->session->userdata("branch") ?>',
                                                idField: 'BranchCode',
                                                textField: 'BranchName',
                                                url: '<?=base_url()?>custom/get_data/branch_no_filter',
                                                method: 'get',
                                                columns: [[
                                                    {field:'BranchCode',title:'Branch No',width:100,halign:'center'},
                                                    {field:'BranchName',title:'Branch Name',width:161,halign:'center'}
                                                ]],
                                                fitColumns: true
                                            ">
                                        </select>
                                        <?php
											}else{
										?>
                                        <select class="combo-grid" name="cbg_branch" id="cbg_branch" style="width:252px;height:22px;" required <?=(($this->session->userdata("branch_status")==1)?"disabled":'') ?> editable="false" readonly
                                            data-options="
                                                panelWidth:400,
                                                value:'<?= $this->session->userdata("branch") ?>',
                                                idField: 'BranchCode',
                                                textField: 'BranchName',
                                                url: '<?=base_url()?>custom/get_data/branch_no_filter',
                                                method: 'get',
                                                columns: [[
                                                    {field:'BranchCode',title:'Branch No',width:100,halign:'center'},
                                                    {field:'BranchName',title:'Branch Name',width:161,halign:'center'}
                                                ]],
                                                fitColumns: true
                                            ">
                                        </select>
                                        <?php
											}
										?>
                                    </td>
                                </tr>

                                <tr height="20">
                                    <td width="120"><b>Paid To</b></td>
                                    <td width="10"><b>:</b></td>
                                    
                                    <td>
                                    	<?php
											if($txt_cheque_no == ""){
										?>
                                        	 <select class="combo-grid" name="cbg_vendor" id="cbg_vendor" style="width:252px;height:22px;" required 
                                                data-options="
                                                    panelWidth:400,
                                                    idField: 'VendorCode',
                                                    textField: 'VendorName',
                                                    url: '<?=base_url()?>custom/get_data/vendor_new_by_branch',
                                                    method: 'get',
                                                    columns: [[
                                                        {field:'VendorCode',title:'Vendor No',width:100,halign:'center'},
                                                        {field:'VendorName',title:'Vendor Name',width:161}
                                                    ]],
                                                    fitColumns: true
                                                ">
                                            </select>
                                        <?php
											}else{
										?>
                                         <select class="combo-grid" name="cbg_vendor" id="cbg_vendor" style="width:252px;height:22px;" required readonly
                                            data-options="
                                                panelWidth:400,
                                                idField: 'VendorCode',
                                                textField: 'VendorName',
                                                url: '<?=base_url()?>custom/get_data/vendor_new_by_branch',
                                                method: 'get',
                                                columns: [[
                                                    {field:'VendorCode',title:'Vendor No',width:100,halign:'center'},
                                                    {field:'VendorName',title:'Vendor Name',width:161}
                                                ]],
                                                fitColumns: true
                                            ">
                                        </select>
                                        <?php
											}
										?>
                                        
                                       
                                    </td>
                                </tr>

                                <tr height="20">
                                    <td width="120" valign="top" style="padding-top:4px;"><b>Payee</b></td>
                                    <td width="10" valign="top" style="padding-top:4px;"><b>:</b></td>
                                    
                                    <td>
                                        <textarea name="txa_address" id="txa_address" rows="4" style="width:246px;resize:none;" required readonly></textarea>
                                    </td>
                                </tr>                                
                            </table>
                        </td>
                    
                        <td width="50%" valign="top">
                            <table width="100%" cellpadding="0" cellspacing="1">
                                
                                
                                <tr valign="middle">
                                    <td colspan="2" width="322">&nbsp;</td>
                                    <td width="120"><b>Payment Date</b></td>
                                    <td width="10"><b>:</b></td>
                                    
                                    <td>
                                        <input name="txt_ppay_date" id="txt_ppay_date" type="text" class="easyui-datebox validBackward" style="width:259px;" value='<?=$ppay_date?>' required editable="false" />
                                    </td>
                                </tr>
                                <tr valign="middle">
                                    <td colspan="2" width="322">&nbsp;</td>
                                    <td width="120"><b>Cheque No.</b></td>
                                    <td width="10"><b>:</b></td>
                                    
                                    <td>
                                        <input type="text" name="txt_cheque_no" id="txt_cheque_no" class="easyui-validatebox" style="width:257px;" readonly value="<?php echo $txt_cheque_no;?>"/>
                                    </td>
                                </tr>
                                
                                <tr height="47"valign="middle"></tr>

                                <tr>
                                    <td colspan="2" width="322">&nbsp;</td>
                                    <td colspan="2" width="322">&nbsp;</td>
                                    
                                    <td height="20">
                                        <input type="checkbox" name="chk_fiscal_payment" id="chk_fiscal_payment" onClick="fiscal_payment();" value="1" disabled="disabled">Fiscal Payment
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>

                    <tr>
                        <td colspan="8"><hr/></td>
                    </tr>

                    <tr>
                        <td width="50%" valign="top">
                            <table width="100%" cellpadding="0" cellspacing="1">
                                <tr height="20">
                                    <td width="120"><b>Bank Payment</b></td>
                                    <td width="10"><b>:</b></td>
                                    
                                    <td>
                                        <?php
											if($txt_cheque_no == ""){
										?>
                                        	 <select class="easyui-combogrid" name="cbg_bank" id="cbg_bank" style="width:252px;height:22px;" required editable="true"
                                                data-options="
                                                    panelWidth:400,
                                                    idField: 'AccountNumber',
                                                    textField: 'AccountName',
                                                    url: '<?=base_url()?>list/purchases_payment/ajax_bank_account',
                                                    method: 'post',
                                                    fitColumns: true,
                                                    columns: [[
                                                        {field:'AccountName',title:'Account Bank Name',width:100, halign:'center'},                     
                                                        {field:'CurrencyCode',title:'Currency Name',width:100, halign:'center'}                     
                                                    ]]                                                
                                                ">
                                            </select>
                                        <?php
											}else{
										?>
                                         <select class="easyui-combogrid" name="cbg_bank" id="cbg_bank" style="width:252px;height:22px;" required editable="true" readonly
                                            data-options="
                                                panelWidth:400,
                                                idField: 'AccountNumber',
                                                textField: 'AccountName',
                                                url: '<?=base_url()?>list/purchases_payment/ajax_bank_account',
                                                method: 'post',
                                                fitColumns: true,
                                                columns: [[
                                                    {field:'AccountName',title:'Account Bank Name',width:100, halign:'center'},                     
                                                    {field:'CurrencyCode',title:'Currency Name',width:100, halign:'center'}                     
                                                ]]                                                
                                            ">
                                        </select>
                                        <?php
											}
										?>
                                    </td>
                                </tr>
                                
                                <tr height="20">
                                    <td width="120"><b>Currency Rate</b></td>
                                    <td width="10"><b>:</b></td>
                                    
                                    <td>
                                        <input type="text" name="txt_bank_curr_rate" id="txt_bank_curr_rate" class="easyui-numberbox" required="true" value="1" style="width:70px;text-align:right;"
                                            data-options="min:1, precision:2, groupSeparator:'.', decimalSeparator:','" 
                                        />
                                        
                                        <span id="currency" style="font-weight:bold"></span>
                                    </td>
                                </tr>
                                
                                <tr height="20">
                                    <td width="120" valign="top" style="padding-top:4px;"><b>Memo</b></td>
                                    <td width="10" valign="top" style="padding-top:4px;"><b>:</b></td>
                                    
                                    <td>
                                        <textarea name="txa_description" id="txa_description" rows="4" style="width:246px;resize:none;"></textarea>
                                    </td>
                                </tr>
                            </table>
                        </td>
                        
                        <td width="50%" valign="top">
                            <table width="100%" cellpadding="0" cellspacing="1">
                                <tr>
                                    <td width="293" rowspan="5">&nbsp;</td>
                                    <td>
                                        <span id="lbl_balance_curr" style="font-weight:bold">Balance (IDR)</span>
                                    </td>
                                    <td width="13"><b>:</b></td>
                                    
                                    <td width="129">
                                        <input type="numberbox" name="txt_bank_balance" id="txt_bank_balance" class="easyui-numberbox" value='0,00'  style="width:127px;text-align:right;" readonly
                                            data-options="precision:2, groupSeparator:'.', decimalSeparator:','" 
                                        />
                                    </td>
                                </tr>
                            
                               
                                <tr>
                                   <td>
                                        <input type="hidden" name="txt_vend_dp_part" id="txt_vend_dp_part" class="easyui-numberbox" value='0,00' style="width:127px;text-align:right;" readonly
                                            data-options="min:0, precision:2, groupSeparator:'.', decimalSeparator:','"
                                        />
                                    </td>
                                </tr>
                                                                
                                <tr>
                                    <td width="209"><b>Saldo DP Invoice</b></td>
                                    <td width="13"><b>:</b></td>
                                    
                                    <td>
                                        <input type="text" name="txt_vendor_dp" id="txt_vendor_dp" class="easyui-numberbox" value='0,00' style="width:127px;text-align:right;" readonly 
                                            data-options="min:0, precision:2, groupSeparator:'.', decimalSeparator:','"
                                        />
                                    </td>
                                </tr>
                                
                                <tr>
                                    <td width="209"><b>Saldo DP Payment</b></td>
                                    <td width="13"><b>:</b></td>
                                    <td>
                                        <input type="text" name="txt_vendor_dp_return" id="txt_vendor_dp_return" class="easyui-numberbox" value='0,00' style="width:127px;text-align:right;" readonly 
                                            data-options="min:0, precision:2, groupSeparator:'.', decimalSeparator:','"
                                        />
                                    </td>
                                </tr>
                                
                                <tr>
                                    <td width="209"><span id="lbl_payment_curr" style="font-weight:bold">Total Payment (IDR)</span></td>
                                    <td width="13"><b>:</b></td>
                                    
                                    <td>
                                        <input type="text" name="txt_total_amount" id="txt_total_amount" class="easyui-numberbox" value='0,00' style="width:127px;text-align:right;" readonly 
                                            data-options="min:0, precision:2, groupSeparator:'.', decimalSeparator:','"
                                        />
                                    </td>
                                </tr>                                
                            </table>
                        </td>
                    </tr>
                </table>
                <!-- End of form header -->
                
                <!--Item Grid -->
                <div class="easyui-tabs" data-options="tabHeight:18" style="margin-top:5px;">       
                    <div id="tab_item" title="Item">
                        <table id="dtg_item" class="easyui-edatagrid" title="" iconCls="icon-edit" singleSelect="true" rowNumbers="true" fitColumns="true" style="height:240px"
                            url="<?=base_url()?>list/purchases_payment/ajax_item" 
                        >
                            <thead frozen="true">
                                <tr>
                                    <th rowspan="2" field="Paid"              width="40"  data-options="halign:'center',align:'center'" resizable='false' editor="{type:'checkbox',options:{on:'Yes',off:'No'}}">Paid</th>
                                    <th rowspan="2" field="Invoice_Type"      width="50"  data-options="halign:'center',align:'center'" resizable='false' editor="{type:'validatebox'}" hidden>Invoice Type</th>
                                    <th rowspan="2" field="Invoice_Number"    width="160" data-options="halign:'center',align:'left'"   resizable="false" editor="{type:'validatebox',options:{required:true,disabled:true}}">Invoice No.</th>
                                    <th rowspan="2" field="Invoice_Date"      width="87"  data-options="halign:'center',align:'center'" resizable="false" editor="{type:'validatebox',options:{required:true,disabled:true}}" formatter="formatDate">Inv. Date</th>
                                    <th colspan="1">Invoice Amount</th>
                                    <th colspan="1">Invoice Owing</th>
                                    <th colspan="1">Downpayment</th>
                                    <th colspan="1">Discount</th>                                        
                                    <th rowspan="1">Rounding</th>
                                    <th colspan="2">Payment Amount</th>                                    

                                    <th rowspan="2" field="ItemCategoryCode"  width="115" data-options="halign:'center',align:'center'" resizable="false" editor="{type:'text', disabled:'true'}" hidden>Category Code</th>
                                    <th rowspan="2" field="ItemCategoryID"    width="70"  data-options="halign:'center',align:'center'" resizable="false" editor="{type:'text', disabled:'true'}" hidden>Category ID</th>
                                    <th rowspan="2" field="InvoiceCurrCode"   width="50"  data-options="halign:'center',align:'right'"  resizable="false" editor="{type:'text', disabled:'true'}" hidden>Invoice Currency Code</th>
                                    <th rowspan="2" field="InvoiceCurrRate"   width="50"  data-options="halign:'center',align:'right'"  resizable="false" editor="{type:'text', disabled:'true'}" hidden>Invoice Currency Rate</th>
                                    <th rowspan="2" field="InvoiceFiscalRate" width="80"  data-options="halign:'center',align:'right'"  resizable="false" editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',min:0,disabled:true}}" formatter="formatRp" hidden>Fiscal Rate</th>
                                    <th rowspan="2" field="InvoiceAmountCopy" width="115" data-options="halign:'center',align:'right'"  editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',min:0,disabled:true}}" resizable="false" halign='center' align="right" formatter="formatRp" hidden>Invoice Copy</th>
                                    <th rowspan="2" field="Select_DP"         width="100"  data-options="halign:'center',align:'right'" editor="{type:'text'}" >Select DP</th>
                                    <th rowspan="2" field="InvoiceOwingHidden" width="115" data-options="halign:'center',align:'right'"  
                                    editor="{type:'numberbox',options:{decimalSeparator:',',required:true,disabled:true,
                                    formatter:function(v){
                                        return v;
                                    },
                                    parser:function(s){
                                        var v = parseFloat(s);
                                        return v;
                                    }
                                    }}"
                                    hidden>Owing Copy</th>
                                    
                                    <th rowspan="2" field="InvoiceOwingHiddenNew" width="115" data-options="halign:'center',align:'right'"  
                                    editor="{type:'numberbox',options:{decimalSeparator:',',required:true,disabled:true,
                                    formatter:function(v){
                                        return v;
                                    },
                                    parser:function(s){
                                        var v = parseFloat(s);
                                        return v;
                                    }
                                    }}"
                                    hidden>Invoice Owing Temp</th>
                                </tr>
                                
                                <tr>
                                    <th field="Invoice_Amount"   width="127" data-options="halign:'center',align:'right'" resizable="false" editor="{type:'numberbox',options:{min:0,precision:2,decimalSeparator:','}}" formatter="formatRp"><span class="spn_vend_curr">(IDR)</span></th>
                                    <th field="Invoice_Owing"    width="127" data-options="halign:'center',align:'right'" resizable="false" editor="{type:'numberbox',options:{min:0,precision:2,decimalSeparator:','}}" formatter="formatRp"><span class="spn_vend_curr">(IDR)</span></th>
                                    <th field="Payment_DP"       width="127" data-options="halign:'center',align:'right'" resizable="false" editor="{type:'numberbox',options:{min:0,precision:2,decimalSeparator:',',required:true}}" formatter="formatRp"><span class="spn_vend_curr">(IDR)</span></th>
                                    <th field="Payment_Discount" width="127" data-options="halign:'center',align:'right'" resizable="false" editor="{type:'numberbox',options:{min:0,precision:2,decimalSeparator:',',required:true}}" formatter="formatRp"><span class="spn_vend_curr">(IDR)</span></th>
                                    <th field="Load_Rounding"    width="127" data-options="halign:'center',align:'right'" resizable="false" editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',required:true}}" formatter="formatRp"><span class="spn_vend_curr">(IDR)</span></th>

                                    <th field="Payment_Amount"   width="127" data-options="halign:'center',align:'right'" resizable="false" editor="{type:'numberbox',options:{min:0,precision:2,decimalSeparator:',',required:true}}" formatter="formatRp"><span class="spn_vend_curr">(IDR)</span></th>
                                    <th field="Payment_Currency" width="127" data-options="halign:'center',align:'right'" resizable="false" editor="{type:'numberbox',options:{min:0,precision:2,decimalSeparator:',',required:true}}" formatter="formatRp"><span class="spn_bank_curr">(IDR)</span></th>
                                </tr>                                
                            </thead>
                        </table>
                    </div>
                </div>
            </form>
        </div>
    </div>
    
    <div id="form-footer" data-options="region:'south',border:false">
        <table width="100%" cellpadding="0" cellspacing="0">
            <tr>
                <td>&nbsp;</td>
                
                <td align="right">
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-help" onclick="" plain="true">Help</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button save-button" iconCls="icon-save" onclick="insert_data();" <?php echo $cek_autority_save;?>>Save</a>                               
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-cancel" onclick="">Cancel</a>
                </td>
            </tr>
        </table>
    </div>
</div>

<!----------------------------------------------Batas Awal Dialog DP------------------------------------------------------>
<div id="dialog_asset" class="easyui-dialog" style="width:850px;height:510px;background-color:#F8F8F8;padding:0px;" closed="true" data-options="modal:true, iconCls:'icon-edit'" buttons="#dialog-buttons">
    <div id="tab_purchase_req" class="easyui-tabs" style="width:835px;height:485px; background-color:#F8F8F8;" data-options="fit:true,plain:true">
        <div title="List Down Payment" style="padding:10px" >
             <input type="hidden" name="hdn_pord_number" id="hdn_pord_number" />
             
            <table id="tt_choose_dp" class="easyui-edatagrid" rowNumbers="true" fitColumns="true" singleSelect="true" style="width:813px;height:375px;"
                url="<?=base_url()?>list/purchases_payment/get_pordetail_dp_new"                            
            >
                <thead>
                    <tr>
                        <th field='Status'        width="35"  data-options="halign:'center',align:'center'" editor="{type:'checkbox',options:{on:'Yes',off:'No'}}">Status</th>
                        <th field="POrdNumber"    width="110" data-options="halign:'center',align:'center'">Po. Number.</th>
                        <th field="PaymentAmount_SUM" width="65"  data-options="halign:'center',align:'center'" editor="{options:{precision:2,decimalSeparator:',',groupSeparator:'.',disabled:false}}" formatter="formatRp">Dp Balance</th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
</div>

<div id="dialog-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_dp();">OK</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog_asset');">Cancel</a>
</div>
<!-------------------------------------------Batas Akhir Dialog DP---------------------------------------------------->

<script type="text/javascript">
    var SavePrivillage  = '<?php echo $group_user->SavePrivillage;?>';

    $(document).ready(function(){
        $('#dtg_item').datagrid('hideColumn', 'Payment_Currency');
        $('#dtg_item').datagrid('showColumn', 'ItemCategoryCode');
        $('#hdn_payment_status').val(0);

        /*======================== Daswan ========================*/
        var PInvNumber      = '<?=$PInvNumber;?>';
        var BranchCode      = '<?=$branch;?>';
        var VendorCode      = '<?=$VendorCode;?>';
        var CurrencyCode    = '<?=$CurrencyCode;?>';
		var chk_no 			= '<?=$txt_cheque_no;?>';
				
        if(PInvNumber !=''){
            PInvNumber      = PInvNumber.replace(/\__/g,"/");
            PInvNumber      = PInvNumber.replace(/\_/g,"-");
            VendorCode      = VendorCode.replace(/\__/g,"/");
            VendorCode      = VendorCode.replace(/\_/g,"-");
            BranchCode      = BranchCode.replace(/\_/g,"-");
        }
        /*======================= end Daswan =====================*/
		
		/*
			Jika Chk ada isi
		*/
		$('#hdn_pgir_number').val(PInvNumber);
		/*=====================End Giro======================*/
		
        $('#cbg_branch').combogrid({
            onSelect:function(rowIndex, rowData){
                $('#cbg_vendor').combogrid('setValue','');
                $('#cbg_bank').combogrid('setValue','');
                $('#txa_address').val('');

                $('#chk_fiscal_payment').prop('checked', false);
                $('#currency').html('IDR / IDR');
                $('#txt_bank_curr_rate').numberbox('setValue', 1);
               
                $('#txt_cheque_amount').numberbox('setValue', '');
                $('#hdn_branch_for').val(rowData.BranchCode);
                
                $('#txt_bank_balance').numberbox('setValue', 0);
                $('#txt_vendor_dp').numberbox('setValue', 0);
                $('#txt_vendor_dp_return').numberbox('setValue', 0);
                $('#txt_vend_dp_part').numberbox('setValue', 0);
                $('#txt_total_amount').numberbox('setValue', 0);
                                                    
                $('#cbg_vendor').combogrid('grid').datagrid('load',{
                    BranchCode : rowData.BranchCode,
                    CompanyCode : rowData.CompanyCode
                });
                
                $('#cbg_bank').combogrid('grid').datagrid('load',{
                    CurrencyCode : rowData.Vnd_CurrencyCode
                });
                
                $('#dtg_item').datagrid('load',{
                    VendorCode : ''
                });
            },onLoadSuccess:function(data){
                if(PInvNumber!=''){
                    $('#cbg_branch').combogrid('setValue',BranchCode);
                }
            }
    }); 
    
        $('#cbg_vendor').combogrid({
            onSelect:function(rowIndex, rowData){
                var vend_address            = rowData.VendorAddress;
                var vend_curr_code          = rowData.Vnd_CurrencyCode;
                var exchange_rate           = rowData.ExchangeRate;
                var saldodp_balance         = rowData.SaldoDPBalance;
                var saldodp_balance_return  = rowData.SaldoDPReturn;
                
                $('#hdn_vend_curr_code').val(vend_curr_code);
                $('.spn_vend_curr').text('(' + vend_curr_code + ')');
                
                $('#currency').html(vend_curr_code + ' / IDR');
                $('#txt_bank_curr_rate').numberbox('setValue', exchange_rate);
				$('#txt_vendor_rate').numberbox('setValue', exchange_rate);

                $('#txa_address').val(vend_address);
                
                /*Function get invoice dp*/
                get_po_detail_dp_invoice(rowData.VendorCode);
                
                /*Fuunction get pordetail*/
                get_po_detail_dp(rowData.VendorCode);
                
                $('#cbg_bank').combogrid('setValue', '');
                $('#hdn_bank_curr_code').val('');
                
                $('#txt_vend_dp_part').numberbox('setValue', rowData.SaldoDPPartial);
                $('#hdn_lock_status').val(rowData.LockStatus);
                
               
                $('#txt_cheque_amount').numberbox('setValue', '');
                $('#chk_fiscal_payment').prop('checked', false);
                
                
                if(($('#hdn_bank_curr_code').val() == '') || ($('#hdn_vend_curr_code').val() == $('#hdn_bank_curr_code').val())){
                    $('#dtg_item').datagrid('hideColumn', 'Payment_Currency');
                    $('#dtg_item').datagrid('showColumn', 'ItemCategoryCode');
                    $('#hdn_payment_status').val(0);
                }
                else{
                    $('#dtg_item').datagrid('showColumn', 'Payment_Currency');
                    $('#dtg_item').datagrid('hideColumn', 'ItemCategoryCode');
                    $('#hdn_payment_status').val(1);
                }

                if(vend_curr_code == 'IDR'){
                    $('#txt_bank_curr_rate').prop('disabled', true);
                    $('#chk_fiscal_payment').prop('disabled', true);
                }
                else{
                    $('#txt_bank_curr_rate').prop('disabled', false);                  
                }
				
				
				
				if(chk_no == ""){
					
					$('#dtg_item').datagrid('load',{
						VendorCode  : rowData.VendorCode,
						PPnFiscal   : 0
					});
				}
				else{
					$('#dtg_item').datagrid('load',{
						VendorCode  : rowData.VendorCode,
						PGirNumber  : PInvNumber,
						PPnFiscal   : 0
					});
				}
				
                
                
                $('#cbg_bank').combogrid('grid').datagrid('load',{
                    CurrencyCode : rowData.Vnd_CurrencyCode
                });
            },onLoadSuccess:function(data){
                if(PInvNumber!=''){
                    $('#cbg_vendor').combogrid('setValue',VendorCode);
                }
            }
    }); 
        
    $('#cbg_bank').combogrid({
            onSelect:function(rowIndex, rowData){
                var ppay_date       = $('#txt_ppay_date').datebox('getValue');
                var vend_curr_code  = $('#hdn_vend_curr_code').val();
                var bank_curr_code  = rowData.CurrencyCode;
                var bank_out_code   = rowData.BankOutCode;
                
                $('#hdn_bank_number').val(bank_out_code);
                $('#hdn_bank_curr_code').val(bank_curr_code);
                $('#lbl_balance_curr').html('Balance (' + bank_curr_code + ')');
                $('#lbl_payment_curr').html('Total Payment (' + bank_curr_code + ')');
                
                $('#txt_bank_balance').numberbox('setValue', rowData.BankBalance);
                if($('#chk_fiscal_payment').is(':checked')){
                    if($('#hdn_bank_curr_code').val() == 'IDR'){
                        $('#dtg_item').datagrid('hideColumn', 'Payment_Currency');
                        $('#dtg_item').datagrid('showColumn', 'ItemCategoryCode');
                        $('#hdn_payment_status').val(0);
                    }
                    else{
                        $('#dtg_item').datagrid('showColumn', 'Payment_Currency');
                        $('#dtg_item').datagrid('hideColumn', 'ItemCategoryCode');
                        $('#hdn_payment_status').val(1);
                    }
                    $('.spn_bank_curr').text('(' + bank_curr_code + ')');
                    
					if(vend_curr_code == 'IDR'){
						if(bank_curr_code == 'IDR'){
							$('#txt_bank_curr_rate').prop('disabled', true);
						}
						else{
							$('#txt_bank_curr_rate').prop('disabled', false);
						}
					}
					else{
						if(bank_curr_code == 'IDR'){
							$('#txt_bank_curr_rate').prop('disabled', true);
						}
						else{
							$('#txt_bank_curr_rate').prop('disabled', false);
						}
					}
                }
                else{   
                    $('.spn_bank_curr').text('(' + bank_curr_code + ')');
                    
                    if(($('#hdn_vend_curr_code').val() == '') || ($('#hdn_vend_curr_code').val() == $('#hdn_bank_curr_code').val())){
                        $('#dtg_item').datagrid('hideColumn', 'Payment_Currency');
                        $('#dtg_item').datagrid('showColumn', 'ItemCategoryCode');
                        $('#hdn_payment_status').val(0);
                    }
                    else{
                        $('#dtg_item').datagrid('showColumn', 'Payment_Currency');
                        $('#dtg_item').datagrid('hideColumn', 'ItemCategoryCode');
                        $('#hdn_payment_status').val(1);
                    }
					
					if((bank_curr_code == vend_curr_code) && (vend_curr_code == 'IDR')){
						$('#txt_bank_curr_rate').prop('disabled', true);
					}
					else{
						$('#txt_bank_curr_rate').prop('disabled', false);
					}
                }   
                
                if($('#hdn_vend_curr_code').val() != 'IDR'){
                    $('#currency').html(vend_curr_code + ' / IDR');
                }
                else{
                    $('#currency').html(vend_curr_code + ' / ' + bank_curr_code);                    
                }

                if(vend_curr_code == 'IDR'){
					if(chk_no != ""){
						$('#txt_bank_curr_rate').numberbox('setValue', '<?php echo $txt_bank_curr_rate;?>');
					}
					else{
						$('#txt_bank_curr_rate').numberbox('setValue', rowData.CurrencyRate);
					}
                    $('#chk_fiscal_payment').prop('disabled', true);
                }
                else{
                    $('#chk_fiscal_payment').prop('disabled', false);
                    if($('#chk_fiscal_payment').is(':checked')){
						if($('#hdn_bank_curr_code').val() == 'IDR'){
							$('#txt_bank_curr_rate').numberbox('setValue', rowData.CurrencyRate);
							//$('#txt_bank_curr_rate').prop('disabled', true);
						}
						else{
							$('#txt_bank_curr_rate').numberbox('setValue', rowData.CurrencyRate);
							//$('#txt_bank_curr_rate').prop('disabled', false);
						}
					}
					else{
						
						if($('#hdn_bank_curr_code').val() == 'IDR'){
							if(chk_no != ""){
								$('#txt_bank_curr_rate').numberbox('setValue', '<?php echo $txt_bank_curr_rate;?>');
							}
							else{
								$('#txt_bank_curr_rate').numberbox('setValue', $('#txt_vendor_rate').numberbox('getValue'));
							}
							//$('#txt_bank_curr_rate').prop('disabled', true);
						}
						else{
							if(chk_no != ""){
								$('#txt_bank_curr_rate').numberbox('setValue', '<?php echo $txt_bank_curr_rate;?>');
							}
							else{
								$('#txt_bank_curr_rate').numberbox('setValue', rowData.CurrencyRate);
							}
							//$('#txt_bank_curr_rate').prop('disabled', false);
						}
					}
                }
                
                $('#txt_total_amount').numberbox('setValue', 0);
                $('#dtg_item').datagrid('reload'); 
            },onLoadSuccess:function(data){
                var data_rows   = data.rows;
                if(PInvNumber!=''){
                    if(data_rows.length>0){
                        $.each(data_rows,function(index,value){
                            if(value.CurrencyCode == CurrencyCode){
                                $('#cbg_bank').combogrid('setValue',value.AccountNumber);
                                return;
                            }
                        });
                    }
                }
            }
    });
    
    $('#txt_bank_curr_rate').numberbox({
        onChange:function(){
            var item    = $('#dtg_item').datagrid('getRows');
            curr_rate   = $('#txt_bank_curr_rate').numberbox('getValue');
            amount      = $('#txt_total_amount').numberbox('getValue');
            total_payment_amount    = 0;
            total_payment_currency  = 0;
            
            $.each(item, function(index, value){
                if(value.Paid == 'Yes'){
                    payment_amount      = parseFloat(value.Payment_Amount);
                    payment_currency    = parseFloat(value.Payment_Currency);

                    if(isNaN(payment_amount)){payment_amount = 0;}
                    if(isNaN(payment_currency)){payment_currency = 0;}
                        
                    if($('#hdn_payment_status').val() == 0) {
                        if($('#hdn_vend_curr_code').val() == 'IDR'){                
                            payment_currency    = payment_amount;
                        }
                        else{
                             if($('#chk_fiscal_payment').is(':checked')){
                                if($('#hdn_bank_curr_code').val() == 'IDR'){
                                    payment_currency = payment_amount * curr_rate;
                                }
                                else{
                                    payment_currency = payment_amount / curr_rate;
                                }
                            }
                            else{
                                payment_currency    = payment_amount * curr_rate;
                            } 
                            
                        }
                    }
                    else{
                        if($('#hdn_vend_curr_code').val() == 'IDR'){
                            payment_currency    = payment_amount / curr_rate;
                        }
                        else{
                            if($('#chk_fiscal_payment').is(':checked')){
                                if($('#hdn_bank_curr_code').val() == 'IDR'){
                                    payment_currency        = payment_amount * curr_rate;
                                }
                                else{
                                    payment_currency = payment_amount / curr_rate;
                                }
                            }
                            else{
                                payment_currency    = payment_amount * curr_rate;
                            } 
                        }
                    }                        

                    $('#dtg_item').datagrid('updateRow',{
                        index:index,
                        row:{Payment_Currency    : 0,
                                Payment_Amount   : 0,
                                Payment_DP       : 0,
                                Payment_Discount : 0,
                                Load_Rounding    : 0,
                                Select_DP        : '',
                                Invoice_Owing:value.InvoiceAmountCopy}
                    }); 
                        
                     
                    //$('#dtg_item').datagrid('reload');   
                    
                    total_payment_amount    += payment_amount;
                    total_payment_currency  += payment_currency;                                          
                }
            }); 
            $('#txt_total_amount').numberbox('setValue', 0);
        }
    });
    
    $('#tt_choose_dp').datagrid({
        onLoadSuccess:function(rowIndex){
            grid_dp      = $('#tt_choose_dp').datagrid('getRows');
            dp_array     = $('#hdn_pord_number').val();
            dp_code      = dp_array.split(',');

            for($ii=0; $ii<dp_code.length; $ii++){
                $.each(grid_dp, function(index, value) {
                    if(dp_code[$ii] == value.POrdNumber){
                        $('#tt_choose_dp').datagrid('updateRow',{
                            index   : index,
                            row     : {Status : 'Yes'}
                        });
                    }
                });                    
            }
         }
    });     
        
    $('#dtg_item').datagrid({
            onLoadSuccess:function(rowIndex){
				item                    = $('#dtg_item').datagrid('getRows');
                total_dp                = 0;
                total_payment_amount    = 0;
                total_payment_currency  = 0;
                total_invoice           = 0;
                total_owing             = 0;
                total_disc              = 0;
                loop_index_row_pi       = 0;
                loop_index_row_po       = 0;
                
                rate                    = $('#txt_bank_curr_rate').numberbox('getValue');
                $.each(item, function(index, value) {
                    payment_dp          = parseFloat(value.Payment_DP);
                    payment_amount      = parseFloat(value.Payment_Amount);
                    payment_currency    = parseFloat(value.Payment_Currency);
                    invoice_amount      = parseFloat(value.Invoice_Amount);
                    invoice_owing       = parseFloat(value.Invoice_Owing);
                    payment_disc        = parseFloat(value.Payment_Discount);
					currency_rate           = parseFloat($('#txt_bank_curr_rate').numberbox('getValue'));
					 
					//payment_currency    = currency_payment(currency_rate, value.Payment_Amount);
					owing = value.InvoiceAmountCopy - (value.Load_Rounding + value.Payment_Amount);
					
                    if(isNaN(payment_dp)){payment_dp = 0;}
                    if(isNaN(payment_amount)){payment_amount = 0;}
                    if(isNaN(payment_currency)){payment_currency = 0;}
                    
                    if(value.Paid == 'Yes'){
						$('#dtg_item').datagrid('updateRow',{
							index   : index,
							row     : {
								Invoice_Owing 		: owing,
								InvoiceOwingHiddenNew 	: owing
							}                  
						});
					
                        total_dp                += payment_dp;
                        total_payment_amount    += payment_amount;
                        total_payment_currency  += payment_currency;
                        total_invoice           += invoice_amount;
                        total_owing             += invoice_owing;
                        total_disc              += payment_disc;
                        
                        if(value.Invoice_Type == 'PO'){
                            loop_index_row_po           = loop_index_row_po +1;
                        }
                        else{
                            loop_index_row_pi           = loop_index_row_pi +1;
                        }
                    }
                });

                new_balance_dp = parseFloat($('#hdn_vend_saldo_dp').numberbox('getValue')) - parseFloat(total_dp);
                
               // $('#txt_vendor_dp_return').numberbox('setValue', new_balance_dp);
                
                $('#hdn_pi_status').numberbox('setValue',loop_index_row_pi);
                $('#hdn_po_status').numberbox('setValue',loop_index_row_po);
                
                $('#hdn_total_currency').numberbox('setValue',total_payment_currency);
                $('#hdn_PInvTotalAmount').numberbox('setValue', total_invoice);
                $('#hdn_PPayTotalOwing').numberbox('setValue', total_owing);
                //$('#hdn_PPayTotalDPUsed').numberbox('setValue', total_dp);
                $('#hdn_PPayTotalDiscount').numberbox('setValue', total_disc);
                
                
                if($('#chk_fiscal_payment').is(':checked')){
                    if($('#hdn_vend_curr_code').val() != 'IDR'){
                        if($('#hdn_bank_curr_code').val() == 'IDR'){
                            total_payment_curr = total_payment_amount;
                            $('#txt_total_amount').numberbox('setValue', total_payment_curr);
                        }
                        else{
                            total_payment_curr = total_payment_currency;
                            $('#txt_total_amount').numberbox('setValue', total_payment_curr);
                        }
                    }
                }
                else{
                    if($('#hdn_vend_curr_code').val() == 'IDR'){
                        if($('#hdn_bank_curr_code').val() == 'IDR'){
                            total_payment_curr = total_payment_amount;
                            $('#txt_total_amount').numberbox('setValue', total_payment_curr);
                        }
                        else{
                            total_payment_curr = total_payment_currency;
                            $('#txt_total_amount').numberbox('setValue', total_payment_curr);
                        }
                    }
                    else{
                        if($('#hdn_bank_curr_code').val() == 'IDR'){
                            total_payment_curr = total_payment_currency;
                            
                            $('#txt_total_amount').numberbox('setValue', total_payment_curr);
                        }
                        else{
                            total_payment_curr = total_payment_amount;
                            $('#txt_total_amount').numberbox('setValue', total_payment_curr);
                        }
                    }
                }
			},
		 
			onBeginEdit:function(rowIndex,rowDatax){
                editors = $('#dtg_item').datagrid('getEditors', rowIndex);
                
                paid_editor                 = $(editors[0].target); //Paid Editor
                invoice_type_editor         = $(editors[1].target); //Invoice Type
                invoice_no_editor           = $(editors[2].target); //Invoice Number
                invoice_date_editor         = $(editors[3].target); //Invoice Date
                invoice_amount_editor       = $(editors[4].target); //Invoice Amount
                invoice_owing_editor        = $(editors[5].target); //Owing Amount
                invoice_dp_editor           = $(editors[6].target); //DP Amount
                invoice_disc_editor         = $(editors[7].target); //Discount Amount
                invoice_rounding            = $(editors[8].target); //rounding 
                
                invoice_payment_editor      = $(editors[9].target); //Payment Amount
                invoice_paycurr_editor      = $(editors[10].target);//Payment Currency
                invoice_category_editor     = $(editors[11].target);//Category Code
                invoice_amount_editor_hide  = $(editors[16].target);//InvoiceAmountCopy
                invoice_select_dp           = $(editors[17].target);//SelectDP
				invoice_owing_temp          = $(editors[19].target);//Owing Temp
				
                /*====================== Daswan ===========================*/
                $(invoice_no_editor).click(function(){
                    var TransNo             = rowDatax.Invoice_Number;
                    var arrTransNo          = TransNo.split('/');
                    var string_ext          = arrTransNo[1];
                    var res                 = string_ext.substring(6,4);
                    if(res == 'PI'){
                        parent.tab_edit('Edit Purchase Invoice','list/purchases_invoice', 'icon-edit', 'edit',TransNo);
                    }else if(res == 'PO'){
                        parent.tab_edit('Edit Purchase Order','list/purchases_order', 'icon-edit', 'edit',TransNo);
                    }          
                });
                /*====================== End Daswan ===========================*/
                
                $(invoice_dp_editor).numberbox('setValue', $(invoice_dp_editor).numberbox('getValue') == 0 ? 0 : $(invoice_dp_editor).numberbox('getValue'));
                $(invoice_disc_editor).numberbox('setValue', $(invoice_disc_editor).numberbox('getValue') == 0 ? 0 : $(invoice_disc_editor).numberbox('getValue'));

                category_code   = $(invoice_category_editor).val();
                invoice_type    = $(invoice_type_editor).val();
                
                vendor_dp       = parseFloat($('#txt_vendor_dp_return').numberbox('getValue'));
                vendor_dp_hide  = parseFloat($('#hdn_vend_saldo_dp').numberbox('getValue'));
                
                if(isNaN(vendor_dp)){vendor_dp = 0;}
                if(isNaN(vendor_dp_hide)){vendor_dp_hide = 0;}
                
                /*====================on beg in edit saat grid di klik maka tombol save disable===================*/
                save_button_begendedit(SavePrivillage);
                /*=================================end============================================================*/
            
                $(invoice_type_editor).prop('readonly', true);
                $(invoice_no_editor).prop('readonly', true);
                $(invoice_date_editor).prop('readonly', true);
                $(invoice_amount_editor).prop('readonly', true);
                $(invoice_owing_editor).prop('readonly', true);
                $(invoice_dp_editor).prop('readonly', true);
                $(invoice_category_editor).prop('readonly', true);                    

                $(invoice_disc_editor).prop('readonly', true);
                $(invoice_payment_editor).prop('readonly', true);
                $(invoice_paycurr_editor).prop('readonly', true);
                $(invoice_rounding).prop('readonly', true);
                $(invoice_select_dp).prop('disabled',true);

                

                if($('#cbg_bank').combogrid('getValue') == ''){
                    $(paid_editor).val('No');                    
                    $.messager.alert('Warning','Silahkan pilih Bank Payment terlebih dahulu...!!','warning');                    
                }
                else{
                    if($(paid_editor).is(':checked')){
                        if(invoice_type == 'PO'){
							if(chk_no != ""){
								$(paid_editor).prop('disabled', true);
								$(invoice_dp_editor).prop('disable', true);
								$(invoice_disc_editor).prop('readonly', true);
								$(invoice_select_dp).prop('disabled',true);
								$(invoice_rounding).prop('readonly', true);
							}
							else{
								$(invoice_dp_editor).prop('disable', true);
								$(invoice_disc_editor).prop('readonly', true);
								$(invoice_select_dp).prop('disabled',true);
								
								if($('#hdn_vend_curr_code').val() == "IDR"){
									if($('#hdn_bank_curr_code').val() == "IDR"){
										$(invoice_rounding).prop('readonly', true);
									}
									else{
										$(invoice_rounding).prop('readonly', false);
									}
								}
								else{
									if($('#hdn_bank_curr_code').val() == "IDR"){
										$(invoice_rounding).prop('readonly', true);
									}
									else{
										$(invoice_rounding).prop('readonly', true);
									}
								}
							}
                        }
                        else if(category_code == 'PPn Fiscal'){
                            $(invoice_dp_editor).prop('disable', true);
                            $(invoice_disc_editor).prop('readonly', true);
                            $(invoice_select_dp).prop('disabled',true);
                            
                            if($('#hdn_vend_curr_code').val() == "IDR"){
                                if($('#hdn_bank_curr_code').val() == "IDR"){
                                    $(invoice_rounding).prop('readonly', true);
                                }
                                else{
                                    $(invoice_rounding).prop('readonly', false);
                                }
                            }
                            else{
                                if($('#hdn_bank_curr_code').val() == "IDR"){
                                    $(invoice_rounding).prop('readonly', true);
                                }
                                else{
                                    $(invoice_rounding).prop('readonly', false);
                                }
                            }
                        }
                        else{
							if(chk_no != ""){
								$(paid_editor).prop('disabled', true);
								$(invoice_rounding).prop('readonly', true);
								$(invoice_dp_editor).prop('readonly', true);
								$(invoice_disc_editor).prop('readonly', false);
								$(invoice_select_dp).prop('disabled',false);
							}
							else{
								$(invoice_dp_editor).prop('readonly', true);
								$(invoice_disc_editor).prop('readonly', false);
								$(invoice_select_dp).prop('disabled',false);
								
								if($('#hdn_vend_curr_code').val() == "IDR"){
									if($('#hdn_bank_curr_code').val() == "IDR"){
										$(invoice_rounding).prop('readonly', true);
									}
									else{
										$(invoice_rounding).prop('readonly', false);
									}
								}
								else{
									if($('#hdn_bank_curr_code').val() == "IDR"){
										$(invoice_rounding).prop('readonly', true);
									}
									else{
										$(invoice_rounding).prop('readonly', true);
									}
								}
							}
                        }
						
						if(chk_no != ""){
							$(invoice_payment_editor).prop('readonly', true);                        
							$(invoice_paycurr_editor).prop('readonly', true);
						}
						else{
							if($('#hdn_payment_status').val() == 0){
								$(invoice_payment_editor).prop('readonly', false);                        
								$(invoice_paycurr_editor).prop('readonly', true);                                                        
							}
							else{
								$(invoice_payment_editor).prop('readonly', true);                        
								$(invoice_paycurr_editor).prop('readonly', false);                            
							}
						}
                    }
                    else{
                        var hdn_po_status = $('#hdn_po_status').val();
                        var hdn_pi_status = $('#hdn_pi_status').val();
                        
                        if(hdn_po_status > 0){
                            if(invoice_type == 'PI'){
                                $(paid_editor).prop('disabled', true);  
                            }
                        }
                        else if(hdn_pi_status > 0){
                            if(invoice_type == 'PO'){
                                $(paid_editor).prop('disabled', true);  
                            }
                        }
                        
                        $(invoice_paycurr_editor).prop('readonly', true);                            
                        $(invoice_payment_editor).prop('readonly', true);   
                    }        
                    
                    $(paid_editor).click(function(){
                        if($(this).is(':checked')){
                            if((invoice_type == 'PO')){
                                $(invoice_dp_editor).prop('disable', true);
                                $(invoice_disc_editor).prop('readonly', true);
                                $(invoice_select_dp).prop('disabled',true);
                                
                                if($('#hdn_vend_curr_code').val() == "IDR"){
                                    if($('#hdn_bank_curr_code').val() == "IDR"){
                                        $(invoice_rounding).prop('readonly', true);
                                    }
                                    else{
                                        $(invoice_rounding).prop('readonly', false);
                                    }
                                }
                                else{
                                    if($('#hdn_bank_curr_code').val() == "IDR"){
                                        $(invoice_rounding).prop('readonly', true);
                                    }
                                    else{
                                        $(invoice_rounding).prop('readonly', true);
                                    }
                                }
                            }
                            else if(category_code == 'PPn Fiscal'){
                                $(invoice_dp_editor).prop('disable', true);
                                $(invoice_disc_editor).prop('readonly', true);
                                $(invoice_select_dp).prop('disabled',true);
                                
                                if($('#hdn_vend_curr_code').val() == "IDR"){
                                    if($('#hdn_bank_curr_code').val() == "IDR"){
                                        $(invoice_rounding).prop('readonly', true);
                                    }
                                    else{
                                        $(invoice_rounding).prop('readonly', false);
                                    }
                                }
                                else{
                                    if($('#hdn_bank_curr_code').val() == "IDR"){
                                        $(invoice_rounding).prop('readonly', true);
                                    }
                                    else{
                                        $(invoice_rounding).prop('readonly', false);
                                    }
                                }
                            }
                            else{
								$(invoice_dp_editor).prop('readonly', true);
								$(invoice_disc_editor).prop('readonly', false);
								$(invoice_select_dp).prop('disabled',false);
								
								if($('#hdn_vend_curr_code').val() == "IDR"){
									if($('#hdn_bank_curr_code').val() == "IDR"){
										$(invoice_rounding).prop('readonly', true);
									}
									else{
										$(invoice_rounding).prop('readonly', false);
									}
								}
								else{
									if($('#hdn_bank_curr_code').val() == "IDR"){
										$(invoice_rounding).prop('readonly', true);
									}
									else{
										$(invoice_rounding).prop('readonly', true);
									}
								}
                            }
							
							if($('#hdn_payment_status').val() == 0){
								$(invoice_payment_editor).prop('readonly', false);
								$(invoice_paycurr_editor).prop('readonly', true);
							}
							else{
								$(invoice_payment_editor).prop('readonly', true);                        
								$(invoice_paycurr_editor).prop('readonly', false);                            
							}        

							$(invoice_payment_editor).numberbox('setValue', 0);
							$(invoice_paycurr_editor).numberbox('setValue', 0);
                        }
                        else{
							invoice_amount = parseFloat($(invoice_amount_editor_hide).numberbox('getValue'));

							$(invoice_disc_editor).prop('readonly', true);
							$(invoice_payment_editor).prop('readonly', true);
							$(invoice_paycurr_editor).prop('readonly', true);
							$(invoice_rounding).prop('readonly', true);
							$(invoice_dp_editor).prop('readonly', true);
							$(invoice_select_dp).prop('disabled',true);

							$(invoice_owing_editor).numberbox('setValue', invoice_amount);
							$(invoice_dp_editor).numberbox('setValue', 0);
							$(invoice_disc_editor).numberbox('setValue', 0);
							$(invoice_payment_editor).numberbox('setValue', 0);
							$(invoice_paycurr_editor).numberbox('setValue', 0);
							$(invoice_rounding).numberbox('setValue', 0);
							(invoice_select_dp).val('');
                        }
                    });

                    $(invoice_dp_editor).numberbox({
                        onChange:function(){
                            if($(paid_editor).is(':checked')){
                                invoice_amount          = parseFloat($(invoice_amount_editor).numberbox('getValue'));
                                downpayment_amount      = parseFloat($(invoice_dp_editor).numberbox('getValue'));
                                discount_amount         = parseFloat($(invoice_disc_editor).numberbox('getValue'));
                                load_rounding           = parseFloat($(invoice_rounding).numberbox('getValue'));
                                currency_rate           = parseFloat($('#txt_bank_curr_rate').numberbox('getValue'));
                                payment_editor          = parseFloat($(invoice_payment_editor).numberbox('getValue'));
                                paymentcurr_editor      = parseFloat($(invoice_paycurr_editor).numberbox('getValue'));
                                invoice_amount_hide     = parseFloat($(invoice_amount_editor_hide).numberbox('getValue'));
                                

                                if(isNaN(invoice_amount)){invoice_amount = 0;}
                                if(isNaN(downpayment_amount)){downpayment_amount = 0;}
                                if(isNaN(discount_amount)){discount_amount = 0;}
                                if(isNaN(currency_rate)){currency_rate = 0;}
                                if(isNaN(load_rounding)){load_rounding = 0;}
                                if(isNaN(payment_editor)){payment_editor = 0;}
                                if(isNaN(paymentcurr_editor)){paymentcurr_editor = 0;}
                                if(isNaN(invoice_amount_hide)){invoice_amount_hide = 0;}
                                
                                total_amount  = (downpayment_amount + discount_amount + load_rounding + payment_editor);
                                
                                if(downpayment_amount > vendor_dp_hide){
                                    $.messager.alert('Warning','DownPayment tidak boleh lebih besar dari Total Saldo DP','warning',function(){
                                        $(invoice_dp_editor).numberbox('setValue', 0);
                                        $(invoice_disc_editor).numberbox('setValue', 0);
                                        $(invoice_payment_editor).numberbox('setValue', 0);
                                        $(invoice_paycurr_editor).numberbox('setValue', 0); 
                                        $(invoice_rounding).numberbox('setValue', 0);                                   
                                    });
                                }
                                else if(downpayment_amount > invoice_amount_hide){
                                    $.messager.alert('Warning','Downpayment tidak boleh lebih besar dari Invoice Amount','warning',function(){
                                        $(invoice_dp_editor).numberbox('setValue', 0);
                                        $(invoice_disc_editor).numberbox('setValue', 0);
                                        $(invoice_payment_editor).numberbox('setValue', 0);
                                        $(invoice_paycurr_editor).numberbox('setValue', 0);
                                        $(invoice_rounding).numberbox('setValue', 0);
                                    });
                                }
                                /*else if(total_amount > invoice_amount_hide){
                                    $.messager.alert('Warning','Total amount tidak boleh lebih besar dari Invoice Amount','warning',function(){
                                        $(invoice_dp_editor).numberbox('setValue', 0);
                                        $(invoice_disc_editor).numberbox('setValue', 0);
                                        $(invoice_payment_editor).numberbox('setValue', 0);
                                        $(invoice_paycurr_editor).numberbox('setValue', 0);
                                        $(invoice_rounding).numberbox('setValue', 0);
                                    });
                                }*/
                                
                                //payment_amount      = invoice_amount - (downpayment_amount + discount_amount);
                                //payment_currency    = currency_payment(currency_rate, payment_amount);
                                owing_amount        = invoice_amount_hide - (downpayment_amount + discount_amount + load_rounding + payment_editor);
                                
                                $(invoice_owing_editor).numberbox('setValue', owing_amount);
								$(invoice_owing_temp).numberbox('setValue', owing_amount);
                                //$(invoice_payment_editor).numberbox('setValue', payment_amount);
                                //$(invoice_paycurr_editor).numberbox('setValue', payment_currency);
                            }
                        }
                    }),

                    $(invoice_disc_editor).numberbox({
                        onChange:function(){
                            if($(paid_editor).is(':checked')){
                                invoice_amount          = parseFloat($(invoice_amount_editor).numberbox('getValue'));
                                downpayment_amount      = parseFloat($(invoice_dp_editor).numberbox('getValue'));
                                discount_amount         = parseFloat($(invoice_disc_editor).numberbox('getValue'));
                                currency_rate           = parseFloat($('#txt_bank_curr_rate').numberbox('getValue'));
                                load_rounding           = parseFloat($(invoice_rounding).numberbox('getValue'));
                                payment_editor          = parseFloat($(invoice_payment_editor).numberbox('getValue'));
                                paymentcurr_editor      = parseFloat($(invoice_paycurr_editor).numberbox('getValue'));
                                invoice_amount_hide     = parseFloat($(invoice_amount_editor_hide).numberbox('getValue'));

                                if(isNaN(invoice_amount)){invoice_amount = 0;}
                                if(isNaN(downpayment_amount)){downpayment_amount = 0;}
                                if(isNaN(discount_amount)){discount_amount = 0;}
                                if(isNaN(currency_rate)){currency_rate = 0;}
                                if(isNaN(load_rounding)){load_rounding = 0;}
                                if(isNaN(payment_editor)){payment_editor = 0;}
                                if(isNaN(paymentcurr_editor)){paymentcurr_editor = 0;}
                                if(isNaN(invoice_amount_hide)){invoice_amount_hide = 0;}
                                
                                total_amount  = (downpayment_amount + discount_amount + load_rounding + payment_editor);
                                /*if(total_amount > invoice_amount_hide){
                                    $.messager.alert('Warning','Total amount tidak boleh lebih besar dari Invoice Amount','warning',function(){
                                        $(invoice_dp_editor).numberbox('setValue', 0);
                                        $(invoice_disc_editor).numberbox('setValue', 0);
                                        $(invoice_payment_editor).numberbox('setValue', 0);
                                        $(invoice_paycurr_editor).numberbox('setValue', 0);
                                        $(invoice_rounding).numberbox('setValue', 0);
                                    });
                                }*/
                               /* payment_amount          = invoice_amount - (downpayment_amount + discount_amount);
                                payment_currency        = currency_payment(currency_rate, payment_amount);*/
                                owing_amount            = invoice_amount_hide - (downpayment_amount + discount_amount + load_rounding + payment_editor);

                                $(invoice_owing_editor).numberbox('setValue', owing_amount);
								$(invoice_owing_temp).numberbox('setValue', owing_amount);
                                //$(invoice_payment_editor).numberbox('setValue', payment_amount);
                                //$(invoice_paycurr_editor).numberbox('setValue', payment_currency);
                            }
                        }
                    }),
                    
                    $(invoice_rounding).numberbox({
                         onChange:function(){
                            if($(paid_editor).is(':checked')){
                                invoice_amount          = parseFloat($(invoice_amount_editor).numberbox('getValue'));
                                downpayment_amount      = parseFloat($(invoice_dp_editor).numberbox('getValue'));
                                discount_amount         = parseFloat($(invoice_disc_editor).numberbox('getValue'));
                                load_rounding           = parseFloat($(invoice_rounding).numberbox('getValue'));
                                currency_rate           = parseFloat($('#txt_bank_curr_rate').numberbox('getValue'));
                                payment_editor          = parseFloat($(invoice_payment_editor).numberbox('getValue'));
                                paymentcurr_editor      = parseFloat($(invoice_paycurr_editor).numberbox('getValue'));
                                invoice_amount_hide     = parseFloat($(invoice_amount_editor_hide).numberbox('getValue'));
                                
                                if(isNaN(invoice_amount)){invoice_amount = 0;}
                                if(isNaN(downpayment_amount)){downpayment_amount = 0;}
                                if(isNaN(discount_amount)){discount_amount = 0;}
                                if(isNaN(currency_rate)){currency_rate = 0;}
                                if(isNaN(load_rounding)){load_rounding = 0;}
                                if(isNaN(payment_editor)){payment_editor = 0;}
                                if(isNaN(paymentcurr_editor)){paymentcurr_editor = 0;}
                                if(isNaN(invoice_amount_hide)){invoice_amount_hide = 0;}
                                
                                total_amount  = (downpayment_amount + discount_amount + load_rounding + payment_editor);
                                /*if(total_amount > invoice_amount_hide){
                                    $.messager.alert('Warning','Total amount tidak boleh lebih besar dari Invoice Amount','warning',function(){
                                        $(invoice_dp_editor).numberbox('setValue', 0);
                                        $(invoice_disc_editor).numberbox('setValue', 0);
                                        $(invoice_payment_editor).numberbox('setValue', 0);
                                        $(invoice_paycurr_editor).numberbox('setValue', 0);
                                        $(invoice_rounding).numberbox('setValue', 0);
                                    });
                                }*/
                                //payment_amount          = invoice_amount - (downpayment_amount + discount_amount + load_rounding);
                                //payment_currency        = currency_payment(currency_rate, payment_amount);
                                owing_amount            = invoice_amount_hide - (downpayment_amount + discount_amount + load_rounding + payment_editor);

                                $(invoice_owing_editor).numberbox('setValue', owing_amount);
								$(invoice_owing_temp).numberbox('setValue', owing_amount);
                                //$(invoice_payment_editor).numberbox('setValue', payment_amount);
                                //$(invoice_paycurr_editor).numberbox('setValue', payment_currency);
                            }
                         }
                    }),
                    
                    $(invoice_payment_editor).numberbox({
                        onChange:function(){
                            if(($(paid_editor).is(':checked')) && ($('#hdn_payment_status').val() == 0)){
                                invoice_amount          = parseFloat($(invoice_amount_editor).numberbox('getValue'));
                                downpayment_amount      = parseFloat($(invoice_dp_editor).numberbox('getValue'));
                                discount_amount         = parseFloat($(invoice_disc_editor).numberbox('getValue'));
                                payment_amount          = parseFloat($(invoice_payment_editor).numberbox('getValue'));
                                currency_rate           = parseFloat($('#txt_bank_curr_rate').numberbox('getValue'));
                                load_rounding           = parseFloat($(invoice_rounding).numberbox('getValue'));
                                invoice_amount_hide     = parseFloat($(invoice_amount_editor_hide).numberbox('getValue'));
                                
                                if(isNaN(invoice_amount)){invoice_amount = 0;}
                                if(isNaN(downpayment_amount)){downpayment_amount = 0;}
                                if(isNaN(discount_amount)){discount_amount = 0;}
                                if(isNaN(payment_amount)){payment_amount = 0;}
                                if(isNaN(currency_rate)){currency_rate = 1;}
                                if(isNaN(load_rounding)){load_rounding = 0;}
                                if(isNaN(invoice_amount_hide)){invoice_amount_hide = 0;}

                                total_amount            = invoice_amount_hide - (downpayment_amount + discount_amount + load_rounding);
                                
                                owing               = invoice_amount_hide - (downpayment_amount + discount_amount + payment_amount);
                                payment_currency    = currency_payment(currency_rate, payment_amount);
                                
                                $(invoice_owing_editor).numberbox('setValue', owing);
								$(invoice_owing_temp).numberbox('setValue', owing);
                                $(invoice_paycurr_editor).numberbox('setValue', payment_currency);  
                                    
                                /*if(payment_amount > total_amount){
                                    $.messager.alert('Warning','Payment Amount tidak boleh lebih besar dari Invoice Amount','warning',function(){
                                        $(invoice_payment_editor).numberbox('setValue', 0);
                                        $(invoice_paycurr_editor).numberbox('setValue', 0); 
                                        $(invoice_dp_editor).numberbox('setValue', 0);
                                        $(invoice_disc_editor).numberbox('setValue', 0);  
                                        $(invoice_rounding).numberbox('setValue', 0);                                 
                                    });
                                }
                                else{
                                    owing               = invoice_amount_hide - (downpayment_amount + discount_amount + payment_amount);
                                    payment_currency    = currency_payment(currency_rate, payment_amount);
                                    
                                    $(invoice_owing_editor).numberbox('setValue', owing);
                                    $(invoice_paycurr_editor).numberbox('setValue', payment_currency);                                
                                }*/
                            }
                        }
                    }),
                    
                    $(invoice_select_dp).click(function(){
                        
                        vendor_code     = $('#cbg_vendor').combogrid('getValue');
                        pord_number     = $(invoice_select_dp).val();
                        
                        $('#tt_choose_dp').datagrid('load',{
                            VendorCode : vendor_code
                        });

                        $('#hdn_pord_number').val(pord_number);
                        
                        modal_open('Purchase Down Payment','dialog_asset'); 
                    }),
                
                    $(invoice_paycurr_editor).numberbox({
                        onChange:function(){
                            if(($(paid_editor).is(':checked')) && ($('#hdn_payment_status').val() == 1)){
                                invoice_amount          = parseFloat($(invoice_amount_editor).numberbox('getValue'));
                                downpayment_amount      = parseFloat($(invoice_dp_editor).numberbox('getValue'));
                                discount_amount         = parseFloat($(invoice_disc_editor).numberbox('getValue'));
                                paycurr_amount          = parseFloat($(invoice_paycurr_editor).numberbox('getValue'));
                                currency_rate           = parseFloat($('#txt_bank_curr_rate').numberbox('getValue'));
                                invoice_amount_hide     = parseFloat($(invoice_amount_editor_hide).numberbox('getValue'));

                                if(isNaN(invoice_amount)){invoice_amount = 0;}
                                if(isNaN(downpayment_amount)){downpayment_amount = 0;}
                                if(isNaN(discount_amount)){discount_amount = 0;}
                                if(isNaN(paycurr_amount)){paycurr_amount = 0;}
                                if(isNaN(invoice_amount_hide)){invoice_amount_hide = 0;}

                                total_amount            = invoice_amount_hide - (downpayment_amount + discount_amount);
                                payment_amount          = amount_payment(currency_rate, paycurr_amount);
                                $(invoice_payment_editor).numberbox('setValue', payment_amount);
                                
                                owing = invoice_amount_hide - (downpayment_amount + discount_amount + payment_amount);
                                $(invoice_owing_editor).numberbox('setValue', owing);
                                $(invoice_owing_temp).numberbox('setValue', owing);    
                                /*if(payment_amount > total_amount){
                                    $.messager.alert('Warning','Payment Amount tidak boleh lebih besar dari Invoice Amount','warning',function(){
                                        $(invoice_payment_editor).numberbox('setValue', 0);
                                        $(invoice_paycurr_editor).numberbox('setValue', 0);  
                                        $(invoice_dp_editor).numberbox('setValue', 0);
                                        $(invoice_disc_editor).numberbox('setValue', 0);  
                                        $(invoice_rounding).numberbox('setValue', 0);                                  
                                    });
                                }
                                else{
                                    owing = invoice_amount_hide - (downpayment_amount + discount_amount + payment_amount);
                                    $(invoice_owing_editor).numberbox('setValue', owing);
                                }*/
                            }
                        }
                    });
                }
            },
            onEndEdit:function(rowIndex, rowData, changes){
                item                    = $('#dtg_item').datagrid('getRows');
                total_dp                = 0;
                total_payment_amount    = 0;
                total_payment_currency  = 0;
                total_invoice           = 0;
                total_owing             = 0;
                total_disc              = 0;
                loop_index_row_pi       = 0;
                loop_index_row_po       = 0;
                
                rate                    = $('#txt_bank_curr_rate').numberbox('getValue');
                /*====================on beg in edit saat grid di klik maka tombol save disable===================*/
                save_button_endedit(SavePrivillage);
                /*=================================end============================================================*/
                
                $.each(item, function(index, value) {
                    payment_dp          = parseFloat(value.Payment_DP);
                    payment_amount      = parseFloat(value.Payment_Amount);
                    payment_currency    = parseFloat(value.Payment_Currency);
                    invoice_amount      = parseFloat(value.Invoice_Amount);
                    invoice_owing       = parseFloat(value.Invoice_Owing);
                    payment_disc        = parseFloat(value.Payment_Discount);

                    if(isNaN(payment_dp)){payment_dp = 0;}
                    if(isNaN(payment_amount)){payment_amount = 0;}
                    if(isNaN(payment_currency)){payment_currency = 0;}
                    
                    if(value.Paid == 'Yes'){
                        total_dp                += payment_dp;
                        total_payment_amount    += payment_amount;
                        total_payment_currency  += payment_currency;
                        total_invoice           += invoice_amount;
                        total_owing             += invoice_owing;
                        total_disc              += payment_disc;
                        
                        if(value.Invoice_Type == 'PO'){
                            loop_index_row_po           = loop_index_row_po +1;
                        }
                        else{
                            loop_index_row_pi           = loop_index_row_pi +1;
                        }
                    }
                });

                new_balance_dp = parseFloat($('#hdn_vend_saldo_dp').numberbox('getValue')) - parseFloat(total_dp);
                
               // $('#txt_vendor_dp_return').numberbox('setValue', new_balance_dp);
                
                $('#hdn_pi_status').numberbox('setValue',loop_index_row_pi);
                $('#hdn_po_status').numberbox('setValue',loop_index_row_po);
                
                $('#hdn_total_currency').numberbox('setValue',total_payment_currency);
                $('#hdn_PInvTotalAmount').numberbox('setValue', total_invoice);
                $('#hdn_PPayTotalOwing').numberbox('setValue', total_owing);
                //$('#hdn_PPayTotalDPUsed').numberbox('setValue', total_dp);
                $('#hdn_PPayTotalDiscount').numberbox('setValue', total_disc);
                
                
                if($('#chk_fiscal_payment').is(':checked')){
                    if($('#hdn_vend_curr_code').val() != 'IDR'){
                        if($('#hdn_bank_curr_code').val() == 'IDR'){
                            total_payment_curr = total_payment_amount;
                            $('#txt_total_amount').numberbox('setValue', total_payment_curr);
                        }
                        else{
                            total_payment_curr = total_payment_currency;
                            $('#txt_total_amount').numberbox('setValue', total_payment_curr);
                        }
                    }
                }
                else{
                    if($('#hdn_vend_curr_code').val() == 'IDR'){
                        if($('#hdn_bank_curr_code').val() == 'IDR'){
                            total_payment_curr = total_payment_amount;
                            $('#txt_total_amount').numberbox('setValue', total_payment_curr);
                        }
                        else{
                            total_payment_curr = total_payment_currency;
                            $('#txt_total_amount').numberbox('setValue', total_payment_curr);
                        }
                    }
                    else{
                        if($('#hdn_bank_curr_code').val() == 'IDR'){
                            total_payment_curr = total_payment_currency;
                            
                            $('#txt_total_amount').numberbox('setValue', total_payment_curr);
                        }
                        else{
                            total_payment_curr = total_payment_amount;
                            $('#txt_total_amount').numberbox('setValue', total_payment_curr);
                        }
                    }
                }
            }
    });
    
        $("#fm_purchase_payment").click(function() {
            $('#dtg_item').edatagrid('saveRow');
    	});
    });
    
    function get_po_detail_dp(vendor_code){
        url = base_url + 'list/purchases_payment/get_pordetail_dp_return';  
        
        $.post(url, {vendor_code:vendor_code}, function(result){
            
            if((isNaN(result.dp_vendor)) || (result.dp_vendor == null)){
                payment_amount_dp = 0;
            }
            else{
                payment_amount_dp = result.dp_vendor;
            }
            
            $('#txt_vendor_dp_return').numberbox('setValue', payment_amount_dp);
            $('#hdn_vend_saldo_dp').numberbox('setValue',payment_amount_dp);
            $('#hdn_PPayTotalDPUsed').numberbox('setValue',payment_amount_dp);
            
        },'json');
    }
    
    function get_po_detail_dp_invoice(vendor_code){
        url = base_url + 'list/purchases_payment/get_pordetail_dp_invoice';  
        
        $.post(url, {vendor_code:vendor_code}, function(result){
            if (result.status === true){
                if((isNaN(result.dp_vendor_invoice)) || (result.dp_vendor_invoice == null)){
                    payment_amount = 0;
                }
                else{
                    payment_amount = result.dp_vendor_invoice;
                }
                $('#txt_vendor_dp').numberbox('setValue', payment_amount);
            } 
        },'json');
    }
    
    function fiscal_payment(){
        if($('#chk_fiscal_payment').is(':checked')){
            
            $('#dtg_item').datagrid('showColumn', 'InvoiceFiscalRate');
            $('#dtg_item').datagrid('hideColumn', 'Invoice_Date');
            
			bank_curr_code = $('#hdn_bank_curr_code').val();
			vend_curr_code = $('#hdn_vend_curr_code').val();
			
            $('#dtg_item').datagrid('load',{
                VendorCode      : $('#cbg_vendor').combogrid('getValue'),
                PPnFiscalStatus : 1
            });
                    
            if($('#hdn_bank_curr_code').val() == 'IDR'){
                $('.spn_vend_curr').text('(' + $('#hdn_bank_curr_code').val() + ')');
                $('#dtg_item').datagrid('hideColumn', 'Payment_Currency');
                $('#dtg_item').datagrid('showColumn', 'ItemCategoryCode');
                $('#hdn_payment_status').val(0);
            }
            else{
                $('.spn_vend_curr').text('(' + 'IDR' + ')');
                $('#dtg_item').datagrid('showColumn', 'Payment_Currency');
                $('#dtg_item').datagrid('hideColumn', 'ItemCategoryCode');
                $('#hdn_payment_status').val(1);
            }
			
			if(vend_curr_code == 'IDR'){
				if(bank_curr_code == 'IDR'){
					$('#txt_bank_curr_rate').prop('disabled', true);
				}
				else{
					$('#txt_bank_curr_rate').prop('disabled', false);
				}
			}
			else{
				if(bank_curr_code == 'IDR'){
					$('#txt_bank_curr_rate').prop('disabled', true);
					$('#txt_bank_curr_rate').numberbox('setValue', 1);
				}
				else{
					$('#txt_bank_curr_rate').prop('disabled', false);
				}
			}
        }
        else{
            location.reload();      
        }
    }
    
    function currency_payment(currency_rate, payment){  
        if($('#hdn_payment_status').val() == 0){
            if($('#hdn_vend_curr_code').val() == 'IDR'){                
                payment_curr = payment;
            }
            else{
                payment_curr = payment * currency_rate;
            }
        }
        else{
            if($('#hdn_vend_curr_code').val() == 'IDR'){
                payment_curr = payment / currency_rate;
            }
            else{
                payment_curr = payment * currency_rate;                                
            }
        }
        return payment_curr;
    }

    function amount_payment(currency_rate, payment_curr){  
        if($('#hdn_payment_status').val() == 0){
            if($('#hdn_vend_curr_code').val() == 'IDR'){                
                payment = payment_curr;
            }
            else{
                payment = payment_curr / currency_rate;
            }
        }
        else{
            if($('#hdn_vend_curr_code').val() == 'IDR'){
                payment = payment_curr * currency_rate;
            }
            else{
                if($('#chk_fiscal_payment').is(':checked')){
                    payment = payment_curr * currency_rate;
                }
                else{
                    payment = payment_curr / currency_rate; 
                }                             
            }
        }

        return payment;
    }
    
    function choose_dp(){
       $('#tt_choose_dp').edatagrid('saveRow');
        $('#dtg_item').edatagrid('saveRow');
        
        var selectedrow             = $('#dtg_item').datagrid('getSelected');
        var selected_row_tt_dp      = $('#tt_choose_dp').datagrid('getSelected');
        var row_index               = $('#dtg_item').datagrid('getRowIndex', selectedrow);
        var list_dp                 = $('#tt_choose_dp').datagrid('getRows');
        var list_dtg_grid           = $('#dtg_item').datagrid('getRows');
         
        var pord_number     = new Array();
        var dp_payment      = 0;
        var dp_payment_real = 0;
        var invoice_owing   = 0;
        var owing_back      = 0;
        var dp_select       = 0;
        var return_qty      = $('#hdn_qty_return').val();
        var flag            = true;
        
        $.each(list_dtg_grid, function(index,value){
            if(selected_row_tt_dp.POrdNumber == value.Select_DP){
                $.messager.alert('Unseccessfull','Maaf No Po Sudah terpakai silahkan ganti No PO yang lain.','error',function(){    
                    //$('#tt_choose_dp').datagrid('reload');
                    //$('#dtg_item').datagrid('reload'); 
					
					$('#dtg_item').datagrid('updateRow',{
						index   : row_index,
						row     : {
							Select_DP 			: '',
							Payment_DP 			: 0,
							Payment_Discount 	: 0,
							Load_Rounding 		: 0,
							Payment_Amount 		: 0,
							Payment_Currency 	: 0,
							Invoice_Owing 		: selectedrow.InvoiceAmountCopy,
							InvoiceOwingHiddenNew 	: selectedrow.InvoiceAmountCopy
						}                  
					}); 
                });
                var flag            = false;
            }
        }); 
        
        if(flag != false){
            $.each(list_dp, function(row_index, value) {
                if(value.Status == 'Yes'){
                    pord_number.push(value.POrdNumber);
                    dp_payment  = parseFloat(value.PaymentAmount_SUM);
                    
                    if(parseFloat(dp_payment) > parseFloat(selectedrow.InvoiceAmountCopy)){
                        dp_payment_real_grid = parseFloat(selectedrow.InvoiceAmountCopy - selectedrow.Payment_Discount - selectedrow.Payment_Amount);
                    }
                    else{
                        dp_payment_real_grid = parseFloat(dp_payment);
                    }
                    
                    owing_back      = parseFloat(selectedrow.InvoiceAmountCopy);
                    invoice_owing   = parseFloat(selectedrow.InvoiceAmountCopy) - (parseFloat(dp_payment_real_grid) + parseFloat(selectedrow.Payment_Discount) + parseFloat(selectedrow.Payment_Amount));
                    if(invoice_owing > 0){
						invoice_owing_real = invoice_owing;
					}
					else{
						invoice_owing_real = 0;
					}
					
					dp_payment_real = parseFloat(dp_payment_real_grid);
                    
                    payment_dp      = parseFloat(value.PaymentAmount_SUM);
                    if(isNaN(payment_dp)){payment_dp = 0;}
                    total_dp  += payment_dp;
                    
                    
                    dp_select++;
                }
                else{
                    owing_back      = parseFloat(selectedrow.InvoiceAmountCopy);
                }
            });
            
            if(dp_select <= 0){
                $('#dtg_item').datagrid('updateRow',{
                    index   : row_index,
                    row     : {
                        Select_DP       		: '',
                        Payment_DP      		: 0,
						Payment_Discount 		: 0,
						Load_Rounding 			: 0,
						Payment_Amount 			: 0,
						Payment_Currency 		: 0,
                        Invoice_Owing   		: owing_back,
						InvoiceOwingHiddenNew 	: owing_back
                    }                  
                });
    
                modal_close('dialog_asset');
            }
            else if(dp_select != 1){
                $.messager.alert('Warning','Jumlah Purchase Order Number tidak boleh lebih dari satu (1)','warning');
            }
            else{
                pord_number     = pord_number.join();
                $('#dtg_item').datagrid('updateRow',{
                    index   : row_index,
                    row     : {
                        Select_DP       : pord_number,
                        Payment_DP      : dp_payment_real,
                        Invoice_Owing   : invoice_owing_real,
						InvoiceOwingHiddenNew : invoice_owing_real
                    }                  
                });
                
                $('#hdn_PPayTotalDPUsed').numberbox('setValue', total_dp);
                
                modal_close('dialog_asset');
            }
        }
    }
    
    function insert_data(){
        $('#dtg_item').edatagrid('saveRow');
        
        total_row           = $('#dtg_item').datagrid('getData').total;
        selected_status     = false;
        amount_lessnull     = false;
        
        flag_total_amount   = false;
        no_invoice          = "";
        
        var BankCurrncyCode = $('#hdn_bank_curr_code').val();
        pi_total_amount     = 0;
        pi_total_amount_new = 0;
        
        if(total_row <= 0){
            $.messager.alert('Warning','Purchase Payment tidak dapat disimpan, karena tidak ditemukan data pada list item','warning');
        }
        else {
            item    = $('#dtg_item').datagrid('getRows');
            
            $.each(item, function(index, value) {
                if(value.Paid == 'Yes'){
                    selected_status = true;
                    pi_total_amount     = parseFloat(pi_total_amount) + parseFloat(value.Invoice_Amount);
                    pi_total_amount_new = parseFloat(value.Load_Rounding) + parseFloat(value.Payment_Amount);
                    invoice_amount      = value.Invoice_Amount;
                    total_all_payment   = (parseFloat(value.Payment_DP) + parseFloat(value.Payment_Discount) + parseFloat(value.Load_Rounding) + parseFloat(value.Payment_Amount));
                    
                    if(total_all_payment > value.InvoiceAmountCopy){
                        flag_total_amount   = true;
                        no_invoice          = value.Invoice_Number; 
                    }
                    
                    if(value.Payment_DP > 0){
                        amount_lessnull = false;
                    }
                    else{
                        if((value.Payment_Amount <= 0) || (value.Payment_Currency <= 0)) {
                            amount_lessnull = true;
                        }
                    }
                }
            });
            
            if(BankCurrncyCode == 'IDR'){
                total_pi        = pi_total_amount;
                total_payment   = $('#txt_total_amount').numberbox('getValue');
            }
            else{
                total_pi        = pi_total_amount_new;
                total_payment   = invoice_amount;
            }
        
            if(selected_status == false){
                $.messager.alert('Warning','Tidak ada item yang dipilih untuk disimpan','warning');
            }
            else if(flag_total_amount != false){
                $.messager.alert('Warning','Payment melebihi jumlah owing yang harus dibayar, No Invoice : '+no_invoice+'','warning',function(){
                    //$('#dtg_item').datagrid('reload'); 
                    $('#txt_total_amount').numberbox('setValue', 0); 
                }); 
            }
            else if(amount_lessnull){
                $.messager.alert('Warning','Payment Amount tidak boleh lebih kecil atau sama dengan nol','warning');
            }
            else if($('#hdn_lock_status').val() == 1){
                $.messager.alert('Warning','Maaf masih ada transaksi yang belum di approve di Purchases Payment, untuk vendor ini','warning');
            }
            else{
                if($('#chk_fiscal_payment').is(':checked')){
                    if(total_pi != total_payment){
                        $.messager.alert('Informasi','Maaf transaksi anda gagal, silahkan isi nilai sesuai Invoice Amount untuk Fiscal Payment','info');
                        return false;
                    }
                    else{
                        var item    = $('#dtg_item').datagrid('getRows');
                        var detail  = JSON.stringify(item);
        
                        $("#detail").val(detail);
                        save_transaction('<?=$menu_link?>','#fm_purchase_payment');
                    }
                }
                else{
                    var item    = $('#dtg_item').datagrid('getRows');
                    var detail  = JSON.stringify(item);
    
                    $("#detail").val(detail);
                    save_transaction('<?=$menu_link?>','#fm_purchase_payment');
                }
            }
        }
    }

    
</script>