middleware('auth'); $this->_service = 'rcpa'; $this->_module = 'submission'; $this->_viewname = 'rcpasubmission'; $this->_title = 'RCPA Submission'; $this->_base_url = '/rcpasubmission'; } protected function get_list_fields(){ return [ 'actions' => ['title' => 'Actions', 'buttons' => [ 'edit', 'delete', 'questions' => ['icon' => 'progress-question', 'querystring' => 'id={ID}'], 'import' => ['icon' => 'cloud-upload', 'querystring' => 'id={ID}'] ] ], 'title' => ['title' => 'Title', 'type' => 'text'], 'workflow' => ['title' => 'Workflow', 'type' => 'text'], 'status' => ['title' => 'Status', 'type' => 'select'], ]; } protected function get_form_fields($type, $values){ $users = $this->getlambda('get', 'ums', 'users', 'dropdown'); $users_dd = ['l1manager' => 'L1 Manager', 'l2manager' => 'L2 Manager']; foreach($users->data->items as $user){ $users_dd[$user->id] = $user->firstname . ' ' . $user->lastname . ' (' . $user->employee_code . ')'; } $roles = $this->getlambda('get', 'ums', 'roles', 'dropdown'); $roles_dd = []; foreach($roles->data->items as $role){ $roles_dd[$role->id] = $role->name; } $coattr = $this->getlambda('get', 'contact', 'contactattribute', 'get_all'); $coattr_dd = []; foreach($coattr->data->items as $coatt){ $coattr_dd[$coatt->code] = $coatt->attribute; } return [ 'title' => ['title' => 'Title', 'type' => 'text', 'required' => true], 'status' => ['title' => 'Status', 'type' => 'select', 'required' => true, 'values' => ['active' => 'Active', 'disabled' => 'Disabled'] ], 'metric_submission' => ['title' => 'Metric Submission', 'type' => 'yesno', 'required' => true], 'metric_authkey' => ['title' => 'Auth Key', 'type' => 'text', 'required' => true], 'manager_de_roles' => ['title' => 'Manager Data Entry Roles', 'type' => 'multiselect', 'values' => $roles_dd ], 'contact_attributes' => ['title' => 'Include Doctor Attributes in Export', 'type' => 'multiselect', 'values' => $coattr_dd], 'self_notification' => ['title' => 'Self Email Notification', 'type' => 'yesno', 'required' => true, 'fullwidth' => true], 'manager1_email_notification' => ['title' => '1st Level Manager Email Notification', 'type' => 'yesno', 'required' => true], 'manager1_push_notification' => ['title' => '1st Level Manager Mobile Notification', 'type' => 'yesno', 'required' => true], 'manager2_email_notification' => ['title' => '2nd Level Manager Email Notification', 'type' => 'yesno', 'required' => true], 'manager2_push_notification' => ['title' => '2nd Level Manager Mobile Notification', 'type' => 'yesno', 'required' => true], 'roles_email_notification' => ['title' => 'Email Notification to Roles', 'type' => 'multiselect', 'values' => $roles_dd ], 'roles_push_notification' => ['title' => 'Push Notification to Roles', 'type' => 'multiselect', 'values' => $roles_dd ], 'allow_non_hierarchy_submission' => ['title' => 'Allow Non Hierarchy Submission', 'type' => 'yesno', 'required' => true], 'workflow' => ['title' => 'Enable Workflow', 'type' => 'yesno', 'required' => true], 'workflow_type' => ['title' => 'Workflow type', 'type' => 'select', 'required' => true, 'values' => ['single' => 'Single Persoon', 'multiple' => 'Multiple person', 'sequencial' => 'Sequencial'] ], 'approver_can_edit' => ['title' => 'Approver can edit entry', 'type' => 'yesno', 'required' => true], 'is_personalized' => ['title' => 'Is Personalization Needed', 'type' => 'yesno', 'required' => true], 'allow_delete_by_user' => ['title' => 'Allow Submission Delete by User', 'type' => 'yesno', 'required' => true], 'export_fields' => ['title' => 'Export Fields', 'type' => 'textarea', 'required' => false, 'fullwidth' => true ], 'workflow_approvers' => ['title' => 'Metric', 'type' => 'mapping', 'required' => true, 'approvers' => $users_dd, 'fullwidth' => true ], ]; } protected function _beforeform($type, $values) { if($type == 'edit') { $values->self_notification = isset($values->self_notification) ? $values->self_notification : 'no'; $values->manager1_email_notification = isset($values->manager1_email_notification) ? $values->manager1_email_notification : 'no'; $values->manager1_push_notification = isset($values->manager1_push_notification) ? $values->manager1_push_notification : 'no'; $values->manager2_email_notification = isset($values->manager2_email_notification) ? $values->manager2_email_notification : 'no'; $values->manager2_push_notification = isset($values->manager2_push_notification) ? $values->manager2_push_notification : 'no'; $values->export_fields = isset($values->export_fields) ? $values->export_fields : ''; } //echo '
'; print_r($values); exit;
        return $values;
    }

    public function questions(Request $request) {
        $entity_id = $request->input('id', 0);
        if($entity_id === 0) {
            return redirect('/formentitys')->withError('Form not found!');
        } else {
            $request->session()->put('entity_id', $entity_id);
            return redirect('/formquestions');
        }
    }

    public function postimport(Request $request) {
        $entity_id = $request->input('id');
        if( $request->file('importfile') ) {
            $path = $request->file('importfile')->store('form_import');
            $csv = Reader::createFromPath(storage_path('app') .'/' . $path, 'r');
            $csv->setHeaderOffset(0);

            $header = $csv->getHeader(); //returns the CSV header record
            $records = $csv->getRecords(); //returns all the CSV records as an Iterator object

            $response = LambdaRequest::signedrequest('get', $this->_service, 'formquestion/get_all', ['entity_id' => $entity_id]);
            $forfields = [];
            foreach ($response->data->items as $q) {
                $forfields[] = $q->code;
            }

            $count = 0;
            $recs = [];
            // $forfields = ['is-aware-of-the-customer-perspective-potential-prescription-position','has-customer-plan-based-on-customer-perspective','rcpawardicu-quantified-rcpa-comp-taken-pob-dr-perspective-on-molecule-or-price-etc','call-objective-planned-question-opening-statement-materials-and-objection-anticipated','greeted-customer-appropriately','used-planned-opening-statement','gained-attention-of-the-customer','was-ready-with-tab-maintained-eye-contact-with-doctor-had-a-clear-voice','asked-planned-question-as-per-call-planning','asked-question-related-to-customer-behaviourbelief-service','listened-attentively-and-asked-follow-up-question-if-required','generated-desired-response-from-customer','maintained-the-sequence-of-product-as-per-the-speciality-p1p2p3','shared-benefit-of-productdevicesservices-as-per-marketing-strategy','shared-informationused-the-materialstools-as-per-plan-demonstrated-and-discussed-cipla-devices','utilized-the-tab-effectively-smooth-navigation-highlighted-studies-zoomed-graphs-etc','listened-attentively-acknowledged-concern','asked-questions-to-seek-clarity-on-questionobjection','shared-the-relevant-information','confirmed-if-objectionquestion-was-addressed-assured-follow-through-actions-if-required-and-returned-to-call-flow','summarized-the-key-discussion-points','gained-agreement-basis-call-objective-from-the-doctor','quantified-the-demand-with-rx','left-lblreminder-of-focused-product','total_score','area-of-strength','area-of-development','coaching-objective','development-time-frame','next-coaching-visit','development-action-plan'];

            foreach ($records as $record) {
                $record['record_date'] = '2024-04-02';
                $record['created_date'] = strtotime("now");
                $formdata = [];
                foreach($forfields as $field) {
                    if(isset($record[$field])) {
                        $formdata[$field] = $record[$field] == 'Yes' ? 1 : (  $record[$field] == 'No' ? 0 : $record[$field] ) ;
                    }
                }
                $recs[] = [
                    'entity_id' => $entity_id,
                    'formdata' => $formdata,
                    'employee_code' => $record['employee_code'],
                    'partition' => 'rapport-'.Auth::user()->domain,
                    'record_date' => strtotime($record['record_date']),
                    'createdAt' => strtotime($record['created_date'])
                ];
                if(sizeof($recs) >= 25) {
                    $response = LambdaRequest::signedrequest('post', $this->_service, $this->_module.'/import', $recs);
                    $recs = [];
                }
            }
            $response = LambdaRequest::signedrequest('post', $this->_service, $this->_module.'/import', $recs);
            return redirect($this->_base_url.'s')->withMessage('Successfully Uploaded !!!');
        } else {
            return \Redirect::back()->withMessage('Some Error occured!');
        }
    }
}