array( 'columns' => array('order_id'), 'refTableClass' => 'OrderItemModel', 'refColumns' => array('order_id') ) ); /** * Gets an array of arrays containg data on the gift certificates associated with a particular order * @param $oid The order ID * @return An array containing gift certificate data for an order */ public function getAllGiftCertificates($oid) { require_once APPLICATION_PATH . '/models/GiftCertificateModel.php'; $gift = new GiftCertificateModel(); require_once APPLICATION_PATH . '/models/OrderItemsModel.php'; $oim = new OrderItemsModel($gift); $where = array('order_id'=>$oid,'table_def_id'=>$gift->getCPAModel(CPA_ORDER_ITEMS)->getTableDefID()); return $oim->fetchAll($where); } /** * Gets an array of orders that are ready for finalization * @param $state The order state ID for the orders to fetch * @param $now The current time * @return An array of order data */ public function getOrdersForFinalization($state,$now) { $query = "SELECT * FROM `order` WHERE order_state_id = $state AND process_time <= '$now' AND is_active='1'"; $statement = $this->getTable()->getAdapter()->query($query); return $statement->fetchAll(); } /** * Fetch a single order from the database * @param $orderId The ID of the order to fetch * @return An array containing order data for the requeted order */ public function getOrder($orderId) { $o = ORDER_TABLE_NAME; $query = "SELECT * FROM `{$o}` WHERE `{$o}`.is_active='1' AND `{$o}`.id = '{$orderId}'"; $statement = $this->getTable()->getAdapter()->query($query); return $statement->fetch(); } /** * Fetch payments for a given order * @param $orderId The ID of the order to fetch * @return An array of payment information */ public function getOrderPayment($orderId) { $op = ORDER_PAYMENTS_TABLE_NAME; $pst = PAYMENT_SOURCE_TYPES_TABLE_NAME; $query = " SELECT * FROM {$op} LEFT JOIN {$pst} ON {$pst}.id = {$op}.payment_type_id WHERE {$op}.is_active='1' AND {$op}.order_id = '{$orderId}' "; $statement = $this->getTable()->getAdapter()->query($query); return $statement->fetch(); } /** * Gets a listing of saved orders * @param string $state The only orders of this state should be retrieved, right now this is a string like 'saved' or 'finalized'. If set to * null, then orders of all states will be shown. * @param int $person_id If specified, then only orders belonging to this person will be returned. If set to null (default), then orders belonging * to all people will be displayed. * @return An array of saved orders */ public function getOrderList($state=null,$person_id=""){ $p = PERSON_TABLE_NAME; $o = ORDER_TABLE_NAME; $os = ORDER_STATES_TABLE_NAME; $rs = RESTAURANT_TABLE_NAME; $oi = ORDER_INVITEES_TABLE_NAME; $state_filter = ''; if(!empty($state)){ $state_filter = " AND `$os`.state='{$state}' "; } $person_filter = ''; if(!empty($person_id)){ $person_filter = " AND $p.id='{$person_id}' "; } $query = "SELECT $p.first_name,$p.last_name,$os.state,`$o`.*,$rs.name FROM `$o` INNER JOIN $p ON $p.id=`$o`.person_id INNER JOIN $os ON $os.id=`$o`.order_state_id LEFT JOIN $rs ON $rs.id=$o.restaurant_id WHERE `$o`.is_active='1' {$state_filter} {$person_filter}"; $query_vars = array(); $statement = $this->getTable()->getAdapter()->query($query,$query_vars); $results = $statement->fetchAll(); // If filtering by person then check their favorites if(!empty($person_id)){ $favorites = $this->fetchAllByTag($person_id, TagDefModel::ORDER_TYPE, Const_Order::FAVORITE_TAG_NAME, array(), array()); foreach($results as &$order){ foreach($favorites as $fo){ if($fo['id'] == $order['id']){ $order['favorite'] = TRUE; break; } } if(!isset($order['favorite'])){ $order['favorite'] = FALSE; } } } // Add orders that this person is an invitee to, we don't need to do a favorites check on these // because you can't add orders that you have been invited to as a favorite if(!empty($person_id)){ $oim = ORDER_ITEMS_TABLE_NAME; $query = "SELECT $oi.stipend,$p.first_name,$p.last_name,$os.state,`$o`.*,$rs.name,$oi.subtotal,$oi.tax FROM `$oi` INNER JOIN `$o` ON `$o`.id=$oi.order_id INNER JOIN $p ON $p.id=`$o`.person_id INNER JOIN $os ON $os.id=`$o`.order_state_id LEFT JOIN $rs ON $rs.id=$o.restaurant_id WHERE `$o`.is_active='1' {$state_filter} AND $os.state <> 'saved' AND $oi.person_id={$person_id} AND $oi.is_active='1' AND `$o`.order_state_id<>1"; $statement = $this->getTable()->getAdapter()->query($query); $subresults = $statement->fetchAll(); foreach($subresults as &$subresult){ $subresult['amount_charged'] = $subresult['tax']+$subresult['subtotal']; if(!empty($subresult['stipend'])) $subresult['amount_charged'] -= $subresult['stipend']; if($subresult['amount_charged'] <= 0) $subresult['amount_charged'] = 0; } $results = array_merge($results,$subresults); } return $results; } /** * Fetch a user's favorite orders * @param $person_id The ID of the person to get orders for * @return An array of the person's favorite orders */ public function getFavoriteOrderList($person_id = ""){ $whereArray = array(); $whereArray[] = array("r.is_active"=>'1', "o.order_type"=>'gift'); $whereArray[] = array("b.is_active"=>'1', "o.order_type"=>'gift'); $istaggedWhereClause = $this->isTaggedWhereClause(array(new Zend_Db_Expr("o.id = tagged.id")), $person_id, TagDefModel::ORDER_TYPE, Const_Order::FAVORITE_TAG_NAME); $whereArray[] = new Zend_Db_Expr("EXISTS ($istaggedWhereClause)") ; $db = $this->getTable()->getAdapter(); $select = $db->select()->from(array("o"=>ORDER_TABLE_NAME)) ->joinLeft(array("r"=>RESTAURANT_TABLE_NAME), "o.restaurant_id = r.id", array("restaurant_name"=>"name")) ->joinLeft(array("b"=>BRAND_TABLE_NAME), "r.brand_id = b.id", array("image_name")) ->where($this->getWhere($whereArray)); return $db->fetchAll($this->fetchHandler->handle($select)); } /** * Fetch a user's pending orders * @param $person_id The ID of the person to get orders for * @return An array of pending orders for that person */ public function getPendingOrderList($person_id = FALSE){ $person_id = (int)$person_id; $query = "SELECT o.*,r.name as restaurant_name FROM `order` o LEFT JOIN restaurant r ON r.id=o.restaurant_id AND r.is_active='1' WHERE o.is_active='1' AND o.person_id=$person_id AND order_state_id IN (SELECT id FROM order_states WHERE order_states.state IN ('manual_finalization','auto_finalization'))"; $statement = $this->getTable()->getAdapter()->query($query); $subresults = $statement->fetchAll(); $query = "SELECT o.*,r.name as restaurant_name FROM order_invitees i LEFT JOIN `order` o ON o.id=i.order_id AND o.is_active='1' LEFT JOIN restaurant r ON r.id=o.restaurant_id AND r.is_active='1' WHERE i.is_active='1' AND i.person_id=$person_id AND o.order_state_id IN (SELECT id FROM order_states WHERE order_states.state IN ('manual_finalization','auto_finalization'))"; $statement = $this->getTable()->getAdapter()->query($query); $subresults += $statement->fetchAll(); return $subresults; } /** * Count the number of pending orders a person has * @param $person_id The ID of the person */ public function countPendingOrderList($person_id = FALSE){ $rowcount = $this->fetchType("Count")->getPendingOrderList($person_id); return $rowcount[0]["CountRow"]; } }