Hi, First I am a beginner that has started a project for advanced coders. I was able to receive help and with that I almost finished my project.
To complete, I first need to finish this invoice system. The New Invoice page needs a few details done:
- New Invoice No need to be changed to autovalue.
- All prices need to be shown with $
- Sum prices, tax and total amount
- Attracting customers from the customers collection and save in the invoices.
live: https://hortiray.wixsite.com/database/fguy48-uop9l2arwq3
- the invoiceNo number field refering to Invoices Collection number field invoiceNo need to be set to autovalue counting from 0001 to 9999. Currently it has code but line 8-11(in bold) has error: wrong spot and no function.
2)$: The existing code is not working but has worked on other pages before. Might be a different problem? - No code yet, I would like:
priceInput1 + priceInput2 + priceInput3 = subtotal % tax (input20)= taxamount
subtotal + taxamount = totalPriceInput
(Important that priceInput1 and priceinput2 can stay empty) - Existing Customer Dropdown: dropdown1 connecting to the address, suburb, state, postcode, country inputs and resetDropdownsButton
90% of the code I re-used from previous questions/mini-tutorials:
https://www.wix.com/code/home/forum/community-discussion/wix-data-dropdown-multiple-collections-and-repeater and https://www.wix.com/code/home/forum/community-discussion/dropdown-and-database
I am not sure if it works since I have an error in the code but I foresee some issues.
import wixData from 'wix-data';
$w.onReady(function () {
uniqueDropDown1();
$w('#dropdown1').onChange(dropdownHasChanged);
$w('#resetDropdownsButton').onClick(resetDropdownsButton_clicked);
});
let newInvoiceNo = await getNewInvoiceNumber();
if (!newInvoiceNo) {newInvoiceNo = 0;}
$w("#invoiceNo").text = newInvoiceNo;;
export function priceInput1_change(event, $w) {
let num = parseFloat($w("#priceInput1").value)
$w("#priceInput1").value = '$' + num.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")
}
export function priceInput2_change(event, $w) {
let num = parseFloat($w("#priceInput2").value)
$w("#priceInput2").value = '$' + num.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")
}
export function priceInput3_change(event, $w) {
let num = parseFloat($w("#priceInput3").value)
$w("#priceInput3").value = '$' + num.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")
}
export function subtotal_change(event, $w) {
let num = parseFloat($w("#subtotal").value)
$w("#subtotal").value = '$' + num.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")
}
export function taxamount_change(event, $w) {
let num = parseFloat($w("#taxamount").value)
$w("#taxamount").value = '$' + num.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")
}
export function totalPriceInput_change(event, $w) {
let num = parseFloat($w("#totalPriceInput").value)
$w("#totalPriceInput").value = '$' + num.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")
}
export function getNewInvoiceNo() {
return wixData.query("Invoices")
.descending("_createdDate")
.limit(1)
.find()
.then((results => {
let lastAddedItem = results.items[0];
let lastInvoiceNo = lastAddedItem.invoiceNo;
let newInvoiceNo = lastInvoiceNo++;
return newInvoiceNo;
})
}
function resetDropdownsButton_clicked(event) {
$w('#dropdown1').selectedIndex = undefined;
$w('#dropdown1').resetValidityIndication();
$w('#adressInput').value = undefined;
$w('#suburbInput').value = undefined;
$w('#stateInput').value = undefined;
$w('#postcodeInput').value = undefined;
$w('#countryInput').value = undefined;
dropdownHasChanged();
}
function uniqueDropDown1() {
wixData.query("Customers")
.limit(1000)
.find()
.then(results => {
const uniqueTitles = getUniqueTitles(results.items, 'customerName');
$w("#dropdown1").options = buildOptions(uniqueTitles);
});
}
function getUniqueTitles(items, columnKey) {
const titlesOnly = items.map(item => item[columnKey]);
let result = [];
let uniqueSet = new Set(titlesOnly);
let setIterator = uniqueSet.entries();
for (let entry of setIterator) {
let entryValue = entry[0];
if (entryValue) {
result.push(entryValue);
}
}
return result;
}
function buildOptions(uniqueList) {
return uniqueList.map(curr => {
return { label: curr, value: curr };
});
}
function dropdownHasChanged(event) {
let datasetFilter = wixData.filter();
let dropdownValue = "NoCustomerSelected";
if ($w('#dropdown1').selectedIndex >= 0 ) {
dropdownValue = $w('#dropdown1').value;
datasetFilter = datasetFilter.eq('customerName', dropdownValue);
}
$w('#dataset2').setFilter(datasetFilter)
.then(() => {
return $w('#dataset2').refresh();
})
.then(() => {
return wixData.query("Customers") // Make sure this is the correct name
.eq("customerName", dropdownValue)
.find();
})
.then((result) => {
if (result.length !== 1) {
throw Error("Internal Error? Customer record look up for " +
dropdownValue + " returned " +
result.length.toString() + "records");
}
let CustomerRecord = result.items[0];
$w('#adressInput').value = customerRecord.cycle;
$w('#suburbInput').value = customerRecord.cycle;
$w('#stateInput').value = customerRecord.cycle;
$w('#postcodeInput').value = customerRecord.cycle;
$w('#countryInput').value = customerRecord.cycle;
})
.catch((error) => {
// If we get here we have encountered an error so console.log() it for now
console.log(error.message);
});
}
Any assistance would be highly appreciated!