2752 - Import json file
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2021-01-26 07:21:05 +01:00
parent 7bd89009cb
commit 8c71d76988
13 changed files with 340 additions and 13 deletions

View File

@ -20,7 +20,8 @@ export default class Field extends FormInput {
super.$onInit();
if (this.info) this.classList.add('has-icons');
this.input.addEventListener('change', () => this.onChange());
this.input.addEventListener('change', event =>
this.onChange(event));
}
set field(value) {
@ -186,10 +187,13 @@ export default class Field extends FormInput {
this.refreshHint();
}
onChange() {
onChange($event) {
// Changes doesn't reflect until appling async
this.$.$applyAsync(() => {
this.emit('change', {value: this.field});
this.emit('change', {
value: this.field,
$event: $event
});
});
}
}

View File

@ -71,10 +71,11 @@ export default class InputFile extends Field {
this.input.click();
}
onChange() {
onChange($event) {
this.emit('change', {
value: this.field,
$files: this.files
$files: this.files,
$event: $event
});
}
}

View File

@ -0,0 +1,48 @@
module.exports = Self => {
Self.remoteMethodCtx('import', {
description: 'Imports the buys from a JSON file',
accessType: 'WRITE',
accepts: [{
arg: 'id',
type: 'number',
required: true,
description: 'The entry id',
http: {source: 'path'}
},
{
arg: 'buys',
type: ['Object'],
description: 'The buys',
}],
returns: {
type: ['Object'],
root: true
},
http: {
path: `/:id/import`,
verb: 'POST'
}
});
Self.import = async(ctx, id) => {
const args = ctx.args;
const models = Self.app.models;
const entry = await models.Entry.findById(id);
console.log(entry);
const buys = [];
for (let buy of args.buys) {
buys.push({
entryFk: entry.id,
itemFk: buy.itemFk,
stickers: 1,
packing: buy.packing,
grouping: buy.grouping,
packageFk: 1
});
}
await models.Buy.create(buys);
};
};

View File

@ -2,4 +2,5 @@ module.exports = Self => {
require('../methods/entry/filter')(Self);
require('../methods/entry/getEntry')(Self);
require('../methods/entry/getBuys')(Self);
require('../methods/entry/import')(Self);
};

View File

@ -0,0 +1,162 @@
<mg-ajax path="dms/upload" options="vnPost"></mg-ajax>
<vn-watcher
vn-id="watcher"
data="$ctrl.dms">
</vn-watcher>
<form
name="form"
ng-submit="$ctrl.onSubmit()"
class="vn-ma-md">
<div class="vn-w-md">
<vn-card class="vn-pa-lg">
<!-- <vn-horizontal>
<vn-textfield
vn-one
vn-focus
label="Reference"
ng-model="$ctrl.dms.reference"
rule>
</vn-textfield>
<vn-autocomplete vn-one
label="Company"
ng-model="$ctrl.dms.companyId"
url="Companies"
show-field="code"
value-field="id">
</vn-autocomplete>
</vn-horizontal>
<vn-horizontal>
<vn-autocomplete vn-one
label="Warehouse"
ng-model="$ctrl.dms.warehouseId"
url="Warehouses"
show-field="name"
value-field="id">
</vn-autocomplete>
<vn-autocomplete vn-one
label="Type"
ng-model="$ctrl.dms.dmsTypeId"
url="DmsTypes"
show-field="name"
value-field="id">
</vn-autocomplete>
</vn-horizontal>
<vn-horizontal>
<vn-textarea
vn-one
label="Description"
ng-model="$ctrl.dms.description"
rule>
</vn-textarea>
</vn-horizontal> -->
<vn-horizontal>
<vn-textfield
vn-one
label="Invoice"
ng-model="$ctrl.import.invoice">
</vn-textfield>
</vn-horizontal>
<vn-horizontal>
<vn-textarea
vn-one
label="Notes"
ng-model="$ctrl.import.description">
</vn-textarea>
</vn-horizontal>
<vn-horizontal>
<vn-input-file
vn-one
label="File"
ng-model="$ctrl.import.file"
on-change="$ctrl.onFileChange($event)"
required="true"
multiple="true">
<append>
<vn-icon vn-none
color-marginal
title="{{$ctrl.contentTypesInfo}}"
icon="info">
</vn-icon>
</append>
</vn-input-file>
</vn-horizontal>
<vn-horizontal>
<table class="vn-table">
<thead>
<tr>
<th translate center>Item ID</th>
<th translate expand>Description</th>
<th translate center>Size</th>
<th translate center>Quantity</th>
<th translate center>Stickers</th>
<th translate center>Packing</th>
<th translate center>Grouping</th>
<th translate center>Buying value</th>
<!-- <th translate center>Package</th>
<th translate center>Weight</th>
-->
</tr>
</thead>
<tbody ng-repeat="buy in $ctrl.import.buys">
<tr>
<td title="{{::buy.itemFk}}">
<vn-autocomplete
class="dense"
vn-focus
url="Items"
ng-model="buy.itemFk"
show-field="name"
value-field="id"
search-function="$ctrl.itemSearchFunc($search)"
order="id DESC"
tabindex="1">
<tpl-item>
{{::id}} - {{::name}}
</tpl-item>
</vn-autocomplete>
</td>
<td title="{{::buy.description}}" expand>{{::buy.description | dashIfEmpty}}</td>
<td center title="{{::buy.size}}">{{::buy.size | dashIfEmpty}}</td>
<td center title="{{::buy.quantity}}">{{::line.quantity}}</td>
<td center title="{{::buy.stickers | dashIfEmpty}}">{{::line.stickers | dashIfEmpty}}</td>
<td center>
<vn-chip translate-attr="line.groupingMode == 2 ? {title: 'Minimun amount'} : {title: 'Packing'}" ng-class="{'message': line.groupingMode == 2}">
<span translate>{{::buy.packing | dashIfEmpty}}</span>
</vn-chip>
</td>
<td center>
<vn-chip translate-attr="line.groupingMode == 1 ? {title: 'Minimun amount'} : {title: 'Grouping'}" ng-class="{'message': line.groupingMode == 1}">
<span translate>{{::buy.grouping | dashIfEmpty}}</span>
</vn-chip>
</vn-td>
<td>{{::buy.buyingValue | currency: 'EUR':2}}</td>
<!-- <td center title="{{::line.packageFk | dashIfEmpty}}">{{::line.packageFk | dashIfEmpty}}</td> -->
<!-- <td center title="{{::line.weight}}">{{::line.weight}}</td>
<td center>
<vn-chip translate-attr="line.groupingMode == 1 ? {title: 'Minimun amount'} : {title: 'Grouping'}" ng-class="{'message': line.groupingMode == 1}">
<span translate>{{::line.grouping | dashIfEmpty}}</span>
</vn-chip>
</vn-td>
<td center title="{{::line.buyingValue | currency: 'EUR':2}}">{{::line.buyingValue | currency: 'EUR':2}}</td>
<td center title="{{::line.quantity * line.buyingValue | currency: 'EUR':2}}">{{::line.quantity * line.buyingValue | currency: 'EUR':2}}</td>
<td center title="{{::line.price2 | currency: 'EUR':2}}">{{::line.price2 | currency: 'EUR':2}}</td>
<td center title="{{::line.price3 | currency: 'EUR':2}}">{{::line.price3 | currency: 'EUR':2}}</td> -->
</tr>
</tbody>
</table>
</vn-horizontal>
</vn-card>
<vn-button-bar>
<vn-submit
label="Upload">
</vn-submit>
<vn-button
class="cancel"
label="Cancel"
ui-sref="entry.card.buy.index"></vn-button>
</vn-button>
</vn-button-bar>
</div>
</form>

View File

@ -0,0 +1,76 @@
import ngModule from '../../module';
import Section from 'salix/components/section';
import './style.scss';
class Controller extends Section {
constructor($element, $) {
super($element, $);
this.import = {
file: '',
invoice: null,
buys: []
};
}
onFileChange($event) {
const input = $event.target;
const file = input.files[0];
const reader = new FileReader();
reader.onload = event =>
this.fillData(event.target.result);
reader.readAsText(file, 'UTF-8');
}
fillData(raw) {
this.import.file = raw;
const data = JSON.parse(raw);
const [invoice] = data.invoices;
this.$.$applyAsync(() => {
this.import.invoice = invoice.id_invoice;
this.import.description = invoice.tx_awb;
const boxes = invoice.boxes;
const buys = [];
for (let box of boxes) {
for (let product of box.products) {
const packing = product.nu_stems_bunch * product.nu_bunches;
buys.push({
description: product.nm_product,
size: product.nu_length,
packing: packing,
grouping: product.nu_stems_bunch,
buyingValue: parseFloat(product.mny_rate_stem)
});
}
}
this.import.buys = buys;
});
}
onSubmit() {
const params = {buys: this.import.buys};
const query = `Entries/${this.entry.id}/import`;
return this.$http.post(query, params).then(() => {
this.vnApp.showSuccess(this.$t('Data saved!'));
});
}
itemSearchFunc($search) {
return /^\d+$/.test($search)
? {id: $search}
: {name: {like: '%' + $search + '%'}};
}
}
Controller.$inject = ['$element', '$scope'];
ngModule.vnComponent('vnEntryBuyImport', {
template: require('./index.html'),
controller: Controller,
bindings: {
worker: '<'
}
});

View File

@ -0,0 +1,7 @@
vn-ticket-request {
.vn-textfield {
margin: 0!important;
max-width: 100px;
}
}

View File

@ -1 +0,0 @@

View File

@ -0,0 +1,11 @@
<div fixed-bottom-right>
<vn-vertical style="align-items: center;">
<a ui-sref="entry.card.buy.import" vn-bind="+">
<vn-button class="round md vn-mb-sm"
icon="publish"
vn-tooltip="Import buys"
tooltip-position="left">
</vn-button>
</a>
</vn-vertical>
</div>

View File

@ -1,7 +1,7 @@
import ngModule from '../module';
import ngModule from '../../module';
import Section from 'salix/components/section';
ngModule.vnComponent('vnEntryBuy', {
ngModule.vnComponent('vnEntryBuyIndex', {
template: require('./index.html'),
controller: Section,
bindings: {

View File

@ -13,4 +13,6 @@ import './card';
import './note';
import './summary';
import './log';
import './buy';
import './buy/index';
import './buy/import';

View File

@ -11,7 +11,7 @@
],
"card": [
{"state": "entry.card.basicData", "icon": "settings"},
{"state": "entry.card.buy", "icon": "icon-lines"},
{"state": "entry.card.buy.index", "icon": "icon-lines"},
{"state": "entry.card.observation", "icon": "insert_drive_file"},
{"state": "entry.card.log", "icon": "history"}
]
@ -75,14 +75,30 @@
"state": "entry.card.log",
"component": "vn-entry-log",
"description": "Log"
}, {
"url" : "/buy",
},
{
"url": "/buy",
"state": "entry.card.buy",
"component": "vn-entry-buy",
"abstract": true,
"component": "ui-view"
},
{
"url" : "/index",
"state": "entry.card.buy.index",
"component": "vn-entry-buy-index",
"description": "Buy",
"params": {
"entry": "$ctrl.entry"
}
},
{
"url" : "/import",
"state": "entry.card.buy.import",
"component": "vn-entry-buy-import",
"description": "Import buys",
"params": {
"entry": "$ctrl.entry"
}
}
]
}