File: /home/cafsindia/crm_cafsindia_com/application/models/Signups.php
<?php
require_once("sms.php");
class Signups extends CI_Model
{
/*
Inserts Signup
*/
public function save_signup($email,$mobile){
if($mobile !== ""){
$query = $this->db->get_where('signup', array('mobile' => $mobile), 1);
}else
if($email !== ""){
$query = $this->db->get_where('signup', array('email' => $email), 1);
}
$sts = array();
if($query->num_rows() === 1){
$row = $query->row();
$status_from_db= $row->status;
if($status_from_db === "0"){
//New signup for status inactive
$otp = mt_rand(100000,999999);
$this->session->set_userdata('signup_eml', $email);
$this->session->set_userdata('signup_mob', $mobile);
$this->session->set_userdata('signup_otp', $otp);
if($mobile !== ""){
sms('ARIELS',$mobile,"Dear Customer,your OTP for registration with ArielPOS is ". $otp);
}else
if($email !== ""){
}
$sts['sts'] = "3";
$sts['msg'] = "New Mobile Number update";
}else{
// Already Register
$sts['sts'] = "2";
$sts['msg'] = "Sent to login";
}
}else{
//New signup
$otp = mt_rand(100000,999999);
$this->session->set_userdata('signup_eml', $email);
$this->session->set_userdata('signup_mob', $mobile);
$this->session->set_userdata('signup_otp', $otp);
$this->db->insert('signup', array('email' => $email, 'mobile' => $mobile));
if($mobile !== ""){
sms('ARIELS',$mobile,"Dear Customer,your OTP for registration with ArielPOS is ". $otp);
}else
if($email !== ""){
}
$sts['sts'] = "1";
$sts['msg'] = "New signup";
}
}
/*
Used to verify otp and save new customer
*/
public function save_customer($otp,$pwd){
$sts = array();
$signup_eml = $_SESSION['signup_eml'];
$signup_mob = $_SESSION['signup_mob'];
$signup_otp = $_SESSION['signup_otp'];
if($signup_eml !== ""){
$user_id = $signup_eml;
}else
if($signup_mob !== ""){
$user_id = $signup_mob;
}
if((int)$signup_otp !== (int)$otp){
$sts['sts'] = "0";
$sts['msg'] = "Please enter valid OTP";
}else{
$today = date("ym");
$query = $this->db->query("SELECT count(*) as count FROM ospos_employees where username like '%$today%'");
$row = $query->row_array();
$empCount = $row['count'];
if((int)$empCount === 0){
$empCount = 1;
}else{
$empCount = $empCount + 1;
}
$empCount = str_pad($empCount,4,"0",STR_PAD_LEFT);
//$user_id = "AP".date("ym").$empCount;
$shop_id = $this->UniqueCode("SP");
$pwd = md5($pwd);
$this->db->insert('people', array('phone_number' => $signup_mob, 'email' => $signup_eml));
$person_id = $this->db->insert_id();
$date = date("Y-m-d H:i:s");
$this->db->insert('employees', array('username' => $user_id, 'password' => $pwd, 'person_id' => $person_id , 'shop_id' => $shop_id));
if($signup_eml !== ""){
$this->db->where('email', $signup_eml);
$this->db->update('signup', array('status' => "1",'shop_id' => $shop_id,'person_id' => $person_id,'created_on' => $date));
}else
if($signup_mob !== ""){
$this->db->where('mobile', $signup_mob);
$this->db->update('signup', array('status' => "1",'shop_id' => $shop_id,'person_id' => $person_id,'created_on' => $date));
}
$grants_data = array("config","customers","employees","giftcards","items","items_stock","item_kits","messages","receivings","receivings_stock","reports","reports_categories","reports_customers","reports_discounts","reports_employees","reports_inventory","reports_items","reports_payments","reports_receivings","reports_sales","reports_suppliers","reports_taxes","sales","sales_stock","suppliers","reorder","hsn","reports_gsts","email");
foreach($grants_data as $permission_id){
$this->db->insert('grants', array('permission_id' => $permission_id, 'person_id' => $person_id));
}
$this->db->query("INSERT INTO ospos_app_config (ospos_app_config.key, ospos_app_config.value, ospos_app_config.shop_id) SELECT ospos_app_config.key, ospos_app_config.value, '$shop_id' FROM ospos_app_config WHERE ospos_app_config.shop_id = '0'");
if($mobile !== ""){
sms('ARIELS',$signup_mob,"Congratulations! your User id is ". $user_id);
}else
if($email !== ""){
}
$sts['sts'] = "1";
$sts['msg'] = "Created account";
}
return $sts;
}
public function UniqueCode($code="UN") {
$YearMap = array('2017'=>'A','2018'=>'B','2019'=>'C','2020'=>'D','2021'=>'E','2022'=>'F','2023'=>'G','2024'=>'H','2025'=>'I','2026'=>'J','2027'=>'K','2028'=>'L');
$MonthMap = array('1'=>'J','2'=>'K','3'=>'L','4'=>'M','5'=>'N','6'=>'O','7'=>'P','8'=>'Q','9'=>'R','10'=>'S','11'=>'T','12'=>'U');
$DayMap = array('1'=>'A','2'=>'B','3'=>'C','4'=>'D','5'=>'E','6'=>'F','7'=>'G','8'=>'H','9'=>'I','10'=>'J','11'=>'K','12'=>'L','13'=>'M','14'=>'N','15'=>'O','16'=>'P','17'=>'Q','18'=>'R','19'=>'S','20'=>'T','21'=>'U','22'=>'V','23'=>'W','24'=>'X','25'=>'Y','26'=>'Z','27'=>'1','28'=>'2','29'=>'3','30'=>'4','31'=>'5');
$HourMap = array('0'=>'A','1'=>'B','2'=>'C','3'=>'D','4'=>'E','5'=>'F','6'=>'G','7'=>'H','8'=>'I','9'=>'J','10'=>'K','11'=>'L','12'=>'M','13'=>'N','14'=>'O','15'=>'P','16'=>'Q','17'=>'R','18'=>'S','19'=>'T','20'=>'U','21'=>'V','22'=>'W','23'=>'X');
$loctime = time();
$unq4 = substr($loctime,-4,4);
$unq2 = mt_rand(10,99);
$xunq2 = mt_rand(10,99);
$year = $YearMap[ltrim(date('Y'), '0')];
$month = $MonthMap[ltrim(date('n'), '0')];
$day = $DayMap[ltrim(date('j'), '0')];
$hr = ltrim(date('H'), '0');
if($hr == "")
$hr = "0";
$hour = $HourMap[$hr];
$uniqueCode = $code. $year . $month . $day . $hour. $unq4 . $unq2 . $xunq2;
return $uniqueCode;
}
}
?>