From 927c6bf2e6c55ec2bbd3d02ae334e045161ef77b Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Mon, 15 Aug 2022 19:56:37 +0000
Subject: [PATCH] Delete payments in wFirma that were removed from UCRM

---
 src/InvoiceSynchronizer.php |   21 ++++++++++-----------
 1 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/src/InvoiceSynchronizer.php b/src/InvoiceSynchronizer.php
index 3781f00..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();
 
@@ -78,7 +70,8 @@
 
 		$wFirmaContractorId = $this->getContractorId($invoiceData['clientId']);
 		$payment = Invoices\Payment::create(
-			Payments\PaymentMethod::transfer()
+			Payments\PaymentMethod::transfer(),
+			new \DateTime($invoiceData['dueDate'])
 		);
 
 		if ($wFirmaId) {
@@ -104,6 +97,12 @@
 		/** @var \Webit\WFirmaSDK\Invoices\Invoice $invoice */
 		$changed = FALSE;
 
+		if ($invoice->payment()->paymentMethod() != $payment->paymentMethod()
+			|| $invoice->payment()->paymentDate() != $payment->paymentDate()) {
+			$invoice->changePayment($payment);
+			$changed = TRUE;
+		}
+
 		if ($invoice->priceType() != Invoices\PriceType::brutto()) {
 			$invoice->changePriceType(Invoices\PriceType::brutto());
 			$changed = TRUE;

--
Gitblit v1.10.0