PSTW_CentralizeSystem/Areas/Inventory/Views/Item/ItemRegistration.cshtml
2024-11-22 14:23:30 +08:00

595 lines
33 KiB
Plaintext

@{
ViewData["Title"] = "Item Form";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id="registerItem">
<form v-on:submit.prevent="addItem" data-aos="fade-right">
<div class="container register" data-aos="fade-right">
<div class="row" data-aos="fade-right">
@*Left Side*@
<div class="col-md-3 register-left">
<img src="https://media.licdn.com/dms/image/C4E03AQEJ_X-GwTi3xg/profile-displayphoto-shrink_200_200/0/1607307680517?e=2147483647&v=beta&t=UL8IX1nO9iqRxGrQrNZ1O_i4tpjnOVVecIktw-GB6QI" alt="" />
<h3>Welcome</h3>
<p>Registration Product! Click button to go Product Page</p>
<a href="@Url.Action("Product", "Home")" class="btn btn-primary">Product Registration</a><br />
</div>
@*Right Side*@
<div class="col-md-9 register-right">
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<h3 class="register-heading">REGISTRATION ITEM</h3>
<div class="row register-form">
<div class="col-md-6">
<!-- Company Dropdown -->
<div class="form-group row">
<label class="col-sm-3 col-form-label">Company:</label>
<div class="col-sm-9">
<div class="dropdown">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<span id="updateCompany">{{ company || 'Select Company' }}</span>
</button>
<div class="dropdown-menu p-3">
<div v-for="(comp, index) in companies" :key="index" class="form-check">
<input class="form-check-input" type="radio" :id="'company-' + index" :value="comp.Name" name="company" v-model="company" v-on:change="updateDepts(comp.Departments)">
<label class="form-check-label" :for="'company-' + index">{{ comp.Name }}</label>
</div>
</div>
</div>
</div>
</div>
<!-- Department Dropdown -->
<div class="form-group row">
<label class="col-sm-3 col-form-label">Department:</label>
<div class="col-sm-9">
<div class="dropdown">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<span id="updateDept">{{ Dept || 'Select Dept/Div' }}</span>
</button>
<div class="dropdown-menu p-3">
<div v-for="(dept, index) in depts" :key="index" class="form-check">
<input class="form-check-input" type="radio" :id="'Dept-' + index" :value="dept" name="Dept" v-model="Dept">
<label class="form-check-label" :for="'Dept-' + index">{{ dept }}</label>
</div>
</div>
</div>
</div>
</div>
@* Product Name Coding *@
<div class="form-group row">
<label class="col-sm-3 col-form-label">Product Name:</label>
<div class="col-sm-9">
<div class="dropdown">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<span id="updateName">{{ productName || 'Select Product Name' }}</span>
</button>
<div class="dropdown-menu p-3">
<div class="form-check" v-for="product in products" :key="product.modelNo">
<!-- Use modelNo as the key -->
<input class="form-check-input" type="radio" :id="product.modelNo" :value="product.modelNo"
v-on:click="updateProduct(product)" v-model="selectedProduct">
<label class="form-check-label" :for="product.modelNo">{{ product.productName }}</label>
</div>
</div>
</div>
</div>
</div>
@* Product Image Display *@
<div class="form-group row align-items-center">
<label for="imageProduct" class="col-sm-3 col-form-label">Product Image: </label>
<div class="col-sm-9">
<img v-if="imageProduct" :src="'data:image/png;base64,' + imageProduct" alt="Product Image" class="img-fluid" v-on:click="openModal" />
<input type="hidden" id="imageProduct" name="imageProduct" v-model="imageProduct">
</div>
</div>
<div v-if="isModalOpen" class="modal" v-on:click="closeModal">
<div class="modal-content" v-on:click.stop>
<span class="close" v-on:click="closeModal">&times;</span>
<img :src="'data:image/png;base64,' + imageProduct" alt="Product Image" class="enlarged-image" />
</div>
</div>
@* Product Category Coding *@
<div class="form-group row">
<label class="col-sm-3 col-form-label">Product Category:</label>
<div class="col-sm-9">
<div class="dropdown">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<span id="updateProductCategory">{{ productCategory || 'Select Product' }}</span>
</button>
<div class="dropdown-menu p-3">
<div class="form-check">
<input class="form-check-input" type="radio" name="productCategory" id="item" value="Item" v-on:click="updateProductCategory('Item')" v-model="productCategory">
<label class="form-check-label" for="item">Item</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="productCategory" id="part" value="Part" v-on:click="updateProductCategory('Part')" v-model="productCategory">
<label class="form-check-label" for="part">Part</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="productCategory" id="disposable" value="Disposable" v-on:click="updateProductCategory('Disposable')" v-model="productCategory">
<label class="form-check-label" for="disposable">Disposable</label>
</div>
</div>
</div>
</div>
</div>
@* Serial Number and Quantity Coding *@
<div v-if="showSerialNumber">
<div class="form-group row align-items-center">
<div class="col-sm-3">
<label for="serialNumber">Serial Number: </label>
</div>
<div class="col-sm-9">
<input type="text" id="serialNumber" name="serialNumber" v-if="showSerialNumber" v-model="serialNumber" class="form-control">
</div>
</div>
</div>
<div v-if="!showSerialNumber">
<div class="form-group row align-items-center">
<div class="col-sm-3">
<label for="quantity">Quantity: </label>
</div>
<div class="col-sm-9">
<input type="number" id="quantity" name="quantity" v-if="!showSerialNumber" v-model="quantity" class="form-control">
</div>
</div>
</div>
@* Supplier coding *@
<div class="form-group row align-items-center">
<label class="col-sm-3 col-form-label">Supplier: </label>
<div class="col-sm-9">
<div class="dropdown">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<span id="updateSupplierName">{{ supplierName || 'Select Supplier' }}</span>
</button>
<div class="dropdown-menu p-3">
<div class="form-check" v-for="supplier in supplies" :key="supplier.modelNoes">
<input class="form-check-input" type="radio" :id="supplier.modelNoes" :value="supplier.supplierName" v-on:click="updateSupplier(supplier)" v-model="selectedSupplier">
<label class="form-check-label" :for="supplier.modelNoes">{{ supplier.supplierName }}</label>
</div>@*
<div class="form-check">
<input class="form-check-input" type="radio" name="Supplier" id="otherS" v-on:click="toggleOtherInput('Supplier')">
<label class="form-check-label" for="otherS">Other:</label>
</div>
<div v-if="showOtherSupplier">
<input type="text" class="form-control" id="otherSupplier" name="Supplier" placeholder="Your Supplier Name" v-model="Supplier">
</div> *@
</div>
</div>
</div>
</div>
@* Purchase Date coding *@
<div class="form-group row align-items-center">
<label for="purchaseDate" class="col-sm-3 col-form-label">Purchase Date: </label>
<div class="col-sm-9">
<input type="date" id="purchaseDate" name="purchaseDate" required v-model="purchaseDate" class="form-control">
</div>
</div>
@* PO coding *@
<div class="form-group row align-items-center">
<label for="PO" class="col-sm-3 col-form-label">Enter PO: </label>
<div class="col-sm-9">
<input type="text" id="PO" name="PO" required v-model="PO" placeholder="PO123456" class="form-control">
</div>
</div>
</div>
<div class="col-md-6">
@* Item Price in RM *@
<div class="form-group row">
<label for="priceInRM" class="col-sm-3">Item Price In(RM):</label>
<div class="col-sm-9">
<input type="number" id="priceInRM" name="priceInRM" class="form-control" placeholder="RM 00.00" step="0.01" v-on:input="convertCurrency()" required v-model="priceInRM">
</div>
</div>
@* Currency Selection *@
<div class="form-group row">
<label for="currency" class="col-sm-3">Select Currency:</label>
<div class="col-sm-9">
<select id="currency" name="currency" class="form-control" v-model="currency" v-on:change="convertCurrency()">
<option value="" disabled selected>Select a currency</option>
<option v-for="(name, code) in currencies" :key="code" :value="code">
{{ code }} - {{ name }}
</option>
</select>
</div>
</div>
@* Currency Rate *@
<div class="form-group row">
<label for="currencyRate" class="col-sm-3">Currency Rate(%):</label>
<div class="col-sm-9">
<input type="number" id="currencyRate" name="currencyRate" class="form-control" placeholder="0.01%" step="0.01" v-on:input="convertCurrency()" required v-model="currencyRate">
</div>
</div>
@* Item Convert Price *@
<div class="form-group row">
<label for="convertPrice" class="col-sm-3">{{ currency ? 'Item Price (' + currency + ') : ' : 'Item Price : ' }}</label>
<div class="col-sm-9">
<input type="number" id="convertPrice" name="convertPrice" class="form-control" readonly v-model="convertPrice">
</div>
</div>
@* Delivery Order Date *@
<div class="form-group row">
<label for="DODate" class="col-sm-3">Enter DO Date:</label>
<div class="col-sm-9">
<input type="date" id="DODate" name="DODate" class="form-control" v-on:input="calculateWarrantyEndDate()" required v-model="DODate">
</div>
</div>
@* Warranty *@
<div class="form-group row">
<label for="warranty" class="col-sm-3">Enter Warranty Months(Number):</label>
<div class="col-sm-9">
<input type="number" id="warranty" name="warranty" class="form-control" placeholder="0 , 1 , 2 , 3, ..." step="1" min="0" v-on:input="calculateWarrantyEndDate()" required v-model="warranty">
</div>
</div>
@* Warranty End Date *@
<div class="form-group row">
<label for="EndWDate" class="col-sm-3">Warranty End:</label>
<div class="col-sm-9">
<input type="date" id="EndWDate" name="EndWDate" class="form-control" readonly v-model="EndWDate">
</div>
</div>
@* Invoice Date *@
<div class="form-group row">
<label for="invoiceDate" class="col-sm-3">Invoice Date:</label>
<div class="col-sm-9">
<input type="date" id="invoiceDate" name="invoiceDate" class="form-control" required v-model="invoiceDate">
</div>
</div>
</div>
@* Submit and Reset Buttons *@
<div class="form-group row">
<div class="col-sm-9 offset-sm-3">
<button type="button" v-on:click="resetForm" class="btn btn-secondary">Reset</button>
<input type="submit" class="btn btn-primary" value="Submit" />
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
@*Vue Js - POST & RESET*@
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<script>
new Vue({
el: '#registerItem',
data() {
return {
company: null,
Dept: null,
productName: null,
imageProduct: null,
productCategory: null,
serialNumber: '',
quantity: 0,
supplierName: null,
purchaseDate: null,
PO: null,
currency: null,
priceInRM: null,
currencyRate: null,
convertPrice: null,
DODate: null,
warranty: null,
EndWDate: null,
invoiceDate: null,
products: [],
companies: [],
depts: [],
supplies: [],
showOtherCompany: false,
showOtherDept: false,
showOtherSupplier: false,
isModalOpen: false,
selectedProduct: null,
selectedSupplier: null,
selectedCompany: null,
selectedDepartment: null,
currencies: {},
}
},
mounted() {// Log the token to check its value
// Fetch companies, depts, and products from the API
this.fetchCurrencyData();
this.fetchCompanies();
this.fetchProducts();
this.fetchSupplies();
},
computed: {
showSerialNumber() {
return this.productCategory === 'Item' || this.productCategory === 'Part';
}
},
methods: {
async addItem() {
const formData = {
company: this.showOtherCompany ? this.selectedCompany : this.company,
Dept: this.showOtherDept ? this.Dept : this.Dept,
productName: this.productName,
imageProduct: this.imageProduct,
productCategory: this.productCategory,
serialNumber: this.serialNumber,
quantity: this.quantity,
supplierName: this.supplierName,
purchaseDate: this.purchaseDate,
PO: this.PO,
currency: this.currency,
priceInRM: this.priceInRM,
currencyRate: this.currencyRate,
convertPrice: this.convertPrice,
DODate: this.DODate,
warranty: this.warranty,
EndWDate: this.EndWDate,
invoiceDate: this.invoiceDate
};
try {
// List of required fields (excluding serialNumber based on showSerialNumber)
const requiredFields = ['company', 'Dept', 'productCategory', 'productName', 'quantity', 'supplierName', 'purchaseDate', 'PO', 'currency', 'priceInRM', 'currencyRate', 'convertPrice', 'DODate', 'warranty', 'EndWDate', 'invoiceDate'];
// Loop through required fields and check if any are null or empty
for (let field of requiredFields) {
if (this[field] === null || this[field] === '') {
swal(`${field} Error`, `Please fill in required fields: ${field}.`, 'warning');
return; // Exit early if validation fails
}
}
// Additional specific checks
if (this.showSerialNumber) {
this.quantity = 0;
if (this.serialNumber === null || this.serialNumber === '') {
swal('Serial Number Error', 'Serial Number must be filled when selecting Item or Part.', 'warning');
return;
}
}
else {
this.serialNumber = null;
if (this.quantity === 0 || this.quantity === null || this.quantity === '') {
swal('quantity Error', 'Quantity is required when selecting Disposable.', 'warning');
return;
}
}
// Proceed to send the data to the API
const response = await fetch('/api/Item', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.token}`
},
body: JSON.stringify(formData)
});
if (response.ok) {
// If the form submission was successful, display a success message
swal('Success!', 'Item form has been successfully submitted.', 'success');
// Reset the form
this.resetForm();
} else {
throw new Error('Failed to submit form.');
}
} catch (error) {
console.error('Error:', error);
// Displaying error message
swal('Inventory PSTW Error', `An error occurred: ${error.message}`, 'error');
}
},
async fetchProducts() {
try {
const token = localStorage.getItem('token'); // Get the token from localStorage
const response = await fetch('/api/Product/GetProducts', {
method: 'GET', // Specify the HTTP method
headers: {
'Content-Type': 'application/json', // Set content type
'Authorization': `Bearer ${token}` // Include the token in the headers
}
});
if (!response.ok) {
throw new Error('Failed to fetch products');
}
this.products = await response.json();
} catch (error) {
console.error('Error fetching products:', error);
}
},
async fetchCompanies() {
try {
const response = await fetch('/api/Item/GetCompanies', {
method: 'GET', // Specify the HTTP method
headers: {
'Content-Type': 'application/json'
}
});
if (!response.ok) {
throw new Error('Failed to fetch companies');
}
this.companies = await response.json();
} catch (error) {
console.error('Error fetching products:', error);
}
},
async fetchSupplies() {
try {
const response = await fetch('/api/Item/GetSupplies', {
method: 'GET', // Specify the HTTP method
headers: {
'Content-Type': 'application/json'
}
});
if (!response.ok) {
throw new Error('Failed to fetch supplies');
}
this.supplies = await response.json(); // Get the full response object
} catch (error) {
console.error('Error fetching supplies:', error);
}
},
async fetchCurrencyData() {
try {
// Fetch currency data from the API
const response = await fetch('https://openexchangerates.org/api/currencies.json'); // Example API
this.currencies = await response.json();
} catch (error) {
console.error('Error fetching currency data:', error);
}
},
convertCurrency() {
// Your currency conversion logic here
console.log('Selected currency:', this.currency);
},
resetForm() {
this.company = null;
this.Dept = null;
this.productName = null;
this.imageProduct = null;
this.productCategory = null;
this.serialNumber = '';
this.quantity = 0;
this.supplierName = null;
this.purchaseDate = null;
this.PO = null;
this.currency = null;
this.priceInRM = null;
this.currencyRate = null;
this.convertPrice = null;
this.DODate = null;
this.warranty = null;
this.EndWDate = null;
this.invoiceDate = null;
this.showOtherCompany = false;
this.showOtherDept = false;
this.showOtherSupplier = false;
this.selectedCompany = null;
this.selectedSupplier = null;
},
// FRONT END FUNCTIONS
//----------------------//
//Calculate Total Price
convertCurrency() {
const total = this.priceInRM / this.currencyRate;
this.convertPrice = total.toFixed(2);
this.priceInRM = this.priceInRM
.replace(/[^0-9.]/g, '') // Remove non-numeric characters except decimal points
.replace(/(\..*)\..*/g, '$1') // Allow only one decimal point
.replace(/^(\d*\.\d{0,2})\d*$/, '$1'); // Limit to two decimal places
},
openModal() {
this.isModalOpen = true; // Open the modal
},
closeModal() {
this.isModalOpen = false; // Close the modal
},
calculateWarrantyEndDate() {
// Check if DODate and warranty are valid
if (!this.DODate || isNaN(Date.parse(this.DODate))) {
this.EndWDate = null;
return;
}
const DODates = new Date(this.DODate);
const warrantyMonth = parseInt(this.warranty);
// Ensure warranty is a valid number
if (!isNaN(warrantyMonth)) {
DODates.setMonth(DODates.getMonth() + warrantyMonth);
this.EndWDate = DODates.toISOString().split('T')[0];
} else {
this.EndWDate = null;
}
},
//Update Select View
updateCompany(company) {
this.company = company.Name; // Only set the company name
this.depts = company.Departments || []; // Populate depts based on selected company
this.Dept = null; // Reset the selected department
this.showOtherCompany = false; // Hide other company input if needed
},
updateDepts(departments) {
this.depts = departments || []; // Populate depts for the selected company
this.Dept = null; // Reset the selected department
},
updateDept(dept) {
this.Dept = dept;
this.showOtherDept = false;
},
updateProductCategory(productCategory) {
this.productCategory = productCategory;
},
updateProduct(selectedProduct) {
this.productName = selectedProduct.productName;
this.imageProduct = selectedProduct.imageProduct;
this.selectedProduct = selectedProduct.modelNo;
},
updateSupplier(selectedSupplier) {
this.supplierName = selectedSupplier.supplierName;
this.selectedSupplier = selectedSupplier.supplierName;
},
//User Press button Other
toggleOtherInput(type) {
if (type === 'company') {
this.showOtherCompany = true;
this.selectedCompany = null; // Clear company selection when "Other" is chosen
this.company = null;
}
else if (type === 'Dept') {
this.showOtherDept = true;
this.Dept = null; // Clear department selection when "Other" is chosen
}
else if (type === 'Supplier') {
this.supplierName = null; // Clear supplier selection when "Other" is chosen
}
},
},
});
</script>