Mobile POS Front End / build / web / index.html

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title id="systemName">Mobile xxPOS</title>
        <link href="css/bootstrap.css" rel="stylesheet">
    </head>

    <body>
        <div class="container">
            <img id ="mainBanner" />
        </div>

        <div class="container">

            <div class="row" class="span12" id="divMainProductCol"> <!-- start of product search and search result row -->

                <h5> Enter Product ID or name to search </h5>
                <p/>

                <input id="productSearchKey" type="text" placeholder="Product ID or name" class="input-large search-query" >
                <button type="submit" onclick=searchProduct() class="btn" id="btnProductSearch">Search</button>
                <text id="searchErrorMsg" class="text-error">
                <!-- div for displaying of product -->
                <div class="row"  id="divSubProductCol">

                </div>

                <!-- div for displaying of page numberings -->
                <div class ="pagination pagination-centered" id="productPagination"> 

                </div>

            </div> <!--end of product search and search result row -->

            <div class="row">  <!-- start of items added table -->

                <h4> Items added </h4>
                <table class ="table table-striped" style="background-color:white" id="productTable"  >
                    <thead>
                        <tr>
                            <th >Name &nbsp&nbsp&nbsp&nbsp</th>
                            <th>Qty</th>
                            <th>Each</th>
                            <th>Total</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>

                <div class="row"> <!--start of row for displaying of the tax and total -->
                    <div class=" offset10">
                        <h4 id="totalAmt">Total: </h4>
                    </div>
                </div> <!--end of row displaying of tax and total-->

                <br/>

                <div class="row"> <!--start of btm row for pay and remove all product button -->
                    <div class="span3">
                        <button class="btn btn-large btn-danger btn-block" type="button" id="clearFormBtn">Remove all products</button>
                    </div>
                    <div class="span3 offset6">
                        <button class="btn-large btn-block btn-primary span3" type="button" id="PayBtn" > Pay </button>
                    </div>
                </div> <!-- end of btm row for pay and cancel order -->

            </div> <!--end of table added row -->

        </div> <!-- end of container -->

        <!-- jQuery via Google + local fallback, see h5bp.com -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <!-- Bootstrap JS: compiled and minified -->
        <script src="js/bootstrap.min.js"></script>
        <script src="js/jquery.chromatable.js"></script>
        <script src="js/json2.js"></script>
        <script src="js/jquery.paginate.js"></script>

        <!-- config java script -->
        <script src="js/mainConfig.js"></script>

        <script>
            
            $(document).ready(function(){
                
                runAtStartUp();
                 
                var shoppingCartCookies = new Array();
                shoppingCartCookies = $.parseJSON(readCookie('shoppingCart'));
                if(shoppingCartCookies!=null)
                {
                    for(var i=0;i<shoppingCartCookies.length;i++)
                    {        
                        loadItemFromCookie(shoppingCartCookies[i].productId,+shoppingCartCookies[i].productQty,
                        shoppingCartCookies[i].productName,shoppingCartCookies[i].productEach,shoppingCartCookies[i].productTotal);
                        
                    }
                } 
                     
                var payBtn = $("#PayBtn");
                var clearFormBtn = $("#clearFormBtn");
		
                //either product or payment page
                var divProduct = $("#divMainProductCol");
                var divPayment = $("#divPayment");
		
                //product info span
                var productTitle = $("#productTitle");
       	
                clearFormBtn.click(function() {
                    $("#productTable > tbody").empty();
                    reUpdateGrandTotal();
                });
		
                payBtn.click(function() {
			
                    var allProducts = new Array();
                    
                    var totalPrice = 0;
                   
                    //store all the products in the table of item added into an array
                    $("#productTable tbody tr").each(function() {
                        var object = new productBought;
                        object.productId = $(this).find("#productId").attr('class');
                        object.productQty = $(this).find("#productQty").html();
                        object.productName= $(this).find("#productId").html();
                        object.productTotal = $(this).find('#productTotal').html();
                        object.productEach = $(this).find('#productEach').html();
                        allProducts.push(object);
                        
                        var eachSubTotal = $(this).find('#productTotal').html().split("$");
                        totalPrice = Number(eachSubTotal[1]) + Number(totalPrice);
                        
                    });
                    
                    //object for the earlier array
                    function productBought(productTitle,qty)
                    {
                        this.productTitle = productTitle;
                        this.qty = qty;
                    }
		
                    //store the item added and grand total into cookies
                    createCookie('shoppingCart',JSON.stringify(allProducts),0);
                    createCookie('grandTotal',totalPrice,0);
                    
                    /**
                     *code segment for formatting the data and sending to ajax server
                     */
                    var productArray = new Array();
                    var productQtyArray = new Array();
                    
                    for(var i =0;i<allProducts.length;i++)
                    {
                        productArray.push(allProducts[i].productId);
                        productQtyArray.push(allProducts[i].productQty);
                    }
                    
                    var branchId = readCookie('branchId');
                    var employeeId = readCookie('employeeId');
                    
                    //order info to send 
                    var orderInfo = { "Branch_ID": branchId, "Employee_ID": employeeId, "Product_ID": productArray, "Quantity": productQtyArray};
                    $.ajax({
                        type: "PUT",
                        url: "http://lowe.jschua.com/DinoPos/api/orders",
                        data: JSON.stringify(orderInfo),
                        contentType: "application/json; charset=utf-8",
                        success: function(data) {
                            console.log("Success!!!");
                            createCookie('orderId',data,0);
                            window.location = "paymentForm.html";
                        },
                        dataType: "text",
                        error: function (jqXHR, textStatus, errorThrown) {
                            $('#result').text("The following error occured: " + textStatus, errorThrown);
                        }
                    });
                 
                  
                });
		
                //hide the payment at the start
                divPayment.hide();
		
                //reinitialize the nice table that doesn't scroll the header.
                reinitChromaTable();
		
            });
            
            /*
            *Method checks if the search text field is blank before proceeding
            *
            *@return false if search text box is empty
            */
            function validateSearchField()
            {
                if($('#productSearchKey').val().length==0)
                {   
                    $('#searchErrorMsg').text("Search field can't be blank!");
                    return false;
                }
                else{
                    $('#searchErrorMsg').text("");
                }
            }
           
            function searchProduct() {
                
                //checking if search field is not blank
                var ifFieldValid = validateSearchField();
                if(ifFieldValid == false)
                    return;
                
                var searchKeyword = $('#productSearchKey').val();
                
                //deciding which restful api to use depending on search keyword
                var url; 
                if(isNumber(searchKeyword))
                    url = "http://lowe.jschua.com/DinoPos/api/products/" +searchKeyword;
                else
                    url = "http://lowe.jschua.com/DinoPos/api/products/?name=" +searchKeyword;
                
      
                var totalProductFound = 0;
                
                $.ajax({
                    url: url,
                    dataType:'json',
                    async:false,
                    success:function(data){
                        clearSearchResult();
                    
                        if(data!=null)
                        {
                            if(data instanceof Array)
                            {
                                // On success, 'data' contains a list of products.
                                $.each(data, function (key, val) {
                                    displayProductInfo(val,totalProductFound);
                                    totalProductFound++;
                                });
                            }
                            else //only one search result
                            {
                                displayProductInfo(data,totalProductFound);
                                totalProductFound++;
                            }
                        }
                    }
                }).done(createPagnation(totalProductFound));
               
            }
            
            //global var 
            var currentPage=1; //edited in showPage
            var totalPage=0; //set in createPagnation
             
            /**
             * Method create the page numbering after a search result
             */
            function createPagnation(totalElement)
            {
                if(totalElement !=0) //no point running the code if there's no search result
                {
                    totalPage = Math.ceil(totalElement/2);
                
                    //creating the main list
                    var mainUl = document.createElement("ul");
                
                    //adding the "Prev" button
                    var subUl = document.createElement("li");
                    subUl.className ="prev disabled"
                    subUl.innerHTML ="<a href=\"#\" onClick=\"showPage(-1)\">Prev</a>";
                    mainUl.appendChild(subUl);
                    
                    //limit the search result to only 20 pages. [Just like amazon]
                    // for(var x=1;x<=Math.ceil(totalElement/2);x++)
                    for(var x=1;x<=20;x++)
                    {
                        var subUl = document.createElement("li");
                        subUl.className =x;
                        if(x==1)
                            subUl.className = subUl.className +" active";
                        subUl.innerHTML ="<a href=\"#\" onClick=\"showPage("+x+")\">"+x+"</a>";
                        mainUl.appendChild(subUl);
                    }
                    
                    //Adding the next button
                    var subUl = document.createElement("li");
                    subUl.innerHTML ="<a href=\"#\" onClick=\"showPage(0)\">Next</a>";
                    subUl.className = "next";
                    mainUl.appendChild(subUl);
                    
                    var test = document.getElementById("productPagination");
                    test.appendChild(mainUl);
                }
            }
            
            /**
             *Method is meant for the "enter" key listener for searching of product.
             */
            $("#productSearchKey").keyup(function(event){
                if(event.keyCode == 13){
                    $("#btnProductSearch").click();
                }
            });

            
            /**
             *Method take in the page number depending on what user selected.
             *if pageNumber == 0, it means user clicked next button
             *if pageNumber ==-1, it means user clicked previous button
             */
            function showPage(pageNumber)
            {
                //set the previously selected page as not active
                $('#productPagination ul li.'+currentPage).removeClass('active');
                
                //-1 = prev, 0 = next
                if(pageNumber!=-1 && pageNumber!=0){
                    currentPage = pageNumber;
                }
                else if(pageNumber==-1) //prev was selected
                {
                    if(currentPage!=1)
                    {
                        currentPage = currentPage-1;
                        pageNumber = currentPage;
                    }else{
                        pageNumber=1;
                    }
                }
                else if(pageNumber==0) //next was selected
                {
                    if(currentPage!=totalPage){
                        currentPage = currentPage+1;
                        pageNumber = currentPage;
                    }else{
                        pageNumber=totalPage;
                    }
                }
                
                //hide the current page product
                $('._current','#divSubProductCol').removeClass('_current').hide();
                
                var productNo1;
                if(pageNumber==1){    
                    productNo1 = 0;
                }
                else{    
                    productNo1 = (pageNumber-1)*2;
                }
                var productNo2 = productNo1+1;
               
                //show the selected page product
                $('#productNo_'+productNo1).addClass('_current').fadeIn();
                $('#productNo_'+productNo2).addClass('_current').fadeIn();
               
                //below code to disable the pageNumber button
                if(pageNumber!=0 && pageNumber!=-1){
                    $('#productPagination ul li.'+pageNumber).addClass('active');
                    $('#productPagination ul li.next').removeClass('disabled');
                    $('#productPagination ul li.prev').removeClass('disabled');
                }
                if(pageNumber ==totalPage){
                    $('#productPagination ul li.next').addClass('disabled');
                }
                if(pageNumber ==1){
                    $('#productPagination ul li.previous').addClass('disabled');
                }   
            }
            
            
            /**
             *function clear the pagnation and the searchresult
             */
            function clearSearchResult()
            {
                if($('#divSubProductCol').children().length >0) {
                    $('#divSubProductCol').empty();
                }
                if($('#productPagination').children().length >0) {
                    $('#productPagination').empty();
                }
            }
            
            /**
             * Function role is to display the product information
             * 
             * @Param val = product object
             * @Param product numbering = search result order #
             */
            function displayProductInfo(val,productNumbering)
            {
                var divInPlace =  document.getElementById("divSubProductCol");
                  
                var mainDiv = document.createElement("div");
                mainDiv.id = "productNo_"+productNumbering;
                
                //hide product numbering 2 onwards. 
                if(productNumbering >1)
                {
                    mainDiv.setAttribute("style", "display:none")
                    mainDiv.className ="pagedemo";
                }
                else
                {
                    mainDiv.className ="pagedemo _current";
                }
                
                //creating the image layout
                var divForImage = document.createElement("div");
                divForImage.className ="span2";
                divForImage.innerHTML ="<img src=\""+val.ImageUrl+"\" height=150 width=150>"
                               
                //creating the product brand, title, description and price layout
                var divForProductInfo = document.createElement("div");
                divForProductInfo.className ="span4";
                divForProductInfo.setAttribute("id", "div_"+val.Product_ID);
                var productBrand = "<h3> <span id=\"productBrand\">"+val.Brand+"</span></h4>";
                var productTitle = "<h4> <span id=\"productTitle\">"+val.Name+"</span></h4>";
                var productDescription ="<h6> <span id=\"productDescription\">"+val.Description+"</span></h6>";
                var productPrice = "<h6> Price $<span id=\"productPrice\">"+val.Pricesell+"</span> </h6>";
                divForProductInfo.innerHTML= productBrand+productTitle+productDescription+productPrice;
                
                //creating the form main div
                var qtyForm = document.createElement("form");
                qtyForm.className ="form-horizontal";   
                var divForQtyForm = document.createElement("div");
                divForQtyForm.className = "control-group";
                
                //creating the error message
                var labelForErrorMsg = document.createElement("label");
                labelForErrorMsg.className ="control-label text-error";
                labelForErrorMsg.setAttribute("id","errorMsg_"+val.Product_ID);
                
                //creating the quantity input
                var divForControl = document.createElement("div");
                divForControl.className = "controls";
                var inputForQty =document.createElement("input");
                inputForQty.setAttribute("type", "text");
                inputForQty.setAttribute("id","qty_"+val.Product_ID);
                inputForQty.setAttribute("placeholder","Quantity");
                inputForQty.className ="input-small";
                divForControl.appendChild(inputForQty);
                divForQtyForm.appendChild(labelForErrorMsg);
                divForQtyForm.appendChild(divForControl);
                qtyForm.appendChild(divForQtyForm);
             
                //creating the add to cart button
                var addToCartButton = document.createElement("input");
                addToCartButton.setAttribute("id",val.Product_ID);
                addToCartButton.setAttribute("type","button");
                var info = "addItemToTable("+val.Product_ID+")"
                addToCartButton.setAttribute("onClick",info);
                addToCartButton.setAttribute("value","Add this item to cart!");
                addToCartButton.className ="btn btn-info span4";                
                                
                //adding the quantity text input and add to cart button into the product info page
                divForProductInfo.appendChild(qtyForm);
                divForProductInfo.appendChild(addToCartButton);
                                
                //adding the image and product info div into the main div
                mainDiv.appendChild(divForImage);
                mainDiv.appendChild(divForProductInfo); 
               
                //appending the div.
                divInPlace.appendChild(mainDiv);
            }
            
            /*
             * Function activated when user clicked "Add to Cart" button
             * @Param productId
             */
            function addItemToTable(productId)
            {
                //check if the user has keyed in qty
                if($('#qty_'+productId).val().length==0) 
                {
                    $('#errorMsg_'+productId).text("Qty can't be blank!");
                    return;
                }
                
                var whetherExistProduct = false;
            
                //loop through the table to check if the exact product has been added before
                //if added before, just update qty.
                $('#productTable >tbody >tr').each(function(){
                
                    var inTableProductId = $(this).find('#productId').attr('class') 
                
                    if(inTableProductId == productId ) //item wanted to add already exist in table
                    {
                        //update the qty and total instead
                        var currentProductQty = $(this).find("#productQty").html();
                        var currentProductEach = $(this).find('#productEach').html();
                      
                        //finding out the new quantity by adding old quantity + new quantity
                        var oldProductQty = $('#div_'+productId).find('#qty_'+productId).val();
                        var newProductQty = Number(currentProductQty) + Number(oldProductQty);
                     
                        //finding out the total by multiplying new qty and product price each
                        var newTotal = (newProductQty * currentProductEach).toFixed(2);
                     
                        //update the qty field with new qty
                        $(this).find('#productQty').html(newProductQty);
                     
                        //update the total with new total
                        $(this).find('#productTotal').html('$'+newTotal);
                     
                        whetherExistProduct=true;
                        return;
                    }
                    
                });
                
                
                //product added is a new product - create a new row for it.
                if(whetherExistProduct==false)
                {
                
                    var productName = $('#div_'+productId).find('#productTitle').text();
                    var productPrice = Number($('#div_'+productId).find('#productPrice').text());
                    var qtySelected = $('#div_'+productId).find('#qty_'+productId).val();
                  
                    var totalPrice = qtySelected * productPrice;
                
                    var tableDetailHeader ="<tr>";
                    var tableProductTitle ="<td class=\""+productId+"\" id=\"productId\">"+productName+"</td>";
                    var tableQty = "<td id=\"productQty\">"+qtySelected+"</td>";
                    var tableEach = "<td id=\"productEach\">"+(productPrice).toFixed(2)+"</td>";
                    var tableTotal = "<td id=\"productTotal\" >$"+(totalPrice).toFixed(2)+"</td>";
                    var tableRemoveItemBtn = "<td> <button class=\"btn btn-mini\" onclick=\"deleteRowItem(this)\"> x</button></td>"
                    var tableDetailFooter ="</tr>";
                    var entireTable = tableDetailHeader + tableProductTitle + tableQty + tableEach + tableTotal + tableRemoveItemBtn+tableDetailFooter;
                
                    $("#productTable > tbody:last").append(entireTable);
                }  
                reUpdateGrandTotal();
                reinitChromaTable();
            }
            
            
            
            function loadItemFromCookie(productId,productQty,productName,productEach,productTotal)
            {
               
                var tableDetailHeader ="<tr>";
                var tableProductTitle ="<td class=\""+productId+"\" id=\"productId\">"+productName+"</td>";
                var tableQty = "<td id=\"productQty\">"+productQty+"</td>";
                var tableEach = "<td id=\"productEach\">"+productEach+"</td>";
                var tableTotal = "<td id=\"productTotal\" >"+productTotal+"</td>";
                var tableRemoveItemBtn = "<td> <button class=\"btn btn-mini\" onclick=\"deleteRowItem(this)\"> x</button></td>"
                var tableDetailFooter ="</tr>";
                var entireTable = tableDetailHeader + tableProductTitle + tableQty + tableEach + tableTotal + tableRemoveItemBtn+tableDetailFooter;
                
                $("#productTable > tbody:last").append(entireTable);
                
                reinitChromaTable();
            }
            
            function isNumber (o){
                return ! isNaN (o-0) && o != null;
            }
   
   
            function createCookie(name,value,days) {
                if (days) {
                    var date = new Date();
                    date.setTime(date.getTime()+(days*24*60*60*1000));
                    var expires = "; expires="+date.toGMTString();
                }
                else var expires = "";
                document.cookie = name+"="+value+expires+"; path=/";
            }
            
            function readCookie(name) {
                var nameEQ = name + "=";
                var ca = document.cookie.split(';');
                for(var i=0;i < ca.length;i++) {
                    var c = ca[i];
                    while (c.charAt(0)==' ') c = c.substring(1,c.length);
                    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
                }
                return null;
            }
  	
            function reinitChromaTable()
            {
                $('#productTable').chromatable({
                    width: "100%",
                    //width: "450px",
                    height: "300px",
                    scrolling: "yes"
	
                });
            }
            function reUpdateGrandTotal()
            {
                var totalPrice = 0;
                $('#productTable >tbody >tr').each(function(){
                    
                    var eachSubTotal = $(this).find('#productTotal').html().split("$");
                    totalPrice = Number(eachSubTotal[1]) + Number(totalPrice);
                    
                });
                
                $('#totalAmt').text("Total: $"+totalPrice);
            }
            function deleteRowItem(o)
            {
                //o = button, parent = htmltablecell -> htmltablerow -> htmltablesection 
                var p = o.parentNode.parentNode;
                p.parentNode.removeChild(p);
                
                reUpdateGrandTotal();
                
            };
 
        </script>

    </body>

</html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.