Просмотр исходного кода

Ticket #3620 - Xero integration module.

AntonLV 4 лет назад
Родитель
Сommit
36a22a7a31

+ 25 - 2
modules/boonex/xero/classes/BxXeroApi.php

@@ -264,10 +264,11 @@ class BxXeroApi extends BxDol
             $aLineItems = [$oLineItem];		
 
             $oContact = $this->actionGetContact($sProfileEmail);
-            $sContactId = $oContact->getContactId();
+            if(!$oContact)
+                return false;
 
             $oInvoiceContact = new XeroAPI\XeroPHP\Models\Accounting\Contact;
-            $oInvoiceContact->setContactId($sContactId);
+            $oInvoiceContact->setContactId($oContact->getContactId());
 
             $oInvoice = new XeroAPI\XeroPHP\Models\Accounting\Invoice;
             $oInvoice->setReference('Ref-' . $this->_getRandNum())
@@ -334,6 +335,28 @@ class BxXeroApi extends BxDol
         return $mixedResult;
     }
 
+    public function sendInvoice($sInvoiceId)
+    {
+        $mixedApi = $this->_getApiObject();
+        if($mixedApi === false)
+            return false;
+
+        list($sTenantId, $oXeroApi) = $mixedApi;
+
+        $bResult = true;
+        try {
+            $oRequestEmpty = new XeroAPI\XeroPHP\Models\Accounting\RequestEmpty;
+
+            $oXeroApi->emailInvoice($sTenantId, $sInvoiceId, $oRequestEmpty);            
+        } 
+        catch (Exception $oException) {
+            $this->_oLog->write('Send Invoice: ' . $oException->getMessage());
+            $bResult = false;
+        }
+
+        return $bResult;
+    }
+
     protected function _getProvider()
     {
         if(empty($this->_oProvider))

+ 2 - 1
modules/boonex/xero/classes/BxXeroConfig.php

@@ -55,8 +55,9 @@ class BxXeroConfig extends BxBaseModGeneralConfig
             'PARAM_CLIENT_SECRET' => 'bx_xero_client_secret',
             'PARAM_REDIRECT_URL' => 'bx_xero_redirect_url',
             'PARAM_WEBHOOK_KEY' => 'bx_xero_webhook_key',
+            'PARAM_INVOICE_SEND' => 'bx_xero_invoice_send',
         );
-        
+
         $this->_aJsClasses = array(
             'main' => 'BxXeroMain',
         );

+ 18 - 4
modules/boonex/xero/classes/BxXeroModule.php

@@ -131,17 +131,17 @@ class BxXeroModule extends BxDolModule
         return $this->_oApi->actionAddContact($iProfileId, $sProfileName, $sProfileEmail);
     }
 
-    public function serviceAddInvoce($iProfileId, $sName, $fAmount, $iQuantity = 1, $sDueDate = false)
+    public function serviceAddInvoce($iProfileId, $sName, $mixedAmount, $iQuantity = 1, $mixedDueDate = false)
     {
         $oProfile = BxDolProfile::getInstance($iProfileId);
         if(!$oProfile)
             return false;
 
         $sProfileEmail = $oProfile->getAccountObject()->getEmail();
-        return $this->_oApi->actionAddInvoice($sProfileEmail, $sName, $fAmount, $iQuantity, $sDueDate);
+        return $this->addInvoice($sProfileEmail, $sName, $mixedAmount, $iQuantity, $mixedDueDate);
     }
 
-    public function serviceAddInvoceAuto($iProfileId, $sName, $fAmount, $iQuantity = 1, $mixedDueDate = false)
+    public function serviceAddInvoceAuto($iProfileId, $sName, $mixedAmount, $iQuantity = 1, $mixedDueDate = false)
     {
         $oProfile = BxDolProfile::getInstance($iProfileId);
         if(!$oProfile)
@@ -156,7 +156,7 @@ class BxXeroModule extends BxDolModule
                 return false;
         }
 
-        return $this->_oApi->actionAddInvoice($sProfileEmail, $sName, $fAmount, $iQuantity, $mixedDueDate);
+        return $this->addInvoice($sProfileEmail, $sName, $mixedAmount, $iQuantity, $mixedDueDate);
     }
 
 
@@ -175,6 +175,20 @@ class BxXeroModule extends BxDolModule
 
         return $this->_oApi->actionIsContact($sProfileEmail);
     }
+    
+    public function addInvoice($sProfileEmail, $sName, $fAmount, $iQuantity, $mixedDueDate)
+    {
+        $CNF = &$this->_oConfig->CNF;
+
+        $mixedInvoiceId = $this->_oApi->actionAddInvoice($sProfileEmail, $sName, $fAmount, $iQuantity, $mixedDueDate);
+        if(!$mixedInvoiceId)
+            return $mixedInvoiceId;
+
+        if(getParam($CNF['PARAM_INVOICE_SEND']) == 'on')
+            $this->_oApi->sendInvoice($mixedInvoiceId);
+
+        return $mixedInvoiceId;
+    }
 
 
     /*

+ 4 - 0
modules/boonex/xero/install/langs/en.xml

@@ -5,6 +5,7 @@
     <!-- Options -->
     <string name="_bx_xero_options_category_hidden"><![CDATA[Hidden]]></string>
     <string name="_bx_xero_options_category_general"><![CDATA[General]]></string>
+    <string name="_bx_xero_options_category_invoice"><![CDATA[Invoices]]></string>
 
     <string name="_bx_xero_option_token"><![CDATA[Access token]]></string>
     <string name="_bx_xero_option_expires"><![CDATA[Access token expiration time]]></string>
@@ -17,7 +18,10 @@
     <string name="_bx_xero_option_redirect_url"><![CDATA[Redirect URL]]></string>
     <string name="_bx_xero_option_webhook_url"><![CDATA[Webhook URL]]></string>
     <string name="_bx_xero_option_webhook_key"><![CDATA[Webhook Key]]></string>
+    
+    <string name="_bx_xero_option_invoice_send"><![CDATA[Send an invoice via email just after creation.]]></string>
 
+    <!-- Pages -->
     <!--
     <string name="_bx_xero_page_block_title_system_"><![CDATA[]]></string>
     <string name="_bx_xero_page_block_title_"><![CDATA[]]></string>

+ 7 - 0
modules/boonex/xero/install/sql/enable.sql

@@ -30,6 +30,13 @@ INSERT INTO `sys_options` (`name`, `value`, `category_id`, `caption`, `type`, `e
 ('bx_xero_webhook_url', '{site_url}m/xero/webhook', @iCategId, '_bx_xero_option_webhook_url', 'value', '', '', '', 40),
 ('bx_xero_webhook_key', '', @iCategId, '_bx_xero_option_webhook_key', 'digit', '', '', '', 42);
 
+INSERT INTO `sys_options_categories` (`type_id`, `name`, `caption`, `order`)
+VALUES (@iTypeId, 'bx_xero_invoice', '_bx_xero_options_category_invoice', 20);
+SET @iCategId = LAST_INSERT_ID();
+
+INSERT INTO `sys_options` (`name`, `value`, `category_id`, `caption`, `type`, `extra`, `check`, `check_error`, `order`) VALUES
+('bx_xero_invoice_send', 'on', @iCategId, '_bx_xero_option_invoice_send', 'checkbox', '', '', '', 10);
+
 
 -- ALERTS
 INSERT INTO `sys_alerts_handlers` (`name`, `class`, `file`, `service_call`) VALUES