Topic: Cron Job Module

Hi,

I have created cron job module which will automatically update exchange rates and revalue foreign accounts on daily basis. It works when I am logged in but not when I am off or on cron request. I have created new user ex: cron, and tried to process login process with $_SESSION['wa_current_user']->login function without any luck.

Can someone instruct me how can I write authorization code better without use of password.


Regards

Re: Cron Job Module

Make sure that the cron script has the necessary session values or hard code them into it and keep it beyond your webroot and an appropriately set $page_security and $path_to_root.

3 (edited by trecords 10/30/2016 03:37:33 pm)

Re: Cron Job Module

Thanks for hint, these variables are in hooks.php so I have included it inside hooks.php within Class as constructor and it worked smile sample code is below:

    public function __construct() {
        global $_SESSION;
        // if _CJ is defined, login user non interactively
        if (@$cron_request) {
            $_SESSION['wa_current_user']->login($comp, $usr, $pwd);
        }
    }

$cron_request is variable within cron.php file which is server cron accesses.

Re: Cron Job Module

Thanks for the feedback.

Wiki-ed it.

Re: Cron Job Module

I need more help on this.

I create a module with hooks.php having the above function _construct()

I have one more file in module cron.php that contains the code to automatically fetch the exchange rate and update the database of the logged in Company if accessed directly.

Now when this cron.php is called through cron job then how this function in hooks.php creates a session with the defined user login and password. Credentials are hardcoded in hooks.php

Please give some guidelines

www.boxygen.pk

Re: Cron Job Module

construct should be preceded by 2 underscores(_).

7 (edited by Yan Shuang 06/03/2019 08:37:23 am)

Re: Cron Job Module

Hi, I have create a link to trigger the automatically update exchange rates but i have no idea on how to set as cron job. Everytime i try to access the link from outside, it will bring me back to the login page.
Please provide detail guide is possible. Cuz i am total lost here!

Re: Cron Job Module

For such kind of non authorised access you can keep session or for auto exchange rate updates. You can write a custom PHP program to update it on cron job link. When you trigger it with cron job to add. If you have more than one company , use config db file and get each company credentials and update it. Don't go with FA session and login for cron jobs

Subscription service based on FA
HRM CRM POS batch Themes

Re: Cron Job Module

Based on this post and the wiki (https://frontaccounting.com/fawiki/index.php?n=Devel.CronJobSample), I have written a complete script for updating the exchange rates via cron. Be sure to change the 'user' & 'password' at the end of the script to a valid account that has FA security to access the Exchange Rates function. Creating a cron entry should be trivial.

The setup in the first part of the script would be a good template for other batch tasks, though it is not possible to call functions that are expecting a display to be present, which does limit what processes can be performed via cron.

Hope this helps

xr_load.php (assuming that this is in FA root directory)

<?php
/**********************************************************************
*
* Load current day Exchange Rates from external provider using cron
*
* run using:
* cd <fa root dir>
* php xr_load.php
*
***********************************************************************/

// set environment
$path_to_root=".";
$page_security = 'SA_EXCHANGERATE';


// configure fake server environment for cron
$_SERVER = array();
$_SERVER['REMOTE_ADDR']='batch';
$_SERVER['HTTP_USER_AGENT']='cron';
$_SERVER['REQUEST_URI']='';
$_SERVER['HTTPS']='';
define('FA_LOGOUT_PHP_FILE', 'cronjob');
include_once($path_to_root . "/includes/session.inc");
install_hooks();


// load rates function
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/banking.inc");

/**
*
* Load Exchange Rates from external provider
*
* @param {object} $company The FA Company to by loaded
* @param {object} $usr     FA User
* @param {object} $pwd     FA Password
*
* @return
*
*/
function xr_load($company, $usr, $pwd)
{
    global $_SESSION;

    $_SESSION['wa_current_user']->login($company,$usr,$pwd);

    $date_ = Today();

    foreach (get_currencies() as $curr) {
        $curr_abrev = $curr['curr_abrev'];

        if (!is_company_currency($curr_abrev)) {
            $BuyRate = maxprec_format(retrieve_exrate($curr_abrev, $date_));

            if (get_date_exchange_rate($curr_abrev, $date_) == 0)
            {
                add_exchange_rate($curr_abrev, $date_, $BuyRate, $BuyRate);
            } else {
                update_exchange_rate($curr_abrev, $date_, $BuyRate, $BuyRate);
            }
        }
    }
}

// load rates
xr_load(0,'user',password');

Re: Cron Job Module

@PaulShipley ,  it's a great work dude. We can make it as module and available in extension repository.  The needy people will use it.

Subscription service based on FA
HRM CRM POS batch Themes

Re: Cron Job Module

@kvvaradha, thanks, you're welcome.

It would be possible to wrap a UI around this to configure and activate the cron job, and then package that as a module

Re: Cron Job Module

@joe: There must be a better way to do this by calling it based on last timestamp of call taken from some log file in the index.php file itself.

Post's attachments

xr_load.zip 845 b, 9 downloads since 2021-03-08 

You don't have the permssions to download the attachments of this post.