<?php
class OrdersAdmin extends Orders{
	function throwOrdersAdmin( $iList = null ){
		$oDB = DataBase::getInstance();
		if( !isset( $iList ) ){
			$iList = $GLOBALS['config']['admin_list'];
		}
		$iPageNumber = isset( $_GET['iPage'] ) ? $_GET['iPage'] : 1;
		$sql = 'SELECT SQL_CALC_FOUND_ROWS DISTINCT `o`.`iOrder` FROM `' . $oDB->throwTableName( DB_ORDERS ) . '` `o`';
		
		if( ( isset( $GLOBALS['sPhrase'] ) && !empty( $GLOBALS['sPhrase'] ) ) || ( isset( $GLOBALS['sDateFrom'] ) && !empty( $GLOBALS['sDateFrom'] ) ) || ( isset( $GLOBALS['sDateTo'] ) && !empty( $GLOBALS['sDateTo'] ) ) || ( isset( $_GET['iStatus'] ) && ( is_numeric( $_GET['iStatus'] ) || ( $_GET['iStatus'] == 'x' ) ) ) || ( isset( $_GET['iUser'] ) && is_numeric( $_GET['iUser'] ) ) ){
		
			if( isset( $GLOBALS['sPhrase'] ) && !empty( $GLOBALS['sPhrase'] ) ){
				preg_match_all( '/(-?)((\\"([^\\"]+)\\")|([^\\"\\s][^\\s]*))/', htmlspecialchars_decode( $GLOBALS['sPhrase'] ), $aWords, PREG_SET_ORDER );
				$aSearchP = Array( );
				foreach( $aWords as $aWord ){
					$sWord = $aWord[4];
					if( empty( $sWord ) ) $sWord = $aWord[5];
					$sWord = $oDB->escape( $sWord );
					
					$aFields = Array( '`o`.`sFirstName`', '`o`.`sLastName`', '`o`.`sCompanyName`', '`o`.`sStreet`', '`o`.`sZipCode`', '`o`.`sCity`', '`o`.`sPhone`', '`o`.`sPostalName`', '`o`.`sPostalStreet`', '`o`.`sPostalZipCode`', '`o`.`sPostalCity`', '`o`.`sPostalPhone`', '`o`.`sEmail`', '`o`.`sNip`', '`o`.`iOrder`' );
					
					if( $aWord[1] == '-' ){
						$sSearchP  = '( ';
						foreach( $aFields as $sField ){
							$sSearchP .= '( ' . $sField . ' IS NULL OR ' . $sField . ' NOT LIKE \'%' . $sWord . '%\' ) AND ';
						}
						$sSearchP .= 'NOT EXISTS( SELECT 1 FROM `' . $oDB->throwTableName( DB_ORDERS_PRODUCTS ) . '` `ops` WHERE `ops`.`sName` LIKE \'%' . $sWord . '%\' AND `ops`.`iOrder` = `o`.`iOrder` ) )';
						$aSearchP[] = $sSearchP;
					} else{
						$sSearchP  = '( ';
						foreach( $aFields as $sField ){
							$sSearchP .= $sField . ' LIKE \'%' . $sWord . '%\' OR ';
						}
						$sSearchP .= '`op`.`sName` LIKE \'%' . $sWord . '%\' )';
						$aSearchP[] = $sSearchP;
					}
				}
				$sql.= ' INNER JOIN `' . $oDB->throwTableName( DB_ORDERS_PRODUCTS ) . '` `op` USING( `iOrder` ) WHERE ( ' . implode( ' AND ', $aSearchP ) . ' )';
			} else{
				$sql.= ' WHERE 1';
			}

			if( isset( $_GET['sDateFrom'] ) && !empty( $_GET['sDateFrom'] ) ){
				$sql.= ' AND ' . dateToTime( $_GET['sDateFrom'] . ' 00:00:00' ) . ' <= `o`.`iTime`';
			}
			if( isset( $_GET['sDateTo'] ) && !empty( $_GET['sDateTo'] ) ){
				$sql.= ' AND ' . dateToTime( $_GET['sDateTo'] . ' 23:59:59' ) . ' >= `o`.`iTime`';
			}
			
			if( isset( $_GET['iStatus'] ) && ( is_numeric( $_GET['iStatus'] ) || $_GET['iStatus'] == 'x' ) ){
				if( ( $_GET['iStatus'] == 'x' ) ){
					$sql.= ' AND `o`.`iStatus` <= 50 AND `o`.`iStatus` != 8 AND `o`.`iStatus` != 2';
				} else{
					$sql.= ' AND `o`.`iStatus` = ' . intval( $_GET['iStatus'] );
				}
			}

			if( isset( $_GET['iUser'] ) && is_numeric( $_GET['iUser'] ) ){
				$sql.= ' AND `iUser` = ' . intval( $_GET['iUser'] );
			}
		}
		$sql .= ' ORDER BY `iOrder` DESC';
		if( $iList != 'inf' ){
			$sql .= ' LIMIT ' . ( $iList * ( $iPageNumber - 1 ) ) . ',' . $iList;
		}
		$aOrders = $oDB->executeSQL( $sql, true, DataBase::SINGLE_COLUMN );
		if( $iList != 'inf' ){
			$iCount = $oDB->executeSQL( 'SELECT FOUND_ROWS()', true, DataBase::SINGLE_VALUE );
			$aReturn = Array( 'aPagesCountData' => Array( 'iCount' => ceil( $iCount / $iList ), 'iPageNumber' => $iPageNumber ) );
		}
		$this->generateCache();
		$mysqlnd = function_exists('mysqli_fetch_all');
		if( $mysqlnd ){
			$VCstatement = mysqli_stmt_init( $oDB->rDBConnection );
			$r = mysqli_stmt_prepare( $VCstatement, 'SELECT `iOrder` FROM `' . $oDB->throwTableName( DB_ORDERS ) . '` WHERE `iStatus` IN( 51, 52, 53, 9, 10, 11, 12, 13, 16 ) AND `iOrder` != ? AND ( ( `sFirstName` = ? AND `sLastName` = ? ) OR `sCompanyName` = ? OR `sNip` = ? OR ( `sStreet` = ? AND `sCity` = ? ) OR `sPhone` = ? OR `sEmail` = ? OR `iUser` = ? OR `sIp` = ? )' );
			$r = mysqli_stmt_bind_param( $VCstatement, 'issssssssis', $iOrder, $sFirstName, $sLastName, $sCompanyName, $sNip, $sStreet, $sCity, $sPhone, $sEmail, $iUser, $sIp );
			
			$VCstatementOK = mysqli_stmt_init( $oDB->rDBConnection );
			$r = mysqli_stmt_prepare( $VCstatementOK, 'SELECT `iOrder` FROM `' . $oDB->throwTableName( DB_ORDERS ) . '` WHERE `iStatus` = 8 AND `iOrder` != ? AND ( ( `sFirstName` = ? AND `sLastName` = ? ) OR `sCompanyName` = ? OR `sNip` = ? OR ( `sStreet` = ? AND `sCity` = ? ) OR `sPhone` = ? OR `sEmail` = ? OR `iUser` = ? OR `sIp` = ? )' );
			$r = mysqli_stmt_bind_param( $VCstatementOK, 'issssssssis', $iOrder, $sFirstName, $sLastName, $sCompanyName, $sNip, $sStreet, $sCity, $sPhone, $sEmail, $iUser, $sIp );
		}
		foreach( $aOrders as $iOrder ){
			$aData = $oDB->throwDataFromFiles( Array( DB_ORDERS, DB_ORDERS_COMMENTS ), $iOrder, 'iOrder' );
			
			$aOrder = $this->aOrders[$iOrder];
			$this->aProducts = null;
			if( isset( $aOrder['iOrder'] ) ){
				$sFirstName = $aOrder['sFirstName'];
				$sLastName = $aOrder['sLastName'];
				$sCompanyName = $aOrder['sCompanyName'];
				$sNip = $aOrder['sNip'];
				$sStreet = $aOrder['sStreet'];
				$sCity = $aOrder['sCity'];
				$sPhone = $aOrder['sPhone'];
				$sEmail = $aOrder['sEmail'];
				$iUser = $aOrder['iUser'];
				$sIp = $aOrder['sIp'];
				$aOrder['sComment'] = $aData['sComment'];
				$aOrder['sCommentAdmin'] = $aData['sCommentAdmin'];

				if( $mysqlnd ){
					$r = mysqli_stmt_execute( $VCstatement );
					$result = mysqli_stmt_get_result($VCstatement);
					while ( $row = mysqli_fetch_array( $result, MYSQLI_NUM ) ){
						$aOrder['aVCOrders'][] = $row[0];
					}
					
					$r = mysqli_stmt_execute( $VCstatementOK );
					$result = mysqli_stmt_get_result($VCstatementOK);
					while ( $row = mysqli_fetch_array( $result, MYSQLI_NUM ) ){
						$aOrder['aVCOrdersOK'][] = $row[0];
					}
				}

				$aOrder['aProducts'] = $this->throwProducts( $aOrder['iOrder'], false );

				$aReturn['aList'][] = $aOrder;
			}
		}
		if( $mysqlnd ){
			mysqli_stmt_close( $VCstatement );
		}
		return $aReturn;
	}

/******************************************************/

