Permalink
Browse files

Finished importer for billings. Finishes Issue #76.

Finished CSV exporter for billings. Finishes Issue #77.
  • Loading branch information...
1 parent 812c464 commit 7e994a89cada43f607dda571869aed7c39c27476 @tareko committed Oct 20, 2012
View
@@ -52,4 +52,7 @@
/**
* Add router for AJAX
*/
- Router::parseExtensions('json');
+ Router::parseExtensions('json');
+
+ //Add router for CSV
+ Router::parseExtensions('csv');
@@ -8,9 +8,51 @@ class BillingsController extends AppController {
function index() {
- $data = $this->Billing->import('HLAA3D.041');
- debug($data);
$this->render();
}
+ /* Upload function
+ *
+ */
+ function upload() {
+ if ($this->request->isPost()) {
+ $data = $this->Billing->import($this->request->data['Billing']['upload']['tmp_name']);
+ if ($this->Billing->saveAll($data, array('deep' => true))) {
+ return $this->Session->setFlash('Successfully imported file \'' .$this->request->data['Billing']['upload']['name'] . '\'');
+ }
+ else {
+ debug($this->Billing->validationErrors);
+ debug($data);
+ return $this->Session->setFlash('I\'m sorry. This file did not successfully import.');
+ }
+
+ }
+ $this->render();
+ }
+
+ function export() {
+ // Find fields needed without recursing through associated models
+ $data = $this->Billing->find('all', array(
+ 'limit' => 100,
+ 'BillingsItem'));
+ $data = $this->Billing->recombineBilling($data);
+ // Define column headers for CSV file, in same array format as the data itself
+ $headers = array(
+ 'id' => 'ID',
+ 'service_code' => 'Service code',
+ 'fee_submitted' => 'Fee submitted',
+ 'number_of_services' => 'Number of services',
+ 'service_date' => 'Service date',
+ 'billing_id' => 'Billing ID',
+ 'healthcare_provider' => 'Provider',
+ 'patient_birthdate' => 'Patient birthdate',
+ 'payment_program' => 'Payment program',
+ 'payee' => 'Payee',
+ 'referring' => 'Referring physician'
+ );
+ // Add headers to start of data array
+ array_unshift($data,$headers);
+ // Make the data available to the view (and the resulting CSV file)
+ $this->set(compact('data'));
+ }
}
?>
@@ -347,7 +347,7 @@ public function edit($id = null) {
if (!$this->Shift->exists()) {
throw new NotFoundException(__('Invalid shift'));
}
- if ($this->request->is('post') || $this->request->is('put')) {
+ if ($this->request->isPost() || $this->request->isPut()) {
if ($this->Shift->save($this->request->data)) {
$this->Session->setFlash(__('The shift has been saved'));
$this->redirect(array('action' => 'index'));
View
@@ -1,55 +1,134 @@
<?php
+App::uses('AppModel', 'Model');
/**
* Billing Model
*
+ * @property BillingsItem $BillingsItem
*/
class Billing extends AppModel {
-
+
/**
- * Imports MOHLTC file into DB
- * See MOHLTC Technical specifications: Interface to Health Care Systems
- * http://health.gov.on.ca/english/providers/pub/ohip/tech_specific/tech_specific_mn.html
- *
- * Special thanks to Tigrang for some amazing help!
- *
- * @param string $filename
+ * Associations
+ * @var unknown_type
*/
- function import ($filename) {
- // Set the filename to read from
- $filename = TMP . 'uploads' . DS . 'Billing' . DS . $filename;
+ public $hasMany = array(
+ 'BillingsItem' => array(
+ 'className' => 'BillingsItem',
+ 'foreignKey' => 'billing_id',
+ 'conditions' => '',
+ 'fields' => '',
+ 'order' => ''
+ )
+ );
+
+/**
+ * Validation rules
+ *
+ * @var array
+ */
+ public $validate = array(
+ 'patient_birthdate' => array(
+ 'date' => array(
+ 'rule' => array('date'),
+ //'message' => 'Your custom message here',
+ 'allowEmpty' => false,
+ 'required' => false,
+ //'last' => false, // Stop validation after this rule
+ //'on' => 'create', // Limit validation to 'create' or 'update' operations
+ ),
+ ),
- // Open the file
- if (!$file = fopen($filename, 'r')) {
- return false;
- }
-
- // Start parsing for health encounters
+ 'payment_program' => array(
+ 'notempty' => array(
+ 'rule' => array('notempty'),
+ //'message' => 'Your custom message here',
+ //'allowEmpty' => false,
+ 'required' => true,
+ //'last' => false, // Stop validation after this rule
+ //'on' => 'create', // Limit validation to 'create' or 'update' operations
+ ),
+ ),
+ 'payee' => array(
+ 'notempty' => array(
+ 'rule' => array('notempty'),
+ //'message' => 'Your custom message here',
+ //'allowEmpty' => false,
+ 'required' => true,
+ //'last' => false, // Stop validation after this rule
+ //'on' => 'create', // Limit validation to 'create' or 'update' operations
+ ),
+ ),
+/*
+ 'referring' => array(
+ 'numeric' => array(
+ 'rule' => array('numeric'),
+ //'message' => 'Your custom message here',
+ 'allowEmpty' => true,
+ 'required' => false,
+ //'last' => true, // Stop validation after this rule
+ //'on' => 'create', // Limit validation to 'create' or 'update' operations
+ ),
+ ),
+*/
+ );
+
+ public function beforeValidate($options = array()) {
+ if (!empty($this->data['Billing']['patient_birthdate'])) {
+ $this->data['Billing']['patient_birthdate'] = $this->dateFormatBeforeSave($this->data['Billing']['patient_birthdate']);
+ }
+ return true;
+ }
+
+ public function dateFormatBeforeSave($dateString) {
+ return date('Y-m-d', strtotime($dateString));
+ }
+
+ /**
+ * Imports MOHLTC file into DB
+ * See MOHLTC Technical specifications: Interface to Health Care Systems
+ * http://health.gov.on.ca/english/providers/pub/ohip/tech_specific/tech_specific_mn.html
+ *
+ * Special thanks to Tigrang for some amazing help!
+ *
+ * @param string $filename
+ */
+ function import ($filename) {
+ // Set the filename to read from
+ $filename = $filename;
+
+ // Open the file
+ if (!$file = fopen($filename, 'r')) {
+ return false;
+ }
+
+ // Start parsing for health encounters
$data = array();
- $i = 0;
+ $i = 0;
$j = 0;
-
+
while($row = fgets($file)) {
if (substr($row, 0, 3) == 'HEB') {
- $i = $i + 1;
- $data[$i] = $this->parseFields($row, 'HEB');
+ $i = $i + 1;
+ $data_provider[$i] = $this->parseFields($row, 'HEB');
}
- if (substr($row, 0, 3) == 'HEH') {
+ if (substr($row, 0, 3) == 'HEH') {
$j = $j + 1;
- $data[$i][$j] = $this->parseFields($row, 'HEH');
- }
+ $data[$j]['Billing'] = $this->parseFields($row, 'HEH');
+ $data[$j]['Billing']['healthcare_provider'] = $data_provider[$i]['healthcare_provider'];
+ }
if (substr($row, 0, 3) == 'HER') {
- $data[$i][$j]['Items'][] = $this->parseFields($row, 'HER');
- }
- if (substr($row, 0, 3) == 'HET') {
- $data[$i][$j]['Items'][] = $this->parseFields($row, 'HET');
- if (substr($row, 41, 1) != ' ') {
- $data[$i][$j]['Items'][] = $this->parseFields(substr($row, 38), 'HET');
+ $data[$j]['BillingsItem'][] = $this->parseFields($row, 'HER');
+ }
+ if (substr($row, 0, 3) == 'HET') {
+ $data[$j]['BillingsItem'][] = $this->parseFields($row, 'HET');
+ if (substr($row, 41, 1) != ' ') {
+ $data[$j]['BillingsItem'][] = $this->parseFields(substr($row, 38), 'HET');
}
}
- }
+ }
return $data;
- }
-
+ }
+
function parseFields($row, $section) {
$schema = array(
'HEB' => array(
@@ -60,15 +139,15 @@ function parseFields($row, $section) {
'HEH' => array(
'patient_birthdate' => array(
'start' => 15,
- 'length' => 8),
- 'payment_program' => array(
- 'start' => 31,
- 'length' => 3),
- 'payee' => array(
- 'start' => 34,
- 'length' => 1),
- 'referring' => array(
- 'start' => 35,
+ 'length' => 8),
+ 'payment_program' => array(
+ 'start' => 31,
+ 'length' => 3),
+ 'payee' => array(
+ 'start' => 34,
+ 'length' => 1),
+ 'referring' => array(
+ 'start' => 35,
'length' => 6),
),
'HER' => array(
@@ -79,23 +158,43 @@ function parseFields($row, $section) {
'HET' => array(
'service_code' => array(
'start' => 3,
- 'length' => 5),
- 'fee_submitted' => array(
- 'start' => 10,
- 'length' => 6),
- 'number_of_services' => array(
- 'start' => 16,
- 'length' => 2),
- 'service_date' => array(
- 'start' => 18,
+ 'length' => 5),
+ 'fee_submitted' => array(
+ 'start' => 10,
+ 'length' => 6),
+ 'number_of_services' => array(
+ 'start' => 16,
+ 'length' => 2),
+ 'service_date' => array(
+ 'start' => 18,
'length' => 8)
),
- );
+ );
$fields = array();
foreach($schema[$section] as $field => $opts) {
$fields[$field] = substr($row, $opts['start'], $opts['length']);
- }
+ }
return $fields;
- }
+ }
+
+ /**
+ * Recombine billings into one array for ease of CSV export
+ * @param array $data
+ */
+ public function recombineBilling ($data) {
+ $i = 0;
+ foreach ($data as $row) {
+ foreach ($row['BillingsItem'] as $item) {
+ $output[$i] = $item;
+ $output[$i]['healthcare_provider'] = $row['Billing']['healthcare_provider'];
+ $output[$i]['patient_birthdate'] = $row['Billing']['patient_birthdate'];
+ $output[$i]['payment_program'] = $row['Billing']['payment_program'];
+ $output[$i]['payee'] = $row['Billing']['payee'];
+ $output[$i]['referring'] = $row['Billing']['referring'];
+ $i = $i + 1;
+ }
+ }
+ return $output;
+ }
}
View
@@ -0,0 +1,54 @@
+<?php
+App::uses('AppModel', 'Model');
+/**
+ * BillingsItem Model
+ *
+ * @property Billings $Billings
+ */
+class BillingsItem extends AppModel {
+
+ /**
+ * Validation rules
+ *
+ * @var array
+ */
+ public $validate = array(
+ 'service_date' => array(
+ 'date' => array(
+ 'rule' => array('date'),
+ //'message' => 'Your custom message here',
+ //'allowEmpty' => false,
+ 'required' => false,
+ //'last' => false, // Stop validation after this rule
+ //'on' => 'create', // Limit validation to 'create' or 'update' operations
+ ),
+ ),
+ );
+ //The Associations below have been created with all possible keys, those that are not needed can be removed
+
+/**
+ * belongsTo associations
+ *
+ * @var array
+ */
+ public $belongsTo = array(
+ 'Billing' => array(
+ 'className' => 'Billing',
+ 'foreignKey' => 'billing_id',
+ 'conditions' => '',
+ 'fields' => '',
+ 'order' => ''
+ )
+ );
+
+ public function beforeValidate($options = array()) {
+ if (!empty($this->data['BillingsItem']['service_date'])) {
+ $this->data['BillingsItem']['service_date'] = $this->dateFormatBeforeSave($this->data['BillingsItem']['service_date']);
+ }
+ return true;
+ }
+
+ public function dateFormatBeforeSave($dateString) {
+ return date('Y-m-d', strtotime($dateString));
+ }
+}
Oops, something went wrong.

0 comments on commit 7e994a8

Please sign in to comment.