item clonation finish

This commit is contained in:
Daniel Herrero 2018-02-22 13:40:23 +01:00
parent 91b7be32fb
commit 10e47acd6b
8 changed files with 98 additions and 6 deletions

View File

@ -2,7 +2,7 @@
display: none;
justify-content: center;
align-items: center;
z-index: 10;
z-index: 11;
position: fixed;
left: 0;
top: 0;

View File

@ -12,7 +12,7 @@
</vn-one>
<vn-horizontal vn-auto pad-large-top>
<vn-one>
<vn-icon vn-tooltip="Clone" pad-medium-top margin-medium-right margin-small-top icon="icon-clone2"></vn-icon>
<vn-icon ng-click="$ctrl.clone()" vn-tooltip="Clone" pad-medium-top margin-medium-right margin-small-top icon="icon-clone2"></vn-icon>
</vn-one>
<vn-one>
<vn-icon ng-click="$ctrl.preview()" vn-tooltip="Preview" pad-medium-top margin-small-top icon="icon-preview"></vn-icon>

View File

@ -6,7 +6,7 @@ class ItemProduct {
}
clone() {
console.log('clone ', this.item.id);
this.ItemList.cloneItem(this.item);
}
preview() {

View File

@ -27,4 +27,16 @@
<vn-item-summary item="$ctrl.itemSelected"></vn-item-summary>
</tpl-body>
</vn-dialog>
<vn-dialog
vn-id="clone"
on-response="$ctrl.onCloneAccept(response)">
<tpl-body>
<p translate>Do you want to clone this item?</p>
</tpl-body>
<tpl-buttons>
<button response="CANCEL" translate>Cancel</button>
<button response="ACCEPT" translate>Yes, clone</button>
</tpl-buttons>
</vn-dialog>
</div>

View File

@ -3,7 +3,9 @@ import './item-product';
import './style.scss';
class ItemList {
constructor($scope) {
constructor($http, $state, $scope) {
this.$http = $http;
this.$state = $state;
this.$scope = $scope;
this.model = {};
this.itemSelected = null;
@ -11,12 +13,26 @@ class ItemList {
search(index) {
index.accept();
}
cloneItem(item) {
this.itemSelected = item;
this.$scope.clone.show();
}
onCloneAccept(response) {
if (response == 'ACCEPT' && this.itemSelected) {
this.$http.post(`/item/api/Items/${this.itemSelected.id}/clone`).then(res => {
if (res && res.data && res.data.id) {
this.$state.go('item.card.tags', {id: res.data.id});
}
});
}
this.itemSelected = null;
}
showItemPreview(item) {
this.itemSelected = item;
this.$scope.preview.show();
}
}
ItemList.$inject = ['$scope'];
ItemList.$inject = ['$http', '$state', '$scope'];
ngModule.component('vnItemList', {
template: require('./list.html'),

View File

@ -17,4 +17,8 @@ Barcode: Código barras
Item barcode: Código de barras del artículo
Changed by: Cambiado por
Action: Acción
Date: Fecha
Date: Fecha
Preview: Vista previa
Clone: Clonar
Do you want to clone this item?: ¿Desea clonar este artículo?
Yes, clone: Si, clonar

View File

@ -0,0 +1,59 @@
var UserError = require('../../../../loopback/common/helpers').UserError;
module.exports = Self => {
Self.remoteMethod('clone', {
description: 'clone item',
accessType: 'WRITE',
accepts: [{
arg: 'id',
type: 'number',
required: true,
description: 'origin itemId',
http: {source: 'path'}
}],
returns: {
arg: 'id',
description: 'new cloned itemId'
},
http: {
path: `/:id/clone`,
verb: 'post'
}
});
Self.clone = async (itemId, callback) => {
let filter = {
where: {
id: itemId
},
include: [
{relation: "itemTag", scope: {order: "priority ASC", include: {relation: "tag"}}}
]
};
try {
let origin = await Self.findOne(filter);
let copy = JSON.parse(JSON.stringify(origin));
delete copy.id;
delete copy.itemTag;
let newItem = await Self.create(copy);
let promises = [];
let createBotanical = `INSERT INTO vn.itemBotanical (itemFk, botanical, genusFk, specieFk)
SELECT ?, botanical, genusFk, specieFk
FROM vn.itemBotanical ib WHERE itemFk = ?`;
promises.push(Self.rawSql(createBotanical, [newItem.id, origin.id]));
let createTags = `INSERT INTO vn.itemTag (itemFk, tagFk, value, priority)
SELECT ?, tagFk, value, priority
FROM vn.itemTag WHERE itemFk = ?`;
promises.push(Self.rawSql(createTags, [newItem.id, origin.id]));
await Promise.all(promises);
return newItem.id;
} catch (err) {
throw new UserError(err);
}
};
};

View File

@ -1,5 +1,6 @@
module.exports = function(Self) {
require('../methods/item/filter.js')(Self);
require('../methods/item/clone.js')(Self);
Self.validatesPresenceOf('name', {message: 'Cannot be blank'});
Self.validatesPresenceOf('originFk', {message: 'Cannot be blank'});