Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Alter "Payment" Entity Table #8796

Merged
merged 6 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ import { Subject, firstValueFrom } from 'rxjs';
import { debounceTime, filter, tap } from 'rxjs/operators';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { saveAs } from 'file-saver';
import { IInvoice, IPayment, InvoiceStatusTypesEnum, IOrganization, IUser } from '@gauzy/contracts';
import {
IInvoice,
IPayment,
InvoiceStatusTypesEnum,
IOrganization,
IUser,
IPaymentCreateInput
} from '@gauzy/contracts';
import {
InvoiceEstimateHistoryService,
InvoicesService,
Expand Down Expand Up @@ -118,7 +125,7 @@ export class InvoicePaymentsComponent extends TranslationBaseComponent implement
'toContact',
'payments',
'payments.invoice',
'payments.recordedBy'
'payments.createdByUser'
];

// Fetch invoice details
Expand Down Expand Up @@ -312,12 +319,10 @@ export class InvoicePaymentsComponent extends TranslationBaseComponent implement
instance.value = cell.getValue();
}
},
recordedBy: {
createdByUser: {
title: this.getTranslation('INVOICES_PAGE.PAYMENTS.RECORDED_BY'),
type: 'text',
valuePrepareFunction: (value: IUser) => {
return value && value.name ? `${value.name}` : '';
}
valuePrepareFunction: (value: IUser) => value?.name ?? ''
},
note: {
title: this.getTranslation('INVOICES_PAGE.PAYMENTS.NOTE'),
Expand Down Expand Up @@ -353,7 +358,14 @@ export class InvoicePaymentsComponent extends TranslationBaseComponent implement
};
}

async updateInvoiceStatus(totalValue: number, totalPaid: number) {
/**
* Updates the status of an invoice based on the total invoice value and the total amount paid.
*
* @param totalValue - The total amount due for the invoice.
* @param totalPaid - The total amount that has been paid towards the invoice.
* @returns A Promise that resolves when the invoice status has been updated.
*/
async updateInvoiceStatus(totalValue: number, totalPaid: number): Promise<void> {
if (totalPaid <= 0) {
await this.invoicesService.updateAction(this.invoice.id, {
status: InvoiceStatusTypesEnum.VIEWED
Expand All @@ -377,29 +389,25 @@ export class InvoicePaymentsComponent extends TranslationBaseComponent implement
if (!this.invoice) {
return;
}
// Destructure organization properties
const { tenantId } = this.organization;

const { tenantId } = this.store.user;
const payment = {
// Create a payment object
const payment: IPaymentCreateInput = {
amount: this.leftToPay,
paymentDate: new Date(),
currency: this.invoice.currency,
invoice: this.invoice,
invoiceId: this.invoice.id,
organization: this.invoice.fromOrganization,
organizationId: this.invoice.fromOrganization.id,
tenantId,
recordedBy: this.store.user,
userId: this.store.userId
tenantId
};

if (this.invoice.dueDate >= new Date()) {
payment['overdue'] = true;
} else {
payment['overdue'] = false;
}
payment.overdue = this.invoice.dueDate >= new Date();

await this.paymentService.add(payment);
const { amount, currency, invoice } = payment;

if (payment.invoice) {
const action = this.getTranslation('INVOICES_PAGE.PAYMENTS.PAYMENT_AMOUNT_ADDED', { amount, currency });
await this.createInvoiceHistory(action, invoice);
Expand Down Expand Up @@ -441,7 +449,7 @@ export class InvoicePaymentsComponent extends TranslationBaseComponent implement
contact: this.invoice.toContact ? this.invoice.toContact.name : '',
paymentDate: payment.paymentDate.toString().slice(0, 10),
amount: `${payment.currency + ' ' + payment.amount}`,
recordedBy: payment.recordedBy.firstName + payment.recordedBy.lastName,
createdByUser: payment.createdByUser.name,
note: payment.note || '',
paymentMethod: payment.paymentMethod
? this.getTranslation(`INVOICES_PAGE.PAYMENTS.${payment.paymentMethod}`)
Expand Down
8 changes: 4 additions & 4 deletions apps/gauzy/src/app/pages/payments/payments.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export class PaymentsComponent extends PaginationFilterBaseComponent implements

this.smartTableSource = new ServerDataSource(this.httpClient, {
endPoint: `${API_PREFIX}/payments/pagination`,
relations: ['invoice', 'invoice.toContact', 'recordedBy', 'organizationContact', 'project', 'tags'],
relations: ['invoice', 'invoice.toContact', 'createdByUser', 'organizationContact', 'project', 'tags'],
join: {
alias: 'payment',
leftJoin: {
Expand All @@ -234,15 +234,15 @@ export class PaymentsComponent extends PaginationFilterBaseComponent implements
},
resultMap: (payment: IPayment) => {
try {
const { invoice, project, recordedBy, paymentMethod, overdue } = payment;
const { invoice, project, createdByUser, paymentMethod, overdue } = payment;
const organizationContact = payment.organizationContact || (invoice && invoice.toContact);

return {
...payment,
overdue: this.statusMapper(overdue),
invoiceNumber: invoice?.invoiceNumber || null,
projectName: project?.name || null,
recordedByName: recordedBy?.name || null,
createdByUser: createdByUser?.name || null,
paymentMethodEnum: paymentMethod
? this.getTranslation(`INVOICES_PAGE.PAYMENTS.${paymentMethod}`)
: null,
Expand Down Expand Up @@ -536,7 +536,7 @@ export class PaymentsComponent extends PaginationFilterBaseComponent implements
this.setFilter({ field: 'paymentMethod', search: value });
}
},
recordedByName: {
createdByUser: {
title: this.getTranslation('PAYMENTS_PAGE.RECORDED_BY'),
type: 'text',
isFilterable: false,
Expand Down
44 changes: 24 additions & 20 deletions packages/contracts/src/lib/payment.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IBasePerTenantAndOrganizationEntityModel } from './base-entity.model';
import { IBasePerTenantAndOrganizationEntityModel, ID } from './base-entity.model';
import { IInvoice } from './invoice.model';
import { ITag } from './tag.model';
import { IUser } from './user.model';
import { ITaggable } from './tag.model';
import { IHasUserCreator } from './user.model';
import { IOrganizationContact } from './organization-contact.model';
import { IOrganizationProject } from './organization-projects.model';
import { IPaginationInput } from './core.model';
Expand All @@ -16,35 +16,39 @@ export interface PaymentStats {
amount: number; // The total amount of all payments
}

export interface IPayment extends IBasePerTenantAndOrganizationEntityModel {
invoice?: IInvoice;
invoiceId?: string;
tags?: ITag[];
// Base interface for common properties
interface IBasePaymentProperties extends IBasePerTenantAndOrganizationEntityModel, ITaggable, IHasUserCreator {
note?: string;
recordedBy?: IUser;
employeeId?: string;
employeeId?: ID;
employee?: IEmployee;
paymentDate?: Date;
amount?: number;
currency?: string;
overdue?: boolean;
paymentMethod?: PaymentMethodEnum;
invoice?: IInvoice;
invoiceId?: ID;
organizationContact?: IOrganizationContact;
organizationContactId?: string;
organizationContactId?: ID;
project?: IOrganizationProject;
projectId?: string;
projectId?: ID;
}

export interface IPaymentUpdateInput extends IBasePerTenantAndOrganizationEntityModel {
amount?: number;
note?: string;
currency?: string;
paymentDate?: Date;
}
// Main Payment interface
export interface IPayment extends IBasePaymentProperties {}

export interface IPaymentFindInput extends IBasePerTenantAndOrganizationEntityModel {
invoiceId?: string;
}
// Input interface for creating a payment
export interface IPaymentCreateInput extends IBasePaymentProperties {}

// Input interface for updating a payment
export interface IPaymentUpdateInput
extends Pick<IPayment, 'amount' | 'note' | 'currency' | 'paymentDate'>,
IBasePerTenantAndOrganizationEntityModel {}

// Input interface for finding a payment
export interface IPaymentFindInput
extends Pick<IPayment, 'invoiceId' | 'projectId'>,
IBasePerTenantAndOrganizationEntityModel {}

export enum PaymentMethodEnum {
BANK_TRANSFER = 'BANK_TRANSFER',
Expand Down
Loading
Loading