<?php
$q=['isActive=1'];
if($userData['base_id']>0){
    $q[]="base_id=$userData[base_id]";
}
    $customer=$db->selectAll('customer','where '.implode(' and ',$q).'  order by name asc','id,name,code');
    if(!empty($customer)){
        foreach($customer as $k=>$p){
            $customer[$k]['name']=$p['code'].' '.$p['name'];
        }
    }

?>
<section class="mb-4">
       <div class="card">
            <div class="card-header text-center py-3"> 
                <h5 class="mb-0 text-center">
                    <strong>Receive from customer</strong>
                </h5>
            </div>
    <?php
        $general->inputBoxSelect($customer,'Customer','customer_id','id','name','','y','','','Select Custoemr');
        $general->inputBoxText('amount','Amount','','','amount_td');
        $general->inputBoxTextArea('note','Note');
    ?>
    <button onclick="transaction_with_customer()"  class="form-control btn btn-success transaction_with_customer">SUBMIT</button>

</section>
<script type="text/javascript">
    function transaction_with_customer(){
        buttonLoading('transaction_with_customer');
        let amount =parse_float($('#amount').val()) ;
        let customer_id =parse_int($('#customer_id').val()) ;
        let note =$('#note').val() ;
        let error=0;
        if(customer_id<1){error=1; swMessage('Select a customer.');}
        else if(amount<=0){error=1; swMessage('Invalid Amount.');}
            else{
                $.ajax({
                    type:'post',
                    url:ajUrl,
                    data:{module:'transaction_with_customer',customer_id:customer_id,amount:amount,note},
                    success:function(data){
                        button_loading_destroy('transaction_with_customer','SUBMIT');
                        if(typeof(data.status)  !== "undefined"){ 
                            if(data.status==1){
                                $('#customer_id').val('');
                                $('#amount').val('');
                                $('#note').val('');
                                select2Call();
                            }
                            swMessageFromJs(data.m);
                        }
                        else{
                            swMessage(AJAX_ERROR_MESSAGE); 
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        console.log('error') ;
                        button_loading_destroy('transaction_with_customer','SUBMIT');
                        swMessage(AJAX_ERROR_MESSAGE); 
                    }
                }); 
            }
        if(error==1){
            button_loading_destroy('transaction_with_customer','SUBMIT');
        }
    }
    </script>

