@@ -76,7 +76,28 @@ let estimates = ref([]) | |||||
let estimate = ref() | let estimate = ref() | ||||
function newFee(fee, isDebit) { | function newFee(fee, isDebit) { | ||||
this.edit = null | |||||
if (!isDebit) { | |||||
fee.amount = -1 * fee.amount || 0 | |||||
fee.perc = -1 * fee.perc || 0 | |||||
} | |||||
fetch(`/api/fee`, | |||||
{method: 'POST', | |||||
body: JSON.stringify(fee), | |||||
headers: { | |||||
"Accept": "application/json", | |||||
"Authorization": `Bearer ${props.token}`, | |||||
}, | |||||
}).then(resp => { | |||||
if (resp.ok && resp.status == 200) { | |||||
return | |||||
} else { | |||||
// resp.text().then(t => this.errors = [t]) | |||||
// window.location.hash = 'new' | |||||
resp.text().then(t => console.log(t)) | |||||
} | |||||
}) | |||||
edit.value = null | |||||
} | } | ||||
function newType() { | function newType() { | ||||
@@ -57,7 +57,10 @@ class="bi bi-plus" viewBox="0 0 16 16"> <path d="M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 | |||||
@continue="generate" | @continue="generate" | ||||
/> | /> | ||||
<loan-summary v-if="hash == '#new/summary'" | <loan-summary v-if="hash == '#new/summary'" | ||||
:loan="loan" :downpayment="estimate.price - loan.amount" :token="token" :estimate="estimate"/> | |||||
:loan="loan" | |||||
:downpayment="estimate.price - loan.amount" | |||||
:token="token" | |||||
:estimate="estimate"/> | |||||
</div> | </div> | ||||
</template> | </template> | ||||
@@ -137,7 +137,7 @@ CREATE TABLE fee ( | |||||
CREATE TABLE fee_template ( | CREATE TABLE fee_template ( | ||||
id INT AUTO_INCREMENT, | id INT AUTO_INCREMENT, | ||||
user_id INT NOT NULL, | user_id INT NOT NULL, | ||||
branch_id INT NOT NULL, | |||||
branch_id INT, | |||||
amount INT NOT NULL, | amount INT NOT NULL, | ||||
perc SMALLINT NOT NULL, | perc SMALLINT NOT NULL, | ||||
/* Percentage of sale price instead of amount */ | /* Percentage of sale price instead of amount */ | ||||
@@ -145,7 +145,7 @@ CREATE TABLE fee_template ( | |||||
notes VARCHAR(255) NOT NULL, | notes VARCHAR(255) NOT NULL, | ||||
name VARCHAR(30) NOT NULL, | name VARCHAR(30) NOT NULL, | ||||
/* Group heading shown in report */ | /* Group heading shown in report */ | ||||
category VARCHAR(60) NOT NULL, | |||||
category VARCHAR(60) NOT NULL DEFAULT "", | |||||
auto BOOLEAN NOT NULL, | auto BOOLEAN NOT NULL, | ||||
/* If fee should be automatically applied */ | /* If fee should be automatically applied */ | ||||
PRIMARY KEY (`id`), | PRIMARY KEY (`id`), | ||||
@@ -349,10 +349,14 @@ func getFees(db *sql.DB, loan int) ([]Fee, error) { | |||||
func fetchFeesTemp(db *sql.DB, user int, branch int) ([]FeeTemplate, error) { | func fetchFeesTemp(db *sql.DB, user int, branch int) ([]FeeTemplate, error) { | ||||
var fees []FeeTemplate | var fees []FeeTemplate | ||||
rows, err := db.Query( | |||||
"SELECT * FROM fee_template " + | |||||
"WHERE user_id = ? OR branch_id = ?", | |||||
user, branch) | |||||
query := `SELECT | |||||
id, user_id, COALESCE(branch_id, 0), amount, perc, type, notes, name, | |||||
category, auto | |||||
FROM fee_template | |||||
WHERE user_id = ? OR branch_id = ? | |||||
` | |||||
rows, err := db.Query(query, user, branch) | |||||
if err != nil { | if err != nil { | ||||
return nil, fmt.Errorf("Fee template query error %v", err) | return nil, fmt.Errorf("Fee template query error %v", err) | ||||
@@ -396,6 +400,48 @@ func getFeesTemp(w http.ResponseWriter, db *sql.DB, r *http.Request) { | |||||
json.NewEncoder(w).Encode(fees) | json.NewEncoder(w).Encode(fees) | ||||
} | } | ||||
// Fetch fees from the database | |||||
func createFeesTemp(w http.ResponseWriter, db *sql.DB, r *http.Request) { | |||||
var fee FeeTemplate | |||||
var query string | |||||
var row *sql.Row | |||||
var err error | |||||
claims, err := getClaims(r) | |||||
// var id int // Inserted estimate's id | |||||
err = json.NewDecoder(r.Body).Decode(&fee) | |||||
if err != nil { w.WriteHeader(422); return } | |||||
query = `INSERT INTO fee_template | |||||
( | |||||
user_id, | |||||
branch_id, | |||||
amount, | |||||
perc, | |||||
type, | |||||
notes, | |||||
name, | |||||
auto | |||||
) | |||||
VALUES (?, NULL, ?, ?, ?, ?, ?, ?) | |||||
RETURNING id | |||||
` | |||||
row = db.QueryRow(query, | |||||
claims.Id, | |||||
fee.Amount, | |||||
fee.Perc, | |||||
fee.Type, | |||||
fee.Notes, | |||||
fee.Name, | |||||
fee.Auto, | |||||
) | |||||
err = row.Scan(&fee.Id) | |||||
if err != nil { w.WriteHeader(500); return } | |||||
json.NewEncoder(w).Encode(fee) | |||||
} | |||||
func getMi(db *sql.DB, loan int) (MI, error) { | func getMi(db *sql.DB, loan int) (MI, error) { | ||||
var mi MI | var mi MI | ||||
@@ -1501,6 +1547,10 @@ func api(w http.ResponseWriter, r *http.Request) { | |||||
r.Method == http.MethodGet && | r.Method == http.MethodGet && | ||||
guard(r, 1): | guard(r, 1): | ||||
getFeesTemp(w, db, r) | getFeesTemp(w, db, r) | ||||
case match(p, "/api/fee", &args) && | |||||
r.Method == http.MethodPost && | |||||
guard(r, 1): | |||||
createFeesTemp(w, db, r) | |||||
case match(p, "/api/estimates", &args) && | case match(p, "/api/estimates", &args) && | ||||
r.Method == http.MethodGet && | r.Method == http.MethodGet && | ||||
guard(r, 1): | guard(r, 1): | ||||