From 9791f141252b9f9107f92f952d3c958daafeb8e2 Mon Sep 17 00:00:00 2001 From: akshayitzme Date: Tue, 27 Jun 2023 16:20:53 +0530 Subject: [PATCH] incr: invoice returns --- models/baseModels/Invoice/Invoice.ts | 26 +++++++++++++++++--------- models/baseModels/Payment/Payment.ts | 10 +++++++++- models/helpers.ts | 2 +- schemas/app/Payment.json | 7 +++++++ src/components/StatusPill.vue | 4 ++-- src/utils/filters.ts | 8 ++++---- 6 files changed, 40 insertions(+), 17 deletions(-) diff --git a/models/baseModels/Invoice/Invoice.ts b/models/baseModels/Invoice/Invoice.ts index 631db9ac2..d1feb604d 100644 --- a/models/baseModels/Invoice/Invoice.ts +++ b/models/baseModels/Invoice/Invoice.ts @@ -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() { @@ -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 = { @@ -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), diff --git a/models/baseModels/Payment/Payment.ts b/models/baseModels/Payment/Payment.ts index 9a5061026..701cd3bc1 100644 --- a/models/baseModels/Payment/Payment.ts +++ b/models/baseModels/Payment/Payment.ts @@ -37,6 +37,7 @@ export class Payment extends Transactional { writeoff?: Money; paymentType?: PaymentType; for?: PaymentFor[]; + forSales?: boolean; _accountsMap?: AccountTypeMap; async change({ changed }: ChangeArg) { @@ -507,7 +508,6 @@ export class Payment extends Transactional { } if (outstanding?.isPositive()) { - console.log('positive'); return 'Receive'; } return 'Pay'; @@ -521,6 +521,14 @@ export class Payment extends Transactional { formula: () => this.amount!.sub(this.writeoff!), dependsOn: ['amount', 'writeoff', 'for'], }, + forSales: { + formula: async () => { + if (!this.for) { + return this.forSales; + } + return this.for[0].referenceType === ModelNameEnum.SalesInvoice; + }, + }, }; validations: ValidationMap = { diff --git a/models/helpers.ts b/models/helpers.ts index 938e3b0a9..a695fe2c3 100644 --- a/models/helpers.ts +++ b/models/helpers.ts @@ -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'; } diff --git a/schemas/app/Payment.json b/schemas/app/Payment.json index 71afc80a8..613f53075 100644 --- a/schemas/app/Payment.json +++ b/schemas/app/Payment.json @@ -149,6 +149,13 @@ "required": false, "section": "References" }, + { + "fieldname": "forSales", + "fieldtype": "Check", + "default": true, + "required": true, + "hidden": true + }, { "fieldname": "attachment", "placeholder": "Add attachment", diff --git a/src/components/StatusPill.vue b/src/components/StatusPill.vue index 1ceeadd81..928ee70ee 100644 --- a/src/components/StatusPill.vue +++ b/src/components/StatusPill.vue @@ -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'; @@ -105,6 +104,7 @@ function getSubmittableStatus(doc: Doc) { doc.isSubmitted && isInvoice && doc.outstandingAmount?.isZero() !== true && + !doc.outstandingAmount?.isNegative() && !doc.isReturn && !doc.returnCompleted ) { @@ -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'; } diff --git a/src/utils/filters.ts b/src/utils/filters.ts index 1db354b35..9b919af4d 100644 --- a/src/utils/filters.ts +++ b/src/utils/filters.ts @@ -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' }, @@ -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' },