generated from jric11/baseProject
This commit is contained in:
@@ -38,10 +38,12 @@ class LedgerService extends RosterService {
|
||||
Audit::required($db,$this->actor(),'received payment '.json_encode(['payment'=>(int)$db->lastInsertId(),'club'=>$club,'due'=>$due,'amount_cents'=>$cents,'paid_on'=>$paid,'method'=>$method,'reference'=>$reference]));
|
||||
});
|
||||
}
|
||||
public function adjust(int $club,int $due,string $total,string $reason,string $key): void {
|
||||
public function adjust(int $club,int $due,string $total,string $reason,string $key,?int $membership=null): void {
|
||||
$cents=self::cents($total);$reason=$this->reason($reason);$this->key($key);
|
||||
$this->transaction(function(PDO $db)use($club,$due,$cents,$reason,$key){
|
||||
$row=$this->lockedDue($db,$club,$due);$stmt=$db->prepare('SELECT * FROM club_due_adjustments WHERE request_key=?');$stmt->execute([$key]);$existing=$stmt->fetch();
|
||||
$this->transaction(function(PDO $db)use($club,$due,$cents,$reason,$key,$membership){
|
||||
$row=$this->lockedDue($db,$club,$due);
|
||||
if($membership!==null&&(int)$row['membership_serial']!==$membership)throw new AppException('Dues record does not belong to this membership.',404);
|
||||
$stmt=$db->prepare('SELECT * FROM club_due_adjustments WHERE request_key=?');$stmt->execute([$key]);$existing=$stmt->fetch();
|
||||
if($existing){if((int)$existing['due_serial']!==$due||(int)$existing['corrected_amount_cents']!==$cents||$existing['reason']!==$reason)throw new AppException('This request key was already used for different correction details.',409);return;}
|
||||
if($cents<$row['paid'])throw new AppException('Corrected dues cannot be below received payments. Reverse an incorrect payment first.',409);
|
||||
if($cents===$row['effective'])throw new AppException('The corrected amount must differ from the current dues.',409);
|
||||
@@ -49,11 +51,12 @@ class LedgerService extends RosterService {
|
||||
Audit::required($db,$this->actor(),'corrected dues '.json_encode(['adjustment'=>(int)$db->lastInsertId(),'club'=>$club,'due'=>$due,'before_cents'=>$row['effective'],'after_cents'=>$cents,'reason'=>$reason]));
|
||||
});
|
||||
}
|
||||
public function reverse(int $club,int $payment,string $reason,string $key): void {
|
||||
public function reverse(int $club,int $payment,string $reason,string $key,?int $membership=null): void {
|
||||
$reason=$this->reason($reason);$this->key($key);
|
||||
$this->transaction(function(PDO $db)use($club,$payment,$reason,$key){
|
||||
$this->transaction(function(PDO $db)use($club,$payment,$reason,$key,$membership){
|
||||
$this->club($club,$db,true);$stmt=$db->prepare('SELECT p.* FROM club_payments p JOIN club_dues d ON d.due_serial=p.due_serial WHERE p.payment_serial=? AND d.club_serial=?');$stmt->execute([$payment,$club]);$row=$stmt->fetch();if(!$row)throw new AppException('Payment not found in this club.',404);
|
||||
$this->lockedDue($db,$club,(int)$row['due_serial']);
|
||||
$dueRow=$this->lockedDue($db,$club,(int)$row['due_serial']);
|
||||
if($membership!==null&&(int)$dueRow['membership_serial']!==$membership)throw new AppException('Payment does not belong to this membership.',404);
|
||||
$stmt=$db->prepare('SELECT * FROM club_payment_reversals WHERE request_key=? OR payment_serial=?');$stmt->execute([$key,$payment]);$existing=$stmt->fetch();
|
||||
if($existing){if($existing['request_key']===$key&&(int)$existing['payment_serial']===$payment&&$existing['reason']===$reason)return;throw new AppException('Payment is already reversed or this request key was already used.',409);}
|
||||
$stmt=$db->prepare('INSERT INTO club_payment_reversals(payment_serial,reason,recorded_by,request_key) VALUES(?,?,?,?)');$stmt->execute([$payment,$reason,$this->actor(),$key]);
|
||||
|
||||
Reference in New Issue
Block a user