Initial xentral_oss_20.3.c9ffacf

This commit is contained in:
Alex
2021-05-21 08:49:41 +02:00
parent 5406e4a551
commit 34e5ac43d9
18884 changed files with 2109867 additions and 0 deletions
+67
View File
@@ -0,0 +1,67 @@
<?php
namespace Xentral\Modules\AmaInvoice;
use Xentral\Core\DependencyInjection\ContainerInterface;
use Xentral\Modules\AmaInvoice\Scheduler\AmaInvoiceTask;
use Xentral\Modules\AmaInvoice\Service\AmaInvoiceService;
use Xentral\Modules\SuperSearch\Wrapper\CompanyConfigWrapper;
final class Bootstrap
{
/**
* @return array
*/
public static function registerServices()
{
return [
'AmaInvoiceService' => 'onInitAmaInvoiceService',
// Cronjob-Tasks
'AmaInvoiceTask' => 'onInitAmaInvoiceTask',
];
}
/**
* @param ContainerInterface $container
*
* @return AmaInvoiceTask
*/
public static function onInitAmaInvoiceTask(ContainerInterface $container)
{
return new AmaInvoiceTask(
$container->get('AmaInvoiceService'),
self::onInitCompanyConfigWrapper($container)
);
}
/**
* @param ContainerInterface $container
*
* @return AmaInvoiceService
*/
public static function onInitAmaInvoiceService(ContainerInterface $container)
{
return new AmaInvoiceService(
$container->get('Database'),
$container->get('FilesystemFactory'),
$container->get('LegacyApplication')
);
}
/**
* @param ContainerInterface $container
*
* @return CompanyConfigWrapper
*/
private static function onInitCompanyConfigWrapper(ContainerInterface $container)
{
/** @var \ApplicationCore $app */
$app = $container->get('LegacyApplication');
return new CompanyConfigWrapper($app->erp);
}
}
@@ -0,0 +1,7 @@
<?php
namespace Xentral\Modules\AmaInvoice\Exception;
interface AmaInvoiceExceptionInterface
{
}
@@ -0,0 +1,7 @@
<?php
namespace Xentral\Modules\AmaInvoice\Exception;
final class AmazonInvoiceServiceException extends \InvalidArgumentException implements AmaInvoiceExceptionInterface
{
}
@@ -0,0 +1,7 @@
<?php
namespace Xentral\Modules\AmaInvoice\Exception;
final class InvalidArgumentException extends \InvalidArgumentException implements AmaInvoiceExceptionInterface
{
}
@@ -0,0 +1,9 @@
<?php
namespace Xentral\Modules\AmaInvoice\Exception;
use RuntimeException;
final class SchedulerTaskAlreadyRunningException extends RuntimeException implements AmaInvoiceExceptionInterface
{
}
@@ -0,0 +1,8 @@
<?php
namespace Xentral\Modules\AmaInvoice\Exception;
final class ThrottlingException extends \InvalidArgumentException implements AmaInvoiceExceptionInterface
{
}
@@ -0,0 +1,297 @@
<?php
declare(strict_types=1);
namespace Xentral\Modules\AmaInvoice\Scheduler;
use Exception;
use Xentral\Modules\AmaInvoice\Exception\ThrottlingException;
use Xentral\Modules\AmaInvoice\Service\AmaInvoiceService;
use Xentral\Modules\AmaInvoice\Exception\SchedulerTaskAlreadyRunningException;
use Xentral\Modules\SuperSearch\Wrapper\CompanyConfigWrapper;
final class AmaInvoiceTask
{
/** @var AmaInvoiceService $service */
private $service;
/** @var CompanyConfigWrapper $config */
private $config;
/** @var bool */
private $useFtp = false;
/**
* AmaInvoiceService constructor.
*
* @param AmaInvoiceService $service
* @param CompanyConfigWrapper $config
*/
public function __construct($service, $config)
{
$this->service = $service;
$this->config = $config;
}
/**
* @throws Exception
*
* @return void
*/
public function execute(): void
{
$taskActive = (int)$this->config->get('amainvoice_task_mutex');
if ($taskActive > 0) {
throw new SchedulerTaskAlreadyRunningException(
'Amainvoice task is already running. Task can only run once at a time.'
);
}
$this->config->set('amainvoice_task_mutex', '1');
$this->syncNewFiles();
$this->config->set('amainvoice_task_mutex', '1');
$this->service->executeImportDateDbEntries(false, false);
$this->config->set('amainvoice_task_mutex', '1');
$this->service->executeImportDateDbEntries(false, true);
$this->config->set('amainvoice_task_mutex', '1');
$this->service->executeImportDateDbEntries(true, false);
}
/**
* @throws Exception
*/
private function syncNewFiles(): void
{
$files = $this->service->getNewFiles();
$csvFiles = [];
$datevFiles = [];
$positions = [];
$datevs = [];
$pdfFiles = [];
$dateFiles = $this->getFirstApiFiles($files);
if ($this->useFtp) {
$csvFiles = $this->getExportCsvs($files);
$datevFiles = $this->getDatevFiles($files);
[$invoicePdfFiles, $returnOrderPdfFiles] = $this->getPdfFiles($files);
$pdfFiles = array_merge($invoicePdfFiles, $returnOrderPdfFiles);
foreach ($csvFiles as $csvFile) {
$position = $this->syncExportCsv($csvFile);
if (!empty($position)) {
foreach ($position as $pos) {
$positions[] = $pos;
}
}
}
foreach ($datevFiles as $datevFile) {
$datev = $this->syncDatevCsv($datevFile);
if (!empty($datev)) {
foreach ($datev as $pos) {
$datevs[] = $pos;
}
}
}
}
foreach (['invoice', 'returnorder'] as $type) {
foreach ($dateFiles[$type] as $file) {
try {
$this->service->executeImportDateFile($file);
}
catch(ThrottlingException $e) {
break 2;
}
}
if ($this->useFtp) {
$datev = empty($datevs[$type]) ? [] : $datevs[$type];
if (empty($positions[$type])) {
continue;
}
foreach ($datev as $amazonOrderId => $documents) {
if (empty($positions[$type][$amazonOrderId])) {
continue;
}
foreach ($documents as $number => $document) {
$numberInvoice = $number;
if (empty($positions[$type][$amazonOrderId][$number])) {
$numberInvoice = substr($number, 3);
if (empty($positions[$type][$amazonOrderId][$numberInvoice])) {
continue;
}
}
try {
$pdfFile = in_array($number . '.pdf', $pdfFiles, true) ? $number . '.pdf' : null;
if (
$this->service->createDocument(
$type,
$amazonOrderId,
$number,
$document,
$positions[$type][$amazonOrderId][$numberInvoice],
$pdfFile
)
) {
if ($pdfFile !== null) {
$this->service->markFile($pdfFile, 'pdf', 'imported');
}
} elseif ($pdfFile !== null) {
$this->service->markFile($pdfFile, 'pdf', 'error');
}
} catch (Exception $e) {
}
}
}
}
}
if ($this->useFtp) {
foreach ($datevFiles as $datevFile) {
$this->service->markFile($datevFile, 'datev', 'imported');
$this->service->cleanFile($datevFile);
}
foreach ($csvFiles as $csvFile) {
$this->service->markFile($csvFile, 'csv', 'imported');
$this->service->cleanFile($csvFile);
}
foreach ($pdfFiles as $pdfFile) {
$this->service->cleanFile($pdfFile);
}
}
}
/**
* @param string $file
*
* @throws Exception
*
* @return array
*/
private function syncDatevCsv($file): array
{
$csvFile = $this->service->getFile($file);
return $this->service->getPositionFromDatevCsv($csvFile);
}
/**
* @param string $file
*
* @throws Exception
* @return array
*/
private function syncExportCsv($file): array
{
$csvFile = $this->service->getFile($file);
return $this->service->getPositionFromExportCsv($csvFile);
}
/**
* @param array $files
*
* @return array[]
*/
private function getPdfFiles($files): array
{
if (empty($files)) {
return [[], []];
}
$ret = [[], []];
foreach ($files as $file) {
if (substr($file, -4) === '.pdf') {
if (strpos($file, 'GS') === 0) {
$ret[1][] = $file;
}
$ret[0][] = $file;
}
}
return $ret;
}
/**
* @param array $files
*
* @return array
*/
private function getDatevFiles($files): array
{
if (empty($files)) {
return [];
}
$ret = [];
foreach ($files as $file) {
if (strpos($file, 'EXTF_ERLOESE') === 0 && substr($file, -4) === '.csv') {
$ret[] = $file;
}
}
return $ret;
}
/**
* @param array $files
*
* @return array|array[]
*/
private function getFirstApiFiles($files): array
{
$ret = [
'invoice' => [],
'returnorder' => [],
];
if (empty($files)) {
return $ret;
}
foreach ($files as $file) {
if (substr($file, -4) === '.inv') {
$ret['invoice'][] = $file;
return $ret;
}
if (substr($file, -4) === '.rem') {
$ret['returnorder'][] = $file;
return $ret;
}
}
return $ret;
}
/**
* @param array $files
*
* @return array
*/
private function getExportCsvs($files): array
{
if (empty($files)) {
return [];
}
$ret = [];
foreach ($files as $file) {
if (strpos($file, 'export_') === 0 && substr($file, -4) === '.csv') {
$ret[] = $file;
}
}
return $ret;
}
/**
* @return void
*/
public function cleanup(): void
{
$this->config->set('amainvoice_task_mutex', '0');
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,52 @@
var AmaInvoice = function ($) {
'use strict';
var me = {
storage: {
tableList: []
},
expertChange: function () {
if ($('#expert').prop('checked')) {
$('.trexpert').show();
} else {
$('.trexpert').hide();
}
},
init: function () {
$('#import').on('click', function () {
me.storage.tableList = [];
$('#amainvoice_list').find(':checked').each(function () {
me.storage.tableList.push($(this).data('id'));
});
if (me.storage.tableList.length > 0) {
$.ajax({
type: 'POST',
url: 'index.php?module=amainvoice&action=list&cmd=importlist',
data: {
list: me.storage.tableList
},
success: function () {
me.storage.tableList = [];
}
});
}
});
if ($('#createorder').prop('checked')) {
$('#expert').prop('checked', true);
}
$('#expert').on('change', function () {
me.expertChange();
});
me.expertChange();
}
};
return {
init: me.init
};
}(jQuery);
$(document).ready(function () {
AmaInvoice.init();
});