<?php
include($_SERVER['DOCUMENT_ROOT'] .'/script/HeaderInc.php');
//SiteLibs::GetVarDump($_POST);
$VarRepairId = (int)$_GET['repair_id'];

$ObjRepairMod = new RepairMod($MS_SP);
$ArrRepair = $ObjRepairMod->GetFromDb('SqlAll', 'GetRow', "a.repair_id = '". $VarRepairId ."'");
$smarty->assign('ArrRepair', $ArrRepair);
//SiteLibs::GetVarDump($ArrRepair);
$ObjSvtMod = new SvtMod($MS_SP);
$ArrSvt = $ObjSvtMod->GetFromDb('SqlAll', 'GetRow', "a.svt_id = '". $ArrRepair['svt_id'] ."'");
$smarty->assign('ArrSvt', $ArrSvt);
//SiteLibs::GetVarDump($ArrSvt);

$ObjSvtStatusMod = new SvtStatusMod($MS_SP);
$ArrSvtStatus = $ObjSvtStatusMod->GetFromDb('SqlAllLeft', 'GetAll', "b.svt_id = '". $ArrRepair['svt_id'] ."'");

include($_SERVER['DOCUMENT_ROOT'] .'/script/phpqrcode/qrlib.php'); 
function CreateQr($VarId = 0){
  $pngAbsoluteFilePath = '';
  if(true === is_numeric($VarId)){
    $VarContents = SiteLibs::scheme_url() . $_SERVER['HTTP_HOST'] . '/OpRepairDetail.php?repair_id='. $VarId;
    $tempDir = $_SERVER['DOCUMENT_ROOT'] . '/../tmp/'; 
    $fileName = 'qr_repair_'.$VarId .'.png';
    $pngAbsoluteFilePath = $tempDir.$fileName; 
    if (!file_exists($pngAbsoluteFilePath)) { 
      QRcode::png($VarContents, $pngAbsoluteFilePath); 
    }
  }
  return $pngAbsoluteFilePath;
}

$PHPWord  = new PHPWord();
$section  = $PHPWord->createSection();
$styleTable = array('borderSize'=>6, 'cellMargin'=>80);

$styleCell = array('valign'=>'center');
$styleCell2 = array('valign'=>'center','width'=> 2000);

$fontStyleTH = array('bold'=>true, 'align'=>'left', 'spaceAfter'=>0);
$fontStyle = array('bold'=>false, 'align'=>'left');
$paragStyle = array('spaceAfter'=>0);
$imageStyle = array('width'=>$MS_SP->Sp_Config['QrCodesize'], 'height'=>$MS_SP->Sp_Config['QrCodesize']);

$PHPWord->addTableStyle('gridprint', $styleTable);
$table = $section->addTable('gridprint');
$table->addRow();
$table->addCell($styleCell)->addText("QR-код страницы ремонта", $fontStyleTH, $paragStyle);
$table->addCell(3000)->addImage(CreateQr($ArrRepair['repair_id']), $imageStyle);

$table->addRow();
$table->addCell($styleCell)->addText("№ ремонта", $fontStyleTH, $paragStyle);
$table->addCell(2000)->addText($ArrRepair['repair_id'], $fontStyle, $paragStyle);
$table->addRow();
$table->addCell($styleCell)->addText("Дата начала ремонта", $fontStyleTH, $paragStyle);
$table->addCell(2000)->addText(SiteLibs::ConvertSqlDateToText($ArrRepair['repair_date_b']), $fontStyle, $paragStyle);
$table->addRow();
$table->addCell($styleCell)->addText("Поставщик услуги", $fontStyleTH, $paragStyle);
$table->addCell(2000)->addText($ArrRepair['organiz_name'], $fontStyle, $paragStyle);
$table->addRow();
$table->addCell($styleCell)->addText("Наименование сет.", $fontStyleTH, $paragStyle);
$table->addCell(2000)->addText($ArrSvt['svt_name'], $fontStyle, $paragStyle);
$table->addRow();
$table->addCell($styleCell)->addText("Наименование бух.", $fontStyleTH, $paragStyle);
$table->addCell(2000)->addText($ArrSvt['svt_buxname'], $fontStyle, $paragStyle);
$table->addRow();
$table->addCell($styleCell)->addText("Тип СВТ", $fontStyleTH, $paragStyle);
$table->addCell(2000)->addText($ArrSvt['svttype_name'], $fontStyle, $paragStyle);
$table->addRow();
$table->addCell($styleCell)->addText("Инв. номер", $fontStyleTH, $paragStyle);
$table->addCell(2000)->addText($ArrSvt['svt_invnum'], $fontStyle, $paragStyle);
$table->addRow();
$table->addCell($styleCell)->addText("Сер. номер", $fontStyleTH, $paragStyle);
$table->addCell(2000)->addText($ArrSvt['svt_serialnum'], $fontStyle, $paragStyle);
$table->addRow();
$table->addCell($styleCell)->addText("Подразделение", $fontStyleTH, $paragStyle);
$table->addCell(2000)->addText($ArrSvt['structureorganization_title'], $fontStyle, $paragStyle);
$table->addRow();
$table->addCell($styleCell)->addText("Пользователь", $fontStyleTH, $paragStyle);
$table->addCell(2000)->addText($ArrSvt['users_fio'], $fontStyle, $paragStyle);
$table->addRow();
$table->addCell($styleCell)->addText("Поставщик", $fontStyleTH, $paragStyle);
$table->addCell(2000)->addText($ArrSvt['organiz_name'], $fontStyle, $paragStyle);
$table->addRow();
$table->addCell($styleCell)->addText("Дата поставки", $fontStyleTH, $paragStyle);
$table->addCell(2000)->addText(SiteLibs::ConvertSqlDateToText($ArrSvt['svt_date']), $fontStyle, $paragStyle);
$table->addRow();
$table->addCell($styleCell)->addText("Гарантия", $fontStyleTH, $paragStyle);
$table->addCell(2000)->addText(SiteLibs::ConvertSqlDateToText($ArrSvt['svt_garant_date']), $fontStyle, $paragStyle);
$table->addRow();
$table->addCell($styleCell)->addText("Примечание", $fontStyleTH, $paragStyle);
$table->addCell(2000)->addText($ArrSvt['svt_note'], $fontStyle, $paragStyle);

