
<!---- 
    Author 	: Fachri
    Tanggal : 08-06-2016	
    Revisi 	:
-->
<?php 
//---------Untuk privellege jika dia ada tombol save,suspen,jurnal----------
	if($group_user->SavePrivillage == 1){
		$cek_autority_save  = "";
	}
	else{
		$cek_autority_save 	= "disabled";
	}
	
	if($group_user->JournalPrivillage == 1){
		$cek_autority_journal  = "";
	}
	else{
		$cek_autority_journal 	= "disabled";
	}
	
	if($group_user->DeletePrivillage == 1){
		$cek_autority_delete  = "";
	}
	else{
		$cek_autority_delete 	= "disabled";
	}
	
	if($detail->TransStatus == 5) {
		$txt_ship_to = "";
		$txt_desc = "";
		$btn_delete = "";
	}
	else {
		$txt_ship_to = "readonly";
		$txt_desc = "readonly";
		$btn_delete = "disabled";
	}
//-------------end----------------------------
?>
<div class="easyui-layout" data-options="fit:true,border:false">
	<!-- Top view-->
    <div id="form-header" data-options="region:'north',border:false">
        <div id="form-header-wrapper">
            <table cellpadding="0" cellspacing="0" width="100%">
            <tr>
                <td align="left" valign="middle">
					<input type="checkbox" name="chk_suspended" id="chk_suspended" value="3" onClick="suspend(1);" style="margin-top:0px;margin-right:1px;" <?php if($detail->TransStatus == 3){ echo"checked"; } ?> <?=((!in_array($detail->TransStatus,array(0,6,5)))?"disabled":"")?> <?php echo $cek_autority_delete;?>/>
                    <span style="display:inline-block;margin-top:2px;<?=(($detail->TransStatus==3)?'color:gray;':'color:black')?>">Suspend </span>
                	<a href="#" class="easyui-linkbutton" iconCls="icon-journal" plain="true" onclick="parent.tab_add('Preview Journal <?=$menu_label?>', '<?php echo $menu_link; ?>', 'icon-print', 'to_preview_journal/<?php echo $detail->PDpayNumber ?>')" <?php echo $cek_autority_journal;?>>Journal</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"  style="width:1180px;height:526px;padding:5px;" fit='true'>
        	<?php if(in_array($detail->TransStatus,array(2,3,9,8))){ ?>
        	<div class="blanket"></div>
            <div class="status-box"><b><?php if($detail->TransStatus==2){ echo 'Finished'; }elseif($detail->TransStatus==3){ echo 'Suspended'; }elseif($detail->TransStatus==9){ echo 'Canceled by Dir.'; }elseif($detail->TransStatus==8){ echo 'Suspended Item'; }else{ echo 'Unknown'; } ?></b></div>
            <?php } ?>
			<form id="fm_purchase_dp" name="fm_purchase_dp" method="post">
                <table cellpadding="0" cellspacing="3" width="100%" height="100%" border="0">
					<tr height="20">
                            <td width="120"><b>Branch Name</b></td>
                            <td width="10"><b>:</b></td>
                            
                            <td>
                                <select class="combo-grid" name="cbg_branch" id="cbg_branch" style="width:252px;height:22px;" required <?=(($this->session->userdata("branch_status")==1)?"disabled":'') ?> editable="false" readonly
                                    data-options="
                                        panelWidth:400,
                                        value:'<?=$detail->BranchPaidFor?>',
                                        idField: 'BranchCode',
                                        textField: 'BranchName',
                                        url: '<?=base_url()?>custom/get_data/branch_no_filter',
                                        method: 'get',
                                        columns: [[
                                            {field:'BranchCode',title:'Branch No',width:100,halign:'center'},
                                            {field:'BranchName',title:'Branch Name',width:161,halign:'center'}
                                        ]],
                                        fitColumns: true
                                    ">
                                </select>
                            </td>
                            
                            <td align="left" valign="middle">&nbsp;</td>
						<td rowspan="6" align="left" valign="top">
							<table cellpadding="0" cellspacing="0" width="100%" height="100%" border="0">
								<tr>
									<td align="right" valign="top">
										<table cellpadding="0" cellspacing="1" width="100%" border="0">
											<tr>
												<td height="20">&nbsp;</td>
												 <td width="190" align="center" valign="middle" bgcolor="#333399"><span style="color: #fff">Purchase DP No.</span></td> 
												<td width="122" align="center" valign="middle" bgcolor="#333399"><span style="color:#fff;">Purchase Dp Date</span></td>
											</tr>
											<tr>
												<td>&nbsp;</td>
												 <td height="20" align="left" valign="top"> 
												 	<input type="text" name="txt_dp_no" id="txt_dp_no" class="easyui-validatebox textbox" style="width:221px;height:18px;text-align:center;" value="<?php echo $detail->PDpayNumber; ?>" required readonly /> 
												 </td> 
												<td align="right" valign="top"><input name="txt_payment_date" id="txt_payment_date" type="text" class="easyui-datebox validBackward" style="width:122px;height:20px" required editable="false" readonly value='<?=convert_to_dmy($detail->PDpayDate)?>'/></td>
											</tr>
                                            
										</table>
									</td>
								</tr>
							</table>
						</td>
                        </tr>
                    <tr>
						<td width="143"><b>Vendor</b></td>
                        <td width="10"><b>:</b></td>
                        <td colspan="5">
                            <select class="easyui-combogrid" name="cbg_vendor" id="cbg_vendor" style="width:252px;height:22px;" required readonly
                                data-options="
                                	value:'<?=$detail->VendorCode?>',
                                    panelWidth:400,
                                    idField: 'VendorCode',
                                    textField: 'VendorName',
                                    url: '<?=base_url()?>custom/get_data/vendor_new_by_branch',
                                    method: 'get',
                                    columns: [[
                                        {field:'VendorCode',title:'Vendor No',width:100,halign:'center'},
                                        {field:'VendorName',title:'Vendor Name',width:161}
                                    ]],
                                    fitColumns: true
                                ">
                            </select>
                            <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-sales'" onClick="select_sales();" disabled>Select DP.</a>
                        </td>
					</tr>
                    <tr>
						<td width="143"><b>No Trans From.</b></td>
                        <td width="10"><b>:</b></td>
                        <td colspan="5">
							<input type="text" name="txt_trans_dp_no" id="txt_trans_dp_no" class="easyui-validatebox textbox" style="width:221px;height:18px;text-align:left;" value="<?php echo $detail->POrdNumber;?>" required readonly /> 
						</td>
						
					</tr>					
					<tr>
						<td valign="top" height="51" align="left" style="padding-top:4px;"><b>Description</b></td>
                        <td valign="top" style="padding-top:4px;"><b>:</b></td>
						<td valign="top" align="left">
							<textarea class="textbox" name="txa_memo" id="txa_memo" rows="4" style="width:362px;resize:none;white-space:normal;"><?php echo $detail->PDpayDesc;?></textarea>
						</td>
                        <td valign="top" align="left">&nbsp;</td>
					</tr>
					<tr>
						<td height="22" valign="top" align="left"><b>Down Payment Owing</b></td>
                        <td width="10"><b>:</b></td>
						<td valign="top" height="22" align="left">
							<input name="owing_amount"  	 id="owing_amount" type="text" class="easyui-numberbox" data-options="precision:2,decimalSeparator:',',groupSeparator:'.',disabled:true" autoComplete="off" style="width:185px;" value="<?php echo $detail->PDpayAmount;?>" required="required" />
							
                            ~ <input name="txt_equal_invoice"  	 id="txt_equal_invoice" type="text" class="easyui-numberbox" data-options="precision:2,decimalSeparator:',',groupSeparator:'.',disabled:true" autoComplete="off" style="width:162px;" value="<?php echo $summing_data;?>" required="required" />
                            <input name="txt_amount" 		  id="txt_amount" type="hidden" class="easyui-numberbox" data-options="precision:2,decimalSeparator:',',groupSeparator:'.',disabled:true" autoComplete="off" style="width:362px;" />
							<input name="owing_amount_hidden" id="owing_amount_hidden" type="hidden" class="easyui-numberbox" data-options="precision:2,decimalSeparator:',',groupSeparator:'.',disabled:true" autoComplete="off" style="width:362px;" value="<?php echo $detail->PDpayAmount;?>"/>
							<input type="hidden" 			  id="detail" name="detail" />
							<input type="hidden" 			  id="detail_id" name="detail_id">
                            <input type="hidden" 			  id="txt_dp_number" name="txt_dp_number" value="<?php echo $detail->POrdNumber;?>"/>
                            <input type="hidden" 			  id="txt_currency_code" name="txt_currency_code" value="<?php echo $detail->CurrencyCode;?>"/>
						</td>
						<td valign="top" align="left">&nbsp;</td>
					</tr>
					<tr>
						<td valign="top" height="22" align="left">
							<input name="txt_rate" id="txt_rate" type="hidden" class="easyui-numberbox" data-options="precision:2,decimalSeparator:',',groupSeparator:'.'" autoComplete="off" style="width:200px;" required="required" value="<?php echo $detail->CurrencyRate;?>"/>
						</td>
						<td valign="top" align="left">&nbsp;</td>
					</tr>
                    <tr height="20" style="display:none">
                        <td width="150" align="left"><b>Bank Balance (<span class='bank_balance_currency'></span>)</b></td>
                        <td width="10"><b>:</b></td>
                        <td >
                            <input name="txt_bank_balance" id="txt_bank_balance" type="text" class="easyui-numberbox" data-options="precision:2,decimalSeparator:',',groupSeparator:'.',prefix:''" style="text-align:right;width:362px" readonly />
                        </td>
                    </tr>					
				</table>
                
                <!-- Item Grid -->
                <div class="easyui-tabs" data-options="tabHeight:18" style="margin:5px 0px;">
                    <div title="Account">
                        <table toolbar="#tb_dtg_sales_journal" id="dtg_list_invoice" 
                        idField="id" rownumbers="true" fitColumns="true" singleSelect="true" style="height:290px;"
                        
                        url="<?php echo base_url().$menu_link."/ajax_item_for_edit/".$detail->PDpayNumber; ?>"
                        >
                            <thead>
                                <tr>
                                	<th field="AccountName" 	halign="center"	align="left" valign="middle"	data-options="width:900,align:'left'" 	editor="{type:'text',options:{required:true}}">Account Name</th>
                                    <th field="CurrencyCode"	halign='center'	align="left" valign="middle"	data-options="width:300,align:'left'"  	editor="{type:'text',options:{required:true}}"><?php echo label_grid('curr_code');?></th>
                                    <th field="Amount" 			halign="center" align="left" valign="middle"	data-options="width:470,align:'left'" 	editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',required:true}}" formatter="formatRp">Amount</th>
                                    <th field="Rate" 			halign="center" align="left" valign="middle"	data-options="width:470,align:'left'" 	editor="{type:'numberbox',options:{precision:2,decimalSeparator:','}}" formatter="formatRp"><?php echo label_grid('rate');?></th>
                                    <th field="PrimeAmount" 	halign="center" align="left" valign="middle"	data-options="width:510,align:'left'" 	editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',required:true}}" formatter="formatRp">Equal(~)</th>
                                    <th field="Memo"			halign='center'	align="left" valign="middle"	data-options="width:700,align:'left'"  	editor="{type:'text'}">Memo</th>
                                    <th field="AccountNumber" 	halign="center"	align="left" valign="middle"	data-options="width:900,align:'left'" 	editor="{type:'text',options:{required:true}}" hidden>Account Number</th>
                                    <th field="PDpayDetailID" 	halign="center"	align="left" valign="middle"	data-options="width:900,align:'left'" 	editor="{options:{required:true}}" hidden>SDpayDetail ID</th>
                                    <th field="SubtypeName" 	halign="center"	align="left" valign="middle"	data-options="width:900,align:'left'" 	editor="{type:'text',options:{required:true}}" hidden>SubtypeName</th>
                                    <th field="TypeName" 		halign="center"	align="left" valign="middle"	data-options="width:900,align:'left'" 	editor="{type:'text',options:{required:true}}" hidden>TypeName</th>
                                </tr>
                            </thead>
                        </table>
                        
                        <div id="tb_dtg_sales_journal">
                            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="remove_row()">Delete</a>							
                        </div>
                    </div>
                </div>
                <!-- End of item grid -->
            </form>
		</div>
	</div>
    <div id="form-footer" data-options="region:'south',border:false">
    	<table width="100%" cellpadding="0" cellspacing="0">
        <tr>
        	<td>&nbsp;</td>
            <td align="right">
            		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-help" plain="true">Help</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button app-button" iconCls="icon-ok" onclick="approval_sdpay();" <?=($detail->TransStatus==5 and $this->session->userdata('jobtitle_status')==0 and date('d-m-Y')>=date('d-m-Y', strtotime($detail->PDpayDate)))?'':'disabled'?>>App. Dir</a>					
                    <?php
					if(($group_user->ReversePrivillage == 1)){
					?>
					<a href="javascript:void(0)" class="easyui-linkbutton form-button app-button" iconCls="icon-reload" onclick="app_reverse()" <?=(($this->session->userdata("jobtitle_status") == 0) && (in_array($detail->TransStatus,array(1,2,8))))?'':'disabled'?>>Reverse</a>  
					<?php
					}
					?>
                    <a id="save_button" href="javascript:void(0)" class="easyui-linkbutton form-button save-button" iconCls="icon-save" onclick="proses_update();" <?=(in_array($detail->TransStatus,array(0,1,2,3)))?'disabled':''?> <?php echo $cek_autority_save;?>>Save</a> 								
                    <!-- <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-print" onclick="link_print('<?php echo $menu_link ?>/to_print_detail/<?php echo $detail->PDpayNumber ?>');" >Preview & Print</a>   -->
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-print" onclick="parent.tab_add('Form <?=$menu_label?>', '<?=$menu_link?>', 'icon-print', 'to_print_detail/<?=$detail->PDpayNumber?>');" <?=(in_array($detail->TransStatus,array(3,9,2)))?'':'disabled'?>>Preview & Print</a>  
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-cancel" onclick="<?=($this->session->userdata('jobtitle_status')==0)?'suspend(2)':'parent.tab_close(\'tt\')'?>" <?=($this->session->userdata('jobtitle_status')==0 && !in_array($detail->TransStatus,array(0,5,7)))?'disabled':''?>><?=($this->session->userdata('jobtitle_status')==0)?'Cancel':'Close'?></a>
            </td>
        </tr>
        </table>
    </div>
