public function actionView($id) { $this->objectId = $id; $model = NetCustomer::findOne(['id' => $id, 'delete_flag' => MasterValueUtils::MV_FIN_FLG_DELETE_FALSE]); $renderView = 'view'; 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('fin.models', 'Customer')])); } else { // master value $arrCustomerStatus = MasterValueUtils::getArrData('net_customer_status'); $dataPaymentQuery = NetPayment::find()->select('t1.*, t2.bill_date')->from('net_payment t1')->leftJoin('net_bill t2', 't1.order_id = t2.id')->where(['=', 't1.delete_flag', MasterValueUtils::MV_FIN_FLG_DELETE_FALSE]); $sumPaymentQuery = (new Query())->select(['SUM(credit) AS credit, SUM(debit) AS debit'])->from('net_payment')->where(['=', 'delete_flag', MasterValueUtils::MV_FIN_FLG_DELETE_FALSE]); $dataPaymentQuery->andWhere(['=', 't1.customer_id', $model->id]); $sumPaymentQuery->andWhere(['=', 'customer_id', $model->id]); $dataPaymentQuery->orderBy('entry_date DESC, update_date DESC'); $sumPaymentValue = $sumPaymentQuery->createCommand()->queryOne(); // data for rendering $renderData = ['model' => $model, 'arrCustomerStatus' => $arrCustomerStatus, 'dataPaymentQuery' => $dataPaymentQuery, 'sumPaymentValue' => $sumPaymentValue]; } // render GUI return $this->render($renderView, $renderData); }
/** * 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; }
/** * 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; }