salix/services/db/tests/vn/ticketComponentUpdateSale.s...

290 lines
10 KiB
JavaScript
Raw Normal View History

const app = require(`../../../ticket/server/server`);
const server = require(`../../../loopback/server/server`);
const ParameterizedSQL = server.loopbackConnector.ParameterizedSQL;
describe('ticketComponentUpdateSale()', () => {
it(`should update the sale price when option ONE using the base components and reclaculate only the ones with isRenewable TRUE`, async() => {
let stmts = [];
let stmt;
let params = {
warehouseFk: 1,
ticketFk: 1
};
stmts.push('START TRANSACTION');
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.sale');
// createSaleTempTable code comes from vn.ticketComponentUpdate procedure
let createSaleTempTable = `
CREATE TEMPORARY TABLE tmp.sale
(PRIMARY KEY (saleFk))
ENGINE = MEMORY
SELECT id AS saleFk, ? warehouseFk
FROM sale s WHERE s.ticketFk = ?
`;
stmt = new ParameterizedSQL(createSaleTempTable, [
params.warehouseFk,
params.ticketFk
]);
stmts.push(stmt);
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.ticketComponent');
// createTicketComponentTable code comes partially from vn.ticketComponenetCalculate procedure
let createTicketComponentTable = `
CREATE TEMPORARY TABLE tmp.ticketComponent (
warehouseFk INT UNSIGNED NOT NULL,
itemFk INT NOT NULL,
componentFk INT UNSIGNED NOT NULL,
cost DECIMAL(10,4) NOT NULL,
INDEX itemWarehouse USING BTREE (itemFk ASC, warehouseFk ASC),
UNIQUE INDEX itemWarehouseComponent (itemFk ASC, warehouseFk ASC, componentFk ASC))
`;
stmts.push(createTicketComponentTable);
let insertTicketComponentTable = `
INSERT INTO tmp.ticketComponent (warehouseFk, itemFk, componentFk, cost)
VALUES
(1 , 2 , 23 , 999.00),
(1 , 2 , 28 , 2.00),
(1 , 4 , 23 , 999.00),
(1 , 4 , 28 , 1.00)
`;
stmts.push(insertTicketComponentTable);
let updateComponentTwentyThree = `
UPDATE vn.saleComponent
SET value = '5'
WHERE saleFk = 1 AND componentFk = 23
`;
stmts.push(updateComponentTwentyThree);
let updateComponentTwentyEight = `
UPDATE vn.saleComponent
SET value = '5'
WHERE saleFk = 1 AND componentFk = 28
`;
stmts.push(updateComponentTwentyEight);
let priceFixtedToZero = `
UPDATE vn.sale
SET priceFixed = 0
WHERE id = 1
`;
stmts.push(priceFixtedToZero);
let firstSalePriceIndexBefore = stmts.push('SELECT price FROM vn.sale WHERE id = 1') - 1;
stmts.push('CALL vn.ticketComponentUpdateSale(1)');
let firstSalePriceIndexAfter = stmts.push('SELECT price FROM vn.sale WHERE id = 1') - 1;
stmts.push('ROLLBACK');
let sql = ParameterizedSQL.join(stmts, ';');
let result = await app.models.Ticket.rawStmt(sql);
expect(result[firstSalePriceIndexBefore][0].price).toEqual(9.1);
expect(result[firstSalePriceIndexAfter][0].price).toEqual(5);
});
it(`should keep the sale price when option TWO using the base components and save the difference in a new component`, async() => {
let stmts = [];
let stmt;
let params = {
warehouseFk: 1,
ticketFk: 1
};
stmts.push('START TRANSACTION');
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.sale');
// createSaleTempTable code comes from vn.ticketComponentUpdate procedure
let createSaleTempTable = `
CREATE TEMPORARY TABLE tmp.sale
(PRIMARY KEY (saleFk))
ENGINE = MEMORY
SELECT id AS saleFk, ? warehouseFk
FROM sale s WHERE s.ticketFk = ?
`;
stmt = new ParameterizedSQL(createSaleTempTable, [
params.warehouseFk,
params.ticketFk
]);
stmts.push(stmt);
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.ticketComponent');
// createTicketComponentTable code comes partially from vn.ticketComponenetCalculate procedure
let createTicketComponentTable = `
CREATE TEMPORARY TABLE tmp.ticketComponent (
warehouseFk INT UNSIGNED NOT NULL,
itemFk INT NOT NULL,
componentFk INT UNSIGNED NOT NULL,
cost DECIMAL(10,4) NOT NULL,
INDEX itemWarehouse USING BTREE (itemFk ASC, warehouseFk ASC),
UNIQUE INDEX itemWarehouseComponent (itemFk ASC, warehouseFk ASC, componentFk ASC))
`;
stmts.push(createTicketComponentTable);
let insertTicketComponentTable = `
INSERT INTO tmp.ticketComponent (warehouseFk, itemFk, componentFk, cost)
VALUES
(1 , 2 , 23 , 999.00),
(1 , 2 , 28 , 2.00),
(1 , 4 , 23 , 999.00),
(1 , 4 , 28 , 1.00)
`;
stmts.push(insertTicketComponentTable);
let updateComponentTwentyThree = `
UPDATE vn.saleComponent
SET value = '5'
WHERE saleFk = 1 AND componentFk = 23
`;
stmts.push(updateComponentTwentyThree);
let updateComponentTwentyEight = `
UPDATE vn.saleComponent
SET value = '5'
WHERE saleFk = 1 AND componentFk = 28
`;
stmts.push(updateComponentTwentyEight);
let priceFixtedToZero = `
UPDATE vn.sale
SET priceFixed = 0
WHERE id = 1
`;
stmts.push(priceFixtedToZero);
let firstSalePriceIndexBefore = stmts.push('SELECT price FROM vn.sale WHERE id = 1') - 1;
stmts.push('CALL vn.ticketComponentUpdateSale(2)');
let addedComponentIndex = stmts.push('SELECT * FROM vn.saleComponent WHERE saleFk = 1 AND componentFk = 17') - 1;
let firstSalePriceIndexAfter = stmts.push('SELECT price FROM vn.sale WHERE id = 1') - 1;
stmts.push('ROLLBACK');
let sql = ParameterizedSQL.join(stmts, ';');
let result = await app.models.Ticket.rawStmt(sql);
expect(result[addedComponentIndex][0].value).toEqual(4.1);
expect(result[firstSalePriceIndexBefore][0].price).toEqual(result[firstSalePriceIndexAfter][0].price);
});
it(`should not change the sale price when option SEVEN, instead it stores 80% and 20% of the price in two components and any price difference in a third one`, async() => {
let stmts = [];
let stmt;
let params = {
warehouseFk: 1,
ticketFk: 1
};
stmts.push('START TRANSACTION');
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.sale');
// createSaleTempTable code comes from vn.ticketComponentUpdate procedure
let createSaleTempTable = `
CREATE TEMPORARY TABLE tmp.sale
(PRIMARY KEY (saleFk))
ENGINE = MEMORY
SELECT id AS saleFk, ? warehouseFk
FROM sale s WHERE s.ticketFk = ?
`;
stmt = new ParameterizedSQL(createSaleTempTable, [
params.warehouseFk,
params.ticketFk
]);
stmts.push(stmt);
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.ticketComponent');
// createTicketComponentTable code comes partially from vn.ticketComponenetCalculate procedure
let createTicketComponentTable = `
CREATE TEMPORARY TABLE tmp.ticketComponent (
warehouseFk INT UNSIGNED NOT NULL,
itemFk INT NOT NULL,
componentFk INT UNSIGNED NOT NULL,
cost DECIMAL(10,4) NOT NULL,
INDEX itemWarehouse USING BTREE (itemFk ASC, warehouseFk ASC),
UNIQUE INDEX itemWarehouseComponent (itemFk ASC, warehouseFk ASC, componentFk ASC))
`;
stmts.push(createTicketComponentTable);
let insertTicketComponentTable = `
INSERT INTO tmp.ticketComponent (warehouseFk, itemFk, componentFk, cost)
VALUES
(1 , 2 , 29 , 999.00),
(1 , 2 , 28 , 2.00),
(1 , 4 , 29 , 999.00),
(1 , 4 , 28 , 1.00)
`;
stmts.push(insertTicketComponentTable);
let updateComponentTwentyEight = `
UPDATE vn.saleComponent
SET value = '5'
WHERE saleFk = 1 AND componentFk = 28
`;
stmts.push(updateComponentTwentyEight);
let updateComponentTwentyNine = `
UPDATE vn.saleComponent
SET value = '5'
WHERE saleFk = 1 AND componentFk = 29
`;
stmts.push(updateComponentTwentyNine);
let priceFixtedToZero = `
UPDATE vn.sale
SET priceFixed = 0
WHERE id = 1
`;
stmts.push(priceFixtedToZero);
let firstSalePriceIndexBefore = stmts.push('SELECT price FROM vn.sale WHERE id = 1') - 1;
stmts.push('CALL vn.ticketComponentUpdateSale(7)');
let componentTwentyEightIndex = stmts.push('SELECT * FROM vn.saleComponent WHERE saleFk = 1 AND componentFk = 28') - 1;
let componentTwentyNineIndex = stmts.push('SELECT * FROM vn.saleComponent WHERE saleFk = 1 AND componentFk = 29') - 1;
let firstSalePriceIndexAfter = stmts.push('SELECT price FROM vn.sale WHERE id = 1') - 1;
stmts.push('ROLLBACK');
let sql = ParameterizedSQL.join(stmts, ';');
let result = await app.models.Ticket.rawStmt(sql);
expect(result[componentTwentyEightIndex][0].value).toEqual(1.6);
expect(result[componentTwentyNineIndex][0].value).toEqual(0.4);
expect(result[firstSalePriceIndexBefore][0].price).toEqual(result[firstSalePriceIndexAfter][0].price);
});
});