<?php
/* 
 * Author   : Ferry / 31-03-2015 / Preference for Advance Payment
 * Revisi   :
 */
?>

<div id="cc" class="easyui-layout" data-options="fit:true, border:false">
    <div data-options="region:'west',split:true, border:true" title="" style="width:230px; background-color:#F8F8F8;">
        <div title="" style="padding:10px;overflow:auto;" data-options="selected:true" >				
            <div class="preference_menu">
                <ul>
                    <!--<li><a href="<?php echo site_url('setup/preference_account'); ?>" style="border-top:none;"><img src="<?php echo base_url(); ?>assets/custom/icons/clipboard-invoice.png" alt=""/><span>P/L Account</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_services'); ?>"><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_advances'); ?>"><img src="<?php echo base_url(); ?>assets/custom/icons/calculator.png" alt=""/><span><b>Adv. Payment Account</b></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>Cash-Bon Account</span></a></li>
                    <li><a href="<?php echo site_url('setup/preference_others'); ?>"><img src="<?php echo base_url(); ?>assets/custom/icons/money-coin.png" alt=""/><span>Others 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_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:false,plain:true, border:false" style="background:#eee;">
        <div id="wrapptabs" name="" class="easyui-tabs">
            <!--Awal View Service Category-->
            <div id="div_advance_group" title="Advance Category" border="false" style="padding:1px 0 1px 0;background:#EEE;">
                <div style="float:left;padding-left:5px;margin-bottom:5px;width:1116px;height:520px;background:#FFF;border:#95B8E7 1px solid;">
                    <div style="overflow:hidden;padding:1px;">
                        <form id="frm_category"  method="post" class="preference_form">
                            <label>								
                                <div style="margin:0px 0px 10px">
                                    <table id="dtg_category" class="easyui-edatagrid" title="" iconCls="icon-save" rownumbers="true" toolbar="#toolbar_cat" fitColumns="true" singleSelect="true" style="width:1090px;height:431px"
                                        > 
                                        <thead>
                                            <tr>
                                                <th field="AdvCategoryName" width="200" data-options="align:'left',halign:'center'">Category Name</th>
                                                <th field="AdvCategoryDesc" width="355" data-options="align:'left',halign:'center'">Description</th>
                                                <th field="AdvCategoryID" width="55"data-options="align:'center',halign:'center'" hidden>Category ID</th>							
                                                <th field="AdvCategoryType" width="55"data-options="align:'center',halign:'center'" hidden>ModusType</th>							
                                            </tr>
                                        </thead>
                                    </table>

                                    <div id="toolbar_cat">
                                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="load_category_popup(1);">New </a>
                                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="load_category_popup(2);">Edit</a>
                                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="delete_category();">Delete </a>
                                    </div>                                    
                                </div>				
                            </label> <br />
                        </form>
                    </div>			
                </div>

                <div style="padding:5px;text-align:right;width:1110px;">
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-save" onclick="save_category();">Save</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton  form-button" iconCls="icon-cancel" onclick="">Cancel</a>
                </div>	
            </div>
            <!--Akhir View Service Category-->
            
            <!--View Advance Detail-->
            <div id="div_advance_group" title="Advance Detail" border="false" style="padding:1px 0 1px 0;background:#EEE;">
                <div style="float:left;padding-left:5px;margin-bottom:5px;width:1116px;height:526px;background:#FFF;border:#95B8E7 1px solid;">
                    <div style="overflow:hidden;padding:1px;">
                        <form id="frm_category"  method="post" class="preference_form">
                            <label>								
                                <div style="margin:0px 0px 10px">
                                    <table id="dtg_detail" class="easyui-edatagrid" title="" iconCls="icon-save" rownumbers="true" toolbar="#toolbar_det" fitColumns="true" singleSelect="true" style="width:1090px;height:431px"> 
                                        <thead>
                                            <tr>
                                                <th field="AdvDetailName" width="150" data-options="align:'left',halign:'center'">Detail Name</th>
                                                <th field="AdvDetailDesc" width="200" data-options="align:'left',halign:'center'">Description</th>
                                                <th field="AdvLenghtOfDue" width="120" data-options="align:'center',halign:'center'">Length of Payment</th>                                                
                                                <th field="AdvNetDueIn" width="100"data-options="align:'center',halign:'center'" hidden>Net Due In</th>							
                                                <th field="AdvDetailID" width="55"data-options="align:'center',halign:'center'" hidden>Detail ID</th>							
                                                <th field="AdvDetailType" width="55"data-options="align:'center',halign:'center'" hidden>ModusType</th>	
                                                <th field="AdvCategoryID" width="55"data-options="align:'center',halign:'center'" hidden>Category ID</th>	
                                                <th field="AdvCategoryName" width="155"data-options="align:'left',halign:'center'" >Category</th>							
                                            </tr>
                                        </thead>
                                    </table>

                                   <div id="toolbar_det">
                                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="load_detail_popup(1);">New </a>
                                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="load_detail_popup(2);">Edit</a>
                                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="delete_detail();">Delete </a>
                                    </div>                                  
                                </div>				
                            </label> <br />
                        </form>
                    </div>			
                </div>

                <div style="padding:5px;text-align:right;width:1110px;">
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-save" onclick="save_data_detail();">Save</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton  form-button" iconCls="icon-cancel" onclick="">Cancel</a>
                </div>	
            </div>
            
            
            <!--Akhir Tab Advanced-->
            
            <!--Awal View Service Detail-->
            <div id="div_advance_detail" title="Advance Account" style="padding:1px 0 1px 0;background:#EEE;">
                <div style="float:left;padding-left:5px;margin-bottom:5px;width:1116px;height:526px;background:#FFF;border:#95B8E7 1px solid;">
                    <div style="overflow:hidden;padding:1px;">
                        <form id="frm_detail" method="post" class="preference_form">
                            <table width="100%" cellspacing="1" cellpadding="1">
                                <tr>
                                    <td valign="middle"><b>Advance Group</b></td>
                                    
                                    <td width="650" height="15" align="left" valign="middle">
                                        <select id="cbo_category" name="cbo_category" class="easyui-combobox" style="width:650px;" required
                                            data-options = "
                                                valueField:'AdvCategoryID',
                                                textField:'AdvCategoryName',
                                                url:'<?php echo base_url(); ?>setup/preference_advances/ajax_advance_category'
                                            ">
                                        </select>
                                    </td>
                                </tr>
                            </table>	
							
                            <table width="100%" cellspacing="1" cellpadding="1">									
                                <tr>
                                    <td valign="middle"><b>Purchase Expense </b></td>
                                    
                                    <td width="650" height="15" align="left" valign="middle">
                                        <select id="cbo_expense_account" name="cbo_expense_account" class="easyui-combobox" style="width:650px;" required
                                            data-options = "
                                                valueField:'AccountNumber',
                                                textField:'AccountName',
                                                url:'<?php echo base_url(); ?>setup/preference_advances/ExpenseAccount'
                                            ">
                                        </select>
                                    </td>
                                </tr>

                                <tr>
                                    <td valign="middle"><b>Purchase Account</b></td>
                                    
                                    <td width="650" height="15" align="left" valign="middle">
                                        <select id="cbo_advance_account" name="cbo_advance_account" class="easyui-combobox" style="width:650px;" required
                                            data-options = "
                                                valueField:'AccountNumber',
                                                textField:'AccountName',
                                                url:'<?php echo base_url(); ?>setup/preference_advances/AdvanceAccount'
                                            ">
                                        </select>
                                    </td>
                                </tr>
                                
                                <tr>
                                    <td valign="middle"><b>Purchase Advance</b></td>
                                    
                                    <td width="650" height="15" align="left" valign="middle">
                                        <select id="cbo_uangmuka_account" name="cbo_uangmuka_account" class="easyui-combobox" style="width:650px;" required
                                            data-options = "
                                                valueField:'AccountNumber',
                                                textField:'AccountName',
                                                url:'<?php echo base_url(); ?>setup/preference_advances/UangMukaAccount'
                                            ">
                                        </select>
                                    </td>
                                </tr>
                                
                                <tr>
                                    <td valign="middle"><b>Invoice Discount</b></td>
                                    
                                    <td width="650" height="15" align="left" valign="middle">
                                        <select id="cbo_pinv_discount_account" name="cbo_pinv_discount_account" class="easyui-combobox" style="width:650px;" required
                                            data-options = "
                                                valueField:'AccountNumber',
                                                textField:'AccountName',
                                                url:'<?php echo base_url(); ?>setup/preference_advances/InvoiceDiscount'
                                            ">
                                        </select>
                                    </td>
                                </tr>
                                
                                <tr>
                                    <td valign="middle"><b>Payment Discount</b></td>
                                    
                                    <td width="650" height="15" align="left" valign="middle">
                                        <select id="cbo_ppay_discount_account" name="cbo_ppay_discount_account" class="easyui-combobox" style="width:650px;" required
                                            data-options = "
                                                valueField:'AccountNumber',
                                                textField:'AccountName',
                                                url:'<?php echo base_url(); ?>setup/preference_advances/PaymentDiscount'
                                            ">
                                        </select>
                                    </td>
                                </tr>
                                
                                <tr>
                                    <td valign="middle"><b>Unbilled (Owing)</b></td>
                                    
                                    <td width="650" height="15" align="left" valign="middle">
                                        <select id="cbo_unbilled_account" name="cbo_unbilled_account" class="easyui-combobox" style="width:650px;" required
                                            data-options = "
                                                valueField:'AccountNumber',
                                                textField:'AccountName',
                                                url:'<?php echo base_url(); ?>setup/preference_advances/UnbilledAccount'
                                            ">
                                        </select>
                                    </td>
                                </tr>
                                
                                <tr>
                                    <td valign="middle"><b>Realized Gained / Loss</b></td>
                                    
                                    <td width="650" height="15" align="left" valign="middle">
                                        <select id="cbo_realized_account" name="cbo_realized_account" class="easyui-combobox" style="width:650px;" required
                                            data-options = "
                                                valueField:'AccountNumber',
                                                textField:'AccountName',
                                                url:'<?php echo base_url(); ?>setup/preference_advances/RealizedAccount'
                                            ">
                                        </select>
                                    </td>
                                </tr>

                                <tr>
                                    <td valign="middle"><b>Unrealized Gained / Loss</b></td>
                                    
                                    <td width="650" height="15" align="left" valign="middle">
                                        <select id="cbo_unrealized_account" name="cbo_unrealized_account" class="easyui-combobox" style="width:650px;" required
                                            data-options = "
                                                valueField:'AccountNumber',
                                                textField:'AccountName',
                                                url:'<?php echo base_url(); ?>setup/preference_advances/UnRealizedAccount'
                                            ">
                                        </select>
                                    </td>
                                </tr>
                                <!--
                                	Tambahan baru sales taxable
                                	Tgl 15/01/2016
                                    fachri
                                -->
                                <tr>
                                    <td valign="middle"><b>Taxable Ppn 10</b></td>
                                    
                                    <td width="650" height="15" align="left" valign="middle">
                                        <select id="cbo_taxable_account" name="cbo_taxable_account" class="easyui-combobox" style="width:650px;" required
                                            data-options = "
                                                valueField:'AccountNumber',
                                                textField:'AccountName',
                                                url:'<?php echo base_url(); ?>setup/preference_advances/TaxablePPnPurchase'
                                            ">
                                        </select>
                                    </td>
                                </tr>
                                
                                <tr>
                                    <td valign="middle"><b>Purchase Freight Advance</b></td>
                                    
                                    <td width="650" height="15" align="left" valign="middle">
                                        <select id="cbo_freight_account" name="cbo_freight_account" class="easyui-combobox" style="width:650px;" required
                                            data-options = "
                                                valueField:'AccountNumber',
                                                textField:'AccountName',
                                                url:'<?php echo base_url(); ?>setup/preference_advances/FreightAccount'
                                            ">
                                        </select>
                                    </td>
                                </tr>
                                <!---------------------------------------End------------------------------------------------------>
                            </table>
                        </form>
                    </div>			
                </div>
                                
                <div style="padding:5px;text-align:right;width:1110px;">
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-save" onclick="save_detail();">Save</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-cancel" onclick="">Cancel</a>
                </div>	
            </div>
            <!--Akhir View Service Detail-->
        </div>		
    </div>
