<!---- 
Author  : Herpan / 12-08-2014	
Revisi 	: Ferry / 15-01-2015
-->
	
	<?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 Receive Item', '<?=$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 -->
        
    <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_receive_item" name="fm_receive_item" method="post">
            	<input type="hidden" name="detail" id="detail"/>
                <input type="hidden" name="hdn_currency_code" id="hdn_currency_code"/>
                <input type="hidden" name="hdn_currency_rate" id="hdn_currency_rate"/>
                <input type="hidden" name="hdn_ppn10_pcnt" id="hdn_ppn10_pcnt"/>
                
                <!-- Form Header -->
                <table width="100%" height="140" cellpadding="0" cellspacing="0">
                    <tr>
                        <td width="50%" 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 width="270">
                                        <select id="cbg_vendor" name="cbg_vendor" class="easyui-combogrid" style="width:260px;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>
                                    </td>
                            
                                    <td>
                                        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-sales'" onClick="select_po();">Select PO</a>
                                    </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>
                                        <textarea name="txa_address" id="txa_address" rows="4" style="width:254px;resize:none;" readonly></textarea>
                                    </td>
                                    <td>&nbsp;</td>
                                </tr>
                            </table>
                        </td>
                        
                        <td width="50%" valign="top">
                            <table width="100%" cellpadding="0" cellspacing="1" style="margin-top:4px;">
                                <tr height="20">
                                    <td rowspan="7" valign="top">&nbsp;</td>
                                    <td width="190" ></td>
                                    <td width="122" style="background:#333399;text-align:center;color:#FFF;">RI. Date</td>
                                    <td width="122" style="background:#333399;text-align:center;color:#FFF;">Terms</td>
                                </tr>
                                
                                <tr height="20">
                                    <td></td>
                                    <td><input name="txt_prec_date" id="txt_prec_date" type="text" class="easyui-datebox validBackward" value="today" style="width:190px;height:20px;align:center" required="required" 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;">PO. No.</td>
                                    <td style="background:#333399;text-align:center;color:#FFF;">PO. Date</td> -->
                                    <td></td>
                                    <td style="background:#333399;text-align:center;color:#FFF;">Vendor DO. No.</td>
                                    <td style="background:#333399;text-align:center;color:#FFF;">FOB</td>
                                </tr>
                                
                                <tr height="20">
                                    <!--<td>--><input type="hidden" name="txt_pord_no" id="txt_pord_no" style="width:190px;height:18px;text-align:center;" class="easyui-validatebox textbox" required="required" readonly /><!--</td>-->
                                    <!--<td>--><input type="hidden" name="txt_pord_date" id="txt_pord_date" class="easyui-validatebox textbox" style="width:120px;height:18px" required="required" readonly /><!--</td>-->
                                    <td></td>
                                    <td><input type="text" name="txt_vendor_do_no" id="txt_vendor_do_no" style="width:190px;height:18px;text-transform:uppercase;text-align:center;" class="easyui-validatebox textbox" /></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 DO. No.</td> -->
                                    <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_do_no" id="txt_vendor_do_no" style="width:190px;height:18px;text-transform:uppercase;text-align:center;" class="easyui-validatebox textbox" /></td> -->
                                    <td></td>
                                    <td><input name="txt_ship_date" id="txt_ship_date" type="text" class="easyui-datebox validBackward" value="today" style="width:190px;height:20px;align:center" editable="false" /></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 title="Item">
                        <table id="dtg_item" idField="id" rownumbers="true" fitColumns="true" singleSelect="true" toolbar="#tb_dtg_item" style="height:250px;" 
                            url="<?=base_url(). $menu_link .'/ajax_item/'?>"
                        >
                            <thead>
                                <tr>
                                    <th field="POrdNumber"       width="40"  data-options="halign:'center',align:'left'" editor="{type:'text'}">PO. No.</th>
                                    <th field="ItemCode"         width="40"  data-options="halign:'center',align:'left'">Item</th>
                                    <th field="ItemCodeVendor"   width="40"  data-options="halign:'center',align:'left'"><?php echo label_grid('no_seri');?></th>
                                    <th field="ItemDesc"         width="120" data-options="halign:'center',align:'left'">Description</th>
                                    <th field="ItemQty"          width="20"  data-options="halign:'center',align:'center'" editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',required:true},min:1}" formatter="formatRp">Qty</th>			
                                    <th field="ItemUnit"         width="20"  data-options="align:'center',halign:'center'">Unit</th>
                                    
                                    <th field="ItemPrice"        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>Unit Price</th>
                                    <th field="ItemAmount"       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 Amount</th>
                                    <th field="ItemMax"          width="20"  data-options="halign:'center',align:'center'" editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',required:true},min:1}" formatter="formatRp" hidden>Item Max</th>
                                    <th field="ItemTaxPPn10"     width="20"  data-options="halign:'center',align:'center'" 
                                    editor="{type:'numberbox',options:{decimalSeparator:',',
                                    formatter:function(v){
                                        return v;
                                    },
                                    parser:function(s){
                                        var v = parseFloat(s);
                                        return v;
                                    }
                                    }}" 
                                    hidden>Item Tax PPn10</th>
                                    <!--<th field="ItemTotalAmount"  width="50"  data-options="align:'right',halign:'center'"  editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',groupSeparator:'.',min:0,required:true,disabled:true}}" formatter="formatRp" hidden>Item Total Amount</th>-->
                                    
                                    <th field="ItemTotalAmount"  width="50"  data-options="align:'right',halign:'center'"  
                                    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="POrdDetailID"     width="20"  data-options="halign:'center',align:'center'" editor="{type:'text'}" hidden>POrd Detail ID</th>
                                    <th field="ItemCategoryID"   width="20"  data-options="halign:'center',align:'center'" editor="{type:'text'}" hidden>Item Category ID</th>
                                    <th field="ItemType"         width="20"  data-options="halign:'center',align:'center'" editor="{type:'text'}" hidden>Item Type</th>
                                    <th field="PPn10_Pcnt"       width="20"  data-options="halign:'center',align:'center'" editor="{type:'text'}" hidden>PPn10 Pnct</th>
                                    <th field="ItemQtyRI"        width="20"  data-options="halign:'center',align:'center'" editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',required:true},min:1}" formatter="formatRp" hidden>ItemQtyRI</th>
                                    <th field="ItemCategoryCode" width="20"  data-options="halign:'center',align:'center'" editor="{type:'text'}" hidden>Item Category Code</th>
                                    <th field="ItemStatus"       width="20"  data-options="halign:'center',align:'center'" editor="{type:'text'}" hidden>Item Status</th>
                                    <th field="ItemDisc"         width="20"  data-options="halign:'center',align:'center'" editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',required:true,min:0}}" formatter="formatRp" hidden>Item Disc</th>
                                </tr>
                            </thead>
                        </table>
                        
                        <div id="tb_dtg_item">
                            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="remove_row();">Delete</a>								
                        </div>
                    </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">
                            <input type="checkbox" name="chk_taxtable" id="chk_taxtable" value="1" readonly hidden>
                            <input type="checkbox" name="chk_inclusive" id="chk_inclusive" value="1" readonly hidden>
                            
                            <input type="text" id="txt_sub_total" name="txt_sub_total" class="easyui-numberbox" value="0" style="text-align:right;" 
                            data-options="min:0,decimalSeparator:',',groupSeparator:'.',
                            formatter:function(v){
                                return v;
                            },
                            parser:function(s){
                                var v = parseFloat(s);
                                return v;
                            }
                            " 
                            hidden readonly />
                            <input type="text" id="txt_ppn10" name="txt_ppn10" class="easyui-numberbox" value="0" style="text-align:right;" 
                            data-options="min:0,decimalSeparator:',',groupSeparator:'.',
                            formatter:function(v){
                                return v;
                            },
                            parser:function(s){
                                var v = parseFloat(s);
                                return v;
                            }
                            " 
                            hidden readonly />
                            <input type="text" id="txt_total_purchase" name="txt_total_purchase" class="easyui-numberbox" value="0" style="text-align:right;" 
                            data-options="min:0,decimalSeparator:',',groupSeparator:'.',
                            formatter:function(v){
                                return v;
                            },
                            parser:function(s){
                                var v = parseFloat(s);
                                return v;
                            }
                            " 
                            hidden readonly />
                        </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>&nbsp;</td>
                <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 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="parent.tab_close('tt');">Close</a>
                </td>
            </tr>
        </table>
    </div>
