Skip to content

Commit

Permalink
add functionality: create product from invoice-tool
Browse files Browse the repository at this point in the history
  • Loading branch information
a-jaillet authored and Embraser01 committed Feb 8, 2021
1 parent 5ef1570 commit 57d483c
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
<button mat-stroked-button (click) = "isPrint = !isPrint" >Afficher les articles par date</button>
<button mat-stroked-button (click) = "isPrintSum = !isPrintSum" >Afficher les articles par somme</button>
<button mat-stroked-button (click) = "onCopyData()" >Copier les articles par somme</button>
</div>
</div>

<p></p>


<div fxLayout="column" fxFlexAlign="center" fxLayoutGap="20px">
<div fxFlexAlign="center" fxLayout="row" fxLayout.xs="column" fxLayoutGap.gt-xs="40px">
<button mat-stroked-button (click) = "onNgSubmitProducts()" >Soumettre les produits</button>
<button mat-stroked-button (click) = "downloadCSV = !downloadCSV" >Télécharger un fichier csv</button>
</div>
</div>

<p></p>
<mat-progress-bar mode="indeterminate" *ngIf="isLoading"></mat-progress-bar>

<div fxLayout="column" fxFlexAlign="center" fxLayoutGap="20px" *ngIf="downloadCSV">
<div fxFlexAlign="center" fxLayout="row" fxLayout.xs="column" fxLayoutGap.gt-xs="40px">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component, OnInit } from '@angular/core';
import { ArticlesService } from '../services/articles.service';
import { Article } from '../article';
import { Subscription } from 'rxjs';

import { ProductsSubmit } from '../services/products-submit.service';

