Skip to content

Commit

Permalink
tweak(SalesExport) Document added running group prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmhh committed Mar 13, 2024
1 parent 81814d4 commit 9c3bf84
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
48 changes: 47 additions & 1 deletion tine20/Sales/Export/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected function _loadTwig()


if ($record->has(Sales_Model_Document_Abstract::FLD_VAT_PROCEDURE) &&
$record->{Sales_Model_Document_Abstract::FLD_VAT_PROCEDURE} === Sales_Config::VAT_PROCEDURE_REVERSE_CHARGE) {
$record->{Sales_Model_Document_Abstract::FLD_VAT_PROCEDURE} === Sales_Config::VAT_PROCEDURE_REVERSE_CHARGE) {
$templates = $config->{Sales_Config::REVERSE_CHANGE_TEMPLATE};
$record->{Sales_Model_Document_Abstract::FLD_VAT_PROCEDURE} = $templates[$lang] ?? $templates[$config->{Sales_Config::LANGUAGES_AVAILABLE}->default];
}
Expand All @@ -111,10 +111,54 @@ protected function _startDataSource($_name)

if ('POSITIONS' === $_name) {
$this->_groupByProperty = Sales_Model_DocumentPosition_Abstract::FLD_GROUPING;
$this->_groupByProcessor = function(?string &$grouping) {
static $lastGrouping = null;
static $groupCount = 0;

if (null === $grouping) {
$grouping = '';
}
if (null === $lastGrouping) {
$lastGrouping = $grouping;
} elseif ($lastGrouping !== $grouping) {
$lastGrouping = $grouping;
++$groupCount;
}

if (preg_match('/^\[_[a-zA-Z0-9]+\]/', $grouping, $m)) {
$prefix = '';
if (preg_match('/^\[_[a-z]/', $grouping)) {
$i = $groupCount;
do {
$prefix .= chr(ord('a') + $i % 26);
$i -= 26;
} while($i >= 0);
} elseif (preg_match('/^\[_[A-Z]/', $grouping)) {
$i = $groupCount;
do {
$prefix .= chr(ord('A') + $i % 26);
$i -= 26;
} while($i >= 0);
} else {
if (($digits = strlen($m[0]) - 3) > 1) {
$prefix = sprintf('%0' . $digits . 'd', $groupCount);
} else {
$prefix = $groupCount;
}
}

$grouping = $prefix . substr($grouping, strlen($m[0]));
}
};
$this->_groupByRecordProcessor = function (Sales_Model_DocumentPosition_Abstract $position, array &$context): void {
$context['sum_net_price'] = ($context['sum_net_price'] ?? 0) + $position->{Sales_Model_DocumentPosition_Abstract::FLD_NET_PRICE};
$context['sum_gross_price'] = ($context['sum_gross_price'] ?? 0) + $position->{Sales_Model_DocumentPosition_Abstract::FLD_GROSS_PRICE};
$context['sum_sales_tax'] = ($context['sum_sales_tax'] ?? 0) + $position->{Sales_Model_DocumentPosition_Abstract::FLD_SALES_TAX};
if (preg_match('/^\[_[a-zA-Z0-9]+\]/', $position->{Sales_Model_DocumentPosition_Abstract::FLD_GROUPING} ?? '', $m)) {
$context['sum_text'] = substr($position->{Sales_Model_DocumentPosition_Abstract::FLD_GROUPING}, strlen($m[0]));
} else {
$context['sum_text'] = $position->{Sales_Model_DocumentPosition_Abstract::FLD_GROUPING};
}
};
}
}
Expand All @@ -125,6 +169,8 @@ protected function _endDataSource($_name)

if ('POSITIONS' === $_name) {
$this->_groupByProperty = null;
$this->_groupByRecordProcessor = null;
$this->_groupByProcessor = null;
}
}
}
10 changes: 5 additions & 5 deletions tine20/Tinebase/Export/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -975,11 +975,6 @@ public function processIteration($_records, $_resolveRecords = true)
$fn = $this->_groupByProcessor;
$fn($propertyValue);
}
if (null !== $this->_groupByRecordProcessor) {
/** @var closure $fn */
$fn = $this->_groupByRecordProcessor;
$fn($record, $this->_groupByContext);
}
if (true === $first || $this->_lastGroupValue !== $propertyValue) {
if ($this->_groupOpen) {
$this->_endGroup();
Expand All @@ -988,6 +983,11 @@ public function processIteration($_records, $_resolveRecords = true)
$this->_currentRecord = $record;
$this->_startGroup();
}
if (null !== $this->_groupByRecordProcessor) {
/** @var closure $fn */
$fn = $this->_groupByRecordProcessor;
$fn($record, $this->_groupByContext);
}
}
$this->_currentRecord = $record;

Expand Down

0 comments on commit 9c3bf84

Please sign in to comment.