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

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_SectionSupport 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 = "section_support";
0040 
0041     
0042     /**
0043      * Support.
0044      *
0045      * @param Local_Payment_ResponseInterface $payment_response
0046      * @param int $member_id Id of the Sender
0047      * @param float $amount amount donations/dollars
0048      * @param string|null $comment Comment from the buyer
0049      * @return mixed The primary key value(s), as an associative array if the
0050      *     key is compound, or a scalar if the key is single-column.
0051      */
0052     public function createNewSectionSupport($support_id, $section_id, $amount, $tier, $period, $period_frequency, $project_id = null, $member_id = null, $project_category_id = null, $referer = null)
0053     {
0054         $new_row = $this->createRow();
0055         $new_row->support_id = $support_id;
0056         $new_row->section_id = $section_id;
0057         $new_row->amount = $amount;
0058         $new_row->tier = $tier;
0059         $new_row->period = $period;
0060         $new_row->period_frequency = $period_frequency;
0061         $new_row->created_at = new Zend_Db_Expr ('Now()');
0062         
0063         $new_row->project_id = $project_id;
0064         $new_row->creator_id = $member_id;
0065         $new_row->project_category_id = $project_category_id;
0066         
0067         $new_row->referer = $referer;
0068 
0069         return $new_row->save();
0070     }
0071     
0072 
0073     /**
0074      * @return int
0075      * @deprecated
0076      */
0077     public function countActive()
0078     {
0079         $q = $this->select()->where('is_active = ?', 1);
0080 
0081         return count($q->query()->fetchAll());
0082     }
0083     
0084     
0085     public function fetchLatestSectionSupportForMember($section_id, $member_id) {
0086         $sql = "
0087             SELECT section_support.section_support_id, section_support.support_id, section_support.section_id, section_support.amount, section_support.tier, section_support.period_frequency
0088             FROM section_support 
0089             JOIN section ON section.section_id = section_support.section_id
0090             JOIN support ON support.id = section_support.support_id AND support.status_id = 2
0091             WHERE section_support.is_active = 1
0092             AND section.section_id = :section_id
0093             AND support.member_id = :member_id
0094             ORDER BY section_support.created_at desc 
0095             LIMIT 1
0096         ";
0097         $resultSet = $this->getAdapter()->fetchRow($sql, array('section_id' => $section_id, 'member_id' => $member_id));
0098         
0099         return $resultSet;
0100     }
0101     
0102     
0103     public function fetchAllSectionSupportsForMember($section_id, $member_id) {
0104         $sql = "
0105             SELECT section_support.section_support_id, section_support.support_id, section_support.section_id, section_support.project_id, m2.member_id AS affiliate_member_id, m2.username AS affiliate_username, section_support.referer ,case when support.subscription_id IS NULL then support.payment_transaction_id ELSE support.subscription_id END AS subscription_id, support.type_id, section_support.amount, section_support.tier, section_support.period, section_support.period_frequency, support.status_id, support.type_id, support.active_time, support.delete_time, support.payment_provider,member.member_id,member.username,
0106             case 
0107             when support.status_id = 2 AND support.type_id = 0 AND (date_format(support.active_time  + INTERVAL 11 MONTH, '%Y%m')) >= date_format(NOW(), '%Y%m') then 'active'
0108             when support.status_id = 2 AND support.type_id = 1 then 'active'
0109             ELSE 'inactive'
0110             END AS active_status
0111             ,(support.active_time  + INTERVAL 11 MONTH) AS active_time_one_year
0112             ,(support.active_time  + INTERVAL 1 MONTH) AS active_time_one_month
0113             ,(SELECT MAX(active_time) FROM support p2 WHERE p2.type_id = 2 and p2.subscription_id = support.subscription_id) AS last_payment_time
0114             ,case 
0115                 when support.type_id = 1 AND section_support.period = 'Y' then (SELECT (MAX(active_time)  + INTERVAL 11 MONTH) FROM support p2 WHERE p2.type_id = 2 and p2.subscription_id = support.subscription_id)
0116           when support.type_id = 1 AND section_support.period = 'M' then (SELECT (STR_TO_DATE(CONCAT(DATE_FORMAT(MAX(active_time) + INTERVAL 1 MONTH,'%Y%m'),'01'),'%Y%m%d') - INTERVAL 1 DAY)  FROM support p2 WHERE p2.type_id = 2 and p2.subscription_id = support.subscription_id)
0117             END AS last_payment_until_time
0118             FROM section_support 
0119             JOIN section ON section.section_id = section_support.section_id
0120             JOIN support ON support.id = section_support.support_id AND support.status_id >= 2
0121             JOIN member ON member.member_id = support.member_id
0122             left JOIN project ON project.project_id = section_support.project_id
0123             left JOIN member m2 ON m2.member_id = project.member_id
0124             WHERE section_support.is_active = 1
0125             AND section.section_id = :section_id
0126             AND support.member_id = :member_id
0127             ORDER BY support.active_time DESC  
0128         ";
0129         $resultSet = $this->getAdapter()->fetchAll($sql, array('section_id' => $section_id, 'member_id' => $member_id));
0130         
0131         return $resultSet;
0132     }
0133 }
0134 
0135 
0136