--- Some code
----delete dulieuketoan where loaidl=-99 ---- lệnh xoá của data đặc thù, không quan tâm
----select importinfor,* from dulieuketoan where importinfor<>'' --- lệnh kiểm tra dữ liệu convert misa
-----I. Câu lệnh kiểm tra xem: các nghiệp vụ phát sinh trên dữ liệu, và có sử dụng btthems
----- lưu ý: importinfor đánh dấu dữ liệu convert misa bằng ký tự 'misa'
----- câu lệnh dưới đang trả về các nghiệp vụ: 101, 301, 501, 1013 ( với dữ liệu khác thì chạy và lấy ra nghiepvu liên quan để kiểm tra tiếp phía dưới)
select nghiepvu from
(
select T1.*, T2.no, T2.co, T2.btthems
from
(select ngay,chungtu,tkno,tkco,nghiepvu,psco from dulieuketoan where importinfor='misa')T1 left join
(select ghichu,no,co,btthems,nghiepvu from btthems )T2 on T1.nghiepvu = T2.nghiepvu and (t1.tkno = t2.no or t1.tkco = t2.co)
--where t2.nghiepvu is null -- kiểm tra các dòng bút toán mẹ. tránh trường hợp ghép thiếu
where t2.nghiepvu is not null
)T group by T.nghiepvu
----II. Chạy từng nghiepvu. Chạy câu select để kiểm tra trước khi chạy câu update
----1. nhập kho nội địa
select T1.*, T2.*
--update t1 set t1.psco = t2.btthems
from
(select ngay,chungtu,tkno,tkco,nghiepvu,psco from dulieuketoan where nghiepvu=101 and importinfor='misa')T1 left join
(select ghichu,no,co,btthems,nghiepvu from btthems )T2 on T1.nghiepvu = T2.nghiepvu and (t1.tkno = t2.no or t1.tkco = t2.co)
--where t2.nghiepvu is null -- kiểm tra các dòng bút toán mẹ. tránh trường hợp ghép thiếu
where t2.nghiepvu is not null
order by t1.ngay, t1.chungtu
update t1 set t1.psco = t2.btthems
from
(select ngay,chungtu,tkno,tkco,nghiepvu,psco from dulieuketoan where nghiepvu=101 and importinfor='misa')T1 left join
(select ghichu,no,co,btthems,nghiepvu from btthems )T2 on T1.nghiepvu = T2.nghiepvu and (t1.tkno = t2.no or t1.tkco = t2.co)
where t2.nghiepvu is not null
----2. bán hàng nội địa; 301 ; btthems vat = 9
select T1.*, T2.*
from
(select ngay,chungtu,sohd,tkno,tkco,nghiepvu,psco from dulieuketoan where nghiepvu=301 and importinfor='misa')T1 left join
(select ghichu,no,co,btthems,nghiepvu from btthems )T2 on T1.nghiepvu = T2.nghiepvu and (t1.tkno = t2.no or t1.tkco = t2.co)
--where t2.nghiepvu is null -- kiểm tra các dòng bút toán mẹ. tránh trường hợp ghép thiếu
where t2.nghiepvu is not null
order by t1.ngay, t1.chungtu
update t1 set t1.psco = t2.btthems
from
(select ngay,chungtu,tkno,tkco,nghiepvu,psco from dulieuketoan where nghiepvu=301 and importinfor='misa')T1 left join
(select ghichu,no,co,btthems,nghiepvu from btthems )T2 on T1.nghiepvu = T2.nghiepvu and (t1.tkno = t2.no or t1.tkco = t2.co)
--where t2.nghiepvu is null -- kiểm tra các dòng bút toán mẹ. tránh trường hợp ghép thiếu
where t2.nghiepvu is not null
----3. chi tiền mặt ; 501
select T1.*, T2.*
from
(select ngay,chungtu,tkno,tkco,nghiepvu,psco from dulieuketoan where nghiepvu=501 and importinfor='misa')T1 left join
(select ghichu,no,co,btthems,nghiepvu from btthems )T2 on T1.nghiepvu = T2.nghiepvu and (t1.tkno = t2.no or t1.tkco = t2.co)
--where t2.nghiepvu is null -- kiểm tra các dòng bút toán mẹ. tránh trường hợp ghép thiếu
where t2.nghiepvu is not null
order by t1.ngay, t1.chungtu
update t1 set t1.psco = t2.btthems
from
(select ngay,chungtu,tkno,tkco,nghiepvu,psco from dulieuketoan where nghiepvu=501 and importinfor='misa')T1 left join
(select ghichu,no,co,btthems,nghiepvu from btthems )T2 on T1.nghiepvu = T2.nghiepvu and (t1.tkno = t2.no or t1.tkco = t2.co)
--where t2.nghiepvu is null -- kiểm tra các dòng bút toán mẹ. tránh trường hợp ghép thiếu
where t2.nghiepvu is not null
----4. Ghi tăng chi phí trả trước; 1013
select T1.*, T2.*
from
(select ngay,chungtu,tkno,tkco,nghiepvu,psco from dulieuketoan where nghiepvu=1013 and importinfor='misa')T1 left join
(select ghichu,no,co,btthems,nghiepvu from btthems )T2 on T1.nghiepvu = T2.nghiepvu and (t1.tkno = t2.no or t1.tkco = t2.co)
--where t2.nghiepvu is null -- kiểm tra các dòng bút toán mẹ. tránh trường hợp ghép thiếu
where t2.nghiepvu is not null
order by t1.ngay, t1.chungtu
update t1 set t1.psco = t2.btthems
from
(select ngay,chungtu,tkno,tkco,nghiepvu,psco from dulieuketoan where nghiepvu=1013 and importinfor='misa')T1 left join
(select ghichu,no,co,btthems,nghiepvu from btthems )T2 on T1.nghiepvu = T2.nghiepvu and (t1.tkno = t2.no or t1.tkco = t2.co)
--where t2.nghiepvu is null -- kiểm tra các dòng bút toán mẹ. tránh trường hợp ghép thiếu
where t2.nghiepvu is not null
select Left(ChungTu,3) as ChungTu,Max(ChungTu) as Vidu from DuLieuKeToan
Group by Left(ChungTu,3)
-- PNVK => 702
-- BH
Update DuLieuKeToan
set NghiepVu=301
where ChungTu like 'BH%' and Nghiepvu=702
-- NK
Update DuLieuKeToan
set NghiepVu=101
where ChungTu like 'NK%' and Nghiepvu=702
--PC
Update DuLieuKeToan
set NghiepVu=501
where ChungTu like 'PC%' and Nghiepvu=702
-- NTT
Update DuLieuKeToan
set NghiepVu=405
where ChungTu like 'NTT%' and Nghiepvu=702
-- XK
Update DuLieuKeToan
set NghiepVu=202
where ChungTu like 'XK%' and Nghiepvu=702
-- PT
Update DuLieuKeToan
set NghiepVu=401
where ChungTu like 'PT%' and Nghiepvu=702
--NVK ==> 702
-- KH
Update DuLieuKeToan
set NghiepVu=621
where ChungTu like 'KH%' and Nghiepvu=702
-- UNC
Update DuLieuKeToan
set NghiepVu=505
where ChungTu like 'UNC%' and Nghiepvu=702
-- PBC
Update DuLieuKeToan
set NghiepVu=1014
where ChungTu like 'PBC%' and Nghiepvu=702
--- BTL: Hàng bán bị trả lại
-- TT200 là 105, TT133 là 1071
Update DuLieuKeToan
set NghiepVu=105
where ChungTu like 'BTL%' and Nghiepvu=702
--- Update bút toán doanh số
Update DuLieuKeToan
set NghiepVu=301,NganhNghe=2,
DinhKhoanTuDong=37,TKno='1311',TKCo='91111'
where ngay>='01/01/2022'
and NghiepVu=702
and Chungtu like 'BH%'
--Update kho cho but toan ban hang
Update DuLieuKeToan
set Kho=3
where ngay>='01/01/2022'
and NghiepVu=301
and Chungtu like 'BH%'
--- Update bút toán gia von xuat kho
Update DuLieuKeToan
set NghiepVu=301,NganhNghe=2,
DinhKhoanTuDong=37,TKno='91121',TKCo='1523'
where ngay>='01/01/2022'
and NghiepVu=702
and Chungtu like 'XK%'
-- update kho nhan ve =0
Update DuLieuKeToan
set khoo=0
where ngay>='01/01/2022'
and NghiepVu=301
and Chungtu like 'XK%' and khoo>0
--- Update nhạp kho
Update DuLieuKeToan
set NghiepVu=101,DinhKhoanTuDong=26,
TkNo='1523',TKCo='33181'
where ngay>='01/01/2022'
and NghiepVu=702
and Chungtu like 'NK%'
--- Update chiet khau mua hang
Update DuLieuKeToan
set NghiepVu=101,DinhKhoanTuDong=26,
TkNo='1523',TKCo='33181',Psno= -Psno
where ngay>='01/01/2022'
and NghiepVu=702
and Chungtu like 'MGG%'
-- Nhap mua dịch vu
Update DuLieuKeToan
set NghiepVu=101,DinhKhoanTuDong=123,
TkNo='91128',TKCo='33181'
where ngay>='01/01/2022'
and NghiepVu=702
and Chungtu like 'MDV%'
--- Update Thu tien ngan hang
Update DuLieuKeToan
set NghiepVu=405,DinhKhoanTuDong=72,
TkNo='1112',TKCo='1311'
where ngay>='01/01/2022'
and NghiepVu=702
and Chungtu like 'NTTK%'
--- Update Thu tien tien
Update DuLieuKeToan
set NghiepVu=401,DinhKhoanTuDong=3,
TkNo='1111',TKCo='1311'
where ngay>='01/01/2022'
and NghiepVu=702
and Chungtu like 'PT%'
--- Update chi tien ngan hang
Update DuLieuKeToan
set NghiepVu=505,DinhKhoanTuDong=60,
TkNo='33181',TKCo='1112'
where ngay>='01/01/2022'
and NghiepVu=702
and Chungtu like 'UNC%'
--- Update chi tien mat
Update DuLieuKeToan
set NghiepVu=501,DinhKhoanTuDong=12,
TkNo='1112',TKCo='1111'
where ngay>='01/01/2022'
and NghiepVu=702
and Chungtu like 'PC%'