Skip to content

Commit 1133722

Browse files
Hansaka WeerasinghaHansaka Weerasingha
authored andcommitted
Version 2.1.2
- Trigger email based on config value - Label saving improved - Code formating - More debug/error logs
1 parent b640f7e commit 1133722

File tree

3 files changed

+147
-98
lines changed

3 files changed

+147
-98
lines changed

aramex.php

Lines changed: 129 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function __construct (& $subject, $config) {
2929
/**
3030
* Create the table for this plugin if it does not yet exist.
3131
*
32-
* @author Valérie Isaksen
32+
* @author Valérie Isaksen
3333
*/
3434
public function getVmPluginCreateTableSQL () {
3535

@@ -67,7 +67,7 @@ function getTableSQLFields () {
6767
* @param integer $virtuemart_shipmentmethod_id The selected shipment method id
6868
* @param string $shipment_name Shipment Name
6969
* @return mixed Null for shipments that aren't active, text (HTML) otherwise
70-
* @author Valérie Isaksen
70+
* @author Valérie Isaksen
7171
* @author Max Milbers
7272
*/
7373
public function plgVmOnShowOrderFEShipment ($virtuemart_order_id, $virtuemart_shipmentmethod_id, &$shipment_name) {
@@ -145,28 +145,28 @@ function plgVmConfirmedOrder (VirtueMartCart $cart, $order) {
145145
'AccountNumber' => $method->account_number,
146146

147147
'PartyAddress' => array(
148-
'Line1' => $method->account_address,
149-
'City' => $method->account_city,
150-
//'StateOrProvinceCode' => $method->addcount_state,
151-
'PostCode' => $method->account_zipcode,
148+
'Line1' => $method->account_address,
149+
'City' => $method->account_city,
150+
//'StateOrProvinceCode' => $method->addcount_state,
151+
'PostCode' => $method->account_zipcode,
152152
'CountryCode' => $method->account_country_code
153153
),
154154

155-
'Contact' => array(
156-
'Department' => $method->shipper_department,
157-
'PersonName' => $method->shipper_personname,
158-
'Title' => $method->shipper_title,
159-
'CompanyName' => $method->shipper_companyname,
160-
'PhoneNumber1' => $method->shipper_phonenumber1,
161-
'PhoneNumber1Ext' => $method->shipper_phonenumber1ext,
162-
'PhoneNumber2' => $method->shipper_phonenumber2,
163-
'PhoneNumber2Ext' => $method->shipper_phonenumber2ext,
164-
'FaxNumber' => $method->shipper_faxnumber,
165-
'CellPhone' => $method->shipper_cellphone,
166-
'EmailAddress' => $method->shipper_emailaddress,
167-
'Type' => ''
168-
)
169-
);
155+
'Contact' => array(
156+
'Department' => $method->shipper_department,
157+
'PersonName' => $method->shipper_personname,
158+
'Title' => $method->shipper_title,
159+
'CompanyName' => $method->shipper_companyname,
160+
'PhoneNumber1' => $method->shipper_phonenumber1,
161+
'PhoneNumber1Ext' => $method->shipper_phonenumber1ext,
162+
'PhoneNumber2' => $method->shipper_phonenumber2,
163+
'PhoneNumber2Ext' => $method->shipper_phonenumber2ext,
164+
'FaxNumber' => $method->shipper_faxnumber,
165+
'CellPhone' => $method->shipper_cellphone,
166+
'EmailAddress' => $method->shipper_emailaddress,
167+
'Type' => ''
168+
)
169+
);
170170

171171
$params['Consignee'] = array(
172172
'Reference1' => $cart->order_number,
@@ -214,39 +214,41 @@ function plgVmConfirmedOrder (VirtueMartCart $cart, $order) {
214214

215215
$params['Details']= array(
216216
'Dimensions' => array(
217-
'Length' => 10,
218-
'Width' => 10,
219-
'Height' => 10,
220-
'Unit' => 'CM',
217+
'Length' => 10,
218+
'Width' => 10,
219+
'Height' => 10,
220+
'Unit' => 'CM',
221221

222222
),
223223

224224

225225
'ActualWeight' => array(
226-
'Value' => number_format($this->getOrderWeight ($cart,'KG'),3),
227-
'Unit' => 'KG'
226+
'Value' => number_format($this->getOrderWeight ($cart,'KG'),3),
227+
'Unit' => 'KG'
228228
),
229229

230230
'ProductGroup' => $method->product_group,
231231
'ProductType' => $method->product_type,
232232
'PaymentType' => $method->payment_type,
233233
'PaymentOptions' => $method->payment_options,
234-
'Services' => $method->services,
234+
'Services' => $method->services,
235235
'NumberOfPieces' => 1,
236-
'DescriptionOfGoods' => $method->description_of_goods,
237-
'GoodsOriginCountry' => $method->goods_country,
238-
'CurrencyCode' => $currency->_vendorCurrency_code_3,
239-
'Items' => $aramex_items
236+
'DescriptionOfGoods' => $method->description_of_goods,
237+
'GoodsOriginCountry' => $method->goods_country,
238+
'CurrencyCode' => $currency->_vendorCurrency_code_3,
239+
'Items' => $aramex_items
240240
);
241241

242-
243-
$params['Details']['CashOnDeliveryAmount'] = array(
244-
'Value' => $cart->pricesUnformatted['billTotal'],
245-
'CurrencyCode' => $currency->_vendorCurrency_code_3
246-
);
242+
if ($method->services == 'COD')
243+
{
244+
$params['Details']['CashOnDeliveryAmount'] = array(
245+
'Value' => $cart->pricesUnformatted['billTotal'],
246+
'CurrencyCode' => $currency->_vendorCurrency_code_3
247+
);
248+
}
247249

248250
$params['Details']['CustomsValueAmount'] = array(
249-
'Value' => $cart->pricesUnformatted['billTotal'],
251+
'Value' => $cart->pricesUnformatted['billTotal'],
250252
'CurrencyCode' => $currency->_vendorCurrency_code_3
251253
);
252254

@@ -283,53 +285,58 @@ function plgVmConfirmedOrder (VirtueMartCart $cart, $order) {
283285

284286
$major_par['ClientInfo'] = array(
285287
'AccountCountryCode' => $method->account_country_code,
286-
'AccountEntity' => $method->account_entity,
287-
'AccountNumber' => $method->account_number,
288-
'AccountPin' => $method->account_pin,
289-
'UserName' => $method->username,
290-
'Password' => $method->password,
291-
'Version' => $method->version
288+
'AccountEntity' => $method->account_entity,
289+
'AccountNumber' => $method->account_number,
290+
'AccountPin' => $method->account_pin,
291+
'UserName' => $method->username,
292+
'Password' => $method->password,
293+
'Version' => $method->version
292294
);
293295

294296
$major_par['LabelInfo'] = array(
295-
'ReportID' => 9201,
296-
'ReportType' => 'URL',
297+
'ReportID' => 9201,
298+
'ReportType' => 'URL',
297299
);
298300

301+
vmdebug('Shippment params', $major_par);
302+
299303
if($method->ship_mode) //live mode
300304
$soapClient = new SoapClient(WSDL_PATH.'shipping-services-api.wsdl');
301-
else//test mode
305+
else //test mode
302306
$soapClient = new SoapClient(WSDL_PATH.'shipping-services-api-test.wsdl');
303-
304-
$auth_call = $soapClient->CreateShipments($major_par);
305307

306308
try
307309
{
308310
$auth_call = $soapClient->CreateShipments($major_par);
311+
vmdebug('Shipment response', $auth_call);
312+
309313
if($auth_call->HasErrors) //error
310314
{
311315
if(count($auth_call->Notifications->Notification) > 1)
312316
{
313317
foreach($auth_call->Notifications->Notification as $notify_error)
314318
{
315-
JError::raiseWarning(500,'Aramex: ' . $notify_error->Code .' - '. $notify_error->Message);
319+
JError::raiseWarning(500, 'Aramex: ' . $notify_error->Code .' - '. $notify_error->Message);
316320
}
317-
}
318-
else
321+
}
322+
else
319323
{
320-
JError::raiseWarning(500,'Aramex: ' . $auth_call->Shipments->ProcessedShipment->Notifications->Notification->Code . ' - '. $auth_call->Shipments->ProcessedShipment->Notifications->Notification->Message);
324+
foreach($auth_call->Shipments->ProcessedShipment->Notifications->Notification as $notify_error)
325+
{
326+
JError::raiseWarning(500, 'Aramex: ' . $notify_error->Code . ' - '. $notify_error->Message);
327+
}
321328
}
322329

323-
324-
$app->redirect(JRoute::_('index.php?option=com_virtuemart&view=cart'));exit;
330+
$app->redirect(JRoute::_('index.php?option=com_virtuemart&view=cart'));
331+
return FALSE;
325332
}
326-
327333
else
328334
{
329-
330335
//save file
336+
$filepath = JPATH_ROOT.DS.'media'.DS.'com_virtuemart'.DS.'labels'.DS;
337+
mkdir($filepath, 0755, true);
331338
$filename = basename($auth_call->Shipments->ProcessedShipment->ShipmentLabel->LabelURL);
332-
file_put_contents(JPATH_ROOT.DS.'media'.DS.$filename, file_get_contents($auth_call->Shipments->ProcessedShipment->ShipmentLabel->LabelURL));
339+
file_put_contents($filepath.$filename, file_get_contents($auth_call->Shipments->ProcessedShipment->ShipmentLabel->LabelURL));
333340

334341
$values['virtuemart_order_id'] = $order['details']['BT']->virtuemart_order_id;
335342
$values['order_number'] = $order['details']['BT']->order_number;
@@ -349,36 +356,38 @@ function plgVmConfirmedOrder (VirtueMartCart $cart, $order) {
349356
$fromname = $app->getCfg('fromname');
350357
$sitename = $app->getCfg('sitename');
351358

352-
$admin_body = $method->admin_body;
353-
$admin_body = str_replace("{shopper_name}",$shipto['first_name']. ' '. $shipto['last_name'],$admin_body);
354-
$admin_body = str_replace("{order_number}",$order['details']['BT']->order_number,$admin_body);
355-
$admin_body = str_replace("{reference_id}",$auth_call->Shipments->ProcessedShipment->ID,$admin_body);
356-
357-
$mail = JFactory::getMailer();
358-
$mail->addRecipient($method->admin_email);
359-
$mail->setSender(array($mailfrom, $fromname));
360-
$mail->setSubject($method->admin_subject);
361-
$mail->setBody($admin_body);
362-
$mail->addAttachment(JPATH_ROOT.DS.'media'.DS.$filename);
363-
$sent = $mail->Send();
359+
if ($method->admin_email)
360+
{
361+
$admin_body = $method->admin_body;
362+
$admin_body = str_replace("{shopper_name}",$shipto['first_name']. ' '. $shipto['last_name'],$admin_body);
363+
$admin_body = str_replace("{order_number}",$order['details']['BT']->order_number,$admin_body);
364+
$admin_body = str_replace("{reference_id}",$auth_call->Shipments->ProcessedShipment->ID,$admin_body);
365+
366+
$mail = JFactory::getMailer();
367+
$mail->addRecipient($method->admin_email);
368+
$mail->setSender(array($mailfrom, $fromname));
369+
$mail->setSubject($method->admin_subject);
370+
$mail->setBody($admin_body);
371+
$mail->addAttachment($filepath.$filename);
372+
$sent = $mail->Send();
373+
}
364374
//shopper
365-
$shopper_body = $method->shopper_body;
366-
$shopper_body = str_replace("{shopper_name}",$shipto['first_name']. ' '. $shipto['last_name'],$shopper_body);
367-
$shopper_body = str_replace("{order_number}",$order['details']['BT']->order_number,$shopper_body);
368-
$shopper_body = str_replace("{reference_id}",$auth_call->Shipments->ProcessedShipment->ID,$shopper_body);
369-
370-
$mail = JFactory::getMailer();
371-
$mail->addRecipient($shipto['email']);
372-
$mail->setSender(array($mailfrom, $fromname));
373-
$mail->setSubject($method->shopper_subject);
374-
$mail->setBody($shopper_body);
375-
$mail->addAttachment(JPATH_ROOT.DS.'media'.DS.$filename);
376-
$sent = $mail->Send();
377-
378-
375+
if ($shipto['email'])
376+
{
377+
$shopper_body = $method->shopper_body;
378+
$shopper_body = str_replace("{shopper_name}",$shipto['first_name']. ' '. $shipto['last_name'],$shopper_body);
379+
$shopper_body = str_replace("{order_number}",$order['details']['BT']->order_number,$shopper_body);
380+
$shopper_body = str_replace("{reference_id}",$auth_call->Shipments->ProcessedShipment->ID,$shopper_body);
381+
382+
$mail = JFactory::getMailer();
383+
$mail->addRecipient($shipto['email']);
384+
$mail->setSender(array($mailfrom, $fromname));
385+
$mail->setSubject($method->shopper_subject);
386+
$mail->setBody($shopper_body);
387+
$mail->addAttachment($filepath.$filename);
388+
$sent = $mail->Send();
389+
}
379390
}
380-
381-
382391
}
383392
catch (SoapFault $fault)
384393
{
@@ -541,6 +550,7 @@ function getCosts (VirtueMartCart $cart, $method, $cart_prices)
541550
),
542551
);
543552

553+
vmdebug('Rates params', $params);
544554

545555
if($method->ship_mode) //live mode
546556
{
@@ -552,6 +562,7 @@ function getCosts (VirtueMartCart $cart, $method, $cart_prices)
552562
}
553563

554564
$results = $soapClient->CalculateRate($params);
565+
vmdebug('Rates response', $results);
555566

556567
if($results->HasErrors)
557568
{
@@ -579,15 +590,40 @@ function getCosts (VirtueMartCart $cart, $method, $cart_prices)
579590
//return 100;
580591
}
581592

582-
/**
583-
* @param \VirtueMartCart $cart
584-
* @param int $method
585-
* @param array $cart_prices
586-
* @return bool
593+
/**
594+
* Check if the shipping conditions are fulfilled for this shipment method.
595+
*
596+
* @author Valerie Isaksen
597+
* @author Max Milbers
598+
* @param VirtueMartCart $cart
599+
* @param int $method
600+
* @param array $cart_prices
587601
*/
588602
protected function checkConditions ($cart, $method, $cart_prices) {
603+
$countries = array();
604+
if (!empty($method->countries)) {
605+
if (!is_array ($method->countries)) {
606+
$countries[0] = $method->countries;
607+
} else {
608+
$countries = $method->countries;
609+
}
610+
}
611+
612+
$address = (($cart->ST == 0) ? $cart->BT : $cart->ST);
613+
// probably did not gave his BT:ST address
614+
if (!is_array ($address)) {
615+
$address = array();
616+
$address['virtuemart_country_id'] = 0;
617+
}
618+
if (!isset($address['virtuemart_country_id'])) {
619+
$address['virtuemart_country_id'] = 0;
620+
}
621+
622+
if (in_array($address['virtuemart_country_id'], $countries) || count($countries) == 0) {
623+
return TRUE;
624+
}
589625

590-
return true;
626+
return FALSE;
591627
}
592628

593629
/**
@@ -613,7 +649,7 @@ function convert (&$method) {
613649
* This functions checks if the called plugin is active one.
614650
* When yes it is calling the standard method to create the tables
615651
*
616-
* @author Valérie Isaksen
652+
* @author Valérie Isaksen
617653
*
618654
*/
619655
function plgVmOnStoreInstallShipmentPluginTable ($jplugin_id) {

aramex.xml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
<authorUrl>http://hansakaonline.blogspot.com</authorUrl>
77
<copyright>Copyright (C) 2016 Hansaka Weerasingha. All rights reserved.</copyright>
88
<license>http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license>
9-
<version>2.1.1</version>
10-
<description>VMSHIPMENT_ARAMEX_PLUGIN_DESC</description>
9+
<version>2.2.0</version>
10+
<description><![CDATA[<p><a href="http://aramex.com/" target="_blank">Aramex</a> is a leading global provider of comprehensive logistics and transportation solutions, our breadth of services include express courierdelivery, freight forwarding, logistics, supply chain management, e-commerce and record management services.</p>
11+
12+
<p>This plugin simply provides integration to Aramex Web Service API to get acurate shipment rates and final shipment label upon order confirmation.</p>]]>
13+
</description>
14+
1115
<languages>
1216
<language tag="en-GB">language/en-GB.plg_vmshipment_aramex.ini</language>
1317
</languages>
@@ -25,6 +29,11 @@
2529
<option value="0">VMSHIPMENT_ARAMEX_CONFIG_SHIPMODE_TEST_LABEL</option>
2630
<option value="1">VMSHIPMENT_ARAMEX_CONFIG_SHIPMODE_LIVE_LABEL</option>
2731
</field>
32+
<!--<field name="generte_label" type="radio" scope="com_virtuemart" default="1" label="VMSHIPMENT_ARAMEX_CONFIG_GENERATE_LABEL" description="VMSHIPMENT_ARAMEX_CONFIG_GENERATE_LABEL_DESC">
33+
<option value="0">COM_VIRTUEMART_NO</option>
34+
<option value="1">COM_VIRTUEMART_YES</option>
35+
</field>-->
36+
<field name="countries" multiple="true" type="vmcountries" scope="com_virtuemart" default="" label="VMSHIPMENT_ARAMEX_CONFIG_COUNTRIES" description="VMSHIPMENT_ARAMEX_CONFIG_COUNTRIES_DESC"/>
2837
</fieldset>
2938
<fieldset name="acinfo">
3039
<field type="vmtitle" default="VMSHIPMENT_ARAMEX_CONFIG_ACCOUNT_INFORMATION" level="level1" />
@@ -107,5 +116,5 @@
107116
<field name="shopper_body" type="textarea" default="" cols="50" rows="5" label="VMSHIPMENT_ARAMEX_CONFIG_SHOPPER_BODY_LABEL" description=""/>
108117
</fieldset>
109118
</fields>
110-
</vmconfig>
119+
</vmconfig>
111120
</extension>

0 commit comments

Comments
 (0)