diff --git a/components/estimates.vue b/components/estimates.vue
index 4a9091d..75c6e6b 100644
--- a/components/estimates.vue
+++ b/components/estimates.vue
@@ -76,7 +76,28 @@ let estimates = ref([])
let estimate = ref()
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() {
diff --git a/components/new/new.vue b/components/new/new.vue
index 39aedfe..95f3ac3 100644
--- a/components/new/new.vue
+++ b/components/new/new.vue
@@ -57,7 +57,10 @@ class="bi bi-plus" viewBox="0 0 16 16">
+ :loan="loan"
+ :downpayment="estimate.price - loan.amount"
+ :token="token"
+ :estimate="estimate"/>
diff --git a/migrations/0_29092022_setup_tables.sql b/migrations/0_29092022_setup_tables.sql
index df65f37..795fca6 100644
--- a/migrations/0_29092022_setup_tables.sql
+++ b/migrations/0_29092022_setup_tables.sql
@@ -137,7 +137,7 @@ CREATE TABLE fee (
CREATE TABLE fee_template (
id INT AUTO_INCREMENT,
user_id INT NOT NULL,
- branch_id INT NOT NULL,
+ branch_id INT,
amount INT NOT NULL,
perc SMALLINT NOT NULL,
/* Percentage of sale price instead of amount */
@@ -145,7 +145,7 @@ CREATE TABLE fee_template (
notes VARCHAR(255) NOT NULL,
name VARCHAR(30) NOT NULL,
/* Group heading shown in report */
- category VARCHAR(60) NOT NULL,
+ category VARCHAR(60) NOT NULL DEFAULT "",
auto BOOLEAN NOT NULL,
/* If fee should be automatically applied */
PRIMARY KEY (`id`),
diff --git a/skouter.go b/skouter.go
index ad14984..b9e8fb2 100644
--- a/skouter.go
+++ b/skouter.go
@@ -349,10 +349,14 @@ func getFees(db *sql.DB, loan int) ([]Fee, error) {
func fetchFeesTemp(db *sql.DB, user int, branch int) ([]FeeTemplate, error) {
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 {
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)
}
+// 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) {
var mi MI
@@ -1501,6 +1547,10 @@ func api(w http.ResponseWriter, r *http.Request) {
r.Method == http.MethodGet &&
guard(r, 1):
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) &&
r.Method == http.MethodGet &&
guard(r, 1):