-- Extract modified tables from fixtures:
-- awk 'match($0, /INSERT INTO `(\w+)`\.`(\w+)`/, ary) {print ary[1]" - "ary[2]}' dump/fixtures.sql | sort -u

CALL `cache`.`last_buy_refresh`(FALSE);

SET foreign_key_checks = 0;

-- XXX: vn-database

INSERT INTO util.config (environment, mockTime, mockUtcTime, mockEnabled)
    VALUES ('local', '2001-01-01 12:00:00', '2001-01-01 11:00:00', TRUE);
/* #5483
INSERT INTO vn.entryConfig (defaultEntry, mailToNotify, inventorySupplierFk, maxLockTime, defaultSupplierFk)
    VALUES(1, NULL, 1, 300, 1);
*/
-- XXX: eti

INSERT INTO vn.travel (id,shipped,landed,warehouseInFk,warehouseOutFk,`ref`,m3,kg,cargoSupplierFk)
    VALUES (9,'2022-05-20','2022-05-20',13,4,'nine travel',69.0,600,1);
/* #5483
INSERT INTO vn.entry (id, supplierFk, dated, invoiceNumber, evaNotes, travelFk, companyFk)
    VALUES
        (9, 1, '0000-00-00 00:00:00', 'Movement 9', 'observation nine', 8, 442),
        (10, 1, '0000-00-00 00:00:00', 'Movement 10', 'observation ten', 9, 442);

INSERT INTO vn.buy (id,entryFk,itemFk,quantity,dispatched,buyingValue,freightValue,isIgnored,stickers,packing,`grouping`,groupingMode,comissionValue,packageValue,packagingFk,price1,price2,price3,minPrice,printedStickers,workerFk,isChecked,isPickedOff,created,`__cm2`,weight)
    VALUES
        (16,10,1,100,0,50.0,1.5,0,1,1,1,0,0.0,1.5,'4',0.0,99.6,99.4,0.0,0,0,1,0,'2022-06-20 00:00:00',0,1.0),
        (17,10,2,450,0,5.0,1.0,0,1,10,10,0,0.0,1.0,'3',0.0,7.3,7.0,0.0,0,0,1,0,'2022-06-20 00:00:00',0,2.5),
        (18,10,15,500,0,55.0,1.0,0,1,1,1,0,0.0,1.0,'5',0.0,78.3,75.6,0.0,0,0,1,0,'2022-06-20 00:00:00',0,2.5),
        (19,10,11,5000,0,50.0,1.0,0,1,1,1,1,0.0,1.0,'4',0.0,99.6,99.4,0.0,0,0,1,0,'2022-06-20 00:00:00',0,2.5);
*/
INSERT INTO vn.split (id,ticketFk,entryFk,dated,note)
    VALUES
        (1,7,2,'00-00-0000 00:00:00.000','nota1'),
        (2,10,6,'00-00-0000 00:00:00.000','nota2'),
        (3,15,7,'00-00-0000 00:00:00.000','nota3'),
        (4,22,10,'00-00-0000 00:00:00.000','nota4');

-- XXX: enc

INSERT INTO vn.mdbVersion (app, branchFk, version)
    VALUES('enc', 'master', 1);

UPDATE vn.warehouse
    SET isForTicket = 1, addressFk = 118
    WHERE id = 1;

UPDATE vn.packaging
    SET freightItemFk = 1, isBox = 1
    WHERE id = '94';
/* #5483
INSERT INTO `vn`.`packagingConfig`(`upperGap`, `defaultSmallPackageFk`, `defaultBigPackageFk`)
    VALUES
        ('10', 1, 2);
*/
UPDATE vn.packaging SET freightItemFk=1 WHERE id='1';
UPDATE vn.packaging SET freightItemFk=1 WHERE id='2';

INSERT INTO vn.packagingWithFreight (packagingFk, priority) VALUES('1', 1);

INSERT INTO vn.cmrConfig (dayOfMonth, landingDays)
    VALUES(5, 1);

UPDATE vn.supplier
    SET isTrucker = 1
    WHERE id = 2;

-- XXX: tpv

UPDATE `vn`.`claimRatio` SET `claimAmount` = '10' WHERE (`clientFk` = '1101');

INSERT INTO `vn`.`agency` (`name`, `warehouseFk`, `isOwn`, `isAnyVolumeAllowed`)
    VALUES
    ('Agencia', '1', '1', '1'),
    ('Otra agencia ', '1', '0', '0');

