diff --git a/wrenchboard/src/shared_tool/coupons.cc b/wrenchboard/src/shared_tool/coupons.cc new file mode 100644 index 00000000..d82a2619 --- /dev/null +++ b/wrenchboard/src/shared_tool/coupons.cc @@ -0,0 +1,185 @@ +// Account management toosl +#include "clog.h" +#include "cgi.h" +#include "input.h" +#include "wrenchboard_api.h" +#include "coupons.h" +#include "email.h" +#include "safestring.h" +#include +#include "pgsql.h" +#include "pgsql_wrapper.h" +#include "cfg.h" +#include +#include "smoney.h" +#include "account_mngt.h" +#include "creditcards.h" +#include "payments.h" + +/* +#define WRENCHBOARD_COUPON_START 85000 + +#define WRENCHBOARD_COUPON_CREATE 85010 +#define WRENCHBOARD_COUPON_ACTIVATE 85015 +#define WRENCHBOARD_COUPON_REDEEM 85020 + +#define WRENCHBOARD_COUPON_END 85999 +****** */ + + +long coupons_calls(CVars in, CVars &out){ +logfmt(logINFO, "coupons_calls()"); + out["result"] = "YES I GET TO BACK END"; + long action = REQ_LONG(in, "action", 0, -1); + CVars x; + long ret = 0; + const char * loc = getenv("REMOTE_ADDR"); + const PGresult *res; + logfmt(logINFO, "action = %ld", action); + + + switch (action) { + case WRENCHBOARD_COUPON_CREATE: + return CreateCoupon(in, out); + break; + + case WRENCHBOARD_COUPON_ACTIVATE: + return AssignCoupon(in, out); + break; + + case WRENCHBOARD_COUPON_REDEEM: + return RedeemCoupon(in, out); + break; + + } + logfmt(logINFO, "/coupons_calls()"); + return ret; +} + +long RedeemCoupon( CVars in, CVars &out ){ + logfmt(logINFO, "long RedeemCoupon( CVars in, CVars &out )()"); + long ret = 0; + + const PGresult *res; + + try{ + REQ_STRING(in, "code", 5, 49, "(.*)"); + REQ_LONG(in, "member_id", 1, -1); + REQ_LONG(in, "code_id", 1, -1); + const char * loc = getenv("REMOTE_ADDR"); + + long payment_id = WrenchMemberCouponPayment( in, out ); + + + } catch (bad_parameter) { + ret = PHP_API_BAD_PARAM; + } + +return ret; +} + +// for coupon table +#define COUPON_ALLOC_STARTED 100 +#define COUPON_ALLOC_ALLOCATED 200 + + +// for allocation table +#define COUPON_ACTIVE 500 + + +long AssignCoupon( CVars in, CVars &out ){ + long ret = 0; + const char * loc = getenv("REMOTE_ADDR"); + const PGresult *res; + + try{ + + REQ_STRING(in, "code", 5, 49, "(.*)"); + REQ_LONG(in, "member_id", 1, -1); + long coupon_id = 0; + + res = pgsql_query("SELECT * FROM coupons WHERE code='%s' AND status = 0", in["code"].c_str()); + if (res != NULL && pgsql_num_rows(res) > 0) { + mapf = pgsql_fetch_assoc(res, 0); + CVars rec; + map_to_cvars(f, rec); + pgsql_exec("UPDATE coupons SET status=%lu WHERE code='%s' AND status=%lu",COUPON_ALLOC_ALLOCATED,in["code"].c_str(),COUPON_ALLOC_STARTED); + // Now We start +/* +CREATE TABLE coupons_allocation ( + id SERIAL, + code VARCHAR(15) UNIQUE REFERENCES coupons( code ) NOT NULL, + amount INT DEFAULT 0, + member_id INT REFERENCES members(id) NOT NULL, + added timestamp without time zone DEFAULT now(), + active timestamp DEFAULT NULL, + loc INET, + status INT DEFAULT 0 + ); + + wrenchboard=> SELECT substring(ca.code,0,4)||'XXXXXXXX' as code, ca.amount,m.email,m.firstname FROM coupons_allocation ca LEFT JOIN members m ON m.id=ca.member_id; + code | amount | email | firstname +-------------+--------+----------------------+----------- + 1M5XXXXXXXX | 0 | ses66181+1@gmail.com | Olusesan +*/ + CVars xx; + xx["member_id"] = in["member_id"]; xx["member_id"].set_valid(true); + xx["code"] = rec["code"]; xx["code"].set_valid(true); + xx["amount"] = rec["amount"]; xx["amount"].set_valid(true); + xx["status"] = COUPON_ACTIVE; xx["status"].set_valid(true); + + coupon_id = insert_db_record(DBS_VALID, "coupons_allocation", "coupons_allocation_id_seq", xx); + if (coupon_id > 0 ){ + CVars inx; + inx["member_id"] = xx["member_id"]; inx["member_id"].set_valid(true); + inx["coupon_id"] = coupon_id; inx["coupon_id"].set_valid(true); + // mark status on coupon + pgsql_exec("UPDATE coupons SET active=now(), status=%lu WHERE code='%s' AND status=0",COUPON_ALLOC_STARTED,in["code"].c_str()); + // send allocation email + coupon_email(WRENCHBOARD_COUPON_ACTIVATE, inx, out); + + } + + + } + else{ + CVars xx; + // xx["member_id"] = in["member_id"]; xx["member_id"].set_valid(true); + // xx["pref_id"] = in["pref_id"]; xx["pref_id"].set_valid(true); + // xx["status"] = in["status"]; xx["status"].set_valid(true); + // insert_db_record(DBS_VALID, "members_settings", "members_settings_id_seq", xx); + } + ret = PHP_API_OK; + + + } catch (bad_parameter) { + ret = PHP_API_BAD_PARAM; + } + +return ret; +} + +long CreateCoupon( CVars in, CVars &out ){ + long ret = 0; + logfmt(logINFO, "CreateCoupon()"); + REQ_STRING(in, "code", 5, 49, "(.*)"); + REQ_LONG(in, "amount", 1, -1); + CVars x; + x["code"] = in["code"]; + x["code"].set_valid(true); + x["amount"] = in["amount"]; + x["amount"].set_valid(true); + x["description"] = in["description"]; + x["description"].set_valid(true); + + ret = insert_db_record(DBS_VALID, "coupons", "coupons_id_seq", x); + if (ret > 0) { + x["coupon_id"] = ret; + x["coupon_id"].set_valid(true); + + // account_email(ACCOUNT_CONTACT_ALERT, x, out); + } + + +return 0; +} \ No newline at end of file