protected function prepareTransactionSimpleFormat($activityId) { $returnData = array('total-commitments' => 0, 'total-disbursements' => 0, 'total-expenditure' => 0, 'total-incoming-funds' => 0); $element = new Iati_Aidstream_Element_Activity_Transaction(); $data = $element->fetchData($activityId, true); if ($element->hasData($data)) { foreach ($data as $transactionData) { $value = $transactionData['TransactionValue']['text']; switch ($transactionData['TransactionType']['@code']) { case 1: $returnData['total-commitments'] += $value; break; case 2: $returnData['total-disbursements'] += $value; break; case 4: $returnData['total-expenditure'] += $value; break; case 5: $returnData['total-incoming-funds'] += $value; break; default: break; } } } return $returnData; }
/** * Function to fetch data used to populate form and display view page. */ public function getDataForForm($activityId) { $data = array(); // Get title $titleEle = new Iati_Aidstream_Element_Activity_Title(); $titleValue = $titleEle->fetchData($activityId, true); $data['title_id'] = $titleValue[0]['id']; $data['title'] = $titleValue[0]['text']; // Get description $descriptionEle = new Iati_Aidstream_Element_Activity_Description(); $descriptionValue = $descriptionEle->fetchData($activityId, true); $data['description_id'] = $descriptionValue[0]['id']; $data['description'] = $descriptionValue[0]['text']; // Get participating org $participatingOrgEle = new Iati_Aidstream_Element_Activity_ParticipatingOrg(); $participatingOrgValue = $participatingOrgEle->fetchData($activityId, true); if (!empty($participatingOrgValue)) { $count = 0; foreach ($participatingOrgValue as $participatingOrg) { $data['funding_org'][$count] = $participatingOrg['text']; $count++; } } // Get Activity date (start data and end date) $activityDateEle = new Iati_Aidstream_Element_Activity_ActivityDate(); $activityDateValues = $activityDateEle->fetchData($activityId, true); foreach ($activityDateValues as $activityDateValue) { if ($activityDateValue['@type'] == 3) { $data['start_date_id'] = $activityDateValue['id']; $data['start_date'] = $activityDateValue['@iso_date']; } else { if ($activityDateValue['@type'] == 4) { $data['end_date_id'] = $activityDateValue['id']; $data['end_date'] = $activityDateValue['@iso_date']; } } } // Get Document $documentEle = new Iati_Aidstream_Element_Activity_DocumentLink(); $documentValue = $documentEle->fetchData($activityId, true); $count = 0; if (!empty($documentValue)) { foreach ($documentValue as $document) { $data['document'][$count]['id'] = $document['id']; $data['document'][$count]['url'] = $document['@url']; // get document category $docCat = $document['Category']; if (!empty($docCat)) { $data['document'][$count]['category_id'] = $docCat[0]['id']; $data['document'][$count]['category_code'] = $docCat[0]['@code']; } // get document title $docTitle = $document['Title']; if (!empty($docTitle)) { $data['document'][$count]['title_id'] = $docTitle[0]['id']; $data['document'][$count]['title'] = $docTitle[0]['text']; } $count++; } } // Get location $locationEle = new Iati_Aidstream_Element_Activity_Location(); $locationValues = $locationEle->fetchData($activityId, true); $count = 0; if (!empty($locationValues)) { foreach ($locationValues as $locationValue) { $data['location'][$count]['location_id'] = $locationValue['id']; //get location name $locationName = $locationValue['Name']; if (!empty($locationName)) { $data['location'][$count]['location_name_id'] = $locationName[0]['id']; $data['location'][$count]['location_name'] = $locationName[0]['text']; } //get location description $locationDesc = $locationValue['Description']; if (!empty($locationDesc)) { $data['location'][$count]['location_desc_id'] = $locationDesc[0]['id']; $vdcs = preg_replace('/ -.*$/', '', $locationDesc[0]['text']); if ($vdcs == VDCS_DEFAULT_VALUE) { $vdcs = ''; } $data['location'][$count]['location_vdcs'] = $vdcs; } //get location coordinates $locationCoords = $locationValue['Coordinates']; if (!empty($locationCoords)) { $data['location'][$count]['location_coord_id'] = $locationCoords['id']; } //get location administrative $locationAdm = $locationValue['Administrative']; if (!empty($locationAdm)) { $data['location'][$count]['location_adm_id'] = $locationAdm['id']; } $count++; } } // Get budget $budgetEle = new Iati_Aidstream_Element_Activity_Budget(); $budgetValues = $budgetEle->fetchData($activityId, true); $count = 0; if (!empty($budgetValues)) { foreach ($budgetValues as $budget) { $data['budget'][$count]['id'] = $budget['id']; //get budget value $budgetVal = $budget['Value']; if (!empty($budgetVal)) { $data['budget'][$count]['value_id'] = $budgetVal['id']; $data['budget'][$count]['currency'] = $budgetVal['@currency']; $data['budget'][$count]['amount'] = $budgetVal['text']; $data['budget'][$count]['signed_date'] = $budgetVal['@value_date']; } //get budget start date $budgetStart = $budget['PeriodStart']; if (!empty($budgetStart)) { $data['budget'][$count]['start_id'] = $budgetStart['id']; $data['budget'][$count]['start_date'] = $budgetStart['@iso_date']; } //get budget end date $budgetEnd = $budget['PeriodEnd']; if (!empty($budgetEnd)) { $data['budget'][$count]['end_id'] = $budgetEnd['id']; $data['budget'][$count]['end_date'] = $budgetEnd['@iso_date']; } $count++; } } // Get transaction $transactionEle = new Iati_Aidstream_Element_Activity_Transaction(); $transactionValues = $transactionEle->fetchData($activityId, true); $expCount = 0; $incomCount = 0; $commCount = 0; if (!empty($transactionValues)) { foreach ($transactionValues as $transaction) { //get transaction start date $transactionType = $transaction['TransactionType']; if (!empty($transactionType)) { $code = $transactionType['@code']; if ($code == 4) { $type = 'expenditure'; $count = $expCount; $expCount++; } elseif ($code == 5) { $type = 'incommingFund'; $count = $incomCount; $incomCount++; } } //get transaction value $transactionVal = $transaction['TransactionValue']; if (!empty($transactionVal)) { $data[$type][$count]['id'] = $transaction['id']; $data[$type][$count]['value_id'] = $transactionVal['id']; $data[$type][$count]['currency'] = $transactionVal['@currency']; $data[$type][$count]['start_date'] = $transactionVal['@value_date']; $data[$type][$count]['amount'] = $transactionVal['text']; } } } // Get sector $sectorEle = new Iati_Aidstream_Element_Activity_Sector(); $sectors = $sectorEle->fetchData($activityId, true); if (!empty($sectors)) { $count = 0; foreach ($sectors as $sector) { $data['sector'][$count]['sector'] = $sector['@code']; $count++; } } //Get Status $statusEle = new Iati_Aidstream_Element_Activity_ActivityStatus(); $status = $statusEle->fetchData($activityId, true); if (!empty($status)) { $data['status_id'] = $status['id']; $data['status'] = $status['@code']; } //Get Result $count = 0; $resultEle = new Iati_Aidstream_Element_Activity_Result(); $results = $resultEle->fetchData($activityId, true); foreach ($results as $result) { $data['result'][$count]['id'] = $result['id']; $data['result'][$count]['result_type'] = $result['@type']; $data['result'][$count]['title_id'] = $result['Title'][0]['id']; $data['result'][$count]['title'] = $result['Title'][0]['text']; $data['result'][$count]['description_id'] = $result['Description'][0]['id']; $data['result'][$count]['description'] = $result['Description'][0]['text']; $data['result'][$count]['indicator_id'] = $result['Indicator'][0]['id']; $data['result'][$count]['indicator_title_id'] = $result['Indicator'][0]['Title'][0]['id']; $data['result'][$count]['indicator'] = $result['Indicator'][0]['Title'][0]['text']; $data['result'][$count]['period_id'] = $result['Indicator'][0]['Period'][0]['id']; $data['result'][$count]['actual_id'] = $result['Indicator'][0]['Period'][0]['Actual'][0]['id']; $data['result'][$count]['achievement'] = $result['Indicator'][0]['Period'][0]['Actual'][0]['@value']; $data['result'][$count]['period_end_id'] = $result['Indicator'][0]['Period'][0]['PeriodEnd'][0]['id']; $data['result'][$count]['end_date'] = $result['Indicator'][0]['Period'][0]['PeriodEnd'][0]['@iso_date']; $count++; } return $data; }
/** * Upload Simple Data to Transaction */ public function uploadSimpleDataToTransaction($activityId) { $count = array(); $count['total'] = $this->prepareSimpleTransactionData(); $element = new Iati_Aidstream_Element_Activity_Transaction(); $result = $element->fetchData($activityId, true); $result = Iati_ElementSorter::sortElementsData($result, array('TransactionDate' => '@iso_date'), array('TransactionValue' => '@value_date')); // Update if existing transaction by compairing 'internal reference' $count['update'] = 0; $duplicate = 0; $transactionKey = 0; foreach ($this->elementData as $key => $data) { $refCount = 0; foreach ($result as $row) { if (strtolower($data['ref']) == strtolower($row['@ref']) && !empty($data['ref'])) { if ($refCount == 0) { $this->elementData[$key]['id'] = $row['id']; $this->elementData[$key]['TransactionType']['id'] = $row['TransactionType']['id']; $this->elementData[$key]['TransactionValue']['id'] = $row['TransactionValue']['id']; $this->elementData[$key]['ProviderOrg']['id'] = $row['ProviderOrg']['id']; $this->elementData[$key]['ReceiverOrg']['id'] = $row['ReceiverOrg']['id']; $this->elementData[$key]['TransactionDate']['id'] = $row['TransactionDate']['id']; $this->elementData[$key]['Description']['id'] = $row['Description']['id']; $count['update'] += 1; // Transaction Update count $refCount = 1; } elseif ($refCount == 1) { $duplicate += 1; $transactionKey = $key; } } } } // Transaction add count $count['add'] = $count['total'] - $count['update']; if ($duplicate >= 1) { $this->error[$transactionKey][]['message'] = "Cannot update transaction. Internal reference duplication on your \n existing transactions. Please use a different internal reference or \n check your existing transactions."; } if (empty($this->error)) { $element->save($this->elementData, $activityId); return $count; } else { return false; } }