</div>

<!-- Batas Awal Dialog Service Detail Adding -->
<div id="dlg_detail_add" class="easyui-dialog" style="width:650px;height:250px;padding:10px;" closed="true" buttons="#dlg_detail_add_button" data-options="modal:true">
    <form id="frm_detail_add" method="post" novalidate>
        <input type="hidden" name="AdvDetailID" id="hdn_det_id_add" value="0"/>
        <input type="hidden" name="AdvCategoryID" id="AdvCategoryID_Add"/>
        <input type="hidden" name="AdvCategoryName" id="AdvCategoryName_Add"/>
        
        <table width="100%" cellpadding="2" cellspacing="0">
            <tr>
                <td width='150'><b>Category</b></td>
                <td width='10'>:</td>

                <td align="left">
                    <select id="cbo_category_group" name="cbo_category_group" class="easyui-combobox" style="width:440px;" required
                        data-options = "
                            valueField:'AdvCategoryID',
                            textField:'AdvCategoryName',
                            url:'<?php echo base_url(); ?>setup/preference_advances/ajax_advance_category'
                        ">
                    </select>
                </td>
            </tr>
            <tr>
                <td width='150'><b>Item Name</b></td>
                <td width='10'>:</td>

                <td align="left">
                    <input id="txt_det_name_add" name="txt_det_name_add" placeholder="< Item Expense Detail Name >" style="width:440px;height:20px; border:1px solid #ccc;" MAXLENGTH="30">
                </td>
            </tr>

            <tr>
                <td valign="top"><b>Description</b></td>
                <td valign="top">:</td>
                
                <td align="left">
                    <textarea id="txa_det_desc_add" name="txa_det_desc_add" placeholder="< Item Advance Detail Description >" rows="4" style="resize:none;width:436px;white-space:normal;"></textarea>
                </td>
            </tr>

            <tr>
                <td valign="top"><b>Length of Payment</b></td>
                <td valign="top">:</td>
                
                <td align="left">
                    <input id="txt_det_year_add" name="txt_det_year_add" class="easyui-numberbox" value='0' style="text-align:right;width:40px;height:20px; border:1px solid #ccc;"> Years        
                    <a style="margin:0px 5px;"></a>
                    
                    <select name="cbo_det_month_add" id="cbo_det_month_add" class="easyui-combobox" editable="false" style="width:45px;resize:none;white-space:normal;" >
                        <option value='0'>0</option>
                        <option value='1'>1</option>
                        <option value='2'>2</option>
                        <option value='3'>3</option>
                        <option value='4'>4</option>
                        <option value='5'>5</option>
                        <option value='6'>6</option>
                        <option value='7'>7</option>
                        <option value='8'>8</option>
                        <option value='9'>9</option>
                        <option value='10'>10</option>
                        <option value='11'>11</option>
                        <option value='12'>12</option>
                    </select> Months
                </td>
            </tr>
        </table>
    </form>
