<!---- 
    Author  : Herpan
    Tanggal : 12-08-2014    
    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($group_user->ReversePrivillage == 1){
        $cek_autority_reverse  = "";
    }
    else{
        $cek_autority_reverse    = "display:none";
    }
    //-------------end----------------------------

    $trans_status = "";
    foreach($afiliasi as $row): 
        $trans_status = $row->status;
        $AppLevel2Status = $row->AppLevel2Status;
?>
<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%" height="100%" border="0">
            <tr>
                <td align="left">
                    <input type="checkbox" class="easyui-checkbox" name="chk_suspended" id="chk_suspended" style="margin-top:0px;margin-right:1px;" onClick="suspend(1)" <?=(!in_array($row->status,array(5,6))) ? 'disabled' : ''?> <?=(in_array($row->status,array(3,9))) ? 'checked' : ''?>  <?= $cek_autority_delete;?>/>
                    <span style="display:inline-block;margin-top:2px;<?=(($row->status==3)?'color:gray;':'color:black')?>">Suspend</span>
                    <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-currency" onclick="app_payment()" <?=(in_array($row->status,array(1)))?'':'disabled'?>>To Payment</a>
                    <a href="#" class="easyui-linkbutton" iconCls="icon-journal" plain="true" onclick="show_journal()" <?= $cek_autority_journal;?>>Journal</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-reload" plain="true" onclick="location.reload()">Refresh</a>
                    
                </td>
                 <td align="center" width="40%"><span id="approval_information">
                <?php
                if($row->status == 5){
                    echo get_status_approval_header($row->status);
                }
                ?>
                </span></td>
                <td height="25" align="right" valign="middle">
                    <span class="form-title" style="margin-left:7px;">
                        <?=$menu_label?>
                    </span>
                </td>
            </tr>
            </table>                
        </div>          
    </div>
    <!-- End of top view -->
    
    <div id="form-body" data-options="region:'center',border:false">
    <?php if($row->status==2):?>
        <div class="blanket"></div>
        <div class="paid-box"><b>Paid</b></div>
    <?php elseif($row->status==3): ?>
         <div class="blanket"></div>
         <div class="suspend-box"><b>Suspended</b></div>
    <?php elseif($row->status == 1): ?>
         <div class="blanket"></div>
         <div class="cancel-box"><b>On Progress</b></div>
    <?php endif;?>
        <div id="form-wrapper" class="easyui-panel" style="width:1180%;height:526px;padding:5px;" fit='true'>
        
            <form id="fm_piutang_afiliasi" name="fm_piutang_afiliasi" method="post">
                <input type="hidden" name="detail" id="detail" />
                <input type="hidden" id="id" name="id" value="<?= $row->AfiliasiNo;?>" />
                <input type="hidden" id="branch_code_hidden" name="branch_code_hidden" value="<?= $row->BranchCode;?>" />
                <input type="hidden" id="link_id" name="link_id" value="<?= $row->LinkNumber;?>" />
                <input type="hidden" id="credit_status" name="credit_status" value="<?= $row->CreditStatus;?>" />
                
                <input type="hidden" name="exsist_trans_status" id="exsist_trans_status" value="<?= $trans_status?>" />
                <input type="hidden" name="code_appmgrspv" id="code_appmgrspv" />
                <input type="hidden" name="code_appdir" id="code_appdir" />
                <input type="hidden" name="copy_suspend" id="copy_suspend" />
            
                <!-- Form Header -->
                <table width="100%" height="141" cellpadding="0" cellspacing="0">
                <tr>
                    <td width="50%" valign="top">
                        <table width="100%" cellpadding="0" cellspacing="1" style="margin-top:4px;">
                        <tr height="20">
                            <td width="150"><b>Transfer From</b></td>
                            <td width="10"><b>:</b></td>
                            <td>
                                <select name="cmb_fromcompany" id="cmb_fromcompany" <?php if(($trans_status == 3) || ($trans_status == 1) || ($trans_status == 2)){ echo "disabled"; } else if($AppLevel2Status !=1){ echo " enabled "; } else { echo"disabled"; } ?>
                                   editable='false' class="combo-grid"
                                    style="width:199px;" 
                                    data-options="
                                        value:'<?= $row->CompTransCode;?>',
                                        panelWidth:199,
                                        idField:'CompanyCode',
                                        textField:'CompanyName',
                                        url:'<?= base_url().$menu_link; ?>/company_by_login/<?= $this->session->userdata('company');?>',
                                        columns: [[
                                            {field:'CompanyCode',title:'Company Code',width:110,halign:'center'}, 
                                            {field:'CompanyName',title:'Company Name',width:190,halign:'center'}
                                        ]]
                                    " required readonly
                                >
                                </select>
                                
                                <select name="cmb_bankfrom" id="cmb_bankfrom" <?php if(($trans_status == 3) || ($trans_status == 1) || ($trans_status == 2)){ echo "disabled"; } else if($AppLevel2Status !=1){ echo " enabled "; } else { echo"disabled"; } ?>
                                    editable='true' class="combo-grid" 
                                    style="width:199px;"  
                                    data-options="
                                        value:'<?= $row->BankTransfer;?>',
                                        panelWidth:199,
                                        idField: 'AccountNumber',
                                        textField: 'AccountName',
                                        url:'<?= base_url().$menu_link; ?>/get_bank_bycompany/<?= $this->session->userdata('company');?>',
                                        columns: [[
                                            {field:'AccountNumber',title:'Account Number',width:110,halign:'center',hidden:true}, 
                                            {field:'AccountName',title:'Bank Name',width:190,halign:'center'}, 
                                            {field:'CurrencyCode',title:'Bank Currency',width:80,halign:'center',align:'center'}
                                        ]],
                                        fitColumns: true,
                                        onSelect:function(rowIndex, rowData){
                                            
                                            get_bank_balance(rowData.AccountNumber,rowData.CurrencyCode);
                                            $('.app-button').linkbutton('disable');
                                        }
                                    " required
                                >
                                </select>
                            </td>
                        </tr>
                        <tr height="20">
                            <td width="150"><b>Transfer To</b></td>
                            <td width="10"><b>:</b></td>
                            <td>
                                <select name="cmb_tocompany" id="cmb_tocompany" <?php if(($trans_status == 3) || ($trans_status == 1) || ($trans_status == 2)){ echo "disabled"; } else if($AppLevel2Status !=1){ echo " enabled "; } else { echo"disabled"; } ?>
                                    editable='true' class="combo-grid"
                                    style="width:199px;" 
                                    data-options="
                                        value:'<?= $row->CompRecpCode;?>',
                                        panelWidth:199,
                                        idField:'CompanyCode',
                                        textField:'CompanyName',
                                        url:'<?= base_url(); ?>list/piutang_afiliasi/company_not_by_login/<?= $this->session->userdata('company');?>',
                                        columns: [[
                                            {field:'CompanyCode',title:'Company Code',width:110,halign:'center',hidden:true}, 
                                            {field:'CompanyName',title:'Company Name',width:190,halign:'center'}
                                        ]],
                                        onSelect: function(indexRow,dataRow){
                                            var url = '<?= base_url(); ?>list/piutang_afiliasi/get_bank_bycompany/'+dataRow.CompanyCode;
                                            $('#cmb_bankto').combogrid('grid').datagrid({
                                                url:url
                                            });
                                            $('#cmb_bankto').combogrid('setValue', '');
                                            $('.app-button').linkbutton('disable');
                                        }
                                    " required
                                >
                                </select>
                                <select name="cmb_bankto" id="cmb_bankto" <?php if(($trans_status == 3) || ($trans_status == 1) || ($trans_status == 2)){ echo "disabled"; } else if($AppLevel2Status !=1){ echo " enabled "; } else { echo"disabled"; } ?>
                                    editable='true' class="combo-grid"
                                    style="width:199px;" 
                                    data-options="
                                        value:'<?= $row->BankReceipt;?>',
                                        panelWidth:199,
                                        idField: 'AccountNumber',
                                        textField: 'AccountName',
                                        url:'<?= base_url(); ?>list/piutang_afiliasi/get_bank_bycompany/<?= $row->ToCompanyCode;?>',
                                        columns: [[
                                            {field:'AccountNumber',title:'Account Number',width:110,halign:'center',hidden:true}, 
                                            {field:'AccountName',title:'Bank Name',width:190,halign:'center'}, 
                                            {field:'CurrencyCode',title:'Bank Currency',width:80,halign:'center',align:'center'}
                                        ]],
                                        fitColumns: true,
                                        onSelect:function(){
                                            $('.app-button').linkbutton('disable');
                                        }
                                    " required 
                                >
                                </select>
                            </td>
                        </tr>
                        <tr height="20">
                            <td width="150"><b><?php echo label_grid('check_no');?></b></td>
                            <td width="10"><b>:</b></td>
                            <td>
                                <input name="txt_chequeno" id="txt_chequeno" type="text" value="<?= $row->ChequeNumber;?>" style="width:197px" <?php if(($trans_status == 3) || ($trans_status == 1) || ($trans_status == 2)){ echo "disabled"; } else if($AppLevel2Status !=1){ echo " enabled "; } else { echo"disabled"; } ?>/>
                            </td>
                        </tr>
                        <!--<tr height="20">
                            <td width="150"><b>Date</b></td>
                            <td width="10"><b>:</b></td>
                            <td>
                                <input name="txt_cheque_date" id="txt_cheque_date" type="text" class="easyui-datebox" style="width:180px;height:22px;" editable="false" <?php if($trans_status == 3 || $trans_status == 1 || $trans_status == 2){ echo "disabled"; } else if($this->session->userdata("jobtitle_status") == 0  &&  $AppLevel2Status !=1){ echo " enabled "; } else { echo"disabled"; } ?>/>
                            </td>
                        </tr>-->
                        <tr height="20">
                            <td width="150"><b>Memo</b></td>
                            <td width="10"><b>:</b></td>
                            <td>
                                <textarea class="textbox" name="txa_memo" id="txa_memo" rows="4" style="width:400px;resize:none;white-space:normal;" <?php if(($trans_status == 3) || ($trans_status == 1) || ($trans_status == 2)){ echo "disabled"; } else if($AppLevel2Status !=1){ echo " enabled "; } else { echo"disabled"; } ?>><?= $row->ADebtDesc;?></textarea>
                            </td>
                        </tr>
                        <tr height="20">
                            <td width="150"><b>Amount (IDR)</b></td>
                            <td width="10"><b>:</b></td>
                            <td>
                                <input name="txt_amount" id="txt_amount" type="" class="easyui-numberbox" data-options="decimalSeparator:',',groupSeparator:'.',precision:'2'" onkeyup="duplicate_amount()" 
                                value="<?= $row->ADebtAmount;?>" style="width:400px;height:20px;text-align:right" required="required" <?php if(($trans_status == 3) || ($trans_status == 1)){ echo "disabled"; } else if($AppLevel2Status !=1){ echo " enabled "; } else { echo"disabled"; } ?>/>
                                <input type="hidden" id="txt_amount_hidden" name="txt_amount_hidden" value="<?= $row->ADebtAmount;?>" />
                            </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:400px" readonly />
                            </td>
                        </tr>
                        </table>
                    </td>
                    <td width="50%" valign="top">
                        <table width="100%" cellpadding="0" cellspacing="1" style="margin-top:4px;">
                        <tr height="20">
                            <td rowspan="7" valign="top">&nbsp;</td>
                            <td>&nbsp;</td>
                            <td width="190" style="background:#333399;text-align:center;color:#FFF;">Piutang Afiliasi No.</td>
                            <td width="122" style="background:#333399;text-align:center;color:#FFF;">Piutang Afiliasi Date</td>
                        </tr>
                        <tr height="20">
                            <td>&nbsp;</td>
                            <td><input type="text" name="afiliasi_no" id="afiliasi_no" class="easyui-validatebox" value="<?= $row->AfiliasiNo;?>" style="width:190px;background-color:#E3E2E2;" required readonly /></td>
                            <td><input name="txt_afiliasi_date" id="txt_afiliasi_date" type="text" class="easyui-datebox" style="width:122px;height:22px" required editable="false" readonly/></td>
                        </tr>
                        </table>
                    </td>
                </tr>
                </table>
                <!-- End of form header -->
            </form>
        </div>
        
        <div id="form-footer">
            
        </div>
    </div>
    <div id="form-footer" data-options="region:'south',border:false">
        <table cellpadding="0" cellspacing="0" width="100%" height="100%" border="0">
             <tr>
               <td align="left">
               </td>
                <td align="right">                              
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-help" onclick="" plain="true">Help</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button app-button" iconCls="icon-ok" onclick="app_dir()" <?= (($this->session->userdata("jobtitle_status") != 0 || !in_array($row->status, array(5,6))) || (date('Y-m-d') < date('Y-m-d', strtotime($row->ADebtDate))))?'disabled':'' ?>>App. Dir</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button app-button" iconCls="icon-reload" onclick="app_reverse()" <?=(in_array($row->status,array(1,2,9)))?'':'disabled'?> style="<?= $cek_autority_reverse;?>">Reverse</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button save-button" iconCls="icon-save" id="" onclick="update_afiliasi()" <?=(in_array($row->status,array(0,1,2,3)))?'disabled':''?> <?= $cek_autority_save;?>>Save</a>
                    <!-- <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-print" onclick="parent.tab_edit('Print Piutang Afiliasi', '<?=$menu_link?>', 'icon-print', 'to_preview_by_afiliasi_number','<?= $row->AfiliasiNo;?>')" >Preview & Print</a>  -->  
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-print" onclick="parent.tab_edit('Print Piutang Afiliasi', '<?=$menu_link?>', 'icon-print', 'to_print_detail','<?= $row->AfiliasiNo;?>');" <?=(in_array($row->status,array(4,5,6,1,2,3,9)))?'':'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\')'?>" <?=(in_array($trans_status,array(3,9)) || $AppLevel2Status==1)?'disabled':''?>>Cancel</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\')'?>" <?=(!in_array($row->status,array(0,5,6)))?'disabled':''?>><?=($this->session->userdata('jobtitle_status')==0)?'Cancel':'Close'?></a>
                </td>
             </tr>
        </table>                
    </div>  
