mirror of
				https://github.com/mokuappio/serverless-invoices.git
				synced 2025-10-30 17:31:08 -04:00 
			
		
		
		
	Add invoice validation & prefill invoice data.
This commit is contained in:
		| @ -1,4 +1,6 @@ | ||||
| import storage from 'localforage'; | ||||
| import { validate, generateInvoiceNumber } from '@/utils/helpers'; | ||||
| import dayjs from 'dayjs'; | ||||
|  | ||||
| class InvoiceService { | ||||
|   async getInvoices() { | ||||
| @ -12,9 +14,32 @@ class InvoiceService { | ||||
|   } | ||||
|  | ||||
|   async createInvoice(invoice) { | ||||
|     // TODO: add invoice no, issued_at, due_at, late_fee, prefill company info, bank_info, currency, vat_rate | ||||
|     const team = storage.getItem('team'); | ||||
|  | ||||
|     const invoices = await this.getInvoices(); | ||||
|  | ||||
|     invoice.issued_at = dayjs() | ||||
|       .format('YYYY-MM-DD'); | ||||
|     invoice.due_at = dayjs() | ||||
|       .add(14, 'days') | ||||
|       .format('YYYY-MM-DD'); | ||||
|     invoice.number = generateInvoiceNumber(dayjs() | ||||
|       .format('YYYY'), invoices.length + 1); | ||||
|     invoice.late_fee = 0.5; | ||||
|     invoice.from_name = team.company_name; | ||||
|     invoice.from_address = team.company_address; | ||||
|     invoice.from_postal_code = team.from_postal_code; | ||||
|     invoice.from_city = team.company_city; | ||||
|     invoice.from_country = team.company_country; | ||||
|     invoice.from_county = team.company_county; | ||||
|     invoice.from_reg_no = team.company_reg_no; | ||||
|     invoice.from_vat_no = team.company_vat_no; | ||||
|     invoice.from_website = team.website; | ||||
|     invoice.from_email = team.contact_email; | ||||
|     invoice.from_phone = team.contact_phone; | ||||
|     invoice.vat_rate = 0; | ||||
|     invoice.currency = 'USD'; | ||||
|  | ||||
|     delete invoice.$id; | ||||
|     delete invoice.$isNew; | ||||
|     delete invoice.$isDirty; | ||||
| @ -24,11 +49,24 @@ class InvoiceService { | ||||
|   } | ||||
|  | ||||
|   async updateInvoice(invoice) { | ||||
|     // TODO: validation | ||||
|     const invoices = await this.getInvoices(); | ||||
|     const index = invoices.findIndex(item => item.id === invoice.id); | ||||
|     invoices[index] = invoice; | ||||
|     return storage.setItem('invoices', invoices); | ||||
|     const necessaryFields = { | ||||
|       currency: 'Currency', | ||||
|       vat_rate: 'Vat Rate', | ||||
|       late_fee: 'Late Fee', | ||||
|       issued_at: 'Issued At', | ||||
|       due_at: 'Due At', | ||||
|       number: 'Number', | ||||
|     }; | ||||
|  | ||||
|     const validation = validate(necessaryFields, invoice); | ||||
|  | ||||
|     if (validation.length === 0) { | ||||
|       const invoices = await this.getInvoices(); | ||||
|       const index = invoices.findIndex(item => item.id === invoice.id); | ||||
|       invoices[index] = invoice; | ||||
|       return storage.setItem('invoices', invoices); | ||||
|     } | ||||
|     return Promise.reject(validation); | ||||
|   } | ||||
|  | ||||
|   async deleteInvoice(invoiceId) { | ||||
| @ -38,10 +76,47 @@ class InvoiceService { | ||||
|     return storage.setItem('invoices', invoices); | ||||
|   } | ||||
|  | ||||
|   bookInvoice(invoice) { | ||||
|     // TODO: validation | ||||
|     invoice.status = 'booked'; | ||||
|     return this.updateInvoice(invoice); | ||||
|   async bookInvoice(invoice) { | ||||
|     const necessaryFields = { | ||||
|       currency: 'Currency', | ||||
|       vat_rate: 'Vat rate', | ||||
|       late_fee: 'Late fee', | ||||
|       issued_at: 'Issued At', | ||||
|       due_at: 'Due At', | ||||
|       number: 'Number', | ||||
|       client_id: 'Client Id', | ||||
|       client_name: 'Client Name', | ||||
|       client_address: 'Client Address', | ||||
|       client_postal_code: 'Client Postal Code', | ||||
|       client_city: 'Client City', | ||||
|       client_email: 'Client Email', | ||||
|       client_country: 'Country', | ||||
|       from_name: 'Name', | ||||
|       from_address: 'Address', | ||||
|       from_postal_code: 'Postal Code', | ||||
|       from_country: 'Country/State', | ||||
|       from_city: 'City', | ||||
|       from_website: 'Website', | ||||
|       from_email: 'Your Email', | ||||
|       from_phone: 'From Phone', | ||||
|       bank_name: 'Bank Name', | ||||
|       bank_account_no: 'Bank Account No.', | ||||
|       project_id: 'Project Id', | ||||
|       rows: { | ||||
|         item: 'Item', | ||||
|         quantity: 'Quantity', | ||||
|         price: 'Price', | ||||
|         unit: 'Unit', | ||||
|       }, | ||||
|     }; | ||||
|  | ||||
|     const validation = await validate(necessaryFields, invoice); | ||||
|  | ||||
|     if (validation.length === 0) { | ||||
|       invoice.status = 'booked'; | ||||
|       return this.updateInvoice(invoice); | ||||
|     } | ||||
|     return Promise.reject(validation); | ||||
|   } | ||||
| } | ||||
|  | ||||
|  | ||||
		Reference in New Issue
	
	Block a user