inv_noorderopin_b.pas

来自「一个MRPII系统源代码版本」· PAS 代码 · 共 1,160 行 · 第 1/3 页

PAS
1,160
字号
  inherited;
  with AdoQry_Head do
  begin
    fieldbyname('whCode').asstring:=getCode(cmbbx_WhCode.text);
    fieldbyname('WhPositionCode').asstring:=getCode(cmbbx_WhPositionCode.text);
    fieldbyname('InvBilldate').asstring:=medt_Date.text;
    fieldbyname('InvBillno').asstring:=Billno;
  end;
  //此程序不能做这一事件,因为用了CASE语句,所以不能增加
end;

procedure TFrm_Inv_NoOrderOpIn_B.FormCreate(Sender: TObject);
begin
  inherited;
  Frm_Entry_Detail:=TFrm_Inv_NoOrderOpIn_D.Create(Self);//设置弹出Detail
  SetFocus_Control:=cmbbx_WhCode;//设置 新增时要聚焦的Control
  cmbbx_WhCode.OnChange:=cmbbx_WhCodeChange;
end;

procedure TFrm_Inv_NoOrderOpIn_B.Extedt_OutCodeExit(Sender: TObject);
var
  currencyName:string;
begin
  inherited;
  UsableVendorCodecheck(sender);
  with AdoQry_tmp do
  begin
    Close;
    sql.clear;
    sql.Add(' select Vendor.currencyCode,'+
            ' currency.currencyCode,currency.currencyName,'+
            ' Vendortaxrate_Percent '+
            ' from Vendor '+
            '  left join currency '+
            '   on Vendor.currencyCode=currency.currencyCode '+
            '  where Vendor.VendorCode='''+Extedt_OutCode.text+'''');
    open;
    currencyCode:=fieldbyname('currencyCode').asstring;
    Vendortaxrate_Percent:=fieldbyname('Vendortaxrate_Percent').asstring;
    lb_Vendortaxrate_Percent.Caption:=fieldbyname('Vendortaxrate_Percent').asstring+'%';
    lbl_CurrencyName.Caption:=fieldbyname('currencyCode').asstring+' '+fieldbyname('currencyName').asstring;
    currencyName:=fieldbyname('currencyCode').asstring+' '+fieldbyname('currencyName').asstring;
    if currencyCode<>'00' then
    begin
      Close;
      sql.clear;
      sql.Add('select ExchRate from ExchRate where exchMonth='''+copy(medt_Date.text,1,7)+''' and currencyCode='''+currencyCode+'''');
      open;
      if not eof then
      begin
        ExchRate:=fieldbyname('ExchRate').asstring;
      end
      else
      begin
        //这里将存在一个问题,暂不考虑
        DispInfo('该供应商主控货币'+currencyCode+' '+currencyName+'在'+copy(medt_Date.text,1,7)+' 月没有定义!',3);
        ExchRate:='1';
      end;
    end;
  end;
  {else
  begin
    if cmbbx_OutType.text='部门' then
    begin
      DeptCodecheck(sender);
    end
    else
    begin
      lbl_OutName.Caption:='';
      Extedt_OutCode.Text:='';
    end;
    lb_Vendortaxrate_Percent.Caption:='0'+'%';
    Vendortaxrate_Percent:='0';
  end;}
  Frm_Entry_Detail.SetFormParam('',Extedt_OutCode.text,currencyCode,Vendortaxrate_Percent,ExchRate,medt_Date.text);
end;

procedure TFrm_Inv_NoOrderOpIn_B.SaveData;
var
  Bill_Id_1201,update_field,fields,fieldValues,tmp_InvBillPrice,tmp_InvBillnotaxPrice,
  tmp_InvBillAmount,tmp_InvBillnotaxAmount,tmp_InvBillPricec,tmp_WhCode,tmp_WhPositionCode,
  tmp_InvBillnotaxPricec,tmp_InvBillAmountc,tmp_InvBillnotaxAmountc:string;
  NeedChangePrice,BeforeSaveError:boolean;
  M:integer;
  Amount:real;
begin
  inherited;
  tmp_WhCode:=getCode(cmbbx_WhCode.text);
  tmp_WhPositionCode:=getCode(cmbbx_WhPositionCode.text);
  if AdoQry_Body.RecordCount<1 then
  begin
    DispInfo('新增的单据没有记录,不允许保存!',1);
    abort;
  end;

  //主表存盘
  if AdoQry_Head.state<>dsBrowse then
    AdoQry_Head.post;

  if AdoQry_Body.state<>dsBrowse then
    AdoQry_Body.post;

     fields:='VendorCode,';
     fieldValues:=''''+Extedt_OutCode.text+''',';
  NeedChangePrice:=NeedChangeAveragePrice(AdoQry_tmp,copy(MEdt_Date.text,1,7));
  AdoQry_Body.Connection.beginTrans;
  try
    //全面有效检查
    BeforeSaveError:=True;
    //检查日期、库存
    medt_Date.onexit(medt_Date);
    AdoQry_Body.First;
    for M:=0 to AdoQry_Body.recordCount-1 do
    begin
      if not InvQtyCheck
          (NeedChangePrice,
           AdoQry_Body,
           AdoQry_tmp,
           tmp_WhCode,
           tmp_WhPositionCode,
           AdoQry_Body.fieldbyname('ItemCode').asstring,
           'InvBillqty',
           AdoQry_Body.fieldbyname('InvBillqty').asfloat,
           True) then
        abort;
      if AdoQry_Body.fieldbyname('Batchno').asstring<>'' then
      begin
        if not BatchInvCheck(AdoQry_tmp,
                             AdoQry_Body.fieldbyname('Batchno').asstring,
                             'R',
                             tmp_WhCode,
                             tmp_WhPositionCode,
                             AdoQry_Body.fieldbyname('InvBillqty').asfloat) then
          abort;
      end;
        AdoQry_Body.next;
    end;
    //真正开始存盘
    BeforeSaveError:=False;
    //取单据号
    Billno:=getno(AdoQry_Body.Connection,getCode(cmbbx_WhCode.text)+'R'+copy(medt_Date.text,3,2)+copy(medt_Date.text,6,2),'InvBill');
    BillHeadno:=Billno;
       //记录本仓库数据发生改变
    ChangeInvStatus(AdoQry_tmp,getCode(cmbbx_WhCode.text),copy(medt_Date.text,1,7));
     //增加InvInBill
    AdoQry_tmp.Close;
    AdoQry_tmp.sql.clear;
    AdoQry_tmp.sql.Add('insert InvInBill'+
                       '(InvBillno,'+
                       'whCode,'+
                       'InvBilldate,'+
                       'InvBillMonth,'+
                       'BillTypeCode,'+
                       'BillType2Code,'+
                       'opBill,'+
                       'EmployeeCode,'+
                       'Stk_EmployeeCode,'+
                       fields+
                       'WhPositionCode,'+
                       'InvBillStkchck,'+
                       'InvBillWHChck,'+
                       'currencyCode,'+
                       'InvBilltaxrate,'+
                       'InvBillremArk) '+
               'Values('''+Billno+''','+
                      ''''+getCode(cmbbx_WhCode.text)+''','+
                      ''''+medt_Date.text+''','+
                      ''''+copy(medt_Date.text,1,7)+''','+
                      ''''+BillTypeCode+''','+
                      ''''+'116'+''','+
                      '1,'+
                      ''''+userCode+''','+
                      ''''+userCode+''','+
                      fieldValues+
                      ''''+getCode(cmbbx_WhPositionCode.text)+''','+
                      '1,'+
                      '1,'+
                      ''''+currencyCode+''','+
                      Vendortaxrate_Percent+','+
                      ''''+Extedt_memo.text+''')');

      AdoQry_tmp.execsql;
          //取InvBillID
    Billid:='';
    AdoQry_tmp.Close;
    AdoQry_tmp.sql.clear;
    AdoQry_tmp.sql.Add(' select @@IDENTITY as Billid ');
    AdoQry_tmp.open;
    Billid:=AdoQry_tmp.fieldbyname('Billid').asstring;
    Amount:=0;
    with AdoQry_Body do
    begin
      First;
      for M:=0 to recordCount-1 do
      begin
        //由于显示在DBGRID中的价格不管外币还是人民币,都是用同一个字段,因
        //此在存盘时把字段分开,如下:
        if currencyCode='00' then
        begin
          tmp_InvBillPrice:=fieldbyname('InvBillPrice').asstring;
          tmp_InvBillnotaxPrice:=fieldbyname('InvBillnotaxPrice').asstring;
          tmp_InvBillAmount:=fieldbyname('InvBillAmount').asstring;
          tmp_InvBillnotaxAmount:=fieldbyname('InvBillnotaxAmount').asstring;
          tmp_InvBillPricec:=fieldbyname('InvBillPrice').asstring;
          tmp_InvBillnotaxPricec:=fieldbyname('InvBillnotaxPrice').asstring;
          tmp_InvBillAmountc:=fieldbyname('InvBillAmount').asstring;
          tmp_InvBillnotaxAmountc:=fieldbyname('InvBillnotaxAmount').asstring;
        end
        else
        begin
          tmp_InvBillPrice:=floattostr(fieldbyname('InvBillPrice').asfloat/strtofloat(ExchRate));
          tmp_InvBillnotaxPrice:=floattostr(fieldbyname('InvBillnotaxPrice').asfloat/strtofloat(ExchRate));
          tmp_InvBillAmount:=floattostr(fieldbyname('InvBillAmount').asfloat/strtofloat(ExchRate));
          tmp_InvBillnotaxAmount:=floattostr(fieldbyname('InvBillnotaxAmount').asfloat/strtofloat(ExchRate));
          tmp_InvBillPricec:=fieldbyname('InvBillPrice').asstring;
          tmp_InvBillnotaxPricec:=fieldbyname('InvBillnotaxPrice').asstring;
          tmp_InvBillAmountc:=fieldbyname('InvBillAmount').asstring;
          tmp_InvBillnotaxAmountc:=fieldbyname('InvBillnotaxAmount').asstring;
        end;
        //只用人民币结算
        Amount:=Amount+strtofloat(tmp_InvBillAmount);
        //更改加权平均价
        if True then
        begin
          AdoQry_tmp.Close;
          AdoQry_tmp.sql.clear;
          AdoQry_tmp.sql.text:=
            ' if not exists (select Apqty from AveragePrice '+
                            ' where whCode='''+getCode(CmbBx_WhCode.text)+''' and '+
                                 ' ItemCode='''+fieldbyname('ItemCode').asstring+''')'+
            ' insert AveragePrice(whCode,ItemCode) '+
                 ' Values('''+getCode(CmBbx_WhCode.text)+''','+
                 ''''+fieldbyname('ItemCode').asstring+''')'+
            ' update AveragePrice '+
            ' set Apqty=Apqty+'+fieldbyname('InvBillqty').asstring+','+
            '  ApAmount=ApAmount+'+tmp_InvBillnotaxAmount+
             ' where whCode='''+getCode(CmBbx_WhCode.text)+''' and '+
                            '       ItemCode='''+fieldbyname('ItemCode').asstring+''''+
            ' update AveragePrice '+
                            '  set AveragePrice=case when Apqty=0 then AveragePrice else ApAmount/Apqty end '+
                            ' where whCode='''+getCode(CmbBx_WhCode.text)+''' and '+
                            '       ItemCode='''+fieldbyname('ItemCode').asstring+'''';
          AdoQry_tmp.execsql;
        end;
        //增加InvBillLINE
        AdoQry_tmp.Close;
        AdoQry_tmp.sql.clear;
        AdoQry_tmp.sql.Add('insert InvInBillline'+
                                     '(InvBillid,'+
                                     'InvBilllineno,'+
                                     'ItemCode,'+
                                     'InvBillqty,'+
                                     'BilllineremArk,'+
                                     'Batchno,'+
                                     'bl.InvBillPrice,'+
                                     'bl.InvBillAmount,'+
                                     'bl.InvBillnotaxPrice,'+
                                     'bl.InvBillnotaxAmount,'+
                                     'bl.InvBillPricec,'+
                                     'bl.InvBillAmountc,'+
                                     'bl.InvBillnotaxPricec,'+
                                     'bl.InvBillnotaxAmountc )'+
                           'Values('+Billid+','+
                                   ''''+fieldbyname('InvBilllineno').asstring+''','+
                                   ''''+fieldbyname('ItemCode').asstring+''','+
                                   fieldbyname('InvBillqty').asstring+','+
                                   ''''+fieldbyname('BilllineremArk').asstring+''','+
                                   ''''+fieldbyname('Batchno').asstring+''','+
                                   tmp_InvBillPrice+','+
                                   tmp_InvBillAmount+','+
                                   tmp_InvBillnotaxPrice+','+
                                   tmp_InvBillnotaxAmount+','+
                                   tmp_InvBillPricec+','+
                                   tmp_InvBillAmountc+','+
                                   tmp_InvBillnotaxPricec+','+
                                   tmp_InvBillnotaxAmountc+')' );
        AdoQry_tmp.ExecSQL;

        //更改当前库存  更改Item表的库存数据
       if  UpdateInv(AdoQry_tmp,
                  tmp_WhCode,
                  tmp_WhPositionCode,
                  fieldbyname('ItemCode').asstring,
                  '('+fieldbyname('InvBillqty').asstring+')',
                  NeedChangePrice,True)<>'' then
         abort;

        //更改批次库存数量
        if fieldbyname('Batchno').asstring<>'' then
        begin
          ChangeBatchInv(AdoQry_tmp,fieldbyname('Batchno').asstring,
                         'R',
                         medt_Date.text,
                         tmp_WhCode,
                         tmp_WhPositionCode,
                         fieldbyname('InvBillqty').asfloat);
        end;
        next;
      end;
    end;
    //更改当前应收应付
    with AdoQry_tmp do
    begin
      Close;
      sql.clear;
      sql.Add('select ChangeAp from BillType2 where BillType2Code='''+getCode(cmbbx_BillType2Code.text)+'''');
      open;
      if (fieldbyname('ChangeAp').asinteger=1)  then
      begin
        Close;
        sql.clear;
        sql.Add('if not exists (select VendorCode '+
                                ' from CurrentAp '+
                                ' where VendorCode='''+Extedt_OutCode.text+''' and '+
                                     '  currencyCode='''+currencyCode+''') '+
                ' insert CurrentAp(VendorCode,currencyCode) '+
                          ' Values('''+Extedt_OutCode.text+''','''+currencyCode+''') '+
                ' update CurrentAp '+
                '    set ApNoInvoice=ApNoInvoice+'+floattostr(Amount)+
                ' where VendorCode='''+Extedt_OutCode.text+''' and '+
                '       currencyCode='''+currencyCode+''' ');
        execsql;
      end;
    end;

        //增加一张单据记录材料费
    Billno:=getno(AdoQry_Body.Connection,getCode(cmbbx_WhCode.text)+'R'+copy(medt_Date.text,3,2)+copy(medt_Date.text,6,2),'InvBill');
    AdoQry_Tmp.Close;
    AdoQry_Tmp.SQL.Text:=
      'Insert InvInBill'
          +'(InvBillNo,WHCode,InvBillDate,InvBillMonth,currencyCode,InvBillTaxRate'
          +',BillTypeCode,EmployeeCode,WH_EmployeeCode,VendorCode,WhPositionCode'
          +',InvBillRemArk,opBill,OverPlan)'
     +' Values('
          +''''+BillNo+''''
          +','''+GetCode(cmbbx_WhCode.Text)+''''
          +','''+medt_Date.Text+''''
          +','''+Copy(medt_Date.Text,1,7)+''''
          +','''+CurrencyCode+''''
          +','+Vendortaxrate_Percent
          +','''+'1201'+''''
          +','''+UserCode+''''
          +','''+UserCode+''''
          +','''+Extedt_OutCode.Text+''''
        +','''+GetCode(cmbbx_WhPositionCode.Text)+''''
        +','''+Extedt_memo.Text+''''
        +',1,0'
        +')';
    AdoQry_Tmp.ExecSQL;
    AdoQry_Tmp.Close;
    AdoQry_Tmp.SQL.Text:='Select @@identity as Bill_Id';
    AdoQry_Tmp.Open;
    Bill_Id_1201:=AdoQry_Tmp.fieldbyname('Bill_Id').AsString;

    ChangeInvStatus(AdoQry_Tmp,GetCode(cmbbx_WhCode.Text),Copy(medt_Date.Text,1,7));
    AdoQry_Body.First;
    for M:=0 to AdoQry_Body.recordCount-1 do
    begin
      AdoQry_Tmp.Close;
      AdoQry_Tmp.SQL.Text:='Insert InvInBillLine'
          +'(InvBillId,InvBillLineNo,ItemCode,BillLineRemArk)'
          +' Values('
          +' '+Bill_Id_1201+''
          +','+''''+AdoQry_Body.fieldbyname('InvBilllineno').asstring+''''
          +','''+AdoQry_Body.fieldbyname('ItemCode').AsString+''''
          +','''+AdoQry_Body.fieldbyname('BillLineRemArk').AsString+''''
          +')';
      AdoQry_Tmp.ExecSQL;
      AdoQry_Body.next;
    end;
    AdoQry_RealOpQty.filtered:=False;
    AdoQry_RealOpQty.First;
    For M:=0 to AdoQry_RealOpQty.recordCount-1 do
    begin
      //如果存在,更改所有物料的定额及损耗,否则增加
      AdoQry_Tmp.Close;
      AdoQry_Tmp.SQL.Text:=
{        'If Not Exists (Select ItemCode'
        +' From RealOpQty'
        +' Where InvBillId='+Billid
        +' And InvBillLineNo='''+AdoQry_RealOpQty.fieldbyname('InvBillLineNo').AsString+''''
        +' And Ite_ItemCode='''+AdoQry_RealOpQty.fieldbyname('Ite_ItemCode').AsString+''''
        +' And ItemCode='''+AdoQry_RealOpQty.fieldbyname('ItemCode').AsString+''')'
}
        ' Insert RealOpQty(InvBillId,InvBillLineNo,Ite_ItemCode,ItemCode'
        +',RealOpQty,RealOpQtyNew,RealOPScrAp_Percent,RealOPScrAp_PercentNew)'
        +' Values('

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?