</div>
<!----------------------------------------------Batas Awal Dialog Sales Invoice-------------------------------------------------->
<div id="dialog-si" class="easyui-dialog" closed="true" data-options="modal:true, iconCls:'icon-save'" buttons="#dialog-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="Down Payment" style="padding:10px" >
            
             <table id="tt_choose" class="easyui-datagrid" 
                url="<?php echo base_url() ?>list/sales_dp/dp_filter" 
                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="POrdNumber" 		width="200" sortable="true">Trans. Number</th>
                        <th field="SaldoDPBalance" 	width="180" editor="{type:'numberbox',options:{precision:2,decimalSeparator:','}}" formatter="formatRp" sortable="true">DP Balance</th>
                    	<th field="SInvDesc" 		width="306">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_item_si();">&nbsp; OK &nbsp; &nbsp;</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-si');">Cancel</a>
            </div>
            <!-- End of Dialog Button for Dialog Item-->
            </div>
         </div>
</div>
<!---------------------------------------------Batas Akhir Dialog Purchase Invoice------------------------------------------------>

<div id="dialog-account" class="easyui-dialog" style="width:850px;height:523px;top:27px;background-color:#F8F8F8;padding:0px" closed="true" data-options="modal:true, iconCls:'icon-save'" buttons="#dialog-buttons-account-payment">
    <table width="100%" align="left" border="0">
        
        <tr>
            <td><b>Account Number</b></td>
            <td width="10"><b>:</b></td>
            
            <td>
                <input type="text" style="width:705px;height:20px" name="txt_AccountNumber" id="txt_AccountNumber" class="easyui-validatebox" placeholder="Account Number" onkeyup="search_akun();" />
            </td>
        </tr>
        
        <tr>
            <td><b>Account Name</b></td>
            <td><b>:</b></td>
        
            <td>
                <input type="text" style="width:705px;height:20px" name="txt_AccountName" id="txt_AccountName" class="easyui-validatebox" placeholder="Account Name" onkeyup="search_akun();" />
            </td>
        </tr>
        
        <tr>
            <td colspan="3">
                <table id="grid_akun" class="easyui-datagrid" singleSelect="true"  rownumbers="true" style="width:828px;height:387px;"
                    pageSize="150" 
                    data-options = "border:true,view:scrollview,autoRowheight:false" 
                >
                    <thead>
                        <tr bgcolor="">
                           	<th field="AccountNumber" width="150" sortable="true" formatter="format_acc">Account Number</th>
                            <th field="AccountName" width="304" sortable="true" formatter="format_acc">Account Name</th>
                            <th field="SubtypeName" width="300" sortable="true" >Type Name</th>
                            <th field="CurrencyCode" width="460" sortable="true" hidden="true">Currency Code</th>
                            <th field="AccountLevel" width="460" sortable="true" hidden="true">Account Level</th>
                        </tr>
                    </thead>
                </table>
            </td>
        </tr>
    </table>
