<?php
/* +***********************************************************************************
 * The contents of this file are subject to the vtiger CRM Public License Version 1.0
 * ("License"); You may not use this file except in compliance with the License
 * The Original Code is:  vtiger CRM Open Source
 * The Initial Developer of the Original Code is vtiger.
 * Portions created by vtiger are Copyright (C) vtiger.
 * All Rights Reserved.
 * *********************************************************************************** */

class Potentials_syncTransaccional_Action extends Vtiger_IndexAjax_View {

	public function process(Vtiger_Request $request) {
        global $document_root, $adb;
		//Restrict to store indirect relationship from Potentials to Contacts
		$record = $request->get('record');
        $RECORD = Vtiger_Record_Model::getInstanceById($record);
        $ACCOUNTS = '';
        $CONTACTS = '';
        if($RECORD->get('related_to')){
            $ACCOUNTS = Vtiger_Record_Model::getInstanceById($RECORD->get('related_to'));
            $sql_contact = $adb->pquery("SELECT c.contactid FROM vtiger_contactdetails c JOIN vtiger_crmentity cc ON cc.crmid = c.contactid AND cc.deleted = 0 WHERE c.accountid = ?",[$ACCOUNTS->getId()]);
            #Obtiene los Contactos de la cuenta
            if($adb->num_rows($sql_contact) > 0)
            {
                for ($i=0; $i < $adb->num_rows($sql_contact); $i++) { 
                    $idContact = $adb->query_result($sql_contact,$i,'contactid');
                    $CONTACTS[] = Vtiger_Record_Model::getInstanceById($idContact,'Contacts');
                }
                
            }
        }
            

        $ASSIGNED_TO = Vtiger_Record_Model::getInstanceById($RECORD->get('assigned_user_id'),'Users');
        
        $smarty = new Smarty;       
        $smarty->assign('RECORD', $RECORD);
        $smarty->assign('ACCOUNTS', $ACCOUNTS);
        $smarty->assign('ASSIGNED_TO', $ASSIGNED_TO);
        #var_dump($CONTACTS);
        $smarty->assign('CONTACTS', $CONTACTS);
        $result = $smarty->fetch($document_root.'layouts/v7/modules/Potentials/formTransaccional.tpl'); 

        #$response = new Vtiger_Response();
		echo $result;
	}
}
