Blame view

src/app/tasks/EventMailTask.php 7.59 KB
ef60cd4d   Administrator   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  <?php
  
  class EventMailTask extends \Phalcon\CLI\Task
  {
      private $projects;
      private $event_info;
      private $post;
  
  
  
      function mainAction(){
  
  
          $projects      = \projects::find();
  
          $eventsData = $this->getEventData($projects);
  
          foreach($eventsData as $project_id => $events){
              foreach($events as $event){
                  echo "call function \n";
                  $this->eventMailerAction($project_id, $event);
  
  
              }
  
          }
  
  
      }
  
ef60cd4d   Administrator   first commit
31
      public function startEventsAction(){
f7818cdf   Administrator   change request to...
32
33
34
35
  
          //$this->actionEventAction();
          $this->orderTimeAfterAction(); //work
          $this->seriesOfLettersAction();//work
ef60cd4d   Administrator   first commit
36
37
      }
  
853d882a   Administrator   change request to...
38
      public function seriesOfLettersAction(){
7882b398   Administrator   change request to...
39
  
853d882a   Administrator   change request to...
40
41
42
43
          $projects = $this->getActiveProjects();
  
          foreach($projects as $project){
  
62ff66e4   Administrator   change request to...
44
45
  
              $events = \eventInfo::getEventsByTrigger($this->modelsManager, $project->id, 'series_of_letter');
853d882a   Administrator   change request to...
46
47
  
              foreach($events as $event){
62ff66e4   Administrator   change request to...
48
                  if($event instanceof \eventInfo){
853d882a   Administrator   change request to...
49
  
62ff66e4   Administrator   change request to...
50
51
52
53
54
55
56
57
58
59
60
                      $customers = (new customersEmailList())->findFirst("id={$event->customer_id}");
  
                      if($customers instanceof customersEmailList){
  
                          $eventEmail = ( new \eventEmail())->findFirst("id={$event->event_id}");
                          if($eventEmail instanceof eventEmail){
                              $eventEmail = $eventEmail->toArray();
                              print_r($eventEmail);
                          }else {
                              continue;
                          }
f7818cdf   Administrator   change request to...
61
                          //$template = \emailTemplates::findFirst("event_id = {$event->event_id} AND project_id = {$project->id}")->toArray();
62ff66e4   Administrator   change request to...
62
63
64
65
66
67
  
                          $this->post['name']=$customers->name;
                          $this->post['email']=$customers->email;
                          $this->post['phone']=$customers->phone;
                          $this->post['address']=$customers->address;
  
f7818cdf   Administrator   change request to...
68
69
70
71
72
73
74
                          //$this->sendMailData($template,$customers->toArray(),$eventEmail,$project);
  
                          $OfflineEvent = new \MyMailer\EventOffline();
  
                          $event_data = [];
  
                          $OfflineEvent->sendEventDelivery($customers->toArray(),$project,$event,$eventEmail,$event_data,$this->post);
62ff66e4   Administrator   change request to...
75
76
77
  
                          $event->last_delivery =  date("Y-m-d H:i:s");
                          $event->delete();
853d882a   Administrator   change request to...
78
                      }
853d882a   Administrator   change request to...
79
  
853d882a   Administrator   change request to...
80
  
62ff66e4   Administrator   change request to...
81
82
83
  
  
                  }
853d882a   Administrator   change request to...
84
              }
62ff66e4   Administrator   change request to...
85
  
853d882a   Administrator   change request to...
86
87
88
          }
      }
  
ef60cd4d   Administrator   first commit
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
      public function actionEventAction(){
          $projects = $this->getActiveProjects();
  
          foreach($projects as $project){
  
              $events = $this->eventByTrigger($project,'action');
  
              foreach($events as $event){
  
                  if(!$event->isNew() ){
                      if(!$event->isRecurring()){
                          continue;
                      }
                  }
                  print_r($event->toArray());
                  switch ($event->event_action) {
                      case 'birthday':
2dfdc329   Administrator   change request to...
106
107
108
109
110
111
112
113
114
                          $customers = $project->findUsersByBirthday();
                          foreach($customers as $customer){
                              $this->post['name']=$customer->name;
                              $this->post['email']=$customer->email;
                              $this->post['phone']=$customer->phone;
                              $this->post['address']=$customer->address;
                              $this->sendMail($customer->toArray(),$project,$event,$event->eventEmail->toArray());
                          }
  
ef60cd4d   Administrator   first commit
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  
                          break;
                  }
              }
          }
  
  
      }
  
  
      public function orderTimeAfterAction(){
          $projects = $this->getActiveProjects();
          $model = new \spyEvent();
          $customers_model = new customersEmailList();
          foreach($projects as $project){
  
              $events = $this->eventByTrigger($project,'order_time_after');
  
              foreach($events as $event){
  
                  if(!$event->isNew() ){
                      if(!$event->isRecurring()){
                          continue;
                      }
                  }
  
f7818cdf   Administrator   change request to...
141
                  $users = $model->getSpyUsers( $this->modelsManager, $project, $event->after_val.' '.$event->after_val_type);
ef60cd4d   Administrator   first commit
142
143
  
                  foreach( $users  as $user){
f7818cdf   Administrator   change request to...
144
                      $event_data = $model->spyTimeAfter($project->id, $user['id'])->toArray();
ef60cd4d   Administrator   first commit
145
146
                      $customers = $customers_model->findFirst("id={$user['id']}");
  
62ff66e4   Administrator   change request to...
147
                      if($customers instanceof customersEmailList && $customers->checkEventForUser($event->id, $project->id)){
f7818cdf   Administrator   change request to...
148
  
ef60cd4d   Administrator   first commit
149
150
151
152
153
154
155
156
157
158
                          $sum = 0;
                          foreach($event_data as $item_one){
                              $sum += (integer)$item_one['price']*$item_one['quantity'];
                          }
                          $this->post['name']=$customers->name;
                          $this->post['email']=$customers->email;
                          $this->post['phone']=$customers->phone;
                          $this->post['address']=$customers->address;
                          $this->post['sum']=$sum;
  
f7818cdf   Administrator   change request to...
159
                          $OfflineEvent = new \MyMailer\EventOffline();
ef60cd4d   Administrator   first commit
160
  
f7818cdf   Administrator   change request to...
161
                          $OfflineEvent->sendEventDelivery($customers->toArray(),$project,$event,$event->eventEmail->toArray(),$event_data,$this->post);
ef60cd4d   Administrator   first commit
162
163
164
165
166
167
168
169
  
                      }
  
                  }
  
                  //$customers = $project->findUsersTimeAfter()->toArray();
  
              }
2dfdc329   Administrator   change request to...
170
  
ef60cd4d   Administrator   first commit
171
          }
ef60cd4d   Administrator   first commit
172
173
174
      }
  
  
f7818cdf   Administrator   change request to...
175
  
ef60cd4d   Administrator   first commit
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
      private function eventByTrigger($project, $trigger){
  
          $result = array();
  
          foreach($project->offlineEvents() as $eventOffline){
              $offlineData = $eventOffline->selectByTrigger($trigger);
              foreach($offlineData as $data){
                  $result[] = $data;
              }
  
          }
  
          return $result;
      }
  
      private function getActiveProjects(){
  
          return \projects::find("status = 'active'");
  
      }
  
  
  
ef60cd4d   Administrator   first commit
199
  
ef60cd4d   Administrator   first commit
200
  
ef60cd4d   Administrator   first commit
201
  
ef60cd4d   Administrator   first commit
202
  
ef60cd4d   Administrator   first commit
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
  
  
  
      public function getEventData($projects){
          $eventsData = array();
          $date = date('d.m.Y');
  
          foreach($projects as $project){
  
              $events = \eventEmail::find("email_type='event_offline' AND project_id=$project->id")->toArray();
  
              if( $events ){
  
                  foreach($events as $k => $v){
  
                      $conditions = \eventOfflineData::find("event_id = {$v['id']}")->toArray();
                      if(!empty($conditions)){
                          $events[$k]['conditions'] = $conditions[0];
                      } else {
                          $events[$k]['conditions'] = array();
                      }
  
  
                      if(!empty($events[$k]['conditions']['event_trigger']) ) {
  
                          if($events[$k]['conditions']['event_trigger'] == 'birthday'){
                              $events[$k]['users'] = \customersEmailList::find("project_id=$project->id AND birthday = '$date'")->toArray();
                          }
                          continue;
                      }
  
                      if(!empty($events[$k]['conditions']['event_date']) && $events[$k]['conditions']['event_date'] == $date ) {
                          $events[$k]['users'] = \customersEmailList::find("project_id=$project->id")->toArray();
                          echo "will send today";
                      }
  
                      if(!empty($events[$k]['conditions']['equal_fields_one'])){
  
                          $events[$k]['users'] = \customersEmailList::find("project_id=$project->id AND {$events[$k]['conditions']['equal_fields_one']} = '{$events[$k]['conditions']['equal_fields_two']}'")->toArray();
  
                      }
  
  
                  }
  
                  $eventsData[$project->id] = $events;
              }
  
          }
  
          return $eventsData;
      }
  
  }