Skip to content

Commit

Permalink
Merge pull request #9 from malubsi/backendrefactoring
Browse files Browse the repository at this point in the history
lista e form de item de compras
  • Loading branch information
adlerosn committed May 24, 2017
2 parents c9c6cd2 + 13dbdc7 commit e217f71
Show file tree
Hide file tree
Showing 16 changed files with 237 additions and 120 deletions.
3 changes: 3 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { PageFormSupermercado } from '../pages/FormSupermercado/main';
import { PageListaCompras } from '../pages/ListaCompras/main';
import { PageFormCompras } from '../pages/FormCompras/main';
import { PageListaItensCompra } from '../pages/ListaItensCompra/main';
import { PageFormItensCompra } from '../pages/FormItensCompra/main';

import { OrmDatabase } from '../persistence/OrmDatabase.service';
import { Relatorios } from '../providers/Relatorios.service';
Expand All @@ -43,6 +44,7 @@ import { CrudSupermercado } from '../providers/CrudSupermercado.service';
PageListaCompras,
PageFormCompras,
PageListaItensCompra,
PageFormItensCompra,
],
imports: [
BrowserModule,
Expand All @@ -64,6 +66,7 @@ import { CrudSupermercado } from '../providers/CrudSupermercado.service';
PageListaCompras,
PageFormCompras,
PageListaItensCompra,
PageFormItensCompra,
],
providers: [
StatusBar,
Expand Down
34 changes: 1 addition & 33 deletions src/pages/FormCompras/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,7 @@ export class PageFormCompras extends PageForm<Planejamento> {
public toastCtrl: ToastController,
){
super(navCtrl, navParams, toastCtrl);
for(let fieldIndex in this.fields){
this.fields[fieldIndex]['data'] = this.editing[this.fields[fieldIndex]['entity']]
}
for(let field of this.fields){
if(field['type'] == 'select'){
for(let fieldIndex in this.fields){
let saved = this.fields[fieldIndex]['data']
let avail = this.selectablesFor(field['entity'])
if(saved && saved.id){
for(let placeable of avail){
if(placeable.id == saved.id){
this.fields[fieldIndex]['data'] = placeable
}
}
}
}
}
if(field['type'] == 'selectmultiple'){
for(let fieldIndex in this.fields){
let saveds = this.fields[fieldIndex]['data']
let avail = this.selectablesFor(field['entity'])
for(let savi in saveds){
if(saveds[savi] && saveds[savi].id){
for(let placeable of avail){
if(placeable.id == saveds[savi].id){
this.fields[fieldIndex]['data'][savi] = placeable
}
}
}
}
}
}
}
this.postSuper();
}
public textOption(field: string, item: any): string{
let r = '';
Expand Down
22 changes: 22 additions & 0 deletions src/pages/FormItensCompra/main.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ComponentFixture, async } from '@angular/core/testing';
import { TestUtils } from '../../test';
import { PageFormItensCompra } from './main';

let fixture: ComponentFixture<PageFormItensCompra> = null;
let instance: any = null;

declare var describe: any;
declare var it: any;
declare var expect: any;
declare var beforeEach: any;

describe('Pages: Forms: Item de compra', () => {
beforeEach(async(() => TestUtils.beforeEachCompiler([PageFormItensCompra]).then(compiled => {
fixture = compiled.fixture;
instance = compiled.instance;
})));

it('should create the page', async(() => {
expect(instance).toBeTruthy();
}));
});
41 changes: 41 additions & 0 deletions src/pages/FormItensCompra/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { NavParams } from 'ionic-angular';
import { ToastController } from 'ionic-angular';
import { PageForm } from '../generico_form/main';
import { Necessidade } from '../../entities/Necessidade';

@Component({
selector: 'page-form',
templateUrl: '../generico_form/main.html'
})
export class PageFormItensCompra extends PageForm<Necessidade> {
constructor(
public navCtrl: NavController,
public navParams: NavParams,
public toastCtrl: ToastController,
){
super(navCtrl, navParams, toastCtrl);
this.postSuper();
}
public textOption(field: string, item: any): string{
let r = '';
if(field == 'produto'){ r = item['nome'] }
return r;
};
public titulo: string = "item de compra";
public fields: object[] = [
{
type: 'select',
label: 'Produto',
entity: 'produto',
verifywith: 'truthy'
},
{
type: 'number',
label: 'Quantidade',
entity: 'quantidade',
verifywith: 'gtz'
},
];
}
34 changes: 1 addition & 33 deletions src/pages/FormProduto/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,7 @@ export class PageFormProduto extends PageForm<Produto> {
public toastCtrl: ToastController,
){
super(navCtrl, navParams, toastCtrl);
for(let fieldIndex in this.fields){
this.fields[fieldIndex]['data'] = this.editing[this.fields[fieldIndex]['entity']]
}
for(let field of this.fields){
if(field['type'] == 'select'){
for(let fieldIndex in this.fields){
let saved = this.fields[fieldIndex]['data']
let avail = this.selectablesFor(field['entity'])
if(saved && saved.id){
for(let placeable of avail){
if(placeable.id == saved.id){
this.fields[fieldIndex]['data'] = placeable
}
}
}
}
}
if(field['type'] == 'selectmultiple'){
for(let fieldIndex in this.fields){
let saveds = this.fields[fieldIndex]['data']
let avail = this.selectablesFor(field['entity'])
for(let savi in saveds){
if(saveds[savi] && saveds[savi].id){
for(let placeable of avail){
if(placeable.id == saveds[savi].id){
this.fields[fieldIndex]['data'][savi] = placeable
}
}
}
}
}
}
}
this.postSuper();
}
public textOption(field: string, item: any): string{
let r = '';
Expand Down
4 changes: 1 addition & 3 deletions src/pages/FormSupermercado/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class PageFormSupermercado extends PageForm<Supermercado> {
public toastCtrl: ToastController,
){
super(navCtrl, navParams, toastCtrl);
for(let fieldIndex in this.fields){
this.fields[fieldIndex]['data'] = this.editing[this.fields[fieldIndex]['entity']]
}
this.postSuper();
}
public textOption(field: string, item: any): string{ return ''; };
public titulo: string = "supermercado";
Expand Down
4 changes: 1 addition & 3 deletions src/pages/FormUnidadeMedida/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export class PageFormUnidadeMedida extends PageForm<UnidadeMedida> {
public toastCtrl: ToastController,
){
super(navCtrl, navParams, toastCtrl);
for(let fieldIndex in this.fields){
this.fields[fieldIndex]['data'] = this.editing[this.fields[fieldIndex]['entity']]
}
this.postSuper();
}
public textOption(field: string, item: any): string{ return ''; };
public titulo: string = "unidade de medida";
Expand Down
18 changes: 17 additions & 1 deletion src/pages/ListaCompras/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,29 @@ export class PageListaCompras extends PageLista<Planejamento> {
this.contextoExibe['personalizado'].push({
text: 'Gerenciar itens de compra',
role: 'manage',
icon: 'settings',
icon: 'list',
handler: () => {
this.navCtrl.push(PageListaItensCompra,{
'sujeito': this.getClicado()
})
}
})
this.contextoExibe['personalizado'].push({
text: 'Anotar preços',
role: 'manage',
icon: 'cash',
handler: () => {
this.navCtrl.setRoot(PageListaCompras)
}
})
this.contextoExibe['personalizado'].push({
text: 'Ir às compras',
role: 'manage',
icon: 'cart',
handler: () => {
this.navCtrl.setRoot(PageListaCompras)
}
})
}
public items: Planejamento[] = new Array();
public icone: string = "pricetags";
Expand Down
67 changes: 49 additions & 18 deletions src/pages/ListaItensCompra/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { AlertController } from 'ionic-angular';
import { LoadingController } from 'ionic-angular';
import { Toast } from "@ionic-native/toast";
import { Necessidade } from '../../entities/Necessidade';
import { Planejamento } from '../../entities/Planejamento';
import { CrudNecessidade } from '../../providers/CrudNecessidade.service';
import { CrudSupermercado } from '../../providers/CrudSupermercado.service';
import { CrudPlanejamento } from '../../providers/CrudPlanejamento.service';
import { CrudProduto } from '../../providers/CrudProduto.service';
import { PageLista } from '../generico_lista/main';
import { PageFormCompras } from '../FormCompras/main';
import { PageFormItensCompra } from '../FormItensCompra/main';

@Component({
selector: 'page-lista',
Expand All @@ -24,21 +26,41 @@ export class PageListaItensCompra extends PageLista<Necessidade> {
public loadingCtrl: LoadingController,
public necessidadeCrud: CrudNecessidade,
public navParams: NavParams,
public supermercadoCrud: CrudSupermercado,
public produtoCrud: CrudProduto,
public planejamentoCrud: CrudPlanejamento,
){
super(
navCtrl,
actionSheetCtrl,
alert,
toast,
loadingCtrl,
necessidadeCrud
necessidadeCrud,
);
this.contextoExibe['personalizado'].push({
text: 'Gerenciar itens de compra',
role: 'manage',
icon: 'settings',
handler: () => {}
this.subject = navParams.get('sujeito')
}
public subject: Planejamento;
refreshList(){
this.mostraCarregando();
this.planejamentoCrud.recarregarUm(this.subject).then( planejamento => {
this.subject = planejamento
this.necessidadeCrud.recarregarAlguns(planejamento.necessidades).then(necessidades => {
this.produtoCrud.listar().then(produtos => {
for(let necessidade in necessidades){
for(let produto of produtos){
if(
necessidades[necessidade].produto
&&
necessidades[necessidade].produto.id == produto.id
){
necessidades[necessidade].produto = produto
}
}
}
this.items = this.ordenaExibicao(necessidades);
this.escondeCarregando()
})
});
})
}
public items: Necessidade[] = new Array();
Expand All @@ -50,25 +72,34 @@ export class PageListaItensCompra extends PageLista<Necessidade> {
"artigoentidade": "o item de compra",
"capitalEntidade": "Item de compra",
};
public texto(item: Necessidade):string{ return item.produto.nome; };
public posTexto(item: Necessidade):string{ return (item.quantidade || '?') + " itens"; };
public texto(item: Necessidade):string{ return (item.produto || {nome:'<deletado>'}).nome; };
public posTexto(item: Necessidade):string{
return ((item.quantidade || '?')+" "+
(((item.produto || {unidadeMedida:null}).unidadeMedida || {nome:'iten'}).nome)
+(item.quantidade>1 ? 's' : ''))
};
public add():void{
let novo = this.crud.criar()
novo.planejamento = this.subject
this.abreEdicao(novo);
};
public abreEdicao(item: Necessidade):void{
this.supermercadoCrud.listar().then(
supermercados => {
this.navCtrl.push(PageFormCompras,{
this.produtoCrud.listar().then(
produto => {
this.navCtrl.push(PageFormItensCompra,{
sujeito: item,
crud: this.crud,
crud: this.necessidadeCrud,
selecionaveis: {
'supermercados': supermercados
'produto': produto
},
});
}
)
};
public ordenaExibicao(items: Necessidade[]):Necessidade[]{
items.sort((a, b) => {
if (a.produto.nome > b.produto.nome) { return 1; }
if (a.produto.nome < b.produto.nome) { return -1; }
if ((a.produto || {nome:'<deletado>'}).nome > (b.produto || {nome:'<deletado>'}).nome) { return 1; }
if ((a.produto || {nome:'<deletado>'}).nome < (b.produto || {nome:'<deletado>'}).nome) { return -1; }
if (a.quantidade > b.quantidade) { return 1; }
if (a.quantidade < b.quantidade) { return -1; }
return 0;
Expand Down
1 change: 1 addition & 0 deletions src/pages/generico_form/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
{{ field.label }}
</ion-label>
<ion-input *ngIf="field.type == 'textbox'" type="text" [(ngModel)]="field.data"></ion-input>
<ion-input *ngIf="field.type == 'number'" type="number" [(ngModel)]="field.data"></ion-input>
<ion-select *ngIf="field.type == 'select'" [(ngModel)]="field.data" multiple="false">
<ion-option *ngFor="let item of selectablesFor(field.entity)" [value]="item">
{{ textOption(field.entity, item) }}
Expand Down

0 comments on commit e217f71

Please sign in to comment.