summaryrefslogtreecommitdiff
path: root/StoneIsland/www/js/lib/cart/CartConfirm.js
blob: 171f41a1b7161939cfbbbfa11fe3670036768841 (plain)
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
var CartConfirm = FormView.extend({
  
  el: "#cart_confirm",
  
  template: $("#cart_confirm .template").html(),
  
  events: {
  },
  
  initialize: function(opt){
    this.parent = opt.parent
    this.$rows = this.$(".rows")
    this.$subtotal = this.$(".subtotal")
    this.$shipping = this.$(".shipping")
    this.$tax = this.$(".tax")
    this.$total = this.$(".total")
    
    this.$shipping_address = this.$(".shipping_address")
    this.$shipping_method = this.$(".shipping_method")
    
    this.$payment_name = this.$(".payment_name")
    this.$payment_method = this.$(".payment_method")
    this.$payment_address = this.$(".payment_address")
    
    this.scroller = new IScroll('#cart_confirm', app.iscroll_options)
  },
  
  show: function(){
    document.body.className = "cart"
    app.cart.el.className = "confirm"
    app.footer.show("PLACE ORDER", "CANCEL")
    window.location.hash = "#/cart/confirm"
    this.deferScrollToTop()

    app.curtain.show("loading")
    promise(sdk.cart.get_status).then( this.populate.bind(this) )
  },

  populate: function(data){
    console.log(data)
    
    data = data.Cart

   this.$rows.empty()
    
    data.Items.forEach(function(item){
      var $el = $("<div class='item'><img src='img/spinner.gif'></div>")
      this.$rows.append($el)
      var code_ten = item.Code10
      var size_id = item.Size

      var code = code_ten.substr(0, 8)
      app.product.find(code, function(data, details){
        var descriptions = app.product.get_descriptions( details )

        var name_partz = descriptions['ModelNames'].split(' ')
        var num = name_partz.shift()
        var title = name_partz.join(' ')
        var type = title_case( descriptions['MicroCategory'] )
        
        var color_name, size_name

        details.Item.ModelColors.some(function(color){
          if (color['Code10'] == code_ten) {
            color_name = color['ColorDescription']
            return true
          }
          return false
        })

        details.Item.ModelSizes.some(function(size){
          if (size['SizeId'] == size_id) {
            // console.log(size)
            size_name = size['Default']['Text']
            size_name = SIZE_LOOKUP[ size_name ] || size_name
            if (! size_name && ! size['Default']['Labeled']) {
              size_name = size['Default']['Text'] + " " + size['Default']['ClassFamily']
            }

            return true
          }
          return false
        })
        
//         size_name = item.DefaultSize + " " + item.DefaultSizeClassFamily

        var t = this.template
                    .replace(/{{image}}/, sdk.image(item['Code10'], '11_f'))
                    .replace(/{{sku}}/, num)
                    .replace(/{{title}}/, title)
                    .replace(/{{type}}/, type)
                    .replace(/{{size}}/, size_name || "DEFAULT")
                    .replace(/{{color}}/, color_name || "DEFAULT")
                    .replace(/{{quantity}}/, 1)
                    .replace(/{{price}}/, as_cash(details.Item.Price.DiscountedPrice))
        $el.data("price", details.Item.Price.DiscountedPrice)
        $el.html(t)
        this.refreshScroller()
      }.bind(this))
    }.bind(this))
    
    var subtotal = data.Totals.TotalWithoutPromotions
    var shipping_cost = data.DeliveryMethod.Selected.Amount.Total
    var tax = data.Totals.TotalSalesTax
    var total = data.Totals.TotalToPay
    
    this.$subtotal.html( as_cash(subtotal) )
    this.$shipping.html( as_cash(shipping_cost) )
    this.$tax.html( as_cash(tax) )
    this.$total.html( as_cash(total) )

    var street = data.Receiver.StreetWithNumber.replace(/\n$/,"").replace("\n", ", ")
    var address = data.Receiver.Name.toUpperCase() + " " + data.Receiver.Surname.toUpperCase() + "<br>" + street + ", "
    address += data.Receiver.City + ", " + data.Receiver.Region + " " + data.Receiver.PostalCode

    this.$shipping_address.html(address)
    this.$shipping_method.html(data.DeliveryMethod.Selected.Type == 1 ? "* STANDARD SHIPPING" : "* EXPRESS SHIPPING")

    var cc = data.Payment.CreditCard
    var cc_street = cc.HolderAddress.replace(/\n$/,"").replace("\n", ", ")
    var cc_type = cc.Type == "AmericanExpress" ? "American Express" : cc.Type

    this.$payment_name.html( cc.HolderName.toUpperCase() + " " + cc.HolderSurname.toUpperCase() )
    this.$payment_method.html( cc_type.toUpperCase() + " XXXX-XXXX-XXXX-" + cc.Last4 )
    
    app.curtain.hide("loading")
  },

  save: function(){
  },
  
  cancel: function(){
  },

})