File indexing completed on 2024-05-26 05:59:15

0001 <?php
0002 /**
0003  *  ocs-webserver
0004  *
0005  *  Copyright 2016 by pling GmbH.
0006  *
0007  *    This file is part of ocs-webserver.
0008  *
0009  *    This program is free software: you can redistribute it and/or modify
0010  *    it under the terms of the GNU Affero General Public License as
0011  *    published by the Free Software Foundation, either version 3 of the
0012  *    License, or (at your option) any later version.
0013  *
0014  *    This program is distributed in the hope that it will be useful,
0015  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017  *    GNU Affero General Public License for more details.
0018  *
0019  *    You should have received a copy of the GNU Affero General Public License
0020  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021  **/
0022 class Default_Model_DbTable_Support extends Zend_Db_Table_Abstract
0023 {
0024 
0025     const STATUS_NEW = 0;
0026     const STATUS_PAYED = 1;
0027     const STATUS_DONATED = 2;
0028     const STATUS_TRANSFERRED = 3;
0029     const STATUS_FINISHED = 4;
0030     const STATUS_ERROR = 90;
0031     const STATUS_DELETED = 99;
0032     
0033     const SUPPORT_TYPE_SIGNUP = 1;
0034     const SUPPORT_TYPE_PAYMENT = 2;
0035 
0036     /**
0037      * @var string
0038      */
0039     protected $_name = "support";
0040 
0041     /**
0042      * @var array
0043      */
0044     protected $_dependentTables = array(
0045         'Default_Model_DbTable_Member'
0046     );
0047     
0048     
0049     /**
0050      * Support Subscription Signup.
0051      *
0052      * @param Local_Payment_ResponseSubscriptionSignupInterface $payment_response
0053      * @param int $member_id Id of the Sender
0054      * @param float $amount amount donations/dollars
0055      * @param string|null $comment Comment from the buyer
0056      * @return mixed The primary key value(s), as an associative array if the
0057      *     key is compound, or a scalar if the key is single-column.
0058      */
0059     public function createNewSupportSubscriptionPaymentFromResponse($payment_response)
0060     {
0061         $new_row = $this->createRow();
0062         
0063         $signUp = $this->fetchRow("payment_reference_key = '". $payment_response->getCustom() . "' AND type_id = 1");
0064         if(!empty($signUp)) {
0065             $new_row->member_id = $signUp['member_id'];
0066             $new_row->subscription_id = $signUp['subscription_id'];
0067             $new_row->tier = $signUp['tier'];
0068             $new_row->period = $signUp['period'];
0069             $new_row->period_frequency = $signUp['period_frequency'];
0070         }
0071 
0072         $new_row->amount = $payment_response->getTransactionAmount();
0073         $new_row->payment_transaction_id = $payment_response->getTransactionId();
0074         $new_row->donation_time = new Zend_Db_Expr ('Now()');
0075         $new_row->active_time = new Zend_Db_Expr ('Now()');
0076         $new_row->status_id = self::STATUS_DONATED;
0077         $new_row->type_id = self::SUPPORT_TYPE_PAYMENT;
0078         $new_row->payment_reference_key = $payment_response->getCustom();
0079         $new_row->payment_provider = $payment_response->getProviderName();
0080         $new_row->payment_status = $payment_response->getStatus();
0081         $new_row->payment_raw_message = serialize($payment_response->getRawMessage());
0082 
0083         return $new_row->save();
0084     }
0085 
0086     /**
0087      * Support.
0088      *
0089      * @param Local_Payment_ResponseInterface $payment_response
0090      * @param int $member_id Id of the Sender
0091      * @param float $amount amount donations/dollars
0092      * @param string|null $comment Comment from the buyer
0093      * @return mixed The primary key value(s), as an associative array if the
0094      *     key is compound, or a scalar if the key is single-column.
0095      */
0096     public function createNewSupportFromResponse($payment_response, $member_id, $amount, $comment = null)
0097     {
0098         $new_row = $this->createRow();
0099         $new_row->member_id = $member_id;
0100         $new_row->amount = $amount;
0101         $new_row->comment = $comment;
0102         $new_row->donation_time = new Zend_Db_Expr ('Now()');
0103         $new_row->status_id = self::STATUS_NEW;
0104 
0105         $new_row->payment_reference_key = $payment_response->getPaymentId();
0106         $new_row->payment_provider = $payment_response->getProviderName();
0107         $new_row->payment_status = $payment_response->getStatus();
0108         $new_row->payment_raw_message = serialize($payment_response->getRawMessage());
0109 
0110         return $new_row->save();
0111     }
0112     
0113     
0114     /**
0115      * Support.
0116      *
0117      * @param Local_Payment_ResponseInterface $payment_response
0118      * @param int $member_id Id of the Sender
0119      * @param float $amount amount donations/dollars
0120      * @param string|null $comment Comment from the buyer
0121      * @return mixed The primary key value(s), as an associative array if the
0122      *     key is compound, or a scalar if the key is single-column.
0123      */
0124     public function createNewSupport($transaction_id, $member_id, $amount, $comment = null)
0125     {
0126         $new_row = $this->createRow();
0127         $new_row->member_id = $member_id;
0128         $new_row->amount = $amount;
0129         $new_row->comment = $comment;
0130         $new_row->donation_time = new Zend_Db_Expr ('Now()');
0131         $new_row->status_id = self::STATUS_NEW;
0132 
0133         $new_row->payment_reference_key = $transaction_id;
0134         $new_row->payment_provider = 'paypal';
0135         $new_row->payment_status = $this::STATUS_NEW;
0136         $new_row->payment_raw_message = '';
0137 
0138         return $new_row->save();
0139     }
0140     
0141     
0142         /**
0143      * Support.
0144      *
0145      * @param Local_Payment_ResponseInterface $payment_response
0146      * @param int $member_id Id of the Sender
0147      * @param float $amount amount donations/dollars
0148      * @param string|null $comment Comment from the buyer
0149      * @return mixed The primary key value(s), as an associative array if the
0150      *     key is compound, or a scalar if the key is single-column.
0151      */
0152     public function createNewSupportSubscriptionSignup($transaction_id, $member_id, $amount,$tier, $period, $period_frequency, $comment = null)
0153     {
0154         $new_row = $this->createRow();
0155         $new_row->member_id = $member_id;
0156         $new_row->type_id = $this::SUPPORT_TYPE_SIGNUP;
0157         $new_row->amount = $amount;
0158         $new_row->tier = $tier;
0159         $new_row->period = $period;
0160         $new_row->period_frequency = $period_frequency;
0161         $new_row->comment = $comment;
0162         $new_row->donation_time = new Zend_Db_Expr ('Now()');
0163         $new_row->status_id = self::STATUS_NEW;
0164 
0165         $new_row->payment_reference_key = $transaction_id;
0166         $new_row->payment_provider = 'paypal';
0167         $new_row->payment_status = $this::STATUS_NEW;
0168         $new_row->payment_raw_message = '';
0169 
0170         return $new_row->save();
0171     }
0172     
0173 
0174     /**
0175      * Mark donations as payed.
0176      * So they can be used to donation.
0177      *
0178      * @param Local_Payment_ResponseInterface $payment_response
0179      *
0180      */
0181     public function activateSupportFromResponse($payment_response)
0182     {
0183         $updateValues = array(
0184             'status_id' => self::STATUS_DONATED,
0185             'payment_transaction_id' => $payment_response->getTransactionId(),
0186             'payment_raw_Message' => serialize($payment_response->getRawMessage()),
0187             'payment_status' => $payment_response->getTransactionStatus(),
0188             'active_time' => new Zend_Db_Expr ('Now()')
0189         );
0190 
0191         $this->update($updateValues, "payment_reference_key='" . $payment_response->getCustom() . "'");
0192     }
0193     
0194     
0195     /**
0196      * Mark donations as payed.
0197      * So they can be used to donation.
0198      *
0199      * @param Local_Payment_ResponseSubscriptionSignupInterface $payment_response
0200      *
0201      */
0202     public function activateSupportSubscriptionSignupFromResponse($payment_response)
0203     {
0204         $updateValues = array(
0205             'status_id' => self::STATUS_DONATED,
0206             'subscription_id' => $payment_response->getSubscriptionId(),
0207             'payment_raw_Message' => serialize($payment_response->getRawMessage()),
0208             'active_time' => new Zend_Db_Expr ('Now()')
0209         );
0210 
0211         $this->update($updateValues, "payment_reference_key='" . $payment_response->getCustom() . "' AND type_id = 1");
0212     }
0213     
0214     /**
0215      * @param Local_Payment_ResponseInterface $payment_response
0216      */
0217     public function deactivateSupportSubscriptionSignupFromResponse($payment_response)
0218     {
0219         $updateValues = array(
0220             'status_id' => self::STATUS_DELETED,
0221             'payment_raw_Message' => serialize($payment_response->getRawMessage()),
0222             'delete_time' => new Zend_Db_Expr ('Now()')
0223         );
0224 
0225         $this->update($updateValues, "payment_reference_key='" . $payment_response->getCustom() . "' AND type_id = 1");
0226 
0227     }
0228 
0229     /**
0230      * @param Local_Payment_ResponseInterface $payment_response
0231      */
0232     public function deactivateSupportFromResponse($payment_response)
0233     {
0234         $updateValues = array(
0235             'status_id' => 0,
0236             'payment_status' => $payment_response->getTransactionStatus(),
0237             'payment_raw_error' => serialize($payment_response->getRawMessage())
0238         );
0239 
0240         $this->update($updateValues,
0241             "payment_transaction_id='" . $payment_response->getTransactionId() . "' and (status_id=1 or status_id=2)");
0242 
0243     }
0244 
0245     /**
0246      * @param Local_Payment_ResponseInterface $payment_response
0247      * @return null|\Zend_Db_Table_Row_Abstract
0248      */
0249     public function fetchSupportFromResponse($payment_response)
0250     {
0251         if ($payment_response->getCustom() != null) {
0252             $where = array('payment_reference_key = ?' => $payment_response->getCustom());
0253         } elseif ($payment_response->getTransactionId() != null) {
0254             $where = array('payment_transaction_id = ?' => $payment_response->getTransactionId());
0255         } else {
0256             return null;
0257         }
0258 
0259         return $this->fetchRow($where);
0260 
0261     }
0262 
0263     /**
0264      * @param Local_Payment_ResponseInterface $payment_response
0265      */
0266     public function updateSupportTransactionStatusFromResponse($payment_response)
0267     {
0268         $updateValues = array(
0269             'payment_status' => $payment_response->getTransactionStatus(),
0270             'payment_raw_error' => serialize($payment_response->getRawMessage())
0271         );
0272 
0273         $this->update($updateValues,
0274             "payment_transaction_id='" . $payment_response->getTransactionId() . "' and (status_id=0 or status_id=1 or status_id=2)");
0275 
0276     }
0277 
0278     /**
0279      * Payout of the donations successful.
0280      *
0281      * @param string $donation_unique_id
0282      *            Unique-ID, to indentify the donations
0283      * @deprecated
0284      */
0285     public function payout_success($donation_unique_id)
0286     {
0287         $data = array(
0288             'status_id' => '4',
0289             'paypal_payout_success_time' => new Zend_Db_Expr ('Now()')
0290         );
0291         $countRows = 0;
0292         try {
0293             $countRows = $this->update($data, 'paypal_payout_unique_id=' . $donation_unique_id . ' and status_id=3');
0294         } catch (Exception $e) {
0295             Zend_Registry::get('logger')->err(__METHOD__ . ' - ' . print_r($e, true));
0296         }
0297     }
0298 
0299     /**
0300      * Payout was not successful, so the donations went back to staus 2 (donationed,
0301      * but not payouted).
0302      *
0303      * @param string $donation_unique_id
0304      *            Unique-ID, to indentify the donations
0305      * @deprecated
0306      */
0307     public function payout_revert($donation_unique_id)
0308     {
0309         $data = array(
0310             'status_id' => '2',
0311             'paypal_payout_success_time' => null
0312         );
0313         $this->update($data, 'paypal_payout_unique_id=' . $donation_unique_id . ' and status_id=3');
0314     }
0315 
0316     /**
0317      * @return int
0318      * @deprecated
0319      */
0320     public function countActive()
0321     {
0322         $q = $this->select()->where('status_id = ?', 1);
0323 
0324         return count($q->query()->fetchAll());
0325     }
0326 
0327     /**
0328      * @return int
0329      * @deprecated
0330      */
0331     public function countSupported()
0332     {
0333         $q = $this->select()->where('status_id >= ?', 2);
0334 
0335         return count($q->query()->fetchAll());
0336     }
0337 
0338     /**
0339      * @param $memberId
0340      * @return mixed
0341      * @deprecated
0342      */
0343     public function getCountAvailableSupportsPerUser($memberId)
0344     {
0345         // SELECT COUNT(1) FROM support WHERE support.member_id=2861 AND
0346         // donations.status_id=1
0347         $selectArr = $this->_db->fetchRow('SELECT count(*) AS count FROM ' . $this->_name . ' WHERE member_id = ' . $memberId . ' AND status_id = 1');
0348         return $selectArr ['count'];
0349     }
0350 
0351     /**
0352      * @param int $memberId
0353      * @return mixed
0354      * @deprecated
0355      */
0356     public function getCountSupportsPerUser($memberId)
0357     {
0358         return $selectArr ['count'];
0359     }
0360 
0361 
0362     /**
0363      * @param null $limit
0364      * @param null|array $forbidden
0365      * @return null|Zend_Db_Table_Row_Abstract
0366      * @deprecated
0367      */
0368     public function getComments($limit = null)
0369     {
0370         $sqlComments = "select *
0371             from ' . $this->_name . '
0372             straight_join member on member.member_id = ' . $this->_name . '.member_id
0373             straight_join comments on comments.comment_donation_id = ' . $this->_name . '.id
0374             where ' . $this->_name . '.status_id = :status_id
0375             and comments.comment_text > ''
0376         ";
0377 
0378         $sqlComments .= ' order by RAND()';
0379 
0380         if (isset($limit)) {
0381             $sqlComments .= ' limit ' . $limit;
0382         }
0383 
0384         $rowSet = $this->getAdapter()->fetchAll($sqlComments, array('status_id' => self::STATUS_DONATED));
0385 
0386         if (0 == count($rowSet)) {
0387             return array();
0388         }
0389 
0390         return $rowSet;
0391     }
0392 
0393     /**
0394      * @param int|null $limit
0395      * @return null|array
0396      */
0397     public function getSupports($limit = null)
0398     {
0399         $sqlComments = "select *
0400             from ' . $this->_name . '
0401             straight_join member on member.member_id = donations.member_id
0402             left join comments on comments.comment_donation_id = donations.id
0403             where donations.status_id = :status_id
0404             order by donations.create_time desc
0405         ";
0406 
0407         if (isset($limit)) {
0408             $sqlComments .= ' limit ' . $limit;
0409         }
0410 
0411         $rowSet = $this->getAdapter()->fetchAll($sqlComments, array('status_id' => self::STATUS_DONATED));
0412 
0413         if (0 == count($rowSet)) {
0414             return array();
0415         }
0416 
0417         return $rowSet;
0418     }
0419 
0420     /**
0421      * @param null $limit
0422      * @param bool $randomizeOrder
0423      * @return Zend_Db_Table_Rowset_Abstract
0424      */
0425     public function getSupporters($limit = null, $randomizeOrder = false)
0426     {
0427         $sel = $this->select()->setIntegrityCheck(false)->from($this->_name)
0428             ->join('member', 'member.member_id=donations.member_id')
0429             ->where('status_id >= ' . self::STATUS_DONATED)
0430             ->group(array('member.member_id'));
0431         if ($randomizeOrder) {
0432             $sel->order(array('RAND()'));
0433         }
0434         if ($limit !== null) {
0435             $sel->limit($limit);
0436         }
0437 
0438         return $this->fetchAll($sel);
0439     }
0440 
0441     /**
0442      * @return int
0443      */
0444     public function getCountSupporters()
0445     {
0446         $sel = $this->select()->setIntegrityCheck(false)->from($this->_name, 'member_id')
0447             ->join('member', 'member.member_id=donations.member_id')
0448             ->where('status_id >= ' . self::STATUS_DONATED)
0449             ->group(array('donations.member_id'));
0450 
0451         return $this->fetchAll($sel)->count();
0452     }
0453 
0454     /**
0455      * @return Zend_Db_Table_Row_Abstract
0456      * @deprecated
0457      */
0458     public function getLatestSupport()
0459     {
0460         $sel = $this->select()->from($this->_name)
0461             ->where('status_id >= ' . self::STATUS_DONATED)
0462             ->order('active_time DESC');
0463 
0464         return $this->fetchAll($sel)->current();
0465     }
0466 
0467     /**
0468      * @return Zend_Db_Table_Row_Abstract
0469      * @deprecated
0470      */
0471     public function getSupporterDonationList($member_id)
0472     {
0473         $sel = $this->select()->from($this->_name)
0474             ->where('status_id >= ' . self::STATUS_DONATED.' and member_id = ' . $member_id)
0475             ->order('active_time DESC');
0476         return $this->fetchAll($sel);
0477     }
0478 
0479 }
0480 
0481 
0482