<!---- 
    Author 	: Herpan / 12-08-2014	
    Revisi 	: Ferry / 12-01-2014
-->

	<?php
		//---------Untuk privellege jika dia ada tombol save----------
		if($group_user->SavePrivillage == 1){
			$cek_autority_save  = "";
		}
		else{
			$cek_autority_save 	= "disabled";
		}
		//-------------end----------------------------
	?>	

<style>
.datagrid-header-row [field='ck'] input{
	display:none;
}
</style>

<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 Invoice', '<?=$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 of 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_invoice" name="fm_purchase_invoice" method="post">
                <input type="hidden" name="detail" id="detail"/>
                <input type="hidden" name="hdn_pord_no" id="hdn_pord_no"/>
                <input type="hidden" name="hdn_ppn10_pcnt" id="hdn_ppn10_pcnt"/>
                <input type="hidden" name="hdn_pph22_pcnt" id="hdn_pph22_pcnt"/>
                <input type="hidden" name="hdn_pph23_pcnt" id="hdn_pph23_pcnt"/>

                <input type="hidden" name="hdn_currency_code" id="hdn_currency_code"/>
                <input type="hidden" name="hdn_branch_code" id="hdn_branch_code"/>
                <input type="hidden" name="hdn_taxes_status" id="hdn_taxes_status"/>
                <input type="hidden" name="hdn_intax_status" id="hdn_intax_status"/>

            	<!-- Form Header -->
                <table width="100%" height="140" cellpadding="0" cellspacing="0">
                    <tr>
                        <td width="66%" valign="top">
                            <table width="100%" cellpadding="0" cellspacing="1">
                                <tr height="20">
                                    <td width="120"><b>Vendor Name</b></td>
                                    <td width="10"><b>:</b></td>
                                    
                                    <td>
                                        <select name="cbg_vendor" class="easyui-combogrid" id="cbg_vendor" style="width:180px;height:20px" required
                                            data-options="
                                                panelWidth: 445,
                                                idField: 'VendorCode',
                                                textField: 'VendorName',
                                                url: '<?php echo base_url(); ?>custom/get_data/vendor',
                                                method: 'get',
                                                columns: [[
                                                    {field:'VendorCode', title:'Vendor No', halign:'center', align:'center', width:110},
                                                    {field:'VendorName',title:'Vendor Name', halign:'center', width:250}
                                                ]],
                                                fitColumns: true
                                            ">
                                        </select>

                                        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-sales'" onClick="select_ri();">Select RI</a>
                                        
                                        <span class="disableme" style="display:none;" valign="middle" halign='center'>
                                            <input type="checkbox" name="chk_taxtable" id="chk_taxtable" value="1" onClick="set_ppn_value();" disabled>Taxable
                                        </span>
                                        
                                        <span class="disableme1" style="display:none;" valign="middle" halign='center'>
                                            <input type="checkbox" name="chk_inclusive" id="chk_inclusive" value="1" disabled>Inclusive Tax
                                        </span>
                                    </td>

                                    <td width='132' class="disableme2" style="display:none;" valign="middle" >
                                        <input type="checkbox" name="chk_vendor_dp" id="chk_vendor_dp" value='1' disabled/>Vendor Needs DP
                                    </td>
                                </tr>
                                
                                <tr id="row_currency_rate" style="display:none;">
                                    <td><b>Currency Rate</b></td>
                                    <td><b>:</b></td>
                                    
                                    <td colspan="5">
                                        <input type="text" class="easyui-numberbox currency" name="txt_currency_rate" id="txt_currency_rate" style="width:70px;text-align:right;" readonly
                                            data-options="min:1, precision:2, decimalSeparator:',', groupSeparator:'.'"/>
                                        <span id="currency" style="font-weight:bold"></span>
                                    </td>
                                </tr>
                                <tr id="row_fiscal_rate" style="display:none;">
                                    <td><b>Fiscal Rate</b></td>
                                    <td><b>:</b></td>
                                    
                                    <td colspan="5">
                                        <input type="text" class="easyui-numberbox currency" name="txt_fiscal_rate" id="txt_fiscal_rate" style="width:70px;text-align:right;" readonly
                                            data-options=" min:1, precision:2, decimalSeparator:',', groupSeparator:'.'"/>
                                        <span id="fiscal" style="font-weight:bold"></span>
                                    </td>
                                </tr>

                                <tr height="20">
                                    <td width="120" valign="top" style="padding-top:4px;"><b>Vendor Address</b></td>
                                    <td width="10" valign="top" style="padding-top:4px;"><b>:</b></td>
                                    
                                    <td colspan='2'>
                                        <textarea name="txa_address" id="txa_address" rows="4" style="width:254px;resize:none" readonly></textarea>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    
                        <td width="34%" valign="top">
                            <table width="100%" cellpadding="0" cellspacing="1">
                                <tr height="20">
                                    <td width="206" ></td>
                                    <td width="122" style="background:#333399;text-align:center;color:#FFF;">PI. Date</td>
                                    <td width="122" style="background:#333399;text-align:center;color:#FFF;">Terms</td>
                                </tr>

                                <tr height="20">
                                    <td >
                                    	
                                    </td>
                                    <td><input type="text" name="txt_pinv_date" id="txt_pinv_date" class="easyui-datebox validBackward" required="required" value="today" style="width:190px;height:20px" editable="false" /></td>

                                    <td>
                                        <select class="combo-grid" required="required"
                                            name="cbo_terms" id="cbo_terms" 
                                            style="width:122px;height:20px;" 
                                            data-options="
                                                panelWidth: 250,
                                                idField: 'TermspaymentID',
                                                textField: 'TermDesc',
                                                url:'<?=base_url()?>custom/get_data/termspayment',                                        
                                                method: 'get',
                                                columns: [[
                                                    {field:'TermspaymentID',title:'Terms payment ID',width:90,halign:'center'},
                                                    {field:'TermDesc',title:'Term Desc',width:90,halign:'center'}
                                                ]],
                                                fitColumns: true
                                            ">
                                        </select>
                                    </td>
                                </tr>
                                
                                <tr height="20">
                                    <!-- <td style="background:#333399;text-align:center;color:#FFF;">RI. No.</td>
                                    <td style="background:#333399;text-align:center;color:#FFF;">RI. Date</td> -->
                                    <td></td>
                                    <td style="background:#333399;text-align:center;color:#FFF;">Vendor Inv. No.</td>
                                    <td style="background:#333399;text-align:center;color:#FFF;">FOB</td>
                                </tr>
                                
                                <tr height="20">
                                    <!--<td>--><input type="hidden" name="txt_prec_no" id="txt_prec_no" class="easyui-validatebox textbox" style="width:200px;text-align:center;" required readonly /><!--</td>-->
                                    <!--<td>--><input type="hidden" name="txt_prec_date" id="txt_prec_date" class="easyui-validatebox textbox" style="width:120px;" required readonly /><!--</td>-->
                                     <td></td>
                                     <td><input type="text" name="txt_vendor_inv_no" id="txt_vendor_inv_no" class="easyui-validatebox textbox" style="width:190px;height:18px;text-transform:uppercase;text-align:center;"/></td>
                                    <td>
                                        <select class="combo-grid" required="required"
                                            name="cbo_fob" id="cbo_fob" 
                                            style="width:122px;height:20px;" 
                                            data-options="
                                                panelWidth: 250,
                                                idField: 'FobID',
                                                textField: 'FobName',
                                                url:'<?=base_url()?>custom/get_data/fob',                                     
                                                method: 'get',
                                                columns: [[
                                                    {field:'FobID',title:'FOB ID',width:90,halign:'center'},
                                                    {field:'FobName',title:'FOB Name',width:90,halign:'center'}
                                                ]],
                                                fitColumns: true
                                            ">
                                        </select>
                                        
                                    </td>
                                </tr>
                                
                                <tr height="20">
                                    <!-- <td style="background:#333399;text-align:center;color:#FFF;">Vendor Inv. No.</td> -->
                                    <td>
                                    <td style="background:#333399;text-align:center;color:#FFF;">Ship Date</td>
                                    <td style="background:#333399;text-align:center;color:#FFF;">Ship Via</td>
                                </tr>
                                
                                <tr height="20">
                                   <!--  <td><input type="text" name="txt_vendor_inv_no" id="txt_vendor_inv_no" class="easyui-validatebox textbox" style="width:200px;height:18px;text-transform:uppercase;text-align:center;"/></td> -->
                                   <td></td>
                                    <td><input type="text" name="txt_shipment_date" id="txt_shipment_date" class="easyui-datebox validBackward" style="width:190px;height:20px" readonly /></td>
                                    
                                    <td>                                    
                                        <select class="combo-grid" required="required"
                                            name="cbo_ship_via" id="cbo_ship_via" 
                                            style="width:122px;height:20px;" 
                                            data-options="
                                                panelWidth: 250,
                                                idField: 'ShipmentID',
                                                textField: 'ShipmentName',
                                                url:'<?=base_url()?>custom/get_data/shipment',                                     
                                                method: 'get',
                                                columns: [[
                                                    {field:'ShipmentID',title:'Shipment ID',width:90,halign:'center'},
                                                    {field:'ShipmentName',title:'Shipment Name',width:90,halign:'center'}
                                                ]],
                                                fitColumns: true
                                            ">
                                        </select>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
                <!-- End of form header -->
                
                <!--Item Grid -->
                <div class="easyui-tabs" data-options="tabHeight:18" style="margin:5px 0px;">		
                    <div id="tab_item" title="Item">
                        <table id="dtg_item" class="easyui-datagrid" rownumbers="true" fitColumns="true" singleSelect="true" style="height:250px;"
                            url="<?=base_url() . $menu_link .'/ajax_item'?>"
                            data-options="
                               onDblClickRow:function(index,row){
                                    var number_string           = $('#dtg_item').datagrid('getSelected').PRecNumber;
                                    var length_number_string    = number_string.length;
                                    var arr_number              = number_string.split(',');
                                    var total_number            = arr_number.length;
                                    var object1                 = document.getElementById('datagrid-row-r7-2-'+index);
                                    var object2                 = document.getElementById('datagrid-row-r8-2-'+index);
                                    var object3                 = document.getElementById('datagrid-row-r9-2-'+index);

                                    if(object1 != null){
                                        //alert('object 1 '+object1);
                                        var pos_char              = get_character_position('datagrid-row-r7-2-'+index);
                                    }else if(object2 != null){
                                        //alert('object 2 '+object2);
                                        var pos_char              = get_character_position('datagrid-row-r8-2-'+index);
                                    }else{
                                        //alert('object 3 '+object3);
                                        var pos_char              = get_character_position('datagrid-row-r9-2-'+index);
                                    }

                                    var no_trans                = '';

                                    for(var x=0;x<total_number;x++){
                                        var length_per_number       = arr_number[x].length;
                                        var start_pos               = x * length_per_number;
                                        var end_pos                 = start_pos + length_per_number;
                                        if((pos_char >=start_pos) && (pos_char<= end_pos)){
                                            no_trans                = arr_number[x];
                                        }
                                    }

                                    if(pos_char < length_number_string){
                                        parent.tab_edit('Edit Receive Item','list/receive_item', 'icon-edit', 'edit',no_trans);
                                    }
                                }
                            "
                        > 
                            <thead>
                                <tr>
                                    <th field="PRecNumber"       width="60"  data-options="halign:'center',align:'left'"   editor="text">RI No.</th>
                                    <th field="ItemCode"         width="50"  data-options="halign:'center',align:'left'"   editor="text">Item</th>
                                    <th field="ItemCodeVendor"   width="50"  data-options="halign:'center',align:'left'"   ><?php echo label_grid('no_seri');?></th>
                                    <th field="ItemDesc"         width="150" data-options="halign:'center',align:'left'"   editor="text">Description</th>
                                    <th field="ItemQty"          width="30"  data-options="halign:'center',align:'center'" editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',required:true}}" formatter="formatRp">Qty</th>
                                    <th field="ItemUnit"         width="30"  data-options="halign:'center',align:'center'" editor="text">Unit</th>								
                                    <th field="ItemPrice"        width="40"  data-options="halign:'center',align:'right'"  editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',groupSeparator:'.',required:true,disabled:false}}" formatter="formatRp" >Unit Price</th>
                                    <th field="ItemDiscPcnt"  	 width="40"  data-options="halign:'center',align:'right'"  editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',groupSeparator:'.',required:true,disabled:false}}" formatter="formatRp" >Item Disc%</th>
                                    <th field="ItemAmount"       width="40"  data-options="halign:'center',align:'right'"  editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',groupSeparator:'.',required:true,disabled:false}}" formatter="formatRp" >Item Amount</th>									
                                    
                                    <th field="ItemTaxPPn10"     width="30"  data-options="halign:'center',align:'right'"  
                                    editor="{type:'numberbox',options:{decimalSeparator:',',
                                    formatter:function(v){
                                        return v;
                                    },
                                    parser:function(s){
                                        var v = parseFloat(s);
                                        return v;
                                    }
                                    }}"
                                    hidden>Item Tax PPn</th>                                    
                                    <th field="ItemTaxPPh22"     width="30"  data-options="halign:'center',align:'right'"  
                                    editor="{type:'numberbox',options:{decimalSeparator:',',
                                    formatter:function(v){
                                        return v;
                                    },
                                    parser:function(s){
                                        var v = parseFloat(s);
                                        return v;
                                    }
                                    }}"
                                    hidden>Item Tax PPh22</th>
                                    <th field="ItemTaxPPh23"     width="30"  data-options="halign:'center',align:'right'"  
                                    editor="{type:'numberbox',options:{decimalSeparator:',',
                                    formatter:function(v){
                                        return v;
                                    },
                                    parser:function(s){
                                        var v = parseFloat(s);
                                        return v;
                                    }
                                    }}"
                                    hidden>Item Tax PPh23</th>                                    
                                    <th field="ItemTotalAmount"  width="40"  data-options="halign:'center',align:'right'"  
                                    editor="{type:'numberbox',options:{decimalSeparator:',',
                                    formatter:function(v){
                                        return v;
                                    },
                                    parser:function(s){
                                        var v = parseFloat(s);
                                        return v;
                                    }
                                    }}"
                                    hidden>Item Total Amount</th>
                                    <th field="ItemType"         width="50"  data-options="align:'center',halign:'center'" hidden>Item Type</th>
                                    <th field="ItemPPh22Status"  width="50"  data-options="align:'right',halign:'center'"  hidden>Item PPh22 Status</th>
                                    <th field="ItemPPh23Status"  width="50"  data-options="align:'left',halign:'center'"   hidden>Item PPh23 Status</th>
                                    <th field="ItemAdvNetDueIn"  width="50"  data-options="align:'left',halign:'center'"   hidden>Net Due In</th>
                                    <th field="ItemPriceNTax"    width="50"  data-options="align:'right',halign:'center'"  hidden>Item Price NTax</th>
                                    <th field="ItemCategoryID"   width="50"  data-options="halign:'center',align:'center'" editor="text" hidden>Category ID</th>								
                                    <th field="ItemCategoryCode" width="50"  data-options="halign:'center',align:'center'" editor="text" hidden>Category Code</th>								
                                    <th field="ItemStatus"       width="50"  data-options="halign:'center',align:'center'" editor="text" hidden>Item Status</th>
                                  	
                                    <th field="AmountDisc"  width="40"  data-options="halign:'center',align:'right'"  
                                    editor="{type:'numberbox',options:{decimalSeparator:',',
                                    formatter:function(v){
                                        return v;
                                    },
                                    parser:function(s){
                                        var v = parseFloat(s);
                                        return v;
                                    }
                                    }}"
                                    hidden>Amount Disc</th>
                                    
                                    <th field="ItemAmount_Hide"  width="40"  data-options="halign:'center',align:'right'"  
                                    editor="{type:'numberbox',options:{decimalSeparator:',',
                                    formatter:function(v){
                                        return v;
                                    },
                                    parser:function(s){
                                        var v = parseFloat(s);
                                        return v;
                                    }
                                    }}"
                                    hidden>Item Amount Hide</th>
                                </tr>
                            </thead>
                        </table>
                    </div>
                </div>
                <!-- End Item Grid -->
                
                <table width="100%" cellpadding="0" cellspacing="0">
                    <tr>
                	<td width="50%" valign="top">
                            <table width="100%" cellpadding="0" cellspacing="1">
                                <tr>
                                    <td width="120" valign="top" style="padding-top:4px;"><b>Description</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:335px;resize:none;"></textarea>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    
                        <td width="50%" valign="top">
                            <table width="100%" cellpadding="0" cellspacing="1">
                                <tr>
                                    <td rowspan="8">&nbsp;</td>
                                    <td width="120"><b>Sub Total</b></td>
                                    <td width="10"><b>:</b></td>

                                    <td width="170" align="right">
                                        <input type="text" id="txt_sub_total" name="txt_sub_total" class="easyui-numberbox" value="0" style="text-align:right;width:173px;" readonly
                                            data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'"   
                                        />
                                        
                                        <input type="hidden" id="txt_sub_total_hide" name="txt_sub_total_hide" value="0" class="easyui-numberbox" style="text-align:right;" readonly
                                            data-options="min:0,decimalSeparator:',',groupSeparator:'.',
                                            formatter:function(v){
                                                return v;
                                            },
                                            parser:function(s){
                                                var v = parseFloat(s);
                                                return v;
                                            }
                                            " 
                                        />
                                    </td>
                                </tr>
                        
                                <tr>
                                    <td><b>Discount</b></td>
                                    <td><b>:</b></td>

                                    <td align="right">
                                        <input type="text" name="txt_discount" id="txt_discount" class="easyui-numberbox" value="0" style="text-align:right;width:173px;" 
                                            data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'" readonly
                                        />
                                        
                                        <input type="hidden" id="txt_discount_hide" name="txt_discount_hide" value="0" class="easyui-numberbox" style="text-align:right;" readonly
                                            data-options="min:0,decimalSeparator:',',groupSeparator:'.',
                                            formatter:function(v){
                                                return v;
                                            },
                                            parser:function(s){
                                                var v = parseFloat(s);
                                                return v;
                                            }
                                            " 
                                        />
                                    </td>
                                </tr>
                        
                                <tr>
                                    <td>
                                        <span id="lbl_ppn10" style="font-weight:bold">PPn (<?= $taxes_ppn10->TaxesRate ?>%)</span>
                                    </td>
                                    
                                    <td width="10"><b>:</b></td>

                                    <td align="right">
                                        <input type="numberbox" name="txt_ppn10" id="txt_ppn10" class="easyui-numberbox" value="0" min="0" style="text-align:right;" readonly
                                            data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'"
                                        />
                                        
                                        <input type="hidden" id="txt_ppn10_hide" name="txt_ppn10_hide" value="0" class="easyui-numberbox" style="text-align:right;" readonly
                                            data-options="min:0,decimalSeparator:',',groupSeparator:'.',
                                            formatter:function(v){
                                                return v;
                                            },
                                            parser:function(s){
                                                var v = parseFloat(s);
                                                return v;
                                            }
                                            " 
                                        />
                                    </td>
                                </tr>                    

                                <tr>
                                    <td>
                                        <span id="lbl_pph22" style="font-weight:bold">PPh22 (<?= $taxes_pph22->TaxesRate ?>%)</span>
                                    </td>

                                    <td><b>:</b></td>

                                    <td align="right">
                                        <input type="numberbox" name="txt_pph22" id="txt_pph22" class="easyui-numberbox" value="0" style="text-align:right;" readonly
                                            data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'" 
                                        />
                                        
                                        <input type="hidden" id="txt_pph22_hide" name="txt_pph22_hide" value="0" class="easyui-numberbox" style="text-align:right;" readonly
                                            data-options="min:0,decimalSeparator:',',groupSeparator:'.',
                                            formatter:function(v){
                                                return v;
                                            },
                                            parser:function(s){
                                                var v = parseFloat(s);
                                                return v;
                                            }
                                            " 
                                        />
                                    </td>
                                </tr>
                                
                                <tr>
                                    <td>
                                        <span id="lbl_pph23" style="font-weight:bold">PPh23 (<?= $taxes_pph23->TaxesRate ?>%)</span>
                                    </td>

                                    <td><b>:</b></td>

                                    <td align="right">
                                        <input type="numberbox" name="txt_pph23" id="txt_pph23" class="easyui-numberbox" value="0" style="text-align:right;" readonly
                                            data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'" 
                                        />
                                        
                                        <input type="hidden" id="txt_pph23_hide" name="txt_pph23_hide" value="0" class="easyui-numberbox" style="text-align:right;" readonly
                                            data-options="min:0,decimalSeparator:',',groupSeparator:'.',
                                            formatter:function(v){
                                                return v;
                                            },
                                            parser:function(s){
                                                var v = parseFloat(s);
                                                return v;
                                            }
                                            " 
                                        />
                                    </td>
                                </tr>

                                <tr>
                                    <td><b>Est. Freight</b></td>
                                    <td><b>:</b></td>

                                    <td align="right">
                                        <input type="numberbox" name="txt_freight" id="txt_freight" class="easyui-numberbox" value="0" style="text-align:right;" 
                                            data-options="precision:2,decimalSeparator:',',groupSeparator:'.'" 
                                        />
                                    </td>
                                </tr>

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

                                <tr>
                                    <td><b>Total Purchase</b></td>
                                    <td><b>:</b></td>
                                    
                                    <td align="right">
                                        <input type="numberbox" name="txt_total_purchase" id="txt_total_purchase" class="easyui-numberbox" value="0" style="text-align:right;" readonly  
                                            data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'"
                                        />
                                        
                                        <input type="hidden" id="txt_total_purchase_hide" name="txt_total_purchase_hide" value="0" class="easyui-numberbox" style="text-align:right;" readonly
                                            data-options="min:0,decimalSeparator:',',groupSeparator:'.',
                                            formatter:function(v){
                                                return v;
                                            },
                                            parser:function(s){
                                                var v = parseFloat(s);
                                                return v;
                                            }
                                            " 
                                        />
                                    </td>
                                </tr>

                                <tr>
                                    <td rowspan="8">&nbsp;</td>
                                    <td  width="120"><b>Total DP</b></td>
                                    <td><b>:</b></td>
                                    
                                    <td align="right">
                                        <input type="numberbox" name="txt_total_dp" id="txt_total_dp" class="easyui-numberbox" value="0" style="text-align:right;" readonly  
                                            data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'"
                                        />
                                    </td>
                                </tr>

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

                                <tr>
                                    <td width="120"><b>Outstanding</b></td>
                                    <td><b>:</b></td>
                                    
                                    <td align="right">
                                        <input type="numberbox" name="txt_total_invoice" id="txt_total_invoice" class="easyui-numberbox" value="0" style="text-align:right;" readonly  
                                            data-options="precision:2,decimalSeparator:',',groupSeparator:'.'"
                                        />
                                        
                                        <input type="hidden" id="txt_total_invoice_hide" name="txt_total_invoice_hide" value="0" class="easyui-numberbox" style="text-align:right;" readonly
                                            data-options="min:0,decimalSeparator:',',groupSeparator:'.',
                                            formatter:function(v){
                                                return v;
                                            },
                                            parser:function(s){
                                                var v = parseFloat(s);
                                                return v;
                                            }
                                            " 
                                        />
                                    </td>
                                </tr>                                
                            </table>
                        </td>
                    </tr>
                </table>
            </form>
        </div>
    </div>

    <div id="form-footer" data-options="region:'south',border:false">
    	<table width="100%" cellpadding="0" cellspacing="0">
            <tr>
                <td align="right">
                	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-document" onclick="show_legend();" plain="true">Legend</a>
                    <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" 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="parent.tab_close('tt');">Close</a>				
                </td>
            </tr>
        </table>
    </div>
