Jacek Kowalski
2026-02-17 148d376f573020cfc0ff52c96082136d6d65824a
src/InvoiceSynchronizer.php
@@ -2,20 +2,12 @@
namespace SIPL\UCRM\wFirma;
use \Webit\WFirmaSDK\Contractors as Contractors;
use \Webit\WFirmaSDK\Invoices as Invoices;
use \Webit\WFirmaSDK\Payments as Payments;
use Webit\WFirmaSDK\Contractors as Contractors;
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;
   }
   function getContractorId($clientId, $synchronize = TRUE) {
class InvoiceSynchronizer extends Synchronizer {
   protected function getContractorId($clientId, $synchronize = TRUE): ?Contractors\ContractorId {
      if ($synchronize) {
         $synchronizer = new ContractorSynchronizer($this->wfirma, $this->helper);
         $synchronizer->synchronize($clientId);
@@ -37,7 +29,7 @@
      return Contractors\ContractorId::create($wFirmaId);
   }
   function getTaxes() {
   protected function getTaxes(): array {
      $crm = $this->helper->getApi();
      $taxesData = $crm->get('/taxes');
@@ -48,14 +40,14 @@
      return $taxes;
   }
   function compareInvoicesContent(Invoices\InvoicesContent $c1, Invoices\InvoicesContent $c2) {
   protected function compareInvoicesContent(Invoices\InvoicesContent $c1, Invoices\InvoicesContent $c2): int {
      return
         [$c1->name(), $c1->unit(), $c1->count(), $c1->price(), $c1->vat(), $c1->discount()]
         <=>
         [$c2->name(), $c2->unit(), $c2->count(), $c2->price(), $c2->vat(), $c2->discount()];
   }
   function synchronize($ucrmInvoiceId) {
   function synchronize(int $ucrmInvoiceId): bool {
      $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;