一、demo脚本
DROP PROCEDURE IF EXISTS `yanshi_autoaddform11_byform12`;
DELIMITER ;;
CREATE PROCEDURE `yanshi_autoaddform11_byform12`(
tmp_form12id INT(19))
BEGINSET @tmp_form12id=tmp_form12id;
-- SET @tmp_form12id=12947;
SET @tmp_form11id=(SELECT source_list FROM ld_customform12 WHERE customform12id=@tmp_form12id);
SET @tmp_form12no=(SELECT customform12_no FROM ld_customform12 WHERE customform12id=@tmp_form12id);IF EXISTS (SELECT 1 FROM ld_customform11 WHERE deleted=0 AND customform11id=@tmp_form11id) AND NOT EXISTS (SELECT 1 FROM ld_customform11 WHERE deleted=0 AND form12_id=@tmp_form12id) THEN
-- 1、先生成红字单据
-- 获取自动创建单据的 ID
SET @autoadd_maxcrmid=(SELECT MAX(crmid) FROM ld_crmentity);
INSERT INTO ld_crmentity(crmid,setype)
SELECT @autoadd_maxcrmid+1,'CustomForm11';UPDATE ld_crmentity_seq SET id=(SELECT MAX(crmid) FROM ld_crmentity);
-- 插入主表
INSERT INTO ld_customform11(
customform11id,
customform11_no,
entitytype,
source_list,smownerid,
smcreatorid,
modifiedby,
createdtime,
modifiedtime,currency_id,
currency_symbol,
conversion_rate,
total,
subtotal,
discount_amount, -- 折扣直接降价contactid, -- 联系人
accountid, -- 客户名称approvestatus,
currentapprover,
currentapprovestepid,
approvesubmitter,
approvesubmittedtime,
triggercount,
form12_id
)
SELECT
@autoadd_maxcrmid+1,
(SELECT CONCAT(prefix,cur_id+0) FROM ld_modentity_num WHERE semodule='Customform11' AND active=1),
a.entitytype,
a.source_list,a.smownerid,
a.smcreatorid,
a.modifiedby,
NOW(),
NOW(),a.currency_id,
a.currency_symbol,
a.conversion_rate,
0-a.total,
0-a.subtotal,
0-a.discount_amount, -- 折扣直接降价a.contactid, -- 联系人
a.accountid, -- 客户名称'已批准',
NULL,
NULL,
a.approvesubmitter,
a.approvesubmittedtime,
1,
@tmp_form12id
FROM ld_customform11 a
WHERE a.customform11id=@tmp_form11id LIMIT 1;
INSERT INTO ld_customform11scf(
customform11id,
cf_5330,
cf_5331,
cf_5333,
cf_5334,cf_5343,
cf_5344,
cf_5345,
cf_5347,
cf_5348,
cf_5349,
cf_5351,
cf_5353,
cf_5354,
cf_5361,cf_5362,
cf_5365,
cf_5366,
cf_5367,
cf_5370,
cf_5410,
cf_5437,
cf_5483,
cf_5484,
cf_5485,cf_5486,
cf_5487,
cf_5488,
cf_7669,
cf_7683,
cf_7686,
cf_7687,
cf_7688,
cf_7728,
cf_7822
)
SELECT
@autoadd_maxcrmid+1,
cf_5330,
cf_5331,
0-cf_5333, -- 合同总方量
cf_5334,CONCAT(cf_5343,IF(IFNULL(cf_5343,'')='','','\r\n'),'调价单『',@tmp_form12no,'』关联的合同自动生成红字单据'),
cf_5344,
0-cf_5345, -- 合同总额
cf_5347,
cf_5348,
cf_5349,
cf_5351,
cf_5353,
cf_5354,
cf_5361,cf_5362,
cf_5365,
cf_5366,
cf_5367,
cf_5370,
cf_5410,
cf_5437,
cf_5483,
cf_5484,
cf_5485,cf_5486,
cf_5487,
cf_5488,
cf_7669,
cf_7683,
cf_7686,
cf_7687,
cf_7688,
cf_7728,
'红'
FROM ld_customform11scf a
WHERE a.customform11id=@tmp_form11id;UPDATE ld_modentity_num SET cur_id=cur_id+1 WHERE semodule='Customform11' AND active=1;
-- 插入产品明细
INSERT INTO ld_customform11detail(
id,
productid,
sequence_no,quantity,
listprice,
taxprice,
tax_total,
list_amount,
tax_amount,comment,
parent_lineitem_id,
entitytype,
entitytypeid
)
SELECT
@autoadd_maxcrmid+1,
a.productid,
a.sequence_no,0-a.quantity,
a.listprice,
a.taxprice,
a.tax_total,
a.list_amount,
0-a.tax_amount,a.comment,
a.parent_lineitem_id,
a.entitytype,
a.entitytypeid
FROM ld_customform11detail a
WHERE a.id=@tmp_form11id;INSERT INTO ld_customform11detailscf(
sf3856, -- 供应状态
sf3946, -- 调价记录
lineitem_id
)
SELECT
b.sf3856, -- 供应状态
b.sf3946, -- 调价记录
a.lineitem_id
FROM ld_customform11detail a
LEFT JOIN ld_customform11detailscf b ON b.lineitem_id=a.lineitem_id
WHERE a.id=@tmp_form11id+1;
-- 2、再生成1个蓝字单据
-- 获取自动创建单据的 ID
SET @autoadd_maxcrmid=(SELECT MAX(crmid) FROM ld_crmentity);
INSERT INTO ld_crmentity(crmid,setype)
SELECT @autoadd_maxcrmid+1,'CustomForm11';UPDATE ld_crmentity_seq SET id=(SELECT MAX(crmid) FROM ld_crmentity);
-- 插入主表
INSERT INTO ld_customform11(
customform11id,
customform11_no,
entitytype,
source_list,smownerid,
smcreatorid,
modifiedby,
createdtime,
modifiedtime,currency_id,
currency_symbol,
conversion_rate,
total,
subtotal,
discount_amount, -- 折扣直接降价contactid, -- 联系人
accountid, -- 客户名称approvestatus,
currentapprover,
currentapprovestepid,
approvesubmitter,
approvesubmittedtime,
triggercount,
form12_id
)
SELECT
@autoadd_maxcrmid+1,
(SELECT CONCAT(prefix,cur_id+0) FROM ld_modentity_num WHERE semodule='Customform11' AND active=1),
a.entitytype,
a.source_list,a.smownerid,
a.smcreatorid,
a.modifiedby,
NOW(),
NOW(),a.currency_id,
a.currency_symbol,
a.conversion_rate,
a.total,
a.subtotal,
a.discount_amount, -- 折扣直接降价a.contactid, -- 联系人
a.accountid, -- 客户名称'已批准',
NULL,
NULL,
a.approvesubmitter,
a.approvesubmittedtime,
0,
@tmp_form12id
FROM ld_customform11 a
WHERE a.customform11id=@tmp_form11id LIMIT 1;
INSERT INTO ld_customform11scf(
customform11id,
cf_5330,
cf_5331,
cf_5333,
cf_5334,cf_5343,
cf_5344,
cf_5345,
cf_5347,
cf_5348,
cf_5349,
cf_5351,
cf_5353,
cf_5354,
cf_5361,cf_5362,
cf_5365,
cf_5366,
cf_5367,
cf_5370,
cf_5410,
cf_5437,
cf_5483,
cf_5484,
cf_5485,cf_5486,
cf_5487,
cf_5488,
cf_7669,
cf_7683,
cf_7686,
cf_7687,
cf_7688,
cf_7728,
cf_7822
)
SELECT
@autoadd_maxcrmid+1,
cf_5330,
cf_5331,
cf_5333,
cf_5334,CONCAT(cf_5343,IF(IFNULL(cf_5343,'')='','','\r\n'),'调价单『',@tmp_form12no,'』关联的合同自动生成蓝字单据'),
cf_5344,
cf_5345,
cf_5347,
cf_5348,
cf_5349,
cf_5351,
cf_5353,
cf_5354,
cf_5361,cf_5362,
cf_5365,
cf_5366,
cf_5367,
cf_5370,
cf_5410,
cf_5437,
cf_5483,
cf_5484,
cf_5485,cf_5486,
cf_5487,
cf_5488,
cf_7669,
cf_7683,
cf_7686,
cf_7687,
cf_7688,
cf_7728,
'蓝'
FROM ld_customform11scf a
WHERE a.customform11id=@tmp_form11id;UPDATE ld_modentity_num SET cur_id=cur_id+1 WHERE semodule='Customform11' AND active=1;
-- 插入产品明细
INSERT INTO ld_customform11detail(
id,
productid,
sequence_no,quantity,
listprice,
taxprice,
tax_total,
list_amount,
tax_amount,comment,
parent_lineitem_id,
entitytype,
entitytypeid
)
SELECT
@autoadd_maxcrmid+1,
a.productid,
a.sequence_no,b.quantity,
b.listprice,
b.taxprice,
b.tax_total,
b.list_amount,
b.tax_amount,a.comment,
a.parent_lineitem_id,
a.entitytype,
a.entitytypeid
FROM ld_customform11detail a
LEFT JOIN ld_customform12detail b ON b.parent_lineitem_id=a.lineitem_id
WHERE a.id=@tmp_form11id;INSERT INTO ld_customform11detailscf(
sf3856, -- 供应状态
sf3946, -- 调价记录
lineitem_id
)
SELECT
b.sf3856, -- 供应状态
b.sf3946, -- 调价记录
a.lineitem_id
FROM ld_customform11detail a
LEFT JOIN ld_customform11detailscf b ON b.lineitem_id=a.lineitem_id
WHERE a.id=@tmp_form11id+1;
UPDATE ld_customform11 a
LEFT JOIN (
SELECT
id,
SUM(tax_amount) AS total,
SUM(discount_amount) AS discount_total
FROM ld_customform11detail
WHERE id=@autoadd_maxcrmid+1
GROUP BY id
) b ON b.id=a.customform11id
SET
a.total=b.total,
a.subtotal=b.total,
a.discount_amount=b.discount_total,
a.triggercount=triggercount+1
WHERE a.customform11id=@autoadd_maxcrmid+1;END IF;
END
;;
DELIMITER ;
DROP TRIGGER IF EXISTS `trigger_after_form12_update`;
DELIMITER ;;
CREATE TRIGGER `trigger_after_form12_update` AFTER UPDATE ON `ld_customform12` FOR EACH ROW BEGINIF @disable_triggers IS NULL THEN
IF NEW.approvestatus='已批准' AND OLD.approvestatus<>'已批准' THEN
CALL yanshi_autoaddform11_byform12(NEW.customform12id);
END IF;
END IF;END
;;
DELIMITER ;