-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.php
More file actions
94 lines (75 loc) · 2.47 KB
/
Copy pathimport.php
File metadata and controls
94 lines (75 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
require "../../main.inc.php";
require_once DOL_DOCUMENT_ROOT .
"/custom/invoiceimport/class/emailimport.class.php";
require_once DOL_DOCUMENT_ROOT .
"/custom/invoiceimport/class/invoiceimport.class.php";
$socid = GETPOST("socid", "int");
if (empty($socid)) {
setEventMessages("Fournisseur manquant", null, "errors");
header("Location: " . $_SERVER["HTTP_REFERER"]);
exit();
}
/**
* =========================
* MODE AFFICHAGE FORMULAIRE
* =========================
*/
if (empty($_FILES)) {
llxHeader("", "Import facture");
print load_fiche_titre("Importer une facture PDF");
print '<form method="POST" enctype="multipart/form-data">';
print '<input type="hidden" name="socid" value="' . $socid . '">';
print '<input type="file" name="invoice_pdf" accept="application/pdf" required>';
print "<br><br>";
print '<input type="submit" class="button" value="Importer facture">';
print "</form>";
llxFooter();
exit();
}
/**
* =========================
* MODE TRAITEMENT
* =========================
*/
if (empty($_FILES["invoice_pdf"]["tmp_name"])) {
setEventMessages("Aucun fichier", null, "errors");
header("Location: " . DOL_URL_ROOT . "/fourn/card.php?socid=" . $socid);
exit();
}
$filepath = $_FILES["invoice_pdf"]["tmp_name"];
$filename = $_FILES["invoice_pdf"]["name"];
// Parser Python
$python = getDolGlobalString("INVOICEIMPORT_PYTHON_BIN");
if (empty($python)) {
$python = DOL_DOCUMENT_ROOT . "/custom/invoiceimport/venv/bin/python";
}
$emailImport = new EmailImport($db);
$json = $emailImport->parsePdf($python, $filepath);
if (empty($json)) {
setEventMessages("Erreur parsing PDF", null, "errors");
header("Location: " . DOL_URL_ROOT . "/fourn/card.php?socid=" . $socid);
exit();
}
$data = json_decode($json, true);
if (!$data) {
setEventMessages("Erreur parsing JSON", null, "errors");
header("Location: " . DOL_URL_ROOT . "/fourn/card.php?socid=" . $socid);
exit();
}
// Forcer le fournisseur
$data["forced_socid"] = $socid;
// Stocker en base
$import = new InvoiceImport($db);
$id = $import->create($filename, json_encode($data));
$import->fetch($id);
// Création facture
$res = $import->createSupplierInvoice($user, $filepath, $filename, 0);
if ($res > 0) {
setEventMessages("Facture créée", null, "mesgs");
} else {
setEventMessages("Erreur import", null, "errors");
}
// Retour fiche fournisseur
header("Location: " . DOL_URL_ROOT . "/fourn/card.php?socid=" . $socid);
exit();