<?php
$must_login                = true ;
$require_path              = '../../../' ;
$require_sub               = '../../' ;
require( $require_sub.'header.php' ) ;

if ( $boolean_login ){
	$status   		       = '201' ;

	$search_query          = '' ;
    $search_query         .= " AND a.redeem_id = '".$array['id']."'" ;

    $query                 = "SELECT a.redeem_id, a.category_id, a.redeem_type, a.date_start, a.date_end, a.point, a.file, a.redeem_quantity, a.status, a.created_at, b.title, b.content FROM redeem a
                              LEFT JOIN redeem_translation b ON ( a.redeem_id = b.redeem_id )
                              WHERE a.deleted_at IS NULL " . $search_query ;
    $mysqli_query          = $mysqli->query( $query ) ;

    if ( $mysqli_query->num_rows > 0 ){
        $status            = '269' ;

        $row               = $mysqli_query->fetch_assoc() ;
        $point             = $row['point'] ;
        $custom_point      = $array['custom_point'] ;

        $select_category   = $mysqli->query( "SELECT category_mode, number_of_times FROM redeem_category
                                              WHERE deleted_at IS NULL AND status = 'active' AND category_id = '".$row['category_id']."' AND ( category_type = 'all' OR ( category_type = 'date' AND date_start <= '".TODAYDAY."' AND date_end >= '".TODAYDAY."' ) ) LIMIT 1" ) ;

        if ( $select_category->num_rows > 0 ){
            $status                   = '268' ;

            $row_category             = $select_category->fetch_assoc() ;
            $category_mode            = $row_category['category_mode'] ;
            $number_of_times          = $row_category['number_of_times'] ;

            $get_staffcategory        = $mysqli->query( "SELECT * FROM staff_redeem 
                                                         WHERE deleted_at IS NULL AND category_id = '".$row['category_id']."' AND staff_id = '".$staff_info['staff_id']."' AND created_at LIKE '%".date('Y-m', time())."%' AND status != 'rejected'" ) ;

            if ( $number_of_times     > $get_staffcategory->num_rows ){

                $status               = '248' ;

                $is_redeem            = 'no' ;
                $is_custom            = 'no' ;
                if ( $row['status']  == 'active' ){
                    if ( $row['redeem_type'] == 'all' || ( $row['redeem_type'] == 'date' && $row['date_start'] <= TODAYDATE && $row['date_end'] >= TODAYDATE ) ){
                        $is_redeem    = 'yes' ;
                        $is_custom    = ( $category_mode == 'custom' ? 'yes' : 'no' ) ;
                    }
                }

                if ( $is_redeem      == 'yes' ){
                    $status           = '243' ;

                    if ( $is_custom == 'no' || ( $is_custom == 'yes' && $custom_point > 0 ) ){
                        $status           = '246' ;

                        if ( $is_custom  == 'yes' ){
                            $point        = $custom_point ;
                        }

                        $get_staffredeem  = $mysqli->query( "SELECT * FROM staff_redeem 
                                                             WHERE deleted_at IS NULL AND redeem_id = '".$array['id']."' AND status != 'rejected'" ) ;
                        $total_redeem     = $get_staffredeem->num_rows ;
                        
                        if ( $row['redeem_quantity'] > $total_redeem ){
                            $status        = '249' ;

                            $balance       = getStaffPoint( $staff_info['staff_id'] ) ;

                            if ( $balance >= $point ){
                                $status    = '200' ;

                                $mysqli->query( "INSERT INTO staff_redeem
                                                 ( redeem_id, branch_id, category_id, staff_id, point, status ) VALUES
                                                 ( '".$array['id']."', '".$array['branch_id']."', '".$row['category_id']."', '".$staff_info['staff_id']."', '".$point."', 'pending' ) " ) ;

                                $view_id   = $mysqli->insert_id ;
                                $redeem_so = 'RD'.strPad( 6, $view_id ) ;

                                $remark    = 'Deduct point '.$point.' ('.$row['title'].') from redeem ' . $redeem_so ;
                                pointMovement( 'redeem', $view_id, 'exchange', 'normal', $staff_info['staff_id'], -( $point ), $remark ) ;

                                $mysqli->query( "UPDATE staff_redeem SET redeem_so = '".$redeem_so."' WHERE view_id = '".$view_id."'" ) ;

                            }

                        }
                    }
                }
            }
        }
    }
}

require( $require_sub.'footer.php' ) ;
?>