diff --git a/include/Cron.php b/include/Cron.php index a8a5159..66e65d2 100644 --- a/include/Cron.php +++ b/include/Cron.php @@ -18,6 +18,10 @@ class Cron { $days = $this->warn_time->d; $this->strings = array( 'en' => array( + 'new' => array( + 'expiry' => "expires on", + 'serial' => "serial number", + ), 'expiring' => array( 'single' => "The following loan expires in less than $days days:", 'multi' => "The following loans expire in less than $days days:", @@ -32,6 +36,10 @@ class Cron { ), ), 'sv' => array( + 'new' => array( + 'expiry' => "går ut", + 'serial' => "artikelnummer", + ), 'expiring' => array( 'single' => "Följande lån går ut om mindre än $days dagar:", 'multi' => "Följande lån går ut om mindre än $days dagar:", @@ -49,6 +57,81 @@ class Cron { } public function run() { + $this->run_receipts(); + $this->run_reminders(); + } + + private function run_receipts() { + begin_trans(); + $get = prepare('select * from `pending_receipt` + where `send_time` < ?'); + bind($get, 'i', $this->now->getTimestamp()); + execute($get); + foreach(result_list($get) as $row) { + $user = new User($row['user']); + $since_time = $row['since_time']; + + $notify_loans = array(); + foreach($user->get_loans('active') as $loan) { + if($loan->get_starttime() >= $since_time) { + $notify_loans[] = $loan; + } + } + $this->send_receipt($user, $loans); + $delete = prepare('delete from `pending_receipt` + where `user = ? and `send_time` < ?'); + bind($delete, 'ii', $user->get_id(), $this->now->getTimestamp()); + execute($delete); + } + commit_trans(); + } + + private function send_receipt($user, $loans) { + $count = count($loans); + if($count > 1) { + $new_sv = "nya lån"; + $new_en = "new loans"; + $have = "have"; + } else { + $new_sv = "nytt lån"; + $new_en = "new loan"; + $have = "has"; + } + $subject = "DSV Helpdesk: $count $new_sv"; + + $intro_sv = ""; + $intro_en = "$count $new_en $have been registered to your user:"; + + $list_sv = $this->make_notice('sv', 'new', $loans); + $list_en = $this->make_notice('en', 'new', $loans); + + $message = <<<EOF +Hej $name! + +Det här är ett automatiskt meddelande om att $count $new_sv har registrerats på din användare: + +$list_sv + +Eventuella artiklar du inte hämtat ut redan kan hämtas från Helpdesk. Svara på det här mailet om du har några frågor. + +---- + +This is an automated message to inform you that $count $new_en $have been registered in your name: + +$list_en + +Any products you haven't already picked up can be collected from Helpdesk. Please reply to this email if you have any questions. + +Mvh +DSV Helpdesk +helpdesk@dsv.su.se +08 - 16 16 48 + +EOF; + $this->send_email($uid, $subject, $message); + } + + private function run_reminders() { $lastrun = $this->kvs->get_value('lastrun', 0); $nextrun = $this->now ->setTimestamp($lastrun) @@ -99,11 +182,6 @@ class Cron { } $strings = $strings[$type]; - $msg = $strings['single']; - if(count($list) > 1) { - $msg = $strings['multi']; - } - $lines = array(); foreach($list as $loan) { $product = $loan->get_product(); @@ -115,6 +193,15 @@ class Cron { $lines[] = "$brand $name, ".$strings['serial'] ." $serial, ".$strings['expiry']." $endtime"; } + + if($type === 'new') { + return implode("\n", $lines); + } + + $msg = $strings['single']; + if(count($list) > 1) { + $msg = $strings['multi']; + } return $msg."\n\n".implode("\n", $lines); } @@ -195,6 +282,10 @@ helpdesk@dsv.su.se 08 - 16 16 48 EOF; + $this->send_email($uid, $subject, $message); + } + + private function send_email($uid, $subject, $message) { try { mb_send_mail($this->ldap->get_user_email($uid), $subject, @@ -203,8 +294,8 @@ EOF; } catch(Exception $e) { error_log($e->message); mb_send_mail($this->error, - "Kunde inte skicka påminnelse", - "Påminnelse kunde inte skickas till ".$uid); + "Kunde inte skicka mail", + "Mail kunde inte skickas till ".$uid); } } } diff --git a/include/Loan.php b/include/Loan.php index fe8346b..cb7f0ae 100644 --- a/include/Loan.php +++ b/include/Loan.php @@ -12,8 +12,22 @@ class Loan extends Event { $endtime .= '13:00'; bind($insert, 'iii', $event_id, $user->get_id(), strtotime($endtime)); execute($insert); + $loan = new Loan($event_id); + + $now = time(); + $pending = prepare('select * from `pending_receipt` where `user` = ? + and `since_time` < ? and `send_time` > ?'); + bind($pending, 'iii', $user->get_id(), $now, $now); + execute($pending); + if(result_single($pending) === null) { + $add = prepare('insert into `pending_receipt` + (`user`, `since_time`, `send_time`) + values(?, ?, ?)'); + bind($add, 'iii', $user->get_id(), $now, $now + 3600); + execute($add); + } commit_trans(); - return new Loan($event_id); + return $loan; } public function __construct($id) { diff --git a/pending_receipts.sql b/pending_receipts.sql new file mode 100644 index 0000000..550da5b --- /dev/null +++ b/pending_receipts.sql @@ -0,0 +1,9 @@ +create table `pending_receipt` ( + `user` bigint(20) not null, + primary key (`user`), + constraint `pr_f_user` + foreign key(`user`) references `user`(`id`), + `send_time` bigint(20) not null, + `since_time` bigint(20) not null +) character set utf8mb4, + collate utf8mb4_unicode_ci;