25 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
36 public $element =
'intracommreport';
41 public $table_element =
'intracommreport';
46 public $fk_element =
'fk_intracommreport';
51 public $declaration_number;
57 public $ismultientitymanaged = 1;
69 public static $type = array(
70 'introduction'=>
'Introduction',
71 'expedition'=>
'Expédition'
82 $this->exporttype =
'deb';
91 public function create($user, $notrigger = 0)
113 public function delete($id, $user, $notrigger = 0)
126 public function getXML($mode =
'O', $type =
'introduction', $period_reference =
'')
128 global $conf, $mysoc;
131 $party_id = substr(strtr($mysoc->tva_intra, array(
' '=>
'')), 0, 4).$mysoc->idprof2;
132 $declarant = substr($mysoc->managers, 0, 14);
137 $e =
new SimpleXMLElement(
'<?xml version="1.0" encoding="utf-8" standalone="yes"?><INSTAT></INSTAT>');
139 $enveloppe = $e->addChild(
'Envelope');
140 $enveloppe->addChild(
'envelopeId', $conf->global->INTRACOMMREPORT_NUM_AGREMENT);
141 $date_time = $enveloppe->addChild(
'DateTime');
142 $date_time->addChild(
'date', date(
'Y-m-d'));
143 $date_time->addChild(
'time', date(
'H:i:s'));
144 $party = $enveloppe->addChild(
'Party');
145 $party->addAttribute(
'partyType', $conf->global->INTRACOMMREPORT_TYPE_ACTEUR);
146 $party->addAttribute(
'partyRole', $conf->global->INTRACOMMREPORT_ROLE_ACTEUR);
147 $party->addChild(
'partyId', $party_id);
148 $party->addChild(
'partyName', $declarant);
149 $enveloppe->addChild(
'softwareUsed',
'Dolibarr');
150 $declaration = $enveloppe->addChild(
'Declaration');
151 $declaration->addChild(
'declarationId', $id_declaration);
152 $declaration->addChild(
'referencePeriod', $period_reference);
153 if ($conf->global->INTRACOMMREPORT_TYPE_ACTEUR ===
'PSI') {
158 $declaration->addChild(
'PSIId', $psiId);
159 $function = $declaration->addChild(
'Function');
160 $functionCode = $function->addChild(
'functionCode', $mode);
161 $declaration->addChild(
'declarationTypeCode', $conf->global->{
'INTRACOMMREPORT_NIV_OBLIGATION_'.strtoupper($type)});
162 $declaration->addChild(
'flowCode', ($type ==
'introduction' ?
'A' :
'D'));
163 $declaration->addChild(
'currencyCode', $conf->global->MAIN_MONNAIE);
167 $res = $this->
addItemsFact($declaration, $type, $period_reference);
170 $this->errors = array_unique($this->errors);
187 public function getXMLDes($period_year, $period_month, $type_declaration =
'expedition')
191 $e =
new SimpleXMLElement(
'<?xml version="1.0" encoding="utf-8" ?><fichier_des></fichier_des>');
193 $declaration_des = $e->addChild(
'declaration_des');
194 $declaration_des->addChild(
'num_des', self::getDeclarationNumber($this->numero_declaration));
195 $declaration_des->addChild(
'num_tvaFr', $mysoc->tva_intra);
196 $declaration_des->addChild(
'mois_des', $period_month);
197 $declaration_des->addChild(
'an_des', $period_year);
200 $res = $this->addItemsFact($declaration_des, $type_declaration, $period_year.
'-'.$period_month,
'des');
203 $this->errors = array_unique($this->errors);
221 public function addItemsFact(&$declaration, $type, $period_reference, $exporttype =
'deb')
225 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
227 $sql = $this->getSQLFactLines($type, $period_reference, $exporttype);
234 if ($this->
db->num_rows(
$resql) <= 0) {
235 $this->errors[] =
'No data for this period';
239 if ($exporttype ==
'deb' && $conf->global->INTRACOMMREPORT_CATEG_FRAISDEPORT > 0) {
241 $categ_fraisdeport->fetch($conf->global->INTRACOMMREPORT_CATEG_FRAISDEPORT);
242 $TLinesFraisDePort = array();
245 while ($res = $this->
db->fetch_object(
$resql)) {
246 if ($exporttype ==
'des') {
247 $this->addItemXMlDes($declaration, $res, $i);
249 if (empty($res->fk_pays)) {
251 $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>';
253 if ($conf->global->INTRACOMMREPORT_CATEG_FRAISDEPORT > 0 && $categ_fraisdeport->containsObject(
'product', $res->id_prod)) {
254 $TLinesFraisDePort[] = $res;
256 $this->addItemXMl($declaration, $res, $i,
'');
264 if (!empty($TLinesFraisDePort)) {
265 $this->addItemFraisDePort($declaration, $TLinesFraisDePort, $type, $categ_fraisdeport, $i);
268 if (count($this->errors) > 0) {
286 global $mysoc, $conf;
288 if ($type ==
'expedition' || $exporttype ==
'des') {
289 $sql =
"SELECT f.ref as refinvoice, f.total_ht";
291 $table_extraf =
'facture_extrafields';
292 $tabledet =
'facturedet';
293 $field_link =
'fk_facture';
295 $sql =
"SELECT f.ref_supplier as refinvoice, f.total_ht";
296 $table =
'facture_fourn';
297 $table_extraf =
'facture_fourn_extrafields';
298 $tabledet =
'facture_fourn_det';
299 $field_link =
'fk_facture_fourn';
301 $sql .=
", l.fk_product, l.qty
302 , p.weight, p.rowid as id_prod, p.customcode
303 , s.rowid as id_client, s.nom, s.zip, s.fk_pays, s.tva_intra
306 FROM ".MAIN_DB_PREFIX.$tabledet.
" l
307 INNER JOIN ".MAIN_DB_PREFIX.$table.
" f ON (f.rowid = l.".$this->
db->escape($field_link).
")
308 LEFT JOIN ".MAIN_DB_PREFIX.$table_extraf.
" ext ON (ext.fk_object = f.rowid)
309 INNER JOIN ".MAIN_DB_PREFIX.
"product p ON (p.rowid = l.fk_product)
310 INNER JOIN ".MAIN_DB_PREFIX.
"societe s ON (s.rowid = f.fk_soc)
311 LEFT JOIN ".MAIN_DB_PREFIX.
"c_country c ON (c.rowid = s.fk_pays)
312 WHERE f.fk_statut > 0
313 AND l.product_type = ".($exporttype ==
"des" ? 1 : 0).
"
314 AND f.entity = ".((
int) $conf->entity).
"
315 AND (s.fk_pays <> ".((
int) $mysoc->country_id).
" OR s.fk_pays IS NULL)
316 AND f.datef BETWEEN '".$this->
db->escape($period_reference).
"-01' AND '".$this->
db->escape($period_reference).
"-".date(
't').
"'";
330 public function addItemXMl(&$declaration, &$res, $i, $code_douane_spe =
'')
332 $item = $declaration->addChild(
'Item');
333 $item->addChild(
'itemNumber', $i);
334 $cn8 = $item->addChild(
'CN8');
335 if (empty($code_douane_spe)) {
336 $code_douane = $res->customcode;
338 $code_douane = $code_douane_spe;
340 $cn8->addChild(
'CN8Code', $code_douane);
341 $item->addChild(
'MSConsDestCode', $res->code);
342 $item->addChild(
'countryOfOriginCode', substr($res->zip, 0, 2));
343 $item->addChild(
'netMass', round($res->weight * $res->qty));
344 $item->addChild(
'quantityInSU', $res->qty);
345 $item->addChild(
'invoicedAmount', round($res->total_ht));
347 if (!empty($res->tva_intra)) {
348 $item->addChild(
'partnerId', $res->tva_intra);
350 $item->addChild(
'statisticalProcedureCode',
'11');
351 $nature_of_transaction = $item->addChild(
'NatureOfTransaction');
352 $nature_of_transaction->addChild(
'natureOfTransactionACode', 1);
353 $nature_of_transaction->addChild(
'natureOfTransactionBCode', 1);
354 $item->addChild(
'modeOfTransportCode', $res->mode_transport);
355 $item->addChild(
'regionCode', substr($res->zip, 0, 2));
368 $item = $declaration->addChild(
'ligne_des');
369 $item->addChild(
'numlin_des', $i);
370 $item->addChild(
'valeur', round($res->total_ht));
371 $item->addChild(
'partner_des', $res->tva_intra);
384 public function addItemFraisDePort(&$declaration, &$TLinesFraisDePort, $type, &$categ_fraisdeport, $i)
389 if ($type ==
'expedition') {
391 $tabledet =
'facturedet';
392 $field_link =
'fk_facture';
395 $table =
'facture_fourn';
396 $tabledet =
'facture_fourn_det';
397 $field_link =
'fk_facture_fourn';
398 $more_sql =
'f.ref_supplier';
401 foreach ($TLinesFraisDePort as $res) {
402 $sql =
"SELECT p.customcode
403 FROM ".MAIN_DB_PREFIX.$tabledet.
" d
404 INNER JOIN ".MAIN_DB_PREFIX.$table.
" f ON (f.rowid = d.".$this->
db->escape($field_link).
")
405 INNER JOIN ".MAIN_DB_PREFIX.
"product p ON (p.rowid = d.fk_product)
406 WHERE d.fk_product IS NOT NULL
407 AND f.entity = ".((int) $conf->entity).
"
408 AND ".$more_sql.
" = '".$this->
db->escape($res->refinvoice).
"'
411 SELECT MAX(d.total_ht)
412 FROM ".MAIN_DB_PREFIX.$tabledet.
" d
413 INNER JOIN ".MAIN_DB_PREFIX.$table.
" f ON (f.rowid = d.".$this->
db->escape($field_link).
")
414 WHERE d.fk_product IS NOT NULL
415 AND ".$more_sql.
" = '".$this->
db->escape($res->refinvoice).
"'
416 AND d.fk_product NOT IN
419 FROM ".MAIN_DB_PREFIX.
"categorie_product
420 WHERE fk_categorie = ".((int) $categ_fraisdeport->id).
"
425 $ress = $this->
db->fetch_object(
$resql);
427 $this->addItemXMl($declaration, $res, $i, $ress->customcode);
440 $sql =
"SELECT MAX(numero_declaration) as max_declaration_number FROM ".MAIN_DB_PREFIX.$this->table_element;
441 $sql .=
" WHERE exporttype = '".$this->db->escape($this->exporttype).
"'";
444 $res = $this->
db->fetch_object(
$resql);
447 return ($res->max_declaration_number + 1);
458 return str_pad($number, 6, 0, STR_PAD_LEFT);
468 $name = $this->periode.
'.xml';
469 $fname = sys_get_temp_dir().
'/'.$name;
470 $f = fopen($fname,
'w+');
471 fwrite($f, $this->content_xml);
474 header(
'Content-Description: File Transfer');
475 header(
'Content-Type: application/xml');
476 header(
'Content-Disposition: attachment; filename="'.$name.
'"');
477 header(
'Expires: 0');
478 header(
'Cache-Control: must-revalidate');
479 header(
'Pragma: public');
480 header(
'Content-Length: '.filesize($fname));