<?php


    $id = intval($_POST['id']);
    $type =intval($_POST['type']);
    $request = $db->get_rowData('deposit_request','id',$id);
    
    if(empty($request)){$error=fl();setMessage(63,'Deposit request');}
    elseif($type!=0&&$type!=1){$error=fl(); setMessage(63,'Action');}
    elseif($request['status']!=1){$error=fl();setMessage(1,'Already processed.');}
    else{
        if($type==1){
            $amount = $request['amount'];
            $note = 'MPO balance deposit '.$request['note'];
            $user=$db->userInfoByID($request['user_id']);
            $db->transactionStart();
            $user_head=$acc->get_user_head($user);
            if($user_head==false){$error=fl();setMessage(66);}
            $debit_ledger=$request['bank_id'];
            if(!isset($error)){
                $jArray[fl()]=1;
                $newVoucher=$acc->voucher_create(V_T_MPO_BALANCE_TRANSFER,$amount,$debit_ledger,$user_head,TIME,$note,$id,0,[],$jArray);
                if($newVoucher==false){$error=fl(); setMessage(66);}
            }
            if(!isset($error)){
                $data=['status'=>2,'action_by'=>USER_ID,'action_time'=>TIME];
                $where=['id'=>$request['id']];
                $update = $db->update('deposit_request',$data,$where,'array',$jArray);
                if(!$update){$error=fl();setMessage(66);}
            }
            $u = $db->userInfoByID($request['user_id']);
            $user_ledger=$acc->get_user_head($u,$jArray);
            $balance = $acc->headBalance($user_ledger);  //500
            // Fetch records from 'customer_amount_receive' where:
            // - Status is 0 (unprocessed records).
            // - The customer belongs to the current user's base (filtered by base_id).
            // - Results are ordered by 'id' in descending order.
            
            $customer_amount_receive=$db->selectAll('customer_amount_receive','where status=0 and mpo_id= '.$u['id'].' order by id desc','','array',$jArray);
            $jArray[fl()]=$customer_amount_receive;
            if(!empty($customer_amount_receive)){
                foreach($customer_amount_receive as $k=>$car){
                    //300  3
                    $check_balance=$balance-$car['amount'];
                    if($check_balance>=0){
                        unset($customer_amount_receive[$k]);
                        $balance-=$car['amount'];
                    }
                    else{break;}
                }
                foreach($customer_amount_receive as $k=>$car){
                    $action =$acc->confirmCustomerDeposit($car['id'],$jArray);
                    if($action['status']==0){
                        $error=fl();
                        setMessage(1,$action['message']);
                        break;
                    }
                }
            }


            $ac=false;
            if(!isset($error)){
                $ac=true;
                $jArray['status']=1;
                setMessage(2,'Request Accept');
            }
            $db->transactionStop($ac);
        }
        else{
            $data=['status'=>0,'action_by'=>USER_ID,'action_time'=>TIME];
            $where=['id'=>$request['id']];
            $update = $db->update('deposit_request',$data,$where,'array',$jArray);
            if(!$update){$error=fl();setMessage(66);}
            else{
                $jArray['status']=1;
                setMessage(2,'Request Cancel');   
            }
        }
    }
    
