1 |
270 |
ahitrov |
package webshop::SQL::CouponsTable; |
2 |
|
|
|
3 |
|
|
use base 'SQL::DocumentTable'; |
4 |
|
|
|
5 |
|
|
sub db_table |
6 |
|
|
{ |
7 |
|
|
return 'webshop_coupons'; |
8 |
|
|
} |
9 |
|
|
|
10 |
|
|
my $available_filters = [qw( |
11 |
|
|
|
12 |
|
|
_class_filter |
13 |
|
|
_status_filter |
14 |
|
|
_in_id_filter |
15 |
|
|
_id_filter |
16 |
|
|
_name_filter |
17 |
|
|
_class_excludes_filter |
18 |
|
|
_sfilter_filter |
19 |
|
|
_excludes_filter |
20 |
|
|
_datetime_filter |
21 |
|
|
_date_equal_filter |
22 |
|
|
_date_filter |
23 |
|
|
_previous_days_filter |
24 |
|
|
_s_filter |
25 |
|
|
_link_filter |
26 |
|
|
|
27 |
|
|
_uid_filter |
28 |
274 |
ahitrov |
_pid_filter |
29 |
270 |
ahitrov |
_code_filter |
30 |
425 |
ahitrov |
_interval_filter |
31 |
270 |
ahitrov |
)]; |
32 |
|
|
|
33 |
|
|
sub available_filters { |
34 |
|
|
return $available_filters; |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
|
38 |
|
|
# ---------------------------------------------------------------------------- |
39 |
|
|
# Свойства храним в массивах, потому что порядок важен! |
40 |
|
|
# Это общие свойства - одинаковые для всех документов. |
41 |
|
|
# |
42 |
|
|
# attr - обязательный параметр, название атрибута; |
43 |
|
|
# type - тип аттрибута, требуется для отображдения; |
44 |
|
|
# rusname - русское название, опять же требуется для отображения; |
45 |
|
|
# hidden - равен 1, когда |
46 |
|
|
# readonly - инициализации при записи только без изменения в дальнейшем |
47 |
|
|
# db_field - поле в таблице |
48 |
|
|
# default - значение по умолчанию (поле всегда имеет это значение) |
49 |
|
|
# ---------------------------------------------------------------------------- |
50 |
|
|
sub required_properties |
51 |
|
|
{ |
52 |
|
|
my $self = shift; |
53 |
|
|
|
54 |
|
|
my @parent_properties = grep { $_->{attr} ne 'sections' && $_->{attr} ne 'dtime' } $self->SUPER::required_properties; |
55 |
|
|
return ( |
56 |
|
|
@parent_properties, |
57 |
|
|
{ |
58 |
|
|
'attr' => 'code', |
59 |
|
|
'type' => 'string', |
60 |
|
|
'rusname' => 'Код купона', |
61 |
|
|
'shortname' => 'Код', |
62 |
|
|
'column' => 1, |
63 |
|
|
'rem' => 'Оставьте незаполненным для автоматической генерации', |
64 |
|
|
'db_field' => 'code', |
65 |
|
|
'db_type' => 'text', |
66 |
|
|
}, |
67 |
|
|
{ |
68 |
|
|
'attr' => 'dtime', |
69 |
|
|
'type' => 'datetime', |
70 |
|
|
'rusname' => 'Начало действия купона', |
71 |
|
|
'shortname' => 'Начало', |
72 |
686 |
ahitrov |
'column' => 5, |
73 |
270 |
ahitrov |
'db_field' => 'dtime', |
74 |
|
|
'db_type' => 'timestamp', |
75 |
|
|
'db_opts' => 'not null default now()', |
76 |
|
|
'default' => 'CURRENT_TIMESTAMP', |
77 |
|
|
}, |
78 |
|
|
{ |
79 |
|
|
'attr' => 'etime', |
80 |
|
|
'type' => 'datetime', |
81 |
|
|
'rusname' => 'Окончание действия купона', |
82 |
|
|
'shortname' => 'Конец', |
83 |
686 |
ahitrov |
'column' => 6, |
84 |
270 |
ahitrov |
'db_field' => 'etime', |
85 |
|
|
'db_type' => 'timestamp', |
86 |
|
|
'db_opts' => 'not null default now()', |
87 |
|
|
'default' => 'CURRENT_TIMESTAMP', |
88 |
|
|
}, |
89 |
|
|
{ |
90 |
792 |
ahitrov |
'attr' => 'created_by', |
91 |
|
|
'type' => 'integer', |
92 |
|
|
'rusname' => 'ID создателя документа', |
93 |
|
|
'hidden' => 1, |
94 |
|
|
'db_field' => 'created_by', |
95 |
|
|
'db_type' => 'integer', |
96 |
|
|
}, |
97 |
|
|
{ |
98 |
|
|
'attr' => 'edited_by', |
99 |
|
|
'type' => 'integer', |
100 |
|
|
'rusname' => 'ID последнего редактора', |
101 |
|
|
'hidden' => 1, |
102 |
|
|
'db_field' => 'edited_by', |
103 |
|
|
'db_type' => 'integer', |
104 |
|
|
}, |
105 |
|
|
{ |
106 |
270 |
ahitrov |
'attr' => 'pid', |
107 |
|
|
'type' => 'integer', |
108 |
|
|
'rusname' => 'ID прототипа', |
109 |
|
|
'hidden' => 1, |
110 |
|
|
'db_field' => 'pid', |
111 |
|
|
'db_type' => 'integer', |
112 |
|
|
'db_opts' => "not null default 0", |
113 |
|
|
'default' => 0, |
114 |
|
|
}, |
115 |
|
|
{ # User ID |
116 |
|
|
'attr' => 'uid', |
117 |
|
|
'type' => 'integer', |
118 |
|
|
'rusname' => 'ID пользователя', |
119 |
|
|
'db_field' => 'uid', |
120 |
|
|
'db_type' => 'integer', |
121 |
|
|
'db_opts' => "not null default 0", |
122 |
|
|
'default' => 0, |
123 |
|
|
}, |
124 |
|
|
{ |
125 |
|
|
'attr' => 'groups', |
126 |
|
|
'type' => 'lookup_multi', |
127 |
|
|
'rusname' => 'Группы товаров', |
128 |
|
|
'db_field' => 'groups', |
129 |
|
|
'db_type' => 'integer[]', |
130 |
|
|
}, |
131 |
|
|
{ |
132 |
|
|
'attr' => 'sections', |
133 |
|
|
'type' => 'sections_list', |
134 |
|
|
'rusname' => 'Секции', |
135 |
|
|
'hidden' => 1, |
136 |
|
|
'db_field' => 'sections', |
137 |
|
|
'db_type' => 'integer', |
138 |
|
|
}, |
139 |
|
|
); |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
########### FILTERS DESCRIPTION ############################################################################### |
143 |
|
|
sub _s_filter { |
144 |
|
|
my ($self,%opts)=@_; |
145 |
|
|
return undef unless ( exists $opts{s} ); |
146 |
|
|
return &SQL::Common::_generic_int_filter('d.sections', $opts{s}); |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
sub _uid_filter { |
150 |
|
|
my ($self,%opts)=@_; |
151 |
|
|
return undef unless ( exists $opts{uid} ); |
152 |
|
|
return &SQL::Common::_generic_int_filter('d.uid', $opts{uid}); |
153 |
|
|
} |
154 |
|
|
|
155 |
274 |
ahitrov |
sub _pid_filter { |
156 |
|
|
my ($self,%opts)=@_; |
157 |
|
|
return undef unless ( exists $opts{pid} ); |
158 |
|
|
return &SQL::Common::_generic_int_filter('d.pid', $opts{pid}); |
159 |
|
|
} |
160 |
|
|
|
161 |
270 |
ahitrov |
sub _code_filter { |
162 |
|
|
my ($self,%opts)=@_; |
163 |
|
|
return undef unless ( exists $opts{code} ); |
164 |
|
|
return &SQL::Common::_generic_name_filter('d.code', $opts{code}, undef, \%opts); |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
sub _interval_filter { |
168 |
|
|
my ($self,%opts)=@_; |
169 |
|
|
return undef unless ( exists $opts{interval} && ref $opts{interval} eq 'ARRAY' && scalar @{$opts{interval}} == 2 ); |
170 |
432 |
ahitrov |
return undef unless $opts{interval}->[0] && $opts{interval}->[1]; |
171 |
|
|
my $date1 = ref $opts{interval}->[0] ? $opts{interval}->[0] : Contenido::DateTime->new( postgres => $opts{interval}->[0].( $opts{interval}->[0] =~ /\d{2}\:\d{2}\:\d{2}/ ? '' : ' 00:00:00' ) ); |
172 |
|
|
my $date2 = ref $opts{interval}->[1] ? $opts{interval}->[1] : Contenido::DateTime->new( postgres => $opts{interval}->[1].( $opts{interval}->[1] =~ /\d{2}\:\d{2}\:\d{2}/ ? '' : ' 00:00:00' ) ); |
173 |
|
|
return undef if DateTime->compare($date1, $date2) == 1; |
174 |
270 |
ahitrov |
|
175 |
425 |
ahitrov |
my $wheres = "date_trunc('day', d.dtime) <= ? and date_trunc('day', d.etime) >= ?"; |
176 |
432 |
ahitrov |
my @values = ($date2->ymd('-'), $date1->ymd('-')); |
177 |
270 |
ahitrov |
|
178 |
|
|
return ($wheres, \@values); |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
sub _link_filter { |
182 |
|
|
my ($self,%opts)=@_; |
183 |
|
|
|
184 |
|
|
my @wheres=(); |
185 |
|
|
my @binds=(); |
186 |
|
|
|
187 |
|
|
# Связь определенного класса |
188 |
|
|
if (exists($opts{lclass})) { |
189 |
|
|
my ($where, $values) = SQL::Common::_generic_text_filter('l.class', $opts{lclass}); |
190 |
|
|
push (@wheres, $where); |
191 |
|
|
push (@binds, ref($values) ? @$values:$values) if (defined $values); |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
my $lclass = $opts{lclass} || 'Contenido::Link'; |
195 |
|
|
my $link_table = $lclass->_get_table->db_table(); |
196 |
|
|
|
197 |
|
|
# Ограничение по статусу связи |
198 |
|
|
if ( exists $opts{lstatus} ) { |
199 |
|
|
my ($where, $values) = SQL::Common::_generic_int_filter('l.status', $opts{lstatus}); |
200 |
|
|
push (@wheres, $where); |
201 |
|
|
push (@binds, ref($values) ? @$values:$values) if (defined $values); |
202 |
|
|
} |
203 |
|
|
|
204 |
|
|
# Ограничение по uid |
205 |
|
|
if ( exists $opts{luid} ) { |
206 |
|
|
my ($where, $values) = SQL::Common::_generic_int_filter('l.uid', $opts{luid}); |
207 |
|
|
push (@wheres, $where); |
208 |
|
|
push (@binds, ref($values) ? @$values:$values) if (defined $values); |
209 |
|
|
} |
210 |
|
|
|
211 |
|
|
# Ограничение по сессии |
212 |
|
|
if ( exists $opts{lsession} ) { |
213 |
|
|
my ($where, $values) = SQL::Common::_generic_text_filter('l.session', $opts{lsession}); |
214 |
|
|
push (@wheres, $where); |
215 |
|
|
push (@binds, ref($values) ? @$values:$values) if (defined $values); |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
# Связь с определенным документ(ом/тами) по цели линка |
219 |
|
|
if ( exists $opts{ldest} ) { |
220 |
|
|
my ($where, $values) = SQL::Common::_generic_int_filter('l.dest_id', $opts{ldest}); |
221 |
|
|
push (@wheres, $where); |
222 |
|
|
push (@binds, ref($values) ? @$values:$values) if (defined $values); |
223 |
|
|
if ($self->_single_class) { |
224 |
|
|
return (\@wheres, \@binds, " join $link_table as l on l.source_id=d.id"); |
225 |
|
|
} else { |
226 |
|
|
return (\@wheres, \@binds, " join $link_table as l on l.source_id=d.id and l.source_class=d.class"); |
227 |
|
|
} |
228 |
|
|
} |
229 |
|
|
# Связь с определенным документ(ом/тами) по источнику линка |
230 |
|
|
if ( exists $opts{lsource} ) { |
231 |
|
|
my ($where, $values) = SQL::Common::_generic_int_filter('l.source_id', $opts{lsource}); |
232 |
|
|
push (@wheres, $where); |
233 |
|
|
push (@binds, ref($values) ? @$values:$values) if (defined $values); |
234 |
|
|
if ($self->_single_class) { |
235 |
|
|
return (\@wheres, \@binds, " join $link_table as l on l.dest_id=d.id"); |
236 |
|
|
} else { |
237 |
|
|
return (\@wheres, \@binds, " join $link_table as l on l.dest_id=d.id and l.dest_class=d.class"); |
238 |
|
|
} |
239 |
|
|
} |
240 |
|
|
|
241 |
|
|
return (undef); |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
|
245 |
|
|
1; |