示例#1
0
 /**
  * update a Payment
  * @param $paymentModel
  * @param $fmDateTimePhp
  * @throws Exception
  * @return string|true
  */
 private function updatePayment($paymentModel, $fmDateTimePhp)
 {
     // modify data for DB
     $paymentModel->entry_date = DateTimeUtils::parse($paymentModel->entry_date, $fmDateTimePhp, DateTimeUtils::FM_DB_DATE);
     $transaction = Yii::$app->db->beginTransaction();
     $save = true;
     $message = null;
     // begin transaction
     try {
         // save Customer
         $customer = NetCustomer::findOne(['id' => $paymentModel->customer_id]);
         if ($save !== false && !is_null($customer)) {
             $customer->balance = $customer->balance + $paymentModel->credit - $paymentModel->credit_old;
             $save = $customer->save();
         }
         // save Payment
         if ($paymentModel->entry_date == $paymentModel->entry_date_old) {
             $paymentModel->secret_key = EnDescrypt::encryptSha1($paymentModel->customer_id . $paymentModel->entry_date);
             $save = $paymentModel->save(false);
         } else {
             if ($paymentModel->debit > 0) {
                 // save new Payment
                 $newSecretKey = EnDescrypt::encryptSha1($paymentModel->customer_id . $paymentModel->entry_date);
                 $newPaymentModel = new NetPayment();
                 $newPaymentModel->customer_id = $paymentModel->customer_id;
                 $newPaymentModel->entry_date = $paymentModel->entry_date;
                 $newPaymentModel->credit = $paymentModel->credit;
                 $newPaymentModel->debit = 0;
                 $newPaymentModel->order_id = 0;
                 $newPaymentModel->secret_key = $newSecretKey;
                 $save = $newPaymentModel->save(false);
                 // save old Payment
                 if ($save !== false) {
                     $paymentModel->entry_date = $paymentModel->entry_date_old;
                     $paymentModel->credit = 0;
                     $paymentModel->secret_key = EnDescrypt::encryptSha1($paymentModel->customer_id . $paymentModel->entry_date_old);
                     $save = $paymentModel->save(false);
                     $paymentModel->secret_key = $newSecretKey;
                 }
             } else {
                 $paymentModel->secret_key = EnDescrypt::encryptSha1($paymentModel->customer_id . $paymentModel->entry_date);
                 $save = $paymentModel->save(false);
             }
         }
     } catch (Exception $e) {
         $save = false;
         $message = Yii::t('common', 'Unable to save {record}.', ['record' => Yii::t('fin.models', 'Payment')]);
     }
     // end transaction
     try {
         if ($save === false) {
             $transaction->rollback();
             return $message;
         } else {
             $transaction->commit();
         }
     } catch (Exception $e) {
         throw Exception(Yii::t('common', 'Unable to excute Transaction.'));
     }
     return true;
 }