INSERT INTO `vn`.`expedition` (`agencyModeFk`, `ticketFk`, `isBox`, `counter`, `workerFk`, `externalId`, `packagingFk`, `hostFk`, `itemPackingTypeFk`, `hasNewRoute`) VALUES
    ('1', '1', 1, '1', '1', '1', '1', 'pc00', 'F', 0),
    ('1', '1', 1, '2', '1', '1', '1', 'pc00', 'F', 0);

INSERT INTO vn.client (id,name,defaultAddressFk,street,fi,email,dueDay,isTaxDataChecked,accountingAccount,city,provinceFk,postcode,socialName,contact,credit,countryFk,quality,riskCalculated) VALUES
    (100,'root',110,'Valle de la muerte','74974747G','root@mydomain.com',0,1,'4300000078','ALGEMESI',1,'46680','rootSocial','rootContact',500.0,1,10,'2025-01-01');

INSERT INTO vn.address (clientFk,street,city,provinceFk,postalCode,nickname,isEqualizated,isLogifloraAllowed,isDefaultAddress) VALUES
    (100,'rootStreet','ALGEMESI',3,'46680','root',0,0,1);
/* #5483
INSERT INTO vn.ticket (clientFk,warehouseFk,shipped,nickname,refFk,addressFk,routeFk,companyFk,agencyModeFk,landed,zoneFk, totalWithVat, totalWithoutVat) VALUES
    (100,1,'2025-07-06 00:00:00','Saco de patatas',NULL,103,6,442,1,'2025-07-06',13, 500, 450),
    (100,1,'2021-07-06 00:00:00','Alubias de la abuelita Enrriqueta','T4444444',103,6,442,1,'2021-07-06',13, 1000, 900);
*/
INSERT INTO vn.receipt (invoiceFk,amountPaid,payed,workerFk,bankFk,clientFk,isConciliate,companyFk) VALUES
    ('Cobro mental',999.0,'2021-06-21 00:00:00',9,3,100,1,442),
    ('Cobro web',50.0,'2022-06-21 00:00:00',9,3,100,1,567);

INSERT INTO vn.workerBosses (workerFk,bossFk) VALUES
    (44,100),
    (70,100),
    (47,100),
    (58,100),
    (1106,100);

UPDATE vn.agencyMode
    SET deliveryMethodFk=1
    WHERE id=1;

UPDATE vn.agencyMode
    SET deliveryMethodFk=2
    WHERE id=2;
/* #5483
INSERT INTO vn.ticket (clientFk, warehouseFk, shipped, nickname, refFk, addressFk, workerFk, observations, isSigned, isLabeled, isPrinted, packages, location, `hour`, created, isBlocked, solution, routeFk, priority, hasPriority, companyFk, agencyModeFk, landed, isBoxed, isDeleted, zoneFk, zonePrice, zoneBonus, totalWithVat, totalWithoutVat, weight) VALUES
    (100, 3, '2022-07-12 00:00:00', 'root', NULL, 195, NULL, NULL, 0, 0, 0, 0, NULL, 0, '2022-07-12 16:18:58', 1, NULL, 3, NULL, 1, 567, 1, '2022-07-12', 0, 0, 5, NULL, NULL, NULL, NULL, NULL),
    (100, 4, '2022-07-12 00:00:00', 'root', NULL, 195, NULL, NULL, 0, 0, 0, 0, NULL, 0, '2022-07-12 16:18:58', 1, NULL, 4, NULL, 1, 567, 1, '2022-07-12', 0, 0, 6, NULL, NULL, NULL, NULL, NULL),
    (100, 5, '2022-07-12 00:00:00', 'root', NULL, 195, NULL, NULL, 0, 0, 0, 0, NULL, 0, '2022-07-12 16:18:58', 1, NULL, 5, NULL, 1, 567, 1, '2022-07-12', 0, 0, 1, NULL, NULL, NULL, NULL, NULL);
*/
INSERT INTO vn.sector (description,warehouseFk) VALUES
    ('Sector One',1);

INSERT INTO vn.saleGroup (userFk,parkingFk,sectorFk) VALUES
    (100,1,1);

INSERT INTO vn.saleGroupDetail (saleFk,saleGroupFk) VALUES
    (32,1);
