Hi!
I have created a php, based on the examples provided (installDir/dev/examples) that intends to cerate a new facture. It receives via POST the necesary data about the client and products, First checks wether the client already exist, if not it creates it in Dolibar database, in any case, the code just selects it to make it the client in the facture. It does the same with products, based on an array. The problem i have occurs when the code is executed in a server ( when I execute it in a LOCAL installation works fine!!!) it just creates only one facture, and none each time after first usage. For products and clientes still works, but the facture part seems to have the problem. Here is the code, I would apreciate any help, you can teste to see what Im talking about. Just copy the code in a php file into the installDir/dev/examples directory and execute it, after that see on the dolibar facture section or in database the result. Remember to change to the admin user name you have or create one called “admin”:
<?php
//Crear una Factura en Dolibar CRM - copied
//Informacion del Cliente
$cliente_id = 15;
$cliente_nombre = 'Ignacio';
//Informacion del Producto
class producto{
public $prod_ref;
public $prod_nombre;
public $prod_precio;
public $prod_IVA;
}
class Factura {
public static function crear() {
$productA = new producto();
$productA->prod_ref = 'DesH6';
$productA->prod_nombre = 'Axe Fragancia XX';
$productA->prod_precio = 69.99;
$productA->prod_IVA = 21;
}
}
$productA = new producto();
$productA->prod_ref = 'DesH6';
$productA->prod_nombre = 'Axe Fragancia XX';
$productA->prod_precio = 69.99;
$productA->prod_IVA = 21;
$productB = new producto();
$productB->prod_ref = 'Perf98';
$productB->prod_nombre = 'Pefume';
$productB->prod_precio = 30.01 ;
$productB->prod_IVA = 21;
$productos= array();
array_push($productos,$productA);
array_push($productos,$productB);
$sapi_type = php_sapi_name();
$script_file = basename(__FILE__);
$path=dirname(__FILE__).'/';
require_once($path."../../htdocs/master.inc.php");
require_once(DOL_DOCUMENT_ROOT."/societe/class/societe.class.php");
require_once(DOL_DOCUMENT_ROOT."/product/class/product.class.php");
require_once(DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php");
$langs->load("main"); // To load language file for default language
@set_time_limit(0);
$result=$user->fetch('','admin'); // Load user for login 'admin'. Comment line to run as anonymous user.
if (! $result > 0) { dol_print_error('',$user->error); exit; }
$user->getrights();
$db->begin();
$mycompany = new Societe($db);
$mycompany->code_client = $cliente_id;//Es UNICO
$mycompany->nom = $cliente_nombre;
$mycompany->client = 3;
$mycompany_id = $mycompany->create($user);
if ($mycompany_id > 0)
$db->commit();
else
{
/*Definir en product.class.php
function getProductIdByRef($code_client)
{
$sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX."societe";
$sql.= ' WHERE code_client = "'.$code_client.'"';
$resql = $this->db->query($sql);
if ($resql)
{
$row = $this->db->fetch_row($resql);
return $row[0];
}
}
*/
$mycompany_id=$mycompany->getProductIdByRef($cliente_id);
$db->rollback();
$mycompany->update($mycompany_id,$user);
$db->commit();
}
foreach($productos as $producto)
{
$myproduct = new Product($db);
$myproduct->ref = $producto->prod_ref; //Es UNICO
$myproduct->libelle = $producto->prod_nombre;
$myproduct->price = $producto->prod_precio;
$myproduct->price_base_type = 'HT'; //"HT" ==>Net of tax; "TTC"==>Inc. tax
$myproduct->tva_tx = $producto->prod_IVA;
$myproduct->type = 0;
$myproduct->status = 1; //1=> For Purchase; 0 =>Not For Purchase
$myproduct->description = $producto->prod_nombre;
$myproduct->note = $producto->prod_nombre;
$myproduct->weight = 0;
$myproduct->weight_units = 0;
$myproduct->country_id = 23;//23=> Argentina
$product_id = $myproduct->create($user);
if ($product_id > 0)
$db->commit();
else
{
/*Definir en product.class.php
function getProductIdByRef($ref)
{
$sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX."product";
$sql.= ' WHERE ref = "'.$ref.'"';
$resql = $this->db->query($sql);
if ($resql)
{
$row = $this->db->fetch_row($resql);
return $row[0];
}
}
*/
$product_id=$myproduct->getProductIdByRef($producto->prod_ref);
$db->rollback();
$myproduct->update($product_id,$user);
$db->commit();
}
}
$factura = new Facture($db);
$factura->ref = "1234";
$factura->socid = $mycompany_id;//$cliente_id; // Put id of third party (rowid in llx_societe table)
$factura->date = mktime();
$factura->cond_reglement_id = 1;
$factura->mode_reglement_code = 'crabe';
foreach($productos as $producto)
{
$line1 = new FactureLigne($db);
$line1->desc = $producto->prod_nombre;
$line1->product_desc = $producto->prod_nombre;
$line1->total_ht = $producto->prod_precio;
$line1->tva_tx = $producto->prod_IVA;
$line1->qty = 1;
$line1->remise_percent = 0;
$line1->total_tva = 10;
$line1->total_ttc = 110;
$factura->lines[] = $line1;
}
$factura_id = $factura->create($user);
if ($factura_id > 0)
$db->commit();
else
$db->rollback();
$db->close();
?>
Thanks in advanced!