/// <summary>
 /// 检查wmsno的单号是否已经审核
 /// </summary>
 /// <param name="bzmst">主单</param>
 /// <returns></returns>
 private bool ChkHasAdt(wms_bzmst bzmst)
 {
     return(bzmst.chkflg == GetY());
 }
        public ActionResult BokRetrieve(String wmsno)
        {
            using (TransactionScope scop = new TransactionScope(TransactionScopeOption.Required, options))
            {
                Rm.ResultObject = null;
                //检索捡货单主表、明细表
                var qrymst = from e in WmsDc.wms_cang
                             where e.bllid == WMSConst.BLL_TYPE_RETRIEVE &&
                             e.wmsno == wmsno
                             select e;
                var arrmst = qrymst.ToArray();
                var qrydtl = from e in WmsDc.wms_cangdtl
                             where e.bllid == WMSConst.BLL_TYPE_RETRIEVE &&
                             e.wmsno == wmsno
                             select e;
                var arrdtl = qrydtl.ToArray();

                #region 检查输入参数
                if (arrmst.Length <= 0)
                {
                    return(RNoData("N0250"));
                }
                if (arrdtl.Length <= 0)
                {
                    return(RNoData("N0251"));
                }
                wms_cang mst = arrmst[0];
                ////正在生成拣货单,请稍候重试
                //string quRetrv = mst.qu;
                //if (DoingRetrieve(LoginInfo.DefStoreid, quRetrv))
                //{
                //    return RInfo( "I0392" );
                //}
                foreach (wms_cangdtl d in arrdtl)
                {
                    //是否捡货单商品已经审核
                    if (d.bokflg == GetN() && d.tpcode == "y")
                    {
                        return(RInfo("I0462", d.gdsid));
                    }
                }
                if (mst != null && mst.chkflg == GetY())
                {
                    return(RInfo("I0463"));
                }
                #endregion

                #region 播种单查询
                ////播种单主表,明细表
                var qrybzmst = from e in WmsDc.wms_bzmst
                               where e.bllid == WMSConst.BLL_TYPE_BZ &&
                               e.lnkbocidat == mst.lnkbocidat &&
                               e.lnkbocino == mst.lnkbocino &&
                               e.qu == mst.qu &&
                               e.savdptid == mst.savdptid
                               select e;
                var arrbzmst = qrybzmst.ToArray();
                if (arrbzmst.Length <= 0)
                {
                    return(RNoData("N0252"));
                }
                wms_bzmst bzmst = arrbzmst[0];
                //播种单明细表
                var qrybzdtl = from e in WmsDc.wms_bzdtl
                               where e.wmsno == bzmst.wmsno &&
                               arrdtl.Select(e1 => e1.gdsid).Contains(e.gdsid.Trim())
                               select e;
                wms_bzdtl[] arrbzdtls = qrybzdtl.ToArray();
                #endregion

                #region 差异数量生成捡货损溢单,记日志

                //差异数量生成捡货损溢单,记日志
                #region 差异数量生成捡货损溢单,记日志
                //生成捡货损溢单
                JsonResult jr = (JsonResult)MakeNewBllNo(this.LoginInfo.DefSavdptid, mst.qu, WMSConst.BLL_TYPE_RETRIEVE_PROFIT_LOSS, (bllno) =>
                {
                    //生成损溢单主表
                    wms_cang sycang   = new wms_cang();
                    sycang.wmsno      = bllno;
                    sycang.bllid      = WMSConst.BLL_TYPE_RETRIEVE_PROFIT_LOSS;
                    sycang.savdptid   = mst.savdptid;
                    sycang.prvid      = mst.prvid;
                    sycang.qu         = mst.qu;
                    sycang.rcvdptid   = mst.rcvdptid;
                    sycang.times      = mst.times;
                    sycang.lnkbocino  = mst.lnkbocino;
                    sycang.lnkbocidat = mst.lnkbocidat;
                    sycang.mkr        = LoginInfo.Usrid;
                    sycang.mkedat     = DateTime.Now.ToString("yyyyMMdd");
                    sycang.mkedat2    = GetCurrentDate();
                    sycang.ckr        = LoginInfo.Usrid;
                    sycang.chkflg     = GetY();
                    sycang.chkdat     = DateTime.Now.ToString("yyyyMMddHHmmss");
                    sycang.opr        = LoginInfo.Usrid;
                    sycang.brief      = "";
                    sycang.lnkbllid   = mst.bllid;
                    sycang.lnkno      = mst.wmsno;
                    sycang.lnkbrief   = mst.brief;

                    //生成损溢单明细
                    List <wms_cangdtl> lstsydtl = new List <wms_cangdtl>();
                    foreach (wms_cangdtl dt in arrdtl)
                    {
                        if (dt.qty != dt.preqty)
                        {
                            wms_cangdtl sydtl = new wms_cangdtl();
                            sydtl.wmsno       = bllno;
                            sydtl.bllid       = WMSConst.BLL_TYPE_RETRIEVE_PROFIT_LOSS;
                            sydtl.rcdidx      = dt.rcdidx;
                            sydtl.oldbarcode  = dt.oldbarcode;
                            sydtl.barcode     = dt.barcode;
                            sydtl.gdsid       = dt.gdsid;
                            sydtl.pkgid       = dt.pkgid;
                            sydtl.pkgqty      = dt.pkgqty;
                            sydtl.qty         = dt.preqty.Value - dt.qty;
                            sydtl.gdstype     = dt.gdstype;
                            sydtl.bthno       = dt.bthno;
                            sydtl.vlddat      = dt.vlddat;
                            sydtl.bcd         = dt.bcd;
                            sydtl.tpcode      = dt.tpcode;
                            sydtl.bkr         = LoginInfo.Usrid;
                            sydtl.bokflg      = GetY();
                            sydtl.bokdat      = DateTime.Now.ToString("yyyyMMddHHmmss");
                            sydtl.preqty      = dt.preqty.Value - dt.qty;
                            lstsydtl.Add(sydtl);
                            //记录日志
                            Log.i(LoginInfo.Usrid, Mdlid, bllno, WMSConst.BLL_TYPE_RETRIEVE_PROFIT_LOSS, "损溢单生成", dt.gdsid.Trim() + ":应收:" + dt.preqty.Value + ";实收:" + dt.qty, mst.qu, mst.savdptid);
                            //记账并删除为0的仓位信息。
                            var cwggdsbs = WmsDc.wms_cwgdsbs
                                           .Where(e => e.barcode == sydtl.barcode && e.bcd == sydtl.bcd && e.savdptid == mst.savdptid && e.gdsid == sydtl.gdsid && e.gdstype == sydtl.gdstype)
                                           .Select(e => e).Single();
                            if (cwggdsbs != null)
                            {
                                cwggdsbs.qty -= sydtl.qty;
                                if (cwggdsbs.qty <= 0)
                                {
                                    WmsDc.wms_cwgdsbs.DeleteOnSubmit(cwggdsbs);
                                    iDelCwgdsbs(new wms_cwgdsbs[] { cwggdsbs });
                                }
                            }
                        }
                    }
                    WmsDc.wms_cang.InsertOnSubmit(sycang);
                    WmsDc.wms_cangdtl.InsertAllOnSubmit(lstsydtl);


                    return(RRSucc("成功", null, "{{succ}}"));
                });


                Rm = (ResultMessage)jr.Data;
                if (Rm.ResultCode != ResultMessage.RESULTMESSAGE_SUCCESS)
                {
                    return(jr);
                }
                #endregion


                //拣货数量和应拣数量判断是不是相等, 不相等就改播种单数量
                #region 拣货数量和应拣数量判断是不是相等, 不相等就改播种单数量
                var arrdtly = arrdtl
                              .Where(e => e.tpcode == "y")
                              .Select(e => new { e.gdsid, e.gdstype, e.qty });
                var arrdtln = arrdtl
                              .Where(e => e.tpcode == "n")
                              .Select(e => new { e.gdsid, e.gdstype, qty = 0.00 });
                var qsum = arrdtly.Union(arrdtln)
                           .GroupBy(g => new { g.gdsid, g.gdstype })
                           .Select(g => new { gdsid = g.Key.gdsid, gdstype = g.Key.gdstype, sumqty = g.Sum(e => e.qty) });
                foreach (var q in qsum)
                {
                    //查找播种明细商品汇总数量是否和捡货单的商品汇总数量一致
                    var qbzsum = arrbzdtls
                                 .Where(e => e.gdsid == q.gdsid && e.gdstype == q.gdstype)
                                 .GroupBy(g => new { g.gdsid, g.gdstype })
                                 .Select(g => new { gdsid = g.Key.gdsid, gdstype = g.Key.gdstype, qsum = g.Sum(e => e.qty) }).Single();
                    if (qbzsum.qsum < q.sumqty)    //如果数量不一致就生成捡货损溢单,并记日志
                    {
                        return(RInfo("I0464"));
                    }
                    else if (qbzsum.qsum >= q.sumqty)    //如果播种的数量小于捡货单的数量
                    {
                        double remainqty       = q.sumqty;
                        var    arrbzdtlsGdstyp = arrbzdtls.Where(e => e.gdstype == q.gdstype && e.gdsid == q.gdsid);
                        //-----------分派数量----------
                        //按照播种数量从大到小的顺序   |
                        //从小到大开始播种             |
                        //播种到最后就播0              |
                        //-----------------------------

                        /*String cmdsql = "declare @remainqty deciaml(18,4)\r\n"
                         + "declare @wmsno varchar(20), @gdstype varchar(20), @rcdidx int, @gdsid varchar(10), @rcvdptid varchar(10), @qty decimal\r\n"
                         + "set @remainqty={0}\r\n"
                         + "set @gdsid={1}\r\n"
                         + "set @gdstype={2}\r\n"
                         + "set @wmsno={3}\r\n"
                         + "declare cur1 cursor for\r\n"
                         + "    select rcvdptid, qty, rcdidx from wms_bzdtl where wmsno=@wmsno and gdstype=@gdstype and gdsid=@gdsid order by qty desc, rcdidx \r\n"
                         + "open cur1\r\n"
                         + "fetch next from cur1 into @rcvdptid, @qty, @rcdidx\r\n"
                         + "while @@fetch_status=0\r\n"
                         + "begin\r\n"
                         + "    if @remainqty-@qty>0\r\n"
                         + "    begin\r\n"
                         + "        update wms_bzdtl set qty = @qty where wmsno=@wmsno and gdstype=@gdstype and gdsid=@gdsid and rcdidx=@rcdidx\r\n"
                         + "    end\r\n"
                         + "    else if @remainqty>0 and @remainqty-@qty<=0\r\n"
                         + "    begin\r\n"
                         + "        update wms_bzdtl set qty = @remainqty where wmsno=@wmsno and gdstype=@gdstype and gdsid=@gdsid and rcdidx=@rcdidx\r\n"
                         + "    end\r\n"
                         + "    else\r\n"
                         + "    begin\r\n"
                         + "        update wms_bzdtl set qty = 0 where wmsno=@wmsno and gdstype=@gdstype and gdsid=@gdsid and rcdidx=@rcdidx\r\n"
                         + "    end\r\n"
                         + "    set @remainqty = @remainqty-@qty\r\n"
                         + "    fetch next from cur1 into @rcvdptid, @qty, @rcdidx\r\n"
                         + "end\r\n"
                         + "close cur1\r\n"
                         + "deallocate cur1";*/
                        /*
                         * preqty = preqty==null ? qty : preqty
                         *
                         * 公式:taxamt = qty*prc*taxrto
                         * amt = qty*prc
                         * salamt = qty*salprc
                         * patamt = qty*taxprc
                         * stotcstamt = qty*stotcstprc
                         *
                         */

                        String cmdsql = "";
                        if (mst.lnkbllid.Trim() == "206" || mst.lnkbllid.Trim() == "501")
                        {
                            cmdsql = "declare @remainqty deciaml(18,4)\r\n"
                                     + "declare @wmsno varchar(20), @gdstype varchar(20), @rcdidx int, @gdsid varchar(10), @rcvdptid varchar(10), @qty decimal, @stkouno varchar(30)\r\n"
                                     + "set @remainqty={0}\r\n"
                                     + "set @gdsid={1}\r\n"
                                     + "set @wmsno={2}\r\n"
                                     + "declare cur1 cursor for\r\n"
                                     + "    select b.rcvdptid, a.qty, a.rcdidx, b.stkouno from stkotdtl a inner join stkot b on a.stkouno=b.stkouno\r\n"
                                     + "          where b.wmsno=@wmsno and b.wmsbllid='" + WMSConst.BLL_TYPE_RETRIEVE + "' and a.gdsid=@gdsid order by qty desc, rcdidx \r\n"
                                     + "open cur1\r\n"
                                     + "fetch next from cur1 into @rcvdptid, @qty, @rcdidx, @stkouno\r\n"
                                     + "while @@fetch_status=0\r\n"
                                     + "begin\r\n"
                                     + "     update stkotdtl set preqty=case when preqty is null then qty else preqty end where stkouno=@stkouno and wmsbllid='" + WMSConst.BLL_TYPE_RETRIEVE + "' and gdsid=@gdsid and rcvidx=@rcvidx  \r\n"
                                     + "    if @remainqty-@qty>0\r\n"
                                     + "    begin\r\n"
                                     + "        update stkotdtl set qty = @qty where stkouno=@stkouno and gdsid=@gdsid and rcdidx=@rcdidx\r\n"
                                     + "    end\r\n"
                                     + "    else if @remainqty>0 and @remainqty-@qty<=0\r\n"
                                     + "    begin\r\n"
                                     + "        update stkotdtl set qty = @remainqty where stkouno=@stkouno and gdsid=@gdsid and rcdidx=@rcdidx\r\n"
                                     + "    end\r\n"
                                     + "    else\r\n"
                                     + "    begin\r\n"
                                     + "        update stkotdtl set qty = 0 where stkouno=@stkouno and gdsid=@gdsid and rcdidx=@rcdidx\r\n"
                                     + "    end\r\n"
                                     + "    update stkotdtl set amt=qty*prc, salamt=qty*salprc, patamt=qty*taxprc, stotcstamt=qty*stotcstprc where stkouno=@stkouno and gdsid=@gdsid and rcdidx=@rcdidx \r\n"
                                     + "    set @remainqty = @remainqty-@qty\r\n"
                                     + "    fetch next from cur1 into @rcvdptid, @qty, @rcdidx, @stkouno\r\n"
                                     + "end\r\n"
                                     + "close cur1\r\n"
                                     + "deallocate cur1";
                            WmsDc.ExecuteCommand(cmdsql, new object[] { remainqty, q.gdsid, bzmst.wmsno });
                        }
                    }
                }
                #endregion

                #endregion

                #region 修改审核标记
                //修改审核标记
                mst.chkflg = GetY();
                mst.chkdat = DateTime.Now.ToString("yyyyMMddHHmmss");
                mst.ckr    = LoginInfo.Usrid;
                #endregion

                try
                {
                    WmsDc.SubmitChanges();
                    return(RSucc("成功", null, "S0225"));
                }
                catch (Exception ex)
                {
                    return(RErr(ex.Message, "E0073"));
                }
            }
        }