200 lines
7.0 KiB
C++
200 lines
7.0 KiB
C++
// Site Crons Management Tools
|
|
#include "clog.h"
|
|
#include "cgi.h"
|
|
#include "input.h"
|
|
#include "wrenchboard_api.h"
|
|
#include "site_crons.h"
|
|
#include "email.h"
|
|
#include "safestring.h"
|
|
#include <string>
|
|
#include "pgsql.h"
|
|
#include "pgsql_wrapper.h"
|
|
#include "cfg.h"
|
|
#include <curl/curl.h>
|
|
#include "jobs.h"
|
|
|
|
long ReviewReminder(CVars in, CVars &out);
|
|
long OfferPending(CVars in, CVars &out);
|
|
long InterestPending(CVars in, CVars &out);
|
|
long PaymentPending(CVars in, CVars &out);
|
|
long DeadlineLineApprachClient(CVars in, CVars &out);
|
|
long SignUpPendingReminder(CVars in, CVars &out);
|
|
long SignUpStats(CVars in, CVars &out);
|
|
long PastDueWarings(CVars in, CVars &out);
|
|
long PastDueOffersRefund(CVars in, CVars &out);
|
|
|
|
long sitecrons_calls(CVars in, CVars &out) {
|
|
logfmt(logINFO, "sitecrons_calls()");
|
|
//out["result"] = "YES I GET TO BACK END";
|
|
long call_action = REQ_LONG(in, "call_action", 0, -1);
|
|
CVars x;
|
|
long ret = 0;
|
|
const char * loc = getenv("REMOTE_ADDR");
|
|
|
|
switch (call_action) {
|
|
case WRB_CRONJOB_JOBDUE_REMINDER:
|
|
return DeadlineLineApprachClient(in, out);
|
|
break;
|
|
|
|
case WRB_CRONJOB_JOBDUE_PAYMENTS:
|
|
return PaymentPending(in, out);
|
|
break;
|
|
|
|
case WRB_CRONJOB_SIGNUP_ALERT:
|
|
return SignUpStats(in, out);
|
|
break;
|
|
|
|
case WRB_CRONJOB_PASTDUE_ALERT:
|
|
return PastDueWarings(in, out);
|
|
break;
|
|
|
|
case WRB_CRONJOB_OFFER_REFUND_ALERT:
|
|
return PastDueOffersRefund(in, out);
|
|
break;
|
|
|
|
}
|
|
|
|
logfmt(logINFO, "/sitecrons_calls()");
|
|
return ret;
|
|
}
|
|
|
|
long ReviewReminder(CVars in, CVars &out) {
|
|
|
|
|
|
|
|
}
|
|
|
|
long OfferPending(CVars in, CVars &out) {
|
|
|
|
|
|
|
|
}
|
|
|
|
long InterestPending(CVars in, CVars &out) {
|
|
|
|
|
|
|
|
}
|
|
|
|
long PastDueOffersRefund(CVars in, CVars &out) {
|
|
logfmt(logINFO, "ENTER PastDueOffersRefund()");
|
|
long contract_id = 0;
|
|
const PGresult *res = pgsql_query("SELECT j.* "
|
|
" FROM members_jobs_offer j "
|
|
" LEFT JOIN members m1 ON m1.id=j.member_id "
|
|
" LEFT JOIN members m2 ON m2.id=j.client_id "
|
|
" WHERE j.expire < now() "
|
|
" AND j.status = 1 AND j.auto_close IS NULL "
|
|
" AND j.payment_id IS NOT NULL LIMIT 1");
|
|
if (res != NULL && pgsql_num_rows(res) > 0) {
|
|
for (int i = 0, n = pgsql_num_rows(res); i < n; i++) {
|
|
map<const char*, const char*>f = pgsql_fetch_assoc(res, i);
|
|
if (f.empty()) continue;
|
|
CVars rec;
|
|
map_to_cvars(f, rec);
|
|
rec["offer_result"] = OFFER_EXPIRE;
|
|
rec["offer_result"].set_valid( true );
|
|
|
|
WrenchConcludeJobsOffer(rec, out);
|
|
pgsql_exec("UPDATE members_jobs_offer SET auto_close=now() "\
|
|
"WHERE offer_code='%s' ", rec["offer_code"].c_str());
|
|
|
|
}
|
|
}
|
|
logfmt(logINFO, "/PastDueOffersRefund()");
|
|
return 0;
|
|
}
|
|
|
|
long PastDueWarings(CVars in, CVars &out) {
|
|
logfmt(logINFO, "ENTER PastDueWarings()");
|
|
long contract_id = 0;
|
|
const PGresult *res = pgsql_query("SELECT mc.id AS contract_id, mc.member_id,mc.auto_remind "
|
|
" FROM members_jobs_contract mc "
|
|
" LEFT JOIN members m ON m.id=mc.client_id "
|
|
" WHERE mc.status IN (1,2) AND mc.auto_remind < 3 "
|
|
" AND delivery_date < now() GROUP BY mc.member_id ,mc.auto_remind, contract_id ORDER BY mc.auto_remind DESC");
|
|
if (res != NULL && pgsql_num_rows(res) > 0) {
|
|
for (int i = 0, n = pgsql_num_rows(res); i < n; i++) {
|
|
map<const char*, const char*>f = pgsql_fetch_assoc(res, i);
|
|
if (f.empty()) continue;
|
|
CVars rec;
|
|
map_to_cvars(f, rec);
|
|
// note we send once in the loop
|
|
// we need the loop to mar items but wanted to send the email only once
|
|
if ( contract_id != rec["contract_id"].Long() ){
|
|
contract_id = rec["contract_id"].Long();
|
|
cron_email(WRB_CRONJOB_PASTDUE_ALERT, rec, out); // send the cron email
|
|
}
|
|
pgsql_exec("UPDATE members_jobs_contract SET auto_remind=auto_remind + 1 "\
|
|
"WHERE member_id=%lu AND id = %lu ", rec["member_id"].Long(),rec["contract_id"].Long());
|
|
|
|
}
|
|
}
|
|
logfmt(logINFO, "/PastDueWarings()");
|
|
return 0;
|
|
}
|
|
|
|
long SignUpStats(CVars in, CVars &out) {
|
|
logfmt(logINFO, "SignUpStats()");
|
|
|
|
load_db_record(out, "SELECT count(id) AS today_signup FROM members WHERE added::date = now()::date");
|
|
load_db_record(out, "SELECT count(id) AS today_pending_signup FROM members_pending WHERE added::date = now()::date");
|
|
|
|
load_db_record(out, "SELECT TO_CHAR(NOW() :: DATE, 'Day Mon dd, yyyy') AS report_date");
|
|
|
|
in = out;
|
|
|
|
alert_email(WRB_CRONJOB_SIGNUP_ALERT, in, out);
|
|
logfmt(logINFO, "/SignUpStats()");
|
|
return 0;
|
|
|
|
}
|
|
|
|
long PaymentPending(CVars in, CVars &out) {
|
|
logfmt(logINFO, "PaymentPending()");
|
|
const PGresult *res = pgsql_query("SELECT count(id) AS preview_count,member_id FROM members_jobs_contract "\
|
|
" WHERE status = 4 AND pay_remind IS NULL AND delivery_date < now() +'-1 day(s)' "\
|
|
" GROUP BY member_id LIMIT 1");
|
|
if (res != NULL && pgsql_num_rows(res) > 0) {
|
|
for (int i = 0, n = pgsql_num_rows(res); i < n; i++) {
|
|
map<const char*, const char*>f = pgsql_fetch_assoc(res, i);
|
|
if (f.empty()) continue;
|
|
CVars rec;
|
|
map_to_cvars(f, rec);
|
|
job_email(JOBS_PAYMENT_DUE_MAIL, rec, out); // send the reminder email
|
|
pgsql_exec("UPDATE members_jobs_contract SET pay_remind=now() "\
|
|
"WHERE member_id=%lu AND status = 4 AND pay_remind IS NULL AND delivery_date < now() +'-1 day(s)' ", rec["member_id"].Long());
|
|
}
|
|
}
|
|
logfmt(logINFO, "/PaymentPending()");
|
|
return 0;
|
|
|
|
}
|
|
|
|
long DeadlineLineApprachClient(CVars in, CVars &out) // called
|
|
{
|
|
logfmt(logINFO, "DeadlineLineApprachClient()");
|
|
const PGresult *res = pgsql_query("SELECT j.client_id,j.id AS contract_id FROM members_jobs_contract j "\
|
|
"WHERE j.due_remind IS NULL AND j.status IN (1,2) "\
|
|
"AND j.delivery_date > now() AND j.delivery_date < now() + '1 day' "\
|
|
"AND j.client_id IS NOT NULL AND j.client_id > 0 limit %lu", in["one_limit"].Long());
|
|
if (res != NULL && pgsql_num_rows(res) > 0) {
|
|
for (int i = 0, n = pgsql_num_rows(res); i < n; i++) {
|
|
map<const char*, const char*>f = pgsql_fetch_assoc(res, i);
|
|
if (f.empty()) continue;
|
|
CVars rec;
|
|
map_to_cvars(f, rec);
|
|
job_email(JOBS_DUE_APPROACH_MAIL, rec, out); // send the reminder email
|
|
pgsql_exec("UPDATE members_jobs_contract SET due_remind=now() WHERE id=%lu", rec["contract_id"].Long());
|
|
}
|
|
}
|
|
logfmt(logINFO, "/DeadlineLineApprachClient()");
|
|
return 0;
|
|
}
|
|
|
|
long SignUpPendingReminder(CVars in, CVars &out) {
|
|
|
|
|
|
|
|
}
|