11import { Component , OnInit } from '@angular/core' ;
22import { TranslationBaseComponent } from '@gauzy/ui-core/i18n' ;
3- import { IInvoice , InvoiceStatusTypesEnum , IInvoiceItem } from '@gauzy/contracts' ;
3+ import { IInvoice , InvoiceStatusTypesEnum , IInvoiceItem , IInvoiceItemCreateInput } from '@gauzy/contracts' ;
44import { TranslateService } from '@ngx-translate/core' ;
55import { UntypedFormGroup , UntypedFormBuilder , Validators } from '@angular/forms' ;
66import { NbDialogRef } from '@nebular/theme' ;
7- import { InvoiceEstimateHistoryService , InvoicesService , Store , ToastrService } from '@gauzy/ui-core/core' ;
7+ import {
8+ InvoiceEstimateHistoryService ,
9+ InvoiceItemService ,
10+ InvoicesService ,
11+ Store ,
12+ ToastrService
13+ } from '@gauzy/ui-core/core' ;
14+ import moment from 'moment' ;
815
916@Component ( {
10- selector : 'ga-invoice-email' ,
11- templateUrl : './invoice-email-mutation.component.html' ,
12- styleUrls : [ './invoice-email-mutation.component.scss' ] ,
13- standalone : false
17+ selector : 'ga-invoice-email' ,
18+ templateUrl : './invoice-email-mutation.component.html' ,
19+ styleUrls : [ './invoice-email-mutation.component.scss' ] ,
20+ standalone : false
1421} )
1522export class InvoiceEmailMutationComponent extends TranslationBaseComponent implements OnInit {
1623 invoice : IInvoice ;
@@ -26,7 +33,9 @@ export class InvoiceEmailMutationComponent extends TranslationBaseComponent impl
2633 private readonly toastrService : ToastrService ,
2734 private readonly invoiceService : InvoicesService ,
2835 private readonly store : Store ,
29- private readonly invoiceEstimateHistoryService : InvoiceEstimateHistoryService
36+ private readonly invoiceEstimateHistoryService : InvoiceEstimateHistoryService ,
37+ private readonly invoicesService : InvoicesService ,
38+ private readonly invoiceItemService : InvoiceItemService
3039 ) {
3140 super ( translateService ) ;
3241 }
@@ -47,6 +56,11 @@ export class InvoiceEmailMutationComponent extends TranslationBaseComponent impl
4756
4857 const { email } = this . form . value ;
4958
59+ if ( ! this . invoice . id ) {
60+ const createdInvoice = await this . createInvoiceEstimate ( InvoiceStatusTypesEnum . SENT ) ;
61+ if ( createdInvoice ) await this . createInvoiceEstimateItems ( ) ;
62+ }
63+
5064 await this . invoiceService . sendEmail (
5165 email ,
5266 this . invoice . invoiceNumber ,
@@ -90,6 +104,52 @@ export class InvoiceEmailMutationComponent extends TranslationBaseComponent impl
90104 } ) ;
91105 }
92106
107+ async createInvoiceEstimate ( status : string , sendTo ?: string ) {
108+ try {
109+ const createdInvoice = await this . invoicesService . addOwn ( {
110+ invoiceNumber : this . invoice . invoiceNumber ,
111+ invoiceDate : moment ( this . invoice . invoiceDate ) . startOf ( 'day' ) . toDate ( ) ,
112+ dueDate : moment ( this . invoice . dueDate ) . endOf ( 'day' ) . toDate ( ) ,
113+ currency : this . invoice . currency ,
114+ discountValue : this . invoice . discountValue ,
115+ discountType : this . invoice . discountType ,
116+ tax : this . invoice . tax ,
117+ tax2 : this . invoice . tax2 ,
118+ taxType : this . invoice . taxType ,
119+ tax2Type : this . invoice . tax2Type ,
120+ terms : this . invoice . terms ,
121+ paid : false ,
122+ totalValue : this . invoice . totalValue ,
123+ fromUserId : this . invoice . fromUserId ,
124+ fromOrganizationId : this . invoice . fromOrganization ?. id ,
125+ organizationId : this . invoice . fromOrganization ?. id ,
126+ tenantId : this . invoice . tenantId ,
127+ invoiceType : this . invoice . invoiceType ,
128+ tags : this . invoice . tags ,
129+ isEstimate : this . invoice . isEstimate ,
130+ status : status ,
131+ sentTo : sendTo ,
132+ isArchived : this . invoice . isArchived
133+ } ) ;
134+ this . createdInvoice = createdInvoice ;
135+ return createdInvoice ;
136+ } catch ( error ) {
137+ this . toastrService . danger ( error ) ;
138+ }
139+ }
140+
141+ async createInvoiceEstimateItems ( ) {
142+ const invoiceItems : IInvoiceItemCreateInput [ ] = this . invoice . invoiceItems . map ( ( item ) => ( {
143+ ...item ,
144+ invoiceId : this . createdInvoice . id
145+ } ) ) ;
146+ try {
147+ return await this . invoiceItemService . createBulk ( this . createdInvoice ?. id , invoiceItems ) ;
148+ } catch ( error ) {
149+ this . toastrService . danger ( error ) ;
150+ }
151+ }
152+
93153 cancel ( ) {
94154 this . dialogRef . close ( ) ;
95155 }
0 commit comments