2
0
mirror of https://github.com/frappe/books.git synced 2024-11-08 14:50:56 +00:00

feat: add auto stock transfer

This commit is contained in:
18alantom 2023-06-06 13:14:26 +05:30
parent e3b6d30608
commit ce4481b609
4 changed files with 66 additions and 9 deletions

View File

@ -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<string, Doc | undefined>;
export interface SinglesMap {
SystemSettings?: SystemSettings;
AccountingSettings?: AccountingSettings;
InventorySettings?: InventorySettings;
PrintSettings?: PrintSettings;
Defaults?: Defaults;
[key: string]: Doc | undefined;
}

View File

@ -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;

View File

@ -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<StockTransfer | null> {
async getStockTransfer(
isAuto: boolean = false
): Promise<StockTransfer | null> {
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,

View File

@ -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",