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

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

    $adjustment_id      = $array['adjustment_id'] ;

    if ( $array['adjustment_type'] != '' && $array['setting_adjustment_id'] != '' && $array['department_id'] != '' && $array['point'] != ''&& arrayCheck( $array['related_staffs'] ) ){
        $status         = '253' ;

        $point          = $array['point'] ;
        switch ( $array['adjustment_type'] ){
            case 'plus' :
                $point  = ( $point > 0 ? $point : -($point) ) ;
            break ;
            case 'minus' :
                $point  = ( $point > 0 ? -($point) : $point ) ;
            break ;
        }

        if ( $point > 0 || $point < 0 ){
            $status     = '203' ;

            $select_adjustment    = $mysqli->query( "SELECT a.adjustment_id, a.adjustment_group, a.adjustment_scenario, a.adjustment_times, b.title FROM setting_adjustment a
                                                     LEFT JOIN setting_adjustment_translation b ON ( a.adjustment_id = b.adjustment_id )
                                                     WHERE a.deleted_at IS NULL AND b.lang = 'en' AND a.adjustment_id = '".$array['setting_adjustment_id']."' 
                                                     LIMIT 1" ) ;
            if ( $select_adjustment->num_rows > 0 ){
                $status           = '271' ;

                $row_adjustment   = $select_adjustment->fetch_assoc() ;
                $adjustment_times = $row_adjustment['adjustment_times'] ;

                $list_adjustment  = [] ;
                $list_adjustment[]= $row_adjustment['adjustment_id'] ;
                if ( $row_adjustment['adjustment_group'] > 0 ){
                    $select_group = $mysqli->query( "SELECT adjustment_id FROM setting_adjustment
                                                     WHERE deleted_at IS NULL AND adjustment_site = 'frontend' AND adjustment_group = '".$row_adjustment['adjustment_group']."'" ) ;
                    if ( $select_group->num_rows > 0 ){
                        while ( $row_group = $select_group->fetch_assoc() ){
                            $list_adjustment[] = $row_group['adjustment_id'] ;
                        }
                    }
                }

                $is_allow         = false ;
                switch ( $row_adjustment['adjustment_scenario'] ){
                    case 'none'     :
                        $is_allow = true ;
                    break ;
                    case 'daily'    :

                        $boolean_all      = true ;
                        foreach ( $array['related_staffs'] as $k => $v ){
                            $select_total = $mysqli->query( "SELECT point_id FROM staff_adjustment_point 
                                                             WHERE setting_adjustment_id IN ( ".implode( ', ', $list_adjustment )." ) AND staff_id = '".$v['id']."' AND created_at LIKE '%".date( 'Y-m-d', time() )."%'" ) ;
                            if ( $select_total->num_rows >= $adjustment_times ){
                                $boolean_all = false ;
                            }

                        }

                        if ( $boolean_all ){
                            $is_allow     = true ;
                        }

                    break ;
                    case '2daily'   :

                        $boolean_all      = true ;
                        foreach ( $array['related_staffs'] as $k => $v ){
                            $select_total = $mysqli->query( "SELECT point_id FROM staff_adjustment_point 
                                                             WHERE setting_adjustment_id IN ( ".implode( ', ', $list_adjustment )." ) AND staff_id = '".$v['id']."' AND ( created_at LIKE '%".date( 'Y-m-d', strtotime( "-1 days" ) )."%' OR created_at LIKE '%".date( 'Y-m-d', time() )."%' )" ) ;
                            if ( $select_total->num_rows >= $adjustment_times ){
                                $boolean_all = false ;
                            }
                        }

                        if ( $boolean_all ){
                            $is_allow     = true ;
                        }

                    break ;
                    case 'weekly'   :

                        $boolean_all      = true ;
                        foreach ( $array['related_staffs'] as $k => $v ){
                            $select_total = $mysqli->query( "SELECT point_id FROM staff_adjustment_point 
                                                             WHERE setting_adjustment_id IN ( ".implode( ', ', $list_adjustment )." ) AND staff_id = '".$v['id']."' AND created_at BETWEEN '".date( 'Y-m-d', strtotime('monday this week') )." 00:00:00' AND '".date( 'Y-m-d', strtotime('sunday this week') )." 23:59:59'" ) ;
                            if ( $select_total->num_rows >= $adjustment_times ){
                                $boolean_all = false ;
                            }
                        }

                        if ( $boolean_all ){
                            $is_allow     = true ;
                        }

                    break ;
                    case 'monthly'  :

                        $boolean_all      = true ;
                        foreach ( $array['related_staffs'] as $k => $v ){
                            $select_total = $mysqli->query( "SELECT point_id FROM staff_adjustment_point 
                                                             WHERE setting_adjustment_id IN ( ".implode( ', ', $list_adjustment )." ) AND staff_id = '".$v['id']."' AND ( created_at LIKE '%".date( 'Y-m', time() )."%' )" ) ;
                            if ( $select_total->num_rows >= $adjustment_times ){
                                $boolean_all = false ;
                            }

                        }

                        if ( $boolean_all ){
                            $is_allow     = true ;
                        }

                    break ;
                    case 'quaterly' :

                        $array_quarter    = [
                            '1'           => [ '01', '02', '03' ],
                            '2'           => [ '04', '05', '06' ],
                            '3'           => [ '07', '08', '09' ],
                            '4'           => [ '10', '11', '12' ],
                        ] ;
                        $current_month    = date( "m", time() ) ;
                        $current_quarter  = ceil( $current_month / 3 ) ;
                        $get_quarter      = $array_quarter[$current_quarter] ;

                        $boolean_all      = true ;
                        foreach ( $array['related_staffs'] as $k => $v ){
                            $select_total = $mysqli->query( "SELECT point_id FROM staff_adjustment_point 
                                                             WHERE setting_adjustment_id IN ( ".implode( ', ', $list_adjustment )." ) AND staff_id = '".$v['id']."' AND ( created_at LIKE '%".date( 'Y-', time() ).$array_quarter[0]."%' OR created_at LIKE '%".date( 'Y-', time() ).$array_quarter[1]."%' OR created_at LIKE '%".date( 'Y-', time() ).$array_quarter[2]."%' )" ) ;
                            if ( $select_total->num_rows >= $adjustment_times ){
                                $boolean_all = false ;
                            }
                        }

                        if ( $boolean_all ){
                            $is_allow     = true ;
                        }
                    break ;
                    case 'annually' :

                        $boolean_all      = true ;
                        foreach ( $array['related_staffs'] as $k => $v ){
                            $select_total = $mysqli->query( "SELECT point_id FROM staff_adjustment_point 
                                                             WHERE setting_adjustment_id IN ( ".implode( ', ', $list_adjustment )." ) AND staff_id = '".$v['id']."' AND ( created_at LIKE '%".date( 'Y-', time() )."%' )" ) ;
                            if ( $select_total->num_rows >= $adjustment_times ){
                                $boolean_all = false ;
                            }

                        }

                        if ( $boolean_all ){
                            $is_allow     = true ;
                        }

                    break ;
                }

                if ( $is_allow ){

                    if ( $mysqli->query( "INSERT INTO staff_adjustment
                                          ( `adjustment_type`, `created_branch_id`, `created_by`, `department_id`, `setting_adjustment_id`, `point`, `remark` ) VALUES
                                          ( '".$array['adjustment_type']."', '".$array['branch_id']."', '".$staff_info['staff_id']."', '".$array['department_id']."', '".$array['setting_adjustment_id']."', '".$point."', '".$array['remark']."' )" ) ){
                        $status         = '200' ;


                        $adjustment_id  = $mysqli->insert_id ;
                        $adjustment_so  = 'AD'.strPad( 6, $adjustment_id ) ;
                        $mysqli->query( "UPDATE staff_adjustment SET adjustment_so = '".$adjustment_so."' WHERE adjustment_id = '".$adjustment_id."'" ) ;

                        // set point movement
                        // $remark         = $staff_info['staff_shortname'].' ('.$staff_info['staff_idno'].') '.( $point > 0 ? 'give' : 'deduct' ).' the point ('.$row_adjustment['title'].') from adjustment ( ' . $adjustment_so . ' )' ;
                        $remark         = 'You have been '.( $point > 0 ? 'given' : 'deducted' ).' the point ('.$row_adjustment['title'].') from adjustment (' . $adjustment_so . ')' ;

                        foreach ( $array['related_staffs'] as $k => $v ){
                            $mysqli->query( "INSERT INTO staff_adjustment_point ( `adjustment_id`, `setting_adjustment_id`, `staff_id`, `point` ) VALUES ( '".$adjustment_id."', '".$array['setting_adjustment_id']."', '".$v['id']."', '".$point."' )" ) ;

                            pointMovement( 'adjustment', $adjustment_id, $array['adjustment_type'], 'normal', $v['id'], $point, $remark ) ;
                            pushToUserCron( 'staff_adjustment', $adjustment_id, $v['id'], 'Point Adjustment', $remark ) ;
                        }

                    }
                }

            }

        }

    }
}

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