Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiPurchaseController
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 1
56
0.00% covered (danger)
0.00%
0 / 1
 apiSupplierBillUpload
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
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 */
14declare(strict_types=1);
15
16namespace Modules\Billing\Controller;
17
18use Modules\Billing\Models\BillStatus;
19use Modules\Billing\Models\BillTransferType;
20use Modules\Billing\Models\BillTypeMapper;
21use Modules\Billing\Models\SettingsEnum;
22use phpOMS\Message\Http\HttpRequest;
23use phpOMS\Message\Http\HttpResponse;
24use phpOMS\Message\RequestAbstract;
25use phpOMS\Message\ResponseAbstract;
26use phpOMS\System\OperatingSystem;
27use phpOMS\System\SystemType;
28use phpOMS\System\SystemUtils;
29use 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 */
39final 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}