27require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
38 public $element =
'intracommreport';
43 public $table_element =
'intracommreport';
48 public $fk_element =
'fk_intracommreport';
54 public $ismultientitymanaged = 1;
56 public $picto =
'intracommreport';
68 public $declaration_number;
70 public $type_declaration;
83 public static $type = array(
84 'introduction'=>
'Introduction',
85 'expedition'=>
'Expédition'
97 $this->exporttype =
'deb';
107 public function create($user, $notrigger = 0)
131 public function delete($id, $user, $notrigger = 0)
144 public function getXML($mode =
'O', $type =
'introduction', $period_reference =
'')
146 global $conf, $mysoc;
149 $party_id = substr(strtr($mysoc->tva_intra, array(
' '=>
'')), 0, 4).$mysoc->idprof2;
150 $declarant = substr($mysoc->managers, 0, 14);
155 $e =
new SimpleXMLElement(
'<?xml version="1.0" encoding="utf-8" standalone="yes"?><INSTAT></INSTAT>');
157 $enveloppe = $e->addChild(
'Envelope');
158 $enveloppe->addChild(
'envelopeId', $conf->global->INTRACOMMREPORT_NUM_AGREMENT);
159 $date_time = $enveloppe->addChild(
'DateTime');
160 $date_time->addChild(
'date', date(
'Y-m-d'));
161 $date_time->addChild(
'time', date(
'H:i:s'));
162 $party = $enveloppe->addChild(
'Party');
163 $party->addAttribute(
'partyType', $conf->global->INTRACOMMREPORT_TYPE_ACTEUR);
164 $party->addAttribute(
'partyRole', $conf->global->INTRACOMMREPORT_ROLE_ACTEUR);
165 $party->addChild(
'partyId', $party_id);
166 $party->addChild(
'partyName', $declarant);
167 $enveloppe->addChild(
'softwareUsed',
'Dolibarr');
168 $declaration = $enveloppe->addChild(
'Declaration');
169 $declaration->addChild(
'declarationId', $id_declaration);
170 $declaration->addChild(
'referencePeriod', $period_reference);
171 if ($conf->global->INTRACOMMREPORT_TYPE_ACTEUR ===
'PSI') {
176 $declaration->addChild(
'PSIId', $psiId);
177 $function = $declaration->addChild(
'Function');
178 $functionCode = $function->addChild(
'functionCode', $mode);
179 $declaration->addChild(
'declarationTypeCode', $conf->global->{
'INTRACOMMREPORT_NIV_OBLIGATION_'.strtoupper($type)});
180 $declaration->addChild(
'flowCode', ($type ==
'introduction' ?
'A' :
'D'));
181 $declaration->addChild(
'currencyCode', $conf->global->MAIN_MONNAIE);
185 $res = $this->
addItemsFact($declaration, $type, $period_reference);
188 $this->errors = array_unique($this->errors);
205 public function getXMLDes($period_year, $period_month, $type_declaration =
'expedition')
209 $e =
new SimpleXMLElement(
'<?xml version="1.0" encoding="utf-8" ?><fichier_des></fichier_des>');
211 $declaration_des = $e->addChild(
'declaration_des');
212 $declaration_des->addChild(
'num_des', self::getDeclarationNumber($this->numero_declaration));
213 $declaration_des->addChild(
'num_tvaFr', $mysoc->tva_intra);
214 $declaration_des->addChild(
'mois_des', $period_month);
215 $declaration_des->addChild(
'an_des', $period_year);
218 $res = $this->addItemsFact($declaration_des, $type_declaration, $period_year.
'-'.$period_month,
'des');
221 $this->errors = array_unique($this->errors);
239 public function addItemsFact(&$declaration, $type, $period_reference, $exporttype =
'deb')
243 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
245 $sql = $this->getSQLFactLines($type, $period_reference, $exporttype);
247 $resql = $this->db->query($sql);
252 if ($this->db->num_rows($resql) <= 0) {
253 $this->errors[] =
'No data for this period';
257 if ($exporttype ==
'deb' && $conf->global->INTRACOMMREPORT_CATEG_FRAISDEPORT > 0) {
258 $categ_fraisdeport =
new Categorie($this->db);
259 $categ_fraisdeport->fetch($conf->global->INTRACOMMREPORT_CATEG_FRAISDEPORT);
260 $TLinesFraisDePort = array();
263 while ($res = $this->db->fetch_object($resql)) {
264 if ($exporttype ==
'des') {
265 $this->addItemXMlDes($declaration, $res, $i);
267 if (empty($res->fk_pays)) {
269 $this->errors[] =
'Country not filled in for the third party <a href="'.dol_buildpath(
'/societe/soc.php', 1).
'?socid='.$res->id_client.
'">'.$res->nom.
'</a>';
271 if ($conf->global->INTRACOMMREPORT_CATEG_FRAISDEPORT > 0 && $categ_fraisdeport->containsObject(
'product', $res->id_prod)) {
272 $TLinesFraisDePort[] = $res;
274 $this->addItemXMl($declaration, $res, $i,
'');
282 if (!empty($TLinesFraisDePort)) {
283 $this->addItemFraisDePort($declaration, $TLinesFraisDePort, $type, $categ_fraisdeport, $i);
286 if (count($this->errors) > 0) {
304 global $mysoc, $conf;
306 if ($type ==
'expedition' || $exporttype ==
'des') {
307 $sql =
"SELECT f.ref as refinvoice, f.total_ht";
309 $table_extraf =
'facture_extrafields';
310 $tabledet =
'facturedet';
311 $field_link =
'fk_facture';
313 $sql =
"SELECT f.ref_supplier as refinvoice, f.total_ht";
314 $table =
'facture_fourn';
315 $table_extraf =
'facture_fourn_extrafields';
316 $tabledet =
'facture_fourn_det';
317 $field_link =
'fk_facture_fourn';
319 $sql .=
", l.fk_product, l.qty
320 , p.weight, p.rowid as id_prod, p.customcode
321 , s.rowid as id_client, s.nom, s.zip, s.fk_pays, s.tva_intra
324 FROM ".MAIN_DB_PREFIX.$tabledet.
" l
325 INNER JOIN ".MAIN_DB_PREFIX.$table.
" f ON (f.rowid = l.".$this->db->escape($field_link).
")
326 LEFT JOIN ".MAIN_DB_PREFIX.$table_extraf.
" ext ON (ext.fk_object = f.rowid)
327 INNER JOIN ".MAIN_DB_PREFIX.
"product p ON (p.rowid = l.fk_product)
328 INNER JOIN ".MAIN_DB_PREFIX.
"societe s ON (s.rowid = f.fk_soc)
329 LEFT JOIN ".MAIN_DB_PREFIX.
"c_country c ON (c.rowid = s.fk_pays)
330 WHERE f.fk_statut > 0
331 AND l.product_type = ".($exporttype ==
"des" ? 1 : 0).
"
332 AND f.entity = ".((
int) $conf->entity).
"
333 AND (s.fk_pays <> ".((
int) $mysoc->country_id).
" OR s.fk_pays IS NULL)
334 AND f.datef BETWEEN '".$this->db->escape($period_reference).
"-01' AND '".$this->db->escape($period_reference).
"-".date(
't').
"'";
348 public function addItemXMl(&$declaration, &$res, $i, $code_douane_spe =
'')
350 $item = $declaration->addChild(
'Item');
351 $item->addChild(
'itemNumber', $i);
352 $cn8 = $item->addChild(
'CN8');
353 if (empty($code_douane_spe)) {
354 $code_douane = $res->customcode;
356 $code_douane = $code_douane_spe;
358 $cn8->addChild(
'CN8Code', $code_douane);
359 $item->addChild(
'MSConsDestCode', $res->code);
360 $item->addChild(
'countryOfOriginCode', substr($res->zip, 0, 2));
361 $item->addChild(
'netMass', round($res->weight * $res->qty));
362 $item->addChild(
'quantityInSU', $res->qty);
363 $item->addChild(
'invoicedAmount', round($res->total_ht));
365 if (!empty($res->tva_intra)) {
366 $item->addChild(
'partnerId', $res->tva_intra);
368 $item->addChild(
'statisticalProcedureCode',
'11');
369 $nature_of_transaction = $item->addChild(
'NatureOfTransaction');
370 $nature_of_transaction->addChild(
'natureOfTransactionACode', 1);
371 $nature_of_transaction->addChild(
'natureOfTransactionBCode', 1);
372 $item->addChild(
'modeOfTransportCode', $res->mode_transport);
373 $item->addChild(
'regionCode', substr($res->zip, 0, 2));
386 $item = $declaration->addChild(
'ligne_des');
387 $item->addChild(
'numlin_des', $i);
388 $item->addChild(
'valeur', round($res->total_ht));
389 $item->addChild(
'partner_des', $res->tva_intra);
402 public function addItemFraisDePort(&$declaration, &$TLinesFraisDePort, $type, &$categ_fraisdeport, $i)
407 if ($type ==
'expedition') {
409 $tabledet =
'facturedet';
410 $field_link =
'fk_facture';
413 $table =
'facture_fourn';
414 $tabledet =
'facture_fourn_det';
415 $field_link =
'fk_facture_fourn';
416 $more_sql =
'f.ref_supplier';
419 foreach ($TLinesFraisDePort as $res) {
420 $sql =
"SELECT p.customcode
421 FROM ".MAIN_DB_PREFIX.$tabledet.
" d
422 INNER JOIN ".MAIN_DB_PREFIX.$table.
" f ON (f.rowid = d.".$this->db->escape($field_link).
")
423 INNER JOIN ".MAIN_DB_PREFIX.
"product p ON (p.rowid = d.fk_product)
424 WHERE d.fk_product IS NOT NULL
425 AND f.entity = ".((int) $conf->entity).
"
426 AND ".$more_sql.
" = '".$this->db->escape($res->refinvoice).
"'
429 SELECT MAX(d.total_ht)
430 FROM ".MAIN_DB_PREFIX.$tabledet.
" d
431 INNER JOIN ".MAIN_DB_PREFIX.$table.
" f ON (f.rowid = d.".$this->db->escape($field_link).
")
432 WHERE d.fk_product IS NOT NULL
433 AND ".$more_sql.
" = '".$this->db->escape($res->refinvoice).
"'
434 AND d.fk_product NOT IN
437 FROM ".MAIN_DB_PREFIX.
"categorie_product
438 WHERE fk_categorie = ".((int) $categ_fraisdeport->id).
"
442 $resql = $this->db->query($sql);
443 $ress = $this->db->fetch_object($resql);
445 $this->addItemXMl($declaration, $res, $i, $ress->customcode);
458 $sql =
"SELECT MAX(numero_declaration) as max_declaration_number FROM ".MAIN_DB_PREFIX.$this->table_element;
459 $sql .=
" WHERE exporttype = '".$this->db->escape($this->exporttype).
"'";
460 $resql = $this->db->query($sql);
462 $res = $this->db->fetch_object($resql);
465 return ($res->max_declaration_number + 1);
476 return str_pad($number, 6, 0, STR_PAD_LEFT);
486 $name = $this->periode.
'.xml';
487 $fname = sys_get_temp_dir().
'/'.$name;
488 $f = fopen($fname,
'w+');
489 fwrite($f, $this->content_xml);
492 header(
'Content-Description: File Transfer');
493 header(
'Content-Type: application/xml');
494 header(
'Content-Disposition: attachment; filename="'.$name.
'"');
495 header(
'Expires: 0');
496 header(
'Cache-Control: must-revalidate');
497 header(
'Pragma: public');
498 header(
'Content-Length: '.filesize($fname));
Class to manage categories.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Class to manage Dolibarr database access.
Class to manage intracomm report.
static getDeclarationNumber($number)
Verify declaration number.
addItemsFact(&$declaration, $type, $period_reference, $exporttype='deb')
Add line from invoice.
fetch($id)
Function fetch.
const TYPE_DEB
DEB - Product.
const TYPE_DES
DES - Service.
__construct(DoliDB $db)
Constructor.
generateXMLFile()
Generate XML file.
addItemXMl(&$declaration, &$res, $i, $code_douane_spe='')
Add item for DEB.
getXML($mode='O', $type='introduction', $period_reference='')
Generate XML file.
addItemXMlDes($declaration, &$res, $i)
Add item for DES.
addItemFraisDePort(&$declaration, &$TLinesFraisDePort, $type, &$categ_fraisdeport, $i)
This function adds an item by retrieving the customs code of the product with the highest amount in t...
getNextDeclarationNumber()
Return next reference of declaration not already used (or last reference)
getSQLFactLines($type, $period_reference, $exporttype='deb')
Add invoice line.
create($user, $notrigger=0)
Function create.
getXMLDes($period_year, $period_month, $type_declaration='expedition')
Generate XMLDes file.