📄 inv_opout_b.pas
字号:
end;
DBGridEh.Refresh;
ToolButton2.Enabled:=True;}
end;
procedure TFrm_Inv_OpOut_B.SaveData;
var i,m,x :Integer; //循环控制变量
HasRec,BeforeSaveError :Boolean; //HASREC是标志是否有合格记录,
BillNo :String; //保存获得的单据号
Billid :String; //保存单据头中的字增字段值
PriceType,ItemListId :Integer; //价格计算方法
NeedChangePrice:Boolean;
Price:double;
begin
// inherited;
if AdoQry_Body.State<>dsBrowse then
AdoQry_Body.Post;
//AdoQry_Body.DisableControls;
AdoQry_Body.First;
HasRec:=False;
For I:=0 to AdoQry_Body.RecordCount-1 do
begin
If AdoQry_Body.fieldbyname('POOutQty').AsFloat<>0 then
begin
HasRec:=True;
Break;
end;
AdoQry_Body.Next;
end;
If not HasRec then
begin
DispInfo('新增的单据没有记录,不允许保存!',1);
disableall(True);
AdoQry_Body.EnableControls;
Abort;
end;
//全面有效检查
BeforeSaveError:=True;
Cmbx_WhCodeNameExit(Cmbx_WhCodeName);
Medt_DateExit(Medt_Date);
AdoQry_Body.First;
//上月是否已经结帐,否,不进行移动加权平均价的计算
NeedChangePrice:=NeedChangeAveragePrice(AdoQry_tmp,copy(Medt_Date.text,1,7));
for M:=0 to AdoQry_Body.recordCount-1 do
begin
If round(AdoQry_Body.fieldbyname('PoOutQty').AsFloat*1000)/1000=0 then
begin
AdoQry_Body.next;
continue;
end;
if not InvQtyCheck
(NeedChangePrice,
AdoQry_Body,
AdoQry_tmp,
getCode(Cmbx_WhCodeName.text),
getCode(Cmbx_Position.Text),
AdoQry_Body.fieldbyname('ItemCode').asstring,
'PoOutQty',
AdoQry_Body.fieldbyname('PoOutQty').asfloat,
False) then
begin
disableall(True);
//AdoQry_Body.Connection.RollBackTrans;
AdoQry_Body.EnableControls;
abort;
end;
if AdoQry_Body.fieldbyname('BatchNo').asstring<>'' then
if not BatchInvCheck(AdoQry_Tmp,
AdoQry_Body.fieldbyname('BatchNo').asstring,'c',
getCode(Cmbx_WhCodeName.Text),
getCode(Cmbx_Position.Text),
AdoQry_Body.fieldbyname('POOutQty').asfloat) then Abort;
AdoQry_Body.next;
end;
if not CheckAllSame then//正负号同号检测
begin
DispInfo('不允许正负号同时出现!',1);
//AdoQry_Body.Connection.RollBackTrans;
disableall(True);
AdoQry_Body.EnableControls;
abort;
end;
AdoQry_Tmp.Connection.beginTrans;
try
With AdoQry_Tmp do
begin
Close;
Sql.clear;
Sql.Add('Select PriceType from Warehouse where WHCode='''+GetCode(Cmbx_WhCodeName.Text)+'''');
Open;
PriceType:=AdoQry_Tmp.fieldbyname('PriceType').AsInteger;
end;
BeforeSaveError:=False;
BillNo:=GetNo(AdoQry_Tmp.Connection,GetCode(Cmbx_WhCodeName.Text)+'C'+
FormatDateTime('yymm',StrToDate(Medt_Date.Text)),
'InvBill');
ChangeInvStatus(AdoQry_tmp,getCode(Cmbx_WhCodeName.text),copy(Medt_Date.text,1,7));
With AdoQry_Tmp do
begin
Close;
Sql.clear;
//增加单据头记录
If IsOverPlan then
AdoQry_tmp.sql.Add('insert into InvOutBill'+
'(InvBillno,'+
'OverPlan,'+
'whCode,'+
'InvBilldate,'+
'InvBillMonth,'+
'BillTypeCode,'+
'EmployeeCode,'+
'WH_EmployeeCode,'+
'VendorCode,'+
'PoNo,'+
'WhPositionCode,'+
'InvBillremArk)'+
'Values('''+Billno+''','+
'1,'+
''''+getCode(Cmbx_WhCodeName.text)+''','+
''''+Trim(medt_Date.text)+''','+
''''+copy(medt_Date.text,1,7)+''','+
''''+BillTypeCode+''','+
''''+userCode+''','+
''''+userCode+''','+
''''+GetCode(Lbl_VendorCodeName.Caption)+''','+
''''+FPoNo+''','+
''''+getCode(Cmbx_Position.text)+''','+
''''+Edt_memo.text+''')')
else
AdoQry_tmp.sql.Add('insert into InvOutBill'+
'(InvBillno,'+
'whCode,'+
'InvBilldate,'+
'InvBillMonth,'+
'BillTypeCode,'+
'EmployeeCode,'+
'WH_EmployeeCode,'+
'VendorCode,'+
'PoNo,'+
'WhPositionCode,'+
'InvBillremArk)'+
'Values('''+Billno+''','+
''''+getCode(Cmbx_WhCodeName.text)+''','+
''''+Trim(medt_Date.text)+''','+
''''+copy(medt_Date.text,1,7)+''','+
''''+BillTypeCode+''','+
''''+userCode+''','+
''''+userCode+''','+
''''+GetCode(Lbl_VendorCodeName.Caption)+''','+
''''+FPoNo+''','+
''''+getCode(Cmbx_Position.text)+''','+
''''+Edt_memo.text+''')');
AdoQry_tmp.execsql;
AdoQry_tmp.Close;
AdoQry_tmp.sql.clear;
AdoQry_tmp.sql.Add(' select @@IDENTITY as Billid ');
AdoQry_tmp.open;
Billid:=AdoQry_tmp.fieldbyname('Billid').asstring;
end;
with AdoQry_Body do
begin
First;
for X:=0 to recordCount-1 do
begin
ItemListId:=AdoQry_Body.fieldbyname('ItemListId').asinteger;
If AdoQry_Body.fieldbyname('PoOutQty').AsFloat=0 then
begin
next;
continue;
end;
//如果本仓库属于移动加权平均价计价仓
Price:=0;
if True then//NeedChangePrice then
begin
if PriceType=0 then
begin
//正常委外领料,按常规移动加权平均价的算法计算
If AdoQry_Body.fieldbyname('PoOutQty').AsFloat>0 then
begin
//更改移动价
AdoQry_tmp.Close;
AdoQry_tmp.sql.clear;
AdoQry_Tmp.Sql.Add('UpDate AveragePrice'+
' Set ApQty=ApQty-('+
AdoQry_Body.fieldbyname('PoOutQty').AsString+'),'+
'ApAmount=case Apqty when 0 then 0 else round(ApAmount/Apqty*(Apqty-('+fieldbyname('PoOutQty').AsString+')),2) end '+
' where whCode='''+getCode(Cmbx_WhCodeName.Text)+''' and '+
' ItemCode='''+fieldbyname('ItemCode').AsString+'''');
AdoQry_Tmp.ExecSQL;
//获取当前价格
AdoQry_tmp.Close;
AdoQry_tmp.sql.clear;
AdoQry_Tmp.Sql.Add('Select AveragePrice from AveragePrice'+
' where whCode='''+getCode(Cmbx_WhCodeName.Text)+''' and '+
' ItemCode='''+fieldbyname('ItemCode').AsString+'''');
AdoQry_Tmp.open;
Price:=AdoQry_Tmp.fieldbyname('AveragePrice').asfloat;
end
//退料,则视作入库,按入库的移动加权平均价算法计算
//入库价来自供应商当前的移动价
Else If AdoQry_Body.fieldbyname('PoOutQty').AsFloat<0 then
begin
//计算数量、金额
AdoQry_tmp.Close;
AdoQry_tmp.sql.clear;
AdoQry_Tmp.Sql.Add('UpDate AveragePrice'+
' Set ApQty=ApQty-('+
AdoQry_Body.fieldbyname('PoOutQty').AsString+'),'+
'ApAmount=ApAmount-round('+fieldbyname('PoOutQty').AsString+
'*(Select OpAveragePrice From OpAveragePrice '+
' Where VendorCode='''+GetCode(Lbl_VendorCodeName.Caption)+''''+
' and ItemCode='''+fieldbyname('ItemCode').AsString+'''),2)'+
' where whCode='''+getCode(Cmbx_WhCodeName.Text)+''' and '+
' ItemCode='''+fieldbyname('ItemCode').AsString+'''');
AdoQry_Tmp.ExecSQL;
//获取入库的价格
AdoQry_tmp.Close;
AdoQry_tmp.sql.clear;
AdoQry_Tmp.Sql.Add('Select OpAveragePrice From OpAveragePrice '+
' Where VendorCode='''+GetCode(Lbl_VendorCodeName.Caption)+''''+
' and ItemCode='''+fieldbyname('ItemCode').AsString+'''');
AdoQry_Tmp.open;
Price:=AdoQry_Tmp.fieldbyname('OPAveragePrice').asfloat;
//计算价格
AdoQry_tmp.Close;
AdoQry_tmp.sql.clear;
AdoQry_Tmp.SQL.Add('Update AveragePrice '+
' Set AveragePrice=(ApAmount/ApQty) '+
' where whCode='''+getCode(Cmbx_WhCodeName.Text)+''' and '+
' ItemCode='''+fieldbyname('ItemCode').AsString+''' and '+
' ApQty<>0 and ApQty is not null');
AdoQry_Tmp.ExecSQL;
end;
end
else
//如果本仓库属于计划价仓库
begin
AdoQry_tmp.Close;
AdoQry_tmp.sql.clear;
AdoQry_tmp.sql.text:=
'select isnull((Select isnull(SPPrice,0) From StandardPrice where '+
'SPStArtMonth='''+copy(medt_Date.text,1,7)+''''+
' and ItemCode='''+fieldbyname('ItemCode').AsString+'''),0) Price';
AdoQry_tmp.Open;
Price:=AdoQry_tmp.fieldbyname('Price').asfloat;
end;
end;
//Price:=GetPrice(AdoQry_tmp,fieldbyname('ItemCode').AsString,3);
with AdoQry_tmp do
begin
Close;
sql.text:='select isnull(Price,0) as Price from Sc_PriceMaster '+
' where ItemCode='''+AdoQry_Body.fieldbyname('ItemCode').AsString+'''' ;
open;
Price:=AdoQry_tmp.fieldbyname('Price').asfloat;
end;
//插入单据行
AdoQry_tmp.Close;
AdoQry_tmp.sql.clear;
AdoQry_tmp.sql.Add('insert InvOutBillline'+
'(InvBillid,'+
'BatchNo,'+
'InvBilllineno,'+
'ItemCode,'+
'ite_ItemCode,'+
'pono,'+
'polineno,'+
'InvBillNoTaxPrice,'+
'InvBillNoTaxAmount,'+
'InvBillqty,BillLineReMArk) '+
'Select '+Billid+','+
''''+fieldbyname('BatchNo').asstring+''','+
inttostr(X+1)+','+
''''+fieldbyname('ItemCode').asstring+''','+
''''+fieldbyname('ite_ItemCode').asstring+''','+
''''+lbl_Pono.Caption+''','+
''''+fieldbyname('polineno').asstring+''','+
floattostr(Price)+','+
' Round('+fieldbyname('POOutQty').asstring+
'*'+floattostr(Price)+',2),'+
fieldbyname('POOutQty').asstring+','+
' '''+fieldbyname('BillLineReMArk').asstring+'''');
AdoQry_tmp.ExecSQL;
//更新本厂当前库存
{ AdoQry_Tmp.Close;
AdoQry_Tmp.SQL.clear;
AdoQry_tmp.sql.Add('Update CurrentInv '+
'Set '+
GetInvField(AdoQry_Tmp2,getCode(Cmbx_WhCodeName.text),GetCode(Cmbx_Position.Text),'CurrentInv')+
'='+GetInvField(AdoQry_Tmp2,getCode(Cmbx_WhCodeName.text),GetCode(Cmbx_Position.Text),'CurrentInv')
+'-('+fieldbyname('POOutQty').Asstring+')'+
' Where WhCode='''+GetCode(Cmbx_WhCodeName.Text)+''''+
' and ItemCode='''+fieldbyname('ItemCode').AsString+''''+
' and WhPositionCode='''+GetCode(Cmbx_Position.Text)+'''');
AdoQry_tmp.ExecSQL;
AdoQry_tmp.Close;
AdoQry_tmp.sql.clear;
AdoQry_tmp.sql.Add('Update Item '+
'Set '+
GetInvField(AdoQry_Tmp2,getCode(Cmbx_WhCodeName.text),GetCode(Cmbx_Position.Text),'Item')+
'='+GetInvField(AdoQry_Tmp2,getCode(Cmbx_WhCodeName.text),GetCode(Cmbx_Position.Text),'Item')+
'-('+fieldbyname('PoOutQty').Asstring+') '+
'Where ItemCode='''+fieldbyname('ItemCode').AsString+'''');
AdoQry_tmp.ExecSQL;}
if UpdateInv(AdoQry_Tmp,
getCode(Cmbx_WhCodeName.text),
getCode(Cmbx_Position.text),
AdoQry_Body.fieldbyname('ItemCode').asstring,
'(-('+AdoQry_Body.fieldbyname('POOutQty').asstring+'))',
NeedChangePrice,True)<>'' then
abort;
//更新供应商库存
AdoQry_Tmp.Close;
AdoQry_Tmp.Sql.clear;
AdoQry_Tmp.SQL.Add('Select Count(*) as RecCount from OPCurrentInv '+
' Where VendorCode='''+GetCode(Lbl_VendorCodeName.Caption)+''''+
' and ItemCode='''+fieldbyname('ItemCode').AsString+'''');
AdoQry_Tmp.Open;
If AdoQry_Tmp.fieldbyname('RecCount').AsInteger<=0 then
begin
AdoQry_tmp.Close;
AdoQry_Tmp.Sql.clear;
AdoQry_Tmp.SQL.Add('Insert OPCurrentInv '+
' (VendorCode,'+
' ItemCode,'+
' OpInv)'+
'Values('''+GetCode(Lbl_VendorCodeName.Caption)+''','+
''''+fieldbyname('ItemCode').AsString+''','+
'0)');
AdoQry_Tmp.ExecSQL;
end;
AdoQry_Tmp.Close;
AdoQry_Tmp.SQL.clear;
AdoQry_tmp.SQL.Add('Update OPCurrentInv Set'+
' OpInv=OpInv+'+fieldbyname('PoOutQty').AsString+
' Where VendorCode='''+GetCode(Lbl_VendorCodeName.Caption)+''''+
' and ItemCode='''+fieldbyname('ItemCode').AsString+'''');
AdoQry_Tmp.ExecSQL;
if AdoQry_Body.fieldbyname('BatchNo').asstring<>'' then
ChangeBatchInv(AdoQry_Tmp2,
AdoQry_Body.fieldbyname('BatchNo').asstring,
'c',formatdatetime('YYYY.MM.DD',date),
getCode(Cmbx_WhCodeName.text),
getCode(Cmbx_Position.text),
AdoQry_Body.fieldbyname('POOutQty').asFloat);
//更新领料清单
//如果是原来清单中没有的物料,则Insert
AdoQry_Tmp.Close;
AdoQry_Tmp.SQL.clear;
AdoQry_Tmp.SQL.Add('select Count(*) s from opItemList '+
'where pono='''+fpono+''' '+
' and polineno='''+fieldbyname('polineno').asstring+''' '+
' and ItemCode='''+fieldbyname('ItemCode').asstring+'''');
AdoQry_Tmp.open;
IF (AdoQry_Tmp.fieldbyname('s').Asinteger<=0) then
begin
AdoQry_Tmp.Close;
AdoQry_Tmp.SQL.clear;
AdoQry_Tmp.SQL.Add('Insert OpItemList '+
'(PoNo,'+
'PoLineNo,'+
'ItemCode,'+
'POCtrlQty,'+
'PORealQty,'+
'POOutQty)'+
'Values('''+FPoNo+''','+
''''+fieldbyname('POlineNo').AsString+''','+
''''+fieldbyname('ItemCode').AsString+''','+
''''+fieldbyname('POCtrlQty').AsString+''','+
''''+fieldbyname('POOutQty').AsString+''','+
''''+fieldbyname('POOutQty').AsString+''')');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -