1 # ===================================================================== 2 # shopCheckoutXHTML.awk: W-TW XHTML order summary view builder. 3 # 4 # Copyright (c) 2009,2010,2011 Carlo Strozzi 5 # 6 # This program is free software; you can redistribute it and/or modify 7 # it under the terms of the GNU General Public License as published by 8 # the Free Software Foundation; version 2 dated June, 1991. 9 # 10 # This program is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU General Public License for more details. 14 # 15 # You should have received a copy of the GNU General Public License 16 # along with this program; if not, write to the Free Software 17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 # 19 # ===================================================================== 20 21 BEGIN { 22 23 fmt = "%s%d%s" \ 24 "%s%.2f%.2f%.2f\n" 25 26 tmp = tmp1 = tmp2 = tmp3 = tmp4 = tmp5 = tmp6 = tmp7 = _NULL 27 i = tmp8 = 0; delete a; delete b; delete c 28 29 # Set default VAT percentage if unset or invalid. 30 if ((vat=(ENVIRON["TNS_BUY_VAT_PCT"]/=1)) <= 0 || vat > 99) vat = 20 31 32 g_uri = _rcget("tbl_group.g_uri") 33 } 34 35 /^\001/ { next } # skip table header, if any. 36 37 { 38 # k_page, cart_qty, p_name, p_uri, p_descr, p_link, p_store, p_etime 39 40 split($0,a,"\t") 41 42 # Exclude non-store items from output, just in case. 43 if (a[7] == _NULL) next 44 45 # Exclude unavailable (or no longer available) items from output. 46 if (a[7] ~ /^[^:]*:0/) next 47 48 # Handle page expiration dates, accounting for older 49 # versions of page+dat which may lack that field. 50 51 # Set default expiration date. 52 if (a[8] == "") a[8] = "9999-12-31 23:59:59" 53 54 if (ENVIRON["CSA_TIME_ISO"] >= a[8] && \ 55 a[5] !~ /^ *-/) a[5] = "-" a[5] 56 57 # Unconditionally exclude hidden/redirected pages from output. 58 if (a[5] ~ /(^ *-|\(:redirect )/) next 59 60 # Remove any explicit exclusion directive. 61 sub(/^ *! */,_NULL,a[5]) 62 63 # Force default unit of measure if missing. 64 if (a[7] ~ /^[^:]+:[^:]+ /) sub (/ /,":" _nlsmap(_NULL,"units") " ",a[7]) 65 66 # Force unit qty on uncountable items. 67 if (a[7] ~ /^[^:]*:-1/) a[2] = 1 68 69 gsub(/,/,".",a[7]) 70 71 # Item code. 72 tmp = a[7]; sub(/:.*/,_NULL,tmp) 73 74 # Unit of measure. 75 tmp1 = a[7]; split(tmp1,b,/[: ]/); tmp6 = _nlsmap(_NULL,b[3]) 76 77 # Selling price. 78 tmp1 = a[7]; sub(/[^ ]+ +/,_NULL,tmp1); sub(/:.*/,_NULL,tmp1) 79 80 tmp1 *= a[2] # unit price times qty. 81 82 # VAT percentage. 83 tmp2 = a[7]; sub(/[^ ]+ +/,_NULL,tmp2); sub(/ .*/,_NULL,tmp2) 84 85 # VAT defaults to 20%. Note that if the listed price includes VAT 86 # I need to exclude it to get the net price. 87 88 if (split(tmp2,b,":") < 3) { 89 if (_bool(ENVIRON["TNS_BUY_PRICE_HAS_VAT"]) == _TRUE) 90 tmp1 /= ((100 + vat)/100) 91 b[3] = tmp1/100*20 92 } 93 else { 94 if (_bool(ENVIRON["TNS_BUY_PRICE_HAS_VAT"]) == _TRUE) 95 tmp1 /= (1 + b[3]/100) 96 b[3] = tmp1/100*b[3] 97 } 98 99 tmp3 += tmp1 # price subtotal. 100 101 tmp4 += b[3] # tax subtotal. 102 103 # Drop VAT if applicable. 104 if (_rcget("tpl.var.tw.ord.vat",1) == _NULL && \ 105 _bool(ENVIRON["TNS_BUY_VAT_EXEMPT"]) == _TRUE) b[3] = tmp4 = 0 106 107 printf(fmt,tmp,a[2],_xmlencode(tmp6),\ 108 _xmlencode(a[5]),tmp1,b[3],tmp1+b[3]) 109 110 # Shipping & Handling fees: retain only the highest fee 111 # for the selected method, and make sure no unmapped 112 # value was smuggled-through by the user. 113 114 tmp2 = a[7]; sub(/.* /,_NULL,tmp2); i=split(tmp2,c,":") 115 tmp2 = _rcget("tpl.var.tw.ord.ship",1) 116 117 # In case the user tweaked the order form and selected a shipping 118 # method other than those that we provide, thus causing the 119 # shipping price to be zeroed. 120 if (tmp2 < 1 || tmp2 > i) print "csaExit.fault 0038 " \ 121 _nlsmap(_NULL,"shipping/handling") > ENVIRON["tmp_msg"] 122 123 #c[1] = 10; c[2] = 39.5; c[3] = 85 # test values. 124 if (tmp5 != -1 && tmp2 > 0) { 125 if (c[tmp2] > tmp5 || c[tmp2] == -1) tmp5 = c[tmp2] 126 } 127 } 128 129 END { 130 131 fmt = "" _nlsmap(_NULL,"shipping/handling") \ 132 "1" 133 134 if (tmp5 == -1) { 135 fmt = fmt _nlsmap(_NULL,"to be agreed") 136 fmt = fmt "0.00\n" 137 printf(fmt) 138 tmp5 = 0 139 } 140 else { 141 142 # Any discounts are applied on the *net* price, i.e. *before* taxes 143 # are computed and before S+H is also possibly dropped. The latter 144 # is to be considered a *separate* discount applied above a defined 145 # order *gross* amount threshold. 146 147 if (split(ENVIRON["TNS_BUY_DISCOUNTS"],a,/[ \t,]+/)) { 148 d[1] = 0; d[2] = ":" # set discount defaults. 149 for (i in a) { 150 if (split(a[i],b,/[:%]/) == 2) { 151 if ((b[1]/=1) > 0 && (b[2]/=1) > 0 && b[2] < 100) { 152 if (tmp3 >= b[1] && b[2] >= b[2] >= d[1]) { 153 d[1] = b[2] 154 # detect whether the discount is an absolute amount 155 # or a percentage; the last detected mode "wins". 156 if (a[i] !~ /:/) { 157 d[2] = "%" 158 if (d[1] > 100) d[1] = 0 # invalid percentage 159 } 160 } 161 } 162 } 163 } 164 165 # compute actual discount amount. 166 if (d[1] > 0) { 167 if (d[2] == "%") tmp8 = tmp3/100*d[1] 168 else tmp8 = d[1] 169 printf("%s1%s-%.2f-%.2f",\ 170 _nlsmap(_NULL,"discount"),_nlsmap(_NULL,"discount"),tmp8,tmp8) 171 } 172 } 173 174 # Void S+H if order amount is above a configured threshold 175 # for the selected shipping method. The threshold value must 176 # be set taking into account whether listed selling prices 177 # include VAT or not. 178 #if ((tmp=(ENVIRON["TNS_BUY_SHIP_VOID"]/1)) && tmp3 >= tmp) 179 180 if (tmp2 && (tmp=(_rcget("TNS_BUY_SHIP_VOID",tmp2))) \ 181 && tmp3 >= tmp) tmp5 = 0 182 183 if (_bool(ENVIRON["TNS_BUY_PRICE_HAS_VAT"]) == _TRUE) 184 tmp5 /= (1 + ENVIRON["TNS_BUY_SHIP_VAT"]/100) 185 tmp7 = tmp5/100*ENVIRON["TNS_BUY_SHIP_VAT"] 186 187 # Drop shipping VAT if applicable. 188 if (_rcget("tpl.var.tw.ord.vat",1) == _NULL && \ 189 _bool(ENVIRON["TNS_BUY_VAT_EXEMPT"]) == _TRUE) tmp7 = 0 190 191 fmt = fmt _rcget("tpl.var.tw.ord.ship.descr",1) 192 fmt = fmt "%.2f%.2f%.2f\n" 193 printf(fmt,tmp5,tmp7,tmp5+tmp7) 194 } 195 196 fmt = "%.2f\n" 197 printf(fmt,tmp3-tmp8+tmp4+tmp5+tmp7) 198 } 199 200 # EOF