 program define taxsim30
version 14.0
/*

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

*/
display "begin taxsim30.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
     local addvars v10 v11 v12 v13 v14 v15 v16 v17 v18 v19      ///
                        v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 ///
                        v30 v31 v32 v33 v34 v35 v36 v37 v38 v39 ///
                        v40 v41
     local addvars v10-v41
     display "Full Intermediate Calculations Requested"
}
local txpyvars  _taxsimid year   state  mstat     page     sage depx dep13     ///
      dep17     dep18     pwages swages dividends intrec   stcg ltcg otherprop ///
      nonprop   pensions  gssi   ui     transfers rentpaid proptax             ///
      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
if `"`local'"' == `"local"' {        //
   di "executing local taxsim30 "
   ! taxsim30 >`results' <`txpydata'
}                                    //
else if  "`c(os)'"=="Windows" {      //
   di "Windows OS"                   //
   if `"`http'"' == `"http"' {       //
     di "Using http to www.nber.org/uptest/cgi.cgi"   //
     ! powershell.exe -windowstyle=hidden -nologo -command ///
     [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; ///
     @{userfile="`txpydata'";submit='upload'};   ///
     Invoke-WebRequest -method post              ///
     -uri http://wwwdev.nber.org/uptest/cgi.cgi  ///
     -ContentType text/plain                     ///
     -infile `txpydata'                          ///
     -outfile `results'                          //
     ! powershell.exe SelectString -path `results' -Pattern TAXSIM
   }                                                      //
   else {                                                 //
     di "using Windows ftp to taxsim@taxsimftp.nber.org"  //
     file open out using `ftp', write replace             //
     if `"`debug'"' == `"debug"' {                        //
        local slashck "/K"                                //
        di "Type EXIT in DOS window to return to stata."  //
        file write out "debug" _n                         //
     }                                                    //

     file write out "open taxsimftp.nber.org" _char(10)   //
     file write out "user taxsim 02138" _char(10)         //
     file write out "lcd `tmpdir'" _char(10)              //
     file write out "cd tmp" _char(10)                    //
     file write out "type ascii" _char(10)                //
     file write out "put `txpydata' `c(username)'" _char(10) //
     file write out "get `c(username)'.txm30 `results'" _char(10) //
     file write out "get `c(username)'.msg `msg'" _char(10) //
     file write out "quit" _char(10)                       //
     file close out                                        //
     ! cmd /c  ftp -n -s:`ftp' -w:12888                    //
   }                                                       //
}                                                          //
else if "`c(os)'" == "Unix" {                              //
   di "Unix"                                               //
   if "`http'" == "http" {
     di "curl to https://www.nber.org/taxsim/taxsim30/cgi30.cgi" //
     ! curl -F '`txpydata'=@/tmp/txpydata.raw' "https://www.nber.org/taxsim/taxsim30/cgi30.cgi >`results' //
   }                                                      //
   else  {                                                //
     di "curl to taxsim@taxsimftp.nber.org/tmp/`servername'"               //
     ! curl -u taxsim:02138 -T `txpydata' "ftp://taxsimftp.nber.org/tmp/`servername'" //
     ! curl -u taxsim:02138 "ftp://taxsimftp.nber.org/tmp/`servername'.txm30" -o `results' //
     ! curl -u taxsim:02138 "ftp://taxsimftp.nber.org/tmp/`servername'.msg" -o `msg' //
   } //
} //
capture confirm file "`msg'"         //
if _rc==0 {                          //
    type `msg'                       //
}                                    //

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