From e3b6d306089eb5ee0e20338a03c963a6ed0d7230 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Tue, 6 Jun 2023 12:33:58 +0530 Subject: [PATCH 1/3] incr: update schemas for auto stock transfer --- schemas/app/Defaults.json | 16 ++++++++++++++++ schemas/app/Invoice.json | 5 +++++ schemas/app/PurchaseInvoice.json | 8 ++++++++ schemas/app/SalesInvoice.json | 8 ++++++++ 4 files changed, 37 insertions(+) diff --git a/schemas/app/Defaults.json b/schemas/app/Defaults.json index ab09679b..ffc6ca2e 100644 --- a/schemas/app/Defaults.json +++ b/schemas/app/Defaults.json @@ -20,6 +20,22 @@ "create": true, "section": "Auto Payments" }, + { + "fieldname": "defaultShipmentLocation", + "label": "Shipment Location", + "fieldtype": "Link", + "target": "Location", + "create": true, + "section": "Auto Stock Transfer" + }, + { + "fieldname": "defaultPurchaseReceiptLocation", + "label": "Purchase Receipt Location", + "fieldtype": "Link", + "target": "Location", + "create": true, + "section": "Auto Stock Transfer" + }, { "fieldname": "salesInvoiceNumberSeries", "label": "Sales Invoice Number Series", diff --git a/schemas/app/Invoice.json b/schemas/app/Invoice.json index 29c10053..04775e4c 100644 --- a/schemas/app/Invoice.json +++ b/schemas/app/Invoice.json @@ -147,6 +147,11 @@ "readOnly": false, "tab": "Settings" }, + { + "abstract": true, + "fieldname": "makeAutoStockTransfer", + "tab": "Settings" + }, { "fieldname": "outstandingAmount", "label": "Outstanding Amount", diff --git a/schemas/app/PurchaseInvoice.json b/schemas/app/PurchaseInvoice.json index fca1587b..4cfa1f86 100644 --- a/schemas/app/PurchaseInvoice.json +++ b/schemas/app/PurchaseInvoice.json @@ -22,6 +22,14 @@ "target": "PurchaseReceipt", "section": "References" }, + { + "fieldname": "makeAutoStockTransfer", + "label": "Make Purchase Receipt On Submit", + "fieldtype": "Check", + "default": false, + "readOnly": false, + "tab": "Settings" + }, { "fieldname": "items", "label": "Items", diff --git a/schemas/app/SalesInvoice.json b/schemas/app/SalesInvoice.json index 34e3d66e..6125e1ae 100644 --- a/schemas/app/SalesInvoice.json +++ b/schemas/app/SalesInvoice.json @@ -22,6 +22,14 @@ "target": "Shipment", "section": "References" }, + { + "fieldname": "makeAutoStockTransfer", + "label": "Make Shipment On Submit", + "fieldtype": "Check", + "default": false, + "readOnly": false, + "tab": "Settings" + }, { "fieldname": "items", "label": "Items", From ce4481b609b5460340d8fe8583bfb325330d4003 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Tue, 6 Jun 2023 13:14:26 +0530 Subject: [PATCH 2/3] feat: add auto stock transfer --- fyo/model/types.ts | 10 ++++- models/baseModels/Defaults/Defaults.ts | 4 ++ models/baseModels/Invoice/Invoice.ts | 57 +++++++++++++++++++++++--- schemas/app/Defaults.json | 4 +- 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/fyo/model/types.ts b/fyo/model/types.ts index 47563666..8c2f5279 100644 --- a/fyo/model/types.ts +++ b/fyo/model/types.ts @@ -1,10 +1,14 @@ import { Fyo } from 'fyo'; import { DocValue, DocValueMap } from 'fyo/core/types'; -import SystemSettings from 'fyo/models/SystemSettings'; +import type SystemSettings from 'fyo/models/SystemSettings'; import { FieldType, Schema, SelectOption } from 'schemas/types'; import { QueryFilter } from 'utils/db/types'; import { RouteLocationRaw, Router } from 'vue-router'; import { Doc } from './doc'; +import type { AccountingSettings } from 'models/baseModels/AccountingSettings/AccountingSettings'; +import type { Defaults } from 'models/baseModels/Defaults/Defaults'; +import type { PrintSettings } from 'models/baseModels/PrintSettings/PrintSettings'; +import type { InventorySettings } from 'models/inventory/InventorySettings'; /** * The functions below are used for dynamic evaluation @@ -47,6 +51,10 @@ export type DocMap = Record; export interface SinglesMap { SystemSettings?: SystemSettings; + AccountingSettings?: AccountingSettings; + InventorySettings?: InventorySettings; + PrintSettings?: PrintSettings; + Defaults?: Defaults; [key: string]: Doc | undefined; } diff --git a/models/baseModels/Defaults/Defaults.ts b/models/baseModels/Defaults/Defaults.ts index d23318b2..bfee0ff6 100644 --- a/models/baseModels/Defaults/Defaults.ts +++ b/models/baseModels/Defaults/Defaults.ts @@ -7,6 +7,10 @@ export class Defaults extends Doc { salesPaymentAccount?: string; purchasePaymentAccount?: string; + // Auto Stock Transfer + shipmentLocation?: string; + purchaseReceiptLocation?: string; + // Number Series salesInvoiceNumberSeries?: string; purchaseInvoiceNumberSeries?: string; diff --git a/models/baseModels/Invoice/Invoice.ts b/models/baseModels/Invoice/Invoice.ts index 368c5f17..00f60ef7 100644 --- a/models/baseModels/Invoice/Invoice.ts +++ b/models/baseModels/Invoice/Invoice.ts @@ -50,6 +50,7 @@ export abstract class Invoice extends Transactional { submitted?: boolean; cancelled?: boolean; makeAutoPayment?: boolean; + makeAutoStockTransfer?: boolean; get isSales() { return this.schemaName === 'SalesInvoice'; @@ -105,6 +106,16 @@ export abstract class Invoice extends Transactional { return null; } + get autoStockTransferLocation(): string | null { + const fieldname = this.isSales ? '' : 'purchasePaymentAccount'; + const value = this.fyo.singles.Defaults?.[fieldname]; + if (typeof value === 'string' && value.length) { + return value; + } + + return null; + } + constructor(schema: Schema, data: DocValueMap, fyo: Fyo) { super(schema, data, fyo); this._setGetCurrencies(); @@ -139,6 +150,13 @@ export abstract class Invoice extends Transactional { await payment?.submit(); await this.load(); } + + if (this.makeAutoStockTransfer && this.autoStockTransferLocation) { + const stockTransfer = await this.getStockTransfer(true); + await stockTransfer?.sync(); + await stockTransfer?.submit(); + await this.load(); + } } async afterCancel() { @@ -458,11 +476,18 @@ export abstract class Invoice extends Transactional { return true; } - if (!this.autoPaymentAccount) { + return !this.autoPaymentAccount; + }, + makeAutoStockTransfer: () => { + if (this.submitted) { return true; } - return false; + if (!this.fyo.singles.AccountingSettings?.enableInventory) { + return true; + } + + return !this.autoStockTransferLocation; }, setDiscountAmount: () => true || !this.enableDiscounting, discountAmount: () => @@ -574,7 +599,9 @@ export abstract class Invoice extends Transactional { return this.fyo.doc.getNewDoc(ModelNameEnum.Payment, data) as Payment; } - async getStockTransfer(): Promise { + async getStockTransfer( + isAuto: boolean = false + ): Promise { if (!this.isSubmitted) { return null; } @@ -604,9 +631,14 @@ export abstract class Invoice extends Transactional { backReference: this.name, }; - const location = - (this.fyo.singles.InventorySettings as InventorySettings) - .defaultLocation ?? null; + let location = this.autoStockTransferLocation; + if (!location) { + location = this.fyo.singles.InventorySettings?.defaultLocation ?? null; + } + + if (isAuto && !location) { + return null; + } const transfer = this.fyo.doc.getNewDoc(schemaName, data) as StockTransfer; for (const row of this.items ?? []) { @@ -615,6 +647,10 @@ export abstract class Invoice extends Transactional { } const itemDoc = (await row.loadAndGetLink('item')) as Item; + if (isAuto && (itemDoc.hasBatch || itemDoc.hasSerialNumber)) { + continue; + } + const item = row.item; const quantity = row.stockNotTransferred; const trackItem = itemDoc.trackItem; @@ -631,6 +667,15 @@ export abstract class Invoice extends Transactional { continue; } + if (isAuto) { + const stock = + (await this.fyo.db.getStockQuantity(item, location!, data.date)) ?? 0; + + if (stock <= quantity) { + continue; + } + } + await transfer.append('items', { item, quantity, diff --git a/schemas/app/Defaults.json b/schemas/app/Defaults.json index ffc6ca2e..9b5e5c3d 100644 --- a/schemas/app/Defaults.json +++ b/schemas/app/Defaults.json @@ -21,7 +21,7 @@ "section": "Auto Payments" }, { - "fieldname": "defaultShipmentLocation", + "fieldname": "shipmentLocation", "label": "Shipment Location", "fieldtype": "Link", "target": "Location", @@ -29,7 +29,7 @@ "section": "Auto Stock Transfer" }, { - "fieldname": "defaultPurchaseReceiptLocation", + "fieldname": "purchaseReceiptLocation", "label": "Purchase Receipt Location", "fieldtype": "Link", "target": "Location", From 96cad8a3d5f7695d147c7dc511c0a44a5b30475b Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Tue, 6 Jun 2023 13:49:59 +0530 Subject: [PATCH 3/3] incr: fix default fieldname - fix passed date --- models/baseModels/Invoice/Invoice.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/models/baseModels/Invoice/Invoice.ts b/models/baseModels/Invoice/Invoice.ts index 00f60ef7..865307ea 100644 --- a/models/baseModels/Invoice/Invoice.ts +++ b/models/baseModels/Invoice/Invoice.ts @@ -107,7 +107,9 @@ export abstract class Invoice extends Transactional { } get autoStockTransferLocation(): string | null { - const fieldname = this.isSales ? '' : 'purchasePaymentAccount'; + const fieldname = this.isSales + ? 'shipmentLocation' + : 'purchaseReceiptLocation'; const value = this.fyo.singles.Defaults?.[fieldname]; if (typeof value === 'string' && value.length) { return value; @@ -436,6 +438,12 @@ export abstract class Invoice extends Transactional { formula: () => !!this.autoPaymentAccount, dependsOn: [], }, + makeAutoStockTransfer: { + formula: () => + !!this.fyo.singles.AccountingSettings?.enableInventory && + !!this.autoStockTransferLocation, + dependsOn: [], + }, }; getStockTransferred() { @@ -511,6 +519,10 @@ export abstract class Invoice extends Transactional { static defaults: DefaultMap = { makeAutoPayment: (doc) => doc instanceof Invoice && !!doc.autoPaymentAccount, + makeAutoStockTransfer: (doc) => + !!doc.fyo.singles.AccountingSettings?.enableInventory && + doc instanceof Invoice && + !!doc.autoStockTransferLocation, numberSeries: (doc) => getNumberSeries(doc.schemaName, doc.fyo), terms: (doc) => { const defaults = doc.fyo.singles.Defaults as Defaults | undefined; @@ -669,9 +681,15 @@ export abstract class Invoice extends Transactional { if (isAuto) { const stock = - (await this.fyo.db.getStockQuantity(item, location!, data.date)) ?? 0; + (await this.fyo.db.getStockQuantity( + item, + location!, + undefined, + data.date + )) ?? 0; + console.log(quantity, stock); - if (stock <= quantity) { + if (stock < quantity) { continue; } }