1 # ===================================================================== 2 # shopCheckoutTXT.awk: W-TW plain-text 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 print "\001Code\t\001Qty\t\001Unit\t\001Description" \ 24 "\t\001Amount\t\001Taxes\t\001Subtotals" 25 26 fmt = "%s\t%d\t%s\t%s\t%.2f\t%.2f\t%.2f\n" 27 28 tmp = tmp1 = tmp2 = tmp3 = tmp4 = tmp5 = tmp6 = tmp7 = _NULL 29 i = tmp8 = 0; delete a; delete b; delete c; delete d 30 31 # Set default VAT percentage if unset or invalid. 32 if ((vat=(ENVIRON["TNS_BUY_VAT_PCT"]/=1)) <= 0 || vat > 99) vat = 20 33 34 g_uri = _rcget("tbl_group.g_uri") 35 } 36 37 /^\001/ { next } # skip table header, if any. 38 39 { 40 # k_page, cart_qty, p_name, p_uri, p_descr, p_link, p_store, p_etime 41 42 split($0,a,"\t") 43 44 # Exclude non-store items from output, just in case. 45 if (a[7] == _NULL) next 46 47 # Exclude unavailable (or no-longer available) items from output. 48 if (a[7] ~ /^[^:]*:0/) next 49 50 # Handle page expiration dates, accounting for older 51 # versions of page+dat which may lack that field. 52 53 # Set default expiration date. 54 if (a[8] == "") a[8] = "9999-12-31 23:59:59" 55 56 if (ENVIRON["CSA_TIME_ISO"] >= a[8] && \ 57 a[5] !~ /^ *-/) a[5] = "-" a[5] 58 59 # Unconditionally exclude hidden/redirected pages from output. 60 if (a[5] ~ /(^ *-|\(:redirect )/) next 61 62 # Remove any explicit exclusion directive. 63 sub(/^ *! */,_NULL,a[5]) 64 65 # Force default unit of measure if missing. 66 if (a[7] ~ /^[^:]+:[^:]+ /) sub (/ /,":" _nlsmap(_NULL,"units") " ",a[7]) 67 68 # Force unit qty on uncountable items. 69 if (a[7] ~ /^[^:]*:-1/) a[2] = 1 70 71 gsub(/,/,".",a[7]) 72 73 # Item code. 74 tmp = a[7]; sub(/:.*/,_NULL,tmp) 75 76 # Unit of measure. 77 tmp1 = a[7]; split(tmp1,b,/[: ]/); tmp6 = _nlsmap(_NULL,b[3]) 78 79 # Selling price. 80 tmp1 = a[7]; sub(/[^ ]+ +/,_NULL,tmp1); sub(/:.*/,_NULL,tmp1) 81 82 tmp1 *= a[2] # unit price times qty. 83 84 # VAT percentage. 85 tmp2 = a[7]; sub(/[^ ]+ +/,_NULL,tmp2); sub(/ .*/,_NULL,tmp2) 86 87 # Note that if the listed price includes VAT I need to exclude it 88 # to get the net price. 89 90 if (split(tmp2,b,":") < 3) { 91 if (_bool(ENVIRON["TNS_BUY_PRICE_HAS_VAT"]) == _TRUE) 92 tmp1 /= ((100 + vat)/100) 93 b[3] = tmp1/100*vat 94 } 95 else { 96 if (_bool(ENVIRON["TNS_BUY_PRICE_HAS_VAT"]) == _TRUE) 97 tmp1 /= (1 + b[3]/100) 98 b[3] = tmp1/100*b[3] 99 } 100 101 tmp3 += tmp1 # price subtotal. 102 103 tmp4 += b[3] # tax subtotal. 104 105 # Drop VAT if applicable. 106 if (_rcget("tpl.var.tw.ord.vat",1) == _NULL && \ 107 _bool(ENVIRON["TNS_BUY_VAT_EXEMPT"]) == _TRUE) b[3] = tmp4 = 0 108 109 printf(fmt,tmp,a[2],tmp6,a[5],tmp1,b[3],tmp1+b[3]) 110 111 # Shipping & Handling fees: retain only the highest fee 112 # for the selected method. 113 tmp2 = a[7]; sub(/.* /,_NULL,tmp2); i = split(tmp2,c,":") 114 #c[1] = 10; c[2] = 39.5; c[3] = 85 # test values 115 if (tmp5 != -1 && (tmp2=_rcget("tpl.var.tw.ord.ship",1)) > 0) { 116 if (c[tmp2] > tmp5 || c[tmp2] == -1) tmp5 = c[tmp2] 117 } 118 } 119 120 END { 121 122 fmt = "-\t-\t-\t-\t-\t-\t-\n" _nlsmap(_NULL,"shipping/handling") 123 124 if (tmp5 == -1) { 125 fmt = fmt "\t1\t" _nlsmap(_NULL,"to be agreed") 126 fmt = fmt "\t\t\t0.00\t\n" 127 printf(fmt) 128 _rcset("tw_pay_defer","-1",ENVIRON["ord_values"]) 129 tmp5 = 0 130 } 131 132 else { 133 134 # Any discounts are applied on the *net* price, i.e. *before* taxes 135 # are computed and before S+H is also possibly dropped. The latter 136 # is to be considered a *separate* discount applied above a defined 137 # order *gross* amount threshold. 138 139 if (split(ENVIRON["TNS_BUY_DISCOUNTS"],a,/[ \t,]+/)) { 140 d[1] = 0; d[2] = ":" # set discount defaults. 141 for (i in a) { 142 if (split(a[i],b,/[:%]/) == 2) { 143 if ((b[1]/=1) > 0 && (b[2]/=1) > 0 && b[2] < 100) { 144 if (tmp3 >= b[1] && b[2] >= d[1]) { 145 d[1] = b[2] # discount threshold 146 # detect whether the discount is an absolute amount 147 # or a percentage; the last detected mode "wins". 148 if (a[i] !~ /:/) { 149 d[2] = "%" 150 if (d[1] > 100) d[1] = 0 # invalid percentage 151 } 152 } 153 } 154 } 155 } 156 157 # compute actual discount amount. 158 if (d[1] > 0) { 159 if (d[2] == "%") tmp8 = tmp3/100*d[1] 160 else tmp8 = d[1] 161 printf("%s\t1\t\t%s\t-%.2f\t\t-%.2f\n",\ 162 _nlsmap(_NULL,"discount"),_nlsmap(_NULL,"discount"),tmp8,tmp8) 163 } 164 } 165 166 # Void S+H if order amount is above a configured threshold 167 # for the selected shipping method. The threshold value must 168 # be set taking into account whether listed selling prices 169 # include VAT or not. 170 #if ((tmp=(ENVIRON["TNS_BUY_SHIP_VOID"]/1)) && tmp3 >= tmp) 171 172 if (tmp2 && (tmp=(_rcget("TNS_BUY_SHIP_VOID",tmp2))) \ 173 && tmp3 >= tmp) tmp5 = 0 174 175 if (_bool(ENVIRON["TNS_BUY_PRICE_HAS_VAT"]) == _TRUE) 176 tmp5 /= (1 + ENVIRON["TNS_BUY_SHIP_VAT"]/100) 177 tmp7 = tmp5/100*ENVIRON["TNS_BUY_SHIP_VAT"] 178 179 # Drop shipping VAT if applicable. 180 if (_rcget("tpl.var.tw.ord.vat",1) == _NULL && \ 181 _bool(ENVIRON["TNS_BUY_VAT_EXEMPT"]) == _TRUE) tmp7 = 0 182 183 fmt = fmt "\t1\t\t" _rcget("tpl.var.tw.ord.ship.descr",1) 184 fmt = fmt "\t%.2f\t%.2f\t%.2f\n" 185 printf(fmt,tmp5,tmp7,tmp5+tmp7) 186 } 187 188 fmt = "-\t-\t-\t-\t-\t-\t-\n\t\t\t\t\t\t%.2f\n\t\t\t\t\t\t=\n" 189 190 printf(fmt,tmp3-tmp8+tmp4+tmp5+tmp7) 191 192 _rcset("tpl.var.tw.ord.total",\ 193 sprintf("%.2f",tmp3-tmp8+tmp4+tmp5+tmp7),ENVIRON["ord_values"]) 194 } 195 196 # EOF