update API handle ticket
This commit is contained in:
		
							parent
							
								
									aec80f2e94
								
							
						
					
					
						commit
						ed32b03e5b
					
				| 
						 | 
				
			
			@ -55,7 +55,6 @@ class TicketController extends Controller
 | 
			
		|||
        );
 | 
			
		||||
 | 
			
		||||
        return response()->json($responseData);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getAll(Request $request)
 | 
			
		||||
| 
						 | 
				
			
			@ -90,17 +89,18 @@ class TicketController extends Controller
 | 
			
		|||
        );
 | 
			
		||||
 | 
			
		||||
        return response()->json($responseData);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function createTicket(Request $request){
 | 
			
		||||
    public function createTicket(Request $request)
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        // Define validation rules
 | 
			
		||||
        $rules = [
 | 
			
		||||
            'start_date' => 'required|date',
 | 
			
		||||
            'start_period' => 'required|string', // Adjust the validation rule as per your requirements
 | 
			
		||||
            'start_period' => 'required|string',
 | 
			
		||||
            'end_date' => 'required|date|after_or_equal:start_date',
 | 
			
		||||
            'end_period' => 'required|string', // Adjust the validation rule as per your requirements
 | 
			
		||||
            'end_period' => 'required|string',
 | 
			
		||||
            'type' => 'required|string',
 | 
			
		||||
        ];
 | 
			
		||||
 | 
			
		||||
        // Validate the request
 | 
			
		||||
| 
						 | 
				
			
			@ -112,6 +112,8 @@ class TicketController extends Controller
 | 
			
		|||
        $startPeriod = $request->input('start_period'); //The session begins
 | 
			
		||||
        $endDate = $request->input('end_date'); //End date
 | 
			
		||||
        $endPeriod = $request->input('end_period'); //Session ends
 | 
			
		||||
        $type = $request->input('type');
 | 
			
		||||
        $reason = $request->input('reason');
 | 
			
		||||
        $user = auth('admins')->user(); // user create ticket
 | 
			
		||||
 | 
			
		||||
        // return $user;
 | 
			
		||||
| 
						 | 
				
			
			@ -121,6 +123,9 @@ class TicketController extends Controller
 | 
			
		|||
            'start_period' => $startPeriod,
 | 
			
		||||
            'end_date' => Carbon::create($endDate)->setTimezone(env('TIME_ZONE')),
 | 
			
		||||
            'end_period' => $endPeriod,
 | 
			
		||||
            'type' => $type,
 | 
			
		||||
            'status' => 'WAITING',
 | 
			
		||||
            'reason' => $reason,
 | 
			
		||||
            'user_id' => $user->id
 | 
			
		||||
        ]);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -130,7 +135,8 @@ class TicketController extends Controller
 | 
			
		|||
        return response()->json(['data' => $ticket, 'status' => true]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function deleteTicket(Request $request){
 | 
			
		||||
    public function deleteTicket(Request $request)
 | 
			
		||||
    {
 | 
			
		||||
        $rules = [
 | 
			
		||||
            'ticket_id' => 'required'
 | 
			
		||||
        ];
 | 
			
		||||
| 
						 | 
				
			
			@ -154,11 +160,13 @@ class TicketController extends Controller
 | 
			
		|||
        return response()->json(['message' => 'Delete fail', 'status' => false]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function handleTicket(Request $request){
 | 
			
		||||
    public function handleTicket(Request $request)
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        $rules = [
 | 
			
		||||
            'ticket_id' => 'required',
 | 
			
		||||
            'action' => 'required'
 | 
			
		||||
            'action' => 'required',
 | 
			
		||||
            'admin_note' => 'required'
 | 
			
		||||
        ];
 | 
			
		||||
 | 
			
		||||
        // Validate the request
 | 
			
		||||
| 
						 | 
				
			
			@ -169,9 +177,12 @@ class TicketController extends Controller
 | 
			
		|||
        $action = $request->input('action'); // 'confirm' or 'refuse'
 | 
			
		||||
        $admin = auth('admins')->user();
 | 
			
		||||
        $ticket = Ticket::find($ticket_id);
 | 
			
		||||
 | 
			
		||||
        if (!$ticket || $ticket->status !== "WAITING") {
 | 
			
		||||
            return response()->json(['message' => "Ticket not found", 'status' => false]);
 | 
			
		||||
        }
 | 
			
		||||
        $results = $this->getAllPeriod($ticket->start_date, $ticket->start_period, $ticket->end_date, $ticket->end_period);
 | 
			
		||||
        
 | 
			
		||||
        return $results;
 | 
			
		||||
        // $admin->id != user_id of ticket ---> continue
 | 
			
		||||
        // Confirm
 | 
			
		||||
        // Add records to the notes table like function Timekeeping.addNoteForUser() based on the $results array
 | 
			
		||||
| 
						 | 
				
			
			@ -185,7 +196,37 @@ class TicketController extends Controller
 | 
			
		|||
 | 
			
		||||
        // Send notification email to users
 | 
			
		||||
 | 
			
		||||
    // false
 | 
			
		||||
        if ($action == "confirm") {
 | 
			
		||||
            foreach ($results as $result) {
 | 
			
		||||
                list($year, $month, $day) = explode('-', $result['date']);
 | 
			
		||||
                Notes::create([
 | 
			
		||||
                    'n_user_id' => $ticket->user_id,
 | 
			
		||||
                    'n_day' => $day,
 | 
			
		||||
                    'n_month' => $month,
 | 
			
		||||
                    'n_year' => $year,
 | 
			
		||||
                    'n_time_type' => $result['period'],
 | 
			
		||||
                    'n_reason' => $ticket->type,
 | 
			
		||||
                    'n_note' => $ticket->reason
 | 
			
		||||
                ]);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $ticket['updated_by'] = $admin->name;
 | 
			
		||||
            $ticket['admin_note'] = $admin_note;
 | 
			
		||||
            $ticket['status'] = 'CONFIRMED';
 | 
			
		||||
 | 
			
		||||
            $ticket->save();
 | 
			
		||||
            $this->createOrUpdateRecordForCurrentMonth($month, $year);
 | 
			
		||||
            return response()->json(['message' => "confirmed", 'status' => true]);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ($action == "refuse") {
 | 
			
		||||
            $ticket['updated_by'] = $admin->name;
 | 
			
		||||
            $ticket['admin_note'] = $admin_note;
 | 
			
		||||
            $ticket['status'] = 'REFUSED';
 | 
			
		||||
            return response()->json(['message' => "refused", 'status' => true]);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return response()->json(['message' => "failed", 'status' => false]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function getAllPeriod($startDate, $startPeriod, $endDate, $endPeriod)
 | 
			
		||||
| 
						 | 
				
			
			@ -196,28 +237,56 @@ class TicketController extends Controller
 | 
			
		|||
        //Use CarbonPeriod to create a period from the start date to the end date
 | 
			
		||||
        $period = CarbonPeriod::create($startDate, $endDate);
 | 
			
		||||
 | 
			
		||||
        $time_type = Category::where('c_type','TIME_TYPE');
 | 
			
		||||
        $time_type = Category::where('c_type', 'TIME_TYPE')->get();
 | 
			
		||||
        $morning = null;
 | 
			
		||||
        $afternoon = null;
 | 
			
		||||
        $all_day = null;
 | 
			
		||||
 | 
			
		||||
        foreach ($time_type as $item) {
 | 
			
		||||
            switch ($item->c_code) {
 | 
			
		||||
                case 'S':
 | 
			
		||||
                    $morning = $item;
 | 
			
		||||
                    break;
 | 
			
		||||
                case 'C':
 | 
			
		||||
                    $afternoon = $item;
 | 
			
		||||
                    break;
 | 
			
		||||
                case 'ALL':
 | 
			
		||||
                    $all_day = $item;
 | 
			
		||||
                    break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        foreach ($period as $date) {
 | 
			
		||||
            //If it is the start date
 | 
			
		||||
            if ($date->isSameDay($startDate)) {
 | 
			
		||||
                //Add start session
 | 
			
		||||
                $results[] = ['date' => $date->toDateString(), 'period' => $startPeriod];
 | 
			
		||||
                //If the start date is morning, add afternoon
 | 
			
		||||
                if ($startPeriod == 'morning') {
 | 
			
		||||
                    $results[] = ['date' => $date->toDateString(), 'period' => 'afternoon'];
 | 
			
		||||
                if ($startDate == $endDate) {
 | 
			
		||||
                    if ($startPeriod == $endPeriod) {
 | 
			
		||||
                        $results[] = ['date' => $date->toDateString(), 'period' => $startPeriod];
 | 
			
		||||
                    } else {
 | 
			
		||||
                        $results[] = ['date' => $date->toDateString(), 'period' => $all_day->c_code];
 | 
			
		||||
                    }
 | 
			
		||||
                } else {
 | 
			
		||||
                    if ($startPeriod == $morning->c_code) {
 | 
			
		||||
                        $results[] = ['date' => $date->toDateString(), 'period' => $all_day->c_code];
 | 
			
		||||
                    } else {
 | 
			
		||||
                        $results[] = ['date' => $date->toDateString(), 'period' => $startPeriod];
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            } elseif ($date->isSameDay($endDate)) {
 | 
			
		||||
                //If it is the end date
 | 
			
		||||
                //If the end session is afternoon, add morning first
 | 
			
		||||
                if ($endPeriod == 'afternoon') {
 | 
			
		||||
                    $results[] = ['date' => $date->toDateString(), 'period' => 'morning'];
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if ($endPeriod == $afternoon->c_code) {
 | 
			
		||||
                    $results[] = ['date' => $date->toDateString(), 'period' => $all_day->c_code];
 | 
			
		||||
                } else {
 | 
			
		||||
                    $results[] = ['date' => $date->toDateString(), 'period' => $endPeriod];
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                //Add both sessions for days between intervals
 | 
			
		||||
                $results[] = ['date' => $date->toDateString(), 'period' => 'morning'];
 | 
			
		||||
                $results[] = ['date' => $date->toDateString(), 'period' => 'afternoon'];
 | 
			
		||||
                $results[] = ['date' => $date->toDateString(), 'period' => $all_day->c_code];
 | 
			
		||||
                // $results[] = ['date' => $date->toDateString(), 'period' => 'afternoon'];
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -114,17 +114,6 @@ class TimekeepingController extends Controller
 | 
			
		|||
        if ($user_id == "") {
 | 
			
		||||
            return response()->json(['status' => false, 'message' => 'User not found!']);
 | 
			
		||||
        }
 | 
			
		||||
        // Notes::insert([
 | 
			
		||||
        //     [
 | 
			
		||||
        //         "n_user_id" => $user_id,
 | 
			
		||||
        //         "n_day" => $day,
 | 
			
		||||
        //         "n_month" => $month,
 | 
			
		||||
        //         "n_year" => $year,
 | 
			
		||||
        //         "n_time_type" => $time_type,
 | 
			
		||||
        //         "n_reason" => $reason,
 | 
			
		||||
        //         "n_note" => $note
 | 
			
		||||
        //     ]
 | 
			
		||||
        // ]);
 | 
			
		||||
 | 
			
		||||
        $existingNote = Notes::where('n_user_id', $user_id)
 | 
			
		||||
            ->where('n_day', $day)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,6 +20,7 @@ return new class extends Migration
 | 
			
		|||
            $table->string('end_period');
 | 
			
		||||
            $table->string('type');
 | 
			
		||||
            $table->string('reason');
 | 
			
		||||
            $table->string('status');
 | 
			
		||||
            $table->string('admin_note')->nullable();
 | 
			
		||||
            $table->string('updated_by');
 | 
			
		||||
            $table->timestamps();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue