addition of import/export for delivery prices in adminShipping

This commit is contained in:
Marion Muszynski 2017-01-24 14:02:45 +01:00
parent 8ae11e0adb
commit 17ac2cc24c

View File

@ -1,6 +1,6 @@
<?php
/*
* 2007-2011 PrestaShop
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
@ -87,48 +87,152 @@ class AdminShipping extends AdminTab
{
if ($this->tabAccess['edit'] === '1')
{
if (($id_carrier = (int)(Tools::getValue('id_carrier'))) AND $id_carrier == ($id_carrier2 = (int)(Tools::getValue('id_carrier2'))))
/* @Override Antadis */
if (Tools::isSubmit('submitFeesImportCSV'))
{
$carrier = new Carrier($id_carrier);
if (Validate::isLoadedObject($carrier))
{
/* Get configuration values */
$shipping_method = $carrier->getShippingMethod();
$rangeTable = $carrier->getRangeTable();
$carrier->deleteDeliveryPrice($rangeTable);
$this->processFeesImportCsv();
if (!count($this->_delivery_prices)){
$this->_errors[] = Tools::displayError('Les données du fichier ne correspondent pas au transporteur.');
return;
}
} elseif(Tools::isSubmit('exportFeesImportCSV')) {
$this->processFeesExportCsv();
/* @End Override Antadis */
} else {
/* Build prices list */
$priceList = '';
foreach ($_POST AS $key => $value)
if (strstr($key, 'fees_'))
{
$tmpArray = explode('_', $key);
$priceList .= '('.($shipping_method == Carrier::SHIPPING_METHOD_PRICE ? (int)($tmpArray[2]) : 'NULL').',
'.($shipping_method == Carrier::SHIPPING_METHOD_WEIGHT ? (int)($tmpArray[2]) : 'NULL').', '.(int)$carrier->id.',
'.(int)$tmpArray[1].', '.number_format(abs(preg_replace("#,#", '.', $value)), 6, '.', '').'),';
unset($tmpArray);
}
$priceList = rtrim($priceList, ',');
/* Update delivery prices */
$carrier->addDeliveryPrice($priceList);
Tools::redirectAdmin($currentIndex.'&conf=6'.'&token='.$this->token);
if (($id_carrier = (int)(Tools::getValue('id_carrier'))) AND $id_carrier == ($id_carrier2 = (int)(Tools::getValue('id_carrier2'))))
{
$carrier = new Carrier($id_carrier);
if (Validate::isLoadedObject($carrier))
{
/* Get configuration values */
$shipping_method = $carrier->getShippingMethod();
$rangeTable = $carrier->getRangeTable();
$carrier->deleteDeliveryPrice($rangeTable);
/* Build prices list */
$priceList = '';
foreach ($_POST AS $key => $value)
if (strstr($key, 'fees_'))
{
$tmpArray = explode('_', $key);
$priceList .= '('.($shipping_method == Carrier::SHIPPING_METHOD_PRICE ? (int)($tmpArray[2]) : 'NULL').',
'.($shipping_method == Carrier::SHIPPING_METHOD_WEIGHT ? (int)($tmpArray[2]) : 'NULL').', '.(int)$carrier->id.',
'.(int)$tmpArray[1].', '.number_format(abs(preg_replace("#,#", '.', $value)), 6, '.', '').'),';
unset($tmpArray);
}
$priceList = rtrim($priceList, ',');
/* Update delivery prices */
$carrier->addDeliveryPrice($priceList);
Tools::redirectAdmin($currentIndex.'&conf=6'.'&token='.$this->token);
}
else
$this->_errors[] = Tools::displayError('An error occurred while updating fees (cannot load carrier object).');
}
elseif (isset($id_carrier2))
{
$_POST['id_carrier'] = $id_carrier2;
}
else
$this->_errors[] = Tools::displayError('An error occurred while updating fees (cannot load carrier object).');
}
elseif (isset($id_carrier2))
{
$_POST['id_carrier'] = $id_carrier2;
}
else
$this->_errors[] = Tools::displayError('An error occurred while updating fees (cannot load carrier object).');
}
else
$this->_errors[] = Tools::displayError('You do not have permission to edit here.');
}
}
public function processFeesExportCsv(){
$id_carrier = Tools::getValue('id_carrier2');
$carrier = new Carrier((int) $id_carrier);
$rangeObj = $carrier->getRangeObject();
$ranges = $rangeObj->getRanges($id_carrier);
$delivery = Carrier::getDeliveryPriceByRanges('range_weight', $id_carrier);
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Description: File Transfer');
header('Content-Type: application/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=export_insurances.csv;');
header('Content-Transfer-Encoding: binary');
$headers = array('id_zone');
foreach ($ranges as $range)
{
$headers[] = $range['delimiter1'] . 'kg ' . $this->l('to') . ' ' . $range['delimiter2'] . 'kg';
}
$range_prices = array();
foreach ($delivery as $price)
{
if (!isset($range_prices[$price['id_zone']]))
{
$range_prices[$price['id_zone']] = array();
}
$range_prices[$price['id_zone']][$price['id_range_weight']] = $price;
}
ob_clean();
$f = fopen('php://output', 'w');
fputcsv($f, $headers, ';', '"');
foreach ($range_prices as $id_zone => $prices)
{
$row_data = array($id_zone);
foreach ($ranges as $range) {
if (isset($prices[$range['id_range_weight']]))
{
$row_data[] = (float) $prices[$range['id_range_weight']]['price'];
}
else
{
$row_data[] = 0.00;
}
}
fputcsv($f, $row_data, ';', '"');
}
fclose($f);
exit;
}
public function processFeesImportCsv(){
if (!isset($_FILES['feesImportCsvFile']) || !$_FILES['feesImportCsvFile']['tmp_name']){
$this->_errors[] = Tools::displayError('No file.');
return false;
}
$id_carrier = Tools::getValue('id_carrier2');
$carrier = new Carrier((int) $id_carrier);
$rangeObj = $carrier->getRangeObject();
$ranges = $rangeObj->getRanges($id_carrier);
$zones = $carrier->getZones();
$filename = dirname(__FILE__) . '/../' . uniqid() . '_' . Tools::encrypt($id_carrier) . '.csv';
if (!move_uploaded_file(
$_FILES['feesImportCsvFile']['tmp_name'],
$filename
)) {
$this->_errors[] = Tools::displayError('Erreur lors du téléchargement du fichier');
return false;
}
$i = 0;
$this->_delivery_prices = array();
$handle = fopen($filename, 'r');
while(($delivery_info = fgetcsv($handle, 0, ';') ) !== FALSE)
{
$i++;
if ($i <= 1)
continue;
if (!isset($delivery_info[0]) || !Validate::isUnsignedId($delivery_info[0]))
continue;
if (count($ranges) +1 != count($delivery_info))
continue;
$_id_zone = $delivery_info[0];
$this->_delivery_prices[$_id_zone] = array();
unset($delivery_info[0]);
foreach ($delivery_info as $price)
$this->_delivery_prices[$_id_zone][] = $price;
}
if (file_exists($filename))
unlink($filename);
return $this->_delivery_prices;
}
public function display()
{
$this->displayFormHandling();
@ -146,7 +250,7 @@ class AdminShipping extends AdminTab
unset($confKeys['PS_SHIPPING_METHOD']);
echo '
<form action="'.$currentIndex.'&submitHandling'.$this->table.'=1&token='.$this->token.'" method="post">
<form action="'.$currentIndex.'&submitHandling'.$this->table.'=1&token='.$this->token.'" method="post" >
<fieldset>
<legend><img src="../img/admin/delivery.gif" />'.$this->l('Handling').'</legend>';
@ -195,7 +299,7 @@ class AdminShipping extends AdminTab
public function displayFormFees()
{
global $currentIndex;
$carrierArray = array();
$id_carrier = Tools::getValue('id_carrier');
$carriers = Carrier::getCarriers((int)(Configuration::get('PS_LANG_DEFAULT')), true , false,false, NULL, Carrier::PS_CARRIERS_AND_CARRIER_MODULES_NEED_RANGE);
@ -211,13 +315,13 @@ class AdminShipping extends AdminTab
$id_carrier = (int)$carrierArray[0]['id'];
$carrierSelected = new Carrier($id_carrier);
}
echo '<br /><br />
<h2>'.$this->l('Fees by carrier, geographical zone, and ranges').'</h2>
<form action="'.$currentIndex.'&token='.$this->token.'" id="fees" name="fees" method="post">
<form action="'.$currentIndex.'&token='.$this->token.'" id="fees" name="fees" method="post" enctype="multipart/form-data">
<fieldset>
<legend><img src="../img/admin/delivery.gif" />'.$this->l('Fees').'</legend>';
if (!count($carrierArray))
echo $this->l('You only have free carriers, there is no need to configure your delivery prices.');
else
@ -233,7 +337,7 @@ class AdminShipping extends AdminTab
<th>'.$this->l('Zone / Range').'</th>';
$currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
$rangeObj = $carrierSelected->getRangeObject();
$rangeTable = $carrierSelected->getRangeTable();
$suffix = $carrierSelected->getRangeSuffix();
@ -260,15 +364,17 @@ class AdminShipping extends AdminTab
echo '<td class="center">'.$currency->getSign('left').'<input type="text" id="fees_all_'.$range[$rangeIdentifier].'" onchange="this.value = this.value.replace(/,/g, \'.\');" onkeyup="if ((event.keyCode||event.which) != 9){ spreadFees('.$range[$rangeIdentifier].') }" style="width: 45px;" />'.$currency->getSign('right').'</td>';
echo '</tr>';
}
foreach ($zones AS $zone)
{
echo '
<tr>
<th style="height: 30px;">'.$zone['name'].'</th>';
foreach ($ranges AS $range)
foreach ($ranges AS $_index => $range)
{
if (isset($deliveryArray[$zone['id_zone']][$id_carrier][$range[$rangeIdentifier]]))
if (isset($this->_delivery_prices[$zone['id_zone']][$_index]))
$price = $this->_delivery_prices[$zone['id_zone']][$_index];
elseif (isset($deliveryArray[$zone['id_zone']][$id_carrier][$range[$rangeIdentifier]]))
$price = $deliveryArray[$zone['id_zone']][$id_carrier][$range[$rangeIdentifier]];
else
$price = '0.00';
@ -278,7 +384,24 @@ class AdminShipping extends AdminTab
</tr>';
}
}
/* @Override Antadis - import csv */
if (sizeof($ranges) && !$carrierSelected->is_free){
echo '<tr>
<td colspan="'.(sizeof($ranges) + 1).'" class="left" style="border-bottom: none; height: 40px;">
<input type="submit" value="'.$this->l('Export CSV').'" class="button" name="exportFeesImportCSV" />
</td>
</tr>';
echo '<tr>
<td colspan="'.(sizeof($ranges) + 1).'" class="left" style="border-bottom: none; height: 40px;">
<input type="file" name="feesImportCsvFile" />
&nbsp;&nbsp;&nbsp;
<input type="submit" value="'.$this->l('Import CSV').'" class="button" name="submitFeesImportCSV" />
</td>
</tr>';
}
/* @End Override */
echo '<tr>
<td colspan="'.(sizeof($ranges) + 1).'" class="center" style="border-bottom: none; height: 40px;">
<input type="hidden" name="submitFees'.$this->table.'" value="1" />';