 program define taxsimlocal30
version 14.0
/*

net from http://www.nber.org/stata
net describe taxsimlocal30
net install taxsimlocal30

*/
display "begin taxsimlocal30.ado on $S_DATE (version 30.1)"
syntax [, Http Ftp local Replace Secondary Long Interest Wages]

capture drop _taxsimid
gen  _taxsimid = _n

tempfile original
save `original'

/* c(tmpdir) only sometimes shows trailing slash*/
local servername `c(username)'
local tmpdir `c(tmpdir)'
local tmpdir: subinstr local tmpdir "p/" "p"
local results  "`tmpdir'/`c(username)'-results.raw"
local txpydata "`tmpdir'/`c(username)'-txpydata.raw"
local msg      "`tmpdir'/`c(username)'-msg.txt"
local ftp      "`tmpdir'/`c(username)'-ftp.txt"
capture { rm `txpydata')
capture { rm `results' }
capture ( rm `msg'     }
capture { rm `ftp'     }

di "`txpydata' " _n "`results' " _n "`msg' " _n "`ftp' "

if `"`secondary'"' == `"secondary"' {
     gen mtr = 86
     display "Marginal rate with respect to secondary earner"
}
if `"`wages'"' == `"wages"' {
     gen mtr = 11
     display "Marginal rate with respect to overall earnings"
}
if `"`interest'"' == `"interest"' {
     gen mtr = 14
     display "Marginal rate with respect to interest income"
}
if `"`long'"' == `"long"' {
     gen mtr = 70
     display "Marginal rate with respect to long term gains"
}
if `"`full'"' == `"full"' {
     local idtl 2
                        v40 v41
     local addvars v10-v41
     display "Full Intermediate Calculations Requested"
}
      otheritem childcare mortgage mtr idtl businc profinc scorp

capture gen state=0
local not_found " "
foreach X of local txpyvars {
     capture confirm var `X'
     if _rc > 0 {
          di "`X' " _continue
          local not_found  are not found, and default to zero.
     }
     else {
          capture assert `X' != .
          if _rc >0 di "`X' has one or more missing values. taxsim30.ado is aborting."
          local tosend `tosend' `X'
     }

}
if _N==0 {
  display "Aborting:  No observations in data set"
  exit
}

di "`not_found'"

quietly summarize mstat
local obs=r(N)
di "Write `obs' obs to `txpydata'"
outsheet `tosend' using `txpydata', replace comma
   di "executing local taxsim30 "
   ! taxsim30 >`results' <`txpydata'
     ! powershell.exe SelectString -path `results' -Pattern TAXSIM

   if "`http'" == "http" {

quietly import delimited using "`results'",clear varnames(1) delimiter(",")
di "`obs' obs received"
quietly describe
if ( `r(N)'==0 ) {
   di "No calculations were returned by the tax calculator."
   if `"`http'"' == `"http"'   di "Possibly Powershell is not available?"
   if `"`local'"' == `"local"' di "Is taxsim30.exe file in the executable path?"
   if `"`ftp'"' == `"ftp"'     di "Is there a firewall problem? See: http://www.nber.org/taxsim/ftp-problems.html"
   exit
}
if ( `r(N)'-`obs'!=0 & `r(N)'- 51*`obs'!=0 ) {
   di "`obs' records sent and `r(N)' records received"
   di "An error occurred. There should be a report in
   di "this log or in the last few lines of `results'.
   di "Consult the Notes and Support section of the help file at:"
   di "http://www.nber.org/taxsim/stata/ "
   exit
}
label variable state  "state id"
label variable fiitax "Federal Income Tax"
label variable siitax "State Income Tax"
label variable fica   "OASDI and HI Payroll Tax"
label variable frate  "IIT marginal rate"
label variable srate  "state marginal rate"
label variable ficar  "SS marginal rate"
if `"`full'"' == `"full"' {
  label variable v10 "Federal AGI"
  label variable v11 "UI in AGI"
  label variable v12 "Social Security in AGI"
  label variable v13 "Zero Bracket Amount"
  label variable v14 "Personal Exemptions"
  label variable v15 "Exemption Phaseout"
  label variable v16 "Deduction Phaseout"
  label variable v17 "Deductions allowed"
  label variable v18 "Federal Taxable Income"
  label variable v19 "Federal Regular Tax"
  label variable v20 "Exemption Surtax"
  label variable v21 "General Tax Credit"
  label variable v22 "Child Tax Credit (as adjusted)"
  label variable v23 "Refundable Part of Child Tax Credit"
  label variable v24 "Child Care Credit"
  label variable v25 "Earned Income Credit"
  label variable v26 "Income for the Alternative Minimum Tax"
  label variable v27 "AMT Liability (addition to regular tax)"
  label variable v28 "Income Tax before Credits"
  label variable v29 "FICA"
  label variable v30 "State Household Income"
  label variable v31 "State Rent Payments"
  label variable v32 "State AGI"
  label variable v33 "State Exemption amount"
  label variable v34 "State Standard Deduction"
  label variable v35 "State Itemized Deductions"
  label variable v36 "State Taxable Income"
  label variable v37 "State Property Tax Credit"
  label variable v38 "State Child Care Credit"
  label variable v39 "State EITC "
  label variable v40 "State Total Credits"
  label variable v41 "State Bracket Rate"
  label variable v42 "QBI Deduction"
}
drop if state == -1
if "`replace'" == "replace" {
   di "original `original'"
   capture drop _merge
   capture merge 1:m _taxsimid state year using `original'
   if _rc != 0 {
     di "Merge failed " _rc
     tab _merge
   }
}
else {
   use `original',clear
   di "Save results to `tmpdir'/results"
   save "`tmpdir'/results",replace
}
capture drop _taxsimid _merge
end