Skip to content

Commit

Permalink
feat(purchase): purchase client creates orders
Browse files Browse the repository at this point in the history
This commit hooks up the client services with the database to ensure
that purchase orders can be posted to the server.  In order to
accomplish this, I changed the schema slightly to account for the
new `payment_method` field.  This field might need to be an ID instead
of a text field, but this works for a first implementation until we
decide upon the value of the method.

Next, a purchase receipt should be implemented followed by end to end
tests.
  • Loading branch information
jniles committed Jun 27, 2016
1 parent f5ee72d commit 019eae9
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 19 deletions.
38 changes: 36 additions & 2 deletions client/src/js/services/PurchaseOrderService.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
angular.module('bhima.services')
.service('PurchaseOrderService', PurchaseOrderService);

PurchaseOrderService.$inject = [ 'PrototypeApiService' ];
PurchaseOrderService.$inject = [
'PrototypeApiService', 'SessionService'
];

/**
* @class PurchaseOrderService
Expand All @@ -10,8 +12,40 @@ PurchaseOrderService.$inject = [ 'PrototypeApiService' ];
* @description
* Connects client controllers with the purchase order backend.
*/
function PurchaseOrderService(Api) {
function PurchaseOrderService(Api, Session) {
var service = new Api('/purchases/');

// bind public methods to the instance
service.create = create;

/**
* @method create
*
* @description
* Preprocesses purchase order data for submission to the server
*/
function create(data) {

// loop through the items ensuring that they are properly formatted for
// inserting into the database. We only want to send minimal information
// to the server.
// Technically, this filtering is also done on the server, but this reduces
// bandwidth required for the POST request.
data.items = data.items.map(function (item) {
delete item._initialised;
delete item._invalid;
delete item._valid;
delete item.code;
delete item.description;
delete item.unit;
return item;
});

// automatically set the currency_id to the enterprise currency
data.currency_id = Session.enterprise.currency_id;

return Api.create.call(service, data);
}

return service;
}
3 changes: 3 additions & 0 deletions client/src/js/services/receipts/modal/receiptModal.tmpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
ng-src="{{ReceiptCtrl.receipt}}"
type="application/pdf"
style="width : 100%; height : 50vh; margin-bottom : -5px; border : none;">
<style>
#zoom-toolbar { display: none; }
</style>
</iframe>
</div>

Expand Down
3 changes: 1 addition & 2 deletions client/src/partials/purchases/create/PurchaseOrderForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ function PurchaseOrderFormService(Inventory, AppCache, Store, Pool, PurchaseOrde

// the order details
this.details = {
payment_method: 1,
payment_method: 'FORM.LABELS.ON_DELIVERY',
date: new Date(),
cost: 0,
description: null
};

// the supplier is null
Expand Down
10 changes: 5 additions & 5 deletions client/src/partials/purchases/create/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div class="container-fluid">

<!--
top description panel for the purchase order.details
top note panel for the purchase order.details
inspired by the patient invoice page's ergonomics
-->
<div class="row">
Expand Down Expand Up @@ -52,19 +52,19 @@

<div class="radio" style="margin-top:0;">
<label>
<input type="radio" name="paymentMethod" id="on-purchase" value="0" ng-model="PurchaseCtrl.order.details.payment_method" required>
<input type="radio" name="paymentMethod" id="on-purchase" value="FORM.LABELS.ON_PURCHASE" ng-model="PurchaseCtrl.order.details.payment_method" required>
{{ "FORM.LABELS.ON_PURCHASE" | translate }}
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="paymentMethod" id="on-delivery" value="1" ng-model="PurchaseCtrl.order.details.payment_method" required>
<input type="radio" name="paymentMethod" id="on-delivery" value="FORM.LABELS.ON_DELIVERY" ng-model="PurchaseCtrl.order.details.payment_method" required>
{{ "FORM.LABELS.ON_DELIVERY" | translate }}
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="paymentMethod" id="other" value="2" ng-model="PurchaseCtrl.order.details.payment_method" required>
<input type="radio" name="paymentMethod" id="other" value="FORM.LABELS.OTHER" ng-model="PurchaseCtrl.order.details.payment_method" required>
{{ "FORM.LABELS.OTHER" | translate }}
</label>
</div>
Expand All @@ -89,7 +89,7 @@
ng-class="{'has-error' : PurchaseOrderForm.$submitted && PurchaseOrderForm.notes.$invalid }">
<label class="control-label">{{ "FORM.LABELS.NOTES" | translate }}</label>
<textarea
ng-model="PurchaseCtrl.order.details.description"
ng-model="PurchaseCtrl.order.details.note"
name="notes"
class="form-control"
rows="4"
Expand Down
13 changes: 13 additions & 0 deletions client/src/partials/purchases/create/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ function PurchaseOrderController(Purchases, PurchaseOrder, Suppliers, Notify, Se
vm.gridApi.core.scrollTo(firstInvalidItem);
return;
}

// copy the purchase order object into something that can be sent to the server
var order = angular.copy(vm.order.details);
order.items = angular.copy(vm.order.store.data);

return Purchases.create(order)
.then(function (res) {
console.log(res);

// reset the module
clear(form);
})
.catch(Notify.handleError);
}

// fired whenever an input in the grid is changed.
Expand Down
3 changes: 3 additions & 0 deletions server/controllers/finance/purchases.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ function create(req, res, next) {
data.date = new Date(data.date);
}

data.user_id = req.session.user.id;
data.project_id = req.session.project.id;

const sql =
'INSERT INTO purchase SET ?';

Expand Down
21 changes: 11 additions & 10 deletions server/models/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1452,16 +1452,17 @@ CREATE TABLE `province` (
DROP TABLE IF EXISTS `purchase`;

CREATE TABLE `purchase` (
`uuid` BINARY(16) NOT NULL,
`project_id` SMALLINT(5) UNSIGNED NOT NULL,
`reference` INT(10) UNSIGNED NOT NULL DEFAULT 0,
`cost` DECIMAL(19,4) UNSIGNED NOT NULL DEFAULT 0.0,
`currency_id` TINYINT(3) UNSIGNED NOT NULL,
`supplier_uuid` BINARY(16) DEFAULT NULL,
`date` DATETIME NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_id` SMALLINT(5) UNSIGNED NOT NULL,
`note` TEXT,
`uuid` BINARY(16) NOT NULL,
`project_id` SMALLINT(5) UNSIGNED NOT NULL,
`reference` INT(10) UNSIGNED NOT NULL DEFAULT 0,
`cost` DECIMAL(19,4) UNSIGNED NOT NULL DEFAULT 0.0,
`currency_id` TINYINT(3) UNSIGNED NOT NULL,
`supplier_uuid` BINARY(16) DEFAULT NULL,
`date` DATETIME NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_id` SMALLINT(5) UNSIGNED NOT NULL,
`payment_method` TEXT,
`note` TEXT,
PRIMARY KEY (`uuid`),
UNIQUE KEY `purchase_1` (`project_id`, `reference`),
KEY `project_id` (`project_id`),
Expand Down

0 comments on commit 019eae9

Please sign in to comment.