</div>

<div id="dialog-ri" class="easyui-dialog" closed="true" data-options="modal:true, iconCls:'icon-save'" buttons="#dialogpi-buttons" style="width:860px;height:532px;top:27px;background-color:#F8F8F8;padding:5px;"> 
        <div id="tab_purchase_req" class="easyui-tabs" style="width:835px;height:485px; background-color:#F8F8F8;" data-options="fit:true,plain:true">
            <div title="Item / Expense / Services" style="padding:10px" >
             <table id="tt_choose_ri" class="easyui-datagrid" 
                url="<?php echo base_url().$menu_link."/ajax_for_choose_ri" ?>"      
                remoteSort="false"
                pageSize="150"
                fitColumns="true" 
                rowNumbers="true"
                singleSelect="true"
                style="width:812px;height:400px" 
                data-options = "border:true,view:scrollview,autoRowheight:false"
            >                      
                <thead>
                    <tr>
                        <th field="ck" checkbox="true"></th>
                        <th field="POrdNumber" width="170" halign='center' align='center' sortable="true"><span class="poheader_code">PO. No.</span></th>
                        <th field="POrdDate" width="90" halign='center' align='center' sortable="true" formatter="formatDate">PO. Date</th>
                        <th field="PRecNumber" width="150" halign='center' align='center' sortable="true">RI. No.</th>
                        <th field="POrdDesc" width="315" halign="center" align="left"><span class="poheader_desc">Description</span></th>
                    </tr>
                </thead>
            </table>
            <!-- Dialog Button for Dialog Item-->
            <div style="border:none; margin-top:10px; float:right;">
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_ri();">&nbsp; OK &nbsp; &nbsp;</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-ri');">Cancel</a>
            </div>
            <!-- End of Dialog Button for Dialog Item-->
            </div>
            
            <div title="Adv. Payment" style="padding:10px">
            
            <table id="tt_choose_adv" class="easyui-datagrid" 
                url="<?php echo base_url().$menu_link."/ajax_for_choose_adv" ?>"   
                remoteSort="false"
                pageSize="150"
                fitColumns="true" 
                rowNumbers="true"
                singleSelect="true"
                style="width:812px;height:400px" 
                data-options = "border:true,view:scrollview,autoRowheight:false"
            >                   
                <thead>
                    <tr>
                        <th field="ck" checkbox="true"></th>
                        <th field="POrdNumber" width="170" halign='center' align='center' sortable="true"><span class="poheader_code">PO. No.</span></th>
                        <th field="POrdDate" width="90" halign='center' align='center' sortable="true" formatter="formatDate">PO. Date</th>
                        <th field="PRecNumber" width="150" halign='center' align='center' sortable="true">RI. No.</th>
                        <th field="POrdDesc" width="315" halign="center" align="left"><span class="poheader_desc">Description</span></th>
                    </tr>
                </thead>
            </table>
             <!-- Dialog Button for Dialog servis-->
            <div style="border:none; margin-top:10px; float:right;">
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_adv();">&nbsp; OK &nbsp; &nbsp;</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-ri');">Cancel</a>
            </div>
            <!-- End of Dialog Button for Dialog Item-->
            </div>
         </div>
