<?php
error_reporting(E_ERROR | E_PARSE);
header('Content-Type:application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: X-Requested-With');
//include "include.php";
include "../core/dbInfo.php";
include_once "../core/dbFunctions.php";
include_once "../core/core_classes.php";
include_once "../core/get_classes.php";
include_once "../core/insert_classes.php";
include_once "../core/update_classes.php";
include_once "../core/errors.php";
include_once '../core/PHPMailerAutoload.php';

parameters_check();

$o = new stdClass();
$o1 = new stdClass();
$o2 = new stdClass();
$o3 = new stdClass();
$o4 = new stdClass();
$o5 = new stdClass();
$o6 = new stdClass();
$o7 = new stdClass();
$o8 = new stdClass();
$o9 = new stdClass();
$o10 = new stdClass();

$factory = new TypeFactory($dbName);
$insertor = new TypeInsertor($dbName);
$updater = new TypeUpdater($dbName);
//pt($_REQUEST);die;
  $o->user_id = 116;
if ($_REQUEST['email']=="") {
	$result['error'] = "1";
	$result['error_msg'] = "Please Enter Email";
	echo json_encode($result);die;	
}
if ($_REQUEST['mobile']=="") {
	$result['error'] = "1";
	$result['error_msg'] = "Please Enter mobile number";
	echo json_encode($result);die;	
}
if ($_REQUEST['name']=="") {
	$result['error'] = "1";
	$result['error_msg'] = "Please Enter name";
	echo json_encode($result);die;	
}
if ($_REQUEST['user_address']=="") {
	$result['error'] = "1";
	$result['error_msg'] = "Please Enter address";
	echo json_encode($result);die;	
}
if ($_REQUEST['company_name']=="") {
	$result['error'] = "1";
	$result['error_msg'] = "Please Enter shop name";
	echo json_encode($result);die;	
}


if ($_REQUEST) {
    $o = $factory->get_object($o->user_id, "users", "user_id");

            $o1->email = $_REQUEST['email'];
            $o1->mobile = $_REQUEST['mobile'];
            $o1->is_active = "1";
			$sql_chk_user = "Select user_id from users where (email = '" . $o1->email . "' or  mobile = '" . $o1->mobile . "') and user_id != '" . $o1->user_id . "'";
            $res_chk_user = getXbyY($sql_chk_user);
            $row_chk_user = count($res_chk_user);
			
			if ($row_chk_user > 0) {
                $result['error'] = "1";
                $result['error_msg'] = "Mobile/Email Already Exists";
            } else { 
                $o1->user_type = "Retailer";
                $o1->parent_id = $o->user_id;
                $o1->plan_id = $o->plan_id;
                $password = rand(100000, 999999);
                $o1->password = cpassword($password);
                $o1->last_login = todaysDate();
                $o1->pincode = "0";
                $o1->name = $_REQUEST['name'];
                $o1->mobile_1 = $_REQUEST['mobile'];
                $o1->dob = 0;
                $o1->otp = rand(1000, 9999);
                $o1->mobile_otp = rand(1000, 9999);
                $o1->email_otp = rand(1000, 9999);
                $o1->login_ip = $_SERVER['REMOTE_ADDR'];
                $o1->district = 0;
                $o1->state = 0;
                $o1->user_address = $_REQUEST['user_address'];
                $o1->gst_no = 0;
                $o1->pancard = 0;
                $o1->adhaar_card = 0;
                $o1->company_name = $_REQUEST['company_name'];
                $o1->company_type = 0;
                $o1->is_active = '1';
                $o1->gender = 'Male';
                    $o1->amount_balance = '0';
                    $o1->credit_amount = '0';
                    $o1->credit_limit = '0';
                    $o1->kyc_id=rand(5000, 9999);
                    $o1->uuid = '0';
                    $time = strtotime(todaysDate());
                    $final = date("Y-m-d", strtotime("+1 month", $time));
                    $o1->kyc_date = $final;
                    $o1->commission = '0';
                    $o1->route = $o->route;

                    $o1->force_logout = '0';
                    $o1->mobile_verified = 'Yes';
                    $o1->email_verified = 'Yes';
                    $o1->kyc_modal = 'No';
                    $o1->multi_api = 'No';
                    $o1->tds = $o->tds;
                    $o1->gst = $o->gst;
                    $o1->is_active = '1';
                    $o1->approved_by = $o->user_id;
                    $o1->user_name = create_username($o1->user_type);
                    $o1->created_at = todaysDate();
					
                    $o1->user_id = $insertor->insert_object($o1, "users");
                    if ($o1->user_type == 'Retailer') {
                            $o2->user_id = $o1->user_id;
                            $o3->user_id = $o1->user_id;
                            $sql_services = "Select service_id,service_name from services";
                            $res_services = getXbyY($sql_services);
                            $rows_services = count($res_services);
                            if ($rows_services > 0) {
                                for ($i = 0; $i < $rows_services; $i++) {
                                    $o2->service_id = $res_services[$i]['service_id'];
                                    $o2->service_name = $res_services[$i]['service_name'];
            
                                    if($res_services[$i]['service_name']=="Prepaid" || $res_services[$i]['service_name']=="Postpaid" || $res_services[$i]['service_name'] =="DTH" || $res_services[$i]['service_name'] =="Landline" || $res_services[$i]['service_name'] =="Electricity" || $res_services[$i]['service_name'] =="Insurance" || $res_services[$i]['service_name'] =="Water" || $res_services[$i]['service_name']=="Stv" ){
                                        $o2->status = 'Yes';
                                    }else{
                                     $o2->status = 'No';
                                 }
                                 $o2->is_active = '1';
                                 $o2->created_at = todaysDate();
                                 $o2->updated_at = todaysDate();
                                 $o2->user_service_id = $insertor->insert_object($o2, "user_services");
                             }
                         }
                         
                    }
                    $email_subject = $res_site[0]['site_name'] . " Account is active now.";
                    include "../mails/new_user.php";
                    sendmail($email_from, $o1->email, $email_subject, $email_message);

                    $message = "Congratulations! Your Account on  is active now. Username: " . $o1->mobile . " or " . $o1->email . " and password is: " . $password." and User Pin is:".$o1->kyc_id;
                    //pt($message);//die;
					sendsms_payzoom($o1->mobile, $message);
                    $result['error'] = "0";
                    $result['error_msg'] = "Register Successfully";
                }
            }else{
				$result['error'] = "1";
                $result['error_msg'] = "Invalid Request";
			}
        
echo json_encode($result);
?>