Files
HIS-GUI/gui/dialogs/payment_dialog.cpp
2026-04-07 21:30:39 +08:00

209 lines
6.6 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "payment_dialog.h"
#include "core/his_core.h"
#include "models/payment.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QDoubleSpinBox>
#include <QTextEdit>
#include <QPushButton>
#include <QMessageBox>
#include <ctime>
PaymentDialog::PaymentDialog(core::HisCore& core, QWidget* parent)
: QDialog(parent), core_(core), paymentSuccessful_(false) {
setWindowTitle("支付");
setModal(true);
setMinimumWidth(500);
setupUI();
}
void PaymentDialog::setupUI() {
QVBoxLayout* mainLayout = new QVBoxLayout(this);
// Patient Info Section
QHBoxLayout* patientLayout = new QHBoxLayout();
patientLayout->addWidget(new QLabel("患者ID:"));
patientIDEdit_ = new QLineEdit();
patientIDEdit_->setReadOnly(true);
patientLayout->addWidget(patientIDEdit_);
patientLayout->addWidget(new QLabel("患者姓名:"));
patientNameEdit_ = new QLineEdit();
patientNameEdit_->setReadOnly(true);
patientLayout->addWidget(patientNameEdit_);
mainLayout->addLayout(patientLayout);
// Operation Info Section
QHBoxLayout* operationLayout = new QHBoxLayout();
operationLayout->addWidget(new QLabel("操作类型:"));
operationTypeEdit_ = new QLineEdit();
operationTypeEdit_->setReadOnly(true);
operationLayout->addWidget(operationTypeEdit_);
mainLayout->addLayout(operationLayout);
// Amount Section
QHBoxLayout* amountLayout = new QHBoxLayout();
amountLayout->addWidget(new QLabel("支付金额(¥):"));
amountSpinBox_ = new QDoubleSpinBox();
amountSpinBox_->setReadOnly(true);
amountSpinBox_->setMinimum(0.0);
amountSpinBox_->setMaximum(999999.99);
amountSpinBox_->setDecimals(2);
amountLayout->addWidget(amountSpinBox_);
mainLayout->addLayout(amountLayout);
// Payment Method Section
QHBoxLayout* methodLayout = new QHBoxLayout();
methodLayout->addWidget(new QLabel("支付方式:"));
paymentMethodCombo_ = new QComboBox();
paymentMethodCombo_->addItem("现金", 0);
paymentMethodCombo_->addItem("信用卡", 1);
paymentMethodCombo_->addItem("移动支付", 2);
paymentMethodCombo_->addItem("医保", 3);
methodLayout->addWidget(paymentMethodCombo_);
connect(paymentMethodCombo_, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &PaymentDialog::onPaymentMethodChanged);
mainLayout->addLayout(methodLayout);
// Description Section
mainLayout->addWidget(new QLabel("备注:"));
descriptionEdit_ = new QTextEdit();
descriptionEdit_->setMaximumHeight(80);
mainLayout->addWidget(descriptionEdit_);
// Balance Label
balanceLabel_ = new QLabel("余额: ¥0.00");
mainLayout->addWidget(balanceLabel_);
// Button Section
QHBoxLayout* buttonLayout = new QHBoxLayout();
confirmButton_ = new QPushButton("确认支付");
cancelButton_ = new QPushButton("取消");
connect(confirmButton_, &QPushButton::clicked, this, &PaymentDialog::onConfirmPayment);
connect(cancelButton_, &QPushButton::clicked, this, &PaymentDialog::onCancel);
buttonLayout->addStretch();
buttonLayout->addWidget(confirmButton_);
buttonLayout->addWidget(cancelButton_);
mainLayout->addLayout(buttonLayout);
setLayout(mainLayout);
}
void PaymentDialog::setPaymentInfo(const QString& patientID,
const QString& patientName,
const QString& operationType,
double amount) {
patientIDEdit_->setText(patientID);
patientNameEdit_->setText(patientName);
operationTypeEdit_->setText(operationType);
amountSpinBox_->setValue(amount);
}
void PaymentDialog::onPaymentMethodChanged(int index) {
// 根据支付方式更新余额或其他信息
QString method;
switch (index) {
case 0:
method = "现金";
break;
case 1:
method = "信用卡";
break;
case 2:
method = "移动支付";
break;
case 3:
method = "医保";
break;
default:
method = "未知";
}
// 可以在此处根据支付方式的不同进行额外处理
}
void PaymentDialog::onConfirmPayment() {
if (!validateInput()) {
return;
}
std::string patientID = patientIDEdit_->text().toStdString();
std::string operationType = operationTypeEdit_->text().toStdString();
double amount = amountSpinBox_->value();
PaymentMethod method = PaymentMethod::Cash;
int methodIndex = paymentMethodCombo_->currentIndex();
switch (methodIndex) {
case 0:
method = PaymentMethod::Cash;
break;
case 1:
method = PaymentMethod::CreditCard;
break;
case 2:
method = PaymentMethod::MobilePayment;
break;
case 3:
method = PaymentMethod::HealthInsurance;
break;
}
std::string description = descriptionEdit_->toPlainText().toStdString();
// 创建支付记录
std::string outPaymentID;
if (core_.paymentService.createPayment(patientID, amount, method, operationType,
"", // operationID will be set separately
description, outPaymentID)) {
// 完成支付
if (core_.paymentService.completePayment(outPaymentID)) {
paymentID_ = QString::fromStdString(outPaymentID);
paymentSuccessful_ = true;
QMessageBox::information(this, "支付成功", "支付完成支付ID: " + paymentID_);
accept();
} else {
core_.paymentService.removePayment(outPaymentID);
QMessageBox::warning(this, "支付失败", "无法完成支付,请重试");
}
} else {
QMessageBox::warning(this, "支付失败", "无法创建支付记录,请重试");
}
}
void PaymentDialog::onCancel() {
paymentSuccessful_ = false;
reject();
}
bool PaymentDialog::validateInput() {
if (patientIDEdit_->text().isEmpty()) {
QMessageBox::warning(this, "验证失败", "患者ID不能为空");
return false;
}
if (amountSpinBox_->value() <= 0) {
QMessageBox::warning(this, "验证失败", "支付金额必须大于0");
return false;
}
return true;
}
std::string PaymentDialog::paymentMethodToString(int index) {
switch (index) {
case 0:
return "Cash";
case 1:
return "CreditCard";
case 2:
return "MobilePayment";
case 3:
return "HealthInsurance";
default:
return "Cash";
}
}