DROP TRIGGER IF EXISTS `triger_cash_sales`;
DELIMITER //
CREATE TRIGGER `triger_cash_sales` BEFORE INSERT ON `comp_cash_sales`
 FOR EACH ROW set new.serialNo = (select nextSerialNo 
			   from view_serial_comp_cash_sales
                    where companyID = new.companyID)
//
DELIMITER ;

DROP TRIGGER IF EXISTS `trigger_before_insert_donn1_add_serialNo`;
DELIMITER //
CREATE TRIGGER `trigger_before_insert_donn1_add_serialNo` BEFORE INSERT ON `comp_delivery_order`
 FOR EACH ROW set new.serialNo = (select nextSerialNo 
			   from view_serial_comp_delivery_order 
			   where companyID = new.companyID)
//
DELIMITER ;



DROP TRIGGER IF EXISTS `trigger_before_insert_cp_add_serialNo`;
DELIMITER //
CREATE TRIGGER `trigger_before_insert_cp_add_serialNo` BEFORE INSERT ON `comp_payment`
 FOR EACH ROW set new.serialNo = (select nextSerialNo 
			   from view_serial_comp_payment 
			   where companyID = new.companyID)
//
DELIMITER ;

DROP TRIGGER IF EXISTS `trigger_before_insert_payo_add_serialNo`;
DELIMITER //
CREATE TRIGGER `trigger_before_insert_payo_add_serialNo` BEFORE INSERT ON `comp_payment_others`
 FOR EACH ROW set new.serialNo = (select nextSerialNo 
			   from view_serial_comp_payment_others 
			   where companyID = new.companyID)
//
DELIMITER ;

DROP TRIGGER IF EXISTS `mytrigerfopobeforeinsert`;
DELIMITER //
CREATE TRIGGER `mytrigerfopobeforeinsert` BEFORE INSERT ON `comp_purchase_order`
 FOR EACH ROW set new.serialNo = 
(select nextSerialNo from view_serial_comp_purchase_order 
	where companyID = new.companyID)
//
DELIMITER ;

DROP TRIGGER IF EXISTS `trigger_before_insert_cq_add_serialNo`;
DELIMITER //
CREATE TRIGGER `trigger_before_insert_cq_add_serialNo` BEFORE INSERT ON `comp_quotation`
 FOR EACH ROW set new.serialNo = 
(select nextSerialNo from view_serial_comp_quotation 
	where companyID = new.companyID)
//
DELIMITER ;

DROP TRIGGER IF EXISTS `triger_BEFORE_Insert_addSerialForReceiveItem`;
DELIMITER //
CREATE TRIGGER `triger_BEFORE_Insert_addSerialForReceiveItem` BEFORE INSERT ON `comp_receive_item`
 FOR EACH ROW set new.serialNo = 
(select nextSerialNo from view_serial_comp_receive_item 
	where companyID = new.companyID)
//
DELIMITER ;

DROP TRIGGER IF EXISTS `updateBalanceItem`;
DELIMITER //
CREATE TRIGGER `updateBalanceItem` BEFORE INSERT ON `comp_receive_item_detail`
 FOR EACH ROW if new.balanceitem is null then
set new.balanceItem = new.quantityReceived;
end if
//
DELIMITER ;

DROP TRIGGER IF EXISTS `trigger_before_insert_rpay_add_serialNo`;
DELIMITER //
CREATE TRIGGER `trigger_before_insert_rpay_add_serialNo` BEFORE INSERT ON `comp_receive_payment`
 FOR EACH ROW set new.serialNo = (select nextSerialNo 
			   from view_serial_comp_receive_payment
			   where companyID = new.companyID)
//
DELIMITER ;

DROP TRIGGER IF EXISTS `trigger_before_insert_rpay_add_others_serialNo`;
DELIMITER //
CREATE TRIGGER `trigger_before_insert_rpay_add_others_serialNo` BEFORE INSERT ON `comp_receive_payment_others`
 FOR EACH ROW set new.serialNo = (select nextSerialNo 
			   from view_serial_comp_receive_payment_others
			   where companyID = new.companyID)
