167 lines
6.8 KiB
JavaScript
167 lines
6.8 KiB
JavaScript
// 1 => LOADING
|
|
// 2 => DONE
|
|
// 3 => ERROR
|
|
import * as api from "../api/px.js"
|
|
|
|
export default {
|
|
namespaced: true,
|
|
state: {
|
|
|
|
search_status: 0,
|
|
search_error_message: '',
|
|
|
|
query_px: '',
|
|
pxs: [],
|
|
selected_px: {},
|
|
total_px: 0,
|
|
|
|
packet_price: 0,
|
|
estimate_total_price: 0,
|
|
|
|
nat_tests: []
|
|
},
|
|
mutations: {
|
|
|
|
update_search_error_message(state, patient) {
|
|
state.search_error_message = patient
|
|
},
|
|
|
|
|
|
// PX
|
|
update_query_px(state, q) {
|
|
state.query_px = q
|
|
},
|
|
|
|
update_pxs(state, d) {
|
|
state.pxs = d.records
|
|
state.total_px = d.total
|
|
},
|
|
|
|
update_selected_px(state, d) {
|
|
state.selected_px = d
|
|
},
|
|
|
|
update_search_status(state, val) {
|
|
state.search_status = val
|
|
},
|
|
|
|
update_errors(state, val) {
|
|
state.errors = val
|
|
},
|
|
|
|
update_estimate_total_price(state, v) {
|
|
state.estimate_total_price = v
|
|
},
|
|
|
|
update_packet_price(state, v) {
|
|
state.packet_price = v
|
|
},
|
|
|
|
update_nat_tests(state, v) {
|
|
state.nat_tests = v
|
|
}
|
|
},
|
|
actions: {
|
|
async search_px(context) {
|
|
try {
|
|
// Set loading state
|
|
context.commit("update_search_status", 1)
|
|
|
|
let packet = context.rootState.packet.selected_packet
|
|
console.log(packet)
|
|
let resp = await api.search(one_token(), packet.T_PacketID, context.state.query_px)
|
|
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 = {
|
|
records: resp.data.records,
|
|
total: resp.data.total
|
|
}
|
|
|
|
context.commit("update_pxs", data)
|
|
|
|
// Calculate and set estimate_total_price (BRUTO) from loaded data
|
|
// Use T_PacketOriginalBruto from packet if available, otherwise calculate from details
|
|
let totalBruto = 0;
|
|
if (packet.T_PacketOriginalBruto && parseFloat(packet.T_PacketOriginalBruto) > 0) {
|
|
totalBruto = parseFloat(packet.T_PacketOriginalBruto);
|
|
} else {
|
|
// Calculate from detail records
|
|
for (let i in resp.data.records) {
|
|
totalBruto += parseFloat(resp.data.records[i].T_PacketDetailPriceAmount)
|
|
|| parseFloat(resp.data.records[i].T_PriceAmount)
|
|
|| 0;
|
|
}
|
|
}
|
|
context.commit("update_estimate_total_price", totalBruto)
|
|
|
|
// Set packet price (NETTO) from T_PacketPrice
|
|
let packetPrice = parseFloat(packet.T_PacketPrice) || 0;
|
|
context.commit("update_packet_price", packetPrice)
|
|
}
|
|
} catch (e) {
|
|
context.commit("update_search_status", 3)
|
|
context.commit("update_search_error_message", e.message)
|
|
}
|
|
},
|
|
|
|
async save_px(context) {
|
|
try {
|
|
// Set loading state
|
|
context.commit("update_search_status", 1)
|
|
|
|
let packet = context.rootState.packet.selected_packet
|
|
let packet_id = packet.T_PacketID
|
|
let packet_price = packet.T_PacketPrice
|
|
let packet_original_price = packet.T_PacketOriginalPrice
|
|
|
|
let pxs = context.state.pxs
|
|
console.log('=== SAVING PXS DATA ===', pxs)
|
|
let json_px = []
|
|
for (let i in pxs) {
|
|
// Use the correct field names that match the UI component
|
|
// T_PacketDetail* fields are what we use in the UI
|
|
// Use explicit checks with hasOwnProperty or !== undefined to handle 0 values correctly
|
|
let testDisc = (pxs[i].T_PacketDetailPriceDisc !== undefined && pxs[i].T_PacketDetailPriceDisc !== null)
|
|
? parseFloat(pxs[i].T_PacketDetailPriceDisc)
|
|
: (pxs[i].T_PriceDisc !== undefined ? parseFloat(pxs[i].T_PriceDisc) : 0);
|
|
|
|
let testDiscRp = (pxs[i].T_PacketDetailPriceDiscRp !== undefined && pxs[i].T_PacketDetailPriceDiscRp !== null)
|
|
? parseFloat(pxs[i].T_PacketDetailPriceDiscRp)
|
|
: (pxs[i].T_PriceDiscRp !== undefined ? parseFloat(pxs[i].T_PriceDiscRp) : 0);
|
|
|
|
json_px.push({
|
|
test_id: pxs[i].T_TestID,
|
|
test_price: pxs[i].T_PacketDetailPrice || pxs[i].T_PriceTotal, // Final price after discounts
|
|
test_disc: testDisc, // Percentage discount (handles 0 correctly)
|
|
test_discrp: testDiscRp, // Rupiah discount (handles 0 correctly)
|
|
test_subtotal: pxs[i].T_PacketDetailPriceSubTotal || pxs[i].T_PriceSubTotal, // Subtotal = price
|
|
test_bruto: pxs[i].T_PacketDetailPriceAmount || pxs[i].T_PriceAmount // Original bruto price
|
|
})
|
|
}
|
|
console.log('=== JSON PX TO SAVE ===', json_px)
|
|
json_px = JSON.stringify(json_px)
|
|
|
|
let resp = await api.save(one_token(), packet_id, packet_price, packet_original_price, json_px)
|
|
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", "")
|
|
|
|
// DON'T auto-exit add mode
|
|
// Let user click "Kembali" button to exit
|
|
// context.commit("test/update_state_add", false, {root:true})
|
|
}
|
|
} catch (e) {
|
|
context.commit("update_search_status", 3)
|
|
context.commit("update_search_error_message", e.message)
|
|
}
|
|
}
|
|
}
|
|
}
|