@@ -23,7 +23,7 @@ public function getTransactionListQuery()
2323 public function filterTransactionList ($ query )
2424 {
2525 $ q = request ('search ' );
26- if (empty ($ q ['value ' ]) || empty ($ q = trim ($ q ['value ' ]))){
26+ if (empty ($ q ['value ' ]) || empty ($ q = trim ($ q ['value ' ]))) {
2727 return ;
2828 }
2929
@@ -97,36 +97,62 @@ public function update(int $id, int $amount)
9797
9898 \DB ::transaction (function () use ($ transaction , $ amount ) {
9999
100- $ oldAmount = $ transaction ->amount ;
101-
102- // Изменения баланса дебита
103- $ newDebitUserBalance = $ transaction ->debit_user_balance + $ oldAmount - $ amount ;
104- $ newUserBalanse = $ transaction ->debitUser ->balance ->balance + $ oldAmount - $ amount ;
105-
106- if ($ newDebitUserBalance < 0 || $ newUserBalanse < 0 ) {
107- throw new Exception ('Too large amount! ' );
108- }
109- $ success = $ transaction ->debitUser ->balance ()->update (['balance ' => $ newUserBalanse ]);
110-
111- // Изменения баланса кредита
112- $ newCreditUserBalance = $ transaction ->credit_user_balance + $ amount - $ oldAmount ;
113- $ newUserBalanse = $ transaction ->creditUser ->balance ->balance + $ amount - $ oldAmount ;
114-
115- if ($ newCreditUserBalance < 0 || $ newUserBalanse < 0 ) {
116- throw new Exception ('Too small amount! ' );
117- }
118- $ success &= $ transaction ->creditUser ->balance ()->update (['balance ' => $ newUserBalanse ]);
100+ $ success = $ this ->updateDebitUserBalance ($ transaction , $ amount );
119101
102+ $ success &= $ this ->updateCreditUserBalance ($ transaction , $ amount );
120103
121104 $ success &= $ transaction ->update ([
122105 'amount ' => $ amount ,
123- 'debit_user_balance ' => $ newDebitUserBalance ,
124- 'credit_user_balance ' => $ newCreditUserBalance
106+ 'debit_user_balance ' => $ transaction -> debitUser -> balance -> fresh ()-> balance ,
107+ 'credit_user_balance ' => $ transaction -> creditUser -> balance -> fresh ()-> balance
125108 ]);
126109
127110 if (!$ success ) {
128111 throw new Exception ('Database error! ' );
129112 }
130113 });
131114 }
115+
116+ /**
117+ * @param $transaction - transaction object
118+ * @param $amount - amount
119+ *
120+ * @return mixed
121+ * @throws Exception
122+ */
123+ public function updateDebitUserBalance ($ transaction , $ amount )
124+ {
125+ $ oldAmount = $ transaction ->amount ;
126+
127+ $ newDebitUserBalance = $ transaction ->debit_user_balance + $ oldAmount - $ amount ;
128+ $ newUserBalance = $ transaction ->debitUser ->balance ->balance + $ oldAmount - $ amount ;
129+
130+ if ($ newDebitUserBalance < 0 || $ newUserBalance < 0 ) {
131+ throw new Exception ('Too large amount! ' );
132+ }
133+
134+ return $ transaction ->debitUser ->balance ()->update (['balance ' => $ newUserBalance ]);
135+ }
136+
137+ /**
138+ * @param $transaction - transaction object
139+ * @param $amount - amount
140+ *
141+ * @return mixed
142+ * @throws Exception
143+ */
144+ public function updateCreditUserBalance ($ transaction , $ amount )
145+ {
146+ $ oldAmount = $ transaction ->amount ;
147+
148+ $ newCreditUserBalance = $ transaction ->credit_user_balance + $ amount - $ oldAmount ;
149+ $ newUserBalance = $ transaction ->creditUser ->balance ->balance + $ amount - $ oldAmount ;
150+
151+ if ($ newCreditUserBalance < 0 || $ newUserBalance < 0 ) {
152+ throw new Exception ('Too small amount! ' );
153+ }
154+
155+ return $ transaction ->creditUser ->balance ()->update (['balance ' => $ newUserBalance ]);
156+ }
157+
132158}
0 commit comments