select('COUNT(id_order) AS total_count'); $sql->from('orders'); $sql->where('ddw_order_date = "' . pSQL($ddw_order_date).'"'); if ($ddw_order_time != '') { $sql->where('ddw_order_time = "' . pSQL($ddw_order_time) . '"'); } if ($id_carrier > 0) { $sql->where('id_carrier = ' . (int)$id_carrier); } $sql->where('id_shop = ' . (int)$id_shop); $sql->where('current_state <> ' . Configuration::get('PS_OS_CANCELED')); $row = DB::getInstance()->getRow($sql); return $row['total_count']; } /** * Get all orders in a time window * @param $ddw_order_date * @param $ddw_order_time * @param $id_shop * @param int $id_carrier * @return array|bool|mysqli_result|PDOStatement|resource|null * @throws PrestaShopDatabaseException */ public static function getOrdersInTimeWindow($ddw_order_date, $ddw_order_time, $id_shop, $id_carrier = 0) { $sql = new DbQuery(); $sql->select('*'); $sql->from('orders'); $sql->where('ddw_order_date = "' . pSQL($ddw_order_date) . '"'); if ($ddw_order_time != '') { $sql->where('ddw_order_time = "' . pSQL($ddw_order_time) . '"'); } if ($id_carrier > 0) { $sql->where('id_carrier = ' . (int)$id_carrier); } $sql->where('id_shop = ' . (int)$id_shop); $sql->where('current_state <> ' . Configuration::get('PS_OS_CANCELED')); $result = DB::getInstance()->executeS($sql); return $result; } /** * Get order limit for a date * @param $date * @param $id_carrier * @param $id_shop * @return int * @throws PrestaShopDatabaseException * @throws PrestaShopException */ public static function getOrderLimit($date, $id_carrier, $id_shop) { $current_week_day = date('w', $date); $ddw_weekday = new DDWWeekdayModel(); $ddw_weekday->loadWeekday($id_carrier, $current_week_day, $id_shop); return $ddw_weekday->order_limit; } /** * Determine if an order limit has been exceeded based on the date * @param $date * @param $id_carrier * @param $id_shop * @return bool */ public static function orderLimitExceeded($date, $id_carrier, $id_shop) { $ddw_override_helper = new DDWOverrideHelper($date, $id_carrier, $id_shop); if ($ddw_override_helper->order_limit > 0) { $count = DDWOrderHelper::getOrderCountInTimeWindow($date, '', $id_shop, $id_carrier); if ($count >= $ddw_override_helper->order_limit) { return true; } } return false; } /** * get all orders due for delivery on specified date * @param $date * @param $id_shop * @return mixed */ public static function getOrdersByDeliveryDate($date, $id_shop) { $sql = new DbQuery(); $sql->select('*'); $sql->from('orders'); $sql->where('ddw_order_date = "' . pSQL($date) . '"'); if ((int)$id_shop > 0) { $sql->where('id_shop = ' . (int)$id_shop); } $exclude_order_states = array( Configuration::get('PS_OS_CANCELED'), Configuration::get('PS_OS_REFUND'), Configuration::get('PS_OS_ERROR'), ); foreach ($exclude_order_states as $exclude_order_state) { $sql->where('current_state <> ' . $exclude_order_state); } $sql->where('current_state <> ' . Configuration::get('PS_OS_CANCELED')); $result = DB::getInstance()->executeS($sql); return $result; } /** * ger all orders by delivery date in given date range * @param $date_start * @param $date_end * @param $delivered * @param $id_shop * @return array|bool|mysqli_result|PDOStatement|resource|null * @throws PrestaShopDatabaseException */ public static function getOrdersInDateRange($date_start, $date_end, $delivered, $id_shop) { $sql = new DbQuery(); $sql->select('*'); $sql->from('orders', 'o'); $sql->where('ddw_order_date >= "' . pSQL($date_start) . '"'); $sql->where('ddw_order_date <= "' . pSQL($date_end) . '"'); $sql->innerJoin('order_state', 'os', 'o.current_state = os.id_order_state AND os.paid = 1'); if ((int)$id_shop > 0) { $sql->where('id_shop = ' . (int)$id_shop); } $exclude_order_states = array( Configuration::get('PS_OS_CANCELED'), Configuration::get('PS_OS_REFUND'), Configuration::get('PS_OS_ERROR'), ); if (!$delivered) { $exclude_order_states[] = Configuration::get('PS_OS_DELIVERED'); } foreach ($exclude_order_states as $exclude_order_state) { $sql->where('current_state <> ' . $exclude_order_state); } $result = DB::getInstance()->executeS($sql); return $result; } }