File: /var/www/grapossconnect/center-admin/agentFunctions.php
<?php
//wallet recharge success
function walletCreditSuccess($AGENT_ID, $txn_id, $paymentTxn_id, $paymentOrd_id, $amount, $wltamount, $txn_other_data, $pg)
{
//include '../wp-config.php';
global $wpdb;
$date = date('Y-m-d H:i:s');
$agentDetails = $wpdb->get_row("SELECT agent_code, mf_id, name, email, mobile, state_id FROM `grp_agents` where agent_id='$AGENT_ID'");
$agentFinDetails = $wpdb->get_row("SELECT gstin FROM `grp_agents_bank_details` where agent_id='$AGENT_ID'");
$agent_code = $agentDetails->agent_code;
$mf_id = $agentDetails->mf_id;
//$walletAmt = $wpdb->get_var("SELECT amount from grp_online_txn_temp where transaction_id='$txn_id'");
$walletAmt = $wltamount;
$checkTxnId = $wpdb->get_var("SELECT count(id) FROM grp_online_txn_main where transaction_id='$txn_id'");
if ($checkTxnId == 0 || $checkTxnId == '') {
$insertTxn = "INSERT into grp_online_txn_main set txn_type='Wallet', agent_id='$AGENT_ID', amount='$wltamount', net_amount='$amount', payment_gateway='$pg',transaction_id='$txn_id',payment_txn_id='$paymentTxn_id',payment_order_id='$paymentOrd_id', txn_status='S',txn_msg='$txn_other_data',txn_date='$date'";
if ($wpdb->query($insertTxn)) {
$insert_id = $wpdb->insert_id;
//update temptable
$updateTemp = $wpdb->query("UPDATE grp_online_txn_temp set txn_status='S', success_txn_id='$paymentTxn_id', txn_msg='$txn_other_data' where transaction_id='$txn_id'");
//update agent wallet
$checkTxnId1 = $wpdb->get_var("SELECT count(wallet_id) FROM `grp_agents_wallet` where agent_id='$AGENT_ID'");
if ($checkTxnId1 == 0 || $checkTxnId1 == '') {
$agentCode = $agent_code;
$newRecharged = $walletAmt;
$newBalance = $walletAmt;
$balance = '0';
$wpdb->query("INSERT INTO `grp_agents_wallet` SET agent_id='$AGENT_ID', agent_code='$agentCode', total_recharged='$newRecharged', balance_amount='$newBalance', last_recharge_date='$date', active='1', remarks='New Wallet', wallet_created_date='$date'");
} else {
$agWallet = $wpdb->get_row("SELECT agent_code, total_recharged, balance_amount FROM `grp_agents_wallet` where agent_id='$AGENT_ID'");
$prevBal = $agWallet->total_recharged;
$balance = $agWallet->balance_amount;
$agentCode = $agent_code;
$newRecharged = ($prevBal + $walletAmt);
$newBalance = ($balance + $walletAmt);
$updateWallet = $wpdb->query("UPDATE `grp_agents_wallet` set total_recharged='$newRecharged', balance_amount='$newBalance', last_recharge_date='$date' where agent_id='$AGENT_ID'");
}
//insert credit history
$insertCreditHis = "INSERT into grp_agents_credit_history set agent_id='$AGENT_ID', mf_id='$mf_id', agent_code='$agent_code', amount='$walletAmt', last_balance='$balance', payment_type='Online', txn_id='$txn_id', success_txn_id='$paymentTxn_id', reference_id='$insert_id', gateway_type='Razorpay', remarks='Wallet Recharge successful', gateway_response='$txn_other_data',credit_date='$date'";
$insertHistory = $wpdb->query($insertCreditHis);
$order_id = $wpdb->insert_id;
//insert wallet transaction
$insertWalletTxn = "INSERT into grp_agents_transaction set agent_id='$AGENT_ID', mf_id='$mf_id', agent_code='$agent_code', txn_type='cr', product_id='0', txn_amount='$walletAmt', order_id='$order_id', before_payment='$balance', after_payment='$newBalance', txn_date='$date'";
$insertWalletHistory = $wpdb->query($insertWalletTxn);
// $e_mob = '8700171280';
// include '/var/www/html/selfanalyse/wp-content/plugins/psychometrics/api/sendsmsapi.php';
// $mnt_message = "Thank you, Your recharge is successful";
// $encodedMessage = urlencode($mnt_message);
// $obj = new Sendsms();
// $var = $obj->sendMessage('ANLYSE', $e_mob, $encodedMessage);
# store invoice
invoice($order_id, $amount, $walletAmt, $txn_id, $paymentTxn_id, $AGENT_ID, $date);
/**sned mail */
//rechargeSuccess($email, $name, $amount,'success');
}
} else {
//nothing
}
}
//wallet recharge failed
function walletCreditFail($AGENT_ID, $txn_id, $amount, $txn_other_data, $pg)
{
//include '../wp-config.php';
global $wpdb;
$date = date('Y-m-d H:i:s');
$checkTxnId = $wpdb->get_var("SELECT count(id) FROM grp_online_txn_main where transaction_id='$txn_id'");
if ($checkTxnId == 0 || $checkTxnId == '') {
$insertTxn = "INSERT into grp_online_txn_main set txn_type='Wallet',agent_id='$AGENT_ID', amount='$amount', payment_gateway='$pg',transaction_id='$txn_id', txn_status='F',txn_msg='$txnMessage',txn_date='$date'";
if ($wpdb->query($insertTxn)) {
//update temptable
$updateTemp = $wpdb->query("UPDATE grp_online_txn_temp set txn_status='F', txn_msg='$txnMessage' where transaction_id='$txn_id'");
rechargeSuccess($email, $name, $amount, 'failed');
}
} else {
//nothing
}
}
// function rechargeSuccess($email, $name, $amount,$type)
// {
// include_once '../wp-content/plugins/miu-india/mails-sms/mail-functions.php';
// $subject = 'Recharge '.ucwords($type);
// $content = '<p>Dear ' . $name . '</p>';
// $content .= '<p>Your recharge of Rs.' . $amount . ' is '.$type.'</p>';
// $content .= '<p>Team MIU.</p>';
// $fromemail = "info@sarkaripariksha.com";
// $replyto = "info@sarkaripariksha.com";
// smtpMail($fromemail,$replyto,'Team MIU', $subject, $content, $email);
// }
# store invoice of transaction
function invoice($order_id, $txnamount, $walletAmt, $txn_id, $paymentTxn_id, $AGENT_ID, $date)
{
global $wpdb;
$curDateTime = date('Y-m-d H:i:s');
$GRP_AGENTS = 'grp_agents';
$GRP_STATES = 'grp_states';
$GRP_INVOICE = 'grp_gepl_invoice';
$GRP_AGENT_BANK_DATA = 'grp_agents_bank_details';
$agentDetails = $wpdb->get_row("SELECT a.agent_id, a.agent_code, a.name, a.email, a.mobile, a.state_id, a.state_code, a.address, s.state_name, b.gstin FROM $GRP_AGENTS as a LEFT JOIN $GRP_STATES as s on s.state_id=a.state_id LEFT JOIN $GRP_AGENT_BANK_DATA as b on b.agent_id=a.agent_id where a.agent_id='$AGENT_ID'");
$a_agent_id = $agentDetails->agent_id;
$a_agent_code = $agentDetails->agent_code;
$a_name = $agentDetails->name;
$a_email = $agentDetails->email;
$a_mobile = $agentDetails->mobile;
$a_address = $agentDetails->address;
$a_state_id = $agentDetails->state_id;
$a_state_code = $agentDetails->state_code;
$a_state_name = $agentDetails->state_name;
$a_gstin = $agentDetails->gstin;
//GEPL common invoice for all transaction
$from_domain = "https://grapossconnect.com";
$if_vendor = "GEPL";
$transaction_type = "GEPL-CENTER-WALLET-RECHARGE";
$payment_for = "Wallet Recharge";
$payment_type = "Online";
$payment_gateway_name = "Razorpay";
$ref_name1 = "center_id";
$ref_id1 = $AGENT_ID;
$ref_name2 = "order_id";
$ref_id2 = $order_id;
$ref_name3 = "center_code";
$ref_id3 = $a_agent_code;
$transaction_id2 = $txn_id;
$discount_amount = "";
if ($a_state_code == 'DL' || $a_state_name == 'Delhi') {
$amount = $walletAmt;
$total_amount = round(($amount / 118 * 100), 2);
$igst_amount = 0;
$cgst_amount = round(($total_amount * 9 / 100), 2);
$sgst_amount = round(($total_amount * 9 / 100), 2);
$grand_amount = round((($amount / 118 * 100) + ($total_amount * 9 / 100) + ($total_amount * 9 / 100)), 2);
} else {
$cgst_amount = 0;
$sgst_amount = 0;
$amount = $walletAmt;
$total_amount = round(($amount / 118 * 100), 2);
$igst_amount = round(($total_amount * 18 / 100), 2);
$grand_amount = round((($amount / 118 * 100) + ($total_amount * 18 / 100)), 2);
}
$insertInvoiceQuery = "INSERT INTO `grp_gepl_invoice` set `from_domain`='$from_domain', `if_vendor`='$if_vendor', `transaction_type`='$transaction_type', `payment_for`='$payment_for', `payment_type`='$payment_type', `payment_gateway_name`='$payment_gateway_name', `order_id`='$order_id', `product_mrp`='$walletAmt', `gst_rate`='18', `igst_amount`='$igst_amount', `cgst_amount`='$cgst_amount', `sgst_amount`='$sgst_amount', `taxable_amount`='$total_amount', `gepl_amount`='$walletAmt', `discount_amount`='$discount_amount', `net_amount`='$txnamount', `customer_name`='$a_name', `customer_address`='$a_address', `customer_mobile`='$a_mobile', `customer_email`='$a_email', `customer_gst_name`='', `customer_gstin`='$a_gstin', `customer_state_id`='$a_state_id', `customer_state_code`='$a_state_code', `customer_state_name`='$a_state_name', `transaction_id1`='$paymentTxn_id', `transaction_id2`='$transaction_id2', `transaction_date`='$date', `invoice_issue_date`='$curDateTime', `year_extn`='', `invoice_status`='1', `system_datetime`='$curDateTime', `cancelled_reason_if_any`='', `cancelled_datetime`='', `ref_name1`='$ref_name1', `ref_id1`='$ref_id1', `ref_name2`='$ref_name2', `ref_id2`='$ref_id2', `ref_name3`='$ref_name3', `ref_id3`='$ref_id3'";
if ($wpdb->query($insertInvoiceQuery)) {
#invoice stored
}
}