Beispiel #1
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getBillPersonalQuotes()
 {
     return $this->hasMany(BillPersonalQuotes::className(), ['bill_person_id' => 'id']);
 }
 /**
  * Displays a single Person model.
  * @param string $id
  * @return mixed
  */
 public function actionView($id)
 {
     if (isset($_POST['hasEditable']) && $_POST['hasEditable'] == 1) {
         \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
         $bill = $_POST['bill'];
         $person = $_POST['person'];
         $billPerson = BillPersonal::find()->where(['bill_id' => $bill, 'personal_id' => $person])->one();
         if ($billPerson == null) {
             $billPerson = new BillPersonal();
             $billPerson->bill_id = $bill;
             $billPerson->personal_id = $person;
             $billPersonAmount = isset($_POST['bp_amount']) ? $_POST['bp_amount'] : 0.0;
             $billPersonDescription = isset($_POST['bp_description']) ? $_POST['bp_description'] : "";
         } else {
             $billPersonAmount = isset($_POST['bp_amount']) ? $_POST['bp_amount'] : $billPerson->amount;
             $billPersonDescription = isset($_POST['bp_description']) ? $_POST['bp_description'] : $billPerson->description;
         }
         $billPerson->description = $billPersonDescription;
         $billPerson->amount = $billPersonAmount;
         if (!$billPerson->save()) {
             return ['output' => '', 'message' => print_r($billPerson->errors, true)];
         } else {
             if (isset($_POST['split-bill']) && $_POST['split-bill'] == 1) {
                 $n_quotes = 2;
                 for ($i = 0; $i < $n_quotes; $i++) {
                     $quote = new BillPersonalQuotes();
                     $quote->amount = (double) $billPerson->amount / (double) $n_quotes;
                     $quote->bill_person_id = $billPerson->id;
                     if (!$quote->save()) {
                         return ['output' => '', 'message' => print_r($quote->errors, true)];
                     }
                 }
             }
             if (isset($_POST['bp_amount'])) {
                 return ['output' => $billPersonAmount, 'message' => ''];
             } elseif (isset($_POST['bp_description'])) {
                 return ['output' => $billPersonDescription, 'message' => ''];
             }
         }
         return ['output' => '', 'message' => 'Validation error'];
     }
     $total = 0.0;
     if (isset($_POST['selected'], $_GET['id'])) {
         $bill_ids = $_POST['selected'];
         $billPersonals = BillPersonal::find()->where(['bill_id' => $bill_ids, 'personal_id' => $_GET['id']])->all();
         if (isset($billPersonals)) {
             $saveMode = isset($_POST['mode']) && $_POST['mode'] == 1;
             if ($saveMode) {
                 $transaction = BillPersonal::getDb()->beginTransaction();
             }
             try {
                 foreach ($billPersonals as $billPersonal) {
                     $total += $billPersonal->amount;
                     if ($saveMode) {
                         if ($billPersonal->paid == 0) {
                             $billPersonal->paid = 1;
                         } else {
                             $billPersonal->paid = 1;
                         }
                         if (!$billPersonal->save()) {
                             throw new Exception(print_r($billPersonal->errors, true));
                         }
                     }
                 }
                 if ($saveMode) {
                     $transaction->commit();
                 }
             } catch (\Exception $e) {
                 if ($saveMode) {
                     $transaction->rollBack();
                 }
             }
         }
     }
     $billSearchModel = new BillSearch();
     $billDataProvider = $billSearchModel->searchWithPerson(Yii::$app->request->queryParams);
     return $this->render('view', ['billSearchModel' => $billSearchModel, 'billDataProvider' => $billDataProvider, 'model' => $this->findModel($id), 'total' => $total]);
 }