Skip to content

Commit

Permalink
UPD: sadrs report
Browse files Browse the repository at this point in the history
  • Loading branch information
Itskiprotich committed Aug 6, 2024
1 parent 3024e59 commit 3229164
Show file tree
Hide file tree
Showing 13 changed files with 2,081 additions and 350 deletions.
148 changes: 148 additions & 0 deletions src/Controller/Api/SubCountiesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

declare(strict_types=1);

namespace App\Controller\Api;

use App\Controller\AppController;
use Cake\Event\EventInterface;
use Cake\Http\Exception\NotFoundException;

/**
* SubCounties Controller
*
* @property \App\Model\Table\SubCountiesTable $SubCounties
* @method \App\Model\Entity\SubCounty[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
*/
class SubCountiesController extends AppController
{

public function beforeFilter(EventInterface $event): void
{
parent::beforeFilter($event);
$this->Auth->allow('autocomplete');
}

public function autocomplete()
{

$term = $this->request->getQuery('county');
$subCounties = $this->SubCounties->find()
->where(['SubCounties.county_id' => $term])
->all();

if ($subCounties->isEmpty()) {
throw new NotFoundException(__('No sub-counties found for the selected county.'));
}

// Prepare data for JSON response
$subCountiesList = [];
foreach ($subCounties as $subCounty) {
$subCountiesList[] = [
'id' => $subCounty->id,
'name' => $subCounty->sub_county_name
];
}

$this->set([
'codes' => $subCountiesList,
'_serialize' => ['codes']
]);
}
/**
* Index method
*
* @return \Cake\Http\Response|null|void Renders view
*/
public function index()
{
$this->paginate = [
'contain' => ['Counties'],
];
$subCounties = $this->paginate($this->SubCounties);

$this->set(compact('subCounties'));
}

/**
* View method
*
* @param string|null $id Sub County id.
* @return \Cake\Http\Response|null|void Renders view
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$subCounty = $this->SubCounties->get($id, [
'contain' => ['Counties', 'Aefis', 'Aggregates', 'Ce2bs', 'Padrs', 'Pqmps', 'Sadrs'],
]);

$this->set(compact('subCounty'));
}

/**
* Add method
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$subCounty = $this->SubCounties->newEmptyEntity();
if ($this->request->is('post')) {
$subCounty = $this->SubCounties->patchEntity($subCounty, $this->request->getData());
if ($this->SubCounties->save($subCounty)) {
$this->Flash->success(__('The sub county has been saved.'));

return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The sub county could not be saved. Please, try again.'));
}
$counties = $this->SubCounties->Counties->find('list', ['limit' => 200])->all();
$this->set(compact('subCounty', 'counties'));
}

/**
* Edit method
*
* @param string|null $id Sub County id.
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function edit($id = null)
{
$subCounty = $this->SubCounties->get($id, [
'contain' => [],
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$subCounty = $this->SubCounties->patchEntity($subCounty, $this->request->getData());
if ($this->SubCounties->save($subCounty)) {
$this->Flash->success(__('The sub county has been saved.'));

return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The sub county could not be saved. Please, try again.'));
}
$counties = $this->SubCounties->Counties->find('list', ['limit' => 200])->all();
$this->set(compact('subCounty', 'counties'));
}

/**
* Delete method
*
* @param string|null $id Sub County id.
* @return \Cake\Http\Response|null|void Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$subCounty = $this->SubCounties->get($id);
if ($this->SubCounties->delete($subCounty)) {
$this->Flash->success(__('The sub county has been deleted.'));
} else {
$this->Flash->error(__('The sub county could not be deleted. Please, try again.'));
}

return $this->redirect(['action' => 'index']);
}
}
17 changes: 1 addition & 16 deletions src/Controller/FacilityCodesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,7 @@ public function wards()
'codes' => $codes,
'_serialize' => ['codes']
]);

// $this->RequestHandler->setContent('json', 'application/json');
// if (is_numeric($this->request->query['term'])) {
// $coders = $this->FacilityCode->finder($this->request->query['term'], 'N');
// } else {
// $coders = $this->FacilityCode->finder($this->request->query['term'], 'A');
// }
// $codes = array();
// foreach ($coders as $key => $value) {
// $codes[] = array(
// 'value' => $value['FacilityCode']['facility_code'], 'label' => $value['FacilityCode']['facility_name'], 'sub_county' => $value['FacilityCode']['district'],
// 'desc' => $value['FacilityCode']['county'], 'addr' => $value['FacilityCode']['official_address'], 'phone' => $value['FacilityCode']['official_mobile']
// );
// }
// $this->set('codes', $codes);
// $this->set('_serialize', 'codes');

}
public function autocomplete()
{
Expand Down
127 changes: 118 additions & 9 deletions src/Controller/Reporter/SadrsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function view($id = null)
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
*/
public function add($id = null)
{
{
$sadr = $this->Sadrs->newEmptyEntity();
$data = array(
'user_id' => $this->Auth->User('id'),
Expand All @@ -77,7 +77,7 @@ public function add($id = null)

$sadr = $this->Sadrs->patchEntity($sadr, $data);
$this->Sadrs->save($sadr);
$this->Flash->success(__('The sadr has been saved.'));
$this->Flash->success(__('The sadr has been saved.'));
$this->redirect(array('action' => 'edit', $sadr->id));
}

Expand All @@ -95,21 +95,130 @@ public function edit($id = null)
'contain' => [],
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$sadr = $this->Sadrs->patchEntity($sadr, $this->request->getData());
if ($this->Sadrs->save($sadr)) {
$this->Flash->success(__('The sadr has been saved.'));

return $this->redirect(['action' => 'index']);
$validate = false;
if (!empty($this->request->getData('submitReport'))) {
$validate = 'first';
debug("submitting....");
}
$this->Flash->error(__('The sadr could not be saved. Please, try again.'));
// debug($this->request->getData());
// exit;
$sadr = $this->Sadrs->patchEntity($sadr, $this->request->getData(), [
'associated' => ['Attachment']
]);
if ($this->Sadrs->save($sadr, array('validate' => $validate, 'deep' => true))) {
if (!empty($this->request->getData('submitReport'))) {
$this->Sadrs->saveField('submitted', 2);
$this->Sadrs->saveField('submitted_date', date("Y-m-d H:i:s"));
//lucian
// if(empty($sadr->reference_no)) {
if (!empty($sadr['reference_no']) && $sadr['reference_no'] == 'new') {
$count = $this->Sadrs->find('count', array(
'fields' => 'Sadrs.reference_no',
'conditions' => array(
'Sadrs.submitted_date BETWEEN ? and ?' => array(date("Y-01-01 00:00:00"), date("Y-m-d H:i:s")), 'Sadrs.reference_no !=' => 'new'
)
));
$count++;
$count = ($count < 10) ? "0$count" : $count;
$reference = 'SADR/' . date('Y') . '/' . $count;
$this->Sadrs->saveField('reference_no', $reference);
}
//bokelo
// $sadr = $this->Sadr->read(null, $id);

// //****************** Send Email and Notifications to Reporter and Managers *****************************
// $this->loadModel('Message');
// $html = new HtmlHelper(new ThemeView());
// $message = $this->Message->find('first', array('conditions' => array('name' => 'reporter_sadr_submit')));
// $variables = array(
// 'name' => $this->Auth->User('name'), 'reference_no' => $sadr['Sadr']['reference_no'],
// 'reference_link' => $html->link(
// $sadr['Sadr']['reference_no'],
// array('controller' => 'sadrs', 'action' => 'view', $sadr['Sadr']['id'], 'reporter' => true, 'full_base' => true),
// array('escape' => false)
// ),
// 'modified' => $sadr['Sadr']['modified']
// );
// $datum = array(
// 'email' => $sadr['Sadr']['reporter_email'],
// 'id' => $id, 'user_id' => $this->Auth->User('id'), 'type' => 'reporter_sadr_submit', 'model' => 'Sadr',
// 'subject' => CakeText::insert($message['Message']['subject'], $variables),
// 'message' => CakeText::insert($message['Message']['content'], $variables)
// );

// $this->loadModel('Queue.QueuedTask');
// $this->QueuedTask->createJob('GenericEmail', $datum);
// $this->QueuedTask->createJob('GenericNotification', $datum);


// //Send SMS
// if (!empty($sadr['Sadr']['reporter_phone']) && strlen(substr($sadr['Sadr']['reporter_phone'], -9)) == 9 && is_numeric(substr($sadr['Sadr']['reporter_phone'], -9))) {
// $datum['phone'] = '254' . substr($sadr['Sadr']['reporter_phone'], -9);
// $variables['reference_url'] = Router::url(['controller' => 'sadrs', 'action' => 'view', $sadr['Sadr']['id'], 'reporter' => true, 'full_base' => true]);
// $datum['sms'] = CakeText::insert($message['Message']['sms'], $variables);
// $this->QueuedTask->createJob('GenericSms', $datum);
// }

// //Notify managers
// $users = $this->Sadr->User->find('all', array(
// 'contain' => array(),
// 'conditions' => array('User.group_id' => 2, 'User.is_active' => '1')
// ));
// foreach ($users as $user) {
// $variables = array(
// 'name' => $user['User']['name'], 'reference_no' => $sadr['Sadr']['reference_no'],
// 'reference_link' => $html->link(
// $sadr['Sadr']['reference_no'],
// array('controller' => 'sadrs', 'action' => 'view', $sadr['Sadr']['id'], 'manager' => true, 'full_base' => true),
// array('escape' => false)
// ),
// 'modified' => $sadr['Sadr']['modified']
// );
// $datum = array(
// 'email' => $user['User']['email'],
// 'id' => $id, 'user_id' => $user['User']['id'], 'type' => 'reporter_sadr_submit', 'model' => 'Sadr',
// 'subject' => CakeText::insert($message['Message']['subject'], $variables),
// 'message' => CakeText::insert($message['Message']['content'], $variables)
// );

// $this->QueuedTask->createJob('GenericEmail', $datum);
// $this->QueuedTask->createJob('GenericNotification', $datum);
// }
//********************************** END *********************************

// If the report is serious sent an alert:
// $serious = $sadr['Sadr']['serious'];
// if ($serious == "Yes") {
// $this->notifyCountyPharmacist($sadr);
// }

$this->Flash->success(__('The SADR has been submitted to PPB'));
$this->redirect(array('action' => 'view', $sadr->id));
}
// debug($this->request->data);
$this->Flash->success(__('The SADR has been saved'));
$this->redirect($this->referer());
} else {
$this->Flash->error(__('The SADR could not be saved. Please review the error(s) and resubmit and try again.'));
}

// $sadr = $this->Sadrs->patchEntity($sadr, $this->request->getData());
// if ($this->Sadrs->save($sadr)) {
// $this->Flash->success(__('The sadr has been saved.'));

// return $this->redirect(['action' => 'index']);
// }
// $this->Flash->error(__('The sadr could not be saved. Please, try again.'));
}

$users = $this->Sadrs->Users->find('list', ['limit' => 200])->all();
$pqmps = $this->Sadrs->Pqmps->find('list', ['limit' => 200])->all();
$medications = $this->Sadrs->Medications->find('list', ['limit' => 200])->all();
$counties = $this->Sadrs->Counties->find('list', ['limit' => 200])->all();
$subCounties = $this->Sadrs->SubCounties->find('list', ['limit' => 200])->all();
$sub_counties = $this->Sadrs->SubCounties->find('list', ['limit' => 200])->all();
$designations = $this->Sadrs->Designations->find('list', ['limit' => 200])->all();
$this->set(compact('sadr', 'users', 'pqmps', 'medications', 'counties', 'subCounties', 'designations'));
$this->set(compact('sadr', 'users', 'pqmps', 'medications', 'counties', 'sub_counties', 'designations'));
}

/**
Expand Down
13 changes: 12 additions & 1 deletion src/Controller/Reporter/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,18 @@ public function dashboard(){

$users=[];
$this->set(compact('users'));
$sadrs=[];

$sadrs = $this->Users->Sadrs->find('all', array(
'limit' => 7,
'contain' => array(),
'fields' => array('Sadrs.id', 'Sadrs.user_id', 'Sadrs.created', 'Sadrs.report_title', 'Sadrs.submitted', 'Sadrs.reference_no', 'Sadrs.created', 'Sadrs.serious'),
'order' => array('Sadrs.created' => 'desc'),
'conditions' => array(
// only show SADRs that have been not been deleted
'Sadrs.deleted' => false,
'Sadrs.user_id' => $this->Auth->User('id')
),
));
$this->set(compact('sadrs'));
$aefis=[];
$this->set(compact('aefis'));
Expand Down
7 changes: 7 additions & 0 deletions src/Model/Table/SadrsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ public function initialize(array $config): void
$this->hasMany('Sadrs', [
'foreignKey' => 'sadr_id',
]);

$this->hasMany('Attachment', [
'foreignKey' => 'foreign_key',
'conditions' => array('Attachment.model' => 'Sadr', 'Attachment.group' => 'attachment'),
]);


}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/Model/Table/SubCountiesTable.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace App\Model\Table;
Expand Down Expand Up @@ -48,7 +49,7 @@ public function initialize(array $config): void
parent::initialize($config);

$this->setTable('sub_counties');
$this->setDisplayField('id');
$this->setDisplayField('sub_county_name');
$this->setPrimaryKey('id');

$this->addBehavior('Timestamp');
Expand Down Expand Up @@ -144,4 +145,11 @@ public function buildRules(RulesChecker $rules): RulesChecker

return $rules;
}

public function findByCountyId(Query $query, array $options): Query
{
$term = $options['term'] ?? '';
// Example: searching by numeric term
return $query->where(['county_id LIKE' => "%$term%"]);
}
}
Loading

0 comments on commit 3229164

Please sign in to comment.