feat: port tauri create-service payment flow to admin session settlement
Port the tauri /files/create-service page (payment mode) to the admin SPA
and back it with real multi-part session settlement:
Backend:
- New SessionPayment entity (session_payments table): partial payments
per session with method (wallet/pos/cash/card), amount, paid_at, actor
- PatientSession: settlement discount (percent/fixed), discount_rials,
paid_at, payments relation; remaining debt derived from
final - discount - paid total
- POST /api/v1/session/{uuid}/payments: register a partial payment;
wallet method debits the patient wallet; zero remaining marks paid
- PATCH /api/v1/session/{uuid}: accepts discount_type/discount_value
(null removes) and paid_at, backward compatible
- New error codes: ERR_SESSION_PAYMENT_INVALID/_EXCEEDS,
ERR_SESSION_DISCOUNT_INVALID
- Migration + 14 functional tests (partial/full/wallet/exceed/discount)
Frontend (admin):
- SessionPaymentPage: two-step stepper (پرداخت ← جزییات) ported from
tauri AddService payment mode — service cost, settlement discount
input, Jalali payment date, wallet balance, 4-method payment accordion,
paid-list box, details summary
- SessionStepper + stepper/payment icons ported verbatim from tauri SVGs
- «تکمیل پرداخت» on SessionServiceCard now navigates to the payment page
(replaces the small settle modal on PatientDetailPage)
- Routes for patients/ and my-patients/ variants; vitest coverage
- docs/api/patient.md updated
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -54,6 +54,22 @@ class PatientSession
|
||||
#[ORM\Column(name: 'payment_method', type: 'string', length: 15)]
|
||||
private string $paymentMethod = 'pending';
|
||||
|
||||
/** نوع تخفیف تسویه: percent | fixed | null (بدون تخفیف) */
|
||||
#[ORM\Column(name: 'discount_type', type: 'string', length: 10, nullable: true)]
|
||||
private ?string $discountType = null;
|
||||
|
||||
/** مقدار خام تخفیف (درصد یا ریال، بسته به نوع) */
|
||||
#[ORM\Column(name: 'discount_value', type: 'integer')]
|
||||
private int $discountValue = 0;
|
||||
|
||||
/** مبلغ محاسبهشدهی تخفیف به ریال (سقف: مبلغ نهایی) */
|
||||
#[ORM\Column(name: 'discount_rials', type: 'integer')]
|
||||
private int $discountRials = 0;
|
||||
|
||||
/** زمان تسویهی کامل (unix)؛ تا قبل از صفر شدن بدهی null است */
|
||||
#[ORM\Column(name: 'paid_at', type: 'integer', nullable: true)]
|
||||
private ?int $paidAt = null;
|
||||
|
||||
#[ORM\Column(type: 'text', nullable: true)]
|
||||
private ?string $notes = null;
|
||||
|
||||
@@ -66,6 +82,9 @@ class PatientSession
|
||||
#[ORM\OneToMany(targetEntity: SessionService::class, mappedBy: 'session', cascade: ['remove'])]
|
||||
private Collection $services;
|
||||
|
||||
#[ORM\OneToMany(targetEntity: SessionPayment::class, mappedBy: 'session', cascade: ['remove'])]
|
||||
private Collection $payments;
|
||||
|
||||
public function __construct(PatientRecord $record, ?Appointment $appointment = null)
|
||||
{
|
||||
$this->uuid = Uuid::v4()->toRfc4122();
|
||||
@@ -74,6 +93,7 @@ class PatientSession
|
||||
$this->createdAt = time();
|
||||
$this->updatedAt = time();
|
||||
$this->services = new ArrayCollection();
|
||||
$this->payments = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function getId(): ?int { return $this->id; }
|
||||
@@ -97,6 +117,35 @@ class PatientSession
|
||||
public function getServicesTotalRials(): int { return $this->servicesTotalRials; }
|
||||
public function getFinalPriceRials(): int { return $this->finalPriceRials; }
|
||||
public function getPaymentMethod(): string { return $this->paymentMethod; }
|
||||
public function getDiscountType(): ?string { return $this->discountType; }
|
||||
public function getDiscountValue(): int { return $this->discountValue; }
|
||||
public function getDiscountRials(): int { return $this->discountRials; }
|
||||
public function getPaidAt(): ?int { return $this->paidAt; }
|
||||
public function getPayments(): Collection { return $this->payments; }
|
||||
|
||||
public function addPayment(SessionPayment $payment): self
|
||||
{
|
||||
if (!$this->payments->contains($payment)) {
|
||||
$this->payments->add($payment);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/** مجموع پرداختهای ثبتشده روی این مراجعه (ریال) */
|
||||
public function getPaidTotalRials(): int
|
||||
{
|
||||
return array_sum(array_map(
|
||||
fn(SessionPayment $p) => $p->getAmountRials(),
|
||||
$this->payments->toArray(),
|
||||
));
|
||||
}
|
||||
|
||||
/** ماندهی بدهی پس از کسر تخفیف و پرداختها؛ هرگز منفی نمیشود */
|
||||
public function getRemainingRials(): int
|
||||
{
|
||||
return max(0, $this->finalPriceRials - $this->discountRials - $this->getPaidTotalRials());
|
||||
}
|
||||
|
||||
public function getNotes(): ?string { return $this->notes; }
|
||||
public function getCreatedAt(): int { return $this->createdAt; }
|
||||
public function getUpdatedAt(): int { return $this->updatedAt; }
|
||||
@@ -109,6 +158,15 @@ class PatientSession
|
||||
public function setServicesTotalRials(int $v): self { $this->servicesTotalRials = $v; $this->updatedAt = time(); return $this; }
|
||||
public function setFinalPriceRials(int $v): self { $this->finalPriceRials = $v; $this->updatedAt = time(); return $this; }
|
||||
public function setPaymentMethod(string $v): self { $this->paymentMethod = $v; $this->updatedAt = time(); return $this; }
|
||||
public function setDiscount(?string $type, int $value, int $rials): self
|
||||
{
|
||||
$this->discountType = $type;
|
||||
$this->discountValue = $type === null ? 0 : $value;
|
||||
$this->discountRials = $type === null ? 0 : $rials;
|
||||
$this->updatedAt = time();
|
||||
return $this;
|
||||
}
|
||||
public function setPaidAt(?int $v): self { $this->paidAt = $v; $this->updatedAt = time(); return $this; }
|
||||
public function setNotes(?string $v): self { $this->notes = $v; $this->updatedAt = time(); return $this; }
|
||||
|
||||
public function toArray(): array
|
||||
@@ -128,6 +186,15 @@ class PatientSession
|
||||
'final_price_rials' => $this->finalPriceRials,
|
||||
'payment_method' => $this->paymentMethod,
|
||||
'is_paid' => $this->paymentMethod !== 'pending',
|
||||
'discount_type' => $this->discountType,
|
||||
'discount_value' => $this->discountValue,
|
||||
'discount_rials' => $this->discountRials,
|
||||
'paid_at' => $this->paidAt,
|
||||
'paid_total_rials' => $this->getPaidTotalRials(),
|
||||
'payments' => array_map(
|
||||
fn(SessionPayment $p) => $p->toArray(),
|
||||
$this->payments->toArray()
|
||||
),
|
||||
'services' => array_map(
|
||||
fn(SessionService $s) => $s->toArray(),
|
||||
$this->services->toArray()
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace App\Patient\Entity;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\Patient\Repository\SessionPaymentRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
/**
|
||||
* یک پرداختِ جزئی روی یک مراجعه (تسویهی چندتکه). مجموع پرداختها بهعلاوهی
|
||||
* تخفیف، بدهیِ مراجعه را صفر میکند.
|
||||
*/
|
||||
#[ORM\Entity(repositoryClass: SessionPaymentRepository::class)]
|
||||
#[ORM\Table(name: 'session_payments')]
|
||||
class SessionPayment
|
||||
{
|
||||
public const METHODS = ['wallet', 'pos', 'cash', 'card'];
|
||||
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 36, unique: true)]
|
||||
private string $uuid;
|
||||
|
||||
#[ORM\ManyToOne(targetEntity: PatientSession::class, inversedBy: 'payments')]
|
||||
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
|
||||
private PatientSession $session;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 15)]
|
||||
private string $method;
|
||||
|
||||
#[ORM\Column(name: 'amount_rials', type: 'integer')]
|
||||
private int $amountRials;
|
||||
|
||||
#[ORM\Column(name: 'paid_at', type: 'integer')]
|
||||
private int $paidAt;
|
||||
|
||||
#[ORM\ManyToOne(targetEntity: User::class)]
|
||||
#[ORM\JoinColumn(name: 'created_by_id', nullable: true, onDelete: 'SET NULL')]
|
||||
private ?User $createdBy = null;
|
||||
|
||||
#[ORM\Column(name: 'created_by_name', type: 'string', length: 255, nullable: true)]
|
||||
private ?string $createdByName = null;
|
||||
|
||||
#[ORM\Column(name: 'created_at', type: 'integer')]
|
||||
private int $createdAt;
|
||||
|
||||
public function __construct(PatientSession $session, string $method, int $amountRials, ?int $paidAt = null)
|
||||
{
|
||||
$this->uuid = Uuid::v4()->toRfc4122();
|
||||
$this->session = $session;
|
||||
$this->method = $method;
|
||||
$this->amountRials = $amountRials;
|
||||
$this->paidAt = $paidAt ?? time();
|
||||
$this->createdAt = time();
|
||||
}
|
||||
|
||||
public function getId(): ?int { return $this->id; }
|
||||
public function getUuid(): string { return $this->uuid; }
|
||||
public function getSession(): PatientSession { return $this->session; }
|
||||
public function getMethod(): string { return $this->method; }
|
||||
public function getAmountRials(): int { return $this->amountRials; }
|
||||
public function getPaidAt(): int { return $this->paidAt; }
|
||||
public function getCreatedBy(): ?User { return $this->createdBy; }
|
||||
public function getCreatedByName(): ?string { return $this->createdByName; }
|
||||
public function getCreatedAt(): int { return $this->createdAt; }
|
||||
|
||||
public function setCreatedBy(?User $u): self { $this->createdBy = $u; return $this; }
|
||||
public function setCreatedByName(?string $n): self { $this->createdByName = $n; return $this; }
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'uuid' => $this->uuid,
|
||||
'method' => $this->method,
|
||||
'amount_rials' => $this->amountRials,
|
||||
'paid_at' => $this->paidAt,
|
||||
'created_by_name' => $this->createdByName,
|
||||
'created_at' => $this->createdAt,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user