@Component({
selector: 'invoice-tool-articles',
Expand All @@ -22,9 +22,15 @@ export class ArticlesComponent implements OnInit{

isPrint: boolean;
isPrintSum: boolean;
isLoading: boolean;
downloadCSV: boolean;

constructor( private articleService: ArticlesService){}

constructor( private articleService: ArticlesService,
private productsSubmit: ProductsSubmit,)
{
this.isLoading = false;
}

ngOnInit(): void {
this.articleSubscription = this.articleService.articlesSubject.subscribe(
Expand Down Expand Up @@ -84,6 +90,14 @@ export class ArticlesComponent implements OnInit{
a.click();
window.URL.revokeObjectURL(url);
a.remove();
}
}

async onNgSubmitProducts(): Promise<void> {

this.isLoading = true;
await this.productsSubmit.submitProducts(this.articlessum);
this.isLoading = false;
return Promise.resolve();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import { Article } from './article';
import { InvoicesService } from './services/invoices.service';
import { ParseService } from './services/parse.service';
import { ArticlesService } from './services/articles.service';
import { ProductsSubmit } from './services/products-submit.service';
import { ProductsService } from '../api-services/products.service';
import { ShelvesService } from '../api-services/shelves.service';
import { ProvidersService } from '../api-services/providers.service';


@NgModule({
declarations: [
Expand All @@ -27,7 +32,11 @@ import { ArticlesService } from './services/articles.service';
InvoicesService,
ParseService,
ArticlesService,
Article
ProductsSubmit,
ProductsService,
ShelvesService,
ProvidersService,
Article,
]
})
export class InvoiceToolModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export class ArticlesService {
articlessumSubject = new Subject<Article[]>();
articlessum: Article[];

constructor(private parseService: ParseService) {
constructor(private parseService: ParseService)
{
this.articles = [];
this.articlessum = [];
}
Expand Down Expand Up @@ -51,4 +52,6 @@ export class ArticlesService {
this.emitArticlesumSubject();
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ export class InvoicesService {
}

async submitInvoices(): Promise<void>{
for(let i=0; i<this.invoices.length; i++){
await this.parseService.fromFiletoText(this.invoices[i]);
}
await this.parseService.fromFilestoText(this.invoices);
return Promise.resolve();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ export class ParseService {



async fromFiletoText(invoice: File): Promise<void> {
async fromFilestoText(invoices: File[]): Promise<void> {

const url = URL.createObjectURL(invoice);
const oneinvoice = await this.gettext(url);
for(let i=0;i<oneinvoice.length;i++){
this.textinvoices.push(oneinvoice[i]);
for(let i=0; i<invoices.length; i++){
const url = URL.createObjectURL(invoices[i]);
const oneinvoice = await this.gettext(url);
for(let i=0;i<oneinvoice.length;i++){
this.textinvoices.push(oneinvoice[i]);
}
URL.revokeObjectURL(url);
}
URL.revokeObjectURL(url);
this.parsePDF();
return Promise.resolve();
}
Expand Down Expand Up @@ -78,11 +80,10 @@ export class ParseService {
}

for(let i = 0; i<listinvoices.length; i++){
listinvoices[i][1] = listinvoices[i][1].replace('19,5L','20L'); //Grolsch
const listinvoicesparse = listinvoices[i][1].split(' ');
toremove = [];
for(let i = 0; i<listinvoicesparse.length; i++){
if(listinvoicesparse[i]=='' || listinvoicesparse[i].indexOf(',')>=0 || listinvoicesparse[i].indexOf('°')>=0){
if(listinvoicesparse[i]=='' || listinvoicesparse[i].indexOf('°')>=0){
toremove.push(i);
}
}
Expand All @@ -103,6 +104,33 @@ export class ParseService {
listarticles.push(listinvoicesparse[i]);
}
}

//Parse , and manage GROLSCH exception
for(let i=0; i<listarticles.length; i++){
const onearticleparse = listarticles[i].split(' ');
if(onearticleparse.indexOf("GROLSCH")<0){
toremove = [];
for(let i = 0; i<onearticleparse.length; i++){
if(onearticleparse[i]=='' || onearticleparse[i].indexOf(',')>=0){
toremove.push(i);
}
}
for(let i = toremove.length-1;i>=0;i--){
onearticleparse.splice(toremove[i],1);
}
}
else{
if(onearticleparse[0] == "GROLSCH") onearticleparse.splice(onearticleparse.length-1,1);
else{
onearticleparse.splice(0,5);
onearticleparse.splice(onearticleparse.length-1,1);
}
if(onearticleparse[onearticleparse.length-1].indexOf(",")>0){
onearticleparse[onearticleparse.length-1] = onearticleparse[onearticleparse.length-1].substring(0,onearticleparse[onearticleparse.length-1].length-2) ;
}
}
listarticles[i]=onearticleparse.join(' ');
}

const listarticlesparse = [];
//Cols and Colis
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { Injectable } from '@angular/core';
import { Product } from '../../products/product.model';
import { Article } from '../article';
import { Shelf } from '../../shelves/shelf.model';
import { Provider } from '../../providers/provider.model';

import { ShelvesService } from '../../api-services/shelves.service';
import { ProductsService } from '../../api-services/products.service';
import { ProvidersService } from '../../api-services/providers.service';

import { Router } from '@angular/router';
import { ToasterService } from '../../../core/services/toaster.service';
import { MoleculerListOptions } from 'src/app/shared/models/MoleculerWrapper';



@Injectable()

export class ProductsSubmit {

BASE_PRODUCT = {} as Product;
BASE_MOLECULERLISTOPTIONS = {} as MoleculerListOptions;

constructor( private productsService: ProductsService,
private providersService: ProvidersService,
private shelvesService: ShelvesService,
private toasterService: ToasterService,
private router: Router, ){
}

async submitProducts(articles: Article[]): Promise<void> {

const options = this.BASE_MOLECULERLISTOPTIONS;

for(let j=0; j<articles.length; j++){
//Check that the product does not exist in db

options.pageSize = (await this.productsService.list()).total;
const exist = await this.productsService.list(options).then(function(value){
for(let i=0; i<value.rows.length; i++){
if(value.rows[i].name === articles[j].name){
return true;
}
}
return false;
});
if(!exist){
//Create Product in db
await this.productsService.create(await this.getProductFromArticle(articles[j]).then(
function(value){
return value;
}))
this.toasterService.showToaster("Le produit " + articles[j].name + "est ajouté à la DB");
}
}
this.router.navigate(['/inventory-management/products']);
return Promise.resolve();
}

async getProductFromArticle(
article: Article,
): Promise<Product> {
const product = this.BASE_PRODUCT;
const options = this.BASE_MOLECULERLISTOPTIONS;

let oneShelf: Shelf;
let oneShelfName;
if(article.name.indexOf("FUT")>0) oneShelfName = "Pressions";
else if(article.name.indexOf("SIROP")>0) oneShelfName = "Sirops";
else if(article.name.indexOf("VC")>0 ||article.name.indexOf("VP")>0 ||article.name.indexOf("12X")>0 ||article.name.indexOf("24X")>0) oneShelfName = "Bouteilles";
else oneShelfName = "Autre";

options.pageSize = (await this.shelvesService.list()).total;
await this.shelvesService.list(options).then(function(value){
for(let i=0; i<value.rows.length; i++){
if(value.rows[i].name == oneShelfName){
oneShelf = value.rows[i];
break;
}
}
});

let oneProvider: Provider;
options.pageSize = (await this.providersService.list()).total;
await this.providersService.list(options).then(function(value){
for(let i=0; i<value.rows.length; i++){
if(value.rows[i].name == "France Boissons"){
oneProvider = value.rows[i];
break;
}
}
});

product.name = article.name;
product.provider = oneProvider._id;
product.shelf = oneShelf._id;
product.conversions = [];
return product;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Product } from '../product.model';
styleUrls: ['./list.component.scss'],
})
export class ListComponent implements OnInit, AfterViewInit {

// Modified because it didn't work on my laptop
displayedColumns = ['name', 'action'];
dataSource: ProductsDataSource;

Expand All @@ -31,7 +31,6 @@ export class ListComponent implements OnInit, AfterViewInit {
}

ngAfterViewInit(): void {
// Server-side search
fromEvent(this.input.nativeElement, 'keyup').pipe(
debounceTime(300),
distinctUntilChanged(),
Expand All @@ -47,7 +46,7 @@ export class ListComponent implements OnInit, AfterViewInit {
merge(this.sort.sortChange, this.paginator.page).pipe(tap(() => this.loadProductsPage())).subscribe();
}

loadProductsPage() {
loadProductsPage(): void {
this.dataSource.loadProducts({
pageSize: this.paginator.pageSize,
page: this.paginator.pageIndex + 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ export function getProductModel(
export function getProductFromForm(form: FormGroup, originalProduct?: Product): Product {
const value = form.value;
const original = originalProduct || BASE_PRODUCT;

return {
_id: original._id,
...value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class ListComponent implements OnInit, AfterViewInit {
ngOnInit(): void {
this.dataSource = new MoleculerDataSource<Shelf>(this.shelfService);
this.dataSource.load();
}
}

ngAfterViewInit(): void {
// Server-side search
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const model = {
name: {
type: String, required: true, min: 3, text: true,
},
image: { type: String },
image: { type: String, text: true },
used: { type: Boolean, default: false },
conversions: [new mongoose.Schema({
displayName: { type: String },
Expand All @@ -30,7 +30,7 @@ const model = {
joi: Joi.object({
_id: MONGO_ID.strip(), // Remove _id from the object
name: Joi.string().min(3).required(),
image: Joi.string().uri(),
image: Joi.string().uri().allow(null),
conversions: Joi.array().unique('unit').items(Joi.object({
displayName: Joi.string(),
preferred: Joi.bool(),
Expand Down

0 comments on commit 57d483c

Please sign in to comment.