</div>

<!-- Dialog Button -->
<div id="dlg_detail_add_button" style="border:none;">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="modify_detail_popup(1);">Add Item</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg_detail_add').dialog('close');">Cancel</a>
</div>
<!-- Batas Akhir Dialog Service Detail Adding -->

<!-- Batas Awal Dialog Service Detail Editing -->
<div id="dlg_detail_edit" class="easyui-dialog" style="width:650px;height:250px;padding:10px;" closed="true" buttons="#dlg_detail_edit_button" data-options="modal:true">
    <form id="frm_detail_edit" method="post" novalidate>
        <input type="hidden" name="AdvDetailID" id="hdn_det_id_edit"/>
        <input type="hidden" name="AdvDetailType" id="hdn_det_type_edit"/>
        <input type="hidden" name="AdvCategoryID" id="AdvCategoryID_Edit"/>
        <input type="hidden" name="AdvCategoryName" id="AdvCategoryName_Edit"/>
        
        <!--<input type="text" name="AdvNetDueIn" id="hdn_det_due_edit"/>-->

        <table width="100%" cellpadding="2" cellspacing="0">
            <tr>
                <td width='150'><b>Category</b></td>
                <td width='10'>:</td>

                <td align="left">
                    <select id="AdvCategoryID" name="AdvCategoryID" class="easyui-combobox" style="width:470px;" required
                        data-options = "
                            valueField:'AdvCategoryID',
                            textField:'AdvCategoryName',
                            url:'<?php echo base_url(); ?>setup/preference_advances/ajax_advance_category'
                        ">
                    </select>
                </td>
            </tr>
            <tr>
        	<td width="120"><b>Item Name</b></td>
                <td width="10">:</td>
                
                <td align="left">
                    <input id="txt_det_name_edit" name="AdvDetailName" placeholder="< Item Advance Detail Name >" style="width:470px; height:20px; border:1px solid #ccc" MAXLENGTH="30">
                </td>
            </tr>

            <tr>
        	<td valign="top"><b>Description</b></td>
                <td valign="top">:</td>

                <td align="left">
                    <textarea id="txa_det_desc_edit" name="AdvDetailDesc" placeholder="< Item Advance Detail Description >" rows="4" style="width:466px;resize:none;white-space:normal;"></textarea>
                </td>
            </tr>

            <tr>
                <td valign="top"><b>Length of Payment</b></td>
                <td valign="top">:</td>
                
                <td align="left">
                    <input id="txt_det_year_edit" name="txt_det_year_edit" class="easyui-numberbox" value='0' style="text-align:right;width:40px;height:20px; border:1px solid #ccc;"> Years        
                    <a style="margin:0px 5px;"></a>
                    
                    <select id="cbo_det_month_edit" name="cbo_det_month_edit" class="easyui-combobox" editable="false" style="width:45px;resize:none;white-space:normal;" >
                        <option value='0'>0</option>
                        <option value='1'>1</option>
                        <option value='2'>2</option>
                        <option value='3'>3</option>
                        <option value='4'>4</option>
                        <option value='5'>5</option>
                        <option value='6'>6</option>
                        <option value='7'>7</option>
                        <option value='8'>8</option>
                        <option value='9'>9</option>
                        <option value='10'>10</option>
                        <option value='11'>11</option>
                        <option value='12'>12</option>
                    </select> Months
                </td>
            </tr>
        </table>
    </form>
