Exemple #1
0
 public function insertPoolingSchedule($contact_id, $member_id, $template_id)
 {
     $contactTarget = Contact::find($contact_id);
     $memberTarget = User::find($member_id);
     $templateTarget = EmailTemplate::where('sequence', $template_id)->first();
     // return $templateTarget->id;
     if (!$contactTarget || !$memberTarget || !$templateTarget) {
         return null;
     }
     $stag = new EmailSchedullerPool();
     $stag->contact_id = $contact_id;
     $stag->member_id = $member_id;
     $stag->template_id = $template_id;
     // $configurationMember = MemberConfiguration::where('member_id',$member_id)->where('param_code',MemberConfiguration::FOLLOW_UP_SEQUENCE)->first();
     // if(!$configurationMember){
     //       	return null ;
     // }
     // $followUpSequence = $configurationMember->param_value;
     $followUpSequence = Sysparam::getValue('conf_follow_up_date');
     $stag->execution_date = date('Y-m-d H:i:s', strtotime("+" . $followUpSequence . " day"));
     $emailSchedullerPool = EmailSchedullerPool::where('member_id', $member_id)->where('contact_id', $contact_id)->where('template_id', $template_id)->first();
     if ($emailSchedullerPool) {
         //save history
         $this->saveHistory("canceled", $emailSchedullerPool->member_id, $emailSchedullerPool->template_id);
         //delete pool
         $emailSchedullerPool->delete();
     }
     //add entry
     if ($contactTarget->active && $memberTarget->active) {
         $stag->save();
         return $stag->toJson();
     } else {
         return null;
     }
 }
 public function run()
 {
     $faker = Faker::create();
     foreach (range(1, 10) as $index) {
         EmailSchedullerPool::create([]);
     }
 }
 public function sendEmailpost()
 {
     $rules = ['contact_id' => 'required', 'template_id' => 'required'];
     $validator = Validator::make(Input::all(), $rules);
     // if the validator fails, redirect back to the form
     if ($validator->fails()) {
         return Redirect::back()->withErrors($validator);
     } else {
         $contact = Contact::find(Input::get('contact_id'));
         if (!$contact->active) {
             return Redirect::back()->with('message', 'Kontak tidak aktif');
         }
         $memberid = Auth::user()->id;
         $contactid = Input::get('contact_id');
         $templateid = Input::get('template_id');
         $issent = EmailSchedullerPool::sendManualEmail($memberid, $contactid, $templateid);
         if ($issent) {
             return Redirect::to(Auth::user()->roleString() . '/outbox')->with('message', 'Email berhasil dikirim.');
         }
         return Redirect::back()->with('message', 'Email tidak dapat dikirim, mohon mencoba kembali.');
     }
 }
 public function executeEmails()
 {
     $pools = $this->getTargetEmails();
     if (!$pools) {
         return;
     }
     foreach ($pools as $pool) {
         $trysending = true;
         $contact = Contact::find($pool->contact_id);
         if ($contact->templateExist($pool->template_id)) {
             $templateid = $contact->getAvalaibleTemplate();
             if ($templateid == 0) {
                 //if template for send not exist disactivated contact
                 $trysending = false;
                 $contact->saveHistory("all have been sent", $pool->member_id, $pool->template_id);
                 $contact->active = 0;
                 $contact->save();
             } else {
                 //try to send another template
                 $contact->saveHistory("sendanother", $pool->member_id, $pool->template_id);
                 $pool->template_id = $templateid;
                 $pool->save();
             }
         }
         if ($trysending) {
             if (EmailSchedullerPool::sendmail($pool->member_id, $pool->contact_id, $pool->template_id)) {
                 if ($contact->email_sent == "" || $contact->email_sent == 0) {
                     $contact->email_sent = $pool->template_id;
                 } else {
                     $contact->email_sent = $contact->email_sent . "," . $pool->template_id;
                 }
                 $contact->save();
                 $contact->saveHistory("success", $pool->member_id, $pool->template_id);
                 $newtemplate = $contact->getAvalaibleTemplate();
                 if ($newtemplate) {
                     $contact->insertPoolingSchedule($contact->id, $contact->user_id, $newtemplate);
                 } else {
                     $contact->saveHistory("all have been sent", $pool->member_id, $pool->template_id);
                     $contact->active = 0;
                     $contact->save();
                 }
                 $pool->delete();
             } else {
                 //ARBUD : retry sending email 3 times
             }
         }
     }
 }
 public function runscheduller()
 {
     $email = new EmailSchedullerPool();
     $email->executeEmails();
     return "check";
 }