	function saveOrders( $aForm ){
		if( isset( $aForm['aStatus'] ) && is_array( $aForm['aStatus'] ) && $aForm['iStatus'] > 0 ){
			if( !isset( $this->aOrders ) ) $this->generateCache( );
			foreach( $aForm['aStatus'] as $iOrder => $sValue ){
				if( $this->aOrders[$iOrder]['iStatus'] != $aForm['iStatus'] ){
					$aChange[$iOrder] = $aForm['iStatus'];
				}
			}
		}
		if( isset( $aChange ) ){
			foreach( $aChange as $iOrder => $iStatus ){
				$this->changeStatus( $iOrder, $iStatus );
			}
			$this->generateCache( );
		}
	}
	function deleteOrder( $iOrder ){
		$oDB = & DataBase::getInstance( );
		$oDB->executeSQL( 'START TRANSACTION' );
		$oDB->deleteInFile( DB_ORDERS, $iOrder, 'iOrder' );
		$oDB->deleteInFile( DB_ORDERS_STATUS, $iOrder, 'iOrder' );
		$oDB->deleteInFile( DB_ORDERS_PRODUCTS, $iOrder, 'iOrder' );
		$oDB->deleteInFile( DB_ORDERS_COMMENTS, $iOrder, 'iOrder' );
		$oDB->executeSQL( 'COMMIT' );
	}
	function saveOrder( $aForm ){
		$oDB = & DataBase::getInstance( );
		$aOrder = $this->throwOrder( $aForm['iOrder'] );
		$aForm = changeMassTxt( $aForm, 'H', Array( 'sComment', 'LenHNds' ), Array( 'sCommentAdmin', 'LenHNds' ) );
		if( !isset( $aForm['iInvoice'] ) ) $aForm['iInvoice'] = null;
		if( ( $aForm['sComment'] != $aOrder['sComment'] ) || ( $aForm['sCommentAdmin'] != $aOrder['sCommentAdmin'] ) ){
			$oDB->save( DB_ORDERS_COMMENTS, $aForm, 'iOrder' );
		}
		$aForm = array_merge( $aOrder, $aForm );
		$oDB->save( DB_ORDERS, $aForm, 'iOrder', null );
		if( $aForm['iStatus'] != $aOrder['iStatus'] ){
			$this->changeStatus( $aForm['iOrder'], $aForm['iStatus'], $aOrder['iStatus'] );
		}
	}
	function throwPaymentsAdmin( ){
		$oDB = & DataBase::getInstance( );
		return $oDB->throwFileArray( DB_PAYMENTS );
	}
	function throwActivePaymentsAdmin( ){
		$oDB = & DataBase::getInstance( );
		$sql = 'SELECT * FROM `' . $oDB->throwTableName( DB_PAYMENTS ) . '` WHERE `iStatus` = 1 AND ( `iOuterSystem` NOT IN ( 5, 7, 8 ) OR `iOuterSystem` IS NULL ) AND `iInvoicePayment` != 1';
		return $oDB->executeSQL( $sql, true );
	}
	function deletePayment( $iPayment ){
		$oDB = & DataBase::getInstance( );
		$oDB->deleteInFile( DB_PAYMENTS, $iPayment, 'iPayment' );
		$oDB->deleteInFile( DB_CARRIERS_PAYMENTS, $iPayment, 'iPayment' );
	}
	function savePayment( $aForm ){
		$oDB = & DataBase::getInstance( );
		if( isset( $aForm['iPayment'] ) && is_numeric( $aForm['iPayment'] ) ){
			$sParam = 'iPayment';
		} else{
			$sParam = null;
		}
		$aForm['iPayment'] = $oDB->save( DB_PAYMENTS, changeMassTxt( $aForm, '', Array( 'sDescription', 'nds' ), Array( 'sEmailDescription', 'nds' ) ), $sParam );
		return $aForm['iPayment'];
	}
	function throwCarriersAdmin( ){
		$oDB = & DataBase::getInstance( );
		$aData = $oDB->throwFileArray( DB_CARRIERS );
		if( is_array( $aData ) ) foreach( $aData as $iCarrier => $aCarrier ){
			$aData[$iCarrier]['sPrice'] = displayPrice( $aCarrier['fPrice'] );
		}
		return $aData;
	}
	function listCarriersAdmin( ){
		$oDB = & DataBase::getInstance( );
		return $oDB->throwFileArray( DB_CARRIERS );
	}
	function deleteCarrier( $iCarrier ){
		$oDB = & DataBase::getInstance( );
		$oDB->deleteInFile( DB_CARRIERS, $iCarrier, 'iCarrier' );
		$oDB->deleteInFile( DB_CARRIERS_PAYMENTS, $iCarrier, 'iCarrier' );
		$oDB->deleteInFile( DB_CARRIERS_COUNTRIES, $iCarrier, 'iCarrier' );
		$oDB->deleteInFile( DB_CARRIERS_PRICES, $iCarrier, 'iCarrier' );
	}
	function saveCarrier( $aForm ){
		$oDB = & DataBase::getInstance( );
		$oDB->executeSQL( 'START TRANSACTION' );
		if( isset( $aForm['iCarrier'] ) && is_numeric( $aForm['iCarrier'] ) ){
			$sParam = 'iCarrier';
			$oDB->deleteInFile( DB_CARRIERS_PAYMENTS, $aForm['iCarrier'], 'iCarrier' );
			$oDB->deleteInFile( DB_CARRIERS_COUNTRIES, $aForm['iCarrier'], 'iCarrier' );
			$oDB->deleteInFile( DB_CARRIERS_PRICES, $aForm['iCarrier'], 'iCarrier' );
		} else{
			$sParam = null;
		}
		$aForm['iCarrier'] = $oDB->save( DB_CARRIERS, $aForm, $sParam );
		if( isset( $aForm['aPayments'] ) ){
			foreach( $aForm['aPayments'] as $iPayment => $iValue ){
				$sPrice = isset( $aForm['aPaymentsPrices'][$iPayment] ) ? $aForm['aPaymentsPrices'][$iPayment] : null;
				$fFreeDelivery = isset( $aForm['aPaymentsDeliveries'][$iPayment] ) ? $aForm['aPaymentsDeliveries'][$iPayment] : null;
				$oDB->save( DB_CARRIERS_PAYMENTS, Array( 'iPayment' => $iPayment, 'sPrice' => $sPrice, 'iCarrier' => $aForm['iCarrier'], 'fFreeDelivery' => $fFreeDelivery ) );
			}
		}
		if( isset( $aForm['aCountries'] ) && is_array( $aForm['aCountries'] ) ){
			foreach( $aForm['aCountries'] as $iCountry ){
				$oDB->save( DB_CARRIERS_COUNTRIES, Array( 'iCarrier' => $aForm['iCarrier'], 'iCountry' => $iCountry ) );
			}
		}
		if( isset( $aForm['aCountriesRegions'] ) && is_array( $aForm['aCountriesRegions'] ) ){
			foreach( $aForm['aCountriesRegions'] as $iCountry => $aRegions ) foreach( $aRegions as $iRegion ){
				$oDB->save( DB_CARRIERS_COUNTRIES, Array( 'iCarrier' => $aForm['iCarrier'], 'iCountry' => $iCountry, 'iRegion' => $iRegion ) );
			}
		}
		if( isset( $aForm['aPrices'] ) && is_array( $aForm['aPrices'] ) ){
			foreach( $aForm['aPrices'] as $aPriceData['fMinWeight'] => $aPriceData['aInfo'] ) foreach( $aPriceData['aInfo'] as $aPriceData['fMinSummary'] => $aPriceData['fPrice'] ){
				$aPriceData['iCarrier'] = $aForm['iCarrier'];
				$oDB->save( DB_CARRIERS_PRICES, $aPriceData );
			}
		}
		$oDB->executeSQL( 'COMMIT' );
		return $aForm['iCarrier'];
	}
	function listPaymentsCarriersAdmin( $iCarrier = null ){
		$oDB = & DataBase::getInstance( );
		$sql = 'SELECT `p`.`iPayment`, `p`.`sName`, `pc`.`sPrice`, IF( `pc`.`iCarrier` IS NOT NULL, 1, 0 ) AS `iStatus`, `pc`.`fFreeDelivery` FROM `' . $oDB->throwTableName( DB_PAYMENTS ) . '` `p` LEFT JOIN ( SELECT `iCarrier`, `sPrice`, `iPayment`, `fFreeDelivery` FROM `' . $oDB->throwTableName( DB_CARRIERS_PAYMENTS ) . '` WHERE `iCarrier` = ' . intval( $iCarrier ) . ' ) `pc` USING( `iPayment` ) ORDER BY `p`.`iPayment` ASC';
		return $oDB->executeSQL( $sql, true );
	}
	function throwCarrierCountriesArray( $iCarrier ){
		$oDB = DataBase::getInstance( );
		$sql = 'SELECT `iCountry`
							FROM `' . $oDB->throwTableName( DB_CARRIERS_COUNTRIES ) . '`
							WHERE `iRegion` IS NULL AND `iCarrier` = ' . intval( $iCarrier );
		return $oDB->executeSQL( $sql, true, DataBase::SINGLE_COLUMN );
	}
	function throwCarrierRegionsArray( $iCarrier ){
		$oDB = DataBase::getInstance( );
		$sql = 'SELECT `iRegion`
							FROM `' . $oDB->throwTableName( DB_CARRIERS_COUNTRIES ) . '`
							WHERE `iRegion` IS NOT NULL AND `iCarrier` = ' . intval( $iCarrier );
		return $oDB->executeSQL( $sql, true, DataBase::SINGLE_COLUMN );
	}
	function throwOrderStatuses( $iOrder ){
		$oDB = & DataBase::getInstance( );
		$sql = 'SELECT `iStatus`, `iTime`
							FROM `' . $oDB->throwTableName( DB_ORDERS_STATUS ) . '`
							WHERE `iOrder` = ' . intval( $iOrder ) . '
							ORDER BY `iTime` DESC';
		$aStatuses = $oDB->executeSQL( $sql, true );
		if( is_array( $aStatuses ) ) foreach( $aStatuses as &$aStatus ){
			$aStatus['sDate'] = date( 'Y-m-d H:i', $aStatus['iTime'] );
			$aStatus['sStatus'] = $this->throwStatus( $aStatus['iStatus'] );
		}
		return $aStatuses;
	}
};
?>