<?php
function deleteProducer( $iProducer ){
	$oDB = DataBase::getInstance( );
	$oDB->deleteInFile( DB_PRODUCERS, $iProducer, 'iProducer' );
}
function saveProducer( $aForm ){
	$oDB = DataBase::getInstance( );
	$oFile = FilesAdmin::getInstance( );
	if( isset( $aForm['iProducer'] ) && is_numeric( $aForm['iProducer'] ) ){
		$sParam = 'iProducer';
		$aForm = array_merge( throwProducer( $aForm['iProducer'] ), $aForm );
	} else{
		$sParam = null;
	}
	$aForm = changeMassTxt( $aForm, '' );
	if( !isset( $aForm['iPosition'] ) || !is_numeric( $aForm['iPosition'] ) || $aForm['iPosition'] < - 99 || $aForm['iPosition'] > 999 ) $aForm['iPosition'] = 0;
	if( !empty( $aForm['iProducer'] ) && ( ( isset( $_FILES['producerLogo']['name'] ) && !empty( $_FILES['producerLogo']['name'] ) && isset( $aForm['sLogoUrl'] ) ) || ( isset( $aForm['iDeleteLogo'] ) && isset( $aForm['sLogoUrl'] ) ) ) ){
		$oFile->deleteSelectedFiles( Array( $aForm['iProducer'] => 1 ), 50, 'iProducer', 'sLogoUrl' );
		$aForm['sLogoUrl'] = '';
	}
	if( !empty( $aForm['iProducer'] ) && ( ( isset( $_FILES['sGuaranteeFile']['name'] ) && !empty( $_FILES['sGuaranteeFile']['name'] ) && isset( $aForm['sGuaranteeFile'] ) ) || ( isset( $aForm['iDeleteGuaranteeFile'] ) && isset( $aForm['sGuaranteeFile'] ) ) ) ){
		$oFile->deleteSelectedFiles( Array( $aForm['iProducer'] => 1 ), 50, 'iProducer', 'sGuaranteeFile' );
		$aForm['sGuaranteeFile'] = '';
	}
	if( isset( $_FILES['producerLogo']['name'] ) ){
		if( !empty( $_FILES['producerLogo']['name'] ) && $oDB->checkCorrectFile( $_FILES['producerLogo']['name'], 'gif|jpg|png|jpeg' ) ){
			$aForm['sLogoUrl'] = $oDB->uploadFile( Array( 'tmp_name' => $_FILES['producerLogo']['tmp_name'], 'name' => $_FILES['producerLogo']['name'] ), DIR_FILES );
			$oFile->generateThumbs( $aForm['sLogoUrl'] );
		}
	}
	
	if( !empty( $aForm['aDirFiles'] ) ){
		$aForm['sGuaranteeFile'] = $aForm['aDirFiles'][0];
	} else {
		if( isset( $_FILES['sGuaranteeFile']['name'] ) ){
				if( !empty( $_FILES['sGuaranteeFile']['name'] ) && $oDB->checkCorrectFile( $_FILES['sGuaranteeFile']['name'], 'pdf' ) ){
				$aForm['sGuaranteeFile'] = $oDB->uploadFile( Array( 'tmp_name' => $_FILES['sGuaranteeFile']['tmp_name'], 'name' => $_FILES['sGuaranteeFile']['name'] ), DIR_FILES );
			}
		}
	}
	
	$aForm['iProducer'] = $oDB->save( DB_PRODUCERS, $aForm, $sParam, 'sort' );
	
	$oDB->deleteInFile( DB_PRODUCERS_GUARANTEE, $aForm['iProducer'], 'iProducer' );
	if( isset( $aForm['aGuarantee'] ) && is_array( $aForm['aGuarantee'] ) ){
		#var_dump( $aForm['aGuarantee'] );
		foreach( $aForm['aGuarantee']['sGuarantee'] as $sKey => $oGuarantee )if( !empty( $oGuarantee ) ){
			$aTab = array( 'sGuarantee' => $oGuarantee, 'iPage' => $aForm['aGuarantee']['iPage'][ $sKey ], 'iProducer' => $aForm['iProducer'] );
			#var_dump( $aTab );
			$oDB->save( DB_PRODUCERS_GUARANTEE, $aTab );
		}
	}

	if( $aForm['bUpdateMargins'] == 1 ){
		$oDB->executeSQL( 'UPDATE `' . $oDB->throwTableName( DB_PRODUCTS ) .'` SET `fPrice` = ROUND( `fBuyPrice` ' . ( strpos( $aForm['sMargin'], '%' ) !== false ? '* ( 1 + '.str_replace( '%', '', $aForm['sMargin'] ).'/100 )' : '+ '.$aForm['sMargin'] ) . ', 2 ) WHERE `iProducer` = ' . $aForm['iProducer'] . ' AND ( `sMargin` IS NULL OR `sMargin` = \'\' ) AND `fBuyPrice` > 0' );
		$_SESSION['iUpdatedProducts'] = $oDB->affectedRows( );
		$oDB->executeSQL( 'UPDATE `' . $oDB->throwTableName( DB_PRODUCTS_VARIANTS ) .'` `pv` INNER JOIN `' . $oDB->throwTableName( DB_PRODUCTS ) .'` `p` USING( `iProduct` ) SET `pv`.`fPrice` = ROUND( `pv`.`fBuyPrice` ' . ( strpos( $aForm['sMargin'], '%' ) !== false ? '* ( 1 + '.str_replace( '%', '', $aForm['sMargin'] ).'/100 )' : '+ '.$aForm['sMargin'] ) . ', 2 ) WHERE `p`.`iProducer` = ' . $aForm['iProducer'] . ' AND ( `pv`.`sMargin` IS NULL OR `pv`.`sMargin` = \'\' ) AND ( `p`.`sMargin` IS NULL OR `p`.`sMargin` = \'\' ) AND `pv`.`fBuyPrice` > 0' );
		$_SESSION['iUpdatedVariants'] = $oDB->affectedRows( );
	}

	return $aForm['iProducer'];
}
function throwProducerBySubId( $iSubId ){
	$oDB = DataBase::getInstance( );
	$aData = $oDB->throwData( DB_PRODUCERS, $iSubId, 'iSubId' );
	if( isset( $aData ) ){
		return $aData;
	} else{
		return false;
	}
}
function throwProducerByName( $sName ){
	$oDB = DataBase::getInstance( );
	$aData = $oDB->throwData( DB_PRODUCERS, $sName, 'sName' );
	if( isset( $aData ) ){
		return $aData;
	} else{
		return false;
	}
}
?>