initial commit
This commit is contained in:
450
app/Model/Meal.php
Normal file
450
app/Model/Meal.php
Normal file
@@ -0,0 +1,450 @@
|
||||
<?php
|
||||
class Meal extends AppModel
|
||||
{
|
||||
var $name='Meal';
|
||||
//1 = meal already has been added
|
||||
//2 = participant in this Meal is deleted by other user or not available
|
||||
|
||||
|
||||
|
||||
private function validateAddMeal($data){
|
||||
|
||||
$errorCode = "";
|
||||
|
||||
$checkParticipantList = array();
|
||||
|
||||
$form = $this->getFormDataByData($data);
|
||||
|
||||
|
||||
//check the participant involes in the meal are available or not
|
||||
if($errorCode == "" ){
|
||||
|
||||
|
||||
$participantAmtStrArray = explode(",",$form["meal_amount"]);
|
||||
foreach($participantAmtStrArray as $key=>$value){
|
||||
$parts = explode("|",$value);
|
||||
array_push($checkParticipantList,$parts[0]);
|
||||
}
|
||||
|
||||
//make array unique
|
||||
//$checkParticipantList = array_unique($checkParticipantList);
|
||||
|
||||
//retrieve participant from DB
|
||||
$dbParticipantList = $this->getParticipantIdListByExpId($form['expense_id']);
|
||||
|
||||
//retrieve participants from DB
|
||||
//print_r($checkParticipantList);exit;
|
||||
|
||||
foreach($checkParticipantList as $index=>$item){
|
||||
if (!in_array($item, $dbParticipantList)) {
|
||||
$errorCode = "2";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $errorCode;
|
||||
}
|
||||
|
||||
|
||||
private function validateEditMeal($data){
|
||||
$errorCode= $this->validateAddMeal($data);
|
||||
|
||||
//check the meal object exist or not in DB
|
||||
//check the new participant involves in meal exist in participant table or not
|
||||
|
||||
|
||||
if(empty($errorCode)){
|
||||
//check the expense is in DB or not
|
||||
|
||||
}
|
||||
|
||||
return $errorCode;
|
||||
}
|
||||
|
||||
public function getMealLog($data){
|
||||
|
||||
$expense_id = $data["expense_id"];
|
||||
$frommDate = $data["fromDate"]." 00:00:00";
|
||||
/* $date = new DateTime($frommDate);
|
||||
$frommDate = $date->format('Y-m-d'); */
|
||||
|
||||
$toDate = date("Y-m-d", strtotime("+1 day", strtotime($data["toDate"])));
|
||||
/* $date = new DateTime($toDate);
|
||||
$toDate = $date->format('Y-m-d'); */
|
||||
|
||||
App::import('model','Log');
|
||||
$log = new Log();
|
||||
$result = $log->find('all', array('conditions' => array(
|
||||
'Log.expense_id' =>$expense_id,
|
||||
'Log.create_date >= ' => $frommDate,
|
||||
'Log.create_date <= ' => $toDate
|
||||
),
|
||||
'order' => 'Log.create_date DESC'));
|
||||
// $log = $this->getDataSource()->getLog(false, false);
|
||||
// $fp = fopen("testResult.txt","a+");
|
||||
// fwrite($fp,$frommDate."=".$toDate);
|
||||
// debug($log);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private function getParticipantIdListByExpId($expense_id){
|
||||
App::import('model','Participant');
|
||||
$participant = new Participant();
|
||||
$dbParticipantIdList = array();
|
||||
$data = $participant->find('all',
|
||||
array('conditions' =>
|
||||
array('Participant.expense_id =' => $expense_id)));
|
||||
foreach($data as $index=>$object){
|
||||
array_push($dbParticipantIdList,$object["Participant"]["participant_id"]);
|
||||
}
|
||||
|
||||
return $dbParticipantIdList;
|
||||
}
|
||||
|
||||
public function addMeal($data){
|
||||
|
||||
$result = null;
|
||||
//check the any of the involves participant(s) has been deleted or not
|
||||
//check account is closed already or not
|
||||
//update participant's balance
|
||||
//22 = Data not saved successfully in Database
|
||||
|
||||
$form = $this->getFormDataByData($data);
|
||||
|
||||
//check already record exist in DB for this date or not
|
||||
$dbMeal = $this->find('all',
|
||||
array('conditions' =>
|
||||
array('Meal.expense_id =' => $form["expense_id"],'Meal.date_new =' => $form["date_new"])));
|
||||
|
||||
if(!empty($dbMeal)){
|
||||
$errorCode = "1";
|
||||
} else {
|
||||
$errorCode = $this->validateAddMeal($data);
|
||||
}
|
||||
|
||||
|
||||
if(empty($errorCode)){
|
||||
|
||||
/*
|
||||
|
||||
$fp = fopen("result.txt",'a+');
|
||||
fwrite($fp,implode(" ",$form));*/
|
||||
|
||||
$fileds = array('expense_id','date','date_new','update_date','meal_amount','version');
|
||||
$dbData["Meal"] = $form;
|
||||
|
||||
if(!$this->save($dbData,false,$fileds)){
|
||||
$result["22"] = __("LBL_MEAL_NOT_SAVED");
|
||||
}
|
||||
} else {
|
||||
$result[$errorCode] = __("LBL_ADD_MEAL_FAILS");
|
||||
}
|
||||
|
||||
|
||||
|
||||
return $result;
|
||||
|
||||
}
|
||||
|
||||
public function addMultipleMeal($mealList,$expense_id){
|
||||
|
||||
$error = "";
|
||||
//check the any of the involves participant(s) has been deleted or not
|
||||
//check account is closed already or not
|
||||
//update participant's balance
|
||||
//22 = Data not saved successfully in Database
|
||||
|
||||
$i = 0;
|
||||
if(!empty($mealList)){
|
||||
foreach( $mealList as $key=>$value){
|
||||
$value["source"] = "2"; //2=mobile
|
||||
$value["expense_id"] = $expense_id;
|
||||
$form["Meal"][$i] = $this->getFormDataByData($value);
|
||||
|
||||
$i = $i + 1;
|
||||
}
|
||||
|
||||
if(!$this->saveMany($form["Meal"])){
|
||||
$error = "22";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $error;
|
||||
}
|
||||
|
||||
private function getFormDataByData($data){
|
||||
$form = array();
|
||||
//$form["id"] = $data["id"];
|
||||
$form["expense_id"] = $data["expense_id"];
|
||||
if(isset($data["date_old"])){
|
||||
$form["date"] = $data["date_old"];
|
||||
}
|
||||
|
||||
$form["date_new"] = $data["date"];
|
||||
$form["meal_amount"] = $data["meal_amount"];
|
||||
|
||||
$form["version"] = 0;
|
||||
|
||||
$form["update_date"] = $this->getSystemCurrentIntTime();
|
||||
|
||||
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
|
||||
public function modifyMultipleMeal($expenseList,$expenseId){
|
||||
|
||||
$error = "";
|
||||
//check the any of the involves participant(s) has been deleted or not
|
||||
//check account is closed already or not
|
||||
//update participant's balance
|
||||
//22 = Data not saved successfully in Database
|
||||
|
||||
$i = 0;
|
||||
|
||||
foreach( $expenseList as $key=>$value){
|
||||
$value["expense_id"] = $expenseId;
|
||||
$version = $value["version"];
|
||||
$form["Meal"][$i] = $this->getFormDataByData($value);
|
||||
$form["Meal"][$i]['version'] = ($version)+1;
|
||||
$form["Meal"][$i]['id'] = $value["id"];
|
||||
|
||||
/*//set log List
|
||||
$logList["Log"][$i]['expense_id'] = $form["GroupExpense"][$i]["expense_id"];
|
||||
$logList["Log"][$i]['previous_desc'] = $form["GroupExpense"][$i]["previous_desc"];
|
||||
$logList["Log"][$i]['new_desc'] = $form["GroupExpense"][$i]["new_desc"];
|
||||
$logList["Log"][$i]['previous_paid_by'] = $form["GroupExpense"][$i]["previous_paid_by"]; */
|
||||
|
||||
$i++;
|
||||
}
|
||||
|
||||
|
||||
/*App::import('model','Log');
|
||||
$log = new Log();*/
|
||||
|
||||
|
||||
//check the any of the involves participant(s) has been deleted or not
|
||||
//check account is closed already or not
|
||||
//update participant's balance
|
||||
//3 = group expense not available in database;
|
||||
//22 = Data not saved successfully in Database
|
||||
//23 = Log insertion fail
|
||||
|
||||
//increase number of version
|
||||
|
||||
|
||||
//check similar like Add expense
|
||||
//$errorCode = $this->validateEditExpense($data);
|
||||
|
||||
|
||||
if(!$this->saveMany($form["Meal"])){
|
||||
//data modify fails
|
||||
$error = "22";
|
||||
} else {
|
||||
//data modify successfull.hence insert logs
|
||||
|
||||
/*if(!$log->saveMany($logList["Log"])){
|
||||
//data modify fails
|
||||
$error = "23";
|
||||
}*/
|
||||
}
|
||||
|
||||
|
||||
return $error;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function modifyMeal($data){
|
||||
|
||||
//check the any of the involves participant(s) has been deleted or not
|
||||
//check account is closed already or not
|
||||
//update participant's balance
|
||||
//3 = group expense not available in database;
|
||||
//22 = Data not saved successfully in Database
|
||||
//23 = Log insertion fail
|
||||
|
||||
$result = null;
|
||||
$form = $this->getFormDataByData($data);
|
||||
|
||||
//retrieve the existing expense from DB
|
||||
|
||||
$dbMeal = $this->find('all',
|
||||
array('conditions' =>
|
||||
array('Meal.expense_id =' => $form["expense_id"],'Meal.date_new =' => $data["old_date"])));
|
||||
|
||||
|
||||
if(empty($dbMeal)){
|
||||
//if expense not available in DB
|
||||
$result["3"] = __("LBL_MEAL_NOT_AVAILABLE");
|
||||
} else {
|
||||
|
||||
//increase number of version
|
||||
$form['version'] = ($dbMeal["0"]["Meal"]["version"])+1;
|
||||
$form["id"] = $dbMeal["0"]["Meal"]["id"];
|
||||
|
||||
//check similar like Add expense
|
||||
$errorCode = $this->validateEditMeal($data);
|
||||
|
||||
if(empty($errorCode)){
|
||||
|
||||
$fileds = array('id','expense_id','date','date_new','update_date','meal_amount','version');
|
||||
$data["Meal"] = $form;
|
||||
|
||||
if(!$this->save($data,false,$fileds)){
|
||||
//data modify fails
|
||||
$result["22"] = __("LBL_DATA_SAVED_SUCCESSFULLY_DATABASE");
|
||||
} else {
|
||||
//data modify successfull.hence insert logs
|
||||
/*App::import('model','Log');
|
||||
$log = new Log();
|
||||
|
||||
$fileds = array('expense_id','previous_paid_by','previous_desc','new_desc');
|
||||
$logData['Log'] = $this->getEditLogData($form);
|
||||
if(!$log->save($logData,false,$fileds)){
|
||||
//data modify fails
|
||||
$result["23"] = "Log insertion fail";
|
||||
}*/
|
||||
}
|
||||
} else {
|
||||
//Found error
|
||||
$result[$errorCode] = __("LBL_EDIT_MEAL_FAILS");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
private function getEditLogData($form){
|
||||
$result = array();
|
||||
|
||||
$result['expense_id'] = $form['expense_id'];
|
||||
$result['previous_desc'] = $form['previous_desc'];
|
||||
$result['new_desc'] = $form['new_desc'];
|
||||
$result['previous_paid_by'] = $form['previous_paid_by'];
|
||||
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
public function deleteMultipleMeal($expenseList,$expenseId){
|
||||
|
||||
$error = "";
|
||||
//check the any of the involves participant(s) has been deleted or not
|
||||
//check account is closed already or not
|
||||
//update participant's balance
|
||||
//22 = Data not saved successfully in Database
|
||||
|
||||
$i = 0;
|
||||
|
||||
foreach( $expenseList as $key=>$value){
|
||||
$mealIdList[] = $value["id"];
|
||||
//set log List
|
||||
/*$logList["Log"][$i]['expense_id'] = $expenseId;
|
||||
$logList["Log"][$i]['previous_desc'] = $value["previous_desc"];
|
||||
$logList["Log"][$i]['new_desc'] = $value["new_desc"];
|
||||
$logList["Log"][$i]['previous_paid_by'] = $value["previous_paid_by"];
|
||||
|
||||
$i++;*/
|
||||
}
|
||||
|
||||
/*
|
||||
App::import('model','Log');
|
||||
$log = new Log();
|
||||
*/
|
||||
|
||||
//check the any of the involves participant(s) has been deleted or not
|
||||
//check account is closed already or not
|
||||
//update participant's balance
|
||||
//3 = group expense not available in database;
|
||||
//22 = Data not saved successfully in Database
|
||||
//23 = Log insertion fail
|
||||
|
||||
//increase number of version
|
||||
|
||||
|
||||
//check similar like Add expense
|
||||
//$errorCode = $this->validateEditExpense($data);
|
||||
|
||||
|
||||
if(!$this->deleteAll(array('Meal.id' => $mealIdList))){
|
||||
//data modify fails
|
||||
$error = "22";
|
||||
} else {
|
||||
//data modify successfull.hence insert logs
|
||||
|
||||
/*if(!$log->saveMany($logList["Log"])){
|
||||
//data modify fails
|
||||
$error = "23";
|
||||
}*/
|
||||
}
|
||||
|
||||
|
||||
return $error;
|
||||
|
||||
}
|
||||
|
||||
public function deleteMeal($data){
|
||||
|
||||
$result = null;
|
||||
$form = $this->getFormDataByData($data);
|
||||
|
||||
// $fp = fopen("result.txt",'w+');
|
||||
// fwrite($fp,$form["expense_id"]."==".$form["date"]);exit;
|
||||
|
||||
//retrieve the existing expense from DB
|
||||
|
||||
$dbMeal = $this->find('all',
|
||||
array('conditions' =>
|
||||
array('Meal.expense_id =' => $form["expense_id"],'Meal.date_new =' => $form["date_new"])));
|
||||
|
||||
//$fp = fopen("result.txt",'w+');
|
||||
// fwrite($fp,implode(" ",$dbMeal));
|
||||
|
||||
|
||||
|
||||
if(empty($dbMeal)){
|
||||
//if expense not available in DB
|
||||
$result["3"] = __("LBL_MEAL_NOT_AVAILABLE");
|
||||
} else {
|
||||
$form["id"] = $dbMeal["0"]["Meal"]["id"];
|
||||
if(!$this->delete(array('id'=>$form["id"]))){
|
||||
|
||||
//data modify fails
|
||||
$result["22"] = __("LBL_MEAL_NOT_DELETED");
|
||||
} else {
|
||||
|
||||
//data modify successfull.hence insert logs
|
||||
/*App::import('model','Log');
|
||||
$log = new Log();
|
||||
|
||||
$fileds = array('expense_id','previous_paid_by','previous_desc','new_desc');
|
||||
$logData['Log'] = $this->getEditLogData($form);
|
||||
if(!$log->save($logData,false,$fileds)){
|
||||
//data modify fails
|
||||
$result["23"] = "Log insertion fail";
|
||||
}*/
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function getSystemCurrentIntTime(){
|
||||
date_default_timezone_set("UTC");
|
||||
return time();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user