File indexing completed on 2025-02-09 07:14:34

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_Plings extends Zend_Db_Table_Abstract
0023 {
0024 
0025     const STATUS_NEW = 0;
0026     const STATUS_PAYED = 1;
0027     const STATUS_PLINGED = 2;
0028     const STATUS_TRANSFERRED = 3;
0029     const STATUS_FINISHED = 4;
0030     const STATUS_ERROR = 90;
0031     const STATUS_DELETED = 99;
0032 
0033     /**
0034      * @var string
0035      */
0036     protected $_name = "plings";
0037 
0038     /**
0039      * @var array
0040      */
0041     protected $_dependentTables = array(
0042         'Default_Model_DbTable_Member',
0043         'Default_Model_DbTable_Project'
0044     );
0045 
0046     /**
0047      * Pling a project.
0048      *
0049      * @param Local_Payment_ResponseInterface $payment_response
0050      * @param int $member_id Id of the Sender
0051      * @param int $project_id Id of the receiving project
0052      * @param float $amount amount plings/dollars
0053      * @param string|null $comment Comment from the buyer
0054      * @return mixed The primary key value(s), as an associative array if the
0055      *     key is compound, or a scalar if the key is single-column.
0056      */
0057     public function createNewPlingFromResponse($payment_response, $member_id, $project_id, $amount, $comment = null)
0058     {
0059         $new_row = $this->createRow();
0060         $new_row->member_id = $member_id;
0061         $new_row->project_id = $project_id;
0062         $new_row->amount = $amount;
0063         $new_row->comment = $comment;
0064         $new_row->pling_time = new Zend_Db_Expr ('Now()');
0065         $new_row->status_id = self::STATUS_NEW;
0066 
0067         $new_row->payment_reference_key = $payment_response->getPaymentId();
0068         $new_row->payment_provider = $payment_response->getProviderName();
0069         $new_row->payment_status = $payment_response->getStatus();
0070         $new_row->payment_raw_message = serialize($payment_response->getRawMessage());
0071 
0072         return $new_row->save();
0073     }
0074 
0075     /**
0076      * Mark plings as payed.
0077      * So they can be used to pling.
0078      *
0079      * @param Local_Payment_ResponseInterface $payment_response
0080      *
0081      */
0082     public function activatePlingsFromResponse($payment_response)
0083     {
0084         $updateValues = array(
0085             'status_id' => self::STATUS_PLINGED,
0086             'payment_transaction_id' => $payment_response->getTransactionId(),
0087             'payment_raw_Message' => serialize($payment_response->getRawMessage()),
0088             'payment_status' => $payment_response->getTransactionStatus(),
0089             'active_time' => new Zend_Db_Expr ('Now()')
0090         );
0091 
0092         $this->update($updateValues, "payment_reference_key='" . $payment_response->getPaymentId() . "'");
0093     }
0094 
0095     /**
0096      * @param Local_Payment_ResponseInterface $payment_response
0097      */
0098     public function deactivatePlingsFromResponse($payment_response)
0099     {
0100         $updateValues = array(
0101             'status_id' => 0,
0102             'payment_status' => $payment_response->getTransactionStatus(),
0103             'payment_raw_error' => serialize($payment_response->getRawMessage())
0104         );
0105 
0106         $this->update($updateValues,
0107             "payment_transaction_id='" . $payment_response->getTransactionId() . "' and (status_id=1 or status_id=2)");
0108 
0109     }
0110 
0111     /**
0112      * @param Local_Payment_ResponseInterface $payment_response
0113      * @return null|\Zend_Db_Table_Row_Abstract
0114      */
0115     public function fetchPlingFromResponse($payment_response)
0116     {
0117         if ($payment_response->getPaymentId() != null) {
0118             $where = array('payment_reference_key = ?' => $payment_response->getPaymentId());
0119         } elseif ($payment_response->getTransactionId() != null) {
0120             $where = array('payment_transaction_id = ?' => $payment_response->getTransactionId());
0121         } else {
0122             return null;
0123         }
0124 
0125         return $this->fetchRow($where);
0126 
0127     }
0128 
0129     /**
0130      * @param Local_Payment_ResponseInterface $payment_response
0131      */
0132     public function updatePlingTransactionStatusFromResponse($payment_response)
0133     {
0134         $updateValues = array(
0135             'payment_status' => $payment_response->getTransactionStatus(),
0136             'payment_raw_error' => serialize($payment_response->getRawMessage())
0137         );
0138 
0139         $this->update($updateValues,
0140             "payment_transaction_id='" . $payment_response->getTransactionId() . "' and (status_id=0 or status_id=1 or status_id=2)");
0141 
0142     }
0143 
0144     /**
0145      * pling a project.
0146      *
0147      * @param int $member_id
0148      *            Pling-Geber
0149      * @param int $project_id
0150      *            Pling-Empfänger
0151      * @param int $amount
0152      * @return mixed
0153      */
0154     public function pling($member_id, $project_id, $amount = 0)
0155     {
0156         $rowset = $this->fetchAll($this->select()->where('member_id = ' . $member_id . ' and status_id=1')->order(' create_time desc'));
0157         $row = $rowset->current();
0158 
0159         $row->project_id = $project_id;
0160         $row->status_id = 2;
0161         $row->pling_time = new Zend_Db_Expr ('Now()');
0162         $row->amount = $amount;
0163         $newID = $row->save();
0164 
0165         return $newID;
0166     }
0167 
0168     /**
0169      * Move active plings from 1 project to another.
0170      *
0171      * @param int $project_id_from
0172      *            Sender der Plings
0173      * @param int $project_id_to
0174      *            Empfänger der Plings
0175      * @deprecated
0176      */
0177     public function movePlings($project_id_from, $project_id_to)
0178     {
0179         $updateValues = array(
0180             'project_id' => $project_id_to
0181         );
0182         $this->update($updateValues, "project_id='" . $project_id_from . "' and status_id=2");
0183     }
0184 
0185     /**
0186      * Mark these plings as ready to payout.
0187      *
0188      * @param string $project_id
0189      *            Projekt, welches ausbezahlt werden soll
0190      * @param string $email
0191      *            PayPal-Konto des Empfängers
0192      * @param string $pling_unique_id
0193      *            pling-id, wird für PayPal benutzt
0194      * @deprecated
0195      */
0196     public function payout_request($project_id, $email, $pling_unique_id)
0197     {
0198         $data = array(
0199             'status_id' => '3',
0200             'paypal_payout_request_time' => new Zend_Db_Expr ('Now()'),
0201             'paypal_payout_unique_id' => $pling_unique_id
0202         );
0203         $this->update($data, 'project_id=' . $project_id . ' and status_id=2');
0204     }
0205 
0206     /**
0207      * Payout of the plings successful.
0208      *
0209      * @param string $pling_unique_id
0210      *            Unique-ID, to indentify the plings
0211      * @deprecated
0212      */
0213     public function payout_success($pling_unique_id)
0214     {
0215         $data = array(
0216             'status_id' => '4',
0217             'paypal_payout_success_time' => new Zend_Db_Expr ('Now()')
0218         );
0219         $countRows = 0;
0220         try {
0221             $countRows = $this->update($data, 'paypal_payout_unique_id=' . $pling_unique_id . ' and status_id=3');
0222         } catch (Exception $e) {
0223             Zend_Registry::get('logger')->err(__METHOD__ . ' - ' . print_r($e, true));
0224         }
0225     }
0226 
0227     /**
0228      * Payout was not successful, so the plings went back to staus 2 (plinged,
0229      * but not payouted).
0230      *
0231      * @param string $pling_unique_id
0232      *            Unique-ID, to indentify the plings
0233      * @deprecated
0234      */
0235     public function payout_revert($pling_unique_id)
0236     {
0237         $data = array(
0238             'status_id' => '2',
0239             'paypal_payout_success_time' => null
0240         );
0241         $this->update($data, 'paypal_payout_unique_id=' . $pling_unique_id . ' and status_id=3');
0242     }
0243 
0244     /**
0245      * @param $memberId
0246      * @return int
0247      * @deprecated
0248      */
0249     public function getCountPlingedProjectsForMember($memberId)
0250     {
0251         // selectArr = $this->_db->fetchRow('SELECT count(*) as anzahl FROM
0252         // '.$this->_name.' WHERE status_id>=2 AND project_id='.$_projectId);
0253         // selectArr = $this->_db->fetchRow('select project_id from
0254         // '.$this->_name.' WHERE member_id = '.$memberId . ' and project_id is
0255         // not null group by project_id');
0256         // eturn count($selectArr);
0257         $q = $this->select()->where('member_id = ?', $memberId)->group('project_id');
0258 
0259         return count($q->query()->fetchAll());
0260     }
0261 
0262     /**
0263      * @return int
0264      * @deprecated
0265      */
0266     public function countActive()
0267     {
0268         $q = $this->select()->where('status_id = ?', 1);
0269 
0270         return count($q->query()->fetchAll());
0271     }
0272 
0273     /**
0274      * @return int
0275      * @deprecated
0276      */
0277     public function countPlinged()
0278     {
0279         $q = $this->select()->where('status_id >= ?', 2)->where('project_id is not null');
0280 
0281         return count($q->query()->fetchAll());
0282     }
0283 
0284     /**
0285      * @param $memberId
0286      * @return mixed
0287      * @deprecated
0288      */
0289     public function getCountAvailablePlingsPerUser($memberId)
0290     {
0291         // SELECT COUNT(1) FROM plings WHERE plings.member_id=2861 AND
0292         // plings.status_id=1
0293         $selectArr = $this->_db->fetchRow('SELECT count(*) AS count FROM ' . $this->_name . ' WHERE member_id = ' . $memberId . ' AND status_id = 1');
0294         return $selectArr ['count'];
0295     }
0296 
0297     /**
0298      * @param int $memberId
0299      * @return mixed
0300      * @deprecated
0301      */
0302     public function getCountPlingsPerUser($memberId)
0303     {
0304         // SELECT count(1) FROM plings where project_id in (select project_id
0305         // from project where member_id = 2861)
0306         $selectArr = $this->_db->fetchRow('SELECT count(*) AS count FROM ' . $this->_name . ' WHERE  project_id IN (SELECT project_id FROM project WHERE member_id = ' . $memberId . ' )');
0307         return $selectArr ['count'];
0308     }
0309 
0310     /**
0311      * @param int $memberId
0312      * @return mixed
0313      * @deprecated
0314      */
0315     public function getCountPlingsToPayPerUser($memberId)
0316     {
0317         // SELECT count(1) FROM plings where status_id in (2,3) and project_id
0318         // in (select project_id from project where member_id = 2861)
0319         $selectArr = $this->_db->fetchRow('SELECT count(*) AS count FROM ' . $this->_name . ' WHERE status_id IN (2,3) AND project_id IN (SELECT project_id FROM project WHERE member_id = ' . $memberId . ' )');
0320         return $selectArr ['count'];
0321     }
0322 
0323     /**
0324      * @param int $memberId
0325      * @param $status_id
0326      * @return mixed
0327      * @deprecated
0328      */
0329     public function getCountPlingsPerUserStatus($memberId, $status_id)
0330     {
0331         // SELECT count(1) FROM plings where project_id in (select project_id
0332         // from project where member_id = 2861)
0333         $selectArr = $this->_db->fetchRow('SELECT count(*) AS count FROM ' . $this->_name . ' WHERE member_id = ' . $memberId . ' AND status_id = ' . $status_id);
0334         return $selectArr ['count'];
0335     }
0336 
0337     /**
0338      * @param int $projectId
0339      * @param $memberId
0340      * @return mixed
0341      * @deprecated
0342      */
0343     public function getCountPlingsPerProjectUser($projectId, $memberId)
0344     {
0345         // SELECT COUNT(1) FROM plings WHERE plings.member_id=2861 AND
0346         // plings.status_id=1
0347         $selectArr = $this->_db->fetchRow('SELECT count(*) AS count FROM ' . $this->_name . ' WHERE project_id = ' . $projectId . ' AND member_id = ' . $memberId . ' AND status_id >= 2');
0348         return $selectArr ['count'];
0349     }
0350 
0351     
0352 
0353     /**
0354      * @param int $projectId
0355      * @return int
0356      */
0357     public function getCountPlingsForProject($projectId)
0358     {
0359         $sql = "SELECT count(1) AS countPlinged
0360                 FROM {$this->_name} AS p
0361                 WHERE project_id = ?
0362                 AND
0363                 status_id >= ?;";
0364         $sql = $this->_db->quoteInto($sql, $projectId, 'INTEGER', 1);
0365         $sql = $this->_db->quoteInto($sql, self::STATUS_PLINGED, 'INTEGER', 1);
0366         $result = $this->_db->fetchRow($sql);
0367 
0368         return ( int )$result ['countPlinged'];
0369     }
0370 
0371     /**
0372      * @param int $projectId
0373      * @return float
0374      */
0375     public function getAmountPlingsForProject($projectId)
0376     {
0377         $sql = "
0378             SELECT SUM(amount) AS countPlinged
0379             FROM {$this->_name} AS p
0380             WHERE project_id = ?
0381             AND
0382             status_id >= ?;
0383         ";
0384         $sql = $this->_db->quoteInto($sql, $projectId, 'INTEGER', 1);
0385         $sql = $this->_db->quoteInto($sql, self::STATUS_PLINGED, 'INTEGER', 1);
0386         $result = $this->_db->fetchRow($sql);
0387 
0388         return ( float )$result ['countPlinged'];
0389     }
0390 
0391     /**
0392      * @param int $projectId
0393      * @return Zend_Db_Table_Rowset_Abstract
0394      */
0395     public function getSupporterForProjectId($projectId)
0396     {
0397         $subquerySel = $this->select()->setIntegrityCheck(false)->from($this->_name, array('MAX(id)'))
0398             ->where('project_id = ' . $projectId)
0399             ->where('status_id >= ' . self::STATUS_PLINGED)
0400             ->group('member_id');
0401 
0402         $backerSel = $this->select()->setIntegrityCheck(false)->from($this->_name)
0403             ->join('member', 'member.member_id=plings.member_id')
0404             ->where('plings.id IN (?)', $subquerySel);
0405 
0406         return $this->fetchAll($backerSel);
0407     }
0408 
0409     /**
0410      * @param int $projectId
0411      * @return Zend_Db_Table_Rowset_Abstract
0412      */
0413     public function getSupporterWithPlingsForProjectId($projectId)
0414     {
0415         $backerSel = $this->select()->setIntegrityCheck(false)->from($this->_name,
0416             array('*', 'sum(plings.amount) as sum_plings', 'count(plings.member_id) as count_support'))
0417             ->join('member', 'member.member_id=plings.member_id')
0418             ->where('project_id = ' . $projectId)
0419             ->where('status_id >= ' . self::STATUS_PLINGED)
0420             ->group('plings.member_id')
0421             ->order('sum(plings.amount) desc','create_time desc');
0422 
0423         return $this->fetchAll($backerSel);
0424     }
0425 
0426     /**
0427      * @param int $project_id
0428      * @return mixed
0429      */
0430     public function getPlingersCountForProject($project_id)
0431     {
0432         return $this->getCount($project_id);
0433     }
0434 
0435     /**
0436      * @param int $_projectId
0437      * @return mixed
0438      */
0439     public function getCount($_projectId)
0440     {
0441         // selectArr = $this->_db->fetchRow('SELECT count(*) as anzahl FROM
0442         // '.$this->_name.' WHERE status_id>=2 AND project_id='.$_projectId);
0443         $selectArr = $this->_db->fetchRow('SELECT count(*) AS count FROM ( SELECT member_id FROM ' . $this->_name . ' WHERE status_id >= 2 AND project_id = ' . $_projectId . ' GROUP BY member_id) a');
0444 
0445         return $selectArr ['count'];
0446     }
0447 
0448     /**
0449      * @param int $projectId
0450      * @param null $limit
0451      * @param null|array $forbidden
0452      * @return null|Zend_Db_Table_Row_Abstract
0453      * @deprecated
0454      */
0455     public function getCommentsForProject($projectId, $limit = null)
0456     {
0457         $sqlComments = "select *
0458             from plings
0459             straight_join member on member.member_id = plings.member_id
0460             straight_join comments on comments.comment_pling_id = plings.id
0461             where plings.project_id = :project_id
0462             and plings.status_id = :status_id
0463             and comments.comment_text > ''
0464         ";
0465 
0466         $sqlComments .= ' order by RAND()';
0467 
0468         if (isset($limit)) {
0469             $sqlComments .= ' limit ' . $limit;
0470         }
0471 
0472         $rowSet = $this->getAdapter()->fetchAll($sqlComments, array('project_id' => $projectId, 'status_id' => self::STATUS_PLINGED));
0473 
0474         if (0 == count($rowSet)) {
0475             return array();
0476         }
0477 
0478         return $rowSet;
0479     }
0480 
0481     /**
0482      * @param int $projectId
0483      * @param int|null $limit
0484      * @return null|array
0485      */
0486     public function getDonationsForProject($projectId, $limit = null)
0487     {
0488         $sqlComments = "select *
0489             from plings
0490             straight_join member on member.member_id = plings.member_id
0491             left join comments on comments.comment_pling_id = plings.id
0492             where plings.project_id = :project_id
0493             and plings.status_id = :status_id
0494             order by plings.create_time desc
0495         ";
0496 
0497         if (isset($limit)) {
0498             $sqlComments .= ' limit ' . $limit;
0499         }
0500 
0501         $rowSet = $this->getAdapter()->fetchAll($sqlComments, array('project_id' => $projectId, 'status_id' => self::STATUS_PLINGED));
0502 
0503         if (0 == count($rowSet)) {
0504             return array();
0505         }
0506 
0507         return $rowSet;
0508     }
0509 
0510     /**
0511      * @param int $projectId
0512      * @param null $limit
0513      * @param bool $randomizeOrder
0514      * @return Zend_Db_Table_Rowset_Abstract
0515      */
0516     public function getProjectSupporters($projectId, $limit = null, $randomizeOrder = false)
0517     {
0518         $sel = $this->select()->setIntegrityCheck(false)->from($this->_name)
0519             ->join('member', 'member.member_id=plings.member_id')
0520             ->where('plings.project_id = ?', $projectId)
0521             ->where('status_id >= ' . self::STATUS_PLINGED)
0522             ->group(array('member.member_id'));
0523         if ($randomizeOrder) {
0524             $sel->order(array('RAND()'));
0525         }
0526         if ($limit !== null) {
0527             $sel->limit($limit);
0528         }
0529 
0530         return $this->fetchAll($sel);
0531     }
0532 
0533     /**
0534      * @param int $projectId
0535      * @return int
0536      */
0537     public function getCountSupporters($projectId)
0538     {
0539         $sel = $this->select()->setIntegrityCheck(false)->from($this->_name, 'member_id')
0540             ->join('member', 'member.member_id=plings.member_id')
0541             ->where('plings.project_id = ?', $projectId)
0542             ->where('status_id >= ' . self::STATUS_PLINGED)
0543             ->group(array('plings.member_id'));
0544 
0545         return $this->fetchAll($sel)->count();
0546     }
0547 
0548     /**
0549      * @param $projectId
0550      * @return Zend_Db_Table_Row_Abstract
0551      * @deprecated
0552      */
0553     public function getLatestPling($projectId)
0554     {
0555         $sel = $this->select()->from($this->_name)
0556             ->where('plings.project_id = ?', $projectId)
0557             ->where('status_id >= ' . self::STATUS_PLINGED)
0558             ->order('active_time DESC');
0559 
0560         return $this->fetchAll($sel)->current();
0561     }
0562 
0563 }