salix/db/changes/232601/00-client_create.sql

113 lines
2.9 KiB
SQL

DROP PROCEDURE IF EXISTS vn.clientCreate;
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`client_create`(
vFirstname VARCHAR(50),
vSurnames VARCHAR(50),
vFi VARCHAR(9),
vAddress TEXT,
vPostcode CHAR(5),
vCity VARCHAR(25),
vProvinceFk SMALLINT(5),
vCompanyFk SMALLINT(5),
vPhone VARCHAR(11),
vEmail VARCHAR(255),
vUserFk INT)
BEGIN
/**
* Create new client
*
* @params vFirstname firstName
* @params vSurnames surnames
* @params vFi company code from accounting transactions
* @params vAddress address
* @params vPostcode postCode
* @params vCity city
* @params vProvinceFk province
* @params vCompanyFk company in which he has become a client
* @params vPhone telephone number
* @params vEmail email address
* @params vUserFk user id
*/
DECLARE vPayMethodFk INT;
DECLARE vDueDay INT;
DECLARE vDefaultCredit DECIMAL(10, 2);
DECLARE vIsTaxDataChecked TINYINT(1);
DECLARE vHasCoreVnl BOOLEAN;
DECLARE vMandateTypeFk INT;
SELECT payMethodFk,
dueDay,
credit,
isTaxDataChecked,
hasCoreVnl,
mandateTypeFk
INTO vPayMethodFk,
vDueDay,
vDefaultCredit,
vIsTaxDataChecked,
vHasCoreVnl,
vMandateTypeFk
FROM clientNewConfig;
INSERT INTO `client`
SET id = vUserFk,
name = CONCAT(vFirstname, ' ', vSurnames),
street = vAddress,
fi = TRIM(vFi),
phone = vPhone,
email = vEmail,
provinceFk = vProvinceFk,
city = vCity,
postcode = vPostcode,
socialName = CONCAT(vSurnames, ' ', vFirstname),
payMethodFk = vPayMethodFk,
dueDay = vDueDay,
credit = vDefaultCredit,
isTaxDataChecked = vIsTaxDataChecked,
hasCoreVnl = vHasCoreVnl,
isEqualizated = FALSE
ON duplicate KEY UPDATE
payMethodFk = vPayMethodFk,
dueDay = vDueDay,
credit = vDefaultCredit,
isTaxDataChecked = vIsTaxDataChecked,
hasCoreVnl = vHasCoreVnl,
isActive = TRUE;
INSERT INTO mandate (clientFk, companyFk, mandateTypeFk)
SELECT vUserFk, vCompanyFk, vMandateTypeFk
WHERE NOT EXISTS (
SELECT 1
FROM mandate
WHERE clientFk = vUserFk
AND companyFk = vCompanyFk
AND mandateTypeFk = vMandateTypeFk
);
END$$
DELIMITER ;
CREATE TABLE IF NOT EXISTS vn.clientNewConfig (
id int unsigned auto_increment NULL,
payMethodFk tinyint(3) unsigned NULL,
dueDay int unsigned NULL,
credit decimal(10, 2) NULL,
isTaxDataChecked tinyint(1) NULL,
hasCoreVnl boolean NULL,
mandateTypeFk smallint(5) NULL,
CONSTRAINT clientNewConfig_PK PRIMARY KEY (id),
CONSTRAINT clientNewConfigPayMethod_FK FOREIGN KEY (payMethodFk) REFERENCES vn.payMethod(id),
CONSTRAINT clientNewConfigMandateType_FK FOREIGN KEY (mandateTypeFk) REFERENCES vn.mandateType(id)
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8mb3
COLLATE=utf8mb3_unicode_ci;
INSERT IGNORE INTO vn.clientNewConfig
SET id = 1,
payMethodFk = 4,
dueDay = 5,
credit = 300.0,
isTaxDataChecked = 1,
hasCoreVnl = 1,
mandateTypeFk = 2;