// SupplyOrders: CSV
This commit is contained in:
+15
-17
@@ -27,28 +27,26 @@
|
||||
|
||||
/**
|
||||
* Simple class to output CSV data
|
||||
* Uses CollectionCore
|
||||
* @since 1.5
|
||||
*/
|
||||
class CSVCore
|
||||
{
|
||||
public $filename;
|
||||
public $objects;
|
||||
public $collection;
|
||||
public $delimiter;
|
||||
|
||||
/**
|
||||
* Loads objects, filename and optionnaly a delimiter.
|
||||
* @param Array|object $objects : Assumed to be an Array of objects or an object
|
||||
* @param Collection|Array : colletion of objects / array (of non-objects)
|
||||
* @param string $filename : used later to save the file
|
||||
* @param string $delimiter Optional : delimiter used
|
||||
*/
|
||||
public function __construct(&$objects, $filename, $delimiter = ';')
|
||||
public function __construct(&$collection, $filename, $delimiter = ';')
|
||||
{
|
||||
$this->filename = $filename;
|
||||
$this->delimiter = $delimiter;
|
||||
|
||||
$this->objects = $objects;
|
||||
if (!is_array($objects))
|
||||
$this->objects = array($objects);
|
||||
$this->collection = $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,16 +58,15 @@ class CSVCore
|
||||
{
|
||||
$this->headers();
|
||||
|
||||
$current_object_name = '';
|
||||
foreach ($this->objects as $object)
|
||||
$header_line = false;
|
||||
|
||||
foreach ($this->collection as $object)
|
||||
{
|
||||
$vars = get_object_vars($object);
|
||||
|
||||
// ouputs keys if needed
|
||||
if (get_class($object) != $current_object_name)
|
||||
if (!$header_line)
|
||||
{
|
||||
$this->output(array_keys($vars));
|
||||
$current_object_name = get_class($object);
|
||||
$header_line = true;
|
||||
}
|
||||
|
||||
// outputs values
|
||||
@@ -93,9 +90,9 @@ class CSVCore
|
||||
* @param string $data
|
||||
* @return string $data
|
||||
*/
|
||||
public function wrap($data)
|
||||
public static function wrap($data)
|
||||
{
|
||||
$data = preg_replace('/"(.+)"/', '""$1""', $data);
|
||||
$data = Tools::safeOutput($data, '";');
|
||||
return sprintf('"%s"', $data);
|
||||
}
|
||||
|
||||
@@ -104,8 +101,9 @@ class CSVCore
|
||||
*/
|
||||
public function headers()
|
||||
{
|
||||
header('Content-Type: application/csv');
|
||||
header("Content-disposition: attachment; filename={$this->filename}.csv");
|
||||
header('Content-type: text/csv');
|
||||
header('Cache-Control: no-store, no-cache');
|
||||
header('Content-disposition: attachment; filename="'.$this->filename.'.csv"');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -154,9 +154,12 @@ class SupplyOrderCore extends ObjectModel
|
||||
'is_template' => 'isBool',
|
||||
);
|
||||
|
||||
/**
|
||||
* @var array Defition used by the Collection object
|
||||
*/
|
||||
public static $definition = array(
|
||||
'table' => 'supply_order',
|
||||
'primary' => 'id_supply_order',
|
||||
'table' => 'supply_order',
|
||||
'primary' => 'id_supply_order',
|
||||
);
|
||||
|
||||
public function getFields()
|
||||
|
||||
@@ -345,6 +345,18 @@ class AdminSupplyOrdersControllerCore extends AdminController
|
||||
if (!($this->tabAccess['add'] === '1'))
|
||||
unset($this->toolbar_btn['new']);
|
||||
|
||||
$this->toolbar_btn['export-csv-orders'] = array(
|
||||
'short' => 'Export Orders',
|
||||
'href' => $this->context->link->getAdminLink('AdminSupplyOrders').'&csv_orders',
|
||||
'desc' => $this->l('Export Orders (CSV)'),
|
||||
);
|
||||
|
||||
$this->toolbar_btn['export-csv-details'] = array(
|
||||
'short' => 'Export Orders Details',
|
||||
'href' => $this->context->link->getAdminLink('AdminSupplyOrders').'&csv_orders_details',
|
||||
'desc' => $this->l('Export Orders Details (CSV)'),
|
||||
);
|
||||
|
||||
// overrides query
|
||||
$this->_select = '
|
||||
s.name AS supplier,
|
||||
@@ -375,6 +387,12 @@ class AdminSupplyOrdersControllerCore extends AdminController
|
||||
$this->_where .= ' AND st.enclosed != 1';
|
||||
$first_list = parent::renderList();
|
||||
|
||||
if (Tools::isSubmit('csv_orders') || Tools::isSubmit('csv_orders_details') || Tools::isSubmit('csv_order_details'))
|
||||
{
|
||||
$this->renderCSV();
|
||||
die;
|
||||
}
|
||||
|
||||
// second list : templates
|
||||
$second_list = null;
|
||||
unset($this->tpl_list_vars['warehouses']);
|
||||
@@ -383,6 +401,8 @@ class AdminSupplyOrdersControllerCore extends AdminController
|
||||
|
||||
// unsets actions
|
||||
$this->actions = array();
|
||||
unset($this->toolbar_btn['export-csv-orders']);
|
||||
unset($this->toolbar_btn['export-csv-details']);
|
||||
// adds actions
|
||||
$this->addRowAction('delete');
|
||||
$this->addRowAction('view');
|
||||
@@ -1027,6 +1047,70 @@ class AdminSupplyOrdersControllerCore extends AdminController
|
||||
$this->loadProducts($quantity_threshold);
|
||||
}
|
||||
|
||||
protected function renderCSV()
|
||||
{
|
||||
// exports orders
|
||||
if (Tools::isSubmit('csv_orders'))
|
||||
{
|
||||
$ids = array();
|
||||
foreach ($this->_list as $entry)
|
||||
$ids[] = $entry['id_supply_order'];
|
||||
|
||||
$orders = new Collection('SupplyOrder', $id_lang = (int)Context::getContext()->language->id);
|
||||
$orders->where('is_template = 0');
|
||||
$orders->where('id_supply_order IN('.implode(', ', $ids).')');
|
||||
$orders->getAll();
|
||||
$csv = new CSV($orders, $this->l('supply_orders'));
|
||||
$csv->export();
|
||||
}
|
||||
// exports details for all orders
|
||||
else if (Tools::isSubmit('csv_orders_details'))
|
||||
{
|
||||
// header
|
||||
header('Content-type: text/csv');
|
||||
header('Cache-Control: no-store, no-cache');
|
||||
header('Content-disposition: attachment; filename="'.$this->l('supply_orders_details').'.csv"');
|
||||
|
||||
// echoes details
|
||||
$ids = array();
|
||||
foreach ($this->_list as $entry)
|
||||
$ids[] = $entry['id_supply_order'];
|
||||
|
||||
// for each supply order
|
||||
$keys = array('id_supply_order', 'id_product', 'id_product_attribute', 'reference', 'supplier_reference', 'ean13', 'upc', 'name',
|
||||
'unit_price_te', 'quantity_expected', 'quantity_received', 'price_te', 'discount_rate', 'discount_value_te',
|
||||
'price_with_discount_te', 'tax_rate', 'tax_value', 'price_ti', 'tax_value_with_order_discount',
|
||||
'price_with_order_discount_te');
|
||||
echo sprintf("%s\n", implode(';', array_map(array('CSVCore', 'wrap'), $keys)));
|
||||
|
||||
foreach ($ids as $id)
|
||||
{
|
||||
$query = new DbQuery();
|
||||
$query->select(implode(',', $keys));
|
||||
$query->from('supply_order_detail');
|
||||
$query->where('id_supply_order = '.(int)$id);
|
||||
$query->orderBy('id_supply_order_detail DESC');
|
||||
$resource = Db::getInstance()->execute($query);
|
||||
// gets details
|
||||
while ($row = Db::getInstance()->nextRow($resource))
|
||||
echo sprintf("%s\n", implode(';', array_map(array('CSVCore', 'wrap'), $row)));
|
||||
}
|
||||
|
||||
}
|
||||
// exports details for the given order
|
||||
else if (Tools::isSubmit('csv_order_details') && Tools::getValue('id_supply_order'))
|
||||
{
|
||||
$supply_order = new SupplyOrder((int)Tools::getValue('id_supply_order'));
|
||||
if (Validate::isLoadedObject($supply_order))
|
||||
{
|
||||
$details = $supply_order->getEntriesCollection();
|
||||
$details->getAll();
|
||||
$csv = new CSV($details, $this->l('supply_order').'_'.$supply_order->reference.'_details');
|
||||
$csv->export();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for AdminSupplyOrdersController::postProcess()
|
||||
*
|
||||
@@ -1632,14 +1716,14 @@ class AdminSupplyOrdersControllerCore extends AdminController
|
||||
|
||||
$content = '<span style="width:20px; margin-right:5px;">';
|
||||
if ($supply_order_state->editable == false)
|
||||
$content .= '<a href="pdf.php?id_supply_order='.(int)$supply_order->id.'"><img src="../img/admin/pdf.gif" alt="invoice" /></a>';
|
||||
$content .= '<a href="pdf.php?id_supply_order='.(int)$supply_order->id.'" title="Export as PDF"><img src="../img/admin/pdf.gif" alt="invoice" /></a>';
|
||||
else
|
||||
$content .= '-';
|
||||
$content .= '</span>';
|
||||
|
||||
$content .= '<span style="width:20px">';
|
||||
if ($supply_order_state->enclosed == true && $supply_order_state->receipt_state == true)
|
||||
$content .= '<a href="csv.php?id_supply_order='.(int)$supply_order->id.'"><img src="../img/admin/excel_file.png" alt="csv" /></a>';
|
||||
$content .= '<a href="'.$this->context->link->getAdminLink('AdminSupplyOrders').'&id_supply_order='.(int)$supply_order->id.'&csv_order_details" title="Export as CSV"><img src="../img/admin/excel_file.png" alt="" /></a>';
|
||||
else
|
||||
$content .= '-';
|
||||
$content .= '</span>';
|
||||
|
||||
Reference in New Issue
Block a user