//
DELIMITER ;

DROP TRIGGER IF EXISTS `trigger_before_insert_scn_add_serialNo`;
DELIMITER //
CREATE TRIGGER `trigger_before_insert_scn_add_serialNo` BEFORE INSERT ON `comp_sales_creditnote`
 FOR EACH ROW set new.serialNo = (select nextSerialNo 
			   from view_serial_comp_sales_creditnote
			   where companyID = new.companyID)
//
DELIMITER ;

DROP TRIGGER IF EXISTS `trigger_before_insert_sdn_add_serialNo`;
DELIMITER //
CREATE TRIGGER `trigger_before_insert_sdn_add_serialNo` BEFORE INSERT ON `comp_sales_debitnote`
 FOR EACH ROW set new.serialNo = (select nextSerialNo 
			   from view_serial_comp_sales_debitnote
			   where companyID = new.companyID)
//
DELIMITER ;

DROP TRIGGER IF EXISTS `Addserialsaleorder`;
DELIMITER //
CREATE TRIGGER `Addserialsaleorder` BEFORE INSERT ON `comp_sales_order`
 FOR EACH ROW set new.serialNo = (select nextSerialNo 
			   from view_serial_comp_sales_order
			   where companyID = new.companyID)
//
DELIMITER ;

-- ----------------

DROP TRIGGER IF EXISTS `trigger_before_insert_scno_add_serialNo`;
DELIMITER //
CREATE TRIGGER `trigger_before_insert_scno_add_serialNo` BEFORE INSERT ON `comp_supplier_creditnote`
 FOR EACH ROW set new.serialNo = (select nextSerialNo 
			   from view_serial_comp_supplier_creditnote
			   where companyID = new.companyID)
//
DELIMITER ;

DROP TRIGGER IF EXISTS `trigger_before_insert_sdno_add_serialNo`;
DELIMITER //
CREATE TRIGGER `trigger_before_insert_sdno_add_serialNo` BEFORE INSERT ON `comp_supplier_debitnote`
 FOR EACH ROW set new.serialNo = (select nextSerialNo 
			   from view_serial_comp_supplier_debitnote
			   where companyID = new.companyID)
//
DELIMITER ;

DROP TRIGGER IF EXISTS `trigger_before_insert_supinv_add_serialNo`;
DELIMITER //
CREATE TRIGGER `trigger_before_insert_supinv_add_serialNo` BEFORE INSERT ON `comp_supplier_invoice`
 FOR EACH ROW set new.serialNo = (select nextSerialNo 
			   from view_serial_comp_supplier_invoice 
			   where companyID = new.companyID)
//
DELIMITER ;

DROP TRIGGER IF EXISTS `trigger_before_insert_item`;
DELIMITER //
CREATE TRIGGER `trigger_before_insert_item` BEFORE INSERT ON `item`
 FOR EACH ROW begin
if new.incomeID = 0 or new.incomeID is null then 
set new.incomeID = (select accountID from comp_control_acct_setup where ID > 0 and companyID = new.companyID and accountCode='saleacct'); 
end if;
if new.cogsID = 0 or new.cogsID is null then 
set new.cogsID = (select accountID from comp_control_acct_setup where id>0 and companyID = new.companyID and accountCode = 'saleacct');
end if;

end
//
DELIMITER ;

DROP TRIGGER IF EXISTS `trigger_default_created_date`;
DELIMITER //
CREATE TRIGGER `trigger_default_created_date` BEFORE INSERT ON `supplier`
 FOR EACH ROW if ( isnull(new.createddate) ) then
 set new.createddate=curdate();
end if
//
DELIMITER ;

DROP TRIGGER IF EXISTS `trir_befor_inser_SI`;
DELIMITER //
CREATE TRIGGER `trir_befor_inser_SI` BEFORE INSERT ON `comp_sales_invoice`
 FOR EACH ROW set new.serialNo = (select nextSerialNo 
			   from view_serial_comp_sales_invoice
			   where companyID = new.companyID)
//
DELIMITER ;