Item: { fields: { name: Fields.text("Name"), stock: Fields.integer("Stock").computed('sum(transactions.quantity)'), price: Fields.money("Price"), transactions: Fields.relation("Transactions", "Transaction", "item") }, referenceName: "name" }
Transaction: { fields: { item: Fields.reference("Item", "Item"), order: Fields.reference("Order", "Order"), orderItem: Fields.reference("Order item", "OrderItem"), quantity: Fields.integer("Quantity") }, showInGrid: ['item', 'order', 'quantity'] }
Order: { fields: { number: Fields.integer("Order #"), date: Fields.date("Date"), total: Fields.money("Total").computed('sum(orderItems.finalPrice)'), orderItems: Fields.relation("Items", "OrderItem", "order") }, beforeSave: function (Entity, Dates, Crud) { if (!Entity.date) { Entity.date = Dates.nowDate(); } return Crud.crudFor('OrderCounter').find({}).then(function (last) { if (!Entity.number) { Entity.number = last[0].number; return Crud.crudFor('OrderCounter').updateEntity({id: last[0].id, number: last[0].number + 1}); } }) }, beforeDelete: function (Entity, Crud, Q) { var crud = Crud.crudFor('OrderItem'); return crud.find({filtering: {order: Entity.id}}).then(function (items) { return Q.all(items.map(function (i) { return crud.deleteEntity(i.id) })); }); }, referenceName: "number", views: { PointOfSale: { customView: 'pos' } } }
OrderItem: { fields: { order: Fields.reference("Order", "Order"), item: Fields.fixedReference("Item", "Item").required(), quantity: Fields.integer("Quantity").required(), finalPrice: Fields.money("Final price").readOnly().addToTotalRow() }, showInGrid: ['item', 'quantity', 'finalPrice'], beforeSave: function (Crud, Entity) { return Crud.crudFor('Item').readEntity(Entity.item.id).then(function (item) { Entity.finalPrice = Entity.quantity * item.price; }) }, afterSave: function (Crud, Entity) { var crud = Crud.crudForEntityType('Transaction'); return removeTransaction(Crud, Entity).then(function () { return crud.createEntity({ order: Entity.order, orderItem: {id: Entity.id}, item: Entity.item, quantity: Entity.quantity * -1 }) }) }, beforeDelete: function (Crud, Entity) { return removeTransaction(Crud, Entity); } }
function removeTransaction(Crud, Entity) { var crud = Crud.crudForEntityType('Transaction'); return crud.find({filtering: {orderItem: Entity.id}}).then(function (transactions) { if (transactions.length) { return crud.deleteEntity(transactions[0].id); } }); }
OrderCounter: { fields: { number: Fields.integer("Counter") } }
extends main include mixins block vars - var hasToolbar = false block content div(ng-app='allcount', ng-controller='EntityViewController') +defaultList() .container.screen-container(ng-cloak) .row(ng-controller="PosController") .col-md-8 .items-bar.row.btn-toolbar(lc-list="'Item'", paging="{}") .col-lg-4.col-md-6.col-xs-12(ng-repeat="item in items") button.btn.btn-lg.btn-block.btn-default(ng-click="addItem(item)") p {{item.name}} p {{(item.price / 100) | currency}} .container-fluid h1 Total: {{viewState.editForm.entity().total/100 | currency}} .row.btn-toolbar .col-md-4 button.btn.btn-lg.btn-danger.btn-block(ng-click="deleteEntity()", ng-disabled="!viewState.formEntityId") Cancel .col-md-4(ng-hide='viewState.isFormEditing') +startFormEditingButton()(ng-disabled="!viewState.formEntityId").btn-block.btn-lg .col-md-4(ng-show='viewState.isFormEditing') +doneFormEditingButton()(ng-disabled="!viewState.formEntityId").btn-block.btn-lg .col-md-4 button.btn.btn-lg.btn-success.btn-block(ng-click="viewState.mode = 'list'; viewState.formEntityId = undefined", ng-disabled="!viewState.formEntityId") Finish .col-md-4 +defaultEditForm()(ng-show="true") +defaultFormTemplate() block js +entityJs() script. angular.module('allcount').controller('PosController', ['$scope', 'lcApi', '$q', function ($scope, lcApi, $q) { $scope.addItem = function (item) { var promise; if (!$scope.viewState.formEntityId) { promise = lcApi.createEntity({entityTypeId: 'Order'}, {}).then(function (orderId) { $scope.navigateTo(orderId) return orderId; }) } else { promise = $q.when($scope.viewState.formEntityId); } promise.then(function (orderId) { return lcApi.findRange({entityTypeId: 'OrderItem'}, {filtering: {order: orderId}}).then(function (items) { var existingOrderItem = _.find(items, function (i) { return i.item.id === item.id; }) return (existingOrderItem ? lcApi.updateEntity({entityTypeId: 'OrderItem'}, { id: existingOrderItem.id, quantity: 1 + existingOrderItem.quantity }) : lcApi.createEntity({entityTypeId: 'OrderItem'}, { order: {id: orderId}, item: item, quantity: 1 }) ).then(function () { return $scope.editForm.reloadEntity(); }) }) }) } }]) style. .items-bar .btn-block { margin-bottom: 10px; }
extends main include mixins
div(ng-app='allcount', ng-controller='EntityViewController')
.container.screen-container(ng-cloak) .row(ng-controller="PosController")
.col-md-8 ... .col-md-4 …
.items-bar.row.btn-toolbar(lc-list="'Item'", paging="{}") .col-lg-4.col-md-6.col-xs-12(ng-repeat="item in items") button.btn.btn-lg.btn-block.btn-default(ng-click="addItem(item)") p {{item.name}} p {{(item.price / 100) | currency}}
.col-lg-4.col-md-6.col-xs-12(ng-repeat="item in items")
.container-fluid h1 Total: {{viewState.editForm.entity().total/100 | currency}}
.row.btn-toolbar .col-md-4 button.btn.btn-lg.btn-danger.btn-block(ng-click="deleteEntity()", ng-disabled="!viewState.formEntityId") Cancel .col-md-4(ng-hide='viewState.isFormEditing') +startFormEditingButton()(ng-disabled="!viewState.formEntityId").btn-block.btn-lg .col-md-4(ng-show='viewState.isFormEditing') +doneFormEditingButton()(ng-disabled="!viewState.formEntityId").btn-block.btn-lg .col-md-4 button.btn.btn-lg.btn-success.btn-block(ng-click="viewState.mode = 'list'; viewState.formEntityId = undefined", ng-disabled="!viewState.formEntityId") Finish
+defaultEditForm()(ng-show="true") +defaultFormTemplate()
block js +entityJs() script. angular.module('allcount').controller('PosController', ['$scope', 'lcApi', '$q', function ($scope, lcApi, $q) { $scope.addItem = function (item) { var promise; if (!$scope.viewState.formEntityId) { promise = lcApi.createEntity({entityTypeId: 'Order'}, {}).then(function (orderId) { $scope.navigateTo(orderId) return orderId; }) } else { promise = $q.when($scope.viewState.formEntityId); } promise.then(function (orderId) { return lcApi.findRange({entityTypeId: 'OrderItem'}, {filtering: {order: orderId}}).then(function (items) { var existingOrderItem = _.find(items, function (i) { return i.item.id === item.id; }) return (existingOrderItem ? lcApi.updateEntity({entityTypeId: 'OrderItem'}, {id: existingOrderItem.id, quantity: 1 + existingOrderItem.quantity}) : lcApi.createEntity({entityTypeId: 'OrderItem'}, {order: {id: orderId}, item: item, quantity: 1}) ).then(function () { return $scope.editForm.reloadEntity(); }) }) }) } }]) style. .items-bar .btn-block { margin-bottom: 10px; }
A.app({ appName: "POS and inventory", appIcon: "calculator", onlyAuthenticated: true, menuItems: [ { name: "Transactions", entityTypeId: "Transaction", icon: "send-o" }, { name: "Items", entityTypeId: "Item", icon: "cubes" }, { name: "Orders", entityTypeId: "Order", icon: "shopping-cart" }, { name: "POS", entityTypeId: "PointOfSale", icon: "calculator" } ], entities: function (Fields) { return { Transaction: { fields: { item: Fields.reference("Item", "Item"), order: Fields.reference("Order", "Order"), orderItem: Fields.reference("Order item", "OrderItem"), quantity: Fields.integer("Quantity") }, showInGrid: ['item', 'order', 'quantity'] }, Item: { fields: { name: Fields.text("Name"), stock: Fields.integer("Stock").computed('sum(transactions.quantity)'), price: Fields.money("Price"), transactions: Fields.relation("Transactions", "Transaction", "item") }, referenceName: "name" }, Order: { fields: { number: Fields.integer("Order #"), date: Fields.date("Date"), total: Fields.money("Total").computed('sum(orderItems.finalPrice)'), orderItems: Fields.relation("Items", "OrderItem", "order") }, beforeSave: function (Entity, Dates, Crud) { if (!Entity.date) { Entity.date = Dates.nowDate(); } return Crud.crudFor('OrderCounter').find({}).then(function (last) { if (!Entity.number) { Entity.number = last[0].number; return Crud.crudFor('OrderCounter').updateEntity({ id: last[0].id, number: last[0].number + 1 }); } }) }, beforeDelete: function (Entity, Crud, Q) { var crud = Crud.crudFor('OrderItem'); return crud.find({filtering: {order: Entity.id}}).then(function (items) { return Q.all(items.map(function (i) { return crud.deleteEntity(i.id) })); }); }, referenceName: "number", views: { PointOfSale: { customView: 'pos' } } }, OrderItem: { fields: { order: Fields.reference("Order", "Order"), item: Fields.fixedReference("Item", "Item").required(), quantity: Fields.integer("Quantity").required(), finalPrice: Fields.money("Final price").readOnly().addToTotalRow() }, showInGrid: ['item', 'quantity', 'finalPrice'], beforeSave: function (Crud, Entity) { return Crud.crudFor('Item').readEntity(Entity.item.id).then(function (item) { Entity.finalPrice = Entity.quantity * item.price; }) }, afterSave: function (Crud, Entity) { var crud = Crud.crudForEntityType('Transaction'); return removeTransaction(Crud, Entity).then(function () { return crud.createEntity({ order: Entity.order, orderItem: {id: Entity.id}, item: Entity.item, quantity: Entity.quantity * -1 }) }) }, beforeDelete: function (Crud, Entity) { return removeTransaction(Crud, Entity); } }, OrderCounter: { fields: { number: Fields.integer("Counter") } }, } }, migrations: function (Migrations) { return [ { name: "demo-records-1", operation: Migrations.insert("Item", [ {id: "1", name: "Snickers", price: 299}, {id: "2", name: "Coffee", price: 199}, {id: "3", name: "Tea", price: 99} ]) }, { name: "demo-records-2", operation: Migrations.insert("Transaction", [ {id: "1", item: {id: "1"}, quantity: "50"}, {id: "2", item: {id: "2"}, quantity: "100"}, {id: "3", item: {id: "3"}, quantity: "200"} ]) }, { name: "order-counter", operation: Migrations.insert("OrderCounter", [ {id: "2", number: 1} ]) } ] } }); function removeTransaction(Crud, Entity) { var crud = Crud.crudForEntityType('Transaction'); return crud.find({filtering: {orderItem: Entity.id}}).then(function (transactions) { if (transactions.length) { return crud.deleteEntity(transactions[0].id); } }); }
Source: https://habr.com/ru/post/272031/
All Articles