Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 55 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
ApiPurchaseController | |
0.00% |
0 / 55 |
|
0.00% |
0 / 1 |
56 | |
0.00% |
0 / 1 |
apiSupplierBillUpload | |
0.00% |
0 / 55 |
|
0.00% |
0 / 1 |
56 |
1 | <?php |
2 | |
3 | /** |
4 | * Jingga |
5 | * |
6 | * PHP Version 8.1 |
7 | * |
8 | * @package Modules\Billing |
9 | * @copyright Dennis Eichhorn |
10 | * @license OMS License 2.0 |
11 | * @version 1.0.0 |
12 | * @link https://jingga.app |
13 | */ |
14 | declare(strict_types=1); |
15 | |
16 | namespace Modules\Billing\Controller; |
17 | |
18 | use Modules\Billing\Models\BillStatus; |
19 | use Modules\Billing\Models\BillTransferType; |
20 | use Modules\Billing\Models\BillTypeMapper; |
21 | use Modules\Billing\Models\SettingsEnum; |
22 | use phpOMS\Message\Http\HttpRequest; |
23 | use phpOMS\Message\Http\HttpResponse; |
24 | use phpOMS\Message\RequestAbstract; |
25 | use phpOMS\Message\ResponseAbstract; |
26 | use phpOMS\System\OperatingSystem; |
27 | use phpOMS\System\SystemType; |
28 | use phpOMS\System\SystemUtils; |
29 | use phpOMS\Uri\HttpUri; |
30 | |
31 | /** |
32 | * Billing class. |
33 | * |
34 | * @package Modules\Billing |
35 | * @license OMS License 2.0 |
36 | * @link https://jingga.app |
37 | * @since 1.0.0 |
38 | */ |
39 | final class ApiPurchaseController extends Controller |
40 | { |
41 | /** |
42 | * Api method to create bill files |
43 | * |
44 | * @param RequestAbstract $request Request |
45 | * @param ResponseAbstract $response Response |
46 | * @param array $data Generic data |
47 | * |
48 | * @return void |
49 | * |
50 | * @api |
51 | * |
52 | * @throws \Exception |
53 | * |
54 | * @since 1.0.0 |
55 | */ |
56 | public function apiSupplierBillUpload(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void |
57 | { |
58 | /** @var \Model\Setting $setting */ |
59 | $setting = $this->app->appSettings->get( |
60 | names: SettingsEnum::ORIGINAL_MEDIA_TYPE, |
61 | module: self::NAME |
62 | ); |
63 | |
64 | $originalType = $request->getDataInt('type') ?? ((int) $setting->content); |
65 | |
66 | /** @var \Modules\Billing\Models\BillType $purchaseTransferType */ |
67 | $purchaseTransferType = BillTypeMapper::get() |
68 | ->where('transferType', BillTransferType::PURCHASE) |
69 | ->limit(1) |
70 | ->execute(); |
71 | |
72 | $files = $request->files; |
73 | foreach ($files as $file) { |
74 | // Create default bill |
75 | $billRequest = new HttpRequest(new HttpUri('')); |
76 | $billRequest->header->account = $request->header->account; |
77 | $billRequest->header->l11n = $request->header->l11n; |
78 | $billRequest->setData('supplier', 0); |
79 | $billRequest->setData('status', BillStatus::UNPARSED); |
80 | $billRequest->setData('type', $purchaseTransferType->id); |
81 | |
82 | $billResponse = new HttpResponse(); |
83 | $billResponse->header->l11n = $response->header->l11n; |
84 | |
85 | $this->app->moduleManager->get('Billing', 'Api')->apiBillCreate($billRequest, $billResponse, $data); |
86 | |
87 | $billId = $billResponse->getDataArray('')['response']->id; |
88 | |
89 | // Upload and assign document to bill |
90 | $mediaRequest = new HttpRequest(); |
91 | $mediaRequest->header->account = $request->header->account; |
92 | $mediaRequest->header->l11n = $request->header->l11n; |
93 | $mediaRequest->addFile($file); |
94 | |
95 | $mediaResponse = new HttpResponse(); |
96 | $mediaResponse->header->l11n = $response->header->l11n; |
97 | |
98 | $mediaRequest->setData('bill', $billId); |
99 | $mediaRequest->setData('type', $originalType); |
100 | $mediaRequest->setData('parse_content', true, true); |
101 | $this->app->moduleManager->get('Billing', 'Api')->apiMediaAddToBill($mediaRequest, $mediaResponse, $data); |
102 | |
103 | /** @var \Modules\Media\Models\Media[] $uploaded */ |
104 | $uploaded = $mediaResponse->getDataArray('')['response']['upload']; |
105 | if (empty($uploaded)) { |
106 | throw new \Exception(); |
107 | } |
108 | |
109 | $in = \reset($uploaded)->getAbsolutePath(); // pdf parsed content is available in $in->content |
110 | if (!\is_file($in)) { |
111 | throw new \Exception(); |
112 | } |
113 | |
114 | // Create internal document |
115 | $billResponse = new HttpResponse(); |
116 | $billRequest = new HttpRequest(new HttpUri('')); |
117 | |
118 | $billRequest->header->account = $request->header->account; |
119 | $billRequest->setData('bill', $billId); |
120 | |
121 | $this->app->moduleManager->get('Billing', 'Api')->apiBillPdfArchiveCreate($billRequest, $billResponse); |
122 | |
123 | // Offload bill parsing to cli |
124 | $cliPath = \realpath(__DIR__ . '/../../../cli.php'); |
125 | if ($cliPath === false) { |
126 | return; |
127 | } |
128 | |
129 | try { |
130 | SystemUtils::runProc( |
131 | OperatingSystem::getSystem() === SystemType::WIN ? 'php.exe' : 'php', |
132 | \escapeshellarg($cliPath) |
133 | . ' /billing/bill/purchase/parse ' |
134 | . '-i ' . \escapeshellarg((string) $billId), |
135 | true |
136 | ); |
137 | } catch (\Throwable $t) { |
138 | $this->app->logger->error($t->getMessage()); |
139 | } |
140 | } |
141 | } |
142 | } |