From 1dca5104e2c5cd71dded61f27014427f2cf54fdc Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Tue, 17 May 2011 18:28:54 +0000 Subject: [PATCH] Works on paypal module --- htdocs/core/class/commonobject.class.php | 49 ++++++++++++++++ htdocs/paypal/ajaxtransaction.php | 73 ++++++++++++++++++++++-- htdocs/societe/class/societe.class.php | 6 +- 3 files changed, 122 insertions(+), 6 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 6873b20b1ce..12996efd68b 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -547,6 +547,55 @@ class CommonObject $this->$object->fetch($this->origin_id); } + /** + * Load object from import key + * @param table Table element or element line + * @param key Import key + * @return int <0 if KO, >0 if OK + */ + function fetchObjectFromImportKey($table,$key) + { + global $conf; + + $result=false; + + $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX.$table; + $sql.= " WHERE import_key = '".$key."'"; + $sql.= " AND entity = ".$conf->entity; + $resql = $this->db->query($sql); + if ($resql) + { + $row = $this->db->fetch_row($resql); + $result = $this->fetch($row[0]); + } + + return $result; + } + + /** + * Load object from external reference + * @param table Table element or element line + * @param ref External reference + * @return int <0 if KO, >0 if OK + */ + function fetchObjectFromRefExt($table,$ref) + { + global $conf; + + $result=false; + + $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX.$table; + $sql.= " WHERE ref_ext = '".$ref."'"; + $sql.= " AND entity = ".$conf->entity; + $resql = $this->db->query($sql); + if ($resql) + { + $row = $this->db->fetch_row($resql); + $result = $this->fetch($row[0]); + } + + return $result; + } /** * \brief Load properties id_previous and id_next diff --git a/htdocs/paypal/ajaxtransaction.php b/htdocs/paypal/ajaxtransaction.php index fdefc234b09..8af30f69ca2 100644 --- a/htdocs/paypal/ajaxtransaction.php +++ b/htdocs/paypal/ajaxtransaction.php @@ -30,7 +30,8 @@ if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1'); if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1'); require('../main.inc.php'); -include_once(DOL_DOCUMENT_ROOT."/societe/class/societe.class.php"); +require_once(DOL_DOCUMENT_ROOT."/societe/class/societe.class.php"); +require_once(DOL_DOCUMENT_ROOT."/contact/class/contact.class.php"); require_once(DOL_DOCUMENT_ROOT.'/paypal/lib/paypal.lib.php'); require_once(DOL_DOCUMENT_ROOT."/paypal/lib/paypalfunctions.lib.php"); @@ -61,18 +62,82 @@ if (isset($_GET['action']) && ! empty($_GET['action']) && ( (isset($_GET['elemen if ($_GET['action'] == 'create') { $soc = new Societe($db); - $socid = $soc->fetchObjectFromImportKey($soc->table_element,$object['PAYERID']); - if ($socid < 0) + $ret = $soc->fetchObjectFromRefExt($soc->table_element,$_SESSION[$_GET['transaction_id']]['PAYERID']); + if ($ret < 0) { + // Load object modCodeTiers + $module=$conf->global->SOCIETE_CODECLIENT_ADDON; + if (! $module) dolibarr_error('',$langs->trans("ErrorModuleThirdPartyCodeInCompanyModuleNotDefined")); + if (substr($module, 0, 15) == 'mod_codeclient_' && substr($module, -3) == 'php') + { + $module = substr($module, 0, dol_strlen($module)-4); + } + require_once(DOL_DOCUMENT_ROOT ."/includes/modules/societe/".$module.".php"); + $modCodeClient = new $module; + // Create customer and return rowid + $soc->ref_ext = $_SESSION[$_GET['transaction_id']]['PAYERID']; + $soc->name = empty($conf->global->MAIN_FIRSTNAME_NAME_POSITION)?trim($_SESSION[$_GET['transaction_id']]['FIRSTNAME'].' '.$_SESSION[$_GET['transaction_id']]['LASTNAME']):trim($_SESSION[$_GET['transaction_id']]['LASTNAME'].' '.$_SESSION[$_GET['transaction_id']]['FIRSTNAME']); + $soc->nom_particulier = $_SESSION[$_GET['transaction_id']]['LASTNAME']; + $soc->prenom = $_SESSION[$_GET['transaction_id']]['FIRSTNAME']; + $soc->address = $_SESSION[$_GET['transaction_id']]['SHIPTOSTREET']; + $soc->zip = $_SESSION[$_GET['transaction_id']]['SHIPTOZIP']; + $soc->town = $_SESSION[$_GET['transaction_id']]['SHIPTOCITY']; + //$soc->pays_id = $_POST["pays_id"]; + $soc->email = $_SESSION[$_GET['transaction_id']]['EMAIL']; + $soc->code_client = ($modCodeClient->code_auto ? $modCodeClient->getNextValue($soc,0):''); + $soc->tva_assuj = 1; + $soc->client = 1; + $soc->particulier = 1; + + $db->begin(); + $result = $soc->create($user); + if ($result >= 0) + { + if ($soc->particulier) + { + $contact=new Contact($db); + + $contact->civilite_id = $soc->civilite_id; + $contact->name=$soc->nom_particulier; + $contact->firstname=$soc->prenom; + $contact->address=$soc->address; + $contact->zip=$soc->zip; + $contact->cp=$soc->cp; + $contact->town=$soc->town; + $contact->ville=$soc->ville; + $contact->fk_pays=$soc->fk_pays; + $contact->socid=$soc->id; + $contact->status=1; + $contact->email=$soc->email; + $contact->priv=0; + + $result=$contact->create($user); + } + } + + if ($result >= 0) + { + $db->commit(); + } + else + { + $db->rollback(); + $langs->load("errors"); + echo $langs->trans($contact->error); + echo $langs->trans($soc->error); + } } + + echo 'socid='.$soc->id; // Create element (order or bill) - + /* foreach ($_SESSION[$_GET['transaction_id']] as $key => $value) { echo $key.': '.$value.'
'; } + */ } else if ($_GET['action'] == 'showdetails') diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index 48569f43ee1..7cd72956dbd 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -128,7 +128,8 @@ class Societe extends CommonObject var $default_lang; var $canvas; - + + var $ref_ext; var $import_key; var $logo; @@ -201,11 +202,12 @@ class Societe extends CommonObject if ($result >= 0) { - $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe (nom, entity, datec, datea, fk_user_creat, canvas, status)"; + $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe (nom, entity, datec, datea, fk_user_creat, canvas, status, ref_ext)"; $sql.= " VALUES ('".$this->db->escape($this->name)."', ".$conf->entity.", '".$this->db->idate($now)."', '".$this->db->idate($now)."'"; $sql.= ", ".($user->id > 0 ? "'".$user->id."'":"null"); $sql.= ", ".($this->canvas ? "'".$this->canvas."'":"null"); $sql.= ", ".$this->status; + $sql.= ", ".($this->ref_ext ? "'".$this->ref_ext."'":"null"); $sql.= ")"; dol_syslog("Societe::create sql=".$sql);