/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer $id the ID of the model to be loaded * @return TbpOutputLog the loaded model * @throws CHttpException */ public function loadModel($id) { $model = TbpOutputLog::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }
/** * 新增支出 * @param type $perform */ private function createOutputData($array, $perform) { //支出金額初始值 $outputTotal = 0; // 取得LOG的類別及細項 $ary = $this->getLogItem(); $logItemArray = $ary[0]; // 細項 $logTypeArray = $ary[1]; // 類別 $logMainArray = $ary[2]; // 主項 $logSubArray = $ary[3]; // 次項 // 錯誤訊息 $errMsg = ''; $model = TbmEmpItem::model(); $transaction = $model->dbConnection->beginTransaction(); try { // 處理刪除 $checkDelete = TRUE; // 驗證 $valid = TRUE; // 刪除與取消刪除優先處理 if (isset($_POST['TbpOutputLog'])) { foreach ($_POST['TbpOutputLog'] as $j => $TbpOutputLog) { if (isset($_POST['TbpOutputLog'][$j])) { $array[$j] = new TbpOutputLog(); $array[$j]->attributes = $TbpOutputLog; $array[$j]->pdate = $perform->pdate; $array[$j]->storecode = $perform->storecode; $array[$j]->storename = $perform->storename; // 幫itemID都查詢好main跟sub if ($array[$j]->itemid != '') { $array[$j]->itemname = $logItemArray[$array[$j]->itemid]; //得到細項名稱 $array[$j]->type = $logTypeArray[$array[$j]->itemid]; //得到細項型態 $array[$j]->mainid = $logMainArray[$array[$j]->itemid]; //得到主項ID $array[$j]->subid = $logSubArray[$array[$j]->itemid]; //得到次項ID } if ($TbpOutputLog['id'] != '') { $array[$j]->id = $TbpOutputLog['id']; //當按了支出明細的刪除 if (isset($_POST[$j . 'delete'])) { // Yii::app()->user->setFlash('success', '1'); $array[$j] = TbpOutputLog::model()->findByPk($TbpOutputLog['id']); $array[$j]->opt1 = 0; $this->updateData($array[$j]); $valid = $array[$j]->save() && $valid; $checkDelete = FALSE; } elseif (isset($_POST[$j . 'cancel_del'])) { //當按了支出明細的取消刪除 $array[$j] = TbpOutputLog::model()->findByPk($TbpOutputLog['id']); $array[$j]->opt1 = 1; $this->updateData($array[$j]); $valid = $array[$j]->save() && $valid; $checkDelete = FALSE; } if (!$valid) { $errMsg = $errMsg . '第' . ($j + 2) . '筆' . CVarDumper::dumpAsString($array[$j]->getErrors() . '<br>'); } } // if($TbpOutputLog['id'] != '') { } // if (isset($_POST['TbpOutputLog'][$j])) { } //foreach ($_POST['TbpOutputLog'] as $j=>$TbpOutputLog) } if ($checkDelete && isset($_POST['send'])) { // 先針對所有的資料,做一次加總,看看總和對不對 foreach ($array as $log) { // 以 price、itemid 值判斷金額相加 if ($log->itemid != '' && $log->price > 0) { $outputTotal = $outputTotal + $log->price; //支出金額相加 } } // 檢查總計是否正確 if ($perform->output > 0) { if ($perform->output == $outputTotal) { Yii::app()->user->setFlash('success', '支出金額: ' . $perform->output . ' 與零用金明細金額: ' . $outputTotal . ' 吻合!'); } else { Yii::app()->user->setFlash('error', '支出金額: ' . $perform->output . ' 與零用金明細金額: ' . $outputTotal . ' 比對不吻合,重新輸入!!'); $valid = false; } } if ($valid) { // 先針對所有的資料,做一次加總,看看總和對不對 foreach ($array as $k => $log) { // 判斷資料是否已存在 if ($array[$k]->id != '') { $tbpLog = TbpOutputLog::model()->findByPk($array[$k]->id); if (isset($tbpLog)) { /* 不加下面這個 下面$array[$i]->save(FALSE)資料庫驗證會錯誤 */ $theSame = $array[$k]->compare($tbpLog); $array[$k]->isNewRecord = FALSE; //他是舊的,如果是true Yii會認為是新的create if (!$theSame) { $this->updateData($array[$k]); } } } else { // if($TbpOutputLog['id'] != '') $this->createData($array[$k]); } // 以 price、itemid 值判斷 if ($array[$k]->itemid != '' && $array[$k]->price > 0) { $valid = $array[$k]->validate() && $valid; if (!$valid) { $errMsg = $errMsg . '第' . ($k + 2) . '筆' . CVarDumper::dumpAsString($array[$k]->getErrors() . '<br>'); } } } if ($valid) { for ($i = 0; $i < count($array); $i++) { if (isset($array[$i]) && $array[$i]->price > 0 && $array[$i]->itemid != '') { $array[$i]->save(FALSE); } } } } if ($errMsg != '') { Yii::app()->user->setFlash('error', $errMsg); } } if ($valid) { $transaction->commit(); // Yii::app()->user->setFlash('success', "成功!"); } else { $transaction->rollback(); // Yii::app()->user->setFlash('error', $errMsg ."失敗!"); } } catch (Exception $e) { $transaction->rollback(); throw $e; } return $array; }