diff --git a/accounts.go b/accounts.go index 23eff8d..dc2665c 100644 --- a/accounts.go +++ b/accounts.go @@ -131,11 +131,20 @@ func DeleteAccount(a *Account) error { return err } - // Re-parent splits to this account's parent account - _, err = transaction.Exec("UPDATE splits SET AccountId=? WHERE AccountId=?", a.ParentAccountId, a.AccountId) - if err != nil { - transaction.Rollback() - return err + if a.ParentAccountId != -1 { + // Re-parent splits to this account's parent account if this account isn't a root account + _, err = transaction.Exec("UPDATE splits SET AccountId=? WHERE AccountId=?", a.ParentAccountId, a.AccountId) + if err != nil { + transaction.Rollback() + return err + } + } else { + // Delete splits if this account is a root account + _, err = transaction.Exec("DELETE FROM splits WHERE AccountId=?", a.AccountId) + if err != nil { + transaction.Rollback() + return err + } } // Re-parent child accounts to this account's parent account diff --git a/static/accounts.js b/static/accounts.js index 43c1718..9fe57cd 100644 --- a/static/accounts.js +++ b/static/accounts.js @@ -35,6 +35,12 @@ const getAccountDisplayList = function(account_list, includeRoot, rootName) { }; const AccountCombobox = React.createClass({ + getDefaultProps: function() { + return { + includeRoot: true, + rootName: "New Root Account" + }; + }, handleAccountChange: function(account) { if (this.props.onSelect != null && account.hasOwnProperty('AccountId') && @@ -43,7 +49,7 @@ const AccountCombobox = React.createClass({ } }, render: function() { - var accounts = getAccountDisplayList(this.props.accounts, true, "New Root Account"); + var accounts = getAccountDisplayList(this.props.accounts, this.props.includeRoot, this.props.rootName); return ( @@ -115,46 +134,46 @@ const NewAccountModal = React.createClass({
+ label="Name" + value={this.state.name} + onChange={this.handleChange} + ref="name" + labelClassName="col-xs-2" + wrapperClassName="col-xs-10"/> + label="Parent Account" + labelClassName="col-xs-2" + wrapperClassName="col-xs-10"> + accounts={this.props.accounts} + account_map={this.props.account_map} + value={this.state.parentaccountid} + onSelect={this.handleParentChange} + ref="parent" /> + label="Security" + labelClassName="col-xs-2" + wrapperClassName="col-xs-10"> + data={this.props.securities} + valueField='SecurityId' + textField='Name' + value={this.state.security} + onSelect={this.handleSecurityChange} + ref="security" /> + label="Account Type" + labelClassName="col-xs-2" + wrapperClassName="col-xs-10"> + data={AccountTypeList} + valueField='TypeId' + textField='Name' + value={this.state.type} + onSelect={this.handleTypeChange} + ref="type" /> @@ -169,6 +188,106 @@ const NewAccountModal = React.createClass({ } }); +const DeleteAccountModal = React.createClass({ + getInitialState: function() { + if (this.props.initialAccount != null) + var accountid = this.props.initialAccount.AccountId; + else if (this.props.accounts.length > 0) + var accountid = this.props.accounts[0].AccountId; + else + var accountid = -1; + return {error: "", + accountid: accountid, + checked: false, + show: false}; + }, + handleCancel: function() { + if (this.props.onCancel != null) + this.props.onCancel(); + }, + handleChange: function(account) { + this.setState({accountid: account.AccountId}); + }, + handleCheckboxClick: function() { + this.setState({checked: !this.state.checked}); + }, + handleSubmit: function() { + if (this.props.account_map.hasOwnProperty(this.state.accountid)) { + if (this.state.checked) { + if (this.props.onSubmit != null) + this.props.onSubmit(this.props.account_map[this.state.accountid]); + } else { + this.setState({error: "You must confirm you wish to delete this account."}); + } + } else { + this.setState({error: "You must select an account."}); + } + }, + componentWillReceiveProps: function(nextProps) { + if (nextProps.show && !this.props.show) { + this.setState(this.getInitialState()); + } + }, + render: function() { + var checkbox = []; + if (this.props.account_map.hasOwnProperty(this.state.accountid)) { + var parentAccountId = this.props.account_map[this.state.accountid].ParentAccountId; + var parentAccount = "will be deleted and any child accounts will become top-level accounts."; + if (parentAccountId != -1) + parentAccount = "and any child accounts will be re-parented to: " + this.props.account_map[parentAccountId].Name; + + var warningString = "I understand that deleting this account cannot be undone and that all transactions " + parentAccount; + checkbox = (); + } + var warning = []; + if (this.state.error.length != "") { + warning = ( + Error: {this.state.error} + ); + } + + return ( + + + Delete Account + + + {warning} +
+ + + + {checkbox} + +
+ + + + + + +
+ ); + } +}); + const AccountTreeNode = React.createClass({ mixins: [CollapsibleMixin], getCollapsibleDOMNode: function() { @@ -268,7 +387,8 @@ const AccountsTab = React.createClass({ getInitialState: function() { return { selectedAccount: null, - creatingNewAccount: false + creatingNewAccount: false, + deletingAccount: false }; }, handleNewAccount: function() { @@ -278,16 +398,24 @@ const AccountsTab = React.createClass({ console.log("handleEditAccount"); }, handleDeleteAccount: function() { - console.log("handleDeleteAccount"); + this.setState({deletingAccount: true}); }, handleCreationCancel: function() { this.setState({creatingNewAccount: false}); }, + handleDeletionCancel: function() { + this.setState({deletingAccount: false}); + }, handleCreateAccount: function(account) { if (this.props.onCreateAccount != null) this.props.onCreateAccount(account); this.setState({creatingNewAccount: false}); }, + handleRemoveAccount: function(account) { + if (this.props.onDeleteAccount != null) + this.props.onDeleteAccount(account); + this.setState({deletingAccount: false}); + }, handleAccountSelected: function(account) { this.setState({selectedAccount: account}); }, @@ -300,11 +428,19 @@ const AccountsTab = React.createClass({ +