# XỬ LÝ SỐ LƯỢNG QUY ĐỔI

## Yêu cầu

* dữ liệu kế toán nhập theo số lượng đơn vị phụ, nhập vào cột soluong,dongia,psno
* sau đó, khai báo tỷ lệ quy đổi đvt chính và đvt phụ
* Mong muốn

  * Cập nhật lại soluong,dongia => soluong2, dongia2
  * soluong = soluong2 \* tyle
  * dongia = psno / (soluong2 \* tyle)

## Thao tác

* update soluong2=soluong, dongia2=dongia

```sql
declare @nghiepvu int = 101
declare @tungay datetime = '01/01/2022'
declare @denngay datetime = '12/31/2022'
update T1 set T1.soluong2 = T1.soluong, T1.dongia2=T1.dongia2
from 
(
	select *
	from dulieuketoan
	where nghiepvu=@nghiepvu
	and tkno like '15%'
	and ngay>=@tungay and ngay<=@denngay
	and loaidl=0 
	and vattu in (26,21,25,22,23,24,173,172,46,44,102,87,171,14,99,96,100,98,122,37,31,35,30,125,126,36,39,29,43,32,33,41,95,92,94,93,45,91,28,40,42,27,38)
)T1
inner join 
(
	select vattu,dvdo,vdvdo,tyle,congthuc
	from dulieudvdo 
	where tyle<>0 and dvdo<>0 and vattu<>0 and congthuc like '*'
	and vattu in (26,21,25,22,23,24,173,172,46,44,102,87,171,14,99,96,100,98,122,37,31,35,30,125,126,36,39,29,43,32,33,41,95,92,94,93,45,91,28,40,42,27,38)
)T2
on T1.vattu=T2.vattu and T1.dvdo = T2.vdvdo and T1.dvdo <> T2.dvdo
```

* update soluong= soluong2 \* tyle, dongia = psno/(soluong2 \*tyle)

```sql

declare @nghiepvu int = 101
declare @tungay datetime = '01/01/2022'
declare @denngay datetime = '12/31/2022'
update T1 set T1.soluong = T1.soluong2 * T2.tyle, T1.dongia = T1.psno/(T1.soluong2*T2.tyle)
from 
(
	select *
	from dulieuketoan
	where nghiepvu=@nghiepvu
	and tkno like '15%'
	and ngay>=@tungay and ngay<=@denngay
	and loaidl=0 
	and vattu in (26,21,25,22,23,24,173,172,46,44,102,87,171,14,99,96,100,98,122,37,31,35,30,125,126,36,39,29,43,32,33,41,95,92,94,93,45,91,28,40,42,27,38)
)T1
inner join 
(
	select vattu,dvdo,vdvdo,tyle,congthuc
	from dulieudvdo 
	where tyle<>0 and dvdo<>0 and vattu<>0 and congthuc like '*'
	and vattu in (26,21,25,22,23,24,173,172,46,44,102,87,171,14,99,96,100,98,122,37,31,35,30,125,126,36,39,29,43,32,33,41,95,92,94,93,45,91,28,40,42,27,38)
)T2
on T1.vattu=T2.vattu and T1.dvdo = T2.vdvdo and T1.dvdo <> T2.dvdo
```

## TRƯỜNG HỢP 2:

**Mô tả:** Khách hàng nhập liệu ban đầu số dư, nhập mua hàng khai báo mặt hàng theo đơn vị tính to sau đó phát hiện cần chỉnh sửa xuống đơn vị nhỏ nhất để quản lý tồn kho, ví dụ Nhập vào trước là Két/Thùng sau đó cần đổi sang là Chai, Lon

**Phương án:**

* Sao lưu dữ liệu đánh dấu thời điểm hiện tại đang bị sai Dữ liệu quy đổi
* Yêu cầu khách hàng sửa lại và thực hiện khai báo quy đổi đơn vị tính trên danh mục vật tư về đơn vị nhỏ nhất, ví dụ là Chai/Lon
* Thực hiện chạy lệnh sql để tiến hành cập nhật cho đúng số lượng 2, đơn giá 2, => Số lượng, đơn giá

```sql
--1. Update Soluong2 = soluong,dongia2 =dongia, dvdo = DuLieuDvdo=vdvdo
-- Chú ý trên bảng DuLieuDvDo thì lấy cột vDvDo, bổ sung thêm điều kiện where
Update a
set a.Soluong2=a.soluong,a.dongia2=a.dongia,a.dvdo=b.vdvdo
from DuLieuKeToan a
inner join DuLieuDvdo b
on a.Vattu=b.Vattu
where a.NghiepVu=101
--and a.Vattu=47 and b.vattu=47
```

<pre class="language-sql"><code class="lang-sql"><strong>
</strong><strong>--2. Update lại số lượng = số lượng 2* tỷ lệ, đơn giá = Tiền/(số lượng 2* tỷ lệ)
</strong><strong>-- Bổ sung thêm điều kiện where
</strong><strong>update T1
</strong>set T1.soluong = T1.soluong2 * T2.tyle, T1.dongia = case when T1.Soluong2&#x3C;>0 then T1.psno/(T1.soluong2*T2.tyle) else 1 end
From DuLieuKeToan T1
inner join DuLieuDvdo T2
on T1.Vattu = T2.Vattu
Where T1.NghiepVu=101
--and T1.Vattu=47 and T2.vattu=47
</code></pre>

* Tìm kiếm lại dữ liệu nhập mua hàng xem đã điền đúng được 2 cột Số lượng,  số lượng phụ, đơn giá, đơn giá phú là được.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kythuat.dtechvn.com/danh-muc/x-ly-so-luong-quy-doi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
