<?php
/*	
 *	Author 	: Wahyu / Tanggal : 12-09-2014	
 *	Revisi 	: Ferry
 */
?>

<div id="cc" class="easyui-layout" data-options="fit:true, border:false">
    <div data-options="region:'west',split:true, border:true" title="" style="width:250px; background-color:#F8F8F8;">
        <div title="" style="padding:5px;overflow:auto;" data-options="selected:true" >				
            <div class="preference_menu">
                <ul>
                   <li><a href="<?php echo site_url('setup/preference_account_gl'); ?>"><img src="<?php echo base_url(); ?>assets/custom/icons/clipboard-invoice.png" alt=""/><span><b>Account G/L</b></span></a></li>	
                   <li><a href="<?php echo site_url('setup/preference_item'); ?>"><img src="<?php echo base_url(); ?>assets/custom/icons/clipboard-invoice.png" alt=""/><span>Item Account.</span></a></li>	
                   <li><a href="<?php echo site_url('setup/preference_sales'); ?>"><img src="<?php echo base_url(); ?>assets/custom/icons/moneys.png" alt=""/><span>Sales Account</span></a></li>				   	
                   <li><a href="<?php echo site_url('setup/preference_purchases'); ?>"><img src="<?php echo base_url(); ?>assets/custom/icons/sticky-notes.png" alt=""/><span>Purchases Account</span></a></li>	
                   <li><a href="<?php echo site_url('setup/preference_expense'); ?>"><img src="<?php echo base_url(); ?>assets/custom/icons/calculator.png" alt=""/><span>Expense Account</span></a></li>
                   <li><a href="<?php echo site_url('setup/preference_approval'); ?>"><img src="<?php echo base_url(); ?>assets/custom/icons/property.png" alt=""/><span>Sales Approval Limit</span></a></li>	
                   <li><a href="<?php echo site_url('setup/preference_bonus'); ?>"><img src="<?php echo base_url(); ?>assets/custom/icons/money-coin.png" alt=""/><span><b>Bonus Quantity Limit</b></span></a></li>				   
                    <li><a href="<?php echo site_url('setup/preference_miscellaneous'); ?>"><img src="<?php echo base_url(); ?>assets/custom/icons/folder-stand.png" alt=""/><span>Miscellaneous</span></a></li>
                </ul>
            </div>	
        </div>
    </div>
	
	<div data-options="region:'center',fit:true,plain:true, border:false" style="background:#eee;">
		<div style="float:left; padding-left: 0px; width:1000px; height:520px;" >
			<div style="overflow:hidden;padding:1px;">
				<table id="tt" class="easyui-datagrid" url="<?php echo base_url(); ?>setup/preference_bonus/list_quantity_limit" title="" iconCls="icon-save" rownumbers="true" toolbar="#toolbar" pagination="true" fitColumns="true" singleSelect="true" style="width:925px;height:590px">
					<thead frozen="true">
					    <tr>
						    <th colspan="3">Range QTY Bonus</th>
						    <th rowspan="2" field="QtyBonus" width="380" align="center">QTY (pcs)</th>							
						</tr>
						<tr>	
                           	 <th field="BonusLimitID" width="0" align="center">ID</th>					
							<th field="SalesBegin" width="260" formatter="formatRp" align="center">From</th>
							<th field="SalesEnd" width="260" formatter="formatRp" align="center">To</th>
						</tr>						
					</thead>								
				</table>
				
				<!-- Toolbar -->
				<div id="toolbar">
					<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="popup_qty_bonus_limit()">New </a>
					<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="update()">Edit </a>
					<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="delete_bonus_limit()">Delete </a>
				</div>				
			</div>			
        </div>
	</div>
</div>

