import { Injectable } from "@angular/core"; import { Utility } from "./utility"; import { InvoiceService } from "../services/invoiceService"; import { IRslt, Invoice, Payment, PaymentRsltDto } from "../../public-api"; @Injectable({ providedIn: 'root' }) export class InvoiceState { allInvoices: Array = new Array(); currInvoice: Invoice = new Invoice(); constructor(private service: InvoiceService, private utility: Utility) { } formatDollar(num: number) { return (num / 100).toLocaleString("en-US", { style: "currency", currency: "USD" }) } calcShipping(invoice: Invoice) { const sum = new Array(); for (var i = 0; i < invoice.items.length; i++) { const payment = invoice.items[i]; if (payment?.option?.cost) { sum.push(payment.option.cost) } } return sum.reduce((a, b) => a + b, 0) } calcTotalCost(invoice: Invoice) { return this.calcCost(invoice) + invoice.tax + this.calcShipping(invoice) } //can be an estimate or exact value. Need to notify client on the front end of that fact calcCost(invoice: Invoice) { const sum = new Array() for (var i = 0; i < invoice.items.length; i++) { const payment = invoice.items[i]; sum.push(payment.unitCost ? payment.unitCost : 0) // if (payment.unit.isRange) { // if(payment.unitCost){ // sum.push(payment.unitCost) // } // // else if(payment.unit.low && payment.unit.high){ // // const avg = (payment.unit.low + payment.unit.high)/2 // // sum.push(avg) // // } // } else if(payment?.unit?.msrp) { // sum.push(payment?.unit.msrp) // } } if (sum.length === 0) { return 0 } return sum.reduce((a, b) => a + b, 0) } calcPendingPayment(payments: Array) { const sum = new Array() for (var i = 0; i < payments.length; i++) { const payment = payments[i]; // if(payment.paymentType === 'square'){ // const payrslt = JSON.parse(payment.payment) as PaymentRsltDto; sum.push(payment.amount ? payment.amount : 0) // } } // console.log(JSON.stringify(sum)) return sum.reduce((a, b) => a + b, 0); } calcPayment(payments: Array) { const sum = new Array() for (var i = 0; i < payments.length; i++) { const payment = payments[i]; // if(payment.paymentType === 'square'){ // const payrslt = JSON.parse(payment.payment) as PaymentRsltDto; sum.push(payment.fulfilled ? payment.fulfilled : 0) // } } // console.log(JSON.stringify(sum)) return sum.reduce((a, b) => a + b, 0); } updateInvoice(payments: Array, invoice: Invoice) { const payment = this.calcPayment(payments) * 100; const total = this.calcTotalCost(invoice) //if true then a payment option is loaded that matches the total if (payment === total) { //if true the payment has completed and if (total === this.calcPayment(payments)) { invoice.stage = 'shipping' } else { invoice.stage = 'processing' } // await this.invoice.updateInvoice(invoices[i]) } else { //the payment may not have been processed return null; } return invoice; } // calcShipping(invoice:Invoice){ // const total = [] // for(var i = 0;i < invoice.items.length;i++){ // const itm = invoice.items[i] // if(!itm.option.isLater){ // total.push(itm.option.cost); // } // } // } // notComplete(invoice:Invoice){ // // const total = [] // return invoice.items.some(s => s.option.isLater) // // for(var i = 0;i < invoice.items.length;i++){ // // const itm = invoice.items[i] // // if(!itm.option.isLater){ // // total.push(itm.option.cost); // // } // // } // } get(done: any, error: any) { const self = this; this.service.get().subscribe({ next(rslt) { const data = rslt as IRslt if (data) { self.utility.update(self.allInvoices, rslt.results) } done() }, error(e) { error(e) }, complete() { } }) } getID(id: string, done: any, error: any) { const self = this; this.service.getByID(id).subscribe({ next(rslt) { const data = rslt as IRslt if (data) { self.utility.update(self.allInvoices, rslt.results) } done() }, error(e) { error(e) }, complete() { } }) } post(invoice: Invoice, done: any, error: any) { const self = this; this.service.post(invoice).subscribe({ next(rslt) { const data = rslt as IRslt // if (data) { // self.utility.update(self.allInvoices, rslt.results) // } done(rslt.results) }, error(e) { }, complete() { } }) } put(invoice: Invoice, done: any, error: any) { const self = this; this.service.put(invoice).subscribe({ next(rslt) { const data = rslt as IRslt // if (data) { // self.utility.update(self.allInvoices, rslt.results) // } done(rslt.results) }, error(e) { error(e) }, complete() { } }) } delete(invoice: Invoice, done: any, error: any) { const self = this; this.service.delete(invoice).subscribe({ next(rslt) { const data = rslt as IRslt // if (data) { // self.utility.update(self.allInvoices, rslt.results) // } done(rslt.results) }, error(e) { error(e) }, complete() { } }) } }