mirror of
https://github.com/Oreolek/kangana.git
synced 2024-06-26 03:40:56 +03:00
check subscription period on preparation
This commit is contained in:
parent
d8d8614313
commit
25f8f37ff1
|
@ -84,6 +84,15 @@ class Model_Subscription extends ORM {
|
|||
return DB::select('id')->from('subscriptions')->execute()->get('id');
|
||||
}
|
||||
|
||||
public static function get_period($subscription_id)
|
||||
{
|
||||
return DB::select('period')
|
||||
->from('subscriptions')
|
||||
->where('subscription_id', '=', $subscription_id)
|
||||
->execute()
|
||||
->get('period');
|
||||
}
|
||||
|
||||
public static function get_letter_ids($subscription_id)
|
||||
{
|
||||
return DB::select('id')
|
||||
|
|
|
@ -111,4 +111,32 @@ class Model_Task extends ORM {
|
|||
->values(array($client_id, $letter_id, date('Y-m-d'), self::STATUS_PENDING))
|
||||
->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get last sent or prepared letter and check if it's time to send another one.
|
||||
**/
|
||||
public static function check_period($client_id, $letters, $period)
|
||||
{
|
||||
$query = DB::select('date')
|
||||
->from('tasks');
|
||||
if (is_array($letters))
|
||||
{
|
||||
$query = $query->where('letter_id', 'IN', $letters);
|
||||
}
|
||||
else
|
||||
{
|
||||
$query = $query->where('letter_id', '=', $letters);
|
||||
}
|
||||
$check = NULL;
|
||||
$check = $query
|
||||
->and_where('status', '=', self::STATUS_SENT)
|
||||
->or_where('status', '=', self::STATUS_PENDING)
|
||||
->and_where('client_id', '=', $client_id)
|
||||
->and_where(DB::expr('DATEDIFF(CURDATE(), `date`)'), '=', $period)
|
||||
->execute()
|
||||
->get('date');
|
||||
if (!empty($check))
|
||||
return TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,27 +17,31 @@ class Task_Prepare extends Minion_Task
|
|||
$count = Model_Subscription::count_letters($subscription);
|
||||
if ($count == 0)
|
||||
return;
|
||||
$clients = Model_Subscription::get_client_ids($subscription);
|
||||
$period = Model_Subscription::get_period($subscription);
|
||||
$clients = Model_Subscription::get_client_ids($subscription, $period);
|
||||
$letters = Model_Subscription::get_letter_ids($subscription);
|
||||
if (!is_array($clients))
|
||||
{
|
||||
$this->prepare_letters($clients, $letters);
|
||||
$this->prepare_letters($clients, $letters, $period);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach ($clients as $client)
|
||||
{
|
||||
$this->prepare_letters($client, $letters);
|
||||
$this->prepare_letters($client, $letters, $period);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function prepare_letters($client_id, $letter_ids)
|
||||
protected function prepare_letters($client_id, $letter_ids, $period)
|
||||
{
|
||||
$letter = Model_Task::next_unsent($client_id, $letter_ids);
|
||||
if ($letter !== FALSE)
|
||||
if (Model_Task::check_period($client_id, $letter_ids, $period))
|
||||
{
|
||||
Model_Task::prepare($client_id, $letter);
|
||||
$letter = Model_Task::next_unsent($client_id, $letter_ids);
|
||||
if ($letter !== FALSE)
|
||||
{
|
||||
Model_Task::prepare($client_id, $letter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue