
    Li                        d Z ddlmZmZmZmZ ddlZddlZddlmZm	Z	m
Z
  ee          Zej                            d          Zd Ze                    d          d             Ze                    d	          d
             Ze                    d          d             Ze                    ddg          d             Ze                    d          d             Zedk    re                    ddd           dS dS )u&   Finance Butler Dashboard — Flask App    )Flaskrender_templatejsonifyrequestN)datetimedate	timedeltaz,~/.hermes/profiles/finance-butler/finance.dbc                  Z    t          j        t                    } t           j        | _        | S )N)sqlite3connectDB_PATHRowrow_factory)cons    ./home/debian/Projects/finance-dashboard/app.pyget_dbr   
   s    
/'
"
"CkCOJ    /c                       t          d          S )Nz
index.html)r    r   r   indexr      s    <(((r   z/api/summaryc                  6   t                      } |                                 }t          j                    }|                    d          }|j         d|j         }|                    d|f                                          d         pd}|                    d                                          }|dk    r|n|r|d         n|}||k    r1|                    d|f                                          d         pd}|                    d|f          	                                }|                    d          	                                }	|                    d          	                                }
i }|
D ]M}|                    d	|d
         f                                          }|rt          |          nd ||d
         <   N|                                  g }d}|
D ]}|                    |d
                   }|r|d         |d         z  nd }|r||z  }|                    |d
         |d         |d         |d         |r|d         nd |r|d         nd |r|d         nd |d           t          ||d |D             d |	D             ||dd          S )Nz%Y-%m-z.SELECT SUM(amount) FROM expenses WHERE month=?r   HSELECT month FROM expenses WHERE month != '' ORDER BY month DESC LIMIT 1z
        SELECT parent, SUM(amount) as total
        FROM expenses WHERE month=?
        GROUP BY parent ORDER BY total DESC
    z
        SELECT month, SUM(amount) as total
        FROM expenses
        WHERE date >= date('now', '-6 months')
        GROUP BY month ORDER BY month
    zSELECT * FROM holdingsz
            SELECT close_price, change_amount, change_percent
            FROM stock_prices WHERE symbol=? ORDER BY date DESC LIMIT 1
        symbolclose_pricesharesname
total_costchange_amountchange_percent)r   r   r   r   r   r    r!   market_valuec                 ,    g | ]}t          |          S r   dict.0rs     r   
<listcomp>zapi_summary.<locals>.<listcomp>Y   s    111!d1gg111r   c                 ,    g | ]}t          |          S r   r$   r&   s     r   r)   zapi_summary.<locals>.<listcomp>Z   s    777Qa777r   iq )display_monthmonth_total	by_parentmonths_trendholdingstotal_market_valuer   )r   cursorr   todaystrftimeyearmonthexecutefetchonefetchallr%   closegetappendr   )r   curr2   
this_monthcurrent_monthr,   latest_month_rowr+   r-   r.   r/   priceshpholdings_datar0   
market_vals                    r   api_summaryrE      s$   
((C
**,,CJLLE((J z11EK11M++8=:J hjj   {{R hjj  &11__MMRb;u;KA;N;NhuM%%kk<}>N
 

(**Q  	
   		  $8::	  ;;   	 	
 
  {{344==??HF 5 5KK  k^  &XZZ 	
 *+4d1gggq{IIKKKM  JJq{##9:Da&844
 	-*,kfIkL//0:1]++d34>Q//$56@a 011D&	
 	
 		 		 		 		 &"11y11177,777!0    r   z/api/expensesc                  8   t                      } |                                 }t          j                            dd          }t          j                    }|r|dk    rht                      }|                    d                                          }|	                                 |r|d         n|
                    d          }|                    d|f                                          }| 	                                 t          d |D                       S )	Nr5    currentr   r   %Y-%-mz
        SELECT date, account, amount, category, parent, note
        FROM expenses WHERE month=?
        ORDER BY date DESC, id DESC
        LIMIT 200
    c                 ,    g | ]}t          |          S r   r$   r&   s     r   r)   z api_expenses.<locals>.<listcomp>s       ***DGG***r   )r   r1   r   argsr:   r   r2   r6   r7   r9   r3   r8   r   )r   r<   r5   r2   con2rowrowss          r   api_expensesrP   `   s    
((C
**,,CLWb))EJLLE <EY&&xxlleffooqq

;A5>>(#;#;;; 
  
 8:: 	 IIKKK**T***+++r   z/api/add_expensePOST)methodsc                     t           j        } t          j                    }t	                      }|                                }|                    d|                     dd          f                                          }|r|d         nd}|                    d|                     d|	                    d                    |                     d|	                    d	                    |                     d
d          | d         |                     dd          ||                     dd          |                     dd          f           |
                                 |                                 t          ddi          S )Nz1SELECT parent FROM categories WHERE subcategory=?categoryrG   r   z
        INSERT INTO expenses (date, month, account, amount, category, parent, note, person)
        VALUES (?,?,?,?,?,?,?,?)
    r   z%Y/%m/%dr5   rI   accountu   現金amountnotepersonzamiokT)r   jsonr   r2   r   r1   r6   r:   r7   r3   commitr9   r   )datar2   r   r<   parents        r   api_add_expenser_   u   sL   <DJLLE
((C
**,,C[[;dhhzRT>U>U=W hjj  !(VAYYbFKK  	
3344%..2233H%%XR  6""	
   JJLLLIIKKKD$<   r   z/api/categoriesc                      t                      } |                     d                                          }|                                  t	          d |D                       S )NzMSELECT subcategory, parent, type FROM categories ORDER BY parent, subcategoryc                 ,    g | ]}t          |          S r   r$   r&   s     r   r)   z"api_categories.<locals>.<listcomp>   rK   r   )r   r6   r8   r9   r   )r   rO   s     r   api_categoriesrb      sS    
((C;;fggpprrDIIKKK**T***+++r   __main__z0.0.0.0i  F)hostportdebug)__doc__flaskr   r   r   r   r   osr   r   r	   __name__apppath
expanduserr   r   router   rE   rP   r_   rb   runr   r   r   <module>rp      s   , , : : : : : : : : : : : :         . . . . . . . . . .eHoo
'

K
L
L  
 3) ) ) >J J JX ?, , ,( x00! ! 10!6 , , , zGGeG44444 r   