/* #5483
INSERT INTO vn.itemShelving (id,itemFk,visible,shelvingFk,`grouping`,packing,packagingFk,userFk) VALUES
    (4,1,5,'HEJ',12,12,'2',100);
*/
INSERT INTO vn.beach (code,warehouseFk) VALUES
    ('TEST',1);

INSERT INTO vn.routesMonitor (routeFk,name,beachFk,m3,expeditionTruckFk) VALUES
    (1,'TEST','TEST',1.0,1);
/* #5483
INSERT INTO vn.ticket (clientFk, warehouseFk, shipped, nickname, refFk, addressFk, workerFk, observations, isSigned, isLabeled, isPrinted, packages, location, `hour`, created, isBlocked, solution, routeFk, priority, hasPriority, companyFk, agencyModeFk, landed, isBoxed, isDeleted, zoneFk, zonePrice, zoneBonus, totalWithVat, totalWithoutVat, weight)
    VALUES (1101, 1, '2022-07-19 10:00:00', 'Bruce Wayne', NULL, 1, NULL, NULL, 0, 0, 0, 0, NULL, 0, '2022-07-19 08:14:37', 1, NULL, 1, 1, 1, 442, 1, '2022-07-06', 0, 0, 13, 5.00, 1.00, 80.00, 75.00, NULL);

INSERT INTO vn.sale (itemFk, ticketFk, concept, quantity, originalQuantity, price, discount, priceFixed, reserved, isPicked, isPriceFixed, created, isAdded)
    VALUES (1, 24, 'Ranged weapon longbow 2m', 1.00, NULL, 8.07, 0, 0.00, 0, 0, 0, '2022-07-06 00:00:00', 0);
*/
-- XXX: lab

INSERT INTO `vn`.`businessReasonEnd` (`id`, `reason`)
    VALUES
        ('1', 'Baja voluntaria'),
        ('2', 'No supera PP empresa'),
        ('3', 'Despido');

INSERT INTO `vn2008`.`payroll_categorias` (`codcategoria`, `descripcion`, `Tarifa`)
    VALUES
        ('11', 'INFORMATICA', '2'),
        ('12', 'AUX INFORMATICA', '7');

INSERT INTO `vn`.`occupationCode` (`code`, `name`)
    VALUES
        ('a', 'Personal en trabajaos exclusivos de oficina'),
        ('b', 'Representantes de comercio'),
        ('c', 'Personal de oficios en trabajos de construcción en general, y en instalac.,edificios y obras');

INSERT INTO `vn2008`.`payroll_employee` (`CodTrabajador`,`nss`,`codpuesto`,`codempresa`,`codcontrato`,`FAntiguedad`,`grupotarifa`,`codcategoria`,`ContratoTemporal`)
    VALUES
        (36,'46/10515497-58',6,20,189,'2009-01-02',5,10,0),
        (43,'46/10235353-50',7,20,189,'2009-04-21',5,10,0),
        (76,'46/10250562-30',1,20,189,'2009-09-07',9,5,0),
        (1106,'46/10297768-94',4,20,100,'2021-03-09',7,18,0),
        (1107,'46/1627085-11',15,20,402,'2021-03-15',9,6,1),
        (1108,'46/10446901-41',25,20,502,'2021-03-22',10,29,1),
        (1109,'46/10552113-8',3,20,402,'2021-03-23',9,9,1),
        (1110,'46/10723579-75',3,20,402,'2021-03-23',9,9,1);

INSERT INTO `vn`.`trainingCourseType` (`id`, `name`)
    VALUES
        ('1', 'Curso 1'),
        ('2', 'Curso 2');

INSERT INTO `vn`.`trainingCenter` (`id`, `name`)
    VALUES
        ('1', 'Centro curso 1'),
        ('2', 'Centro Curso 2');

INSERT INTO `vn`.`medicalCenter` (`id`, `name`)
    VALUES
        ('1', 'Centro médico 1'),
        ('2', 'Centro médico 2');

INSERT INTO `vn`.`disabilityGrade` (`id`, `description`)
    VALUES
        ('1', 'Grado 1'),
        ('2', 'Grado 2');

