import { Component , OnInit } from '@angular/core' ; import { StepState , TdLoadingService , LoadingType , LoadingMode } from "@covalent/core" ; import { RetailShopsService , RetailShop } from "../../../services/shop.service" ; import { Observable } from "rxjs" ; import { DistributorService , ItemsService , Product , Stock , DistributorBill , DistributorBillsService , StocksService , Distributor } from "../../../services/items.service" ; import { MdSnackBar } from "@angular/material" ; @ Component ( { selector : 'app-add-stock' , templateUrl : './add-stock.component.html' , styleUrls : [ './add-stock.component.scss' ] , viewProviders : [ DistributorBillsService , StocksService ] } ) export class AddStockComponent implements OnInit { stateStep1 : StepState = StepState . Required ; stateStep2 : StepState = StepState . Required ; stateStep3 : StepState = StepState . Required ; disabled : boolean = false ; shop : RetailShop ; shops : RetailShop [ ] ; state : number = 1 ; entryType : string ; stocks : Stock [ ] ; bill : DistributorBill = < DistributorBill > { } ; distributors : any [ ] = [ ] ; products : any [ ] = [ ] ; constructor ( private _shopService , private _itemService , private _snackBarService , private _loadingService , private _distributorService , private _stockService , private _distributorBillService ) { this . _loadingService . create ( { name : 'distributor-bill' , type : LoadingType . Circular , mode : LoadingMode . Indeterminate , color : 'warn' , } ) ; } ngOnInit ( ) { this . shop = this . _shopService . shop ; this . shops = this . _shopService . shops ; if ( this . shop . id ) { this . setShop ( this . shop ) ; } this . _shopService . shops$ . subscribe ( ( data ) => { this . shops = data ; } ) ; this . _shopService . shop$ . subscribe ( ( data ) => { this . shop = data ; this . setShop ( this . shop ) ; } ) ; } setEntryType ( event ) { this . entryType = event ; this . state = 3 ; this . stateStep2 = StepState . Complete ; this . stocks = [ ] ; } setShop ( data ) { this . shop = data ; this . stateStep1 = StepState . Complete ; this . state = 2 ; } getDistributors = ( event ) : Observable < string [ ] > => { return this . _distributorService . query ( { __retail_shop_id__equal : this . shop . id , __only : [ 'id' , 'name' , 'retail_shop_id' ] , __name__contains : event } ) . map ( data => data . data ) ; } ; getProducts = ( event ) : Observable < string [ ] > => { let ids = this . stocks . map ( ( stock ) => { return stock . product . id ; } ) ; if ( ! ids . length ) { ids = undefined ; } return this . _itemService . query ( { __retail_shop_id__equal : this . shop . id , __only : [ 'id' , 'name' , 'retail_shop_id' ] , __name__contains : event , __id__not_in : ids } ) . map ( data => data . data ) ; } ; removeDistributor ( ) { this . stocks = [ ] ; } removeProduct ( ) { this . stocks = [ ] ; } addDistributor ( event ) { this . _loadingService . register ( 'distributor-bill' ) ; this . _distributorService . query ( { __retail_shop_id__equal : this . shop . id , __id__equal : event . value , __only : [ 'products' ] , __include : [ 'products' ] } ) . subscribe ( ( data ) => { this . stocks = data . data [ 0 ] . products . map ( ( data ) => { let stock = < Stock > { product : data , purchase_amount : data . last_purchase_amount , selling_amount : data . last_selling_amount , units_purchased : data . stock_required , product_id : data . id , quantity_label : data . quantity_label } ; stock . product = data ; return stock ; } ) ; this . _loadingService . resolve ( 'distributor-bill' ) ; } , ( ) => { this . _loadingService . resolve ( 'distributor-bill' ) ; } ) ; } addProduct ( event ) { console . log ( this . stocks ) ; this . _loadingService . register ( 'distributor-bill' ) ; this . _itemService . query ( { __retail_shop_id__equal : this . shop . id , __id__equal : event . value , __only : [ 'id' , 'name' , 'last_selling_amount' , 'last_purchase_amount' , 'stock_required' , 'quantity_label' ] , __include : [ 'last_selling_amount' , 'last_purchase_amount' , 'stock_required' ] } ) . subscribe ( ( data ) => { this . stocks = this . stocks . concat ( data . data . map ( ( data ) => { return < Stock > { product : data , purchase_amount : data . last_purchase_amount , selling_amount : data . last_selling_amount , units_purchased : data . stock_required , product_id : data . id , quantity_label : data . quantity_label , add : true } ; } ) ) ; this . _loadingService . resolve ( 'distributor-bill' ) ; } , ( ) => { this . _loadingService . resolve ( 'distributor-bill' ) ; } ) ; } saveBill ( ) { this . _loadingService . register ( 'distributor-bill' ) ; this . bill . distributor_id = this . distributors [ 0 ] . value ; this . bill . purchased_items = this . stocks . filter ( ( data ) => { delete data . product ; return data . add ; } ) ; this . _distributorBillService . create ( this . bill ) . subscribe ( ( data ) => { this . stateStep3 = StepState . Complete ; this . _shopService . getProductUpdate ( this . shop . id , null , { __id__in : this . stocks . map ( ( data ) => { return data . product_id ; } ) } ) . then ( ( ) => { this . stocks = [ ] ; this . distributors = [ ] ; this . entryType = null ; this . _snackBarService . open ( 'Stock updated with bill ID#' + JSON . stringify ( data . data [ 0 ] . id ) , '' , { duration : 3000 } ) ; this . _loadingService . resolve ( 'distributor-bill' ) ; } ) ; } , ( ) => { this . _loadingService . resolve ( 'distributor-bill' ) ; } ) ; } saveProductStock ( ) { this . _stockService . post ( this . stocks ) . subscribe ( ( data ) => { console . log ( data ) ; this . _shopService . getProductUpdate ( this . shop . id , null , { __id__in : this . stocks . map ( ( stock ) => { return stock . product . id ; } ) } ) . then ( ( ) => { this . stocks = [ ] ; this . distributors = [ ] ; this . products = [ ] ; this . _snackBarService . open ( 'Stock updated with ID#' + JSON . stringify ( 1 ) , '' , { duration : 3000 } ) ; this . _loadingService . resolve ( 'distributor-bill' ) ; } ) ; } , ( ) => { this . _loadingService . resolve ( 'distributor-bill' ) ; } ) ; } }