// 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, order_company: { is_bill: "N", min_dp: 0, min_dp_rp: 0, on_hold: "N", on_hold_text: "" }, payment_cash_amount: 0, payment_debit_amount: 0, payment_credit_amount: 0, payments: [], payment_total: 0, payment_id: 0, payment_number: '', finish_dialog_is_active: false, paid: 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 state.order_company = { is_bill: data.order_header.M_CompanyIsBill, min_dp: data.order_header.M_CompanyMinDP, min_dp_rp: Math.round(data.order_header.M_CompanyMinDP * data.order_header.order_total / 100), on_hold: data.order_header.M_CompanyIsAgingOnHold, on_hold_text: data.order_header.M_CompanyIsAgingOnHoldNote } }, 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 }, update_payment_id(state, val) { state.payment_id = val }, update_paid(state, val) { state.paid = val }, update_order_company(state, val) { state.order_company = 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) { if (Math.round(p[i].payment_amount) == 0) continue; payments.push({ type: p[i].payment_type_id, amount: p[i].payment_amount, actual: p[i].payment_actual, changes: p[i].payment_change, note: p[i].payment_note, card: p[i].payment_card_id, edc: p[i].payment_edc_id }) } // context.commit("update_search_status",1) try { let resp= await api.save(one_token(), 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_payment_id', resp.data.data.payment_id) context.commit('update_finish_dialog_is_active', true) context.commit('update_paid', true) } } catch(e) { // context.commit("update_search_status",3) // context.commit("update_search_error_message",e.message ) } }, async print_nota (context, a) { let usr = one_user() let ts = Date.now() / 1000 | 0 let x = context.rootState.company.selected_mou let rpt_url = '/birt/run?__report=report/one/fo/rpt_t_003.rptdesign&PID='+a+'&username='+usr.M_UserUsername+'&__format=pdf&ts='+ts if (x.M_MouIsBill == 'Y') rpt_url = '/birt/run?__report=report/one/fo/rpt_t_006.rptdesign&__format=pdf&username='+usr.M_UserUsername+'&PID='+a+'&ts='+ts context.commit('order/update_rpt_url', window.BASE_URL + rpt_url, {root:true}) context.commit('order/update_print_dialog_is_active', true, {root:true}) }, async print_invoice (context, a) { context.commit('order/update_rpt_url', window.BASE_URL + '/birt/run?__report=report/one/fo/rpt_t_001.rptdesign&PID='+a+'&username=admin&__format=pdf', {root:true}) context.commit('order/update_print_dialog_is_active', true, {root:true}) }, async print_control (context, a) { context.commit('order/update_rpt_url', window.BASE_URL + '/birt/run?__report=report/one/lab/rpt_fo_001.rptdesign&PID='+a+'&username=admin&__format=pdf', {root:true}) context.commit('order/update_print_dialog_is_active', true, {root:true}) } } }