INSERT INTO `vn`.`absenceType` (`id`,`name`,`rgb`,`color`,`permissionRate`,`code`,`isAllowedToWork`,`isPrintable`,`discountRate`,`holidayEntitlementRate`)
    VALUES
        (7,'Excedencia','#C71585',16711935,0.00,'leave',0,0,1.00,1.00),
        (10,'Baja mutua','#00ffff',16776960,1.00,'insurance',0,0,1.00,0.00),
        (11,'Permiso retribuido','#0080ff',16744448,1.00,NULL,0,1,1.00,0.00),
        (12,'Visita médico','#ff80ff',16744703,1.00,NULL,0,0,1.00,0.00),
        (13,'Visita mutua','#808040',4227200,0.00,NULL,0,0,1.00,0.00),
        (14,'Maternidad/Paternidad','#008040',4227072,1.00,'maternity',0,1,1.00,0.00),
        (16,'Visita comercial','#00ff00',65280,1.00,NULL,0,1,1.00,0.00),
        (17,'Compensar','#dbec40',4844763,0.00,NULL,0,1,1.00,0.00),
        (18,'Parte','#0af5a0',10747402,1.00,'proof',0,0,1.00,0.00),
        (19,'Descanso','#664208',4798193,0.00,NULL,0,0,1.00,0.00),
        (22,'Sin justificar','#8B3E2F',3096203,1.00,NULL,1,1,1.00,0.00);

-- XXX: app

INSERT INTO vn.deviceProductionModels (code)
    VALUES('TC210K');

INSERT INTO vn.deviceProduction (id, imei, modelFk, macWifi, serialNumber, android_id, purchased)
    VALUES(100, NULL, 'TC210K', '78B8D67298A6', '21114523025303', NULL, '1900-01-01');

INSERT INTO vn.deviceProductionUser (deviceProductionFk, userFk, created)
    VALUES(100, 9, NULL);

INSERT INTO vn.expeditionStateType (id, description, code)
    VALUES(15, 'Perdida', 'LOST');

INSERT INTO vn.expedition (agencyModeFk,ticketFk,isBox,counter,hostFk,hasNewRoute)
    VALUES (1,21,1,1,'',0);
/* #5483
INSERT INTO vn.sectorCollection (id, created, userFk, sectorFk)
    VALUES(18, '2022-07-29 09:03:31', 9, 1);
*/
INSERT INTO vn.saleGroup (id, created, userFk, parkingFk, sectorFk)
    VALUES(3, '2022-07-29 07:18:49', 9, NULL, 1);

INSERT INTO vn.sectorCollectionSaleGroup (id, created, sectorCollectionFk, saleGroupFk)
    VALUES(7, '2022-07-29 09:05:37', 18, 3);

INSERT INTO vn.saleGroupDetail (created, saleFk, saleGroupFk)
    VALUES
        ( '2022-07-29 07:26:44', 14, 3),
        ( '2022-07-29 07:26:44', 15, 3);

INSERT INTO vn.saleTracking (saleFk, isChecked, created, originalQuantity, workerFk, id, stateFk)
    VALUES
        (15, 1, '2022-07-29 07:56:16', 10.0, 9, 6, 26);
/* #5483
INSERT INTO vn.ticket (id, clientFk, warehouseFk, shipped, nickname, refFk, addressFk, workerFk, observations, isSigned, isLabeled, isPrinted, packages, location, `hour`, created, isBlocked, solution, routeFk, priority, hasPriority, companyFk, agencyModeFk, landed, isBoxed, isDeleted, zoneFk, zonePrice, zoneBonus, totalWithVat, totalWithoutVat, weight)
    VALUES
        (3000001, 1110, 1, '2022-07-28 21:00:00', 'ATELIER ART FLORAL', NULL, 129, NULL, NULL, 0, 0, 0, 1, NULL, 0, '2022-07-28 20:27:14', 1, NULL, 1, 16, 1, 442, 1, '2022-07-29', 0, 0, 1, 4.00, 0.00, 41.68, 37.42, NULL);

INSERT INTO vn.collection (id, created, workerFk, stateFk, itemPackingTypeFk, saleTotalCount, salePickedCount, trainFk)
    VALUES
        (600001, '2022-07-28 20:04:24', 9, 5, NULL, 5, 5, 1);

INSERT INTO vn.sale (itemFk, ticketFk, concept, quantity, originalQuantity, price, discount, priceFixed, reserved, isPicked, isPriceFixed, created, isAdded)
    VALUES
        ( 1, 3000001, 'Ranged weapon longbow 2m', 5.00, NULL, 100.39, 0, 0.00, 0, 1, 0, '2022-06-13 00:00:00', 0);
*/
INSERT INTO vn.collectionColors(code, shelve, wagon, trainFk)
    VALUES
        ( 'ROJO', 1, 1, 1);

