Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Mapper;
6
 
7
 
8
use Laminas\Db\Adapter\AdapterInterface;
9
use Laminas\Db\ResultSet\HydratingResultSet;
10
use Laminas\Paginator\Adapter\DbSelect;
11
use Laminas\Paginator\Paginator;
12
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
13
 
14
use LeadersLinked\Model\CompanyService;
15
use LeadersLinked\Mapper\Common\MapperCommon;
16
 
17
 
18
class CompanyServiceMapper extends MapperCommon
19
{
20
    const _TABLE = 'tbl_company_services';
21
 
22
 
23
    /**
24
     *
25
     * @var CompanyServiceMapper
26
     */
27
    private static $_instance;
28
 
29
    /**
30
     *
31
     * @param AdapterInterface $adapter
32
     */
33
    private function __construct($adapter)
34
    {
35
        parent::__construct($adapter);
36
    }
37
 
38
    /**
39
     *
40
     * @param AdapterInterface $adapter
41
     * @return CompanyServiceMapper
42
     */
43
    public static function getInstance($adapter)
44
    {
45
        if(self::$_instance == null) {
46
            self::$_instance = new CompanyServiceMapper($adapter);
47
        }
48
        return self::$_instance;
49
    }
50
 
51
    /**
52
     *
53
     * @param int $id
54
     * @return CompanyService
55
     */
56
    public function fetchOne($id)
57
    {
58
        $select = $this->sql->select(self::_TABLE);
59
        $select->where->equalTo('id', $id);
60
        $select->limit(1);
61
 
62
        $prototype = new CompanyService();
63
        return $this->executeFetchOneObject($select, $prototype);
64
    }
65
 
66
    /**
67
     *
68
     * @param int $company_id
69
     * @return CompanyService[]
70
     */
71
    public function fetchAllByCompanyId($company_id)
72
    {
73
        $prototype = new CompanyService();
74
        $select = $this->sql->select(self::_TABLE);
75
        $select->where->equalTo('company_id', $company_id);
76
 
77
        return $this->executeFetchAllObject($select, $prototype);
78
    }
79
 
80
 
81
    /**
82
     *
83
     * @param int $service_id
84
     * @return CompanyService[]
85
     */
86
    public function fetchAllActiveByServiceId($service_id)
87
    {
88
        $now = date('Y-m-d H:i:s');
89
 
90
        $prototype = new CompanyService();
91
        $select = $this->sql->select(self::_TABLE);
92
        $select->where->equalTo('service_id', $service_id);
93
        $select->where->equalTo('status', CompanyService::ACTIVE);
94
        $select->where->nest->lessThanOrEqualTo('paid_from', $now)->and->greaterThanOrEqualTo('paid_to', $now );
95
 
96
        return $this->executeFetchAllObject($select, $prototype);
97
    }
98
 
99
 
100
    /**
101
     *
102
     * @param int $company_id
103
     * @param int $service_id
104
     * @return CompanyService[]
105
     */
106
    public function fetchOneByCompanyIdAndServiceId($company_id, $service_id)
107
    {
108
        $prototype = new CompanyService();
109
        $select = $this->sql->select(self::_TABLE);
110
        $select->where->equalTo('company_id', $company_id);
111
        $select->where->equalTo('service_id', $service_id);
112
 
113
 
114
        return $this->executeFetchOneObject($select, $prototype);
115
    }
116
 
117
    /**
118
     *
119
     * @param int $company_id
120
     * @param int $service_id
121
     * @return CompanyService[]
122
     */
123
    public function fetchOneActiveByCompanyIdAndServiceId($company_id, $service_id)
124
    {
125
        $now = date('Y-m-d H:i:s');
126
 
127
        $prototype = new CompanyService();
128
        $select = $this->sql->select(self::_TABLE);
129
        $select->where->equalTo('company_id', $company_id);
130
        $select->where->equalTo('service_id', $service_id);
131
        $select->where->equalTo('status', CompanyService::ACTIVE);
132
        $select->where->nest->lessThanOrEqualTo('paid_from', $now)->and->greaterThanOrEqualTo('paid_to', $now );
133
 
134
        //echo $select->getSqlString($this->adapter->platform); exit;
135
 
136
        return $this->executeFetchOneObject($select, $prototype);
137
    }
138
 
139
    /**
140
     *
141
     * @param CompanyService $companyService
142
     * @return boolean
143
     */
144
    public function insert($companyService)
145
    {
146
        $hydrator = new ObjectPropertyHydrator();
147
        $values = $hydrator->extract($companyService);
148
        $values = $this->removeEmpty($values);
149
 
150
        if(!$companyService->paid_from) {
151
            $values['paid_from'] = null;
152
        }
153
        if(!$companyService->paid_to) {
154
            $values['paid_to'] = null;
155
        }
156
 
157
 
158
        $insert = $this->sql->insert(self::_TABLE);
159
        $insert->values($values);
160
 
161
 
162
        $result = $this->executeInsert($insert);
163
        if($result) {
164
            $companyService->id = $this->lastInsertId;
165
        }
166
 
167
        return $result;
168
 
169
    }
170
 
171
    /**
172
     *
173
     * @param CompanyService $companyService
174
     * @return boolean
175
     */
176
    public function update($companyService)
177
    {
178
        $hydrator = new ObjectPropertyHydrator();
179
        $values = $hydrator->extract($companyService);
180
        $values = $this->removeEmpty($values);
181
 
182
        if(!$companyService->paid_from) {
183
            $values['paid_from'] = null;
184
        }
185
        if(!$companyService->paid_to) {
186
            $values['paid_to'] = null;
187
        }
188
 
189
        $update = $this->sql->update(self::_TABLE);
190
        $update->set($values);
191
        $update->where->equalTo('id', $companyService->id);
192
 
193
        return $this->executeUpdate($update);
194
    }
195
 
196
    /**
197
     *
198
     * @param int $id
199
     * @return boolean
200
     */
201
    public function delete($id)
202
    {
203
        $delete = $this->sql->delete(self::_TABLE);
204
        $delete->where->equalTo('id', $id);
205
 
206
        return $this->executeDelete($delete);
207
 
208
    }
209
 
210
    /**
211
     *
212
     * @param int $company_id
213
     * @param int $service_id
214
     * @return boolean
215
     */
216
    public function deleteByCompanyIdAndServiceId($company_id, $service_id)
217
    {
218
        $delete = $this->sql->delete(self::_TABLE);
219
        $delete->where->equalTo('company_id', $company_id);
220
        $delete->where->equalTo('service_id', $service_id);
221
 
222
        return $this->executeDelete($delete);
223
    }
224
 
225
}