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;
71 public $type_declaration;
72 public $numero_declaration;
85 public static $type = array(
86 'introduction'=>
'Introduction',
87 'expedition'=>
'Expédition'
99 $this->exporttype =
'deb';
109 public function create($user, $notrigger = 0)
133 public function delete($id, $user, $notrigger = 0)
146 public function getXML($mode =
'O', $type =
'introduction', $period_reference =
'')
148 global $conf, $mysoc;
151 $party_id = substr(strtr($mysoc->tva_intra, array(
' '=>
'')), 0, 4).$mysoc->idprof2;
152 $declarant = substr($mysoc->managers, 0, 14);
157 $e =
new SimpleXMLElement(
'<?xml version="1.0" encoding="utf-8" standalone="yes"?><INSTAT></INSTAT>');
159 $enveloppe = $e->addChild(
'Envelope');
160 $enveloppe->addChild(
'envelopeId', $conf->global->INTRACOMMREPORT_NUM_AGREMENT);
161 $date_time = $enveloppe->addChild(
'DateTime');
162 $date_time->addChild(
'date', date(
'Y-m-d'));
163 $date_time->addChild(
'time', date(
'H:i:s'));
164 $party = $enveloppe->addChild(
'Party');
165 $party->addAttribute(
'partyType', $conf->global->INTRACOMMREPORT_TYPE_ACTEUR);
166 $party->addAttribute(
'partyRole', $conf->global->INTRACOMMREPORT_ROLE_ACTEUR);
167 $party->addChild(
'partyId', $party_id);
168 $party->addChild(
'partyName', $declarant);
169 $enveloppe->addChild(
'softwareUsed',
'Dolibarr');
170 $declaration = $enveloppe->addChild(
'Declaration');
171 $declaration->addChild(
'declarationId', $id_declaration);
172 $declaration->addChild(
'referencePeriod', $period_reference);
173 if ($conf->global->INTRACOMMREPORT_TYPE_ACTEUR ===
'PSI') {
178 $declaration->addChild(
'PSIId', $psiId);
179 $function = $declaration->addChild(
'Function');
180 $functionCode = $function->addChild(
'functionCode', $mode);
181 $declaration->addChild(
'declarationTypeCode',
getDolGlobalString(
'INTRACOMMREPORT_NIV_OBLIGATION_'.strtoupper($type)));
182 $declaration->addChild(
'flowCode', ($type ==
'introduction' ?
'A' :
'D'));
183 $declaration->addChild(
'currencyCode', $conf->global->MAIN_MONNAIE);
187 $res = $this->
addItemsFact($declaration, $type, $period_reference);
190 $this->errors = array_unique($this->errors);
207 public function getXMLDes($period_year, $period_month, $type_declaration =
'expedition')
211 $e =
new SimpleXMLElement(
'<?xml version="1.0" encoding="utf-8" ?><fichier_des></fichier_des>');
213 $declaration_des = $e->addChild(
'declaration_des');
214 $declaration_des->addChild(
'num_des', self::getDeclarationNumber($this->numero_declaration));
215 $declaration_des->addChild(
'num_tvaFr', $mysoc->tva_intra);
216 $declaration_des->addChild(
'mois_des', $period_month);
217 $declaration_des->addChild(
'an_des', $period_year);
220 $res = $this->
addItemsFact($declaration_des, $type_declaration, $period_year.
'-'.$period_month,
'des');
222 $this->errors = array_unique($this->errors);
240 public function addItemsFact(&$declaration, $type, $period_reference, $exporttype =
'deb')
244 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
248 $resql = $this->db->query($sql);
253 if ($this->db->num_rows($resql) <= 0) {
254 $this->errors[] =
'No data for this period';
258 if ($exporttype ==
'deb' &&
getDolGlobalInt(
'INTRACOMMREPORT_CATEG_FRAISDEPORT') > 0) {
259 $categ_fraisdeport =
new Categorie($this->db);
260 $categ_fraisdeport->fetch($conf->global->INTRACOMMREPORT_CATEG_FRAISDEPORT);
261 $TLinesFraisDePort = array();
264 while ($res = $this->db->fetch_object($resql)) {
265 if ($exporttype ==
'des') {
268 if (empty($res->fk_pays)) {
270 $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>';
272 if (
getDolGlobalInt(
'INTRACOMMREPORT_CATEG_FRAISDEPORT') > 0 && $categ_fraisdeport->containsObject(
'product', $res->id_prod)) {
273 $TLinesFraisDePort[] = $res;
275 $this->
addItemXMl($declaration, $res, $i,
'');
283 if (!empty($TLinesFraisDePort)) {
284 $this->
addItemFraisDePort($declaration, $TLinesFraisDePort, $type, $categ_fraisdeport, $i);
287 if (count($this->errors) > 0) {
305 global $mysoc, $conf;
307 if ($type ==
'expedition' || $exporttype ==
'des') {
308 $sql =
"SELECT f.ref as refinvoice, f.total_ht";
310 $table_extraf =
'facture_extrafields';
311 $tabledet =
'facturedet';
312 $field_link =
'fk_facture';
314 $sql =
"SELECT f.ref_supplier as refinvoice, f.total_ht";
315 $table =
'facture_fourn';
316 $table_extraf =
'facture_fourn_extrafields';
317 $tabledet =
'facture_fourn_det';
318 $field_link =
'fk_facture_fourn';
320 $sql .=
", l.fk_product, l.qty
321 , p.weight, p.rowid as id_prod, p.customcode
322 , s.rowid as id_client, s.nom, s.zip, s.fk_pays, s.tva_intra
325 FROM ".MAIN_DB_PREFIX.$tabledet.
" l
326 INNER JOIN ".MAIN_DB_PREFIX.$table.
" f ON (f.rowid = l.".$this->db->escape($field_link).
")
327 LEFT JOIN ".MAIN_DB_PREFIX.$table_extraf.
" ext ON (ext.fk_object = f.rowid)
328 INNER JOIN ".MAIN_DB_PREFIX.
"product p ON (p.rowid = l.fk_product)
329 INNER JOIN ".MAIN_DB_PREFIX.
"societe s ON (s.rowid = f.fk_soc)
330 LEFT JOIN ".MAIN_DB_PREFIX.
"c_country c ON (c.rowid = s.fk_pays)
331 WHERE f.fk_statut > 0
332 AND l.product_type = ".($exporttype ==
"des" ? 1 : 0).
"
333 AND f.entity = ".((
int) $conf->entity).
"
334 AND (s.fk_pays <> ".((
int) $mysoc->country_id).
" OR s.fk_pays IS NULL)
335 AND f.datef BETWEEN '".$this->db->escape($period_reference).
"-01' AND '".$this->db->escape($period_reference).
"-".date(
't').
"'";
349 public function addItemXMl(&$declaration, &$res, $i, $code_douane_spe =
'')
351 $item = $declaration->addChild(
'Item');
352 $item->addChild(
'itemNumber', $i);
353 $cn8 = $item->addChild(
'CN8');
354 if (empty($code_douane_spe)) {
355 $code_douane = $res->customcode;
357 $code_douane = $code_douane_spe;
359 $cn8->addChild(
'CN8Code', $code_douane);
360 $item->addChild(
'MSConsDestCode', $res->code);
361 $item->addChild(
'countryOfOriginCode', substr($res->zip, 0, 2));
362 $item->addChild(
'netMass', round($res->weight * $res->qty));
363 $item->addChild(
'quantityInSU', $res->qty);
364 $item->addChild(
'invoicedAmount', round($res->total_ht));
366 if (!empty($res->tva_intra)) {
367 $item->addChild(
'partnerId', $res->tva_intra);
369 $item->addChild(
'statisticalProcedureCode',
'11');
370 $nature_of_transaction = $item->addChild(
'NatureOfTransaction');
371 $nature_of_transaction->addChild(
'natureOfTransactionACode', 1);
372 $nature_of_transaction->addChild(
'natureOfTransactionBCode', 1);
373 $item->addChild(
'modeOfTransportCode', $res->mode_transport);
374 $item->addChild(
'regionCode', substr($res->zip, 0, 2));
387 $item = $declaration->addChild(
'ligne_des');
388 $item->addChild(
'numlin_des', $i);
389 $item->addChild(
'valeur', round($res->total_ht));
390 $item->addChild(
'partner_des', $res->tva_intra);
403 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";
459 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element;
460 $sql .=
" WHERE exporttype = '".$this->db->escape($this->exporttype).
"'";
461 $resql = $this->db->query($sql);
463 $res = $this->db->fetch_object($resql);
466 return ($res->max_declaration_number + 1);
477 return str_pad($number, 6, 0, STR_PAD_LEFT);
488 $name = $this->period.
'.xml';
491 $fname = sys_get_temp_dir().
'/'.$name;
493 $f = fopen($fname,
'w+');
494 fwrite($f, $content_xml);
497 header(
'Content-Description: File Transfer');
498 header(
'Content-Type: application/xml');
499 header(
'Content-Disposition: attachment; filename="'.$name.
'"');
500 header(
'Expires: 0');
501 header(
'Cache-Control: must-revalidate');
502 header(
'Pragma: public');
503 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.
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.
generateXMLFile($content_xml)
Generate XML file.
create($user, $notrigger=0)
Function create.
getXMLDes($period_year, $period_month, $type_declaration='expedition')
Generate XMLDes file.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.