#### begin quickbooks for [tx-param order_number] #####
[tmp quickbooks_record]
[tmp order_number][either][scratch qb_invoice_num][or][tx-param order_number][/either][/tmp]
[if-tx-param salestax > 0]
	[set NAMEISTAXABLE]Y[/set]
	[set ISTAXABLE]1[/set]
	[set NAME]Invoice[/set]
[else]
	[set NAMEISTAXABLE]N[/set]
	[set ISTAXABLE]0[/set]
	[set NAME]Invoice with Sales Tax[/set]
[/else]
[/if-tx-param]
Setting salestax: ISTAXABLE=[scratch ISTAXABLE]
[if-tx-param shipping]
	[set HASSHIPPING]1[/set]
[else]
	[set HASSHIPPING]0[/set]
[/else]
[/if-tx-param]
Checking shipping: HASSHIPPING=[scratch HASSHIPPING]
[if-tx-param b_address1]
	[seti ADDR1][if-tx-param b_fname][tx-param b_fname] [tx-param b_lname][else][tx-param fname] [tx-param lname][/else][/if-tx-param][/seti]
	[seti ADDR2][if-tx-param company][tx-param company][/if-tx-param][/seti]
	[seti ADDR3][tx-param b_address1][if-tx-param b_address2], [tx-param b_address2][/if-tx-param][/seti]
	[seti ADDR4][tx-param b_city], [tx-param b_state] [tx-param b_zip][/seti]
	[seti ADDR5][data table=country field=name key="[tx-param b_country]"][/seti]
	[seti SADDR1][tx-param fname] [tx-param lname][/seti]
	[seti SADDR2][if-tx-param company][tx-param company][/if-tx-param][/seti]
	[seti SADDR3][tx-param address1][if-tx-param address2], [tx-param address2][/if-tx-param][/seti]
	[seti SADDR4][tx-param city], [tx-param state] [tx-param zip][/seti]
	[seti SADDR5][data table=country field=name key="[tx-param country]"][/seti]
[else]
	[seti ADDR1][tx-param fname] [tx-param lname][/seti]
	[seti ADDR2][if-tx-param company][tx-param company][/if-tx-param][/seti]
	[seti ADDR3][tx-param address1][if-tx-param address2], [tx-param address2][/if-tx-param][/seti]
	[seti ADDR4][tx-param city], [tx-param state] [tx-param zip][/seti]
	[seti ADDR5][data table=country field=name key="[tx-param country]"][/seti]
	[seti SADDR1][tx-param fname] [tx-param lname][/seti]
	[seti SADDR2][if-tx-param company][tx-param company][/if-tx-param][/seti]
	[seti SADDR3][tx-param address1][if-tx-param address2], [tx-param address2][/if-tx-param][/seti]
	[seti SADDR4][tx-param city], [tx-param state] [tx-param zip][/seti]
	[seti SADDR5][data table=country field=name key="[tx-param country]"][/seti]
[/else]
[/if-tx-param]
[if variable FILE_BY_COMPANY]
	[seti CUSTNAME][tx-param company] ([tx-param username])[/seti]
[else]
	[seti CUSTNAME][tx-param lname], [tx-param fname] ([tx-param username])[/seti]
