From 286d38fc4ab966b71f741065e9a5948544a26df2 Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Mon, 15 Aug 2022 19:59:16 +0000
Subject: [PATCH] Release version 1.1

---
 src/PaymentSynchronizer.php |   42 ++++++++++++++++++++++++++++++++++++------
 1 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/src/PaymentSynchronizer.php b/src/PaymentSynchronizer.php
index c924d27..f6f190a 100644
--- a/src/PaymentSynchronizer.php
+++ b/src/PaymentSynchronizer.php
@@ -5,14 +5,11 @@
 use \Webit\WFirmaSDK\Invoices as Invoices;
 use \Webit\WFirmaSDK\Payments as Payments;
 
-class PaymentSynchronizer {
-	protected $wfirma;
-	protected $helper;
+class PaymentSynchronizer extends Synchronizer {
 	protected $ucrmMainDir;
 
 	function __construct(\Webit\WFirmaSDK\Entity\ModuleApiFactory $wFirmaApi, UcrmHelper $ucrmHelper) {
-		$this->wfirma = $wFirmaApi;
-		$this->helper = $ucrmHelper;
+		parent::__construct($wFirmaApi, $ucrmHelper);
 
 		$backtrace = debug_backtrace();
 		$backtrace = end($backtrace);
@@ -27,7 +24,7 @@
 			[$p2->objectName(), $p2->objectId(), $p2->amount()->value(), $p2->date()->format('Y-m-d'), $p2->paymentMethod()];
 	}
 
-	function synchronize($ucrmPaymentId) {
+	function synchronize(int $ucrmPaymentId) {
 		$crm = $this->helper->getApi();
 		$wFirmaPaymentsApi = $this->wfirma->paymentsApi();
 
@@ -173,4 +170,37 @@
 
 		return $changed;
 	}
+
+	function delete(array $paymentData) {
+		$wFirmaPaymentsApi = $this->wfirma->paymentsApi();
+		$paymentAttributeId = $this->helper->getAttributes()->getIdForCode('payment');
+
+		$wFirmaIds = '';
+		foreach ($paymentData['attributes'] as $attribute) {
+			if ($attribute['customAttributeId'] === $paymentAttributeId) {
+				$wFirmaIds = $attribute['value'];
+			}
+		}
+		if (strlen($wFirmaIds) > 0) {
+			$wFirmaIds = explode(',', $wFirmaIds);
+		} else {
+			$wFirmaIds = [];
+		}
+
+		$changed = FALSE;
+
+		foreach($wFirmaIds as $wFirmaId) {
+			try {
+				$payment = $wFirmaPaymentsApi->get(
+					Payments\PaymentId::create($wFirmaId)
+				);
+				$wFirmaPaymentsApi->delete($payment->id());
+				$changed = TRUE;
+			} catch(\Webit\WFirmaSDK\Entity\Exception\NotFoundException $e) {
+				// do nothing
+			}
+		}
+
+		return $changed;
+	}
 }

--
Gitblit v1.10.0