From 0ce9507a4aae268bb88d5c0af594ec76f1141fcc Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Wed, 29 Mar 2023 11:44:59 +0530 Subject: [PATCH 1/2] fix(ux): allow setting of Stock Transfer backref --- models/inventory/StockTransfer.ts | 40 ++++++++++++++++++++++ schemas/app/inventory/PurchaseReceipt.json | 1 - schemas/app/inventory/Shipment.json | 1 - 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/models/inventory/StockTransfer.ts b/models/inventory/StockTransfer.ts index 1d8bb97f..5678a47d 100644 --- a/models/inventory/StockTransfer.ts +++ b/models/inventory/StockTransfer.ts @@ -3,6 +3,7 @@ import { Attachment } from 'fyo/core/types'; import { Doc } from 'fyo/model/doc'; import { Action, + ChangeArg, DefaultMap, FiltersMap, FormulaMap, @@ -15,6 +16,7 @@ import { addItem, getLedgerLinkAction, getNumberSeries } from 'models/helpers'; import { LedgerPosting } from 'models/Transactional/LedgerPosting'; import { ModelNameEnum } from 'models/types'; import { Money } from 'pesa'; +import { TargetField } from 'schemas/types'; import { validateBatch } from './helpers'; import { StockTransferItem } from './StockTransferItem'; import { Transfer } from './Transfer'; @@ -66,6 +68,11 @@ export abstract class StockTransfer extends Transfer { role: ['in', [doc.isSales ? 'Customer' : 'Supplier', 'Both']], }), numberSeries: (doc: Doc) => ({ referenceType: doc.schemaName }), + backReference: () => ({ + stockNotTransferred: true, + submitted: true, + cancelled: false, + }), }; override _getTransferDetails() { @@ -257,4 +264,37 @@ export abstract class StockTransfer extends Transfer { async addItem(name: string) { return await addItem(name, this); } + + override async change({ doc, changed }: ChangeArg): Promise { + if (doc.name === this.name && changed === 'backReference') { + await this.setFieldsFromBackReference(); + } + } + + async setFieldsFromBackReference() { + const backReference = this.backReference; + const { target } = this.fyo.getField( + this.schemaName, + 'backReference' + ) as TargetField; + + if (!backReference || !target) { + return; + } + + const brDoc = await this.fyo.doc.getDoc(target, backReference); + if (!(brDoc instanceof Invoice)) { + return; + } + + const stDoc = await brDoc.getStockTransfer(); + if (!stDoc) { + return; + } + + await this.set('party', stDoc.party); + await this.set('terms', stDoc.terms); + await this.set('date', stDoc.date); + await this.set('items', stDoc.items); + } } diff --git a/schemas/app/inventory/PurchaseReceipt.json b/schemas/app/inventory/PurchaseReceipt.json index 7b051053..e145edb6 100644 --- a/schemas/app/inventory/PurchaseReceipt.json +++ b/schemas/app/inventory/PurchaseReceipt.json @@ -20,7 +20,6 @@ "label": "Back Reference", "fieldtype": "Link", "target": "PurchaseInvoice", - "readOnly": true, "section": "References" }, { diff --git a/schemas/app/inventory/Shipment.json b/schemas/app/inventory/Shipment.json index 242a0d64..b2265281 100644 --- a/schemas/app/inventory/Shipment.json +++ b/schemas/app/inventory/Shipment.json @@ -20,7 +20,6 @@ "label": "Back Reference", "fieldtype": "Link", "target": "SalesInvoice", - "readOnly": true, "section": "References" }, { From 4e22ac1d8c09f87f9d0907b164e5ad3daf846b69 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Wed, 29 Mar 2023 12:15:47 +0530 Subject: [PATCH 2/2] fix: backReference filter - test manual setting of backReference --- models/inventory/StockTransfer.ts | 2 +- .../inventory/tests/testStockTransfer.spec.ts | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/models/inventory/StockTransfer.ts b/models/inventory/StockTransfer.ts index 5678a47d..aa1e4df8 100644 --- a/models/inventory/StockTransfer.ts +++ b/models/inventory/StockTransfer.ts @@ -69,7 +69,7 @@ export abstract class StockTransfer extends Transfer { }), numberSeries: (doc: Doc) => ({ referenceType: doc.schemaName }), backReference: () => ({ - stockNotTransferred: true, + stockNotTransferred: ['!=', 0], submitted: true, cancelled: false, }), diff --git a/models/inventory/tests/testStockTransfer.spec.ts b/models/inventory/tests/testStockTransfer.spec.ts index 51cc6023..0229e1ab 100644 --- a/models/inventory/tests/testStockTransfer.spec.ts +++ b/models/inventory/tests/testStockTransfer.spec.ts @@ -8,6 +8,7 @@ import { RawValue } from 'schemas/types'; import test from 'tape'; import { closeTestFyo, getTestFyo, setupTestFyo } from 'tests/helpers'; import { InventorySettings } from '../InventorySettings'; +import { Shipment } from '../Shipment'; import { StockTransfer } from '../StockTransfer'; import { ValuationMethod } from '../types'; import { getALEs, getItem, getSLEs, getStockTransfer } from './helpers'; @@ -536,4 +537,52 @@ test('Cancel and Delete Sales Invoice with cancelled Shipments', async (t) => { } }); +test('Create Shipment from manually set Back Ref', async (t) => { + const rate = (testDocs['Item'][item].rate as number) ?? 0; + const totalQuantity = 10; + const prec = await getStockTransfer( + ModelNameEnum.PurchaseReceipt, + party, + new Date('2022-01-08'), + [ + { + item, + location, + quantity: totalQuantity, + rate, + }, + ], + fyo + ); + await (await prec.sync()).submit(); + + const sinv = fyo.doc.getNewDoc(ModelNameEnum.SalesInvoice) as Invoice; + const quantity = 5; + await sinv.set({ + party, + date: new Date('2022-01-09'), + account: 'Debtors', + }); + await sinv.append('items', { item, quantity, rate }); + await (await sinv.sync()).submit(); + + t.equal(sinv.stockNotTransferred, quantity, "stock hasn't been transferred"); + + const shpm = fyo.doc.getNewDoc(ModelNameEnum.Shipment) as Shipment; + await shpm.set('backReference', sinv.name); + await shpm.set('date', new Date('2022-01-10')); + shpm.items?.[0].set('location', location); + + t.equal(shpm.party, sinv.party, 'party set'); + + await (await shpm.sync()).submit(); + t.equal( + await fyo.db.getStockQuantity(item, location), + totalQuantity - quantity, + 'quantity shipped' + ); + + t.equal(sinv.stockNotTransferred, 0, 'stock has been transferred'); +}); + closeTestFyo(fyo, __filename);