Monday 13 March 2017

Custom save function vTiger module

 
Generally vTiger provides save_module , save function for saving records.But let say you have a deposit module and you want custom function for saving records in vTiger (6.4). basically deposit module has relation with Account module. so in vTiger vtiger_crmentity table maintain every records or operation done and vtiger_crmentity_seq store max id or sequence number , so below are the functions which process save functionality by using custom queries for two modules.
 
function save_module_custom($module, $post_array) {
        return $this->insertIntoDepositTable('vtiger_deposit', $module, $post_array);
    }
function insertIntoDepositTable($table_name, $module, $post_array) {
        global $log, $adb;
        $log->info("in insertIntoDepositTable  " . $table_name . "    module is  " . $module);
        $payment_method = $post_array['payment_method'];
        $account_no = $post_array['account_no'];
        $amount = $post_array['amount'];
        $account_id = $post_array['account_id'];
        $order_id = $this->gen_uuid();
        $currency = 'USD';
        $createdtime = date('Y-m-d H:i:s');
        $assigned_user_id = getRecordOwnerId($post_array['customer_id']);
        $id = $this->getMaxId();
        $deposit_status = 'Pending';
        $comment = '';
        $sql = "INSERT INTO vtiger_deposit(depositid, currency, payment_method, amount, account_id, 
        deposit_status ,order_id,comment) ". "VALUES ($id,'" . $currency . "',
 '" . $payment_method . "','" . $amount . "','" . $account_id . "', 
 '" . $deposit_status . "',
 '" . $order_id . "','" . $comment . "')";
        $adb->pquery($sql, array());
        $sql = "INSERT INTO vtiger_depositcf(depositid) ". "VALUES ($id)";
        $adb->pquery($sql, array());
        $crmid = $this->custom_save_related_module($id, $account_no, $assigned_user_id);
        return array('depositid' => $id, 'crmid' => $crmid);
    }
function custom_save_related_module($relcrmid, $account_no, $assigned_user_id) {
        global $log, $adb;
        $log->info("in custom_save_related_module  vtiger_crmentity,vtiger_crmentity_seq");
        //First get seq no max
        $getCrmSqNo = $adb->pquery("SELECT id FROM vtiger_crmentity_seq", array());
        $norows = $adb->num_rows($res);
        $seq_id = $adb->query_result($getCrmSqNo, 0, 'id');
        $seq_id = $seq_id + 1;
        $adb->pquery("Update vtiger_crmentity_seq set id='$seq_id'", array());
        $liveaccountinfo = $adb->pquery("SELECT liveaccountid FROM vtiger_liveaccount 
 where account_no=?", array($mt4_account_no));
        $norows = $adb->num_rows($liveaccountinfo);
        $liveaccountid = $adb->query_result($liveaccountinfo, 0, 'liveaccountid');
        $module = 'Deposit';
        $with_module = 'Account';
        $adb->pquery("INSERT INTO vtiger_crmentityrel(crmid, module, relcrmid, relmodule) 
 VALUES(?,?,?,?)", Array($liveaccountid, $module, $relcrmid, $with_module));
        $adb->pquery("INSERT INTO vtiger_crmentity(crmid , setype, createdtime, modifiedtime,label 
 , smcreatorid , smownerid) 
 VALUES(?,?,?,?,?,?,?)", Array($seq_id, $module, date('Y-m-d H:i:s'), date('Y-m-d H:i:s'), 
 $liveaccountid, 
 $assigned_user_id, $assigned_user_id));
        return $seq_id;
    }
function gen_uuid() {
        global $adb;
        $order_id = NULL;
        $res = $adb->pquery("SELECT uuid() As order_id", array());
        $norows = $adb->num_rows($res);
        if ($norows > 0) {
            $order_id = $adb->query_result($res, 0, 'order_id');
            $order_id = str_replace('-', '', $order_id);
        }
        return $order_id;
    }

Below function get max id from your custom table because we can put this id to reference table for maintaining relationship.

function getMaxId() {
        global $adb;
        $id = NULL;
        $res = $adb->pquery("SELECT MAX(id) As depositid FROM vtiger_crmentity", array());
        $norows = $adb->num_rows($res);
        if ($norows > 0) {
            $id = $adb->query_result($res, 0, 'depositid');
            $id = $id + 1;
        }
        return $id;
    }