$section->addTextBreak(2);

$PHPWord->addTableStyle('gridprint2', $styleTable);
$table = $section->addTable('gridprint2');
$table->addRow();
$table->addCell(3000, $styleCell2)->addText("Перемещение", $fontStyleTH, $paragStyle);
$table->addCell(2000)->addText("Фамилия И.О.", $fontStyleTH, $paragStyle);
$table->addCell(2000)->addText("Подпись", $fontStyleTH, $paragStyle);
$table->addCell(2000)->addText("Дата", $fontStyleTH, $paragStyle);
$table->addRow();
$table->addCell(3000, $styleCell2)->addText("Передал СВТ отдел ИТ:", $fontStyle, $paragStyle);
$table->addCell(2000)->addText("", $fontStyle, $paragStyle);
$table->addCell(2000)->addText("", $fontStyle, $paragStyle);
$table->addCell(2000)->addText("", $fontStyle, $paragStyle);
$table->addRow();
$table->addCell(3000, $styleCell2)->addText("Принял СВТ:", $fontStyle, $paragStyle);
$table->addCell(2000)->addText("", $fontStyle, $paragStyle);
$table->addCell(2000)->addText("", $fontStyle, $paragStyle);
$table->addCell(2000)->addText("", $fontStyle, $paragStyle);
$table->addRow();
$table->addCell(3000, $styleCell2)->addText("Отвез СВТ в ремонт:", $fontStyle, $paragStyle);
$table->addCell(2000)->addText("", $fontStyle, $paragStyle);
$table->addCell(2000)->addText("", $fontStyle, $paragStyle);
$table->addCell(2000)->addText("", $fontStyle, $paragStyle);
$table->addRow();
$table->addCell(3000, $styleCell2)->addText("Привез СВТ из ремонта:", $fontStyle, $paragStyle);
$table->addCell(2000)->addText("", $fontStyle, $paragStyle);
$table->addCell(2000)->addText("", $fontStyle, $paragStyle);
$table->addCell(2000)->addText("", $fontStyle, $paragStyle);
$table->addRow();
$table->addCell(3000, $styleCell2)->addText("Передал СВТ в отдел:", $fontStyle, $paragStyle);
$table->addCell(2000)->addText("", $fontStyle, $paragStyle);
$table->addCell(2000)->addText("", $fontStyle, $paragStyle);
$table->addCell(2000)->addText("", $fontStyle, $paragStyle);
$table->addRow();
$table->addCell(3000, $styleCell2)->addText("Принял СВТ от отдела ИТ:", $fontStyle, $paragStyle);
$table->addCell(2000)->addText("", $fontStyle, $paragStyle);
$table->addCell(2000)->addText("", $fontStyle, $paragStyle);
$table->addCell(2000)->addText("", $fontStyle, $paragStyle);

//foreach ($ArrValues['thead'] as $value) {
//  
//  $table->addCell($styleCell)->addText($value, $fontStyleTH, $paragStyle);
//}
//$i = count($ArrValues['colspan'][0]);
//$table->addRow();
//$VarFirst = true;
//

//
//foreach ($ArrValues['tbody'] as $key => $value) {
//  if(true === $VarFirst || $i == count($ArrValues['thead'])){
//    $VarFirst = false;
//    $value = CreateQr($qrcode, $value);
//    if ('' != $value){
//      $imageStyle = array('width'=>$MS_SP->Sp_Config['QrCodesize'], 'height'=>$MS_SP->Sp_Config['QrCodesize']);
//      $VarImage = true;
//    }
//    else{
//      $value = '';
//    }
//  }
//  if($i == count($ArrValues['thead'])){
//    $table->addRow();
//    $i = count($ArrValues['colspan'][0]);
//  }
//  if(isset($ArrValues['colspan'][$key]) && 0 < $ArrValues['colspan'][$key]){
//    $styleCellC=array('gridSpan' => $ArrValues['colspan'][$key]);
//    if(true === $VarImage){
//      $table->addCell(2000,$styleCellC)->addImage($value, $imageStyle);
//      $VarImage = false;
//    }
//    else{
//      $table->addCell(10,$styleCellC)->addText($value, $fontStyle, $paragStyle);
//    }
//  }
//  else{
//    if(true === $VarImage){
//      $table->addCell(2000)->addImage($value, $imageStyle);
//      $VarImage = false;
//    }
//    else{
//      $table->addCell(10)->addText($value, $fontStyle, $paragStyle);
//    }
//  }
//  $i++;
//}
$file = 'DemandTransport_'. $VarRepairId .'_'. date("d_m_Y_H_i_s").'.docx';
$filePath = $_SERVER['DOCUMENT_ROOT'] .'/../tmp/'.$file;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save($filePath);
include($_SERVER['DOCUMENT_ROOT'] .'/script/FooterInc.php');
if(!$filePath) {     
  die('file not found'); 
} 
else {
  ob_clean();
  header("Content-Disposition: attachment; filename=".$file);     
  header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");     
  header('Content-Length: '. filesize($filePath));    
  readfile($filePath); 
  unlink($filePath);
  exit;
}
?>
