Skip to content

Commit

Permalink
incr: invoice returns
Browse files Browse the repository at this point in the history
  • Loading branch information
akshayitzme committed Jun 27, 2023
1 parent a14a5c3 commit 9791f14
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 17 deletions.
26 changes: 17 additions & 9 deletions models/baseModels/Invoice/Invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,12 @@ export abstract class Invoice extends Transactional {

const isItemsReturned = returnInvoices.length;

await this.fyo.db.update(this.schemaName, {
name: this.returnAgainst as string,
isItemsReturned,
});
const invoiceDoc = await this.fyo.doc.getDoc(
this.schemaName,
this.returnAgainst
);
await invoiceDoc.setAndSync({ isItemsReturned });
await invoiceDoc.submit();
}

async _updateReturnInvoiceOutStanding() {
Expand Down Expand Up @@ -488,10 +490,12 @@ export abstract class Invoice extends Transactional {
outstandingAmount = invoiceOutstandingAmount.add(returnInvoiceGrandTotal);
}

await this.fyo.db.update(this.schemaName, {
name: this.returnAgainst as string,
outstandingAmount,
});
const invoiceDoc = await this.fyo.doc.getDoc(
this.schemaName,
this.returnAgainst
);
await invoiceDoc.setAndSync({ outstandingAmount });
await invoiceDoc.submit();
}

formulas: FormulaMap = {
Expand Down Expand Up @@ -728,7 +732,11 @@ export abstract class Invoice extends Transactional {
return null;
}

const accountField = this.isSales ? 'account' : 'paymentAccount';
const accountField =
this.isSales && !this.outstandingAmount?.isNegative()
? 'account'
: 'paymentAccount';

const data = {
party: this.party,
date: new Date().toISOString().slice(0, 10),
Expand Down
10 changes: 9 additions & 1 deletion models/baseModels/Payment/Payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class Payment extends Transactional {
writeoff?: Money;
paymentType?: PaymentType;
for?: PaymentFor[];
forSales?: boolean;
_accountsMap?: AccountTypeMap;

async change({ changed }: ChangeArg) {
Expand Down Expand Up @@ -507,7 +508,6 @@ export class Payment extends Transactional {
}

if (outstanding?.isPositive()) {
console.log('positive');
return 'Receive';
}
return 'Pay';
Expand All @@ -521,6 +521,14 @@ export class Payment extends Transactional {
formula: () => this.amount!.sub(this.writeoff!),
dependsOn: ['amount', 'writeoff', 'for'],
},
forSales: {
formula: async () => {

Check failure on line 525 in models/baseModels/Payment/Payment.ts

View workflow job for this annotation

GitHub Actions / setup_and_lint

Async method 'formula' has no 'await' expression
if (!this.for) {
return this.forSales;
}
return this.for[0].referenceType === ModelNameEnum.SalesInvoice;
},
},
};

validations: ValidationMap = {
Expand Down
2 changes: 1 addition & 1 deletion models/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ export function getInvoiceStatus(doc: RenderData | Doc): InvoiceStatus {
doc.submitted &&
!doc.cancelled &&
!(doc.outstandingAmount as Money).isZero() &&
(doc.outstandingAmount as Money) < (doc.grandTotal as Money)
(doc.outstandingAmount as Money).abs() < (doc.grandTotal as Money)
) {
return 'PartlyPaid';
}
Expand Down
7 changes: 7 additions & 0 deletions schemas/app/Payment.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@
"required": false,
"section": "References"
},
{
"fieldname": "forSales",
"fieldtype": "Check",
"default": true,
"required": true,
"hidden": true
},
{
"fieldname": "attachment",
"placeholder": "Add attachment",
Expand Down
4 changes: 2 additions & 2 deletions src/components/StatusPill.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Doc } from 'fyo/model/doc';
import { isPesa } from 'fyo/utils';
import { Invoice } from 'models/baseModels/Invoice/Invoice';
import { Party } from 'models/baseModels/Party/Party';
import { Money } from 'pesa';
import { getBgTextColorClass } from 'src/utils/colors';
import { defineComponent } from 'vue';
Expand Down Expand Up @@ -105,6 +104,7 @@ function getSubmittableStatus(doc: Doc) {
doc.isSubmitted &&
isInvoice &&
doc.outstandingAmount?.isZero() !== true &&
!doc.outstandingAmount?.isNegative() &&
!doc.isReturn &&
!doc.returnCompleted
) {
Expand All @@ -127,7 +127,7 @@ function getSubmittableStatus(doc: Doc) {
doc.isSubmitted &&
isInvoice &&
!doc.outstandingAmount?.isZero() &&
(doc.outstandingAmount as Money) < (doc.grandTotal as Money)
doc.outstandingAmount!.abs() < doc.grandTotal!
) {
return 'PartlyPaid';
}
Expand Down
8 changes: 4 additions & 4 deletions src/utils/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ export const routeFilters = {
SalesItems: { for: ['in', ['Sales', 'Both']] },
PurchaseItems: { for: ['in', ['Purchases', 'Both']] },
Items: { for: 'Both' },
PurchasePayments: {},
SalesPayments: {},
PurchasePayments: { forSales: false },
SalesPayments: { forSales: true },
Suppliers: { role: ['in', ['Supplier', 'Both']] },
Customers: { role: ['in', ['Customer', 'Both']] },
Party: { role: 'Both' },
Expand All @@ -13,8 +13,8 @@ export const createFilters = {
SalesItems: { for: 'Sales' },
PurchaseItems: { for: 'Purchases' },
Items: { for: 'Both' },
PurchasePayments: {},
SalesPayments: {},
PurchasePayments: { forSales: false },
SalesPayments: { forSales: true },
Suppliers: { role: 'Supplier' },
Customers: { role: 'Customer' },
Party: { role: 'Both' },
Expand Down

0 comments on commit 9791f14

Please sign in to comment.