</div>

<script type="text/javascript"> 
var SavePrivillage  = '<?=$group_user->SavePrivillage;?>';
$(document).ready(function(){
    
    $('#txt_afiliasi_date').datebox('setValue', '<?= date("d-m-Y", strtotime($row->ADebtDate));?>');
    $('#txt_cheque_date').datebox('setValue', '<?= date("d-m-Y", strtotime($row->ChequeDate));?>');
    
    $('#chk_suspend').click(function(){
        if($(this).is(':checked')){
            $('#copy_suspend').val('1');
        }
        else{
            $('#copy_suspend').val('');
        }
    });

    var bank_transfer = '<?= $row->BankTransfer ?>';
    get_bank_balance(bank_transfer,'IDR');
    
    $('#save').click(function(){
        if($('#cmb_bankto').combogrid('getValue') == $('#cmb_bankfrom').combogrid('getValue')){
            $.messager.alert('Warning','BANK Transfer From tidak boleh sama dengan BANK Transfer To atau tidak boleh kosong');
            return false;
        }
    });
});
    
function show_journal(){
    var id          = $('#afiliasi_no').val();
    parent.tab_edit('Journal Piutang Afiliasi', '<?=$menu_link?>', 'icon-journal', 'to_journal', id);
}

function success(data){
    alert(JSON.stringify(data));
}

