464 lines
20 KiB
PHP
Executable File

<?php
// require_once dirname(__FILE__).'/PHPExcel.php';
class MakeFile {
const TYPE_BDC = 1;
static $arrayBorder = array(
'borders' => array(
'allborders' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN,
'color' => array(
'rgb' => '000000'
)
)
)
);
static $arrayBorderAutour = array(
'borders' => array(
'left' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN,
),
'right' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN,
),
'bottom' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN,
),
'top' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN,
)
)
);
public static function makeFileByType($private_sale, $lines, $type = self::TYPE_BDC, $private_sales_export = null)
{
require(dirname(__FILE__).'/PHPExcel.php');
$objPHPExcel = new PHPExcel();
$TVA = '20';
switch($type)
{
case self::TYPE_BDC :
$show_product_ids = LOGISTIQUE_SHOW_DECIMAL_ID_PRODUCT;
// $title = 'Bon de commande ' . $private_sale->name . ' du '.$date_from->getDate('STR').' au '.$date_to->getDate('STR');
$title = 'BDC '.$private_sale->title.' - n°'. Db::getInstance()->getValue('SELECT MAX(`id_privatesales_export`) FROM `'._DB_PREFIX_.'privatesales_logistique_export`');
$letter_bdc_logo = 'B';
$letter_bdc_logo_end = 'C';
$letter_bdc_title = 'D';
$letter_tel = 'B';
$letter_tel_end = 'D';
$letter_date = 'E';
$letter_address_title = 'B';
$letter_address = 'C';
$letter_reference = 'B';
if($show_product_ids)
{
$letter_product = 'C';
$letter_product_attribute = 'D';
$letter_product_quantite = 'E';
$letter_product_name = 'F';
$letter_prix_unitaire = 'G';
$letter_prix_total = 'H';
$letter_total_title = 'G';
$letter_total_resultat = 'H';
$lastColumn = 'H';
}
else
{
$letter_product_quantite = 'C';
$letter_product_name = 'D';
$letter_prix_unitaire = 'E';
$letter_prix_total = 'F';
$letter_total_title = 'E';
$letter_total_resultat = 'F';
$lastColumn = 'F';
}
$letter_date_end = $lastColumn;
$letter_address_end = $lastColumn;
break;
}
$objPHPExcel->getProperties()->setCreator("Antadis");
$objPHPExcel->getProperties()->setTitle( $title );
$objPHPExcel->getProperties()->setSubject( $title );
$objPHPExcel->getProperties()->setDescription( $title );
$activeSheet = $objPHPExcel->getActiveSheet();
$i = 1;
$activeSheet->getRowDimension($i)->setRowHeight(80);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LOGO
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(file_exists(_PS_IMG_DIR_.'logo.jpg'))
{
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setWorksheet($activeSheet);
$objDrawing->setName("name");
$objDrawing->setDescription("Description");
$objDrawing->setPath(_PS_IMG_DIR_.'logo.jpg');
$objDrawing->setCoordinates($letter_bdc_logo.$i);
$objDrawing->setOffsetX(1);
$objDrawing->setOffsetY(5);
$activeSheet->getRowDimension($i)->setRowHeight(70);
}
else
{
$activeSheet->getRowDimension($i)->setRowHeight(30);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TITRE
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
$activeSheet->mergeCells($letter_bdc_logo.$i.':'.$letter_bdc_logo_end.$i);
$activeSheet->SetCellValue($letter_bdc_title.$i, 'Bon de commande'.($private_sales_export !== null ? ' n°'.$private_sales_export->id : ''));
$activeSheet->getStyle($letter_bdc_title.$i)->getFont()->setBold(true);
$activeSheet->getStyle($letter_bdc_title.$i)->getFont()->setSize(20);
$activeSheet->getStyle($letter_bdc_title.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$activeSheet->getStyle($letter_bdc_title.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$activeSheet->mergeCells($letter_bdc_title.$i.':'.$lastColumn.$i);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//INFOS AU DESSUS FOURNISSEUR
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
$i++;
// $activeSheet->SetCellValue($letter_tel.$i, 'Téléphone : '.Configuration::get('BDC_SHOP_PHONE', null, null, Context::getContext()->shop->id) );
// $activeSheet->getStyle($letter_tel.$i)->getFont()->setBold(true);
// $activeSheet->mergeCells($letter_tel.$i.':'.$letter_tel_end.$i);
$activeSheet->SetCellValue($letter_date.$i, 'Date : '.date('d/m/Y') );
$activeSheet->getStyle($letter_date.$i)->getFont()->setBold(true);
$activeSheet->getStyle($letter_date.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$activeSheet->mergeCells($letter_date.$i.':'.$letter_date_end.$i);
$i ++;
$i ++;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//FOURNISSEUR
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(isset($marque)
&& Validate::isLoadedObject($marque))
{
$no_address = false;
$id_address = $marque->getAddresses(Context::getContext()->language->id);
if(count($id_address) == 0)
$no_address = true;
if(!$no_address)
{
$id_address = $id_address[0]['id_address'];
if(!empty($id_address) && Validate::isLoadedObject($address = new Address($id_address)))
{
$address_txt = $marque->name.chr(13).AddressFormat::generateAddress($address);
$activeSheet->getCell($letter_address.$i)->setValue($address_txt);
$activeSheet->getRowDimension($i)->setRowHeight(75);
$activeSheet->getStyle($letter_address.$i)->getFont()->setBold(true);
}
else
$no_address = true;
}
if($no_address)
{
$activeSheet->getCell($letter_address.$i)->setValue('Aucune information de marque');
$i++;
}
$activeSheet->getStyle($letter_address.$i)->getAlignment()->setWrapText(true);
$activeSheet->getStyle($letter_address.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
$activeSheet->getStyle($letter_address.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$activeSheet->getStyle($letter_address.$i.':'.$lastColumn.$i)->applyFromArray(self::$arrayBorderAutour);
$activeSheet->mergeCells($letter_address.$i.':'.$letter_address_end.$i);
$i ++;
}
$i ++;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//ADRESSE DU SITE
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
$activeSheet->SetCellValue($letter_address_title.$i, 'Adresse de livraison : ');
$activeSheet->getStyle($letter_address_title.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
$activeSheet->getStyle($letter_address_title.$i)->getFont()->setBold(true);
$activeSheet->getStyle($letter_address_title.$i)->getFont()->setSize(12);
$activeSheet->getStyle($letter_address_title.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$styleArray = array(
'font' => array(
'underline' => PHPExcel_Style_Font::UNDERLINE_SINGLE
)
);
$activeSheet->getStyle($letter_address_title.$i)->applyFromArray($styleArray);
$activeSheet->getRowDimension($i)->setRowHeight(65);
$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);
$iOri = $i;
// str_replace(chr(13), pack("ss", 0x0A, 0x00), Configuration::get('BDC_SHOP_ADDRESS', null, null, Context::getContext()->shop->id) )
$activeSheet->SetCellValue($letter_address.$i, str_replace('<br />', ' ' , nl2br(Configuration::get('BDC_SHOP_ADDRESS', null, null, Context::getContext()->shop->id))));
$activeSheet->getStyle($letter_address.$i)->getAlignment()->setWrapText(true);
$activeSheet->getStyle($letter_address.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
$activeSheet->getStyle($letter_address.$i)->getFont()->setSize(10);
$activeSheet->getStyle($letter_address.$i)->getFont()->getColor()->setRGB('000000');
$activeSheet->getStyle($letter_address.$i)->getFont()->setBold(true);
$activeSheet->getRowDimension($i)->setRowHeight(110);
$activeSheet->mergeCells($letter_address.$i.':'.$letter_address_end.$i );
$activeSheet->mergeCells($letter_address.$iOri.':'.$letter_address.$i );
$activeSheet->getStyle($letter_address_title.($iOri).':'.$letter_address_end.$i)->applyFromArray(self::$arrayBorderAutour);
$i++;
$i++;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TABLEAU DES CHIFFRES
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
$activeSheet->SetCellValue($letter_reference.$i, 'Code');
$activeSheet->getStyle($letter_reference.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$activeSheet->getStyle($letter_reference.$i)->getFont()->setBold(true);
//$activeSheet->getStyle('E'.$i)->getBorders()->setBold(true);
if($show_product_ids)
{
$activeSheet->SetCellValue($letter_product.$i, 'ID Produit');
$activeSheet->getStyle($letter_product.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$activeSheet->getStyle($letter_product.$i)->getFont()->setBold(true);
$activeSheet->SetCellValue($letter_product_attribute.$i, 'ID Attribut');
$activeSheet->getStyle($letter_product_attribute.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$activeSheet->getStyle($letter_product_attribute.$i)->getFont()->setBold(true);
}
$activeSheet->SetCellValue($letter_product_quantite.$i, 'Quantité');
$activeSheet->getStyle($letter_product_quantite.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$activeSheet->getStyle($letter_product_quantite.$i)->getFont()->setBold(true);
$activeSheet->SetCellValue($letter_product_name.$i, 'Désignation Article');
$activeSheet->getStyle($letter_product_name.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$activeSheet->getStyle($letter_product_name.$i)->getFont()->setBold(true);
$activeSheet->setCellValue($letter_prix_unitaire.$i, 'PU HT');
$activeSheet->getStyle($letter_prix_unitaire.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$activeSheet->getStyle($letter_prix_unitaire.$i)->getFont()->setBold(true);
$activeSheet->setCellValue($letter_prix_total.$i, 'Prix Total HT');
$activeSheet->getStyle($letter_prix_total.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$activeSheet->getStyle($letter_prix_total.$i)->getFont()->setBold(true);
$i++;
$iOri = $i;
//p(count($lines));
$count = 0;
if($type == self::TYPE_BDC)
foreach($lines as $line)
foreach($line as $l)
$count += $l['total'];
$arrayTmp = array();
foreach($lines as $id_product => $arrayProduct)
{
foreach($arrayProduct as $id_product_attribute => $values)
{
$arrayTmp[$values['line']['product_reference']] = $values;
}
}
$lines2 = $arrayTmp;
ksort($lines2);
foreach($lines2 as $values)
{
$id_product_attribute = $values['line']['product_attribute_id'];
$id_product = $values['line']['product_id'];
$current_product = MakeStats::$array_product[$id_product];
if($id_product_attribute != 0)
{
$combination = new Combination($id_product_attribute);
if($combination->wholesale_price == 0)
$wholesale_price = $current_product->wholesale_price;
else
$wholesale_price = $combination->wholesale_price;
}
else
{
$wholesale_price = $current_product->wholesale_price;
}
$activeSheet->SetCellValue($letter_reference.$i, $values['line']['product_reference']);
$activeSheet->getStyle($letter_reference.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_TEXT );
$activeSheet->SetCellValue($letter_product_name.$i, $values['line']['product_name']);
if($show_product_ids)
{
$activeSheet->SetCellValue($letter_product.$i, $values['line']['product_id']);
$activeSheet->SetCellValue($letter_product_attribute.$i, $values['line']['product_attribute_id']);
}
$activeSheet->SetCellValue($letter_product_quantite.$i, $values['total']);
$activeSheet->getStyle($letter_product_quantite.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$activeSheet->setCellValue($letter_prix_unitaire.$i, (float)$wholesale_price );
$activeSheet->setCellValue($letter_prix_total.$i, '='.$letter_product_quantite.$i.'*'.$letter_prix_unitaire.$i);
$activeSheet->getStyle($letter_prix_unitaire.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR );
$activeSheet->getStyle($letter_prix_total.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR );
$i++;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TOTAUX
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
$activeSheet->SetCellValue($letter_reference.$i, 'Total Produit');
$activeSheet->getStyle($letter_reference.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$activeSheet->getStyle($letter_reference.$i)->getFont()->setBold(true);
$activeSheet->SetCellValue($letter_product_quantite.$i, '=SUM('.$letter_product_quantite.($iOri).':'.$letter_product_quantite.($i-1).')');
$activeSheet->getStyle($letter_product_quantite.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$activeSheet->getStyle($letter_product_quantite.$i)->getFont()->setBold(true);
// if($type == 2)
// {
// $activeSheet->SetCellValue($letter_prix_unitaire.$i, '=SUM('.$letter_prix_unitaire.($iOri).':'.$letter_prix_unitaire.($i-1).')');
// $activeSheet->getStyle($letter_prix_unitaire.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
// $activeSheet->getStyle($letter_prix_unitaire.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR );
// $activeSheet->SetCellValue('G'.$i, '=SUM(G'.($iOri).':G'.($i-1).')');
// $activeSheet->getStyle('G'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
// $activeSheet->getStyle('G'.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR );
// }
$activeSheet->getStyle($letter_reference.($iOri-1).':'.$lastColumn.$i)->applyFromArray(self::$arrayBorder);
$i++;
$activeSheet->setCellValue($letter_total_title.$i, 'Total HT');
$activeSheet->getStyle($letter_total_title.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
if($type == self::TYPE_BDC)
$activeSheet->getStyle($letter_total_title.$i)->getFont()->setBold(true);
$activeSheet->setCellValue($letter_total_resultat.$i, '=SUM('.$letter_total_resultat.($iOri).':'.$letter_total_resultat.($i-2).')');
$activeSheet->getStyle($letter_total_resultat.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR );
// if($type == 2)
// {
// $activeSheet->setCellValue('H'.$i, '=SUM(H'.($iOri).':H'.($i-2).')' );
// $activeSheet->getStyle('H'.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR );
// $activeSheet->getStyle('H'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
// $activeSheet->getStyle('H'.$i)->getFont()->setBold(true);
// $activeSheet->setCellValue('I'.$i, '=SUM(I'.($iOri).':I'.($i-2).')' );
// $activeSheet->getStyle('I'.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR );
// $activeSheet->getStyle('I'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
// $activeSheet->getStyle('I'.$i)->getFont()->setBold(true);
// }
$i++;
$activeSheet->setCellValue($letter_total_title.$i, 'TVA '.$TVA);
$activeSheet->getStyle($letter_total_title.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
if($type == self::TYPE_BDC)
$activeSheet->getStyle($letter_total_title.$i)->getFont()->setBold(true);
$activeSheet->setCellValue($letter_total_resultat.$i, '='.$letter_total_resultat.($i-1).'*'.($TVA/100) );
$activeSheet->getStyle($letter_total_resultat.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR );
$i++;
$activeSheet->setCellValue($letter_total_title.$i, 'TOTAL TTC');
$activeSheet->getStyle($letter_total_title.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
if($type == self::TYPE_BDC)
$activeSheet->getStyle($letter_total_title.$i)->getFont()->setBold(true);
$activeSheet->setCellValue($letter_total_resultat.$i, '='.$letter_total_resultat.($i-1).'+'.$letter_total_resultat.($i-2) );
$activeSheet->getStyle($letter_total_resultat.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR );
if($type == self::TYPE_BDC)
$activeSheet->getStyle('F'.$i)->getFont()->setBold(true);
$activeSheet->getStyle($letter_total_title.($i-3).':'.$lastColumn.$i)->applyFromArray(self::$arrayBorder);
$activeSheet->getColumnDimension($letter_reference)->setWidth(25);
$activeSheet->getColumnDimension($letter_product_quantite)->setWidth(13);
$activeSheet->getColumnDimension($letter_product_name)->setWidth(50);
$activeSheet->getColumnDimension($letter_prix_unitaire)->setWidth(13);
$activeSheet->getColumnDimension($letter_prix_total)->setWidth(13);
// if($type == 2)
// {
// $activeSheet->getColumnDimension('G')->setWidth(13);
// $activeSheet->getColumnDimension('H')->setWidth(13);
// $activeSheet->getColumnDimension('I')->setWidth(13);
// }
$value = Configuration::get('BDC_SHOP_BOTTOM_CONTENT', null, null, Context::getContext()->shop->id);
if($value != '')
{
$i++;
$i++;
$activeSheet->setCellValue('B'.$i, $value);
$activeSheet->mergeCells('B'.$i.':D'.$i );
$activeSheet->getRowDimension($i)->setRowHeight(80);
$activeSheet->getStyle('B'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
$activeSheet->getStyle('B'.$i)->getAlignment()->setWrapText(true);
}
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$title.'.xlsx"');
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
if(Validate::isLoadedObject($private_sales_export))
{
$objWriter->save($private_sales_export->getFile());
echo file_get_contents($private_sales_export->getFile());
}
else
{
$objWriter->save('php://output');
}
exit;
}
public static function makeCSV($content, $name = 'Export')
{
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$name.".csv\";" );
header("Content-Transfer-Encoding: binary");
echo $content;
exit;
}
}