Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

Commit

Permalink
Better Work With Invoice document types
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitexus committed May 31, 2018
1 parent dc4adaf commit 4fc3df1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 52 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
composer.lock
/nbproject/private/
/vendor/
/debian/debhelper-build-stamp
/debian/debhelper-build-stamp
/clover.log
76 changes: 32 additions & 44 deletions FlexiPeeHP/Bricks/ParovacFaktur.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ class ParovacFaktur extends \Ease\Sand
*/
public $daysBack = 1;

/**
* Invoice Types Cache
* @var array
*/
public $docTypes = [];

/**
* Párovač faktur
*/
Expand All @@ -42,27 +36,6 @@ public function __construct()
$this->banker = new \FlexiPeeHP\Banka();
}

/**
*
* @return array
*/
public function getDocumentTypes()
{
$types = [];
$evbackup = $this->invoicer->getEvidence();
$defbackup = $this->invoicer->defaultUrlParams;
$this->invoicer->setEvidence('typ-faktury-vydane');
$this->invoicer->defaultUrlParams = ['limit' => 0];
$typesRaw = $this->invoicer->getColumnsFromFlexibee([
'kod', 'typDoklK']);
$this->invoicer->setEvidence($evbackup);
$this->invoicer->defaultUrlParams = $defbackup;
foreach ($typesRaw as $rawtype) {
$types[$rawtype['kod']] = $rawtype['typDoklK'];
}
return $types;
}

