Пример #1
0
 public static function pay($params, $currentUser, $con)
 {
     // check role's permission
     $permission = RolePermissionQuery::create()->select('pay_credit')->findOneById($currentUser->role_id, $con);
     if (!$permission || $permission != 1) {
         throw new \Exception('Akses ditolak. Anda tidak mempunyai izin untuk melakukan operasi ini.');
     }
     // make sure the credit is not fully paid already
     $credit = CreditQuery::create()->filterByStatus('Active')->filterById($params->credit_id)->withColumn('CONVERT(Credit.Total, SIGNED) - CONVERT(Credit.Paid, SIGNED)', 'Balance')->findOne($con);
     if (!$credit) {
         throw new \Exception('Data tidak ditemukan.');
     }
     // if credit is already fully paid then stop paying
     if ($credit->getBalance() <= 0) {
         throw new \Exception('Piutang ini sudah dilunasi.');
     }
     // create new payment
     $creditPayment = new CreditPayment();
     $creditPayment->setDate($params->date)->setCreditId($params->credit_id)->setPaid($params->paid)->setCashierId($params->cashier)->setStatus('Active')->save($con);
     $payment = CreditPaymentQuery::create()->filterByStatus('Active')->filterByCreditId($params->credit_id)->withColumn('SUM(Paid)', 'paid')->select(array('paid'))->groupBy('CreditId')->findOne($con);
     $credit->setPaid($payment)->save($con);
     $results['success'] = true;
     $results['data'] = 'Yay';
     return $results;
 }