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.
 
  1. function save_module_custom($module, $post_array) {
  2. return $this->insertIntoDepositTable('vtiger_deposit', $module, $post_array);
  3. }
  1. function insertIntoDepositTable($table_name, $module, $post_array) {
  2. global $log, $adb;
  3. $log->info("in insertIntoDepositTable " . $table_name . " module is " . $module);
  4. $payment_method = $post_array['payment_method'];
  5. $account_no = $post_array['account_no'];
  6. $amount = $post_array['amount'];
  7. $account_id = $post_array['account_id'];
  8. $order_id = $this->gen_uuid();
  9. $currency = 'USD';
  10. $createdtime = date('Y-m-d H:i:s');
  11. $assigned_user_id = getRecordOwnerId($post_array['customer_id']);
  12. $id = $this->getMaxId();
  13. $deposit_status = 'Pending';
  14. $comment = '';
  15. $sql = "INSERT INTO vtiger_deposit(depositid, currency, payment_method, amount, account_id,
  16. deposit_status ,order_id,comment) ". "VALUES ($id,'" . $currency . "',
  17. '" . $payment_method . "','" . $amount . "','" . $account_id . "',
  18. '" . $deposit_status . "',
  19. '" . $order_id . "','" . $comment . "')";
  20. $adb->pquery($sql, array());
  21. $sql = "INSERT INTO vtiger_depositcf(depositid) ". "VALUES ($id)";
  22. $adb->pquery($sql, array());
  23. $crmid = $this->custom_save_related_module($id, $account_no, $assigned_user_id);
  24. return array('depositid' => $id, 'crmid' => $crmid);
  25. }
  1. function custom_save_related_module($relcrmid, $account_no, $assigned_user_id) {
  2. global $log, $adb;
  3. $log->info("in custom_save_related_module vtiger_crmentity,vtiger_crmentity_seq");
  4. //First get seq no max
  5. $getCrmSqNo = $adb->pquery("SELECT id FROM vtiger_crmentity_seq", array());
  6. $norows = $adb->num_rows($res);
  7. $seq_id = $adb->query_result($getCrmSqNo, 0, 'id');
  8. $seq_id = $seq_id + 1;
  9. $adb->pquery("Update vtiger_crmentity_seq set id='$seq_id'", array());
  10. $liveaccountinfo = $adb->pquery("SELECT liveaccountid FROM vtiger_liveaccount
  11. where account_no=?", array($mt4_account_no));
  12. $norows = $adb->num_rows($liveaccountinfo);
  13. $liveaccountid = $adb->query_result($liveaccountinfo, 0, 'liveaccountid');
  14. $module = 'Deposit';
  15. $with_module = 'Account';
  16. $adb->pquery("INSERT INTO vtiger_crmentityrel(crmid, module, relcrmid, relmodule)
  17. VALUES(?,?,?,?)", Array($liveaccountid, $module, $relcrmid, $with_module));
  18. $adb->pquery("INSERT INTO vtiger_crmentity(crmid , setype, createdtime, modifiedtime,label
  19. , smcreatorid , smownerid)
  20. VALUES(?,?,?,?,?,?,?)", Array($seq_id, $module, date('Y-m-d H:i:s'), date('Y-m-d H:i:s'),
  21. $liveaccountid,
  22. $assigned_user_id, $assigned_user_id));
  23. return $seq_id;
  24. }
  1. function gen_uuid() {
  2. global $adb;
  3. $order_id = NULL;
  4. $res = $adb->pquery("SELECT uuid() As order_id", array());
  5. $norows = $adb->num_rows($res);
  6. if ($norows > 0) {
  7. $order_id = $adb->query_result($res, 0, 'order_id');
  8. $order_id = str_replace('-', '', $order_id);
  9. }
  10. return $order_id;
  11. }

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

  1. function getMaxId() {
  2. global $adb;
  3. $id = NULL;
  4. $res = $adb->pquery("SELECT MAX(id) As depositid FROM vtiger_crmentity", array());
  5. $norows = $adb->num_rows($res);
  6. if ($norows > 0) {
  7. $id = $adb->query_result($res, 0, 'depositid');
  8. $id = $id + 1;
  9. }
  10. return $id;
  11. }