</div>

<!-- Dialog Button -->
<div id="dialog-buttons-account-payment">
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_account_journal();">OK</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="close_modal()">Cancel</a>
</div>

<script type="text/javascript">
var SavePrivillage 	= '<?php echo $group_user->SavePrivillage;?>';
var array_item 		= [];
var indChoAccount   = 0;
$(document).ready(function(){ 
	$('#dtg_list_invoice').edatagrid({
		onLoadSuccess:function(){
			calculate_total();
		},
		onBeginEdit:function(rowIndex,rowDatax){
			var editors     = $('#dtg_list_invoice').datagrid('getEditors', rowIndex);
			var AccountName	= $(editors[0].target); 
			var Currency   	= $(editors[1].target);
			var Amount   	= $(editors[2].target); 
			var Rate   		= $(editors[3].target); 
			var PrimeAmount   	= $(editors[4].target); 
			var Memo   		= $(editors[5].target); 
			
			Currency.prop('disabled',true);
			PrimeAmount.prop('disabled',true);
			var currency_code = $('#txt_currency_code').val();
			var CurrAccountCode = Currency.val();
			
			$(AccountName).click(function(){
				open_account_journal(rowIndex);
			}).mouseover(function(){
				$(this).css('cursor','pointer');
			})
			
			if(AccountName.val() != ""){
				if(currency_code == 'IDR'){
					if(CurrAccountCode == 'IDR'){
						Rate.prop('disabled',true);
					}
					else{
						Rate.prop('disabled',false);
					}
				}
				else{
					if(CurrAccountCode == 'IDR'){
						Rate.prop('disabled',false);
					}
					else{
						Rate.prop('disabled',false);
					}
				}
			}
			
			$(Amount).numberbox({
				onChange:function(){
					if(currency_code == 'IDR'){
						if(CurrAccountCode == 'IDR'){
							PrimeAmount_Con = Amount.numberbox('getValue') * Rate.numberbox('getValue');
						}
						else{
							PrimeAmount_Con = Amount.numberbox('getValue') * Rate.numberbox('getValue');
						}
					}
					else{
						if(CurrAccountCode == 'IDR'){
							PrimeAmount_Con = Amount.numberbox('getValue') / Rate.numberbox('getValue');
						}
						else{
							PrimeAmount_Con = Amount.numberbox('getValue') * Rate.numberbox('getValue');
						}
					}
	
					$(PrimeAmount).numberbox('setValue', PrimeAmount_Con);
				}
			}); 
			
			$(Rate).numberbox({
				onChange:function(){
					if(currency_code == 'IDR'){
						if(CurrAccountCode == 'IDR'){
							PrimeAmount_Con = Amount.numberbox('getValue') * Rate.numberbox('getValue');
						}
						else{
							PrimeAmount_Con = Amount.numberbox('getValue') * Rate.numberbox('getValue');
						}
					}
					else{
						if(CurrAccountCode == 'IDR'){
							PrimeAmount_Con = Amount.numberbox('getValue') / Rate.numberbox('getValue');
						}
						else{
							PrimeAmount_Con = Amount.numberbox('getValue') * Rate.numberbox('getValue');
						}
					}
	
					$(PrimeAmount).numberbox('setValue', PrimeAmount_Con);
				}
			}); 
			/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
			save_button_begendedit(SavePrivillage);
			/*=================================end============================================================*/
			
		},
		onEndEdit:function(rowIndex, rowData, changes){
			calculate_total();
			save_button_endedit(SavePrivillage);
	   }
	});
	
	var branch = $('#cbg_branch').combogrid('getValue');
	
	search_account('<?php echo $detail->CurrencyCode;?>',branch);
	
	$('#fm_purchase_dp').click(function(){
		$('#dtg_list_invoice').edatagrid('saveRow');
	});     
});

