<?php
echo '<link rel="stylesheet" type="text/css" href="modules/EcmSales/css/style.css"/>';
echo '<script type="text/javascript"
src="include/jQuery/jquery.tablesorter.js"></script>';
echo '<script type="text/javascript"
src="include/jQuery/jquery.tablesorter.pager.js"></script>';
$db = $GLOBALS ['db'];

ini_set ( 'max_execution_time', 9999999999 );
ini_set ( 'memory_limit', - 1 );

global $current_user;
if (count ( $_REQUEST ['mass'] ) > 0) {
	$query = "select document_no, stock_id from ecmsales where id in ('" . implode ( "','", $_REQUEST ['mass'] ) . "') and deleted=0";
	
	$result = $db->query ( $query );
	while ( $dane = $db->fetchByAssoc ( $result ) ) {
		$documents_no [] = 'ZS ' . $dane ['document_no'];
		$stocks [] = $dane ['stock_id'];
	}
	// merge duplicates in stocks
	$stocks = array_unique ( $stocks );

	if (count($stocks) > 1) {
		die('Zamówienia są z różnych magazynów');
	}
/*
	if ($stocks[0] == null) {
		die('Zamówienia nie mają przypisanego magazynu');
	}
	*/
	$query = "select so_id from ecmstockdocouts where so_id in ('" . implode ( "','", $_REQUEST ['mass'] ) . "') and deleted=0";
	
	$no_wz_sales = [ ];
	$wz_sales = [ ];
	$result = $db->query ( $query );
	while ( $dane = $db->fetchByAssoc ( $result ) ) {
		$no_wz_sales [] = $dane ['so_id'];
		$wz_sales [] = $dane ['so_id'];
	}
	
	$query = "select document_no from ecmsales where id in ('" . implode ( "','", $wz_sales ) . "') and deleted=0";
	$wz_documents_no = [ ];
	$result = $db->query ( $query );
	while ( $dane = $db->fetchByAssoc ( $result ) ) {
		$wz_documents_no [] = 'ZS ' . $dane ['document_no'];
	}
	
	$no_wz_sales = array_diff ( $_REQUEST ['mass'], $no_wz_sales );
	$query = "select sum(quantity) as quantity, ecmproduct_id,code,name from ecmsaleitems where ecmsale_id in ('" . implode ( "','", $no_wz_sales ) . "') and deleted=0 group by ecmproduct_id";
	$result = $db->query ( $query );
	
	$total_products = 0;
	$product_id = [ ];
	while ( $dane = $db->fetchByAssoc ( $result ) ) {
		
		$product_id [] = $dane ['ecmproduct_id'];
		$produkt [$dane ['ecmproduct_id']] = $dane;
		$total_products = $total_products + $dane ['quantity'];
	}
	
	$query = "select  * from ecmstockstates where product_id in ('" . implode ( "','", $product_id ) . "') and stock_id='".$stocks[0]."' and deleted=0";
	$result = $db->query ( $query );
	
	$error = false;
	while ( $dane = $db->fetchByAssoc ( $result ) ) {
		$produkt [$dane ['product_id']] ['stock'] = $dane ['quantity'];

		if (round ( $produkt [$dane ['product_id']] ['quantity'], 2 ) > round ( $dane ['quantity'], 2 )) {
			
			$produkt [$dane ['product_id']] ['error'] = true;
			$error = true;
		} else {
			$produkt [$dane ['product_id']] ['error'] = false;
			
		}
		$total_in_stock = $total_in_stock + $dane ['quantity'];
	}
	$smarty = new Sugar_Smarty ();
	$smarty->assign ( 'MOD', $mod_strings );
	$smarty->assign ( "orderNo", implode ( ",", $documents_no ) );
	$smarty->assign ( "wz_documents_no", implode ( ",", $wz_documents_no ) );
	$smarty->assign ( 'total_products', $total_products );
	$smarty->assign ( 'no_wz_sales', $no_wz_sales );
	$smarty->assign ( 'error', $error );
	$smarty->assign ( 'total_in_stock', $total_in_stock );
	$smarty->assign ( 'products', $produkt );
	$smarty->display ( 'modules/EcmSales/tpls/wzCreator.html.tpl' );
} 
function createEcmStockDocOutPdf($record) {
	include_once ("modules/EcmStockDocOuts/PDFTemplate/helper.php");
	
	$focus = new EcmStockDocOut ();
	$focus->retrieve ( $record );
	
	$user = new User ();
	$user->retrieve ( $focus->assigned_user_id );
	
	include_once ("include/MPDF57/mpdf.php");
	$p = new mPDF ( '', 'A4', null, 'helvetica', 10, 10, 30, 45, 5, 5 );
	$labels = return_module_language ( $focus->ecmlanguage, 'EcmStockDocOuts' );
	$mpdf->mirrorMargins = 1;
	$db = $GLOBALS ['db'];
	$positions = formatPDFPositions22 ( $focus->getPositionList ( true ), $focus );
	$quantity = getQuantity22 ( $focus->getPositionList ( true ), $focus );
	$content = '';
	
	include ("modules/EcmStockDocOuts/PDFTemplate/content.php");
	
	$ec = new EcmSysInfo ();
	
	$footer = $ec->getFooterForModule ( 'EcmStockDocOuts' );
	$header = $ec->getHeaderForModule ( 'EcmStockDocOuts' );
	$p->SetHTMLHeader ( $header );
	$p->SetHTMLFooter ( $footer );
	$p->WriteHTML ( $content );
	
	$EcmSysInfo = new EcmSysInfo ();
	$dir = 'upload/' . $EcmSysInfo->getDatabaseName () . '/pdf/EcmStockDocOuts/';
	if (! is_dir ( $dir )) {
		mkdir ( $dir, '755', true );
	}
	$file_name = create_guid () . '.pdf';
	$p->Output ( $dir . $file_name, "F" );
	
	return $dir . $file_name;
}
function createEcmInvoiceOutPdf($record, $outputtype) {
	$EcmInvoiceOut = new EcmInvoiceOut ();
	$EcmInvoiceOut->retrieve ( $record );
	$file_location = $EcmInvoiceOut->get_PDF_file_path ( '0' );
	$EcmSysInfo = new EcmSysInfo ();
	if ($EcmSysInfo->getDatabaseName () == 'preDb_60b08fe051546309b61d2714d4a0438d') {
		$file_location = newPDFCreator ( $record, $type, true );
	}
	// return;
	switch ($outputtype) {
		case "BROSWER" :
			header ( "Location: " . $file_location );
			return true;
		case "FILE" :
			$path = $file_location;
			break;
		case "EMAIL" :
			include_once 'include/ECM/EcmSendMail/EcmSendMail.inc';
			$path = EcmSendMail::TEMP_DIR . 'ZS_' . $EcmInvoiceOut->number . '.pdf';
			copy ( $file_location, $path );
			
			break;
		case "MULTIPDF" :
			include_once 'include/ECM/EcmMultiPdf/EcmMultiPdf.inc';
			$path = EcmMultiPdf::TEMP_DIR . 'EcmSales_' . $focus->id . '_' . create_guid () . '.pdf';
			copy ( $file_location, $path );
			break;
	}
	;
	return $file_location;
}
?>