<!-- Dialog Template Group Expense -->
<div id="dialog-template-bonus-limit" class="easyui-dialog" style="width:620px; height:180px; padding: 10px" closed="true" buttons="#dialog-buttons-qty-bonus" data-options="modal:true">
	<form id="form_template_group_expense" method="post" novalidate>
		<table width="100%" border="0" cellspacing="1" cellpadding="1"> 			        
			<tr>
				 <td width="124" height="15" align="left" valign="middle">QTY Order </td>
				 <td align="left">:</td>
                 <td width="41" align="left">From</td> 	
				 <td height="15" align="left" valign="middle" colspan="2">
					 <input id="SalesBegin"  name="SalesBegin" class="currency" style="width:200px; height:20px; border:1px solid #ccc">
					 <input type="hidden" id="SalesBegin_angka"  name="SalesBegin_angka">
				 </td>
                 <td width="23" align="left">To</td> 	
				 <td width="671" height="35" align="left" valign="middle" colspan="2">
					 <input id="SalesEnd" name="SalesEnd" class="currency" style="width:200px; height:20px; border:1px solid #ccc">
					  <input type="hidden" id="SalesEnd_angka"  name="SalesEnd_angka">
				 </td>
			</tr>
			
			<tr>
				<td width="124" height="25" align="left" valign="middle">QTY Bonus</td>	
                <td width="10" align="left">:</td>
                <td width="41" align="left">&nbsp;</td>					
				<td height="25" align="left" valign="middle" colspan="4"> 
				    <input id="QtyBonus" name="QtyBonus" style="width:200px; height:20px; border:1px solid #ccc">
			    </td>
            </tr>    
		</table>
	</form>
</div>

<!-- Dialog Button -->
<div id="dialog-buttons-qty-bonus">
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="save_data()">Simpan</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dialog-template-bonus-limit').dialog('close')">Batal</a>
</div>

<!-- Dialog Edit Group Expense -->
<div id="dialog-edit-bonus-limit" class="easyui-dialog" style="width:620px; height:180px; padding: 10px" closed="true" buttons="#dialog-edit-qty-bonus" data-options="modal:true">
	<form id="form_edit_bonus_limit" method="post" novalidate>
		<table width="100%" border="0" cellspacing="1" cellpadding="1"> 
            <input type="hidden" id="BonusLimitID"  name="BonusLimitID">		
			<tr>
				 <td width="124" height="15" align="left" valign="middle">QTY Order </td>
				 <td align="left">:</td>
                 <td width="41" align="left">From</td> 	
				 <td height="15" align="left" valign="middle" colspan="2">
					 <input id="SalesBegin_edit"  name="SalesBegin" class="currency" style="width:200px; height:20px; border:1px solid #ccc">
					 <input type="hidden" id="SalesBegin_angka_edit"  name="SalesBegin_angka_edit">
				 </td>
                 <td width="23" align="left">To</td> 	
				 <td width="671" height="35" align="left" valign="middle" colspan="2">
					 <input id="SalesEnd_edit" name="SalesEnd" class="currency" style="width:200px; height:20px; border:1px solid #ccc">
					  <input type="hidden" id="SalesEnd_angka_edit"  name="SalesEnd_angka_edit">
				 </td>
			</tr>
			
			<tr>
				<td width="124" height="25" align="left" valign="middle">QTY Bonus</td>	
                <td width="10" align="left">:</td>
                <td width="41" align="left">&nbsp;</td>					
				<td height="25" align="left" valign="middle" colspan="4"> 
				    <input id="QtyBonus_edit" name="QtyBonus" style="width:200px; height:20px; border:1px solid #ccc">
			    </td>
            </tr>    
		</table>
	</form>
</div>

<!-- Dialog Button -->
<div id="dialog-edit-qty-bonus">
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="save_edit_data()">Simpan</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dialog-edit-bonus-limit').dialog('close')">Batal</a>
</div>