function calculate_total(){
	var item        = $('#dtg_list_invoice').edatagrid('getRows');
	var sub_total   = 0;
	var amount  	= 0;
	var currency_code = $('#txt_currency_code').val();
	
	$.each(item, function( index, value ) {
		if(currency_code == 'IDR'){
			if(value.CurrencyCode == 'IDR'){
				amount  = isNaN(value.Amount) ? 0 : value.Amount;
				PrimeAmount_Con = value.Amount * value.Rate;
			}
			else{
				amount  = isNaN(value.Amount) ? 0 : value.Amount * value.Rate;
				PrimeAmount_Con = value.Amount * value.Rate;
			}
		}
		else{
			if(value.CurrencyCode == 'IDR'){
				amount  = isNaN(value.Amount) ? 0 : value.Amount / value.Rate;
				PrimeAmount_Con = value.Amount / value.Rate;
			}
			else{
				amount  = isNaN(value.Amount) ? 0 : value.Amount;
				PrimeAmount_Con = value.Amount * value.Rate;
			}
		}
		
		sub_total		+= parseFloat(amount);	
		
		$('#dtg_list_invoice').datagrid('updateRow',{
			index:index, 
			row:{PrimeAmount       : PrimeAmount_Con}
		 });		
	});
	$('#txt_amount').numberbox('setValue', sub_total);
}