function app_dir(){

    // var jobtitle_code   = "<?= $this->session->userdata('jobtitle');?>";
    // var jobtitle_status = "<?= $this->session->userdata('jobtitle_status');?>";  

    // if((jobtitle_code == 'DIR') && (jobtitle_status == 0)){
    //     $('#code_appdir').val('5');
    //     approved_dir('<?=$menu_link?>','#fm_piutang_afiliasi');
    // }else{
    //     $.messager.alert('Warning','Approval hanya bisa dilakukan oleh Directur.');
    //     return false;
    // }

    var no_transaksi    = $('#afiliasi_no').val();

    checking_approval(no_transaksi,function(check){
        
        if(check.status == false){
             $('#code_appdir').val('5');
             approved_dir('<?=$menu_link?>','#fm_piutang_afiliasi');
        }else{
            $.messager.alert('Unseccessfull',check.message,'error',function(){
                location.reload();
            });
        }
    });
      
}

function update_afiliasi(){

    var login_as        = "<?= $this->session->userdata('username');?>";
    var created_by      = "<?= $row->CreatedBy;?>";
    var depart_by       = "<?= $row->DepartmentCode;?>";
    var depart_ses      = "<?= $this->session->userdata('department');?>";
    var jobtitle_status = "<?= $this->session->userdata('jobtitle_status');?>";


    var no_transaksi    = $('#afiliasi_no').val();
    var status          = '<?= $row->status;?>';
    

    checking_update(no_transaksi,status,function(check){
        
        if(check.status == true){
            edit_data('<?=$menu_link?>','#fm_piutang_afiliasi',created_by,login_as,depart_by,depart_ses,jobtitle_status);
        }else{
            $.messager.alert('Unseccessfull',check.message,'error',function(){
                location.reload();
            });
        }
    });
}