</div>
<?php $this->load->view('v_custom_pop_up');?>
<script type="text/javascript">
    function select_ri(){
		var Vendor = $('#cbg_vendor').combogrid('grid');	// get datagrid object
		var SelectedVendor = Vendor.datagrid('getSelected');    // get the selected row
		
		if(SelectedVendor == null) {
				$.messager.alert('Warning', "Silahkan pilih 'Vendor' terlebih dahulu", 'warning');
		}
		else{
				modal_open('Select Receive Item', 'dialog-ri');
		}
    }
	
    function choose_ri(){
    	var flag            = true;
    	var ri          	= $('#tt_choose_ri').datagrid('getChecked');
    	var last_po_number	= '';
		
    	$.each(ri, function(index, value) {
                if(value.PRecNumber != undefined){
                    if(last_po_number == '' || last_po_number == value.POrdNumber){
                        last_po_number  = value.POrdNumber;
                        
                        pord_number     = value.POrdNumber;
                        prec_number     = value.PRecNumber;
                        prec_date       = value.PRecDate;
    					prec_desc       = value.PRecDesc;
                        
                        vend_curr_code  = value.CurrencyCode;
                        vend_curr_rate  = value.CurrencyRate;
                        
                        termspayment_id = value.TermsPaymentID;
                        fob_id          = value.FOBID;
                        shipment_id     = value.ShipmentID;
                        shipment_date   = value.ShipmentDate;

                        taxes_status    = value.POrdTaxesStatus;
                        intax_status    = value.POrdInTaxStatus;
                        dp_status       = value.POrdDPStatus;

                        //sub_total       = value.POrdSubTotal;
                        discount        = value.POrdDiscount;
                        ppn10_pcnt      = value.POrdPPn10Pcnt == 0 ? '0' : value.POrdPPn10Pcnt;
                        pph22_pcnt      = value.POrdPPh22Pcnt == 0 ? '0' : value.POrdPPh22Pcnt;
                        pph23_pcnt      = value.POrdPPh23Pcnt == 0 ? '0' : value.POrdPPh23Pcnt;
                        //ppn10_val       = value.POrdPPn10;
                        //pph22_val       = value.POrdPPh22;
                        //pph23_val       = value.POrdPPh23;
                        est_frieght     = value.POrdEstFreight;
                        //total_amount    = value.POrdTotalAmount;
                        total_dp        = value.POrdTotalDP;
                    }
                    else{
                        flag = false;
                        $.messager.alert('Maaf', 'Tidak dapat memilih Receive Item dari Purchase Order yang berbeda.');
                    }
                }
    	});
	
    	if(flag == true){
                $('#dtg_item').datagrid('load',{
                    POrdNumber : pord_number
                });	

                $('#hdn_currency_code').val(vend_curr_code);
                $('#txt_currency_rate').numberbox('setValue', vend_curr_rate);
                $('#txt_fiscal_rate').numberbox('setValue', vend_curr_rate);
                $('#currency').text(vend_curr_code + ' / IDR');
                $('#fiscal').text(vend_curr_code + ' / IDR');                

                $('#hdn_pord_no').val(pord_number);        
                $('#hdn_ppn10_pcnt').val(ppn10_pcnt);
                $('#hdn_pph22_pcnt').val(pph22_pcnt);
                $('#hdn_pph23_pcnt').val(pph23_pcnt);
                $('#hdn_taxes_status').val(taxes_status);
                $('#hdn_intax_status').val(intax_status);

                $('#lbl_ppn10').text('PPn (' + ppn10_pcnt + '%)');
                $('#lbl_pph22').text('PPh22 (' + pph22_pcnt + '%)');
                $('#lbl_pph23').text('PPh23 (' + pph23_pcnt + '%)');

                $('#txt_prec_no').val(prec_number);
                $('#txt_prec_date').val(prec_date);
    			$('#txa_description').val(prec_desc);
    			
                $('#cbo_ship_via').combogrid('setValue', shipment_id);
                $('#cbo_terms').combogrid('setValue', termspayment_id);
                $('#cbo_fob').combogrid('setValue', fob_id);
                $('#txt_shipment_date').datebox('setValue', shipment_date);
    			
    			/*$('#txt_sub_total').numberbox('setValue', sub_total);
                $('#txt_discount').numberbox('setValue', discount);
                $('#txt_ppn10').numberbox('setValue', ppn10_val);
                $('#txt_pph22').numberbox('setValue', pph22_val);
                $('#txt_pph23').numberbox('setValue', pph23_val);            
                $('#txt_freight').numberbox('setValue', est_frieght);
                $('#txt_total_purchase').numberbox('setValue', total_amount);*/
    			$('#dtg_item').datagrid({
    				onLoadSuccess: function(){
    					dg          = $('#dtg_item').datagrid('getRows');
    					sub_total   		= 0;
    					total_sales   		= 0;
    					total_disc 			= 0;
    					total_ppn 			= 0;
    					total_pph22 		= 0;
    					total_pph23 		= 0;
    					total_pph23 		= 0;
    					total_amount 		= 0;
    					all_discount 		= 0;
    					
    					$.each(dg, function(index, value){
    						
    						if(value.ItemAmount_Hide == null){item_amount = 0;}
    						else{item_amount = value.ItemAmount_Hide;}
    						//disc   	= isNaN(value.AmountDisc) ? 0 : parseFloat(value.AmountDisc);
    						ppn   	= isNaN(value.ItemTaxPPn10) ? 0 : parseFloat(value.ItemTaxPPn10);
    						pph22   = isNaN(value.ItemTaxPPh22) ? 0 : parseFloat(value.ItemTaxPPh22);
    						pph23   = isNaN(value.ItemTaxPPh23) ? 0 : parseFloat(value.ItemTaxPPh23);
    						total_amnt   = isNaN(value.ItemTotalAmount) ? 0 : parseFloat(value.ItemTotalAmount);
    						
    						total_disc 	= parseFloat((value.ItemQty * value.ItemPrice) * (value.ItemDiscPcnt / 100));
    							
    						$('#dtg_item').datagrid('updateRow',{
    							index:index,
    							row:{AmountDisc 		: total_disc}
    						});
    							
    						sub_total 		+= parseFloat(item_amount);
    						all_discount 	+= parseFloat(total_disc);
    						total_ppn 		+= parseFloat(ppn);
    						total_pph22 	+= parseFloat(pph22);
    						total_pph23 	+= parseFloat(pph23);
    						total_amount 	+= parseFloat(total_amnt);
    					});
    					$('#txt_sub_total').numberbox('setValue', sub_total);
    					$('#txt_sub_total_hide').numberbox('setValue', sub_total);
    					
    					$('#txt_discount').numberbox('setValue', all_discount);
    					$('#txt_discount_hide').numberbox('setValue', all_discount);
    					
    					$('#txt_ppn10').numberbox('setValue', total_ppn);
    					$('#txt_ppn10_hide').numberbox('setValue', total_ppn);
    					
    					$('#txt_pph22').numberbox('setValue', total_pph22);
    					$('#txt_pph22_hide').numberbox('setValue', total_pph22);
    					
    					$('#txt_pph23').numberbox('setValue', total_pph23);
    					$('#txt_pph23_hide').numberbox('setValue', total_pph23);
    					
    					$('#txt_freight').numberbox('setValue', est_frieght);
    					
    					$('#txt_total_purchase').numberbox('setValue', (sub_total + total_ppn + total_pph22 - total_pph23 + parseFloat(est_frieght)));
    					$('#txt_total_purchase_hide').numberbox('setValue', (sub_total + total_ppn + total_pph22 - total_pph23 + parseFloat(est_frieght)));
    					
    					$('#txt_total_dp').numberbox('setValue', total_dp);
    					
    					var total_purchase = (sub_total + total_ppn + total_pph22 - total_pph23 + parseFloat(est_frieght));
    					if(total_dp > total_purchase){
    						$('#txt_total_invoice').numberbox('setValue', (sub_total + total_ppn + total_pph22 - total_pph23) - total_dp + parseFloat(est_frieght));
    						$('#txt_total_invoice_hide').numberbox('setValue', 0);
    					}
    					else{
    						$('#txt_total_invoice').numberbox('setValue', (sub_total + total_ppn + total_pph22 - total_pph23) - total_dp + parseFloat(est_frieght));
    						$('#txt_total_invoice_hide').numberbox('setValue', (sub_total + total_ppn + total_pph22 - total_pph23) - total_dp + parseFloat(est_frieght));
    					}
    				}
    			});
    			
                if(taxes_status == 1){
                    $('.disableme').css('display','');
                    $('#chk_taxtable').prop('checked',true);
                    
                    if(intax_status == 1){
                        $('.disableme1').css('display','');
                        $('#chk_inclusive').prop('checked',true);
                        $('#chk_inclusive').attr('onclick','return false');
                        $('#chk_taxtable').attr('onclick','return false');
                    }
                    else{
                        $('.disableme1').css('display','none');
                        $('#chk_inclusive').prop('checked',false);
                        $('#chk_inclusive').attr('onclick','return false');
                        $('.chk_taxtable').prop('disabled',false);
                    }	
                }
                else{
                    $('.disableme').css('display','None');                
                    $('.disableme1').css('display','None');                
                    $('#chk_taxtable').prop('checked',false);
                    $('#chk_inclusive').prop('checked',false);
                }
    	
                if(dp_status == 1){
                    $('.disableme2').css('display','');                
                    $('#chk_vendor_dp').prop('checked',true);
                }
                else{
                    $('.disableme2').css('display','None');                
                    $('#chk_vendor_dp').prop('checked',false);
                }
            
                modal_close('dialog-ri');
    	}
    }
	
	function choose_adv(){
		var flag                = true;
		var ri          	= $('#tt_choose_adv').datagrid('getChecked');
		var last_po_number	= '';
			
		$.each(ri, function(index, value) {
				if(value.PRecNumber != undefined){
					if(last_po_number == '' || last_po_number == value.POrdNumber){
						last_po_number  = value.POrdNumber;
						
						pord_number     = value.POrdNumber;
						prec_number     = value.PRecNumber;
						prec_date       = value.PRecDate;
						prec_desc       = value.PRecDesc;
						
						vend_curr_code  = value.CurrencyCode;
						vend_curr_rate  = value.CurrencyRate;
						
						termspayment_id = value.TermsPaymentID;
						fob_id          = value.FOBID;
						shipment_id     = value.ShipmentID;
						shipment_date   = value.ShipmentDate;
	
						taxes_status    = value.POrdTaxesStatus;
						intax_status    = value.POrdInTaxStatus;
						dp_status       = value.POrdDPStatus;
	
						//sub_total       = value.POrdSubTotal;
						//discount        = value.POrdDiscount;
						ppn10_pcnt      = value.POrdPPn10Pcnt == 0 ? '0' : value.POrdPPn10Pcnt;
						pph22_pcnt      = value.POrdPPh22Pcnt == 0 ? '0' : value.POrdPPh22Pcnt;
						pph23_pcnt      = value.POrdPPh23Pcnt == 0 ? '0' : value.POrdPPh23Pcnt;
						
						//ppn10_val       = value.POrdPPn10;
						//pph22_val       = value.POrdPPh22;
						//pph23_val       = value.POrdPPh23;
						est_frieght     = value.POrdEstFreight;
						//total_amount    = value.POrdTotalAmount;
						total_dp        = value.POrdTotalDP;
					}
					else{
						flag = false;
						$.messager.alert('Maaf', 'Tidak dapat memilih Receive Item dari Purchase Order yang berbeda.');
					}
				}
		});
		
		if(flag == true){
				$('#dtg_item').datagrid('load',{
					POrdNumber : pord_number
				});	
	
				$('#hdn_currency_code').val(vend_curr_code);
				$('#txt_currency_rate').numberbox('setValue', vend_curr_rate);
				$('#txt_fiscal_rate').numberbox('setValue', vend_curr_rate);
				$('#currency').text(vend_curr_code + ' / IDR');
				$('#fiscal').text(vend_curr_code + ' / IDR');                
	
				$('#hdn_pord_no').val(pord_number);        
				$('#hdn_ppn10_pcnt').val(ppn10_pcnt);
				$('#hdn_pph22_pcnt').val(pph22_pcnt);
				$('#hdn_pph23_pcnt').val(pph23_pcnt);
				$('#hdn_taxes_status').val(taxes_status);
				$('#hdn_intax_status').val(intax_status);
	
				$('#lbl_ppn10').text('PPn (' + ppn10_pcnt + '%)');
				$('#lbl_pph22').text('PPh22 (' + pph22_pcnt + '%)');
				$('#lbl_pph23').text('PPh23 (' + pph23_pcnt + '%)');
	
				$('#txt_prec_no').val(prec_number);
				$('#txt_prec_date').val(prec_date);
				$('#txa_description').val(prec_desc);
	
				$('#cbo_ship_via').combogrid('setValue', shipment_id);
				$('#cbo_terms').combogrid('setValue', termspayment_id);
				$('#cbo_fob').combogrid('setValue', fob_id);
				$('#txt_shipment_date').datebox('setValue', shipment_date);
	
				/*$('#txt_sub_total').numberbox('setValue', sub_total);
				$('#txt_discount').numberbox('setValue', discount);
				$('#txt_ppn10').numberbox('setValue', ppn10_val);
				$('#txt_pph22').numberbox('setValue', pph22_val);
				$('#txt_pph23').numberbox('setValue', pph23_val);            
				$('#txt_freight').numberbox('setValue', est_frieght);
				$('#txt_total_purchase').numberbox('setValue', total_amount);
				
				$('#txt_total_invoice').numberbox('setValue', total_amount - total_dp);*/
				$('#dtg_item').datagrid({
					onLoadSuccess: function(){
						dg          = $('#dtg_item').datagrid('getRows');
						sub_total   		= 0;
						total_sales   		= 0;
						total_disc 			= 0;
						total_ppn 			= 0;
						total_pph22 		= 0;
						total_pph23 		= 0;
						total_pph23 		= 0;
						total_amount 		= 0;
						all_discount 		= 0;
		
						$.each(dg, function(index, value){
							
							if(value.ItemAmount == null){item_amount = 0;}
							else{item_amount = value.ItemAmount;}
							//disc   	= isNaN(value.ItemDiscPcnt) ? 0 : parseFloat(value.ItemDiscPcnt);
							ppn   	= isNaN(value.ItemTaxPPn10) ? 0 : parseFloat(value.ItemTaxPPn10);
							pph22   = isNaN(value.ItemTaxPPh22) ? 0 : parseFloat(value.ItemTaxPPh22);
							pph23   = isNaN(value.ItemTaxPPh23) ? 0 : parseFloat(value.ItemTaxPPh23);
							total_amnt   = isNaN(value.ItemTotalAmount) ? 0 : parseFloat(value.ItemTotalAmount);
							
							total_disc 	= parseFloat((value.ItemQty * value.ItemPrice) * (value.ItemDiscPcnt / 100));
							
							$('#dtg_item').datagrid('updateRow',{
								index:index,
								row:{AmountDisc 		: total_disc}
							});
							
							sub_total 		+= parseFloat(item_amount);
							all_discount 	+= parseFloat(total_disc);
							total_ppn 		+= parseFloat(ppn);
							total_pph22 	+= parseFloat(pph22);
							total_pph23 	+= parseFloat(pph23);
							total_amount 	+= parseFloat(total_amnt);
				
						});
						$('#txt_sub_total').numberbox('setValue', sub_total);
						$('#txt_sub_total_hide').numberbox('setValue', sub_total);
						
						$('#txt_discount').numberbox('setValue', all_discount);
						$('#txt_discount_hide').numberbox('setValue', all_discount);
						
						$('#txt_ppn10').numberbox('setValue', total_ppn);
						$('#txt_ppn10_hide').numberbox('setValue', total_ppn);
						
						$('#txt_pph22').numberbox('setValue', total_pph22);
						$('#txt_pph22_hide').numberbox('setValue', total_pph22);
						
						$('#txt_pph23').numberbox('setValue', total_pph23);
						$('#txt_pph23_hide').numberbox('setValue', total_pph23);
						
						$('#txt_freight').numberbox('setValue', est_frieght);
						
						$('#txt_total_purchase').numberbox('setValue', (sub_total + total_ppn + total_pph22 - total_pph23 + parseFloat(est_frieght)));
						$('#txt_total_purchase_hide').numberbox('setValue', (sub_total + total_ppn + total_pph22 - total_pph23 + parseFloat(est_frieght)));
						
						$('#txt_total_dp').numberbox('setValue', total_dp);
						
						var total_purchase = (sub_total + total_ppn + total_pph22 - total_pph23 + parseFloat(est_frieght));
						if(total_dp > total_purchase){
							$('#txt_total_invoice').numberbox('setValue', (sub_total + total_ppn + total_pph22 - total_pph23) - total_dp + parseFloat(est_frieght));
							$('#txt_total_invoice_hide').numberbox('setValue', 0);
						}
						else{
							$('#txt_total_invoice').numberbox('setValue', (sub_total + total_ppn + total_pph22 - total_pph23) - total_dp + parseFloat(est_frieght));
							$('#txt_total_invoice_hide').numberbox('setValue', (sub_total + total_ppn + total_pph22 - total_pph23) - total_dp + parseFloat(est_frieght));
						}
					}
				});
				
	
				if(taxes_status == 1){
					$('.disableme').css('display','');
					$('#chk_taxtable').prop('checked',true);
					
					if(intax_status == 1){
						$('.disableme1').css('display','');
						$('#chk_inclusive').prop('checked',true);
						$('#chk_inclusive').attr('onclick','return false');
						$('#chk_taxtable').attr('onclick','return false');
					}
					else{
						$('.disableme1').css('display','none');
						$('#chk_inclusive').prop('checked',false);
						$('#chk_inclusive').attr('onclick','return false');
						$('.chk_taxtable').prop('disabled',false);
					}	
				}
				else{
					$('.disableme').css('display','None');                
					$('.disableme1').css('display','None');                
					$('#chk_taxtable').prop('checked',false);
					$('#chk_inclusive').prop('checked',false);
				}
		
				if(dp_status == 1){
					$('.disableme2').css('display','');                
					$('#chk_vendor_dp').prop('checked',true);
				}
				else{
					$('.disableme2').css('display','None');                
					$('#chk_vendor_dp').prop('checked',false);
				}
			
				modal_close('dialog-ri');
		}
    }
	
    $(document).ready(function(){

        /*============== Daswan ====================*/
        var POrdNumber = '<?=$POrdNumber;?>';
        var VendorCode = '<?=$VendorCode;?>';

        if(POrdNumber !=''){
            POrdNumber      = POrdNumber.replace(/\__/g,"/");
            POrdNumber      = POrdNumber.replace(/\_/g,"-");
            VendorCode      = VendorCode.replace(/\__/g,"/");
            VendorCode      = VendorCode.replace(/\_/g,"-");
        }
        /*============== end Daswan ===================*/

    	$('#cbg_vendor').combogrid({
                onSelect:function(rowIndex, rowData){
                    vend_curr_code = rowData.CurrencyCode;
                    vend_curr_rate = rowData.CurrencyRate;
                    
                    $('#dtg_item').datagrid('load',{
                        Number: ''
                    });
                    
                    $('#tt_choose_ri').datagrid('load',{
                        item_type   : 'item/expense',
                        vendor_code : rowData.VendorCode
    					//branch_code : rowData.BranchCode
                    });
    				
    				$('#tt_choose_adv').datagrid('load',{
                        item_type   : 'advance',
                        vendor_code : rowData.VendorCode
    					//branch_code : rowData.BranchCode
                    });

                    $('.disableme').css('display','None');
                    $('.disableme1').css('display','None');
                    $('.disableme2').css('display','None');

                    $('#txa_address').val(rowData.VendorAddress); 
                    $('#hdn_currency_code').val(vend_curr_code);
    				$('#hdn_branch_code').val(rowData.BranchCode);
                    $('#txt_currency_rate').numberbox('setValue', vend_curr_rate);
                    $('#txt_fiscal_rate').numberbox('setValue', vend_curr_rate);
                    $('#currency').text(vend_curr_code + ' / IDR');
                    $('#fiscal').text(vend_curr_code + ' / IDR');                
                    
                    if(rowData.CurrencyCode != 'IDR'){
                        $('#row_currency_rate').show();
                        $('#row_fiscal_rate').show();

                        $('#txt_currency_rate').attr('readonly',true);
                        $('#txt_fiscal_rate').attr('readonly',false);
                    }
                    else{
                        $('#row_currency_rate').hide();
                        $('#row_fiscal_rate').hide();

                        $('#txt_currency_rate').attr('readonly',true);
                        $('#txt_fiscal_rate').attr('readonly',true);
                    }
                    
                    $('#txt_sub_total').numberbox('setValue', 0);
                    $('#txt_discount').numberbox('setValue', 0);
                    $('#txt_ppn10').numberbox('setValue', 0);
                    $('#txt_pph22').numberbox('setValue', 0);
                    $('#txt_pph23').numberbox('setValue', 0);
                    $('#txt_freight').numberbox('setValue', 0);
                    $('#txt_total_purchase').numberbox('setValue', 0);                
                    $('#txt_total_dp').numberbox('setValue', 0);
                    $('#txt_total_invoice').numberbox('setValue', 0);
                },onLoadSuccess:function(data){
                    /*============ Daswan ==============*/
                    if(POrdNumber!=''){
                        $('#cbg_vendor').combogrid('setValue',VendorCode);
                    }
                    /*=========== end Daswan ====================*/
                }
    	});

        $('#tt_choose_ri').datagrid({
            onLoadSuccess:function(data){
                /*==================== Daswan ============================*/
                if(POrdNumber!=''){
                    var data_ri     = $('#tt_choose_ri').datagrid('getRows');
                    var ind_prec    = findWithAttr(data_ri,'POrdNumber',POrdNumber);
                    
                    if( typeof ind_prec == 'undefined'){
                        var data_adv    = $('#tt_choose_adv').datagrid('getRows');
                        ind_prec        = findWithAttr(data_adv,'POrdNumber',POrdNumber);
                        $('#tt_choose_adv').datagrid('checkRow',ind_prec);
                        choose_adv();
                    }else{
                         $('#tt_choose_ri').datagrid('checkRow',ind_prec);
                        choose_ri();
                    }
                }
               /*==================== end Daswan ==========================*/
            }
                
       });
    	
    	$('#tab_purchase_req').tabs({
    		border:true,
    		onSelect:function(title){
    			if(title == 'Item / Expense / Services'){
    				 var vendor_code = $('#cbg_vendor').combogrid('grid').datagrid('getSelected').VendorCode;
    				 var branch_code = $('#cbg_vendor').combogrid('grid').datagrid('getSelected').BranchCode;
    				
    				$('#tt_choose_ri').datagrid('load',{
                        item_type   : 'item/expense',
                        vendor_code : vendor_code
    					//branch_code : branch_code
                    });
    			}
    			else{
    				var vendor_code = $('#cbg_vendor').combogrid('grid').datagrid('getSelected').VendorCode;
    				var branch_code = $('#cbg_vendor').combogrid('grid').datagrid('getSelected').BranchCode;
    				 $('#tt_choose_adv').datagrid('load',{
                        item_type   : 'advance',
                        vendor_code : vendor_code
    					//branch_code : branch_code
                    });
    			}
    		}
    	});

	
        $('#txt_discount').numberbox({
            onChange:function(){
                calculate_total();
            }
        });
        
        $('#txt_freight').numberbox({
            onChange:function(){
				 freight = $('#txt_freight').numberbox('getValue');

                if(isNaN(freight) || freight == ''){
                    $('#txt_freight').numberbox('setValue', 0);
                }
                calculate_total();
            }
        });
        datagrid_item_type("dtg_item");//untuk warna grid
    }); 

    function calculate_total(){
        subtotal    = parseFloat($('#txt_sub_total_hide').numberbox('getValue'));
        discount    = parseFloat($('#txt_discount_hide').numberbox('getValue'));
        ppn10       = parseFloat($('#txt_ppn10_hide').numberbox('getValue'));
        pph22       = parseFloat($('#txt_pph22_hide').numberbox('getValue'));
        pph23       = parseFloat($('#txt_pph23_hide').numberbox('getValue'));
        freight     = parseFloat($('#txt_freight').numberbox('getValue'));
        totaldp     = parseFloat($('#txt_total_dp').numberbox('getValue'));

        total_purchase  = subtotal + ppn10 + pph22 - pph23 + freight;
        total_invoice   = total_purchase - totaldp;

        $('#txt_total_purchase').numberbox('setValue', total_purchase);
		$('#txt_total_purchase_hide').numberbox('setValue', total_purchase);
		
        $('#txt_total_invoice').numberbox('setValue', total_invoice); 
		$('#txt_total_invoice_hide').numberbox('setValue', total_invoice);    
    }
    
    function set_ppn_value(){
	if($('#chk_taxtable').is(':checked')){            
            $('#hdn_taxes_status').val(1);

            if($('#chk_inclusive').is(':checked')){
                $('#hdn_intax_status').val(1);
            }
            else{
                $('#hdn_intax_status').val(0);
            }
	}
	else{
            $('#hdn_taxes_status').val(0);
            $('#hdn_intax_status').val(0);
	}
	
	calculate_ppn();
    }
    
    function calculate_ppn(){
        var item            = $('#dtg_item').datagrid('getRows');
        var ppn10_pcnt      = $('#hdn_ppn10_pcnt').val();

        total_amount        = 0;
        item_total_amount   = 0;

        $.each(item, function(index, value){
            item_ppn    = 0;
            item_amount = value.ItemAmount;
            item_disc   = value.ItemDiscount;

            if(itemamount > 0 || itemamount !== null){
        	if($('#chk_taxtable').is(':checked')){
                    item_ppn = parseFloat(item_amount) * (ppn10_pcnt / 100);
                }
                else{
                    item_ppn = 0;
                }
            }

            item_total_amount   = parseFloat(item_amount) + parseFloat(item_ppn);
                        
            $('#dtg_item').datagrid('updateRow',{
                index:index,
                row: {ItemTaxPPn10      : item_ppn,
                      ItemTotalAmount   : item_total_amount}
            });
        });

	$('#dtg_item').edatagrid('saveRow');
    }

    function insert_data(){
		var i       = $('#dtg_item').datagrid('getRows');
		var detail  = JSON.stringify(i);
		
		sub_total 	= $('#txt_sub_total').numberbox('getValue');
		freight 	= $('#txt_freight').numberbox('getValue');
		txt_pph23 	= $('#txt_pph23').numberbox('getValue');
		
		if(freight < 0){
			if(Math.abs(freight) > sub_total){
				if(txt_pph23 > 0){
					if(Math.abs(freight) > parseFloat(sub_total - txt_pph23)){
						$.messager.alert('Maaf', 'PPh 23 Aktif, Total Freight tidak boleh melebihi (Sub total - PPh23), silahkan lihat kembali transaksi anda');
					}
				}
				else{
					$.messager.alert('Maaf', 'Total Freight tidak boleh melebihi sub total, silahkan lihat kembali transaksi anda');
				}
			}
			else{
				if(txt_pph23 > 0){
					if(Math.abs(freight) > parseFloat(sub_total - txt_pph23)){
						$.messager.alert('Maaf', 'PPh 23 Aktif, Total Freight tidak boleh melebihi (Sub total - PPh23), silahkan lihat kembali transaksi anda');
					}
					else{
						$("#detail").val(detail);
						save_transaction('<?php echo $menu_link?>','#fm_purchase_invoice');
					}
				}
				else{
					$("#detail").val(detail);
					save_transaction('<?php echo $menu_link?>','#fm_purchase_invoice');
				}
			}
		}
		else{
			$("#detail").val(detail);
			save_transaction('<?php echo $menu_link?>','#fm_purchase_invoice');
		}
    }    
</script>