function open_account_journal(indexRow){ 
	indChoAccount = indexRow;
   modal_open('Account List','dialog-account');
}

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

	if(item <= 1){
		$.messager.alert('Warning','Anda tidak dapat menghapus list item, minimum list item adalah satu (1)','error');
	}
	else {
		var selectedrow     = $('#dtg_list_invoice').datagrid('getSelected');
		var row_index       = $('#dtg_list_invoice').datagrid('getRowIndex', selectedrow);

		$.messager.confirm('Confirmation :', 'Anda yakin, Anda ingin menghapus item "' + selectedrow.AccountName + '"',  
			function(r){
				if (r){
					array_item.push(selectedrow.SDpayDetailID);
					
                   	$("#detail_id").val(JSON.stringify(array_item));
                   	$('#dtg_list_invoice').datagrid('deleteRow', row_index);	
					calculate_total();
					save_button_endedit(SavePrivillage);
				}
			}
		);        
					
	}            
}

function close_modal(){
	modal_close('dialog-account');
}

function format_acc(val,row){
	return '<span style="padding-left:'+row.AccountLevel+'em;">'+val+'</span>';	
}

function search_account(CurrencyCode,branch){
	$('#grid_akun').datagrid({
		url:'<?php echo base_url(); ?>list/purchase_dp/get_akun/'+CurrencyCode+'/'+branch
	});
}

