120 lines
4.0 KiB
JavaScript
Executable File
120 lines
4.0 KiB
JavaScript
Executable File
const db = require(`../config/dbMysqlConn`);
|
|
const MysqlHelpers = require(`../library/LibMysqlHelper`);
|
|
|
|
class OrdersCheckpointsModels {
|
|
|
|
static IS_UNPAID = 0;
|
|
static IS_PAID_OFF = 1;
|
|
static IS_TF_FAIL = 2;
|
|
|
|
static TF_METHOD_MANUAL = 1;
|
|
static TF_METHOD_DANA = 2;
|
|
|
|
static list(filter = {}) {
|
|
return new Promise(async (resolve, reject) => {
|
|
let params = [], select = '', where = '', join = '', order_by = '', group_by = '', limit = '';
|
|
|
|
if (filter['checkpoint_id']) {
|
|
where += ' AND checkpoint.id = ?';
|
|
params.push(filter['checkpoint_id']);
|
|
}
|
|
if (filter['ord_id']) {
|
|
where += ' AND checkpoint.ord_id = ?';
|
|
params.push(filter['ord_id']);
|
|
}
|
|
if (filter['ord_pocket_id']) {
|
|
where += ' AND checkpoint.ord_pocket_id = ?';
|
|
params.push(filter['ord_pocket_id']);
|
|
}
|
|
if (filter['pck_id']) {
|
|
where += ' AND checkpoint.pck_id = ?';
|
|
params.push(filter['pck_id']);
|
|
}
|
|
|
|
if (filter['did']) {
|
|
where += ' AND ord_drv.drv_id = ?';
|
|
params.push(filter['did']);
|
|
}
|
|
|
|
if (filter['is_paid']) {
|
|
where += ' AND checkpoint.pocket_is_paid = ?';
|
|
params.push(filter['is_paid']);
|
|
}
|
|
|
|
if (filter['select_paid_by']) {
|
|
where += ' ,(SELECT first_name FROM t_users WHERE id = checkpoint.pocket_paid_by LIMIT 1) as pocket_paid_by_name';
|
|
params.push(filter['is_paid']);
|
|
}
|
|
|
|
if (filter['group_by']) {
|
|
group_by = filter['group_by'];
|
|
}
|
|
|
|
if (filter['limit']) {
|
|
limit = ' LIMIT ' + filter['limit'];
|
|
}
|
|
|
|
let query = `SELECT
|
|
ord.id as ord_id,ord.code as ord_code
|
|
,checkpoint.id as checkpoint_id,checkpoint.ord_pocket_id,checkpoint.pocket_sort,checkpoint.pocket_total
|
|
,checkpoint.pocket_is_paid,checkpoint.pocket_paid_at,checkpoint.pocket_paid_by
|
|
,ord_drv.drv_id,ord_drv.drv_name,ord_drv.drv_phone_code,ord_drv.drv_phone_val,ord_drv.drv_mail,ord_drv.drv_addr
|
|
,ord_drv.drv_name2,ord_drv.drv_phone2_code,ord_drv.drv_phone2_val
|
|
${select}
|
|
FROM t_orders_checkpoints as checkpoint
|
|
INNER JOIN t_orders as ord ON checkpoint.ord_id = ord.id
|
|
INNER JOIN t_orders_drivers as ord_drv ON checkpoint.ord_id = ord_drv.ord_id
|
|
${join}
|
|
WHERE checkpoint.dlt is null
|
|
${where}
|
|
${group_by}
|
|
${order_by}
|
|
${limit}
|
|
;`;
|
|
try {
|
|
const conn = await MysqlHelpers.createConnection();
|
|
const result = await MysqlHelpers.query(conn, query, params);
|
|
await MysqlHelpers.releaseConnection(conn);
|
|
resolve(result);
|
|
} catch (e) {
|
|
reject(e);
|
|
}
|
|
// db.query(query, params, function (err, resp) {
|
|
// if (err) {
|
|
// reject(err);
|
|
// return false;
|
|
// }
|
|
// resolve(resp);
|
|
// return true;
|
|
// });
|
|
});
|
|
}
|
|
|
|
static bundleUpdt (updtOrdCheckpoint = {}, insOrdAItems = {}, insOrdLogsTf = {}, updtDana = {}) {
|
|
return new Promise(async (resolve, reject) => {
|
|
try {
|
|
const conn = await MysqlHelpers.createConnection();
|
|
await MysqlHelpers.createTrx(conn);
|
|
|
|
let result = undefined;
|
|
|
|
if (Object.keys(updtOrdCheckpoint).length > 0) result = await MysqlHelpers.queryTrx(conn, `UPDATE t_orders_checkpoints SET ? WHERE id = ?;`, [updtOrdCheckpoint, updtOrdCheckpoint.id]);
|
|
if (Object.keys(insOrdAItems).length > 0) result = await MysqlHelpers.queryTrx(conn, `INSERT INTO t_orders_a_items SET ?;`, [insOrdAItems]);
|
|
if (Object.keys(insOrdLogsTf).length > 0) result = await MysqlHelpers.queryTrx(conn, `INSERT INTO t_orders_logs_tf SET ?;`, [insOrdLogsTf]);
|
|
if (Object.keys(updtDana).length > 0) result = await MysqlHelpers.queryTrx(conn, `UPDATE t_dana SET ? WHERE id = ?;`, [updtDana, updtDana.id]);
|
|
|
|
await MysqlHelpers.commit(conn);
|
|
|
|
resolve({
|
|
type: 'success',
|
|
result,
|
|
})
|
|
} catch (err) {
|
|
reject(err);
|
|
}
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = OrdersCheckpointsModels; |