</div>

<div id="dialog-po" class="easyui-dialog" closed="true" data-options="modal:true, iconCls:'icon-save'" buttons="#dialogpo-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="Select Purchases Orders" style="padding:10px" >
            
             <table id="tt_choose_po" class="easyui-datagrid" 
                url="<?=base_url() . $menu_link . "/ajax_for_choose_po"?>" 
                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="147" halign='center' align='center' sortable="true">PO. Number</th>
                        <th field="PODate" width="90" halign='center' align='center' sortable="true">PO. Date</th>
                        <th field="POrdDesc" width="300" halign="center" align="left">Description</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_po();">&nbsp; OK &nbsp; &nbsp;</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-po');">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">
	var SavePrivillage 	= '<?php echo $group_user->SavePrivillage;?>';
	
    function select_po(){
        var Vendor          = $('#cbg_vendor').combogrid('grid');	// get datagrid object
        var SelectedVendor  = Vendor.datagrid('getSelected');    // get the selected row
	
        if(SelectedVendor === null ){
            $.messager.alert('Maaf','Silahkan pilih Vendor dulu','warning');
        }
        else{
            modal_open('Purchase Order', 'dialog-po');
        }		
    }
	
    function choose_po(){		
        flag	= true;
        po      = $('#tt_choose_po').datagrid('getSelected');
		
        po_grid = $('#dtg_item').datagrid('getRows');
	
         $('#dtg_item').datagrid('load',{
			Number: po.POrdNumber	
		});

		$('#txt_pord_no').val(po.POrdNumber);
		$('#txt_pord_date').val(po.POrdDate);
		$('#cbo_ship_via').combogrid('setValue', po.ShipmentID);
		$('#cbo_terms').combogrid('setValue', po.TermsPaymentID);
		$('#cbo_fob').combogrid('setValue', po.FOBID);

		if (po.POrdTaxesStatus == 1) {$("#chk_taxtable").prop("checked", true);}
		if (po.POrdInTaxStatus == 1) {$("#chk_inclusive").prop("checked", true);}

		$('#hdn_ppn10_pcnt').val(po.POrdPPn10Pcnt);
		$('#hdn_currency_code').val(po.CurrencyCode);
		$('#hdn_currency_rate').val(po.CurrencyRate);
		$('#txa_description').val(po.POrdDesc);
		
		$('#txt_sub_total').numberbox('setValue', po.POrdSubTotal);
		$('#txt_ppn10').numberbox('setValue', po.POrdPPn10);
		$('#txt_total_purchase').numberbox('setValue', parseFloat(po.POrdSubTotal) + parseFloat(po.POrdPPn10));

		modal_close('dialog-po');              
    }
	
    $(document).ready(function(){

        $('#fm_receive_item').click(function(){
            $('#dtg_item').edatagrid('saveRow');
        });

        /*============== 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 ===================*/
	
    	//when vendor selected
    	$('#cbg_vendor').combogrid({
            onSelect:function(rowIndex, rowData){
                var vend_curr_code  = rowData.Vnd_CurrencyCode;
                
                $('#dtg_item').datagrid('load',{
                    Number: ''			
                });

                $('#tt_choose_po').datagrid('load',{
                    VendorCode : rowData.VendorCode
                });	

                var Address = rowData.VendorAddress + ' ' + rowData.VendorCity + ' ' + rowData.VendorState + ' ' + rowData.VendorCountry;
                
                $('#txa_address').val(Address);			
                $('#VendorStatus').val(rowData.VendorStatus);
            },
            onLoadSuccess:function(data){
                if(POrdNumber !=''){
                    if(data.rows.length > 0){
                        $('#cbg_vendor').combogrid('setValue',VendorCode);
                    }
                }
            }
    	});
	
    	$('#dtg_item').edatagrid({
                onLoadSuccess:function(){
                    $('#dtg_item').edatagrid('saveRow');
                    var item    = $('#dtg_item').datagrid('getRows');
                    
    				var ppn10_pcnt      = $('#hdn_ppn10_pcnt').val();
    				ppn10_pcnt 			= parseFloat(ppn10_pcnt);
    				item_total 			= 0;
    							
                    $.each(item, function(index, value) {
    					var price 	= value.ItemPrice;
    					var qty 	= value.ItemQty;
    					var disc    = value.ItemDisc;
    				  
    				  	/*
    						Harga dikurangi discount
    					*/
    					var total_price 	= (parseFloat(price) - (parseFloat(price) * disc / 100)); 
    					/*====================End===============================*/
    					
    					/*
    						Jika dia inclusive
    					*/
    					if($('#chk_taxtable').is(':checked')){
    						 if($('#chk_inclusive').is(':checked')){
    							total_price 	= parseFloat((100 / (100 + ppn10_pcnt) * total_price)); 
    						}
    					}
    					/*===============END=======================*/
    					
    					var amount          = (parseFloat(qty) * parseFloat(total_price).toFixed(9));
    					var ppn             = 0;
    					
    					if($('#chk_taxtable').is(':checked')){
    						ppn = (parseFloat(amount) * (ppn10_pcnt / 100).toFixed(9));
    					}
                       
    					item_total  = (amount + ppn);
    					
                        $('#dtg_item').datagrid('updateRow',{
                            index:index,
                            row:{ItemAmount         : amount,
                                 ItemTaxPPn10       : ppn,
                                 ItemTotalAmount    : item_total}
                        });                    
                    });
                },
                

                onBeginEdit:function(rowIndex,rowDatax){
                    var editors = $('#dtg_item').datagrid('getEditors', rowIndex);	
                    var n0      = $(editors[0].target);  // POrdNumber		
                    var n1      = $(editors[1].target); //Item Qty
                    var n2      = $(editors[2].target); //Item Price
                    var n3      = $(editors[3].target); //Item Amount
                    var n4      = $(editors[4].target); //Item Max
                    var n5      = $(editors[5].target); //Item Tax
                    var n6      = $(editors[6].target); //Item Total Amount
    				var n7      = $(editors[14].target); //Item Disc
    				var n8      = $(editors[9].target); //Item Type
    				
    				/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
    				 save_button_begendedit(SavePrivillage);
    				/*=================================end============================================================*/
    				var item_type  	= n8.text('getData').val();
    				if(item_type == 2){
    					$(n1).prop('readonly',true);
    				}

                    $(n0).prop('readonly', true);
                    $(n0).click(function(){
                        var TransNo             = rowDatax.POrdNumber;
                        parent.tab_edit('Edit Purchase Order','list/purchases_order', 'icon-edit', 'edit',TransNo);
                    });
    							
                    $(n1).numberbox({
                        onChange:function(){
                            var jml 		= n1.numberbox('getValue');
                            var price 		= n2.numberbox('getValue');
                            var jml_max 	= n4.numberbox('getValue');
    						var disc    	= n7.numberbox('getValue');
    						
                            if(parseFloat(jml) > parseFloat(jml_max)){
                                $.messager.alert('Maaf','Quantity tidak boleh lebih dari ' + jml_max,'error',function(){		
    								n1.numberbox('setValue', jml_max);
    								
                                    n3.numberbox('setValue', 0);
                                    $('#dtg_item').edatagrid('saveRow');
                                });
                            }
                            else {
                                var ppn10_pcnt      = $('#hdn_ppn10_pcnt').val();
    							ppn10_pcnt 			= parseFloat(ppn10_pcnt);
    							
    							/*
    								Harga dikurangi discount
    							*/
    							var total_price 	= (parseFloat(price) - (parseFloat(price) * disc / 100)); 
                                /*====================End===============================*/
    							
    							/*
    								Jika dia inclusive
    							*/
    							
    							if($('#chk_taxtable').is(':checked')){
    								 if($('#chk_inclusive').is(':checked')){
    									total_price 	= parseFloat((100 / (100 + ppn10_pcnt) * total_price)); 
    								}
    							}
    							
    							/*===============END=======================*/
    							
    							var amount          = (parseFloat(jml) * parseFloat(total_price).toFixed(9));
                                var ppn             = 0;
                                
                                if($('#chk_taxtable').is(':checked')){
                                    ppn = (parseFloat(amount) * (ppn10_pcnt / 100).toFixed(9));
                                }

                                n3.numberbox('setValue', amount);
                                n5.numberbox('setValue', ppn);
                                n6.numberbox('setValue', parseFloat(amount + ppn));
                                calculate_total();
                            }
                        }
                    });
                },
    			
    			onEndEdit: function(){
    				/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
    				save_button_endedit(SavePrivillage);
    				/*=================================end============================================================*/
                }
    	});
        
        $('#tt_choose_po').datagrid({
            onLoadSuccess:function(data){
                vendor = $('#cbg_vendor').combogrid('grid').datagrid('getSelected');
            
               //if(vendor){
                   //rows = $('#tt_choose_po').datagrid('getRows');
            
                   //if( rows.length === 0 ){
                       //$('#tt_choose_po').datagrid('reload');
                   //}
               //}

               /*==================== Daswan ============================*/
               if(POrdNumber!=''){
                    var data_po  = $('#tt_choose_po').datagrid('getRows');
                    var ind_pord = findWithAttr(data_po,'POrdNumber',POrdNumber);
                    $('#tt_choose_po').datagrid('checkRow',ind_pord);
                    choose_po();
                }
               /*==================== end Daswan ==========================*/
            }
        });
        datagrid_item_type("dtg_item");//untuk warna grid
    });

    
	function calculate_total(){
        $('#dtg_item').edatagrid('saveRow');

        var item            = $('#dtg_item').datagrid('getRows');
        var sub_total       = 0;
        var sub_ppn10       = 0;
        var total_amount    = 0;

        $.each(item, function(index, value) {
            if(value.ItemAmount != undefined){
                sub_total       += parseFloat(value.ItemAmount);
                sub_ppn10       += parseFloat(value.ItemTaxPPn10);
                total_amount    += parseFloat(value.ItemTotalAmount);
            }				
        });

        $('#txt_sub_total').numberbox('setValue', sub_total);
        $('#txt_ppn10').numberbox('setValue', sub_ppn10);
        $('#txt_total_purchase').numberbox('setValue', total_amount);
    }

    function remove_row(){
        var item = $('#dtg_item').datagrid('getData').total;

        if(item <= 1){
            $.messager.alert('Warning','Anda tidak dapat menghapus list item, minimum list item adalah satu (1)','error');
        }
        else {
            delete_row();
        }            
    }

    function delete_row(){
        var selectedrow     = $('#dtg_item').datagrid('getSelected');
        var row_index       = $('#dtg_item').datagrid('getRowIndex', selectedrow);

        $.messager.confirm('Confirmation :', 'Anda yakin, Anda ingin menghapus item "' + selectedrow.ItemCode + '"?',  
            function(r){
                if (r){
                    var item = $('#dtg_item').datagrid('getData').total;

                    if(item == 0){
                        $("#chk_inclusive").prop("checked",false);
                    }
                    
                    $('#dtg_item').datagrid('deleteRow', row_index);
                    calculate_total();
					/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
					$('.save-button').linkbutton('enable');
					/*=================================end============================================================*/
                }
            }
        );
    }

    function insert_data(){
        $('#dtg_item').edatagrid('saveRow');

        var i       = $('#dtg_item').datagrid('getRows');
        var detail  = JSON.stringify(i);

        $("#detail").val(detail);
        save_transaction('<?php echo $menu_link?>','#fm_receive_item');
    }
</script>