function choose_account_journal(){
	var selected_data	=	$('#grid_akun').datagrid('getSelected');
	get_master_rate_on_grid(selected_data.CurrencyCode,function(MstRate){
    	$('#dtg_list_invoice').datagrid('updateRow',{
			index: indChoAccount,
			row: {
				AccountName     : selected_data.AccountName,
				Rate           	: MstRate.value,
				Amount          : 0,
			   CurrencyCode     : selected_data.CurrencyCode,
			   AccountNumber 	: selected_data.AccountNumber,
			   SubtypeName 		: selected_data.SubtypeName,
			   TypeName 		: selected_data.TypeName
			}
		});
		
		$('#dtg_list_invoice').edatagrid('beginEdit', indChoAccount);
    });
	
	//$('#dtg_list_invoice').edatagrid('saveRow');
			
	modal_close('dialog-account');
}
	
function proses_update() {
	var url             = '<?= base_url();?>custom/custom/checking_update';
	var no_transaksi    = $('#txt_dp_no').val();
	var trans_status 	= '<?=$detail->TransStatus?>';
	
	var created_by 	= '<?php echo $detail->CreatedBy;?>';
	var login_as 		= '<?php echo$this->session->userdata('username');?>';
	var job_title 		= '<?php echo $this->session->userdata('jobtitle_status');?>';
	
	if($('#txt_amount').numberbox('getValue') > $('#owing_amount').numberbox('getValue')){
		$.messager.alert('Maaf','Amount melebihi Total Owing Downpayment, silahkan lihat kembali transaksi ini','warning',function(){	
		
		});
	}
	else if($('#txt_status_customer').val() == 1){
		$.messager.alert('Maaf','Maaf Customer sedang di Lock, silahkan proses Transaksi yang berada di Sales Receipt atau Sales Journal','warning',function(){	
		
		});
	}
	else{	
		var i=$('#dtg_list_invoice').datagrid('getRows');
		var detail = JSON.stringify(i);
		$("#detail").val(detail);
		edit_transaction_check('<?=$menu_link?>','#fm_purchase_dp', no_transaksi, url, trans_status, created_by, login_as, job_title);
	}
}

