[php] order

Viewer

  1. // data array
  2. const data = [
  3.     [
  4.         'food',
  5.         'Tomato Soup', 
  6.          240, 
  7.          ,
  8.         'tomato_soup.jpg'
  9.     ],
  10.     [
  11.         'food',
  12.         'Chicken Parmigiana',
  13.          300,
  14.          ,
  15.         'chicken_parmigiana.jpg'
  16.     ],
  17.     [
  18.         'food',
  19.         'Spaghetti Bolognese',
  20.          280, 
  21.          ,
  22.         'spaghetti_bolognese.png'
  23.     ]
  24. ]
  25.  
  26. // global variable
  27. let invoice_item = []
  28. let sub_total = 0
  29. let total = 0
  30. let tax = 0
  31. let price = 0
  32. let input_postion = 0
  33.  
  34. // first loop
  35. let title = $('#name-cat').text()
  36. if(title == 'Food'){
  37.     food_cat()
  38. }
  39.  
  40. //category sorting
  41. $('#food').on('click', function(){
  42.     $('#name-cat').text('Food')
  43.     food_cat()
  44. })
  45.  
  46. $('#drink').on('click', function(){
  47.     drink_cat()
  48. })
  49.  
  50. // reverse
  51. $('#sort').on('click', function(){
  52.     let cat_sort = $('#name-cat').text().toLowerCase()
  53.     if(cat_sort == 'drink'){
  54.         console.log('click drink')
  55.         data.reverse()
  56.         drink_cat()
  57.     }else{
  58.         console.log('click food')
  59.         data.reverse()
  60.         $('#loop-card').html('')
  61.         food_cat()
  62.     }
  63. })
  64.  
  65. //search
  66. $('#search-btn').click(() => {
  67.     $('#search-el').toggle()
  68. })
  69.  
  70. $('#search-input').on('keyup',function(){
  71.     const qvalue = $('#search-input').val()
  72.     const filter = qvalue.toUpperCase()
  73.     const parentCard = document.getElementById('loop-card')
  74.     const card = parentCard.getElementsByClassName('subCardParent')
  75.     for(let i = 0; i < card.length; i++){
  76.         let text = card[i].getElementsByTagName('h5')[0]
  77.         let title = text.textContent
  78.         if(title.toUpperCase().indexOf(filter) > -1){
  79.             card[i].style.display = 'block'
  80.         }else{
  81.             card[i].style.display = 'none'
  82.         }
  83.     }
  84. })
  85.  
  86. function food_cat(){
  87.     $('#loop-card').html('')
  88.     let key = $('#food').text().toLowerCase()
  89.     $.each(data, function(i, val){
  90.         if(val[0] == key){
  91.             const el_card = `
  92.                 <div class="col-md-3 mb-4 subCardParent" onclick="invoice_data('${val[4]}','${val[1]}',${val[2]})">
  93.                     <div class="card">
  94.                     <img src="img/${val[4]}" class="card-img-top" height="130">
  95.                     <div class="card-body">
  96.                         <h5 class="card-title">${val[1]}</h5>
  97.                         <class="card-text">Php ${val[2]}</p>
  98.                     </div>
  99.                     </div>
  100.                 </div>
  101.             `
  102.             $('#loop-card').append(el_card)
  103.         }
  104.     })
  105. }
  106.  
  107. function drink_cat(){
  108.     $('#loop-card').html('')
  109.     let key = $('#drink').text().toLowerCase()
  110.     $('#name-cat').text('Drink')
  111.     $.each(data, function(i, val){
  112.         if(val[0] == key){
  113.             const el_card = `
  114.                 <div class="col-md-3 mb-4 subCardParent" onclick="invoice_data('${val[4]}','${val[1]}',${val[2]})">
  115.                     <div class="card">
  116.                     <img src="img/${val[4]}" class="card-img-top" height="200">
  117.                     <div class="card-body">
  118.                         <h5 class="card-title">${val[1]}</h5>
  119.                         <class="card-text">Php ${val[2]}</p>
  120.                     </div>
  121.                     </div>
  122.                 </div>
  123.             `
  124.             $('#loop-card').append(el_card)
  125.         }
  126.     })
  127. }
  128.  
  129. // push data array to invoice data
  130. function invoice_data(img, name, price){
  131.     let invoice_data = [img, name, price, 0]
  132.     for(let i = 0; i < invoice_item.length; i++){
  133.         if(name == invoice_item[i][1]){
  134.             alert('has available')
  135.             return
  136.         }
  137.     }
  138.     invoice_item.push(invoice_data)
  139.     show_invoice()
  140. }
  141.  
  142. // loop invoice
  143. function show_invoice(){
  144.     $('#loop-invoice').html('')
  145.     $('#total-item').html('')
  146.     $.each(invoice_item, function(i, val){
  147.         const el_media = `
  148.             <div class="media mb-2">
  149.                 <img src="img/${val[0]}" class="align-self-center mr-3" width="95">
  150.                 <div class="media-body">
  151.                 <h6 class="mt-0">${val[1]}</h6>
  152.                 <p>Php <span>${val[2]}</span></p>
  153.                 </div>
  154.                 <input class="quantity mt-3" id="quantity" type="number" value="${Number(val[3])}">
  155.                 <button class="btn delete mt-2"><img src="https://img.icons8.com/wired/35/000000/trash.png"></button>
  156.             </div>
  157.         `
  158.         const el_total = `
  159.             <div class="media mb-2">
  160.                 <img src="img/${val[0]}" class="align-self-center mr-3" width="95">
  161.                 <div class="media-body">
  162.                     <h6 class="mt-0">${val[1]}</h6>
  163.                     <p>Php <span>${val[2]}</span></p>
  164.                     <span>${val[3]}</span>
  165.                 </div>
  166.             </div>
  167.         `
  168.         $('#loop-invoice').append(el_media)
  169.         $('#total-item').append(el_total)
  170.     })
  171. }
  172.  
  173.  
  174. // counting based on quantity
  175. $('#loop-invoice').on('change','.quantity',function(){
  176.     if(isNaN($(this).val()) || $(this).val() <= 0){
  177.         $(this).val(0)
  178.     }
  179.  
  180.     input_postion = $(".quantity").index(this)
  181.     invoice_item[input_postion][3] = $(this).val()
  182.     counting()
  183.     show_invoice()
  184. })
  185.  
  186. // counting
  187. function counting (){
  188.     sub_total = 0
  189.     $.each(invoice_item, function(i, val){
  190.         sub_total += val[2] * val[3]
  191.         tax = sub_total * 0
  192.         total = sub_total + tax
  193.         $('#tax').text(tax)
  194.         $('#total').text(total)
  195.     })
  196. }
  197.  
  198. // input money
  199. $('#money').keypress(function(){
  200.     if (event.keyCode == 13) {
  201.         buy()
  202.     }
  203. })
  204. $('#buy').on('click', buy)
  205.  
  206. // counting total
  207. function buy(){
  208.     let money = $('#money').val()
  209.     let type = $('#buy-type').val()
  210.     let change = money - total
  211.     if(money == ''){
  212.         alert('Insert The Money')
  213.         return
  214.     }else if(money < total){
  215.         alert('your money not enough')
  216.         return
  217.     }else if(isNaN(money)){
  218.         alert("can't counting this value")
  219.         return
  220.     }
  221.     let confirm_invoice = confirm('Confirm Receipt')
  222.     if(confirm_invoice == true){
  223.         $('#tax-in').text(tax)
  224.         $('#total-in').text(total)
  225.         $('#total-type').text(type)
  226.         $('#money-in').text(money)
  227.         $('#change-in').text(change)
  228.         $('#money').val('')
  229.         $('#money').attr('placeholder', 'Insert Your Money')
  230.         $('#modal-invoice').modal('show')
  231.     }else{
  232.         alert('Reciept')
  233.         remove_invoice()
  234.     }
  235. }
  236.  
  237. // delete invoice based on target
  238. $('#loop-invoice').on('click', '.delete', function(){
  239.     let event = $('.delete').index(this)
  240.     invoice_item.splice(event, 1)
  241.     reset_amount()
  242.     counting()
  243.     show_invoice()
  244. })
  245.  
  246.  
  247. // delete all invoice
  248. function remove_invoice(){
  249.     $.each(invoice_item, function(i){
  250.         invoice_item.splice(i, 1)
  251.         invoice_item.shift()
  252.         reset_amount()
  253.     })
  254.     show_invoice()
  255. }
  256.  
  257. function reset_amount(){
  258.     if(invoice_item == ''){
  259.         $('#tax').text(0)
  260.         $('#total').text(0)
  261.     }
  262. }
  263.  
  264. $('#end-payment').on('click', remove_invoice)

Editor

You can edit this paste and save as new:


File Description
  • order
  • Paste Code
  • 09 Jun-2023
  • 6.99 Kb
You can Share it: