EventMailTask.php 7.59 KB
<?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);


            }

        }


    }

    public function startEventsAction(){

        //$this->actionEventAction();
        $this->orderTimeAfterAction(); //work
        $this->seriesOfLettersAction();//work
    }

    public function seriesOfLettersAction(){

        $projects = $this->getActiveProjects();

        foreach($projects as $project){


            $events = \eventInfo::getEventsByTrigger($this->modelsManager, $project->id, 'series_of_letter');

            foreach($events as $event){
                if($event instanceof \eventInfo){

                    $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;
                        }
                        //$template = \emailTemplates::findFirst("event_id = {$event->event_id} AND project_id = {$project->id}")->toArray();

                        $this->post['name']=$customers->name;
                        $this->post['email']=$customers->email;
                        $this->post['phone']=$customers->phone;
                        $this->post['address']=$customers->address;

                        //$this->sendMailData($template,$customers->toArray(),$eventEmail,$project);

                        $OfflineEvent = new \MyMailer\EventOffline();

                        $event_data = [];

                        $OfflineEvent->sendEventDelivery($customers->toArray(),$project,$event,$eventEmail,$event_data,$this->post);

                        $event->last_delivery =  date("Y-m-d H:i:s");
                        $event->delete();
                    }




                }
            }

        }
    }

    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':
                        $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());
                        }


                        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;
                    }
                }

                $users = $model->getSpyUsers( $this->modelsManager, $project, $event->after_val.' '.$event->after_val_type);

                foreach( $users  as $user){
                    $event_data = $model->spyTimeAfter($project->id, $user['id'])->toArray();
                    $customers = $customers_model->findFirst("id={$user['id']}");

                    if($customers instanceof customersEmailList && $customers->checkEventForUser($event->id, $project->id)){

                        $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;

                        $OfflineEvent = new \MyMailer\EventOffline();

                        $OfflineEvent->sendEventDelivery($customers->toArray(),$project,$event,$event->eventEmail->toArray(),$event_data,$this->post);

                    }

                }

                //$customers = $project->findUsersTimeAfter()->toArray();

            }

        }
    }



    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'");

    }










    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;
    }

}