/**
* Start set date
*
Expand Down Expand Up @@ -112,9 +85,10 @@ public function getPaymentsToProcess($daysBack = 1)
*/
public function getInvoicesToProcess()
{
$result = [];
$this->invoicer->defaultUrlParams['order'] = 'datVyst@A';
$invoices = $this->invoicer->getColumnsFromFlexibee([
$result = [];
$this->invoicer->defaultUrlParams['order'] = 'datVyst@A';
$this->invoicer->defaultUrlParams['includes'] = '/faktura-vydana/typDokl';
$invoices = $this->invoicer->getColumnsFromFlexibee([
'id',
'kod',
'stavUhrK',
Expand All @@ -123,6 +97,7 @@ public function getInvoicesToProcess()
'buc',
'varSym',
'specSym',
'typDokl(typDoklK,kod)',
'sumCelkem',
'duzpPuv',
'typDokl',
Expand All @@ -133,6 +108,7 @@ public function getInvoicesToProcess()
if ($this->invoicer->lastResponseCode == 200) {
$result = $invoices;
}
unset($this->invoicer->defaultUrlParams['includes']);
return $result;
}

Expand Down Expand Up @@ -162,7 +138,12 @@ public function invoicesMatchingByBank()
$payment = new \FlexiPeeHP\Banka($paymentData);

foreach ($invoices as $invoiceID => $invoiceData) {
$invoice = new \FlexiPeeHP\FakturaVydana((int) $invoiceData['id']);

$typDokl = $invoiceData['typDokl'][0];
$docType = $typDokl['typDoklK'];
$invoiceData['typDokl'] = \FlexiPeeHP\FlexiBeeRO::code($typDokl['kod']);

$invoice = new \FlexiPeeHP\FakturaVydana($invoiceData);

/*
* Standardní faktura (typDokladu.faktura)
Expand All @@ -174,8 +155,9 @@ public function invoicesMatchingByBank()
* Pohyb Kč / Zůstatek Kč (typBanUctu.kc)
* Pohyb měna / Zůstatek měna (typBanUctu.mena)
*/
$docType = $this->getOriginDocumentType($invoiceData['typDokl']);

switch ($docType) {
case 'typDokladu.zalohFaktura':
case 'typDokladu.faktura':
if ($this->settleInvoice($invoice, $payment)) ;
break;
Expand All @@ -190,7 +172,7 @@ public function invoicesMatchingByBank()
default:
$this->addStatusMessage(
sprintf(_('Unsupported document type: %s %s'),
$docType.': '.$invoiceData['typDokl'],
$typDokl['typDoklK@showAs'].' ('.$docType.'): '.$invoiceData['typDokl'],
$invoice->getApiURL()
), 'warning');
break;
Expand Down Expand Up @@ -218,6 +200,9 @@ public function invoicesMatchingByInvoices()
foreach ($this->getInvoicesToProcess() as $invoiceData) {
$payments = $this->findPayments($invoiceData);
if (!empty($payments) && count(current($payments))) {
$typDokl = $invoiceData['typDokl'][0];
$docType = $typDokl['typDoklK'];
$invoiceData['typDokl'] = \FlexiPeeHP\FlexiBeeRO::code($typDokl['kod']);
$invoice = new \FlexiPeeHP\FakturaVydana($invoiceData);
$this->invoicer->setMyKey($invoiceData['id']);
/*
Expand All @@ -231,10 +216,10 @@ public function invoicesMatchingByInvoices()
* Pohyb měna / Zůstatek měna (typBanUctu.mena)
*/

$docType = $this->getOriginDocumentType($invoiceData['typDokl']);
foreach ($payments as $paymentData) {
$payment = new \FlexiPeeHP\Banka($paymentData);
switch ($docType) {
case 'typDokladu.zalohFaktura':
case 'typDokladu.faktura':
if ($this->settleInvoice($invoice, $payment)) {

Expand All @@ -247,9 +232,11 @@ public function invoicesMatchingByInvoices()
$this->settleCreditNote($invoice, $payments);
break;
default:
$this->addStatusMessages(sprintf(_('Unsupported invoice type: %s'),
'warning'),
$docType.': '.$invoiceData['typDokl']);
$this->addStatusMessage(
sprintf(_('Unsupported document type: %s %s'),
$typDokl['typDoklK@showAs'].' ('.$docType.'): '.$invoiceData['typDokl'],
$invoice->getApiURL()
), 'warning');
break;
}
}
Expand Down Expand Up @@ -561,14 +548,14 @@ public function findInvoices($paymentData)
// }
//

if (count($vInvoices)) {
if (!empty($vInvoices) && count($vInvoices)) {
foreach ($vInvoices as $invoiceID => $invoice) {
if (!array_key_exists($invoiceID, $invoices)) {
$invoices[$invoiceID] = $invoice;
}
}
}
if (count($sInvoices)) {
if (!empty($sInvoices) && count($sInvoices)) {
foreach ($sInvoices as $invoiceID => $invoice) {
if (!array_key_exists($invoiceID, $invoices)) {
$invoices[$invoiceID] = $invoice;
Expand Down Expand Up @@ -648,15 +635,16 @@ public function findPayments($invoiceData)
*/
public function findInvoice($what)
{
$result = null;
$this->invoicer->defaultUrlParams['order'] = 'datVyst@A';
$payments = $this->invoicer->getColumnsFromFlexibee([
$result = null;
$this->invoicer->defaultUrlParams['order'] = 'datVyst@A';
$this->invoicer->defaultUrlParams['includes'] = '/faktura-vydana/typDokl';
$payments = $this->invoicer->getColumnsFromFlexibee([
'id',
'varSym',
'specSym',
'buc',
'kod',
'typDokl',
'typDokl(typDoklK,kod)',
'sumCelkem',
'stitky',
'datVyst'],
Expand All @@ -665,7 +653,7 @@ public function findInvoice($what)
if ($this->invoicer->lastResponseCode == 200) {
$result = $payments;
}

unset($this->invoicer->defaultUrlParams['includes']);
return $result;
}

Expand Down
35 changes: 28 additions & 7 deletions FlexiPeeHP/Bricks/Upominac.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ public function __construct($init = null, $options = array())

/**
* Obtain customer debths Array
*
* @param array $skipLables labels of Customer (Addressbook) to skip
*
* @return Customer
*/
public function getDebths()
public function getDebths($skipLabels = [])
{
$allDebts = [];
$this->addStatusMessage(_('Getting clients'), 'debug');
Expand All @@ -56,21 +58,38 @@ public function getDebths()
$this->addStatusMessage(sprintf(_('%s Clients Found'), count($clients)));
$this->addStatusMessage(_('Getting debts'), 'debug');
foreach ($clients as $cid => $clientIDs) {
$stitky = $clientIDs['stitky'];
$debts = $this->customer->getCustomerDebts((int) $clientIDs['id']);
if (count($debts)) {
$stitky = \FlexiPeeHP\Stitek::listToArray($clientIDs['stitky']);
if (count($skipLabels) && array_intersect($skipLabels, $stitky)) {
continue;
}

$debts = $this->customer->getCustomerDebts((int) $clientIDs['id']);
if (!empty($debts) && count($debts)) {
foreach ($debts as $did => $debtInfo) {
$allDebts[$cid][$did] = $debtInfo;
$debtCount++;
}
} else { //All OK
$this->enableCustomer($stitky, $cid);
$this->everythingPaidOff($cid, $stitky);
}
}
$this->addStatusMessage(sprintf(_('%s Debts Found'), $debtCount));
return $allDebts;
}

/**
* What to do when no debts found for customer
*
* @param int $clientID AddressBook ID
* @param array $stitky Customer's labels
*
* @return boolean customer well processed
*/
public function everythingPaidOff($clientID, $stitky)
{
return $this->enableCustomer(implode(',', $stitky), $clientID);
}

/**
* Enable customer
*
Expand Down Expand Up @@ -106,11 +125,13 @@ function enableCustomer($stitky, $cid)
/**
* Process All Debts of All Customers
*
* @param array $skipLabels Skip Customers (AddressBook) with any of given labels
*
* @return int All Debts count
*/
public function processAllDebts()
public function processAllDebts($skipLabels = [])
{
$allDebths = $this->getDebths();
$allDebths = $this->getDebths($skipLabels);
$this->addStatusMessage(sprintf(_('%d clients to remind process'),
count($allDebths)));
$counter = 0;
Expand Down

0 comments on commit 4fc3df1

Please sign in to comment.