<?php 
ob_start();
session_start();
include "../config/config.php";
$session_id = $_SESSION['spm_gold']['id'];
$today_date = date('Y-m-d');
$updated_at = date("Y-m-d H:i:s");





if(isset($_POST['type']) && $_POST['type']=='create_expenses'){
    
     try{
         
        $pdo->beginTransaction(); 
        
        
        $sale_id = (isset($_POST['sale_id'])) ? $_POST['sale_id'] : "";
        $payment_date = (isset($_POST['payment_date'])) ? $_POST['payment_date'] : "";
        $location_id = (isset($_POST['location_id'])) ? $_POST['location_id'] : "";
        
        $type = "Debit";
        $accounts = 5;
        
        
        
        
        for($i=0; $i<count($_POST['expenses_name']); $i++){
            
            $expenses_name = $_POST['expenses_name'][$i];
            $date = $_POST['date'][$i];
            $exp_category = $_POST['exp_category'][$i];
            $paid_amount = $_POST['paid_amount'][$i];
            $payment_type = $_POST['payment_type'][$i];
            $reference = $_POST['reference'][$i];
            $notes = $_POST['notes'][$i];
            
            
            if($paid_amount!=0){
                
                
                $statement = $pdo->prepare("INSERT INTO `tbl_expenses`(`category`, `name`, `date`, `amount`, `payment_type`, `reference`, `notes`, `created_by`, `location_id`) VALUES (?,?,?,?,?,?,?,?,?)");
                $result = $statement->execute(array($exp_category, $expenses_name, $date, $paid_amount, $payment_type, $reference, $notes, $session_id, $location_id)); 
                
                $expenses_id = $pdo->lastInsertId();  
                
                
                
                        
                $statement = $pdo->prepare("INSERT INTO `tbl_payments` (`date`, `type`, `expense_id`, `accounts`, `payment_type`, 
                           `amount`, `reference_no`, `notes`, `created_date`, `created_by`) VALUES (?,?,?,?,?,
                              ?,?,?,?,?)");
                $result = $statement->execute(array($date, $type, $expenses_id, $accounts, $payment_type,
                       $paid_amount, $reference, $notes, $today_date, $session_id)); 
                        
            }else{
                        
            }

            
            
            
        }
        
        

        
   
        

            
                        
            
            
           if($result){
               
               
                
               
                $pdo->commit();
                
                $return_arr['message']='Expenses Added Successfully...';
                $return_arr['status']=200;
           }else{
                $return_arr['message']='Something Went Wrong..';
                $return_arr['status']=400;
            }
            
            
             
     }catch(PDOException $e){
         $pdo->rollBack();
        $return_arr['message']="$e";
        $return_arr['status']=500;
        }   
    echo json_encode($return_arr);
}



if(isset($_POST['type']) && $_POST['type']=='create_exp_category'){
     try{
         
         $pdo->beginTransaction(); 
         
        // Details Here
        $name = $_POST['name'];
        $status = $_POST['status'];
        

        $statement = $pdo->prepare("INSERT INTO `tbl_exp_category`(`name`, `status`) VALUES (?,?)");
        $result = $statement->execute(array($name,  $status));
         
        if($result){    
            $pdo->commit();
            $return_arr['message']='Expenses Category Added Successfully...';
            $return_arr['status']=200;
        } 
        else{
            $return_arr['message']='Something Went Wrong..';
            $return_arr['status']=400;
        }
    }catch(PDOException $e){
        $pdo->rollBack();
        $return_arr['message']="$e";
        $return_arr['status']=500;
    }   
    echo json_encode($return_arr);
}


if(isset($_POST['type']) && $_POST['type']=='update_exp_category'){
     try{
         
         $pdo->beginTransaction(); 
         
        // Details Here
        $ids = $_POST['ids'];
        $name = $_POST['name'];
        $status = $_POST['status'];
        

         
        $statement = $pdo->prepare("UPDATE `tbl_exp_category` SET `name`=?, `status`=? WHERE id=?");
        $result = $statement->execute(array($name, $status, $ids));
         
        if($result){    
            $pdo->commit();
            $return_arr['message']='Expenses Category Updated Successfully...';
            $return_arr['status']=200;
        } 
        else{
            $return_arr['message']='Something Went Wrong..';
            $return_arr['status']=400;
        }
    }catch(PDOException $e){
        $pdo->rollBack();
        $return_arr['message']="$e";
        $return_arr['status']=500;
    }   
    echo json_encode($return_arr);
}


?>