[/else]
[/if]
[seti TRNSTYPE][either]__QB_TRANSACTION_TYPE__[or]CASH SALE[/either][/seti]
[seti DATE][convert-date format="%m/%d/%y"][tx-param order_ymd][/convert-date][/seti]
[seti FILEDATE][tag time]%Y%m%d[/tag][/seti]
[seti qb_trans]!CUST	NAME	REFNUM	TIMESTAMP	EMAIL	BADDR1	BADDR2	BADDR3	BADDR4	BADDR5	SADDR1	SADDR2	SADDR3	SADDR4	SADDR5	PHONE1	PHONE2	FAXNUM	NOTE	CONT1	CONT2	CTYPE	TERMS	TAXABLE	LIMIT	RESALENUM	REP	TAXITEM	NOTEPAD	SALUTATION	COMPANYNAME	FIRSTNAME	MIDINIT	LASTNAME	CUSTFLD1	CUSTFLD2	CUSTFLD3	CUSTFLD4	CUSTFLD5	CUSTFLD6	CUSTFLD7	CUSTFLD8	CUSTFLD9	CUSTFLD10	CUSTFLD11	CUSTFLD12	CUSTFLD13	CUSTFLD14	CUSTFLD15	JOBDESC	JOBTYPE	JOBSTATUS	JOBSTART	JOBPROJEND	JOBEND	HIDDEN	DELCOUNT
[filter tabbed]CUST
[scratch CUSTNAME]
[scratch order_number][comment][data table=userdb field=customer_number key="[tx-param username]"][/comment][comment]Customer number... just use order number?[/comment]
[tag time]%s[/tag]
[tx-param email]
[scratch ADDR1]
[scratch ADDR2]
[scratch ADDR3]
[scratch ADDR4]
[scratch ADDR5]
[scratch SADDR1]
[scratch SADDR2]
[scratch SADDR3]
[scratch SADDR4]
[scratch SADDR5]
[filter op=mac interpolate=1][tx-param phone_day][/filter]
[filter op=mac interpolate=1][tx-param phone_night][/filter]
[filter op=mac interpolate=1][tx-param fax][/filter]
[filter op=mac interpolate=1][tx-param comments][/filter]
[tx-param fname] [tx-param lname]
[tx-param b_fname] [tx-param b_lname]
Retail
Cash
[if scratch ISTAXABLE]Y[else]N[/else][/if]
[scratch credit_limit]
[scratch resale_number]
[tx-param affiliate]
[if scratch ISTAXABLE][tx-param state][/if]


