From 279758dc82cc4993e90d948b86234b0f1339ba2f Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Mon, 15 Aug 2022 19:56:31 +0000
Subject: [PATCH] Add possibility to synchronize deleted entries

---
 public.php                     |   14 +++++-
 src/Synchronizer.php           |   21 ++++++++++
 src/ContractorSynchronizer.php |   12 +-----
 src/InvoiceSynchronizer.php    |   12 +-----
 src/PaymentSynchronizer.php    |    9 +---
 5 files changed, 39 insertions(+), 29 deletions(-)

diff --git a/public.php b/public.php
index d838a81..ca9eaa5 100644
--- a/public.php
+++ b/public.php
@@ -26,10 +26,18 @@
 		die();
 	}
 
-	if ($synchronizer->synchronize($event['entityId'])) {
-		echo 'Object synchronized';
+	if ($event['changeType'] === 'delete') {
+		if($synchronizer->delete($event['extraData']['entity'])) {
+			echo 'Object deleted';
+		} else {
+			echo 'Nothing to do';
+		}
 	} else {
-		echo 'Nothing to do';
+		if ($synchronizer->synchronize($event['entityId'])) {
+			echo 'Object synchronized';
+		} else {
+			echo 'Nothing to do';
+		}
 	}
 } catch (Exception $e) {
 	header('HTTP/1.1 500 Internal Server Error');
diff --git a/src/ContractorSynchronizer.php b/src/ContractorSynchronizer.php
index a5726a9..08c076c 100644
--- a/src/ContractorSynchronizer.php
+++ b/src/ContractorSynchronizer.php
@@ -5,16 +5,8 @@
 use \Webit\WFirmaSDK\Contractors as Contractors;
 use \Webit\WFirmaSDK\Payments as Payments;
 
-class ContractorSynchronizer {
-	protected $wfirma;
-	protected $helper;
-
-	function __construct(\Webit\WFirmaSDK\Entity\ModuleApiFactory $wFirmaApi, UcrmHelper $ucrmHelper) {
-		$this->wfirma = $wFirmaApi;
-		$this->helper = $ucrmHelper;
-	}
-
-	function synchronize($ucrmClientId) {
+class ContractorSynchronizer extends Synchronizer {
+	function synchronize(int $ucrmClientId) {
 		$crm = $this->helper->getApi();
 		$wFirmaContractors = $this->wfirma->contractorsApi();
 
diff --git a/src/InvoiceSynchronizer.php b/src/InvoiceSynchronizer.php
index 3ddf959..c16d17c 100644
--- a/src/InvoiceSynchronizer.php
+++ b/src/InvoiceSynchronizer.php
@@ -6,15 +6,7 @@
 use \Webit\WFirmaSDK\Invoices as Invoices;
 use \Webit\WFirmaSDK\Payments as Payments;
 
-class InvoiceSynchronizer {
-	protected $wfirma;
-	protected $helper;
-
-	function __construct(\Webit\WFirmaSDK\Entity\ModuleApiFactory $wFirmaApi, UcrmHelper $ucrmHelper) {
-		$this->wfirma = $wFirmaApi;
-		$this->helper = $ucrmHelper;
-	}
-
+class InvoiceSynchronizer extends Synchronizer {
 	function getContractorId($clientId, $synchronize = TRUE) {
 		if ($synchronize) {
 			$synchronizer = new ContractorSynchronizer($this->wfirma, $this->helper);
@@ -55,7 +47,7 @@
 			[$c2->name(), $c2->unit(), $c2->count(), $c2->price(), $c2->vat(), $c2->discount()];
 	}
 
-	function synchronize($ucrmInvoiceId) {
+	function synchronize(int $ucrmInvoiceId) {
 		$crm = $this->helper->getApi();
 		$wFirmaInvoices = $this->wfirma->invoicesApi();
 
diff --git a/src/PaymentSynchronizer.php b/src/PaymentSynchronizer.php
index c924d27..5ee347f 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();
 
diff --git a/src/Synchronizer.php b/src/Synchronizer.php
new file mode 100644
index 0000000..7aeda66
--- /dev/null
+++ b/src/Synchronizer.php
@@ -0,0 +1,21 @@
+<?php
+
+namespace SIPL\UCRM\wFirma;
+
+abstract class Synchronizer {
+	protected $wfirma;
+	protected $helper;
+
+	function __construct(\Webit\WFirmaSDK\Entity\ModuleApiFactory $wFirmaApi, UcrmHelper $ucrmHelper) {
+		$this->wfirma = $wFirmaApi;
+		$this->helper = $ucrmHelper;
+	}
+
+	public function synchronize(int $entityId) {
+		return FALSE;
+	}
+
+	public function delete(array $entity) {
+		return FALSE;
+	}
+}

--
Gitblit v1.10.0