File indexing completed on 2024-05-12 05:58:32

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  **/
0023 class Backend_MemberpayoutController extends Local_Controller_Action_Backend
0024 {
0025 
0026     const RESULT_OK = "OK";
0027     const RESULT_ERROR = "ERROR";
0028     const DATA_ID_NAME = 'id';
0029 
0030     /** @var Default_Model_Member */
0031     protected $_model;
0032 
0033     protected $_modelName = 'Default_Model_DbTable_MemberPayout';
0034     protected $_pageTitle = 'Manage Payouts';
0035 
0036     public function init()
0037     {
0038         $this->_model = new $this->_modelName();
0039 
0040         $this->view->pageTitle = $this->_pageTitle;
0041 
0042         parent::init();
0043     }
0044 
0045     public function indexAction()
0046     {
0047 
0048     }
0049 
0050     public function createAction()
0051     {
0052         $jTableResult = array();
0053         try {
0054             $newRow = $this->_model->createRow($this->getAllParams());
0055             $result = $newRow->save();
0056 
0057             $jTableResult['Result'] = self::RESULT_OK;
0058             $jTableResult['Record'] = $newRow->toArray();
0059         } catch (Exception $e) {
0060             Zend_Registry::get('logger')->err(__METHOD__ . ' - ' . print_r($e, true));
0061             $translate = Zend_Registry::get('Zend_Translate');
0062             $jTableResult['Result'] = self::RESULT_ERROR;
0063             $jTableResult['Message'] = $translate->_('Error while processing data.');
0064         }
0065 
0066         $this->_helper->json($jTableResult);
0067     }
0068 
0069     public function updateAction()
0070     {
0071         $jTableResult = array();
0072         try {
0073             $values = $this->getAllParams();
0074 
0075             foreach ($values as $key => $value) {
0076                 if ($value == '') {
0077                     $values[$key] = new Zend_Db_Expr('NULL');
0078                 }
0079             }
0080 
0081             $record = $this->_model->save($values);
0082             $resultArray = $record->toArray();
0083             $resultArray['color'] = '#ffffff';
0084 
0085             $jTableResult = array();
0086             $jTableResult['Result'] = self::RESULT_OK;
0087             $jTableResult['Record'] = $resultArray;
0088         } catch (Exception $e) {
0089             Zend_Registry::get('logger')->err(__METHOD__ . ' - ' . print_r($e, true));
0090             $translate = Zend_Registry::get('Zend_Translate');
0091             $jTableResult['Result'] = self::RESULT_ERROR;
0092             $jTableResult['Message'] = $translate->_('Error while processing data.');
0093         }
0094 
0095         $this->_helper->json($jTableResult);
0096     }
0097 
0098     public function deleteAction()
0099     {
0100         $dataId = (int)$this->getParam(self::DATA_ID_NAME, null);
0101 
0102         $this->_model->setDeleted($dataId);
0103 
0104         $identity = Zend_Auth::getInstance()->getIdentity();
0105         Default_Model_ActivityLog::logActivity($dataId, null, $identity->member_id, Default_Model_ActivityLog::BACKEND_USER_DELETE,
0106             null);
0107 
0108         $jTableResult = array();
0109         $jTableResult['Result'] = self::RESULT_OK;
0110 
0111         $this->_helper->json($jTableResult);
0112     }
0113 
0114     public function listAction()
0115     {
0116         $startIndex = (int)$this->getParam('jtStartIndex');
0117         $pageSize = (int)$this->getParam('jtPageSize');
0118         $sorting = $this->getParam('jtSorting');
0119         $filter['yearmonth'] = $this->getParam('filter_yearmonth');
0120         if (!$this->getParam('filter_yearmonth')) {
0121             $filter['yearmonth'] = date("Ym", strtotime("first day of previous month"));
0122         }
0123         $filter['status'] = $this->getParam('filter_status');
0124         $filter['member_id'] = $this->getParam('filter_member_id');
0125         $filter['paypal_mail'] = $this->getParam('filter_paypal_mail');
0126         $filter['mail'] = $this->getParam('filter_mail');
0127         
0128         
0129         $sql = " 
0130                 select member_payout.*, payout_status.color
0131                 from member_payout
0132                 join payout_status on payout_status.id = member_payout.`status`
0133 
0134                 ";
0135         
0136 
0137         $select = $this->_model->select()->order($sorting)->limit($pageSize, $startIndex);
0138         
0139         $where = " WHERE 1=1 ";
0140         
0141         /*
0142         $select->join('payout_status',
0143             'member_payout.status = payout_status.id',
0144             array('color')
0145         );*/
0146         //        foreach ($filter as $key => $value) {
0147         //            if (false === empty($value)) {
0148         //                $select->where("{$key} like ?", $value);
0149         //            }
0150         //        }
0151 
0152         $metadata = $this->_model->info(Zend_Db_Table_Abstract::METADATA);
0153 
0154         foreach ($filter as $key => $value) {
0155             if (is_array($value)) {
0156                 $list = '';
0157                 foreach ($value as $element) {
0158                     if (isset($element)) {
0159                         $list = $list . ',' . $element;
0160                     }
0161                 }
0162 
0163                 if (empty($list)) {
0164                     continue;
0165                 }
0166 
0167                 $list = substr($list, 1);
0168 
0169                 $select->where("{$key} in ({$list})");
0170                 
0171                 $where .= " AND {$key} in ({$list})";
0172 
0173                 continue;
0174             }
0175             if (false === empty($value)) {
0176                 $data_type = $metadata[$key]['DATA_TYPE'];
0177                 if (($data_type == 'varchar') OR ($data_type == 'text')) {
0178                     $likeText = "'%".$value."%'";
0179                     $select->where("{$key} like ?", $likeText);
0180                     $where .= " AND {$key} like ".$likeText;
0181                 } else {
0182                     $select->where("{$key} = ?", $value);
0183                     $where .= " AND {$key} = " . $value;
0184                 }
0185             }
0186         }
0187         $sql .= $where;
0188         $sql .= " ORDER BY ". $sorting;
0189         $sql .= " LIMIT ".$startIndex.",".$pageSize;
0190 
0191         $reports = $this->_model->fetchAll($select);
0192         $reportsReturn = $this->_model->getAdapter()->fetchAll($sql);
0193 
0194         $reportsAll = $this->_model->fetchAll($select->limit(null, null)->reset('columns')
0195                                                      ->columns(array('countAll' => new Zend_Db_Expr('count(*)'))));
0196 
0197         $jTableResult = array();
0198         $jTableResult['Result'] = self::RESULT_OK;
0199         $jTableResult['Records'] = $reportsReturn;
0200         $jTableResult['TotalRecordCount'] = $reportsAll->current()->countAll;
0201 
0202         $this->_helper->json($jTableResult);
0203     }
0204 
0205     public function exportAction()
0206     {
0207         $startIndex = (int)$this->getParam('jtStartIndex');
0208         $pageSize = (int)$this->getParam('jtPageSize');
0209         $sorting = $this->getParam('jtSorting');
0210         $filter['yearmonth'] = $this->getParam('filter_yearmonth');
0211         if (!$this->getParam('filter_yearmonth')) {
0212             $filter['yearmonth'] = date("Ym", strtotime("first day of previous month"));
0213         }
0214         $filter['status'] = $this->getParam('filter_status');
0215         $filter['member_id'] = $this->getParam('filter_member_id');
0216         $filter['paypal_mail'] = $this->getParam('filter_paypal_mail');
0217         $filter['mail'] = $this->getParam('filter_mail');
0218         $fields = $this->getParam('field_list');
0219 
0220         $select = $this->_model->select();
0221 
0222         $select->from('member_payout',
0223                 array('member_id as MemberId', 'paypal_mail as PayPalMail', 'amount as Amount', 'status as Status'))->order($sorting)
0224                ->limit($pageSize, $startIndex)
0225         ;
0226         /*
0227          $select->join('payout_status',
0228                  'member_payout.status = payout_status.id',
0229                  array('color')
0230          );*/
0231         //        foreach ($filter as $key => $value) {
0232         //            if (false === empty($value)) {
0233         //                $select->where("{$key} like ?", $value);
0234         //            }
0235         //        }
0236 
0237         $metadata = $this->_model->info(Zend_Db_Table_Abstract::METADATA);
0238 
0239         foreach ($filter as $key => $value) {
0240             if (is_array($value)) {
0241                 $list = '';
0242                 foreach ($value as $element) {
0243                     if (isset($element)) {
0244                         $list = $list . ',' . $element;
0245                     }
0246                 }
0247 
0248                 if (empty($list)) {
0249                     continue;
0250                 }
0251 
0252                 $list = substr($list, 1);
0253 
0254                 $select->where("{$key} in ({$list})");
0255 
0256                 continue;
0257             }
0258             if (false === empty($value)) {
0259                 $data_type = $metadata[$key]['DATA_TYPE'];
0260                 if (($data_type == 'varchar') OR ($data_type == 'text')) {
0261                     $select->where("{$key} like ?", '%' . $value . '%');
0262                 } else {
0263                     $select->where("{$key} = ?", $value);
0264                 }
0265             }
0266         }
0267 
0268         $reports = $this->_model->fetchAll($select);
0269 
0270         $reportsAll = $this->_model->fetchAll($select->limit(null, null)->reset('columns')
0271                                                      ->columns(array('countAll' => new Zend_Db_Expr('count(*)'))));
0272 
0273         $jTableResult = array();
0274         $jTableResult['Result'] = self::RESULT_OK;
0275         $jTableResult['Records'] = $reports->toArray();
0276         $jTableResult['TotalRecordCount'] = $reportsAll->current()->countAll;
0277 
0278         $filename = "Payout_" . $filter['yearmonth'] . ".xls";
0279         header("Content-Type: application/vnd.ms-excel");
0280         header("Content-Disposition: attachment; filename=\"$filename\"");
0281         $this->exportFile($reports->toArray());
0282         //$this->_helper->json($jTableResult);
0283     }
0284 
0285     function exportFile($records)
0286     {
0287         $heading = false;
0288         if (!empty($records)) {
0289             foreach ($records as $row) {
0290                 if (!$heading) {
0291                     // display field/column names as a first row
0292                     echo implode("\t", array_keys($row)) . "\n";
0293                     $heading = true;
0294                 }
0295                 echo implode("\t", array_values($row)) . "\n";
0296             }
0297         }
0298         exit;
0299     }
0300 
0301     public function memberinfoAction()
0302     {
0303         $jTableResult = array();
0304         try {
0305             $memberId = (int)$this->getParam('member_id');
0306 
0307             $modelMember = new  Default_Model_Member();
0308             $record = $modelMember->find($memberId)->current();
0309             $this->view->member = $record;
0310             $view = $this->view->render('user/member.phtml');
0311 
0312             $jTableResult = array();
0313             $jTableResult['Result'] = self::RESULT_OK;
0314             $jTableResult['Record'] = $record->toArray();
0315             $jTableResult['ViewRecord'] = $view;
0316         } catch (Exception $e) {
0317             Zend_Registry::get('logger')->err(__METHOD__ . ' - ' . print_r($e, true));
0318             $translate = Zend_Registry::get('Zend_Translate');
0319             $jTableResult['Result'] = self::RESULT_ERROR;
0320             $jTableResult['Message'] = $translate->_('Error while processing data.');
0321         }
0322 
0323         $this->_helper->json($jTableResult);
0324     }
0325 
0326 }