示例#2
0
 /**
  * update a Bill
  * @param $model
  * @param $arrBillDetail
  * @param $fmShortDatePhp
  * @throws Exception
  * @return string|true
  */
 private function updateBill($model, $arrBillDetail, $fmShortDatePhp)
 {
     // modify data for DB
     $model->bill_date = DateTimeUtils::parse($model->bill_date, $fmShortDatePhp, DateTimeUtils::FM_DB_DATE);
     $model->member_num = count($model->arr_member_list);
     $model->member_list = serialize($model->arr_member_list);
     $pricePerMember = NumberUtils::rounds($model->total / $model->member_num, NumberUtils::NUM_CEIL);
     $pricePerMemberOld = NumberUtils::rounds($model->total_old / $model->member_num_old, NumberUtils::NUM_CEIL);
     foreach ($arrBillDetail as $billDetail) {
         if (!$billDetail->delete_flag) {
             $billDetail->pay_date = DateTimeUtils::parse($billDetail->pay_date, $fmShortDatePhp, DateTimeUtils::FM_DB_DATE);
         }
     }
     $transaction = Yii::$app->db->beginTransaction();
     $save = true;
     $message = null;
     // begin transaction
     try {
         // save Bill
         $save = $model->save(false);
         // save Bill Detail
         if ($save !== false) {
             // delete all Bill Detail
             NetBillDetail::deleteAll(['bill_id' => $model->id]);
             $itemNo = 1;
             foreach ($arrBillDetail as $billDetail) {
                 if (!$billDetail->delete_flag) {
                     $billDetail->setIsNewRecord(true);
                     $billDetail->bill_id = $model->id;
                     $billDetail->item_no = $itemNo;
                     $itemNo++;
                     $save = $billDetail->save(false);
                     if ($save === false) {
                         break;
                     }
                 }
             }
         }
         // save Customer & Payment
         if ($save !== false) {
             foreach ($model->arr_member_list_old as $oldCustomerId) {
                 // save old Customer
                 $oldCustomer = NetCustomer::findOne($oldCustomerId);
                 $oldCustomer->balance = $oldCustomer->balance + $pricePerMemberOld;
                 $save = $oldCustomer->save(false);
                 if ($save === false) {
                     break;
                 }
                 // save old Payment
                 $oldPayment = NetPayment::findOne(['customer_id' => $oldCustomerId, 'order_id' => $model->id]);
                 if (!is_null($oldPayment)) {
                     if ($oldPayment->credit > 0) {
                         $oldPayment->debit = 0;
                         $oldPayment->order_id = 0;
                         $save = $oldPayment->save(false);
                     } else {
                         $save = $oldPayment->delete();
                     }
                     if ($save === false) {
                         break;
                     }
                 }
             }
             if ($save !== false) {
                 foreach ($model->arr_member_list as $customerId) {
                     // save new Customer
                     $customer = NetCustomer::findOne($customerId);
                     $customer->balance = $customer->balance - $pricePerMember;
                     $save = $customer->save(false);
                     if ($save === false) {
                         break;
                     }
                     // save new Payment
                     $payment = NetPayment::findOne(['customer_id' => $customerId, 'entry_date' => $model->bill_date]);
                     if (is_null($payment)) {
                         $payment = new NetPayment();
                         $payment->customer_id = $customerId;
                         $payment->entry_date = $model->bill_date;
                     }
                     $payment->debit = $pricePerMember;
                     $payment->order_id = $model->id;
                     $payment->secret_key = EnDescrypt::encryptSha1($payment->customer_id . $payment->entry_date);
                     $save = $payment->save(false);
                     if ($save === false) {
                         break;
                     }
                 }
             }
         }
     } catch (Exception $e) {
         $save = false;
         $message = Yii::t('common', 'Unable to save {record}.', ['record' => Yii::t('fin.models', 'Bill')]);
     }
     // end transaction
     try {
         if ($save === false) {
             $transaction->rollback();
             return $message;
         } else {
             $transaction->commit();
         }
     } catch (Exception $e) {
         throw Exception(Yii::t('common', 'Unable to excute Transaction.'));
     }
     return true;
 }
示例#3
0
 public function actionUpdate($id)
 {
     $this->objectId = $id;
     $model = NetCustomer::findOne(['id' => $id, 'delete_flag' => MasterValueUtils::MV_FIN_FLG_DELETE_FALSE]);
     $renderView = 'update';
     if (is_null($model)) {
         $model = false;
         $renderData = ['model' => $model];
         Yii::$app->session->setFlash(MasterValueUtils::FLASH_ERROR, Yii::t('common', 'The requested {record} does not exist.', ['record' => Yii::t('net.models', 'Customer')]));
     } else {
         // master value
         $arrCustomerStatus = MasterValueUtils::getArrData('net_customer_status');
         // submit data
         $postData = Yii::$app->request->post();
         $submitMode = isset($postData[MasterValueUtils::SM_MODE_NAME]) ? $postData[MasterValueUtils::SM_MODE_NAME] : false;
         // populate model attributes with user inputs
         $model->load($postData);
         // init value
         $model->scenario = MasterValueUtils::SCENARIO_UPDATE;
         $renderData = ['model' => $model, 'arrCustomerStatus' => $arrCustomerStatus];
         switch ($submitMode) {
             case MasterValueUtils::SM_MODE_INPUT:
                 $isValid = $model->validate();
                 if ($isValid) {
                     $renderView = 'confirm';
                     $renderData['formMode'] = [MasterValueUtils::PG_MODE_NAME => MasterValueUtils::PG_MODE_EDIT];
                 }
                 break;
             case MasterValueUtils::SM_MODE_CONFIRM:
                 $isValid = $model->validate();
                 if ($isValid) {
                     $result = $this->createCustomer($model);
                     if ($result === true) {
                         Yii::$app->session->setFlash(MasterValueUtils::FLASH_SUCCESS, Yii::t('common', '{record} has been saved successfully.', ['record' => Yii::t('net.models', 'Customer')]));
                         return Yii::$app->getResponse()->redirect(Url::to(['update', 'id' => $id]));
                     } else {
                         Yii::$app->session->setFlash(MasterValueUtils::FLASH_ERROR, $result);
                         $renderView = 'confirm';
                         $renderData['formMode'] = [MasterValueUtils::PG_MODE_NAME => MasterValueUtils::PG_MODE_EDIT];
                     }
                 }
                 break;
             case MasterValueUtils::SM_MODE_BACK:
                 break;
             default:
                 break;
         }
     }
     // render GUI
     return $this->render($renderView, $renderData);
 }