<script>	
	function popup_qty_bonus_limit(){
		modal_open('QTY Bonus Limit','dialog-template-bonus-limit');		
	}	
	
	function formatRp(a,b,c,d,e){
	    e=function(f){return f.split('').reverse().join('')};b=e(parseInt(a,10).toString());for(c=0,d='';c<b.length;c++){d+=b[c];if((c+1)%3===0&&c!==(b.length-1)){d+='.';}}return'\t'+e(d)+',00'
	}
	
	function save_data(){
	    var Qty_bonus  = $('#QtyBonus').val();
        var SalesBegin_angka = $('#SalesBegin_angka').val();
        var SalesEnd_angka = $('#SalesEnd_angka').val();		
		
		$.ajax({
			type: "POST",
			url: "<?php echo base_url(); ?>setup/preference_bonus/proses_post_data_bonus_limit",
			data: $.param({ SalesBegin_angka:SalesBegin_angka, SalesEnd_angka:SalesEnd_angka, Qty_bonus:Qty_bonus }),
			cache: false,
			success: function(msg){
				$('#tt').datagrid('reload'); 
				$('#dialog-template-bonus-limit').dialog('close');
			}
		});
    }
	
	function save_edit_data(){
		var Qty_bonus_edit = $('#QtyBonus_edit').val();
        var SalesBegin_angka_edit = $('#SalesBegin_angka_edit').val();
        var SalesEnd_angka_edit = $('#SalesEnd_angka_edit').val();	
        var SalesBegin_edit = $('#SalesBegin_edit').val();
        var SalesEnd_edit = $('#SalesEnd_edit').val();				
		var BonusLimitID = $('#BonusLimitID').val();
		
		$.ajax({
			type: "POST",
			url: "<?php echo base_url(); ?>setup/preference_bonus/edit_data_bonus_limit",
			data: $.param({ Qty_bonus_edit:Qty_bonus_edit, SalesBegin_angka_edit:SalesBegin_angka_edit, SalesEnd_angka_edit:SalesEnd_angka_edit, BonusLimitID:BonusLimitID, SalesBegin_edit:SalesBegin_edit, SalesEnd_edit:SalesEnd_edit }),
			cache: false,
			success: function(msg){
				$('#tt').datagrid('reload'); 
				$('#dialog-edit-bonus-limit').dialog('close');
			}
		});
    }
	
	function toAngka(rp){return parseInt(rp.replace(/,.*|\D/g,''),10)}
	
	$(document).ready(function() {
		$('#SalesBegin').keyup(function() { 
			var SalesBegin = $(this).val();
			var SalesBegin_angka = toAngka(SalesBegin);
			if(SalesBegin_angka > 0){
			   $('#SalesBegin_angka').val(SalesBegin_angka);
			}
		});
		
		$('#SalesBegin_edit').keyup(function() { 
			var SalesBegin_edit = $(this).val();
			var SalesBegin_angka_edit = toAngka(SalesBegin_edit);
			if(SalesBegin_angka_edit > 0){
			   $('#SalesBegin_angka_edit').val(SalesBegin_angka_edit);
			}
		});
		
		$('#SalesEnd').keyup(function() { 
			var SalesEnd = $(this).val();
			var SalesEnd_angka = toAngka(SalesEnd);
			if(SalesEnd_angka > 0){
			   $('#SalesEnd_angka').val(SalesEnd_angka);
			}
		});
		
		$('#SalesEnd_edit').keyup(function() { 
			var SalesEnd_edit = $(this).val();
			var SalesEnd_angka_edit = toAngka(SalesEnd_edit);
			if(SalesEnd_angka_edit > 0){
			   $('#SalesEnd_angka_edit').val(SalesEnd_angka_edit);
			}
		});
	});
	
	function update(){
		var row = $('#tt').datagrid('getSelected');
		if(row){
			$('#dialog-edit-bonus-limit').dialog('open').dialog('setTitle','Edit Detail');
			$('#form_edit_bonus_limit').form('load',row);
		}
	}
	
	function delete_bonus_limit(){
		var id = $('#tt').datagrid('getSelected').BonusLimitID;
		if(id){
			$.ajax({
				type: "POST",
				url: "<?php echo base_url(); ?>setup/preference_bonus/delete_bonus_limit",
				data: $.param({ id:id }),
				cache: false,
				success: function(msg){
					$('#tt').datagrid('reload'); 
				}
			});
		}
	}
</script>