</div>

<!-- Dialog Service Detail Editing Button -->
<div id="dlg_detail_edit_button" style="border:none;">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="modify_detail_popup(2);">Edit Item</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg_detail_edit').dialog('close');">Cancel</a>
</div>
<!-- Batas Akhir Dialog Service Detail Editing -->

<!-- Batas Awal Dialog Services Category Adding -->
<div id="dlg_category_add" class="easyui-dialog" style="width:650px; height:190px; padding: 10px" closed="true" buttons="#dlg_category_add_button" data-options="modal:true">
    <form id="frm_category_add" method="post" novalidate>
        <input type="hidden" name="AdvCategoryID" id="hdn_cat_id_add" value="0"/>
        
        <table width="100%" cellpadding="2" cellspacing="0">
            <tr>
                <td width='120'><b>Category Name</b></td>
                <td width='10'>:</td>
                
                <td align="left">
                    <input id="txt_cat_name_add" name="txt_cat_name_add" placeholder="< Item Advance Category Name >" style="width:470px; height:20px; border:1px solid #ccc" MAXLENGTH="30" required>
                </td>
            </tr>

            <tr>
                <td width='120'><b>Description</b></td>
                <td width='10'>:</td>
                
                <td align="left">
                    <textarea id="txa_cat_desc_add" name="txa_cat_desc_add" placeholder="< Item Advance Category Description >" rows="4" style="resize:none;width:466px;white-space:normal;"></textarea>
                </td>
            </tr>            
        </table>
    </form>
