2023-06-05 06:09:06 +00:00
|
|
|
import { t } from 'fyo';
|
2023-07-31 12:21:16 +00:00
|
|
|
import { Attachment, DocValueMap } from 'fyo/core/types';
|
2022-11-14 08:30:11 +00:00
|
|
|
import { Doc } from 'fyo/model/doc';
|
2023-02-21 06:42:06 +00:00
|
|
|
import {
|
2023-03-29 06:14:59 +00:00
|
|
|
ChangeArg,
|
2023-02-21 06:42:06 +00:00
|
|
|
DefaultMap,
|
|
|
|
FiltersMap,
|
|
|
|
FormulaMap,
|
|
|
|
HiddenMap,
|
|
|
|
} from 'fyo/model/types';
|
2022-11-21 06:35:05 +00:00
|
|
|
import { ValidationError } from 'fyo/utils/errors';
|
2023-05-04 10:45:12 +00:00
|
|
|
import { LedgerPosting } from 'models/Transactional/LedgerPosting';
|
2022-11-18 17:31:50 +00:00
|
|
|
import { Defaults } from 'models/baseModels/Defaults/Defaults';
|
2022-11-22 09:12:49 +00:00
|
|
|
import { Invoice } from 'models/baseModels/Invoice/Invoice';
|
2023-06-05 06:09:06 +00:00
|
|
|
import { addItem, getNumberSeries } from 'models/helpers';
|
2022-11-18 17:31:50 +00:00
|
|
|
import { ModelNameEnum } from 'models/types';
|
|
|
|
import { Money } from 'pesa';
|
2023-03-29 06:14:59 +00:00
|
|
|
import { TargetField } from 'schemas/types';
|
2023-05-05 05:43:04 +00:00
|
|
|
import { SerialNumber } from './SerialNumber';
|
2022-11-18 17:31:50 +00:00
|
|
|
import { StockTransferItem } from './StockTransferItem';
|
|
|
|
import { Transfer } from './Transfer';
|
2023-05-04 10:45:12 +00:00
|
|
|
import {
|
2023-05-05 05:43:04 +00:00
|
|
|
canValidateSerialNumber,
|
2023-05-04 10:45:12 +00:00
|
|
|
getSerialNumberFromDoc,
|
|
|
|
updateSerialNumbers,
|
|
|
|
validateBatch,
|
|
|
|
validateSerialNumber,
|
|
|
|
} from './helpers';
|
2023-07-31 12:21:16 +00:00
|
|
|
import { ReturnDocItem } from './types';
|
2023-09-25 07:00:20 +00:00
|
|
|
import { getShipmentCOGSAmountFromSLEs } from 'reports/inventory/helpers';
|
2022-11-14 08:30:11 +00:00
|
|
|
|
2022-11-18 17:31:50 +00:00
|
|
|
export abstract class StockTransfer extends Transfer {
|
2022-11-14 08:30:11 +00:00
|
|
|
name?: string;
|
2022-11-18 17:31:50 +00:00
|
|
|
date?: Date;
|
2022-11-14 08:30:11 +00:00
|
|
|
party?: string;
|
|
|
|
terms?: string;
|
|
|
|
attachment?: Attachment;
|
2022-11-18 17:31:50 +00:00
|
|
|
grandTotal?: Money;
|
2022-11-22 09:12:49 +00:00
|
|
|
backReference?: string;
|
2022-11-18 17:31:50 +00:00
|
|
|
items?: StockTransferItem[];
|
2023-07-31 12:21:16 +00:00
|
|
|
isReturned?: boolean;
|
2023-07-01 07:31:00 +00:00
|
|
|
returnAgainst?: string;
|
2022-11-18 17:31:50 +00:00
|
|
|
|
|
|
|
get isSales() {
|
|
|
|
return this.schemaName === ModelNameEnum.Shipment;
|
|
|
|
}
|
|
|
|
|
2023-07-31 12:21:16 +00:00
|
|
|
get isReturn(): boolean {
|
2023-08-19 04:45:58 +00:00
|
|
|
return !!this.returnAgainst;
|
2023-07-31 12:21:16 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 06:09:06 +00:00
|
|
|
get invoiceSchemaName() {
|
|
|
|
if (this.isSales) {
|
|
|
|
return ModelNameEnum.SalesInvoice;
|
|
|
|
}
|
|
|
|
return ModelNameEnum.PurchaseInvoice;
|
|
|
|
}
|
|
|
|
|
2022-11-18 17:31:50 +00:00
|
|
|
formulas: FormulaMap = {
|
|
|
|
grandTotal: {
|
|
|
|
formula: () => this.getSum('items', 'amount', false),
|
|
|
|
dependsOn: ['items'],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-02-21 06:42:06 +00:00
|
|
|
hidden: HiddenMap = {
|
|
|
|
backReference: () =>
|
|
|
|
!(this.backReference || !(this.isSubmitted || this.isCancelled)),
|
|
|
|
terms: () => !(this.terms || !(this.isSubmitted || this.isCancelled)),
|
|
|
|
attachment: () =>
|
|
|
|
!(this.attachment || !(this.isSubmitted || this.isCancelled)),
|
2023-08-19 04:42:54 +00:00
|
|
|
returnAgainst: () =>
|
|
|
|
(this.isSubmitted || this.isCancelled) && !this.returnAgainst,
|
2023-02-21 06:42:06 +00:00
|
|
|
};
|
|
|
|
|
2022-11-18 17:31:50 +00:00
|
|
|
static defaults: DefaultMap = {
|
|
|
|
numberSeries: (doc) => getNumberSeries(doc.schemaName, doc.fyo),
|
|
|
|
terms: (doc) => {
|
2023-06-22 08:52:54 +00:00
|
|
|
const defaults = doc.fyo.singles.Defaults;
|
2022-11-18 17:31:50 +00:00
|
|
|
if (doc.schemaName === ModelNameEnum.Shipment) {
|
|
|
|
return defaults?.shipmentTerms ?? '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaults?.purchaseReceiptTerms ?? '';
|
|
|
|
},
|
2022-12-05 10:01:31 +00:00
|
|
|
date: () => new Date(),
|
2022-11-18 17:31:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static filters: FiltersMap = {
|
|
|
|
party: (doc: Doc) => ({
|
|
|
|
role: ['in', [doc.isSales ? 'Customer' : 'Supplier', 'Both']],
|
|
|
|
}),
|
|
|
|
numberSeries: (doc: Doc) => ({ referenceType: doc.schemaName }),
|
2023-03-29 06:14:59 +00:00
|
|
|
backReference: () => ({
|
2023-03-29 06:45:47 +00:00
|
|
|
stockNotTransferred: ['!=', 0],
|
2023-03-29 06:14:59 +00:00
|
|
|
submitted: true,
|
|
|
|
cancelled: false,
|
|
|
|
}),
|
2022-11-18 17:31:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
override _getTransferDetails() {
|
|
|
|
return (this.items ?? []).map((row) => {
|
|
|
|
let fromLocation = undefined;
|
|
|
|
let toLocation = undefined;
|
|
|
|
|
|
|
|
if (this.isSales) {
|
|
|
|
fromLocation = row.location;
|
|
|
|
} else {
|
|
|
|
toLocation = row.location;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
item: row.item!,
|
|
|
|
rate: row.rate!,
|
|
|
|
quantity: row.quantity!,
|
2023-02-28 06:01:04 +00:00
|
|
|
batch: row.batch!,
|
2023-05-04 10:45:12 +00:00
|
|
|
serialNumber: row.serialNumber!,
|
2023-07-31 12:21:16 +00:00
|
|
|
isReturn: row.isReturn,
|
2022-11-18 17:31:50 +00:00
|
|
|
fromLocation,
|
|
|
|
toLocation,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
override async getPosting(): Promise<LedgerPosting | null> {
|
|
|
|
await this.validateAccounts();
|
|
|
|
const stockInHand = (await this.fyo.getValue(
|
|
|
|
ModelNameEnum.InventorySettings,
|
|
|
|
'stockInHand'
|
|
|
|
)) as string;
|
|
|
|
|
2023-09-25 07:00:20 +00:00
|
|
|
const amount = await this.getPostingAmount();
|
2022-11-18 17:31:50 +00:00
|
|
|
const posting = new LedgerPosting(this, this.fyo);
|
|
|
|
|
|
|
|
if (this.isSales) {
|
|
|
|
const costOfGoodsSold = (await this.fyo.getValue(
|
|
|
|
ModelNameEnum.InventorySettings,
|
|
|
|
'costOfGoodsSold'
|
|
|
|
)) as string;
|
|
|
|
|
2023-07-01 07:31:00 +00:00
|
|
|
if (this.isReturn) {
|
|
|
|
await posting.debit(stockInHand, amount);
|
|
|
|
await posting.credit(costOfGoodsSold, amount);
|
|
|
|
} else {
|
|
|
|
await posting.debit(costOfGoodsSold, amount);
|
|
|
|
await posting.credit(stockInHand, amount);
|
|
|
|
}
|
2022-11-18 17:31:50 +00:00
|
|
|
} else {
|
|
|
|
const stockReceivedButNotBilled = (await this.fyo.getValue(
|
|
|
|
ModelNameEnum.InventorySettings,
|
|
|
|
'stockReceivedButNotBilled'
|
|
|
|
)) as string;
|
|
|
|
|
2023-07-01 07:31:00 +00:00
|
|
|
if (this.isReturn) {
|
|
|
|
await posting.debit(stockReceivedButNotBilled, amount);
|
|
|
|
await posting.credit(stockInHand, amount);
|
|
|
|
} else {
|
|
|
|
await posting.debit(stockInHand, amount);
|
|
|
|
await posting.credit(stockReceivedButNotBilled, amount);
|
|
|
|
}
|
2022-11-18 17:31:50 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 06:35:05 +00:00
|
|
|
await posting.makeRoundOffEntry();
|
2022-11-18 17:31:50 +00:00
|
|
|
return posting;
|
|
|
|
}
|
|
|
|
|
2023-09-25 07:00:20 +00:00
|
|
|
async getPostingAmount(): Promise<Money> {
|
|
|
|
if (!this.isSales) {
|
|
|
|
return this.grandTotal ?? this.fyo.pesa(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await getShipmentCOGSAmountFromSLEs(this);
|
|
|
|
}
|
|
|
|
|
2022-11-18 17:31:50 +00:00
|
|
|
async validateAccounts() {
|
|
|
|
const settings: string[] = ['stockInHand'];
|
|
|
|
if (this.isSales) {
|
|
|
|
settings.push('costOfGoodsSold');
|
|
|
|
} else {
|
|
|
|
settings.push('stockReceivedButNotBilled');
|
|
|
|
}
|
|
|
|
|
|
|
|
const messages: string[] = [];
|
|
|
|
for (const setting of settings) {
|
|
|
|
const value = this.fyo.singles.InventorySettings?.[setting] as
|
|
|
|
| string
|
|
|
|
| undefined;
|
|
|
|
const field = this.fyo.getField(ModelNameEnum.InventorySettings, setting);
|
|
|
|
if (!value) {
|
|
|
|
messages.push(t`${field.label} account not set in Inventory Settings.`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const exists = await this.fyo.db.exists(ModelNameEnum.Account, value);
|
|
|
|
if (!exists) {
|
|
|
|
messages.push(t`Account ${value} does not exist.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (messages.length) {
|
|
|
|
throw new ValidationError(messages.join(' '));
|
|
|
|
}
|
|
|
|
}
|
2022-11-21 06:35:05 +00:00
|
|
|
|
2023-02-27 13:09:18 +00:00
|
|
|
override async validate(): Promise<void> {
|
|
|
|
await super.validate();
|
2023-02-28 06:01:04 +00:00
|
|
|
await validateBatch(this);
|
2023-05-04 10:45:12 +00:00
|
|
|
await validateSerialNumber(this);
|
|
|
|
await validateSerialNumberStatus(this);
|
2023-08-19 05:03:04 +00:00
|
|
|
await this._validateHasReturnDocs();
|
2023-02-27 13:09:18 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 09:12:49 +00:00
|
|
|
async afterSubmit() {
|
|
|
|
await super.afterSubmit();
|
2023-07-31 12:21:16 +00:00
|
|
|
await updateSerialNumbers(this, false, this.isReturn);
|
2022-11-22 09:12:49 +00:00
|
|
|
await this._updateBackReference();
|
2023-07-01 07:31:00 +00:00
|
|
|
await this._updateItemsReturned();
|
2022-11-22 09:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async afterCancel(): Promise<void> {
|
|
|
|
await super.afterCancel();
|
2023-07-31 12:21:16 +00:00
|
|
|
await updateSerialNumbers(this, true, this.isReturn);
|
2022-11-22 09:12:49 +00:00
|
|
|
await this._updateBackReference();
|
2023-07-01 07:31:00 +00:00
|
|
|
await this._updateItemsReturned();
|
2022-11-22 09:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async _updateBackReference() {
|
|
|
|
if (!this.isCancelled && !this.isSubmitted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.backReference) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const schemaName = this.isSales
|
|
|
|
? ModelNameEnum.SalesInvoice
|
|
|
|
: ModelNameEnum.PurchaseInvoice;
|
|
|
|
|
|
|
|
const invoice = (await this.fyo.doc.getDoc(
|
|
|
|
schemaName,
|
|
|
|
this.backReference
|
|
|
|
)) as Invoice;
|
|
|
|
const transferMap = this._getTransferMap();
|
|
|
|
|
|
|
|
for (const row of invoice.items ?? []) {
|
|
|
|
const item = row.item!;
|
|
|
|
const quantity = row.quantity!;
|
|
|
|
const notTransferred = (row.stockNotTransferred as number) ?? 0;
|
|
|
|
|
|
|
|
const transferred = transferMap[item];
|
2022-11-23 08:17:23 +00:00
|
|
|
if (
|
|
|
|
typeof transferred !== 'number' ||
|
|
|
|
typeof notTransferred !== 'number'
|
|
|
|
) {
|
2022-11-22 09:12:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isCancelled) {
|
|
|
|
await row.set(
|
|
|
|
'stockNotTransferred',
|
|
|
|
Math.min(notTransferred + transferred, quantity)
|
|
|
|
);
|
|
|
|
transferMap[item] = Math.max(
|
|
|
|
transferred + notTransferred - quantity,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
await row.set(
|
|
|
|
'stockNotTransferred',
|
|
|
|
Math.max(notTransferred - transferred, 0)
|
|
|
|
);
|
|
|
|
transferMap[item] = Math.max(transferred - notTransferred, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const notTransferred = invoice.getStockNotTransferred();
|
|
|
|
await invoice.setAndSync('stockNotTransferred', notTransferred);
|
|
|
|
}
|
|
|
|
|
2023-07-01 07:31:00 +00:00
|
|
|
async _updateItemsReturned() {
|
2023-08-19 04:51:41 +00:00
|
|
|
if (!this.returnAgainst) {
|
2023-08-01 05:45:06 +00:00
|
|
|
return;
|
2023-07-01 07:31:00 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 12:21:16 +00:00
|
|
|
const linkedReference = await this.loadAndGetLink('returnAgainst');
|
|
|
|
if (!linkedReference) {
|
|
|
|
return;
|
|
|
|
}
|
2023-07-01 07:31:00 +00:00
|
|
|
|
|
|
|
const referenceDoc = await this.fyo.doc.getDoc(
|
2023-07-31 12:21:16 +00:00
|
|
|
this.schemaName,
|
|
|
|
linkedReference.name
|
2023-07-01 07:31:00 +00:00
|
|
|
);
|
|
|
|
|
2023-08-01 05:45:06 +00:00
|
|
|
const isReturned = this.isSubmitted;
|
2023-07-31 12:21:16 +00:00
|
|
|
await referenceDoc.setAndSync({ isReturned });
|
2023-07-01 07:31:00 +00:00
|
|
|
}
|
|
|
|
|
2023-08-01 05:45:06 +00:00
|
|
|
async _validateHasReturnDocs() {
|
2023-08-19 05:03:04 +00:00
|
|
|
if (!this.name || !this.isCancelled) {
|
2023-08-01 05:45:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const returnDocs = await this.fyo.db.getAll(this.schemaName, {
|
|
|
|
filters: { returnAgainst: this.name },
|
|
|
|
});
|
|
|
|
|
|
|
|
const hasReturnDocs = !!returnDocs.length;
|
|
|
|
if (!hasReturnDocs) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const returnDocNames = returnDocs.map((doc) => doc.name).join(', ');
|
|
|
|
const label = this.fyo.schemaMap[this.schemaName]?.label ?? this.schemaName;
|
|
|
|
|
|
|
|
throw new ValidationError(
|
|
|
|
t`Cannot cancel ${this.schema.label} ${this.name} because of the following ${label}: ${returnDocNames}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-22 09:12:49 +00:00
|
|
|
_getTransferMap() {
|
|
|
|
return (this.items ?? []).reduce((acc, item) => {
|
|
|
|
if (!item.item) {
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!item.quantity) {
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
|
|
|
acc[item.item] ??= 0;
|
|
|
|
acc[item.item] += item.quantity;
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {} as Record<string, number>);
|
|
|
|
}
|
2022-11-23 08:17:23 +00:00
|
|
|
|
|
|
|
override duplicate(): Doc {
|
|
|
|
const doc = super.duplicate() as StockTransfer;
|
|
|
|
doc.backReference = undefined;
|
|
|
|
return doc;
|
|
|
|
}
|
2022-12-01 08:31:23 +00:00
|
|
|
|
|
|
|
static createFilters: FiltersMap = {
|
|
|
|
party: (doc: Doc) => ({
|
|
|
|
role: doc.isSales ? 'Customer' : 'Supplier',
|
|
|
|
}),
|
|
|
|
};
|
2023-01-16 09:38:02 +00:00
|
|
|
|
|
|
|
async addItem(name: string) {
|
|
|
|
return await addItem(name, this);
|
|
|
|
}
|
2023-03-29 06:14:59 +00:00
|
|
|
|
|
|
|
override async change({ doc, changed }: ChangeArg): Promise<void> {
|
|
|
|
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);
|
|
|
|
}
|
2023-06-05 06:09:06 +00:00
|
|
|
|
|
|
|
async getInvoice(): Promise<Invoice | null> {
|
|
|
|
if (!this.isSubmitted || this.backReference) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const schemaName = this.invoiceSchemaName;
|
|
|
|
|
|
|
|
const defaults = (this.fyo.singles.Defaults as Defaults) ?? {};
|
|
|
|
let terms;
|
|
|
|
let numberSeries;
|
|
|
|
if (this.isSales) {
|
|
|
|
terms = defaults.salesInvoiceTerms ?? '';
|
|
|
|
numberSeries = defaults.salesInvoiceNumberSeries ?? undefined;
|
|
|
|
} else {
|
|
|
|
terms = defaults.purchaseInvoiceTerms ?? '';
|
|
|
|
numberSeries = defaults.purchaseInvoiceNumberSeries ?? undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
party: this.party,
|
|
|
|
date: new Date().toISOString(),
|
|
|
|
terms,
|
|
|
|
numberSeries,
|
|
|
|
backReference: this.name,
|
|
|
|
};
|
|
|
|
|
|
|
|
const invoice = this.fyo.doc.getNewDoc(schemaName, data) as Invoice;
|
|
|
|
for (const row of this.items ?? []) {
|
|
|
|
if (!row.item) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const item = row.item;
|
|
|
|
const unit = row.unit;
|
|
|
|
const quantity = row.quantity;
|
|
|
|
const batch = row.batch || null;
|
|
|
|
const rate = row.rate ?? this.fyo.pesa(0);
|
|
|
|
const description = row.description;
|
|
|
|
const hsnCode = row.hsnCode;
|
|
|
|
|
|
|
|
if (!quantity) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
await invoice.append('items', {
|
|
|
|
item,
|
|
|
|
quantity,
|
|
|
|
unit,
|
|
|
|
rate,
|
|
|
|
batch,
|
|
|
|
hsnCode,
|
|
|
|
description,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!invoice.items?.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return invoice;
|
|
|
|
}
|
2023-07-01 07:31:00 +00:00
|
|
|
|
2023-07-31 12:21:16 +00:00
|
|
|
async getReturnDoc(): Promise<StockTransfer | undefined> {
|
|
|
|
if (!this.name) {
|
|
|
|
return;
|
2023-07-01 07:31:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const docData = this.getValidDict(true, true);
|
2023-07-31 12:21:16 +00:00
|
|
|
const docItems = docData.items as DocValueMap[];
|
|
|
|
|
|
|
|
if (!docItems) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let returnDocItems: DocValueMap[] = [];
|
2023-07-01 07:31:00 +00:00
|
|
|
|
|
|
|
const returnBalanceItemsQty = await this.fyo.db.getReturnBalanceItemsQty(
|
2023-07-31 12:21:16 +00:00
|
|
|
this.schemaName,
|
|
|
|
this.name
|
2023-07-01 07:31:00 +00:00
|
|
|
);
|
|
|
|
for (const item of docItems) {
|
2023-07-31 12:21:16 +00:00
|
|
|
if (!returnBalanceItemsQty) {
|
|
|
|
returnDocItems = docItems;
|
|
|
|
returnDocItems.map((row) => {
|
|
|
|
row.name = undefined;
|
|
|
|
(row.quantity as number) *= -1;
|
|
|
|
return row;
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isItemExist = !!returnDocItems.filter(
|
|
|
|
(balanceItem) => balanceItem.item === item.item
|
|
|
|
).length;
|
|
|
|
|
|
|
|
if (isItemExist) {
|
2023-07-01 07:31:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-07-31 12:21:16 +00:00
|
|
|
const returnedItem: ReturnDocItem | undefined =
|
|
|
|
returnBalanceItemsQty[item.item as string];
|
|
|
|
|
|
|
|
let quantity = returnedItem.quantity;
|
|
|
|
let serialNumber: string | undefined =
|
|
|
|
returnedItem.serialNumbers?.join('\n');
|
2023-07-01 07:31:00 +00:00
|
|
|
|
2023-07-31 12:21:16 +00:00
|
|
|
if (
|
|
|
|
item.batch &&
|
|
|
|
returnedItem.batches &&
|
|
|
|
returnedItem.batches[item.batch as string]
|
|
|
|
) {
|
|
|
|
quantity = returnedItem.batches[item.batch as string].quantity;
|
2023-07-01 07:31:00 +00:00
|
|
|
|
2023-07-31 12:21:16 +00:00
|
|
|
if (returnedItem.batches[item.batch as string].serialNumbers) {
|
|
|
|
serialNumber =
|
|
|
|
returnedItem.batches[item.batch as string].serialNumbers?.join(
|
|
|
|
'\n'
|
|
|
|
);
|
2023-07-01 07:31:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-31 12:21:16 +00:00
|
|
|
returnDocItems.push({
|
|
|
|
...item,
|
|
|
|
serialNumber,
|
|
|
|
name: undefined,
|
|
|
|
quantity: quantity,
|
|
|
|
});
|
2023-07-01 07:31:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const returnDocData = {
|
|
|
|
...docData,
|
2023-07-31 12:21:16 +00:00
|
|
|
name: undefined,
|
2023-07-01 07:31:00 +00:00
|
|
|
date: new Date(),
|
|
|
|
items: returnDocItems,
|
|
|
|
returnAgainst: docData.name,
|
2023-07-31 12:21:16 +00:00
|
|
|
} as DocValueMap;
|
2023-07-01 07:31:00 +00:00
|
|
|
|
2023-07-31 12:21:16 +00:00
|
|
|
const newReturnDoc = this.fyo.doc.getNewDoc(
|
2023-07-01 07:31:00 +00:00
|
|
|
this.schema.name,
|
|
|
|
returnDocData
|
|
|
|
) as StockTransfer;
|
|
|
|
|
2023-07-31 12:21:16 +00:00
|
|
|
await newReturnDoc.runFormulas();
|
|
|
|
return newReturnDoc;
|
2023-07-01 07:31:00 +00:00
|
|
|
}
|
2022-11-14 08:30:11 +00:00
|
|
|
}
|
2023-05-04 10:45:12 +00:00
|
|
|
|
|
|
|
async function validateSerialNumberStatus(doc: StockTransfer) {
|
2023-05-08 09:37:13 +00:00
|
|
|
if (doc.isCancelled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-05 05:43:04 +00:00
|
|
|
for (const { serialNumber, item } of getSerialNumberFromDoc(doc)) {
|
|
|
|
const cannotValidate = !(await canValidateSerialNumber(item, serialNumber));
|
|
|
|
if (cannotValidate) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-05-04 10:45:12 +00:00
|
|
|
const snDoc = await doc.fyo.doc.getDoc(
|
|
|
|
ModelNameEnum.SerialNumber,
|
|
|
|
serialNumber
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!(snDoc instanceof SerialNumber)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const status = snDoc.status ?? 'Inactive';
|
2023-07-31 12:21:16 +00:00
|
|
|
const isSubmitted = !!doc.isSubmitted;
|
|
|
|
const isReturn = !!doc.returnAgainst;
|
|
|
|
|
|
|
|
if (isSubmitted || isReturn) {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-04 10:45:12 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
doc.schemaName === ModelNameEnum.PurchaseReceipt &&
|
|
|
|
status !== 'Inactive'
|
|
|
|
) {
|
|
|
|
throw new ValidationError(
|
|
|
|
t`Serial Number ${serialNumber} is not Inactive`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doc.schemaName === ModelNameEnum.Shipment && status !== 'Active') {
|
|
|
|
throw new ValidationError(
|
|
|
|
t`Serial Number ${serialNumber} is not Active.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|