function app_reverse(){

    var no_transaksi    = $('#afiliasi_no').val();

    checking_reverse(no_transaksi,function(check){
        
        if(check.status == true){
            reversed_data('F','<?=$menu_link?>', $('#link_id').val(), 'Piutang Afiliasi');
        }else{
           $.messager.alert('Unseccessfull',check.message,'error',function(){
                location.reload();
            });
        }
    });
}

function duplicate_amount(){
    var pay_amount      = $('#txt_amount').val()
    var pay_to_angka    = toAngka(pay_amount);
    $('#txt_amount_hidden').val(pay_to_angka);
}

function suspend(type){

    var no_transaksi    = $('#afiliasi_no').val();

    checking_suspend(no_transaksi,function(check){
        
        if(check.status == true){
           suspend_data_new('F',type,'<?=$menu_link?>',$('#link_id').val(),'Piutang Afiliasi');
        }else{
            $.messager.alert('Unseccessfull',check.message,'error',function(){
                location.reload();
            });
        }
    });
}

function app_payment(){

    var isLock          = '<?=$row->AdebtIsLock;?>';
    var no_transaksi    = $('#afiliasi_no').val();

    if(isLock == 0){
       parent.tab_add('New Payment Afiliasi','list/payment_afiliasi','icon-add','add/'+no_transaksi);
    }else{
        $.messager.alert('Unseccessfull','Piutang Afiliasi Lock. Tidak bisa melakukan transaksi payment.','error',function(){
            //location.reload();
        });
    }
    
}

</script>
<?php endforeach;?>