</div>

<!-- Dialog Service Category Button -->
<div id="dlg_category_add_button" style="border:none;">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="modify_category_popup(1);">Add Item</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg_category_add').dialog('close');">Cancel</a>
</div>
<!-- Batas Akhir Dialog Service Catagory -->

<!-- Batas Awal Dialog Services Category Editing -->
<div id="dlg_category_edit" class="easyui-dialog" style="width:650px; height:190px; padding: 10px" closed="true" buttons="#dlg_category_edit_button" data-options="modal:true">
    <form id="frm_category_edit" method="post" novalidate>
        <input type="hidden" name="AdvCategoryID" id="hdn_cat_id_edit"/>
        <input type="hidden" name="AdvCategoryType" id="hdn_cat_type_edit"/>
        
        <table width="100%" cellpadding="2" cellspacing="0">
            <tr>
                <td width='120'><b>Category Name</b></td>
                <td width='10'>:</td>
                
                <td align="left">
                    <input id="txt_cat_name_edit" name="AdvCategoryName" placeholder="< Item Advance Category Name >" style="width:470px; height:20px; border:1px solid #ccc" MAXLENGTH="30" required>
                </td>
            </tr>

            <tr>
                <td width='120'><b>Description</b></td>
                <td width='10'>:</td>
                
                <td align="left">
                    <textarea id="txa_cat_desc_edit" name="AdvCategoryDesc" placeholder="< Item Advance Category Description >" rows="4" style="resize:none;width:466px;white-space:normal;"></textarea>
                </td>
            </tr>            
        </table>
    </form>
</div>

<!-- Dialog Service Category Editing Button -->
<div id="dlg_category_edit_button" style="border:none;">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="modify_category_popup(2);">Edit Item</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg_category_edit').dialog('close');">Cancel</a>
</div>
<!-- Batas Akhir Dialog Service Catagory editing -->

