383 lines
9.0 KiB
PHP
383 lines
9.0 KiB
PHP
<?php
|
|
class Participant extends AppModel
|
|
{
|
|
var $name='Participant';
|
|
|
|
private function validateAddParticipant($dbData,$data,$isAdd){
|
|
|
|
$errorCode = "";
|
|
//1 = Input data format is not correct
|
|
//4 = participant already exist in database
|
|
|
|
if($data["name"] == ""){
|
|
$errorCode = 1;
|
|
} else if(mb_strlen($data["name"], 'UTF-8') > PARTICIPENT_NAME_LENGTH){
|
|
$errorCode = 2;
|
|
} if(mb_strlen($data["name"], 'UTF-8') > mb_strlen(str_replace(",","",$data["name"]), 'UTF-8')
|
|
|| mb_strlen($data["name"], 'UTF-8') > mb_strlen(str_replace("|","",$data["name"]), 'UTF-8')){
|
|
$errorCode = 3;
|
|
}
|
|
|
|
if($errorCode == ""){
|
|
|
|
|
|
|
|
foreach($dbData as $key=>$value){
|
|
|
|
if($isAdd == false && trim($value["Participant"]["participant_id"]) == $data["participant_id"] ){
|
|
continue;
|
|
}
|
|
|
|
if(strtolower(trim($value["Participant"]["name"])) == strtolower($data["name"])){
|
|
|
|
$errorCode = 4;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* $fp = fopen("Test.txt","a+");
|
|
fwrite($fp,"error".$errorCode);exit; */
|
|
|
|
return $errorCode;
|
|
}
|
|
|
|
|
|
private function getFormDataByData($data){
|
|
$form = array();
|
|
$form["participant_id"] = trim($data["id"]);
|
|
$form["expense_id"] = trim($data["expense_id"]);
|
|
$form["name"] = trim($data["name"]);
|
|
$form["is_creator"] = 0;
|
|
|
|
|
|
|
|
return $form;
|
|
}
|
|
|
|
|
|
|
|
function addParticipant($data){
|
|
|
|
|
|
// $fp = fopen("result.txt","w+");
|
|
// fwrite($fp,"in add Participant");
|
|
$result = null;
|
|
//1 = Input data format is not correct
|
|
//4 = participant already exist in database
|
|
//22 = Data not saved successfully in Database
|
|
|
|
$form = $this->getFormDataByData($data);
|
|
|
|
$dbData = $this->find('all', array('conditions' => array('Participant.expense_id' =>$form["expense_id"])));
|
|
|
|
$errorCode = $this->validateAddParticipant($dbData,$form,true);
|
|
|
|
if(empty($errorCode)){
|
|
|
|
|
|
$fileds = array('participant_id','expense_id','name','is_creator');
|
|
|
|
|
|
$dbData["Participant"] = $form;
|
|
|
|
//insert into the table group_expenses
|
|
if(trim($form["name"]) != "" && !$this->save($dbData,false,$fileds)){
|
|
$result["22"] = "Participant has not been added successfully";
|
|
}
|
|
|
|
} else {
|
|
$result[$errorCode] = __("LBL_ADD_PARTICIPANT_FAILS");
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
|
}
|
|
|
|
function addMultipleParticipant($participantList,$expense_id){
|
|
//$fp = fopen("result.txt","w+");
|
|
//fwrite($fp,"in add Multiple Participant");
|
|
$error = "";
|
|
$i = 0;
|
|
//1 = Input data format is not correct
|
|
//4 = participant already exist in database
|
|
//22 = Data not saved successfully in Database
|
|
|
|
if(!empty($participantList)){
|
|
|
|
foreach($participantList as $key=>$value){
|
|
$value["id"] = $value["participant_id"]; //2=mobile
|
|
$value["expense_id"] = $expense_id;
|
|
|
|
if(!empty($value)){
|
|
$form["Participant"][$i] = $this->getFormDataByData($value);
|
|
$form["Participant"][$i]["id"] = "";
|
|
$i = $i + 1;
|
|
}
|
|
}
|
|
|
|
//echo "<pre>";
|
|
// print_r($form["Participant"]);exit;
|
|
|
|
if($i>0 && !$this->saveMany($form["Participant"])){
|
|
$error = "22";
|
|
}
|
|
|
|
}
|
|
|
|
|
|
return $error;
|
|
|
|
|
|
}
|
|
|
|
function modifyParticipant($data){
|
|
|
|
|
|
//$fp = fopen("result.txt","w+");
|
|
// fwrite($fp,"in Modify Participant");
|
|
|
|
$result = null;
|
|
//1 = Input data format is not correct
|
|
//4 = participant already exist in database
|
|
//22 = Data not saved successfully in Database
|
|
|
|
$form = $this->getFormDataByData($data);
|
|
$dbData = $this->find('all', array('conditions' => array('Participant.expense_id' =>$form["expense_id"])));
|
|
|
|
$errorCode = $this->validateAddParticipant($dbData,$form,false);
|
|
|
|
if(empty($errorCode)){
|
|
|
|
|
|
$fileds = array('name','version');
|
|
|
|
//get participant ID
|
|
$form['id'] = "";
|
|
foreach($dbData as $key=>$value){
|
|
if(trim($value["Participant"]["participant_id"]) == $form["participant_id"]){
|
|
$form['id'] = trim($value["Participant"]["id"]);
|
|
$form['version'] = trim($value["Participant"]["version"]) + 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
$saveData["Participant"] = $form;
|
|
|
|
//insert into the table group_expenses
|
|
if(trim($form["name"]) != "" && !$this->save($saveData,false,$fileds)){
|
|
$result["22"] = __("LBL_PARTICIPANT_NOT_UPDATED");
|
|
}
|
|
|
|
} else {
|
|
$result[$errorCode] = __("LBL_PARTICIPANT_FAILS");
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
|
}
|
|
|
|
|
|
function modifyMultipleParticipant($participantList){
|
|
|
|
//$fp = fopen("result.txt","w+");
|
|
// fwrite($fp,"in Modify Multiple Participant");
|
|
|
|
$error = "";
|
|
$i = 0;
|
|
//1 = Input data format is not correct
|
|
//4 = participant already exist in database
|
|
//22 = Data not saved successfully in Database
|
|
|
|
if(!empty($participantList)){
|
|
|
|
//echo "participant List<br/><pre>";
|
|
//print_r($participantList);
|
|
//exit;
|
|
|
|
foreach($participantList as $key=>$value){
|
|
//$value["id"] = ""; //2=mobile
|
|
//$value["expense_id"] = $expense_id;
|
|
if(trim($value["name"]) != ""){
|
|
$form["Participant"][$i]["id"] = $value["id"];
|
|
$form["Participant"][$i]["name"] = $value["name"];
|
|
$form["Participant"][$i]["version"] = $value["version"]+1;
|
|
|
|
$i = $i + 1;
|
|
}
|
|
}
|
|
|
|
if($i > 0 && !$this->saveMany($form["Participant"])){
|
|
$error = "22";
|
|
}
|
|
|
|
}
|
|
|
|
|
|
return $error;
|
|
}
|
|
|
|
function deleteMultipleParticipant($participantList){
|
|
|
|
$error = "";
|
|
$i = 0;
|
|
//1 = Input data format is not correct
|
|
//4 = participant already exist in database
|
|
//22 = Data not saved successfully in Database
|
|
|
|
if(!empty($participantList)){
|
|
$participantIdList = array();
|
|
foreach($participantList as $key=>$value){
|
|
//$value["id"] = ""; //2=mobile
|
|
//$value["expense_id"] = $expense_id;
|
|
$participantIdList[] = $value["id"];
|
|
}
|
|
|
|
if(!$this->deleteAll(array('Participant.id' => $participantIdList))){
|
|
$error = "22";
|
|
}
|
|
|
|
}
|
|
|
|
|
|
return $error;
|
|
}
|
|
|
|
|
|
function deleteParticpant($data){
|
|
|
|
|
|
|
|
$result = null;
|
|
$form = $this->getFormDataByData($data);
|
|
|
|
//retrieve the existing expense from DB
|
|
App::import('model','GroupExpense');
|
|
$GroupExpense = new GroupExpense();
|
|
$dbExpense = $GroupExpense->find('all',
|
|
array('conditions' =>
|
|
array('GroupExpense.expense_id =' => $form["expense_id"])));
|
|
|
|
|
|
|
|
//if(empty($dbExpense)){
|
|
//if expense not available in DB
|
|
// $result["3"] = "Group Expense not available in database";
|
|
//} else {
|
|
|
|
//validate if any of the paid by/amount str has the participant id
|
|
$validation_result = true;
|
|
|
|
//check participant has any expense or not
|
|
if(!empty($dbExpense)){
|
|
|
|
foreach($dbExpense as $key=>$value){
|
|
//check paid by
|
|
if(trim($value['GroupExpense']['paid_by_participant_id']) == $form['participant_id']) {
|
|
$validation_result = false;
|
|
break;
|
|
}
|
|
|
|
//check amount String
|
|
$amountStr = trim($value['GroupExpense']['participant_amount']);
|
|
|
|
$amountStrArray = explode(',',$amountStr);
|
|
foreach($amountStrArray as $seq=>$item){
|
|
$participantIdStrArray = explode("|",$item);
|
|
|
|
if(trim($participantIdStrArray[0]) == $form['participant_id']) {
|
|
$validation_result = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if($validation_result == false)
|
|
break;
|
|
|
|
}
|
|
}
|
|
|
|
//check participant has any meal or not
|
|
if($validation_result == true){
|
|
|
|
App::import('model','Meal');
|
|
$Meal = new Meal();
|
|
$dbMeal = $Meal->find('all',
|
|
array('conditions' => array('Meal.expense_id =' => $form["expense_id"])));
|
|
|
|
if(!empty($dbMeal)){
|
|
|
|
foreach($dbMeal as $key=>$value){
|
|
|
|
//check amount String
|
|
$mealStr = trim($value['Meal']['meal_amount']);
|
|
|
|
$mealStrArray = explode(',',$mealStr);
|
|
foreach($mealStrArray as $seq=>$item){
|
|
$participantIdStrArray = explode("|",$item);
|
|
|
|
if(trim($participantIdStrArray[0]) == $form['participant_id']) {
|
|
$validation_result = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if($validation_result == false)
|
|
break;
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
if($validation_result == true){
|
|
if(!$this->deleteAll(array('Participant.expense_id' => $form["expense_id"],'Participant.participant_id' => $form["participant_id"]), false)){
|
|
|
|
//data modify fails
|
|
$result["22"] = __("LBL_PARTICIPANT_NOT_DELETED");
|
|
}
|
|
} else {
|
|
$result["24"] = __("LBL_PARTICIPANT_NOT_DELETED");
|
|
}
|
|
|
|
//}
|
|
|
|
//$log = $this->getDataSource()->getLog(false, false);
|
|
//debug($log);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
function getExpenseIdListByFacebookId($facebook_id){
|
|
$expenseIdList = NULL;
|
|
|
|
|
|
if(!empty($facebook_id)){
|
|
|
|
$result = $this->find('all', array('conditions' => array(
|
|
'Participant.facebook_id' =>$facebook_id
|
|
) ));
|
|
|
|
|
|
if(!empty($result) && count($result) > 0){
|
|
foreach($result as $key=>$value){
|
|
$expenseIdList[] = $value["Participant"]["expense_id"];
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
return $expenseIdList;
|
|
}
|
|
|
|
}
|
|
?>
|