| 1 |
(in-package :bos.web) |
|---|
| 2 |
|
|---|
| 3 |
(enable-interpol-syntax) |
|---|
| 4 |
|
|---|
| 5 |
(defclass boi-handler (page-handler) |
|---|
| 6 |
()) |
|---|
| 7 |
|
|---|
| 8 |
(defmethod authorized-p ((handler boi-handler)) |
|---|
| 9 |
(bos.m2:editor-p (bknr-session-user))) |
|---|
| 10 |
|
|---|
| 11 |
(defclass create-contract-handler (boi-handler) |
|---|
| 12 |
()) |
|---|
| 13 |
|
|---|
| 14 |
(defun find-sponsor (sponsor-id) |
|---|
| 15 |
(let ((sponsor (store-object-with-id (parse-integer sponsor-id :junk-allowed t)))) |
|---|
| 16 |
(unless sponsor |
|---|
| 17 |
(error "Invalid sponsor ID")) |
|---|
| 18 |
(unless (subtypep (type-of sponsor) 'sponsor) |
|---|
| 19 |
(error "Invalid sponsor ID (wrong type)")) |
|---|
| 20 |
sponsor)) |
|---|
| 21 |
|
|---|
| 22 |
(defmethod handle ((handler create-contract-handler)) |
|---|
| 23 |
(with-xml-error-handler () |
|---|
| 24 |
(with-query-params (num-sqm country sponsor-id name paid expires) |
|---|
| 25 |
(setf num-sqm (ignore-errors (parse-integer num-sqm :junk-allowed t))) |
|---|
| 26 |
(unless num-sqm |
|---|
| 27 |
(error "missing or invalid num-sqm parameter")) |
|---|
| 28 |
(unless country |
|---|
| 29 |
(error "missing country code")) |
|---|
| 30 |
(setf expires (if expires |
|---|
| 31 |
(or (parse-integer expires :junk-allowed t) |
|---|
| 32 |
(error "invalid expires parameter")) |
|---|
| 33 |
7)) |
|---|
| 34 |
(setf expires (+ (get-universal-time) (* expires 60 60 24))) |
|---|
| 35 |
(let* ((sponsor (if sponsor-id |
|---|
| 36 |
(find-sponsor sponsor-id) |
|---|
| 37 |
(make-sponsor :full-name name))) |
|---|
| 38 |
(contract (make-contract sponsor num-sqm :expires expires :paidp paid))) |
|---|
| 39 |
(with-xml-response (:root-element "response") |
|---|
| 40 |
(with-element "status" |
|---|
| 41 |
(attribute "success" 1) |
|---|
| 42 |
(if sponsor-id |
|---|
| 43 |
(text "Contract has been created") |
|---|
| 44 |
(text "Contract and sponsor have been created"))) |
|---|
| 45 |
(with-element "contract" |
|---|
| 46 |
(attribute "id" (store-object-id contract))) |
|---|
| 47 |
(unless sponsor-id |
|---|
| 48 |
(with-element "sponsor" |
|---|
| 49 |
(attribute "id" (store-object-id sponsor)) |
|---|
| 50 |
(attribute "master-code" (sponsor-master-code sponsor))))))))) |
|---|
| 51 |
|
|---|
| 52 |
(defclass pay-contract-handler (boi-handler) |
|---|
| 53 |
()) |
|---|
| 54 |
|
|---|
| 55 |
(defmethod handle ((handler pay-contract-handler)) |
|---|
| 56 |
(with-xml-error-handler () |
|---|
| 57 |
(with-query-params (contract-id name) |
|---|
| 58 |
(unless contract-id |
|---|
| 59 |
(error "missing contract-id parameter")) |
|---|
| 60 |
(let ((contract (get-contract (or (ignore-errors (parse-integer contract-id)) |
|---|
| 61 |
(error "bad contract-id parameter"))))) |
|---|
| 62 |
(when (contract-paidp contract) |
|---|
| 63 |
(error "contract has already been paid for")) |
|---|
| 64 |
(with-transaction (:contract-paid) |
|---|
| 65 |
(contract-set-paidp contract (format nil "~A: manually set paid by ~A" |
|---|
| 66 |
(format-date-time) |
|---|
| 67 |
(user-login (bknr.web:bknr-session-user)))) |
|---|
| 68 |
(when name |
|---|
| 69 |
(setf (user-full-name (contract-sponsor contract)) name)))) |
|---|
| 70 |
(with-xml-response (:root-element "response") |
|---|
| 71 |
(with-element "status" |
|---|
| 72 |
(attribute "success" 1) |
|---|
| 73 |
(text "Contract has been marked as paid for")))))) |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
(defclass cancel-contract-handler (boi-handler) |
|---|
| 77 |
()) |
|---|
| 78 |
|
|---|
| 79 |
(defmethod handle ((handler cancel-contract-handler)) |
|---|
| 80 |
(with-xml-error-handler () |
|---|
| 81 |
(with-query-params (contract-id) |
|---|
| 82 |
(unless contract-id |
|---|
| 83 |
(error "missing contract-id parameter")) |
|---|
| 84 |
(let ((contract (get-contract (or (ignore-errors (parse-integer contract-id)) |
|---|
| 85 |
(error "bad contract-id parameter"))))) |
|---|
| 86 |
(when (contract-paidp contract) |
|---|
| 87 |
(error "contract has already been paid for")) |
|---|
| 88 |
(delete-object contract) |
|---|
| 89 |
(with-xml-response (:root-element "response") |
|---|
| 90 |
(with-element "status" |
|---|
| 91 |
(attribute "success" 1) |
|---|
| 92 |
(text "Contract has been deleted"))))))) |
|---|