<script>
	$('#cbo_category_group').combobox({
        onSelect: function(rec){			
            $('#AdvCategoryID_Add').val(rec.AdvCategoryID);
			$('#AdvCategoryName_Add').val(rec.AdvCategoryName);
        }
    });
	
	$('#AdvCategoryID').combobox({
        onSelect: function(rec){			
            $('#AdvCategoryID_Edit').val(rec.AdvCategoryID);
			$('#AdvCategoryName_Edit').val(rec.AdvCategoryName);
        }
    });
	
	$(function(){
		var dg_group = $('#dtg_category').datagrid({remoteFilter:true,type:'post',url:'<?php echo base_url(); ?>setup/preference_advances/ajax_advance_category'});
		$('#dtg_category').datagrid('enableFilter');
		
		var dg_detail = $('#dtg_detail').datagrid({remoteFilter:true,type:'post',url:'<?php echo base_url(); ?>setup/preference_advances/ajax_advance_detail'});
		$('#dtg_detail').datagrid('enableFilter');
	});
	
    $('#cbo_category').combobox({
        onSelect: function(rec){			
            var category_id = rec.AdvCategoryID;
			
            if(category_id !== 0){
                $.ajax({
                    type    : "POST",
                    url     : "<?php echo base_url(); ?>setup/preference_advances/combo_list_expense_account",
                    data    : "category_id=" + category_id,
                    cache   : false,

                    success : function(result){
                        var result = eval('('+result+')');
                        $("#cbo_expense_account").combobox('select', result.AccountNumber);
                    }
                });

                $.ajax({
                    type    : "POST",
                    url     : "<?php echo base_url(); ?>setup/preference_advances/combo_list_advance_account",
                    data    : "category_id=" + category_id,
                    cache   : false,

                    success : function(result){
                        var result = eval('('+result+')');
                        $("#cbo_advance_account").combobox('select', result.AccountNumber);
                    }
                });
				
				$.ajax({
                    type    : "POST",
                    url     : "<?php echo base_url(); ?>setup/preference_advances/combo_list_uangmuka_account",
                    data    : "category_id=" + category_id,
                    cache   : false,

                    success : function(result){
                        var result = eval('('+result+')');
                        $("#cbo_uangmuka_account").combobox('select', result.AccountNumber);
                    }
                });
				
                $.ajax({
                    type    : "POST",
                    url     : "<?php echo base_url(); ?>setup/preference_advances/combo_list_pinv_discount_account",
                    data    : "category_id=" + category_id,
                    cache   : false,

                    success : function(result){
                        var result = eval('('+result+')');
                        $("#cbo_pinv_discount_account").combobox('select', result.AccountNumber);
                    }
                });

                $.ajax({
                    type    : "POST",
                    url     : "<?php echo base_url(); ?>setup/preference_advances/combo_list_ppay_discount_account",
                    data    : "category_id=" + category_id,
                    cache   : false,

                    success : function(result){
                        var result = eval('('+result+')');
                        $("#cbo_ppay_discount_account").combobox('select', result.AccountNumber);
                    }
                });

                $.ajax({
                    type    : "POST",
                    url     : "<?php echo base_url(); ?>setup/preference_advances/combo_list_unbilled_account",
                    data    : "category_id=" + category_id,
                    cache   : false,

                    success : function(result){
                        var result = eval('('+result+')');
                        $("#cbo_unbilled_account").combobox('select', result.AccountNumber);
                    }
                });

                $.ajax({
                    type    : "POST",
                    url     : "<?php echo base_url(); ?>setup/preference_advances/combo_list_realized_account",
                    data    : "category_id=" + category_id,
                    cache   : false,

                    success : function(result){
                        var result = eval('('+result+')');
                        $("#cbo_realized_account").combobox('select', result.AccountNumber);
                    }
                });

                $.ajax({
                    type    : "POST",
                    url     : "<?php echo base_url(); ?>setup/preference_advances/combo_list_unrealized_account",
                    data    : "category_id=" + category_id,
                    cache   : false,

                    success : function(result){
                        var result = eval('('+result+')');
                        $("#cbo_unrealized_account").combobox('select', result.AccountNumber);
                    }
                });
				
				/*
					Tambahan baru tgl 15/01/2016, fachri
				*/
				$.ajax({
                    type    : "POST",
                    url     : "<?php echo base_url(); ?>setup/preference_advances/combo_list_taxable",
                    data    : "category_id=" + category_id,
                    cache   : false,

                    success : function(result){
                        var result = eval('('+result+')');
                        $("#cbo_taxable_account").combobox('select', result.AccountNumber);
                    }
                });
				
				$.ajax({
                    type    : "POST",
                    url     : "<?php echo base_url(); ?>setup/preference_advances/combo_list_freight",
                    data    : "category_id=" + category_id,
                    cache   : false,

                    success : function(result){
                        var result = eval('('+result+')');
						$("#cbo_freight_account").combobox('select', result.AccountNumber);
                    }
                });
				/*==========================End======================================*/
            }
        }
    });

    function load_category_popup(modus_type){
        if(modus_type == 1){
            modal_open('New Item Advance Category', 'dlg_category_add');
        }
        else {
            var row = $('#dtg_category').datagrid('getSelected');

            if(row){
                $('#dlg_category_edit').dialog('open').dialog('setTitle', 'Edit Item Advance Category');
                $('#frm_category_edit').form('load',row);
            }
        }
    }

    function modify_category_popup(modus_type){   
        if(modus_type == 1){
            category_id     = $('#hdn_cat_id_add').val();
            category_name   = $('#txt_cat_name_add').val().trim();
            category_desc   = $('#txa_cat_desc_add').val();
            action_type     = modus_type;
        }
        else{
            category_id     = $('#hdn_cat_id_edit').val();
            category_name   = $('#txt_cat_name_edit').val().trim();
            category_desc   = $('#txa_cat_desc_edit').val();
            action_type     = $('#hdn_cat_type_edit').val();
        }

        if(category_name != ''){
            confirm_category(category_id, category_name, category_desc, modus_type, action_type);            
        }
        else{
            $.messager.alert('Warning','Item Advance Category Name tidak boleh kosong','error');
        }
    }

    function confirm_category(category_id, category_name, category_desc, modus_type, action_type){
        var url             = base_url + 'setup/preference_advances/check_category_name';
        
        var i               = $('#dtg_category').datagrid('getRows');
        var detail          = JSON.stringify(i);

        var selectedrow     = $('#dtg_category').datagrid('getSelected');
        var row_index       = $('#dtg_category').datagrid('getRowIndex', selectedrow);

        $.post(url, {detail:detail, category_id:category_id, category_name:category_name, modus_type:modus_type}, function(result){
            if (result.status == true){
                if(result.flag == true){
                    $.messager.alert('Warning', result.message, 'error');
                }
                else {
                    $.messager.confirm('Confirmation :', result.message, function(r){
                        if(r){
                            modify_category(row_index, category_name, category_desc, modus_type, action_type);
                        }
                    });
                }                    
            } 
            else {                
                modify_category(row_index, category_name, category_desc, modus_type, action_type);
            }
        },'json');            
    }

    function modify_category(row_index, category_name, category_desc, modus_type, action_type){
        if(modus_type == 1){
            $('#dtg_category').datagrid('appendRow',{
                AdvCategoryName : category_name,
                AdvCategoryDesc : category_desc,
                AdvCategoryType : modus_type,
                AdvCategoryID   : 0
            });

            $('#txt_cat_name_add').val('');
            $('#txa_cat_desc_add').val('');
            modal_close('dlg_category_add');
        }
        else{
            modus_type = action_type == 1 ? 1 : modus_type;
            
            $('#dtg_category').datagrid('updateRow',{
               index:row_index,
               row: {AdvCategoryName  : category_name,
                     AdvCategoryDesc  : category_desc,
                     AdvCategoryType  : modus_type}
            });

            $('#txt_cat_name_edit').val('');
            $('#txa_cat_desc_edit').val('');
            modal_close('dlg_category_edit');
        }
    }

    function delete_category(){        
        var url             = base_url + 'setup/preference_advances/delete_category';

        var selectedrow     = $('#dtg_category').datagrid('getSelected');
        var row_index       = $('#dtg_category').datagrid('getRowIndex', selectedrow);
        
        var adv_category_id = selectedrow.AdvCategoryID;

        $.messager.confirm('Confirmation :', 'Anda yakin, Anda ingin menghapus item "' + selectedrow.AdvCategoryName + '"',  
            function(r){
                if (r){
                    $('#dtg_category').datagrid('deleteRow', row_index);
                    
                    $.post(url, {adv_category_id:adv_category_id}, function(result){
                        if (result.status == true){
                            $.messager.alert('Success',result.message,'info',function(){
                                location.reload();						                    
                            });				
                        } 
                        else {
                            $.messager.alert('Failed',result.message,'error',function(){
                                location.reload();						                    
                            });				
                        }
                    },'json');
                }
            }
	);        
    }

    function load_detail_popup(modus_type){
        if(modus_type == 1){
			modal_open('New Item Advance Detail', 'dlg_detail_add');
		}
		else {
			var row = $('#dtg_detail').datagrid('getSelected');

			if(row){                    
				$('#txt_det_year_edit').val(parseInt(row.AdvNetDueIn / 12)) ;
				$('#cbo_det_month_edit').combobox('setValue', parseInt(row.AdvNetDueIn % 12)) ;
				
				$('#dlg_detail_edit').dialog('open').dialog('setTitle', 'Edit Item Advance Detail');
				$('#frm_detail_edit').form('load',row);
			}
		}
    }

    function modify_detail_popup(modus_type){
        if(modus_type == 1){
            length_payment = (parseFloat($('#txt_det_year_add').val()) * 12) + parseFloat($('#cbo_det_month_add').combobox('getValue'));

            detail_id       = $('#hdn_det_id_add').val();
            detail_name     = $('#txt_det_name_add').val().trim();
            detail_desc     = $('#txa_det_desc_add').val();
			group_name      = $('#AdvCategoryName_Add').val().trim();
			group_id      	= $('#AdvCategoryID_Add').val().trim();
            action_type     = modus_type;
        }
        else{
            length_payment = (parseFloat($('#txt_det_year_edit').val()) * 12) + parseFloat($('#cbo_det_month_edit').combobox('getValue'));

            detail_id       = $('#hdn_det_id_edit').val();
            detail_name     = $('#txt_det_name_edit').val().trim();
            detail_desc     = $('#txa_det_desc_edit').val();
			group_name      = $('#AdvCategoryName_Edit').val().trim();
			group_id      	= $('#AdvCategoryID_Edit').val().trim();
            action_type     = $('#hdn_det_type_edit').val();
        }

        if(detail_name != ''){
            confirm_detail(detail_id, detail_name, detail_desc, length_payment, modus_type, action_type,group_name, group_id);            
        }
        else{
            $.messager.alert('Warning','Item Advance Detail Name tidak boleh kosong','error');
        }
    }

    function confirm_detail(detail_id, detail_name, detail_desc, length_payment, modus_type, action_type, group_name, group_id){
        var url             = base_url + 'setup/preference_advances/check_detail_name';
        
        var i               = $('#dtg_detail').datagrid('getRows');
        var detail          = JSON.stringify(i);

        var selectedrow     = $('#dtg_detail').datagrid('getSelected');
        var row_index       = $('#dtg_detail').datagrid('getRowIndex', selectedrow);

        $.post(url, {detail:detail, detail_id:detail_id, detail_name:detail_name, modus_type:modus_type}, function(result){
            if (result.status == true){
                if(result.flag == true){
                    $.messager.alert('Warning', result.message, 'error');
                }
                else {
                    $.messager.confirm('Confirmation :', result.message, function(r){
                        if(r){
                            modify_detail(row_index, detail_name, detail_desc, length_payment, modus_type, action_type, group_name, group_id);
                        }
                    });
                }                    
            } 
            else {                
                modify_detail(row_index, detail_name, detail_desc, length_payment, modus_type, action_type,group_name, group_id);
            }
        },'json');            
    }
    
    function modify_detail(row_index, detail_name, detail_desc, length_payment, modus_type, action_type,group_name, group_id){
        length_due  = parseInt(length_payment / 12) + ' years ' + (length_payment % 12) + ' months ';
    
        if(modus_type == 1){
            $('#dtg_detail').datagrid('appendRow',{
                AdvDetailName       : detail_name,
                AdvDetailDesc       : detail_desc,
                AdvLenghtOfDue      : length_due,
                AdvNetDueIn         : length_payment,
                AdvDetailType       : modus_type,
				AdvCategoryName 	: group_name,
				AdvCategoryID 		: group_id,
                AdvDetailID         : 0
            });

            $('#txt_name_add').val('');
            $('#txa_desc_add').val('');
            modal_close('dlg_detail_add');
        }
        else{
            modus_type = action_type == 1 ? 1 : modus_type;

            $('#dtg_detail').datagrid('updateRow',{
                index   : row_index,
                row     : {AdvDetailName        : detail_name,
                           AdvDetailDesc        : detail_desc,
                           AdvLenghtOfDue       : length_due,
                           AdvNetDueIn          : length_payment,
						   AdvCategoryName 		: group_name,
						   AdvCategoryID 		: group_id,
                           AdvDetailType        : modus_type}
            }); 

            $('#txt_name_edit').val('');
            $('#txa_desc_edit').val('');
            modal_close('dlg_detail_edit');
        }
    }    
    
    function delete_detail(){        
        var url             = base_url + 'setup/preference_advances/delete_detail';

        var selectedrow     = $('#dtg_detail').datagrid('getSelected');
        var row_index       = $('#dtg_detail').datagrid('getRowIndex', selectedrow);
        
        var adv_detail_id   = selectedrow.AdvDetailID;

        $.messager.confirm('Confirmation :', 'Anda yakin, Anda ingin menghapus item "' + selectedrow.AdvDetailName + '"',  
            function(r){
                if (r){
                    $('#dtg_detail').datagrid('deleteRow', row_index);
                    
                    $.post(url, {adv_detail_id:adv_detail_id}, function(result){
                        if (result.status == true){
                            $.messager.alert('Success',result.message,'info',function(){
//                                location.reload();						                    
                            });				
                        } 
                        else {
                            $.messager.alert('Failed',result.message,'error',function(){
                                location.reload();						                    
                            });				
                        }
                    },'json');
                }
            }
	);        
    }

    function save_category() {
        var url         = base_url + 'setup/preference_advances/modify_category';

        var i           = $('#dtg_category').datagrid('getRows');
        var detail      = JSON.stringify(i);
        
        $.post(url, {detail:detail}, function(result){
            if (result.status == true){
                $.messager.alert('Success',result.message,'info',function(){
                    location.reload();
                });
            } 
            else {                
                $.messager.alert('Error',result.message,'error',function(){
                    location.reload();
                });
            }
        },'json');            
    }
	
	function save_data_detail() {
        var url         = base_url + 'setup/preference_advances/modify_detail_data';

        var i           = $('#dtg_detail').datagrid('getRows');
        var detail      = JSON.stringify(i);
        
        $.post(url, {detail:detail}, function(result){
            if (result.status == true){
                $.messager.alert('Success',result.message,'info',function(){
                    location.reload();
                });
            } 
            else {                
                $.messager.alert('Error',result.message,'error',function(){
                    location.reload();
                });
            }
        },'json');            
    }
	
    function save_detail() {
        var url     = base_url + 'setup/preference_advances/modify_detail';

        var i       = $('#dtg_detail').datagrid('getRows');
        var detail  = JSON.stringify(i);
        
		var category_id         = $("#cbo_category").combobox('getValue');
        var expense_account     = $('#cbo_expense_account').combobox('getValue');
        var advance_account     = $('#cbo_advance_account').combobox('getValue');
		var uangmuka_account    = $('#cbo_uangmuka_account').combobox('getValue');
		
        var pinv_disc_account   = $('#cbo_pinv_discount_account').combobox('getValue');
        var ppay_disc_account   = $('#cbo_ppay_discount_account').combobox('getValue');
        var unbilled_account    = $('#cbo_unbilled_account').combobox('getValue');
        var realized_account    = $('#cbo_realized_account').combobox('getValue');
        var unrealized_account  = $('#cbo_unrealized_account').combobox('getValue');
		var taxableppn_account  = $('#cbo_taxable_account').combobox('getValue');//tambahan baru tgl 15/01/2015, fahri
		var freight_account  	= $('#cbo_freight_account').combobox('getValue');//tambahan baru tgl 27/01/2015, fahri

        if(expense_account == '' || pinv_disc_account == '' || ppay_disc_account == '' || unbilled_account == '' || realized_account == '' || unrealized_account == '' || taxableppn_account == '' || uangmuka_account == ''){
            $.messager.alert('Warning','Advance Detail Account tidak boleh kosong','error');
        }
        else{
            $.post(url, {category_id:category_id, detail:detail, expense_account:expense_account, advance_account:advance_account, pinv_disc_account:pinv_disc_account, 
                         ppay_disc_account:ppay_disc_account, unbilled_account:unbilled_account, realized_account:realized_account, unrealized_account :unrealized_account, taxableppn_account:taxableppn_account, freight_account:freight_account, uangmuka_account:uangmuka_account}, function(result){
                if (result.status == true){
                    $.messager.alert('Success',result.message,'info',function(){
                        location.reload();
                    });
                } 
                else {                
                    $.messager.alert('Error',result.message,'error',function(){
                        location.reload();
                    });
                }
            },'json');            
        }
    }
</script>