Skip to content
Code-Schnipsel Gruppen Projekte
Bestätigt Commit f67e4fd4 erstellt von Henning Leutz's avatar Henning Leutz :martial_arts_uniform:
Dateien durchsuchen

feat: customer files for erp entities

Übergeordneter 727803a5
Keine zugehörigen Branchen gefunden
Keine zugehörigen Tags gefunden
2 Merge Requests!82feat: own template for articles at the article list,!81feat: own template for articles at the article list
<?php
/**
* This file contains package_quiqqer_products_ajax_products_calcNettoPrice
*/
use QUI\ERP\Processes;
/**
* Return the entity files
*
* @param string $hash - Entity hash
* @return array
*/
QUI::$Ajax->registerFunction(
'package_quiqqer_erp_ajax_customerFiles_addFile',
function ($hash, $fileHash) {
$Entity = (new Processes())->getEntity($hash);
$Entity->addCustomerFile($fileHash);
},
['hash', 'fileHash']
);
<?php
/**
* This file contains package_quiqqer_products_ajax_products_calcNettoPrice
*/
use QUI\ERP\Processes;
/**
* Return the entity files
*
* @param string $hash - Entity hash
* @return array
*/
QUI::$Ajax->registerFunction(
'package_quiqqer_erp_ajax_customerFiles_getCustomer',
function ($hash) {
$Entity = (new Processes())->getEntity($hash);
return $Entity->getCustomer()->getUniqueId();
},
['hash']
);
<?php
/**
* This file contains package_quiqqer_products_ajax_products_calcNettoPrice
*/
use QUI\ERP\Processes;
/**
* Return the entity files
*
* @param string $hash - Entity hash
* @return array
*/
QUI::$Ajax->registerFunction(
'package_quiqqer_erp_ajax_customerFiles_getFiles',
function ($hash) {
$Entity = (new Processes())->getEntity($hash);
return $Entity->getCustomerFiles(true);
},
['hash']
);
<?php
/**
* This file contains package_quiqqer_products_ajax_products_calcNettoPrice
*/
use QUI\ERP\Processes;
/**
* Return the entity files
*
* @param string $hash - Entity hash
* @return array
*/
QUI::$Ajax->registerFunction(
'package_quiqqer_erp_ajax_customerFiles_update',
function ($hash, $files) {
$files = json_decode($files, true);
$Entity = (new Processes())->getEntity($hash);
$Entity->setCustomFiles($files);
},
['hash', 'files']
);
define('package/quiqqer/erp/bin/backend/controls/customerFiles/Grid', [
'qui/QUI',
'qui/controls/Control',
'controls/grid/Grid',
'Ajax',
'Locale'
], function(QUI, QUIControl, Grid, QUIAjax, QUILocale) {
'use strict';
const lg = 'quiqqer/erp';
return new Class({
Extends: QUIControl,
Type: 'package/quiqqer/erp/bin/backend/controls/customerFiles/Grid',
Binds: [
'openCustomerFiles',
'$onInject'
],
options: {
hash: false,
customerId: false
},
initialize: function(options) {
this.parent(options);
this.addEvents({
onInject: this.$onInject
});
},
$onInject: function() {
this.getElm().set('data-qui', this.getType());
this.getElm().setStyles({
height: '100%',
width: '100%'
});
const Container = new Element('div', {
style: {
height: '100%',
width: '100%'
}
}).inject(this.getElm());
this.$Grid = new Grid(Container, {
buttons: [
{
text: 'Kundendateien',
title: 'Datei aus Kundendateien auswählen',
events: {
click: this.openCustomerFiles
}
}
],
columnModel: [
{
header: '<span class="fa fa-envelope" title="Als Mailanhang setzen"></span>',
dataIndex: 'mail',
dataType: 'node',
width: 50
}, {
header: QUILocale.get('quiqqer/quiqqer', 'type'),
dataIndex: 'icon',
dataType: 'node',
width: 40
}, {
header: QUILocale.get('quiqqer/quiqqer', 'file'),
dataIndex: 'basename',
dataType: 'string',
width: 300
}, {
header: QUILocale.get('quiqqer/quiqqer', 'size'),
dataIndex: 'filesize_formatted',
dataType: 'string',
width: 100
}, {
header: QUILocale.get('quiqqer/customer', 'window.customer.tbl.header.uploadTime'),
dataIndex: 'uploadTime',
dataType: 'string',
width: 100
}, {
dataIndex: 'hash',
dataType: 'string',
hidden: true
}
]
});
this.$Grid.showLoader();
this.getCustomer().then((customerHash) => {
this.setAttribute('customerId', customerHash);
return this.refresh();
});
},
refresh: function() {
if (!this.$Grid) {
return Promise.resolve();
}
this.$Grid.showLoader();
return new Promise((resolve, reject) => {
QUIAjax.get('package_quiqqer_erp_ajax_customerFiles_getFiles', (files) => {
console.log(files);
const data = [];
files.forEach((entry) => {
data.push({
hash: entry.hash,
basename: entry.file.basename,
filesize_formatted: entry.file.filesize_formatted,
uploadTime: entry.file.uploadTime_formatted,
icon: new Element('img', {
src: entry.file.icon,
styles: {
margin: '5px 0'
}
}),
mail: new Element('input', {
type: 'checkbox',
checked: entry.options.attachToEmail
})
});
});
this.$Grid.setData({
data: data
});
this.getElm().getElements('[type="checkbox"]').addEvent('change', () => {
this.update();
});
this.$Grid.hideLoader();
resolve(files);
}, {
'package': 'quiqqer/erp',
hash: this.getAttribute('hash'),
onError: reject
});
});
},
addFile: function(fileHash) {
this.$Grid.showLoader();
QUIAjax.post('package_quiqqer_erp_ajax_customerFiles_addFile', () => {
this.refresh();
}, {
'package': 'quiqqer/erp',
hash: this.getAttribute('hash'),
fileHash: fileHash
});
},
getCustomer: function() {
return new Promise((resolve, reject) => {
QUIAjax.get('package_quiqqer_erp_ajax_customerFiles_getCustomer', resolve, {
'package': 'quiqqer/erp',
hash: this.getAttribute('hash'),
onError: reject
});
});
},
update: function() {
console.log('update');
const gridData = this.$Grid.getData().map((entry) => {
return {
hash: entry.hash,
options: {
attachToEmail: entry.mail.checked ? 1 : 0
}
};
});
return new Promise((resolve, reject) => {
QUIAjax.get('package_quiqqer_erp_ajax_customerFiles_update', resolve, {
'package': 'quiqqer/erp',
hash: this.getAttribute('hash'),
files: JSON.encode(gridData),
onError: reject
});
});
},
/**
* open customer file window
*/
openCustomerFiles: function() {
console.log(this.getAttribute('customerId'));
require([
'package/quiqqer/customer/bin/backend/controls/customer/userFiles/Window'
], (Window) => {
new Window({
userId: this.getAttribute('customerId'),
events: {
onSelect: (selectedFiles, Win) => {
for (let File of selectedFiles) {
this.addFile(File.hash);
}
Win.close();
}
}
}).open();
});
},
/**
* Get file info
*
* @param {String} fileHash
* @return {Promise}
*/
$getFileData: function(fileHash) {
return new Promise((resolve, reject) => {
QUIAjax.get('package_quiqqer_customer_ajax_backend_files_get', resolve, {
'package': 'quiqqer/customer',
customerId: this.getAttribute('customerId'),
fileHash: fileHash,
onError: reject
});
});
}
});
});
<?php
namespace QUI\ERP;
use QUI;
use QUI\ERP\Customer\CustomerFiles;
use QUI\ERP\User as ErpUser;
use function is_array;
trait ErpEntityCustomerFiles
{
// region needed methods
abstract public function getCustomer(): ?ErpUser;
abstract public function getId(): int;
abstract public function addCustomDataEntry(string $key, mixed $value): void;
abstract public function getCustomDataEntry($key): mixed;
//endregion
// region extended customer file methods
protected array $defaultCustomerFilesOptions = [
'attachToEmail' => false
];
/**
* Add a customer file to this invoice
*
* @param string $fileHash - SHA256 hash of the file basename
* @param array $options (optional) - File options; see $defaultOptions in code for what's possible
*
* @throws Exception
* @throws QUI\Exception
*/
public function addCustomerFile(string $fileHash, array $options = []): void
{
$Customer = $this->getCustomer();
$file = CustomerFiles::getFileByHash($Customer->getId(), $fileHash);
if (empty($file)) {
throw new Exception(
QUI::getLocale()->get('quiqqer/invoice', 'exception.Invoice.addCustomerFile.file_not_found')
);
}
$fileEntry = [
'hash' => $fileHash,
'options' => $this->cleanCustomerFilesOptions($options)
];
$customerFiles = $this->getCustomerFiles();
$customerFiles[] = $fileEntry;
$this->addCustomDataEntry('customer_files', $customerFiles);
}
/**
* @param array $files
* @return void
*
* @throws Exception
* @throws QUI\Exception
*/
public function setCustomFiles(array $files = []): void
{
if (empty($files)) {
return;
}
$this->clearCustomerFiles();
$customerFiles = [];
foreach ($files as $file) {
$options = $file['options'];
if (!is_array($options)) {
$options = [];
}
$fileEntry = [
'hash' => $file['hash'],
'options' => $this->cleanCustomerFilesOptions($options)
];
$customerFiles[] = $fileEntry;
}
$this->addCustomDataEntry('customer_files', $customerFiles);
}
/**
* Clear customer files
*
* @return void
*/
public function clearCustomerFiles(): void
{
try {
$this->addCustomDataEntry('customer_files', []);
} catch (\Exception $e) {
QUI\System\Log::addError($e->getMessage());
}
}
/**
* Get customer files that are attached to this invoice.
*
* @param bool $parsing - true = parses the file hash
* ]
* @return array - Contains file hash and file options
*/
public function getCustomerFiles(bool $parsing = false): array
{
$customerFiles = $this->getCustomDataEntry('customer_files');
if (empty($customerFiles)) {
return [];
}
try {
$Customer = $this->getCustomer();
} catch (\Exception) {
return [];
}
$result = [];
foreach ($customerFiles as $customerFile) {
try {
// check if file is from customer
$file = CustomerFiles::getFileByHash($Customer->getId(), $customerFile['hash']);
if ($parsing) {
$file['uploadTime_formatted'] = QUI::getLocale()->formatDate($file['uploadTime']);
$file['icon'] = QUI\Projects\Media\Utils::getIconByExtension($file['extension']);
$customerFile['file'] = $file;
}
$result[] = $customerFile;
} catch (\Exception) {
}
}
return $result;
}
/**
* cleans a customer files option array
*
* @param array $options
* @return array
*/
protected function cleanCustomerFilesOptions(array $options = []): array
{
// set default options
foreach ($this->defaultCustomerFilesOptions as $k => $v) {
if (!isset($options[$k])) {
$options[$k] = $v;
}
}
// cleanup
foreach ($options as $k => $v) {
if (!isset($this->defaultCustomerFilesOptions[$k])) {
unset($options[$k]);
}
}
return $options;
}
// endregion
}
......@@ -42,7 +42,7 @@ public static function getManufacturerGroupIds(): array
$groupIds = [];
try {
$Conf = QUI::getPackage('quiqqer/erp')->getConfig();
$Conf = QUI::getPackage('quiqqer/erp')->getConfig();
$defaultGroupId = $Conf->get('manufacturers', 'groupId');
if (!empty($defaultGroupId)) {
......@@ -100,7 +100,7 @@ public static function createManufacturer(
): QUI\Users\User {
QUI\Permissions\Permission::checkPermission('quiqqer.erp_manufacturers.create');
$Users = QUI::getUsers();
$Users = QUI::getUsers();
$manufacturerId = $Users::clearUsername($manufacturerId);
// Check ID
......@@ -115,12 +115,12 @@ public static function createManufacturer(
}
$SystemUser = $Users->getSystemUser();
$User = $Users->createChild($manufacturerId, $SystemUser);
$User = $Users->createChild($manufacturerId, $SystemUser);
if (!empty($address)) {
try {
$Address = $User->getStandardAddress();
} catch (QUI\Exception $Exception) {
} catch (QUI\Exception) {
$Address = $User->addAddress();
}
......@@ -198,12 +198,12 @@ public static function createManufacturer(
*/
public static function search(array $searchParams, bool $countOnly = false)
{
$Grid = new QUI\Utils\Grid($searchParams);
$gridParams = $Grid->parseDBParams($searchParams);
$usersTbl = QUI::getDBTableName('users');
$Grid = new QUI\Utils\Grid($searchParams);
$gridParams = $Grid->parseDBParams($searchParams);
$usersTbl = QUI::getDBTableName('users');
$usersAddressTbl = QUI::getDBTableName('users_address');
$binds = [];
$where = [];
$binds = [];
$where = [];
if ($countOnly) {
$sql = "SELECT COUNT(*)";
......@@ -215,16 +215,16 @@ public static function search(array $searchParams, bool $countOnly = false)
$sql .= " FROM `" . $usersTbl . "` as u LEFT JOIN `" . $usersAddressTbl . "` as ua ON u.`address` = ua.`id`";
// Only fetch users in manufacturer groups
$gc = 0;
$gc = 0;
$whereOr = [];
foreach (self::getManufacturerGroupIds() as $groupId) {
$whereOr[] = "u.`usergroup` LIKE :group" . $gc;
$bind = 'group' . $gc++;
$bind = 'group' . $gc++;
$binds[$bind] = [
'value' => '%,' . $groupId . ',%',
'type' => PDO::PARAM_STR
'type' => PDO::PARAM_STR
];
}
......@@ -254,12 +254,12 @@ public static function search(array $searchParams, bool $countOnly = false)
if ($DateFrom) {
$DateFrom->setTime(0, 0, 0);
$bind = 'datefrom';
$bind = 'datefrom';
$where[] = 'u.`regdate` >= :' . $bind;
$binds[$bind] = [
'value' => $DateFrom->getTimestamp(),
'type' => PDO::PARAM_INT
'type' => PDO::PARAM_INT
];
}
}
......@@ -270,12 +270,12 @@ public static function search(array $searchParams, bool $countOnly = false)
if ($DateTo) {
$DateTo->setTime(23, 59, 59);
$bind = 'dateto';
$bind = 'dateto';
$where[] = 'u.`regdate` <= :' . $bind;
$binds[$bind] = [
'value' => $DateTo->getTimestamp(),
'type' => PDO::PARAM_INT
'type' => PDO::PARAM_INT
];
}
}
......@@ -283,8 +283,8 @@ public static function search(array $searchParams, bool $countOnly = false)
if (!empty($searchParams['search'])) {
$searchValue = $searchParams['search'];
$fc = 0;
$whereOr = [];
$fc = 0;
$whereOr = [];
// search value filters
foreach ($searchFields as $filter) {
......@@ -309,7 +309,7 @@ public static function search(array $searchParams, bool $countOnly = false)
$binds[$bind] = [
'value' => '%' . $searchValue . '%',
'type' => PDO::PARAM_STR
'type' => PDO::PARAM_STR
];
$fc++;
......@@ -411,7 +411,7 @@ public static function parseListForGrid(array $data): array
$result = [];
$Groups = QUI::getGroups();
$Users = QUI::getUsers();
$Users = QUI::getUsers();
foreach ($data as $entry) {
$entry['usergroup'] = trim($entry['usergroup'], ',');
......@@ -425,7 +425,7 @@ public static function parseListForGrid(array $data): array
$Group = $Groups->get($groupId);
return $Group->getName();
} catch (QUI\Exception $Exception) {
} catch (QUI\Exception) {
}
return '';
......@@ -437,12 +437,12 @@ public static function parseListForGrid(array $data): array
$groups = trim($groups, ',');
$addressData = [];
$Address = null;
$Address = null;
try {
$User = $Users->get((int)$entry['id']);
$User = $Users->get($entry['id']);
$Address = $User->getStandardAddress();
} catch (QUI\Exception $Exception) {
} catch (QUI\Exception) {
}
if ($Address && (empty($entry['firstname']) || empty($entry['lastname']))) {
......@@ -450,12 +450,12 @@ public static function parseListForGrid(array $data): array
if ($Address->getAttribute('firstname')) {
$entry['firstname'] = $Address->getAttribute('firstname');
$name[] = $Address->getAttribute('firstname');
$name[] = $Address->getAttribute('firstname');
}
if ($Address->getAttribute('lastname')) {
$entry['lastname'] = $Address->getAttribute('lastname');
$name[] = $Address->getAttribute('lastname');
$name[] = $Address->getAttribute('lastname');
}
if (!empty($name)) {
......@@ -480,18 +480,18 @@ public static function parseListForGrid(array $data): array
}
$result[] = [
'id' => (int)$entry['id'],
'status' => !!$entry['active'],
'username' => $entry['username'],
'id' => (int)$entry['id'],
'status' => !!$entry['active'],
'username' => $entry['username'],
'firstname' => $entry['firstname'],
'lastname' => $entry['lastname'],
'company' => $entry['company'],
'email' => $entry['email'],
'regdate' => $DateFormatterLong->format($entry['regdate']),
'lastname' => $entry['lastname'],
'company' => $entry['company'],
'email' => $entry['email'],
'regdate' => $DateFormatterLong->format($entry['regdate']),
'usergroup_display' => $groups,
'usergroup' => $entry['usergroup'],
'address_display' => implode(' - ', $addressData)
'usergroup' => $entry['usergroup'],
'address_display' => implode(' - ', $addressData)
];
}
......
0% Lade oder .
You are about to add 0 people to the discussion. Proceed with caution.
Bearbeitung dieser Nachricht zuerst beenden!
Bitte registrieren oder zum Kommentieren