function suspend(type){
	var created_by 		= '<?php echo $detail->CreatedBy;?>';
	var login_as 			= '<?php echo$this->session->userdata('username');?>';
	var job_title 			= '<?php echo $this->session->userdata('jobtitle_status');?>';
	var url             = '<?= base_url();?>custom/custom/checking_suspend';
	
	suspend_check_trans('F', type, '<?=$menu_link?>', $('#txt_dp_no').val(), 'Sales DP',url, created_by, login_as, job_title);
}

function approval_sdpay() {
	var url_check           = '<?= base_url();?>custom/custom/checking_approval';
	var no_transaksi    	= $('#txt_dp_no').val();
	
	checking_approval(no_transaksi,function(check){
		if(check.status == false){
			approve_transaction();
		}else{
			$.messager.alert('Unseccessfull',check.message,'error',function(){
				location.reload();
			});
		}
	});
}

function approve_transaction(){
	var item 	= $('#dtg_list_invoice').datagrid('getRows');
	var detail 	= JSON.stringify(item);
	$("#dtg_list_invoice").val(detail);
	proses_custom_data('<?=$menu_link?>/approval_mgr','#fm_purchase_dp');
}

function app_reverse(){
	var no_transaksi    = $('#txt_dp_no').val();

	checking_reverse(no_transaksi,function(check){
		
		if(check.status == true){
			var i       = $('#dtg_list_invoice').datagrid('getRows');
			var detail  = JSON.stringify(i);
	
			$("#detail").val(detail);
			reverse_data_by_detail('<?php echo $menu_link?>','#fm_purchase_dp');
		}else{
		   $.messager.alert('Unseccessfull',check.message,'error',function(){
				location.reload();
			});
		}
	});
}
</script>