middleware('auth'); $this->_service = 'rcpa'; $this->_module = 'sku'; $this->_viewname = 'rcpareport'; $this->_title = 'RCPA Reports'; $this->_base_url = '/rcpareport'; } public function tmwise(Request $request) { $skus = LambdaRequest::signedrequest('get', $this->_service, $this->_module.'/list', []); $month = 'September 2024'; $thismonth = strtotime("now"); $month = strtotime($month); $months = []; while($month <= $thismonth) { $months[] = date("F Y", $month); $month = $month + (60*60*24)*date("t", $month); } $skus->data->{'months'} = $months; return $this->view('tmwise')->with((array)$skus->data); } public function apptmwise(Request $request) { $params = $request->all(); $skus = LambdaRequest::getrequest($this->_service, $this->_module.'/list', $params); //echo '
'; print_r($skus); exit;
        $month = 'September 2024';
        $thismonth = strtotime("now");
        $month = strtotime($month);
        $months = [];
        while($month <= $thismonth) {
            $months[] = date("F Y", $month);
            $month = $month + (60*60*24)*date("t", $month);
        }
        $skus->data->{'months'} = $months;
        $skus->data->{'params'} = $params;
        return $this->view('apptmwise')->with((array)$skus->data);
    }

    public function ajaxtmwise(Request $request) {
        $params = $request->all();
        if(sizeof($params) > 0) {
            $skus = LambdaRequest::getrequest($this->_service, $this->_module.'/tmwise', $params);
        } else {
            $skus = LambdaRequest::signedrequest('get', $this->_service, $this->_module.'/tmwise', []);
        }
        
        return response()->json(["chemists" => $skus->data->agg] );
    }

    public function monthwise(Request $request) {
        $list = LambdaRequest::signedrequest('get', $this->_service, $this->_module.'/list', []);
        $skus = LambdaRequest::signedrequest('get', $this->_service, $this->_module.'/monthwise', ['sku'=>'sku1']);
        $skus->data->{'skus'} = $list->data->skus;
        return $this->view('monthwise')->with((array)$skus->data);
    }

    public function appmonthwise(Request $request) {
        $params = $request->all();
        $list = LambdaRequest::getrequest($this->_service, $this->_module.'/list', $params);
        $skus = LambdaRequest::getrequest($this->_service, $this->_module.'/monthwise', $params);
        $skus->data->{'skus'} = $list->data->skus;
        return $this->view('appmonthwise')->with((array)$skus->data);
    }

    public function formwise(Request $request) {
        $forms = LambdaRequest::signedrequest('get', 'form', 'formentity/dropdown', []);
        $forms = $forms->data->items;
        // Filter forms to include only those with 'rcpa' (case-insensitive) in the title
        $forms = array_filter($forms, function($form) {
            return stripos($form->title ?? '', 'rcpa') !== false;
        });
        // Renumber the array to reset keys
        $forms = array_values($forms);
        return $this->view('formwise')->with(['forms' => $forms]);
    }

    public function appformwise(Request $request) {
        $params = $request->all();
        $forms = LambdaRequest::getrequest($this->_service, 'form/list', $params);
        $month = 'September 2024';
        $thismonth = strtotime("now");
        $month = strtotime($month);
        $months = [];
        while($month <= $thismonth) {
            $months[] = date("F Y", $month);
            $month = $month + (60*60*24)*date("t", $month);
        }
        $forms->data->{'months'} = $months;
        $forms->data->{'params'} = $params;
        return $this->view('appformwise')->with((array)$forms->data);
    }

    public function ajaxformwise(Request $request) {
        $params = $request->all();
        
        // Get form_id, date_from, and date_to from request
        $form_id = $params['form_id'] ?? null;
        $date_from = $params['date_from'] ?? null;
        $date_to = $params['date_to'] ?? null;

        $requestData = [
            'form_id' => $form_id
        ];

        if ($date_from && $date_to) {
            $requestData['date_range'] = $date_from . '_' . $date_to;
        }
        
        // Call Lambda service
        $response = LambdaRequest::signedrequest('get', 'form', 'formentity/rcpa_transpose_report', $requestData);
        return response()->json(['response' => $response->data->Result, 'request_data' => $requestData]);
    }
}