// 1 => LOADING // 2 => DONE // 3 => ERROR import * as api from "../api/payment.js" export default { namespaced: true, state: { order_id: 0, selected_patient: { order_no: '-', order_date: '-', order_mou: '-', order_company: '-', patient_name: '-', patient_mr: '-', doctor_sender: '-', doctor_sender_address: '-', doctor_pj: '-' }, order_detail: [ // { n:1, d_id:1, t_id:1, t_name:'SGOT', t_price:80000, t_disctotal:7000, t_total:73000 }, // { n:2, d_id:2, t_id:2, t_name:'SGPT', t_price:75000, t_disctotal:8000, t_total:67000 } ], order_delivery: [], order_subtotal: 0, order_rounding: 0, order_total: 0, payment_cash_amount: 0, payment_debit_amount: 0, payment_credit_amount: 0, payments: [], payment_total: 0, payment_number: '', finish_dialog_is_active: false }, mutations: { update_order (state, data) { state.selected_patient = data.order_header state.order_detail = data.order_detail state.order_delivery = data.order_delivery state.order_subtotal = data.order_header.order_subtotal state.order_rounding = data.order_header.order_rounding state.order_total = data.order_header.order_total }, update_order_id (state, id) { state.order_id = id }, update_payment(state, o) { if (o.type == 'cash') state.payment_cash_amount = o.amount if (o.type == 'debit') state.payment_debit_amount = o.amount if (o.type == 'credit') state.payment_credit_amount = o.amount }, update_payments(state, o) { state.payments = o // Total payments let total = 0 for (let i in o) { o[i].payment_actual = Math.round(o[i].payment_actual) total += o[i].payment_actual } state.payment_total = total // Calculate change for (let i in o) { o[i].payment_amount = o[i].payment_actual if (o[i].payment_type_code == 'CASH') { o[i].payment_change = 0 let chg = total - state.order_total; if (chg > o[i].payment_actual) chg = o[i].payment_actual if (chg < 0) chg = 0 o[i].payment_change = chg // re-calculate payment amount o[i].payment_amount = o[i].payment_actual - o[i].payment_change state.payment_total -= chg } } state.payments = o }, reset_payment(state) { state.payment_total = 0 }, update_finish_dialog_is_active(state, val) { state.finish_dialog_is_active = val }, update_payment_number(state, val) { state.payment_number = val } }, actions: { async get_order(context, prm) { // context.commit("update_search_status",1) try { let resp= await api.get_order(prm) if (resp.status != "OK") { // context.commit("update_search_status",3) // context.commit("update_search_error_message",resp.message) } else { // context.commit("update_search_status",2) // context.commit("update_search_error_message","") let data = resp.data.data context.commit("update_order", data) // commit("patientaddress/test", "X", { root: true }) } } catch(e) { // context.commit("update_search_status",3) // context.commit("update_search_error_message",e.message ) } }, async search(context, prm) { // context.commit("update_search_status",1) try { let resp= await api.search(prm) if (resp.status != "OK") { // context.commit("update_search_status",3) // context.commit("update_search_error_message",resp.message) } else { // context.commit("update_search_status",2) // context.commit("update_search_error_message","") let data = resp.data context.commit("update_payments", data) // commit("patientaddress/test", "X", { root: true }) } } catch(e) { // context.commit("update_search_status",3) // context.commit("update_search_error_message",e.message ) } }, async save(context) { var order_id = context.state.order_id; let payments = [] let p = context.state.payments for (let i in context.state.payments) { payments.push({ type: p[i].payment_type_id, amount: p[i].payment_amount }) } console.log(payments) // context.commit("update_search_status",1) try { let resp= await api.save(order_id, payments) if (resp.status != "200") { // context.commit("update_search_status",3) // context.commit("update_search_error_message",resp.message) alert('error') } else { context.commit('update_payment_number', resp.data.data.payment_number) context.commit('update_finish_dialog_is_active', true) } } catch(e) { // context.commit("update_search_status",3) // context.commit("update_search_error_message",e.message ) } } } }