2
0
mirror of https://github.com/frappe/books.git synced 2024-11-15 09:54:04 +00:00
books/models/inventory/tests/testSerialNumbers.spec.ts

476 lines
11 KiB
TypeScript
Raw Normal View History

import {
assertDoesNotThrow,
assertThrows,
} from 'backend/database/tests/helpers';
2023-04-25 07:08:05 +00:00
import { ModelNameEnum } from 'models/types';
import test from 'tape';
import { closeTestFyo, getTestFyo, setupTestFyo } from 'tests/helpers';
import { getSerialNumbers } from '../helpers';
2023-05-04 10:45:12 +00:00
import { MovementTypeEnum } from '../types';
2023-04-25 07:08:05 +00:00
import { getItem, getStockMovement } from './helpers';
const fyo = getTestFyo();
setupTestFyo(fyo, __filename);
const itemMap = {
Pen: {
name: 'Pen',
rate: 700,
},
Ink: {
name: 'Ink',
rate: 50,
},
};
const locationMap = {
LocationOne: 'LocationOne',
LocationTwo: 'LocationTwo',
};
const partyMap = {
partyOne: { name: 'Someone', Role: 'Both' },
};
2023-05-04 10:45:12 +00:00
const serialNumberMap = {
2023-04-25 07:08:05 +00:00
serialOne: {
name: 'PN-AB001',
item: itemMap.Pen.name,
},
serialTwo: {
name: 'PN-AB002',
item: itemMap.Pen.name,
},
serialThree: {
name: 'PN-AB003',
item: itemMap.Pen.name,
},
};
2023-05-04 10:45:12 +00:00
test('create dummy items, locations, party & serialNumbers', async (t) => {
2023-04-25 07:08:05 +00:00
// Create Items
for (const { name, rate } of Object.values(itemMap)) {
const item = getItem(name, rate, false, true);
await fyo.doc.getNewDoc(ModelNameEnum.Item, item).sync();
}
// Create Locations
for (const name of Object.values(locationMap)) {
await fyo.doc.getNewDoc(ModelNameEnum.Location, { name }).sync();
}
// Create Party
await fyo.doc.getNewDoc(ModelNameEnum.Party, partyMap.partyOne).sync();
t.ok(
await fyo.db.exists(ModelNameEnum.Party, partyMap.partyOne.name),
'party created'
);
2023-05-04 10:45:12 +00:00
// Create SerialNumbers
for (const serialNumber of Object.values(serialNumberMap)) {
const doc = fyo.doc.getNewDoc(ModelNameEnum.SerialNumber, serialNumber);
2023-04-25 07:08:05 +00:00
await doc.sync();
const status = await fyo.getValue(
2023-05-04 10:45:12 +00:00
ModelNameEnum.SerialNumber,
serialNumber.name,
2023-04-25 07:08:05 +00:00
'status'
);
2023-05-04 10:45:12 +00:00
2023-04-25 07:08:05 +00:00
t.equal(
status,
'Inactive',
2023-05-04 10:45:12 +00:00
`${serialNumber.name} exists and inital status Inactive`
2023-04-25 07:08:05 +00:00
);
}
});
2023-05-04 10:45:12 +00:00
test('serialNumber enabled item, create stock movement, material receipt', async (t) => {
2023-04-25 07:08:05 +00:00
const { rate } = itemMap.Pen;
2023-05-04 10:45:12 +00:00
const serialNumber =
serialNumberMap.serialOne.name + '\n' + serialNumberMap.serialTwo.name;
2023-04-25 07:08:05 +00:00
const stockMovement = await getStockMovement(
2023-05-04 10:45:12 +00:00
MovementTypeEnum.MaterialReceipt,
2023-04-25 07:08:05 +00:00
new Date('2022-11-03T09:57:04.528'),
[
{
item: itemMap.Pen.name,
to: locationMap.LocationOne,
quantity: 2,
2023-05-04 10:45:12 +00:00
serialNumber,
2023-04-25 07:08:05 +00:00
rate,
},
],
fyo
);
await (await stockMovement.sync()).submit();
t.equal(
await fyo.db.getStockQuantity(
itemMap.Pen.name,
locationMap.LocationOne,
undefined,
undefined,
undefined,
2023-05-04 10:45:12 +00:00
[serialNumberMap.serialOne.name]
2023-04-25 07:08:05 +00:00
),
1,
2023-05-04 10:45:12 +00:00
'serialNumber one has quantity one'
2023-04-25 07:08:05 +00:00
);
t.equal(
await fyo.db.getStockQuantity(
itemMap.Pen.name,
locationMap.LocationOne,
undefined,
undefined,
undefined,
2023-05-04 10:45:12 +00:00
[serialNumberMap.serialTwo.name]
2023-04-25 07:08:05 +00:00
),
1,
2023-05-04 10:45:12 +00:00
'serialNumber two has quantity one'
2023-04-25 07:08:05 +00:00
);
t.equal(
await fyo.db.getStockQuantity(
itemMap.Pen.name,
locationMap.LocationOne,
undefined,
undefined,
undefined,
2023-05-04 10:45:12 +00:00
[serialNumberMap.serialThree.name]
2023-04-25 07:08:05 +00:00
),
null,
2023-05-04 10:45:12 +00:00
'serialNumber three has no quantity'
2023-04-25 07:08:05 +00:00
);
t.equal(
await fyo.db.getStockQuantity(
itemMap.Ink.name,
locationMap.LocationOne,
undefined,
undefined,
undefined,
2023-05-04 10:45:12 +00:00
[serialNumberMap.serialOne.name]
2023-04-25 07:08:05 +00:00
),
null,
'non transacted item has no quantity'
);
const statusOne = await fyo.getValue(
ModelNameEnum.SerialNumber,
serialNumberMap.serialOne.name,
'status'
);
t.equal(statusOne, 'Active', `serialNumber one is Active`);
2023-05-04 10:45:12 +00:00
const statusTwo = await fyo.getValue(
ModelNameEnum.SerialNumber,
serialNumberMap.serialOne.name,
'status'
);
t.equal(statusTwo, 'Active', 'serialNumber two is Active');
});
2023-05-04 10:45:12 +00:00
test('serialNumber enabled item, create stock movement, material issue', async (t) => {
2023-04-25 07:08:05 +00:00
const { rate } = itemMap.Pen;
const quantity = 1;
const stockMovement = await getStockMovement(
2023-05-04 10:45:12 +00:00
MovementTypeEnum.MaterialIssue,
2023-04-25 07:08:05 +00:00
new Date('2022-11-03T10:00:00.528'),
[
{
item: itemMap.Pen.name,
from: locationMap.LocationOne,
2023-05-04 10:45:12 +00:00
serialNumber: serialNumberMap.serialOne.name,
2023-04-25 07:08:05 +00:00
quantity,
rate,
},
],
fyo
);
await (await stockMovement.sync()).submit();
t.equal(
await fyo.db.getStockQuantity(
itemMap.Pen.name,
locationMap.LocationOne,
undefined,
undefined,
undefined,
2023-05-04 10:45:12 +00:00
[serialNumberMap.serialOne.name]
2023-04-25 07:08:05 +00:00
),
0,
2023-05-04 10:45:12 +00:00
'serialNumber one quantity transacted out'
2023-04-25 07:08:05 +00:00
);
t.equal(
await fyo.db.getStockQuantity(
itemMap.Pen.name,
locationMap.LocationOne,
undefined,
undefined,
undefined,
2023-05-04 10:45:12 +00:00
[serialNumberMap.serialTwo.name]
2023-04-25 07:08:05 +00:00
),
1,
2023-05-04 10:45:12 +00:00
'serialNumber two quantity intact'
2023-04-25 07:08:05 +00:00
);
});
2023-05-04 10:45:12 +00:00
test('serialNumber enabled item, create stock movement, material transfer', async (t) => {
2023-04-25 07:08:05 +00:00
const { rate } = itemMap.Pen;
const quantity = 1;
2023-05-04 10:45:12 +00:00
const serialNumber = serialNumberMap.serialTwo.name;
2023-04-25 07:08:05 +00:00
const stockMovement = await getStockMovement(
2023-05-04 10:45:12 +00:00
MovementTypeEnum.MaterialTransfer,
2023-04-25 07:08:05 +00:00
new Date('2022-11-03T09:58:04.528'),
[
{
item: itemMap.Pen.name,
from: locationMap.LocationOne,
to: locationMap.LocationTwo,
2023-05-04 10:45:12 +00:00
serialNumber,
2023-04-25 07:08:05 +00:00
quantity,
rate,
},
],
fyo
);
await (await stockMovement.sync()).submit();
t.equal(
await fyo.db.getStockQuantity(
itemMap.Pen.name,
locationMap.LocationOne,
undefined,
undefined,
undefined,
2023-05-04 10:45:12 +00:00
[serialNumber]
2023-04-25 07:08:05 +00:00
),
0,
2023-05-04 10:45:12 +00:00
'location one serialNumberTwo transacted out'
2023-04-25 07:08:05 +00:00
);
t.equal(
await fyo.db.getStockQuantity(
itemMap.Pen.name,
locationMap.LocationTwo,
undefined,
undefined,
undefined,
2023-05-04 10:45:12 +00:00
[serialNumber]
2023-04-25 07:08:05 +00:00
),
quantity,
2023-05-04 10:45:12 +00:00
'location two serialNumber transacted in'
2023-04-25 07:08:05 +00:00
);
});
2023-05-04 10:45:12 +00:00
test('serialNumber enabled item, create invalid stock movements', async (t) => {
2023-04-25 07:08:05 +00:00
const { name, rate } = itemMap.Pen;
const quantity = await fyo.db.getStockQuantity(
itemMap.Pen.name,
locationMap.LocationTwo,
undefined,
undefined,
undefined,
2023-05-04 10:45:12 +00:00
[serialNumberMap.serialTwo.name]
2023-04-25 07:08:05 +00:00
);
2023-05-04 10:45:12 +00:00
t.equal(quantity, 1, 'location two, serialNumber one has quantity');
2023-04-25 07:08:05 +00:00
if (!quantity) {
return;
}
let stockMovement = await getStockMovement(
2023-05-04 10:45:12 +00:00
MovementTypeEnum.MaterialIssue,
2023-04-25 07:08:05 +00:00
new Date('2022-11-03T09:59:04.528'),
[
{
item: itemMap.Pen.name,
from: locationMap.LocationTwo,
2023-05-04 10:45:12 +00:00
serialNumber: serialNumberMap.serialOne.name,
2023-04-25 07:08:05 +00:00
quantity,
rate,
},
],
fyo
);
await assertThrows(
async () => (await stockMovement.sync()).submit(),
'invalid stockMovement with insufficient quantity did not throw'
);
stockMovement = await getStockMovement(
2023-05-04 10:45:12 +00:00
MovementTypeEnum.MaterialIssue,
2023-04-25 07:08:05 +00:00
new Date('2022-11-03T09:59:04.528'),
[
{
item: itemMap.Pen.name,
from: locationMap.LocationTwo,
quantity,
rate,
},
],
fyo
);
await assertThrows(
async () => (await stockMovement.sync()).submit(),
2023-05-04 10:45:12 +00:00
'invalid stockMovement without serialNumber did not throw'
2023-04-25 07:08:05 +00:00
);
t.equal(await fyo.db.getStockQuantity(name), 1, 'item still has quantity');
});
test('Material Receipt, auto creation of Serial Number', async (t) => {
const serialNumber = `001\n002\n003`;
const serialNumbers = getSerialNumbers(serialNumber);
for (const sn of serialNumbers) {
t.equal(
await fyo.db.exists(ModelNameEnum.SerialNumber, sn),
false,
`Serial Number${sn} does not exist`
);
}
const doc = await getStockMovement(
MovementTypeEnum.MaterialReceipt,
new Date('2022-11-04T09:59:04.528'),
[
{
item: itemMap.Pen.name,
to: locationMap.LocationOne,
quantity: 3,
rate: 100,
serialNumber,
},
],
fyo
);
await (await doc.sync()).submit();
for (const sn of serialNumbers) {
await assertDoesNotThrow(async () => {
const sndoc = await fyo.doc.getDoc(ModelNameEnum.SerialNumber, sn);
t.equal(sndoc.status, 'Active', `Serial Number ${sn} updated to Active`);
}, `SerialNumber ${sn} exists`);
}
t.equal(
await fyo.db.getStockQuantity(
itemMap.Pen.name,
locationMap.LocationOne,
undefined,
undefined,
undefined,
serialNumbers
),
3,
'location one has quantity 3 of incoming serialNumbers'
);
});
test('Material Issue, status change of Serial Number', async (t) => {
const serialNumber = `001\n002\n003`;
const serialNumbers = getSerialNumbers(serialNumber);
for (const sn of serialNumbers) {
t.equal(
await fyo.db.exists(ModelNameEnum.SerialNumber, sn),
true,
`Serial Number${sn} exists`
);
}
const doc = await getStockMovement(
MovementTypeEnum.MaterialIssue,
new Date('2022-11-05T09:59:04.528'),
[
{
item: itemMap.Pen.name,
from: locationMap.LocationOne,
quantity: 3,
rate: 100,
serialNumber,
},
],
fyo
);
await (await doc.sync()).submit();
for (const sn of serialNumbers) {
const status = await fyo.getValue(ModelNameEnum.SerialNumber, sn, 'status');
t.equal(status, 'Delivered', `Serial Number ${sn} updated to Delivered`);
}
t.equal(
await fyo.db.getStockQuantity(
itemMap.Pen.name,
locationMap.LocationOne,
undefined,
undefined,
undefined,
serialNumbers
),
0,
'location one has quantity 0 of serialNumbers after issue'
);
2023-05-09 06:59:44 +00:00
await doc.cancel();
for (const sn of serialNumbers) {
const status = await fyo.getValue(ModelNameEnum.SerialNumber, sn, 'status');
t.equal(
status,
'Active',
`Serial Number ${sn} updated to Active post cancel`
);
}
});
test('Material Receipt cancellation, Serial Number status update', async (t) => {
const serialNumber = `004\n005\n006`;
const serialNumbers = getSerialNumbers(serialNumber);
const doc = await getStockMovement(
MovementTypeEnum.MaterialReceipt,
new Date('2022-11-04T09:59:04.528'),
[
{
item: itemMap.Pen.name,
to: locationMap.LocationOne,
quantity: 3,
rate: 100,
serialNumber,
},
],
fyo
);
await (await doc.sync()).submit();
for (const sn of serialNumbers) {
const status = await fyo.getValue(ModelNameEnum.SerialNumber, sn, 'status');
t.equal(
status,
'Active',
`Serial Number ${sn} updated to Active after submit`
);
}
await doc.cancel();
for (const sn of serialNumbers) {
const status = await fyo.getValue(ModelNameEnum.SerialNumber, sn, 'status');
t.equal(
status,
'Inactive',
`Serial Number ${sn} updated to InActive after cancel`
);
}
});
2023-04-25 07:08:05 +00:00
closeTestFyo(fyo, __filename);