INSERT INTO vn.ticketCollection (ticketFk, collectionFk, created, `level`, wagon, smartTagFk, usedShelves, itemCount, liters)
    VALUES
        (3000001, 600001, '2022-07-28 20:04:24', 1, 1, NULL, NULL, 6, 122);
/* #5483
INSERT INTO vn.itemShelving (itemFk, shelvingFk, visible, created, `grouping`, packing, packagingFk, userFk, buyFk)
    VALUES
        ( 1, 'HEJ', 4,  '2022-07-13 10:33:34', 12, 12, NULL, 9  , 1),
        ( 1, 'GVC', 10, '2022-07-13 10:33:34', 12, 12, NULL, 100, 2),
        ( 1, 'UXN', 20, '2022-07-13 10:33:34', 12, 12, NULL, 100, 3);

INSERT INTO vn.operator (workerFk, numberOfWagons, trainFk, itemPackingTypeFk, warehouseFk)
    VALUES
        (9, 1, 1, 'V', 1);
*/
-- XXX: web
/* #5483
INSERT INTO `hedera`.`config` (`cookieLife`, `defaultForm`)
    VALUES (15,'cms/home');

INSERT INTO `hedera`.`imageConfig` (`maxSize`, `url`)
    VALUES (20, '//cdn.verdnatura.es/image');

INSERT INTO `hedera`.`orderConfig` (`employeeFk`, `guestMethod`, `guestAgencyFk`, `defaultCompanyFk`)
    VALUES (1, 'PICKUP', 1, 442);

INSERT INTO `hedera`.`news` (`title`,`text`, `image`, `userFk`, `created`, `priority`, `tag`)
    VALUES
        ('Foo new', 'Hi, welcome to Verdnatura''s webshop', 'sd', 9, '2022-01-01 00:00:00', 1, 'new'),
        ('Bar new', 'Here you can buy any plant you want', 'sd', 9, '2022-01-02 00:00:00', 1, 'new');

UPDATE `vn`.`agencyMode` SET `description` = `name`;

INSERT INTO `hedera`.`tpvConfig` (currency, terminal, transactionType, maxAmount, employeeFk, `url`, testMode, testUrl, testKey, merchantUrl)
    VALUES (978, 1, 0, 2000, 9, 'https://sis.redsys.es/sis/realizarPago', 0, 'https://sis-t.redsys.es:25443/sis/realizarPago', 'sq7HjrUOBfKmC576ILgskD5srU870gJ7', NULL);
*/
INSERT INTO hedera.tpvMerchantEnable (merchantFk, companyFk)
    VALUES (1, 442);

-- XXX

SET foreign_key_checks = 1;

INSERT INTO `account`.`userConfig` (`id`, `loginKey`)
    VALUES (1, 'loginKey');

UPDATE `account`.`roleConfig`
    SET `mysqlPassword` = TO_BASE64('nightmare');

CREATE USER 'any'@'%' IDENTIFIED BY 'nightmare';
UPDATE `mysql`.`global_priv` SET `Host` = ''
    WHERE `User` = 'any' AND `Host` = '%';

USE account;

CALL role_sync;

SELECT rolePrefix INTO @rolePrefix FROM roleConfig;

SET @genRoleHost = '%';
SET @prefixLen = CHAR_LENGTH(@rolePrefix);
SET @prefixedLike = CONCAT(@rolePrefix, '%');

INSERT INTO mysql.global_priv (`User`, `Host`, `Priv`)
    SELECT SUBSTR(`User`, @prefixLen + 1), `Host`, `Priv`
        FROM mysql.global_priv
        WHERE `User` LIKE @prefixedLike AND `Host` = @genRoleHost;

INSERT INTO mysql.roles_mapping (`User`, `Host`, `Role`, `Admin_option`)
    SELECT SUBSTR(`User`, @prefixLen + 1), `Host`, `Role`, `Admin_option`
        FROM mysql.roles_mapping
        WHERE `User` LIKE @prefixedLike AND `Host` = @genRoleHost;

-- Actualiza los valores de la nueva columna con los valores correspondientes de la tabla userInfo
UPDATE  `account`.`user` as `user`
JOIN vn.client `client` ON `user`.id = `client`.id
SET `user`.recoveryPhone = `client`.phone;

FLUSH PRIVILEGES;
