<?php
	require_once( $GLOBALS["site_root"]."/includes/models/TemplateMail.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Constants.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/User.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Member.inc.php" );

	class CouponModel {

		public function getCouponTypeOptions( $choose = true ) {
			$retval = array();
			if ($choose) $retval[""]=__("Choose");
			$ops = array( "freedays" );
			foreach ($ops as $op) $retval[$op]=__("coupons_".$op);
			return $retval;
		}

		protected function prepareCouponCampaign( $p ) {
			$ins = array();
			$ins["cc_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["cc_name"] ) );
			$ins["cc_coupon_type"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["cc_coupon_type"] ) );
			$ins["cc_avail_from"]=(isset( $p["cc_avail_from"] ) && strlen( $p["cc_avail_from"] )) ?
				sprintf( "'%s'" , FNTH_db_addslashes( $p["cc_avail_from"] ) ) : "null";
			$ins["cc_avail_until"]=(isset( $p["cc_avail_until"] ) && strlen( $p["cc_avail_until"] )) ?
				sprintf( "'%s'" , FNTH_db_addslashes( $p["cc_avail_until"] ) ) : "null";
			$ins["cc_freedays"]=(isset( $p["cc_freedays"] )) ? sprintf( "%d" , $p["cc_freedays"] ) : 0;
			return $ins;
		}

		public function addCouponCampaign( $p ) {
			$ins = $this->prepareCouponCampaign( $p );
			$q = FNTH_db_generate_insert_query( "coupon_campaigns" , $ins );
			FNTH_db_free_result( FNTH_db_query( $q ) );
			return FNTH_db_get_field( "select currval('coupon_campaigns_coupon_campaign_id_seq') as num" , "num" );
		}

		public function updateCouponCampaign( $cc_id , $p ) {
			$ins = $this->prepareCouponCampaign( $p );
			$q=FNTH_db_generate_update_query("coupon_campaigns",$ins,sprintf("coupon_campaign_id=%d",$cc_id));
			FNTH_db_free_result( FNTH_db_query( $q ) );
			return $cc_id;
		}

		public function getCouponCampaignFromId( $cc_id ) {
			return FNTH_db_get_row(sprintf( "select * from coupon_campaigns where coupon_campaign_id=%d",$cc_id ));
		}

		public function isCouponTicketExists( $str ) {
			$query = sprintf( "select count(*) as num from coupon_tickets where coupon_code='%s'" , FNTH_db_addslashes( $str ) );
			return (FNTH_db_get_field( $query , "num" )>0);
		}

		public function isCouponTicketUsed( $str ) {
			$query = sprintf( "select count(*) as num from coupon_tickets where coupon_code='%s' and user_id is not null" , FNTH_db_addslashes( $str ) );
			return (FNTH_db_get_field( $query , "num" )>0);
		}

		public function getCouponCampaignIdFromTicket( $str ) {
			$query = sprintf( "select coupon_campaign_id from coupon_tickets where coupon_code='%s'" , FNTH_db_addslashes( $str ) );
			return FNTH_db_get_field( $query , "coupon_campaign_id" );
		}

		public function activateCouponToUser( $user_id , $str ) {
			$cc_id = $this->getCouponCampaignIdFromTicket( $str );
			$cc = $this->getCouponCampaignFromId( $cc_id );
			$query = sprintf( "update coupon_tickets set user_id=%d,used_at=now() where coupon_code='%s'",
				$user_id,
				FNTH_db_addslashes( $str ) );
			FNTH_db_free_result( FNTH_db_query( $query ) );
			switch ($cc["cc_coupon_type"]) {
			case "freedays":
				// prolong
				$mm = new MemberModel();
				$member = $mm->getMemberFromUserId( $user_id );
				if (!$member) return;
				$mm->prolongMember( $member["member_id"] , $cc["cc_freedays"] , PM_COUPON );

				// log message
				$ulm = new UserLogModel();
				$ulm->logMemberActivity( USER_LOG_MONETARY | USER_MONETARY_COUPON , sprintf( "kupont váltott be (%d nap)" , $cc["cc_freedays"] ) );

				$um = new UserModel();
				$user = $um->getUserFromUserId( $member["user_id"] );
			
				// 1. user
				$mail = new TemplateMailModel( "sys_coupon_accepted" );
				$cm = new ConstantsModel( $mail );
				$cm->addSystemParametersToTemplate();
				$cm->addUserConstantsToTemplate( $user_id );
				$cm->addMemberConstantsToTemplate( $member["member_id"] );
				$mail->sendMail( $user["email"] );
				
				// 2. admin
				$mail = new TemplateMailModel( "sys_admin_coupon_accepted" );
				$cm = new ConstantsModel( $mail );
				$cm->addSystemParametersToTemplate();
				$cm->addUserConstantsToTemplate( $user_id );
				$cm->addMemberConstantsToTemplate( $member["member_id"] );
				$mail->sendMail( $GLOBALS["fo3_admin_email"] );
			}
		}

		public function isUserUsedCouponTicketOnCampaign( $user_id , $coupon_campaign_id ) {
			$query = sprintf( "select count(*) as num from coupon_tickets where coupon_campaign_id=%d and user_id=%d",
				$coupon_campaign_id , $user_id );
			return (FNTH_db_get_field( $query , "num" )>0);
		}

		protected function _genTicket() {
			return strtoupper( substr( md5( uniqid( "fordit" ) ) , 0 ,6 ) );
		}

		public function generateNewCouponTicket() {
			$str = $this->_genTicket();
			while ($this->isCouponTicketExists( $str )) $str = $this->_genTicket();
			return $str;
		}

		protected function addCouponToCampaign( $cc_id , $ticket ) {
			$ins = array();
			$ins["coupon_campaign_id"]=sprintf( "%d" , $cc_id );
			$ins["coupon_code"]=sprintf( "'%s'" , FNTH_db_addslashes( $ticket ) );
			FNTH_db_free_result( FNTH_db_query( FNTH_db_generate_insert_query( "coupon_tickets" , $ins ) ) );
		}

		public function addCouponsToCampaign( $cc_id , $number_of_coupons = 0 ) {
			if (!$number_of_coupons) return;
			for ($i=0;$i<$number_of_coupons;$i++) {
				$newticket = $this->generateNewCouponTicket();
				$this->addCouponToCampaign( $cc_id , $newticket );
			}
			$query = sprintf( "update coupon_campaigns set cc_number_of_coupons=cc_number_of_coupons+%d where coupon_campaign_id=%d" , $number_of_coupons , $cc_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );
		}

		public function getCouponCodesForCampaign( $cc_id ) {
			$query = sprintf( "select coupon_code from coupon_tickets where coupon_campaign_id=%d and used_at is null", $cc_id );
			return FNTH_db_get_column( $query , "coupon_code" );
		}

	}

?>