[tx-param company]
[tx-param fname]
[comment]MIDINIT[/comment]
[tx-param lname]
[comment]CUSTFLD1[/comment]
[tx-param email][comment]CUSTFLD2[/comment]
[comment]CUSTFLD3[/comment]
[tx-param email]
[comment]CUSTFLD5[/comment]
[comment]CUSTFLD6[/comment]
[comment]CUSTFLD7[/comment]
[comment]CUSTFLD8[/comment]
[comment]CUSTFLD9[/comment]
[comment]CUSTFLD10[/comment]
[comment]CUSTFLD11[/comment]
[comment]CUSTFLD12[/comment]
[comment]CUSTFLD13[/comment]
[comment]CUSTFLD14[/comment]
[comment]CUSTFLD15[/comment]
[comment]JOBDESC[/comment]
[comment]JOBDESC[/comment]
[comment]JOBDESC[/comment]
[comment]JOBDESC[/comment]
[comment]JOBDESC[/comment]
[comment]JOBDESC[/comment]
N
__STORE_ID__[/filter]
!TRNS	TRNSID	TRNSTYPE	DATE	ACCNT	NAME	CLASS	AMOUNT	DOCNUM	MEMO	PONUM	CLEAR	TOPRINT	NAMEISTAXABLE	ADDR1	ADDR2	ADDR3	ADDR4	ADDR5	PAYMETH	SHIPVIA	SADDR1	SADDR2	SADDR3	SADDR4	SADDR5
!SPL	SPLID	TRNSTYPE	DATE	ACCNT	NAME	CLASS	AMOUNT	DOCNUM	MEMO	CLEAR	QNTY	PRICE	INVITEM	PAYMETH	TAXABLE	VALADJ	SERVICEDATE	OTHER2	EXTRA
!ENDTRNS
[filter tabbed]TRNS
[scratch order_number]
[scratch TRNSTYPE]
[scratch DATE]
[either]__QB_TRANSACTION_ACCOUNT__[or]Checking[/either]
[scratch CUSTNAME]
__QB_TRANSACTION_CLASS__
[tx-param total_cost]
[scratch order_number]
IC Order Number: [tx-param order_number].  [filter op=mac interpolate=1][tx-param comments][/filter]
[tx-param order_number]
N
N
[scratch NAMEISTAXABLE]
[scratch ADDR1]
[scratch ADDR2]
[scratch ADDR3]
[scratch ADDR4]
[scratch ADDR5]
[filter op=14 interpolate=1][calc]$_ = $p = q{[tx-param payment_method]}; ($_) = /\((.*)\)/; $_ = uc($_); $_ || $p;[/calc][/filter]
[filter op=14 interpolate=1][tx-param shipmode][/filter]
[scratch SADDR1]
[scratch SADDR2]
[scratch SADDR3]
[scratch SADDR4]
[scratch SADDR5][/filter][comment]end [filter tabbed] for TRNS[/comment][loop prefix=ol search="
	fi=orderline
	sf=order_number
	op=eq
	se='[tx-param order_number]'
	rf=*
	"]
SPL	[filter tabbed][fcounter etc/qbline.number]
[scratch TRNSTYPE]
[scratch DATE]
[either][data table=inventory field=account key="[ol-param sku]"][or]__QB_INCOME_ACCOUNT__[or]Other Income[/either]
[scratch CUSTNAME]
__QB_TRANSACTION_CLASS__
-[ol-param subtotal]
[scratch order_number]
[filter interpolate=1 op="strip mac qb_safe"][ol-param description][/filter]
N
-[ol-param quantity]
[ol-param price]
[tmp filter_length][either]@_QB_ITEM_LENGTH_@[or]45[/either][/tmp][filter 
	op="qb_safe [scratch filter_length]" 
	interpolate=1]@_QB_ITEM_PREFIX_@[either]@_QB_SKU_@[or][ol-param sku][/either][/filter]

[if-ol-param taxable][scratch NAMEISTAXABLE][else]N[/else][/if-ol-param]
N


[/filter][/loop][comment]/end [/loop][/comment][if scratch HASSHIPPING]
SPL[filter tabbed]
[fcounter etc/qbline.number]
[scratch TRNSTYPE]
[scratch DATE]
[either]__QB_SHIPPING_ACCOUNT__[or]__QB_INCOME_ACCOUNT__[or]Other Income[/either]
[scratch CUSTNAME]

-[tx-param shipping]
[scratch order_number]
[tx-param shipmode]
N
-1
[tx-param shipping]
[either]@_QB_SHIPPING_ITEM_@[or]Shipping[/either]

N
N


[/filter]
[/if][filter tabbed]SPL
[fcounter etc/qbline.number]
[scratch TRNSTYPE]
[scratch DATE]
[either]__QB_SALES_TAX_ACCOUNT__[or]Sales Tax Payable[/either]
[either][query list=1 sql=|SELECT qb_tax_agency FROM state WHERE country = '[tx-param country]' AND state = '[tx-param state]'|][sql-param qb_tax_agency][/query][or]__QB_SALES_TAX_AGENCY__[or]State Board of Equalization[/either]

-[tx-param salestax]
[scratch order_number]
[tx-param state]
N
-1

[tmp qb_sales_tax_item]__QB_SALES_TAX_ITEM__[/tmp][either][scratch qb_sales_tax_item][or]Sales Tax Item[/either]

N
N


AUTOSTAX[/filter]
ENDTRNS
[/seti]
[calc]
	$Scratch->{qb_trans} =~ s/\r?\n/\r\n/g;
	$Scratch->{qb_trans} =~ s/\s*$/\r\n/;
	return;
[/calc]
Logging transaction to orders/[tx-param order_number].iif: [log umask=022 file=">orders/[tx-param order_number].iif" type=text interpolate=1][scratch qb_trans][/log]
Also appending transaction to orders/0_qb_results.iif: [log umask=022 file="orders/0_qb_results.iif" type=text interpolate=1][scratch qb_trans][/log]


[scratch qb_trans]


[calc]
	for (qw/
			ADDR1
			ADDR2
			ADDR3
			ADDR4
			ADDR5
			CUSTNAME
			DATE
			FILEDATE
			HASSHIPPING
			ISTAXABLE
			NAME
			NAMEISTAXABLE
			SADDR1
			SADDR2
			SADDR3
			SADDR4
			SADDR5
			TRNSTYPE
			qb_trans
			/)
	{
		delete $Scratch->{$_};
	}
	return;
[/calc]
[/tmp][perl]
	my $out = $Scratch->{quickbooks_record};
	$out =~ s/^\s+//mg;
	$out =~ s/\s+$//mg;
	$out =~ s/[\r\n]+/\n/;
	$out =~ s/:\n(1|yes|succe\w+|fail\w+)\n/: $1\n/g;
	return $out;
[/perl]
#### end quickbooks for [tx-param order_number] #####
