public function getAllOrderEmailsWithDate($date_start, $date_finish, $limit = 0, $types = array(1), $diler_id = 0, $number = 0, $rassrochka = '') {
if(!empty($number))
$searchNumber = ' AND so.number LIKE "%'.$number.'%"';
else
$searchNumber = '';
if(!empty($limit)) $limit = 'LIMIT 0, '.(int)$limit;
else $limit = '';
if(!empty($diler_id)) $diler = ' AND ((so.type_id = 2 AND so.shop_id ='.(int)$diler_id.') OR so.type_id <> 2)';
else $diler = '';
$where_rassrochka = '';
if($rassrochka == 1){
$where_rassrochka = '';
}
if($rassrochka == 2){
$where_rassrochka = ' so.rassrochka > 0 AND ';
}
if($rassrochka == 3){
$where_rassrochka = ' so.rassrochka = 0 AND ';
}
$where_dostavka_fullpay = '';
if(in_array(6,$types)){
$where_dostavka_fullpay = ' ((so.date_fakt_otgruz = "0000-00-00 00:00:00" AND so.price <= so.payment) OR so.fullpay = 1
OR (so.date_fakt_otgruz != "0000-00-00 00:00:00" AND so.price <= so.payment AND so.date_fakt_otgruz >= (SELECT sp.date FROM sales_payments sp WHERE sp.order_id = so.id ORDER BY sp.date DESC LIMIT 1))) AND ';
}
$sql = 'SELECT so.email, so.fio, so.customer_surname, so.customer_name, so.customer_patronymic FROM sales_orders so '.
'WHERE so.email <> "" AND so.email IS NOT NULL AND '.$where_dostavka_fullpay.$where_rassrochka.' so.type_id IN ('.implode(',', $types).') AND date_order >= '.$this->db->escape($date_start).' AND date_order <= '.$this->db->escape($date_finish).' '.$diler.$searchNumber.
'ORDER BY so.date_order DESC, so.date_create DESC, so.number DESC '.$limit;
return $this->db->get_all($sql);
}