<?php

use WHMCS\Session;
use Carbon\Carbon;
use WHMCS\Database\Capsule;

$hook = array(
    'hook' => 'DailyCronJob',
    'function' => 'suiten_AccountAnniversary'
);

if (!function_exists('suiten_AccountAnniversary')) {

	function suiten_AccountAnniversary($DailyObject) {
		
		$global_module_name = 'SUITEN Whatsapp API';

		global $suiten_nomorwhatsapp;
		global $suiten_Whatsapp_Message, $sLogo;
		global $KoreksiNomor;
		global $client_id, $client_info;

		global $templatecontent, $templateenglish, $templatestatus;
		$templatestatus = "0";

		foreach (WHMCS\Database\Capsule::table('s10wa_templates')->WHERE('s10wa_templatetitle', 'Registration Anniversary')->get() as $wamodule_msg_record) {
			$templatecontent = $wamodule_msg_record->s10wa_templatecontent;
			$templateenglish = $wamodule_msg_record->s10wa_templateenglish;
			$templatestatus = $wamodule_msg_record->s10wa_templatestatus;		
		}
		// LogActivity("S10WA Anniversary - ".$templatestatus);  
		
		if ($templatestatus == "0") {
			return $hook;
		}

		foreach (WHMCS\Database\Capsule::table('s10wa_config')->WHERE('suiten_Nama_Modul', $global_module_name)->get() as $modulwa_config) {
		    $gwhost = $modulwa_config->gwhost;
			$whatsapp_source = $modulwa_config->suiten_field_nomorwa;
			$whatsapp_admin = $modulwa_config->suiten_adminwa;
			$whatsapp_metode_kirim = $modulwa_config->suiten_sending_method;
			$whatsapp_Signature_message = $modulwa_config->suiten_signature;
			}
			$tahunskrg = intval(date('Y'));
		// LogActivity("S10WA Anniversary - Scan started");  

		foreach (WHMCS\Database\Capsule::table('tblclients')->where('status','=','Active')->whereDay('datecreated', '=', date('d'))->whereMonth('datecreated', '=', date('m'))->get() as $CustomerInfo) {
			$client_id = $CustomerInfo->id;
			$firstname = rtrim($CustomerInfo->firstname);
			$lastname = rtrim($CustomerInfo->lastname);
			$email = $CustomerInfo->email;
			$phonenumber = $CustomerInfo->phonenumber;
			$alamat1 = $CustomerInfo->address1;
			$alamat2 = $CustomerInfo->address2;
			$kota = $CustomerInfo->city;
			$provinsi = $CustomerInfo->state;
			$kodepos = $CustomerInfo->postcode;
			$negara = $CustomerInfo->country;
			$clientstatus = $CustomerInfo->status;
			$datecreated = date('d M Y',strtotime($CustomerInfo->datecreated));
			$adatecreated = explode('-', $CustomerInfo->datecreated);
			// LogActivity($datecreated);
	
			$client_info = $firstname . ' ' . $lastname . ' (' . $email . ')';
			$bedatahun = $tahunskrg - intval($adatecreated[0]);
			// LogActivity("S10WA Anniversary - User ID: ".$client_id. " ". $client_info. " from ".$adatecreated[0]);

			$suiten_nomorwhatsapp = '';
			if($whatsapp_metode_kirim == 'Custom') {
				$suiten_nomorwhatsapp =  "";
				foreach (Capsule::table('tblcustomfieldsvalues')->where([['fieldid','=',$whatsapp_source],['relid','=',$client_id],])->get() as $datarecord) {
					$suiten_nomorwhatsapp = $datarecord->value;
				}
			} else {
				$suiten_nomorwhatsapp = $phonenumber;
			}

			$KoreksiNomor = suiten_koreksinomor($negara,$suiten_nomorwhatsapp);	
			$body_message = suiten_pilihbahasatemplate($negara, $KoreksiNomor, $templatecontent, $templateenglish);
			$status_message = $s10wa_templatestatus;

			if(($suiten_nomorwhatsapp != "") && ($suiten_nomorwhatsapp != "0") && (strlen($suiten_nomorwhatsapp) > 8) && ($client_id != 0) && ($client_id != "") && ($bedatahun > 0)) {

				$sLogo = "";
				if (strpos(strtolower($body_message),"{logo}") !== false) {
					$body_message = str_ireplace(
						"{logo}", 
						"", 
						$body_message
					);
					$sLogo = "logo";
				}

				// Handles the strings for the message template
				$s10tag_key = array(
					"{S10WA_NamaClient}",
					"{S10WA_FirstName}",
					"{S10WA_LastName}",
					"{suiten_signature}",
					"{S10WA_datecreated}",
					"{S10WA_years}",
					"{S10WA_TimeStamp}"
				);
				// $adatecreated[2] . '/' . $adatecreated[1] . '/' . $adatecreated[0],

				$s10tag_key_new = array(
					trim($firstname) . ' ' . trim($lastname),
					$firstname,
					$lastname,
					$whatsapp_Signature_message,
					$datecreated,
					$bedatahun,
					timeStampWhatsapp()
				);

				$suiten_Whatsapp_Message = str_ireplace(
					$s10tag_key, 
					$s10tag_key_new, 
					$body_message
				);

				try {

					Capsule::connection()->transaction(
						function ($connectionManager)
							{

							global $suiten_nomorwhatsapp;
							global $suiten_Whatsapp_Message, $sLogo;
							global $KoreksiNomor;
							global $client_id, $client_info;

							$connectionManager->table('s10wa_sendmsg')->insert(

								[
									's10wa_date' => timeStampWhatsapp(),
									's10wa_wanumber' => $KoreksiNomor,
									's10wa_interval' => (int)4,
									's10wa_priority' => (int)4,
									's10wa_message' => $suiten_Whatsapp_Message,
									's10wa_msgtype' => (int)1,
									's10wa_attachment' => $sLogo,
									's10wa_clientid' => $client_id,
									's10wa_clientinfo' => $client_info,
									's10wa_status' => (int)0
								]

							);

							logactivity( 'SUITEN_WAMLog: ' . $result . ' Account Anniversary WA#'.$KoreksiNomor. ( $sLogo == "" ? "" : " with ". $sLogo ), $client_id);
							if (function_exists('suiten_SyncDB')) 
									suiten_SyncDB($SuitenObject);

						}
					);			
				} catch (\Exception $e) {

					$result = 'MySQL Error: ' . $e;
					exit();
				}
			}
		}
		
		//LogActivity("S10WA Anniversary - Scan completed");    

		return $hook;

	}
}

return $hook;