<?PHP
/*********************************************************************************
 * SugarCRM Community Edition is a customer relationship management program developed by
 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by the
 * Free Software Foundation with the addition of the following permission added
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License along with
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * 
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
 * technical reasons, the Appropriate Legal Notices must display the words
 * "Powered by SugarCRM".
 ********************************************************************************/

/**
 * THIS CLASS IS FOR DEVELOPERS TO MAKE CUSTOMIZATIONS IN
 */
require_once('modules/cont__centro_llamadas/cont__centro_llamadas_sugar.php');
class cont__centro_llamadas extends cont__centro_llamadas_sugar {
	
    function cont__centro_llamadas(){	
        parent::cont__centro_llamadas_sugar();
    }
        
    function create_new_list_query(){
        if(isset($_REQUEST['action']) && $_REQUEST['action']  == 'quicksearchQuery'){
            //decho print_r($_REQUEST,true);exit;
            $objS = new SugarBean();
            $objS->table_name = "con__centro_llamadas";
            return $objS->create_new_list_query("name","con__centro_llamadas.name LIKE '%".$_REQUEST['query']."%' ");
        }
        $return_array = Array();

        $return_array['select'] = "SELECT con.*, 
                                          cstm.cliente_vip_c,
                                          cstm.referencia1_c, 
                                          cstm.referencia1_tel_c,
                                          cstm.referencia1_cel_c,
                                          cstm.cliente_vip_c,
                                          (
                                            SELECT
                                                stu.fecha_documento
                                            FROM
                                                stu_ventas stu
                                                    INNER JOIN
                                                        stu_ventas_contacts_c stuven ON
                                                            stu.id = stuven.stu_ventas_contactsstu_ventas_idb
                                                    INNER JOIN
                                                        stu_tiendas tiend ON
                                                            tiend.id = stu.stu_tiendas_id_c
                                            WHERE
                                                stuven.stu_ventas_contactscontacts_ida = con.id
                                            ORDER BY fecha_documento ASC
                                            LIMIT 1
                                          ) as fecha_documento,
                                          (
                                            SELECT
                                                stu.id
                                            FROM
                                                stu_ventas stu
                                                    INNER JOIN
                                                        stu_ventas_contacts_c stuven ON
                                                            stu.id = stuven.stu_ventas_contactsstu_ventas_idb
                                                    INNER JOIN
                                                        stu_tiendas tiend ON
                                                            tiend.id = stu.stu_tiendas_id_c
                                            WHERE
                                                stuven.stu_ventas_contactscontacts_ida = con.id
                                            ORDER BY fecha_documento ASC
                                            LIMIT 1
                                          ) as id_ventas,
                                          (
                                            SELECT
                                                tiend.name
                                            FROM
                                                stu_ventas stu
                                                    INNER JOIN
                                                        stu_ventas_contacts_c stuven ON
                                                            stu.id = stuven.stu_ventas_contactsstu_ventas_idb
                                                    INNER JOIN
                                                        stu_tiendas tiend ON
                                                            tiend.id = stu.stu_tiendas_id_c
                                            WHERE
                                                stuven.stu_ventas_contactscontacts_ida = con.id
                                            ORDER BY fecha_documento ASC
                                            LIMIT 1
                                          ) as name_tienda,
                                          (
                                            SELECT
                                                tiend.id
                                            FROM
                                                stu_ventas stu
                                                    INNER JOIN
                                                        stu_ventas_contacts_c stuven ON
                                                            stu.id = stuven.stu_ventas_contactsstu_ventas_idb
                                                    INNER JOIN
                                                        stu_tiendas tiend ON
                                                            tiend.id = stu.stu_tiendas_id_c
                                            WHERE
                                                stuven.stu_ventas_contactscontacts_ida = con.id
                                            ORDER BY fecha_documento ASC
                                            LIMIT 1
                                          ) as id_tienda,
                                          centro.proxima_llamada,
                                          centro.description,
                                          COALESCE(opc.name,'Sin gestion') name_gestion
                                        ";
        $return_array['from'] = " FROM 
                                    contacts con
                                        LEFT JOIN
                                            contacts_cstm cstm ON
                                                con.id = cstm.id_c
                                        LEFT JOIN
                                            (
                                                cont__centro_llamadas_contacts_c llcont 
                                                    INNER JOIN
                                                        (
                                                            cont__centro_llamadas centro 
                                                                LEFT JOIN
                                                                    cont__opciones_respuestas opc ON
                                                                        centro.cont__opciones_respuestas_id_c = opc.id
                                                        )
                                                            ON
                                                                llcont.cont__centro_llamadas_contactscont__centro_llamadas_idb = centro.id
                                            )
                                                ON
                                                    con.id = llcont.cont__centro_llamadas_contactscontacts_ida
                                ";
        $return_array['where'] = " where con.do_not_call = 0 ";
        $return_array['order_by'] = "";

        return  $return_array;
    }
	
}
?>