2019-02-13 07:34:40 +00:00
const app = require ( 'vn-loopback/server/server' ) ;
2018-12-21 19:22:13 +00:00
const ParameterizedSQL = require ( 'loopback-connector' ) . ParameterizedSQL ;
2018-10-14 15:09:05 +00:00
describe ( 'ticketComponentUpdateSale()' , ( ) => {
2019-01-02 12:33:30 +00:00
it ( ` should update the sale price when option ONE using the base components and reclaculate only the ones with isRenewable TRUE ` , async ( ) => {
2018-10-14 15:09:05 +00:00
let stmts = [ ] ;
let stmt ;
let params = {
warehouseFk : 1 ,
2019-07-25 11:32:25 +00:00
ticketFk : 13
2018-10-14 15:09:05 +00:00
} ;
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
2019-07-25 11:32:25 +00:00
( 1 , 4 , 15 , 999.00 ) ,
( 1 , 4 , 17 , 2.00 ) ,
( 1 , 4 , 22 , 999.00 ) ,
( 1 , 4 , 28 , 1.00 ) ,
( 1 , 4 , 29 , 2.00 ) ,
( 1 , 4 , 32 , 999.00 ) ,
( 1 , 4 , 39 , 1.00 )
2018-10-14 15:09:05 +00:00
` ;
stmts . push ( insertTicketComponentTable ) ;
2019-07-25 11:32:25 +00:00
let updateComponentTwentyNine = `
2018-10-14 15:09:05 +00:00
UPDATE vn . saleComponent
SET value = '5'
2019-07-25 11:32:25 +00:00
WHERE saleFk = 26 AND componentFk = 29
2018-10-14 15:09:05 +00:00
` ;
2019-07-25 11:32:25 +00:00
stmts . push ( updateComponentTwentyNine ) ;
2018-10-14 15:09:05 +00:00
let updateComponentTwentyEight = `
UPDATE vn . saleComponent
SET value = '5'
2019-07-25 11:32:25 +00:00
WHERE saleFk = 26 AND componentFk = 28
2018-10-14 15:09:05 +00:00
` ;
stmts . push ( updateComponentTwentyEight ) ;
let priceFixtedToZero = `
UPDATE vn . sale
SET priceFixed = 0
2019-07-25 11:32:25 +00:00
WHERE id = 26
2018-10-14 15:09:05 +00:00
` ;
stmts . push ( priceFixtedToZero ) ;
2019-07-25 11:32:25 +00:00
let firstSalePriceIndexBefore = stmts . push ( 'SELECT price FROM vn.sale WHERE id = 26' ) - 1 ;
2018-10-14 15:09:05 +00:00
stmts . push ( 'CALL vn.ticketComponentUpdateSale(1)' ) ;
2019-07-25 11:32:25 +00:00
let firstSalePriceIndexAfter = stmts . push ( 'SELECT price FROM vn.sale WHERE id = 26' ) - 1 ;
2018-10-14 15:09:05 +00:00
stmts . push ( 'ROLLBACK' ) ;
let sql = ParameterizedSQL . join ( stmts , ';' ) ;
let result = await app . models . Ticket . rawStmt ( sql ) ;
2019-07-25 11:32:25 +00:00
expect ( result [ firstSalePriceIndexBefore ] [ 0 ] . price ) . toEqual ( 1.72 ) ;
expect ( result [ firstSalePriceIndexAfter ] [ 0 ] . price ) . toEqual ( 1005 ) ;
2018-10-14 15:09:05 +00:00
} ) ;
2019-01-02 12:33:30 +00:00
it ( ` should keep the sale price when option TWO using the base components and save the difference in a new component ` , async ( ) => {
2018-10-14 15:09:05 +00:00
let stmts = [ ] ;
let stmt ;
let params = {
warehouseFk : 1 ,
2019-07-25 11:32:25 +00:00
ticketFk : 13
2018-10-14 15:09:05 +00:00
} ;
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 = `
2019-07-25 11:32:25 +00:00
INSERT INTO tmp . ticketComponent ( warehouseFk , itemFk , componentFk , cost )
VALUES
( 1 , 4 , 15 , 999.00 ) ,
( 1 , 4 , 17 , 2.00 ) ,
( 1 , 4 , 22 , 999.00 ) ,
( 1 , 4 , 28 , 1.00 ) ,
( 1 , 4 , 29 , 2.00 ) ,
( 1 , 4 , 32 , 999.00 ) ,
( 1 , 4 , 39 , 1.00 )
2018-10-14 15:09:05 +00:00
` ;
stmts . push ( insertTicketComponentTable ) ;
2019-07-25 11:32:25 +00:00
let updateComponentTwentyNine = `
2018-10-14 15:09:05 +00:00
UPDATE vn . saleComponent
SET value = '5'
2019-07-25 11:32:25 +00:00
WHERE saleFk = 26 AND componentFk = 29
2018-10-14 15:09:05 +00:00
` ;
2019-07-25 11:32:25 +00:00
stmts . push ( updateComponentTwentyNine ) ;
2018-10-14 15:09:05 +00:00
let updateComponentTwentyEight = `
UPDATE vn . saleComponent
SET value = '5'
2019-07-25 11:32:25 +00:00
WHERE saleFk = 26 AND componentFk = 28
2018-10-14 15:09:05 +00:00
` ;
stmts . push ( updateComponentTwentyEight ) ;
let priceFixtedToZero = `
UPDATE vn . sale
SET priceFixed = 0
2019-07-25 11:32:25 +00:00
WHERE id = 26
2018-10-14 15:09:05 +00:00
` ;
stmts . push ( priceFixtedToZero ) ;
2019-07-25 11:32:25 +00:00
let firstSalePriceIndexBefore = stmts . push ( 'SELECT price FROM vn.sale WHERE id = 26' ) - 1 ;
2018-10-14 15:09:05 +00:00
stmts . push ( 'CALL vn.ticketComponentUpdateSale(2)' ) ;
2019-07-25 11:32:25 +00:00
let addedComponentIndex = stmts . push ( 'SELECT * FROM vn.saleComponent WHERE saleFk = 26 AND componentFk = 17' ) - 1 ;
let firstSalePriceIndexAfter = stmts . push ( 'SELECT price FROM vn.sale WHERE id = 26' ) - 1 ;
2018-10-14 15:09:05 +00:00
stmts . push ( 'ROLLBACK' ) ;
let sql = ParameterizedSQL . join ( stmts , ';' ) ;
let result = await app . models . Ticket . rawStmt ( sql ) ;
2019-07-25 11:32:25 +00:00
expect ( result [ addedComponentIndex ] [ 0 ] . value ) . toEqual ( - 1003.28 ) ;
2018-10-14 15:09:05 +00:00
expect ( result [ firstSalePriceIndexBefore ] [ 0 ] . price ) . toEqual ( result [ firstSalePriceIndexAfter ] [ 0 ] . price ) ;
} ) ;
2019-01-02 12:33:30 +00:00
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 ( ) => {
2018-10-14 15:09:05 +00:00
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 ) ;
2019-07-25 11:32:25 +00:00
expect ( result [ componentTwentyEightIndex ] [ 0 ] . value ) . toEqual ( 79.517 ) ;
expect ( result [ componentTwentyNineIndex ] [ 0 ] . value ) . toEqual ( 19.879 ) ;
2018-10-14 15:09:05 +00:00
expect ( result [ firstSalePriceIndexBefore ] [ 0 ] . price ) . toEqual ( result [ firstSalePriceIndexAfter ] [ 0 ] . price ) ;
} ) ;
} ) ;