Jacek Kowalski
2022-08-15 927c6bf2e6c55ec2bbd3d02ae334e045161ef77b
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;
   }
}