コード例 #1
0
        public string AddOrUpdatePurchaseOrder(int id, int branchID, DateTime date, DateTime expectedDate, int supplierID, string notes,
                                               decimal discValue, string terms,
                                               List <PurchaseOrderDetailViewModel> detail)
        {
            var poHeader = id == 0
                ? new PurchaseOrderHeader()
                : context.PurchaseOrderHeaders.Single(po => po.ID == id);

            var autoNo = new AutoNumberProvider(context, principal);
            var emp    = new EmployeeProvider(context, principal);

            if (id == 0)
            {
                poHeader.DocumentNo = autoNo.GeneratePurchaseOrderRunningNumber(CurrentCompanyCode, date);
            }
            poHeader.BranchID        = branchID;
            poHeader.Date            = date;
            poHeader.ExpectedDate    = expectedDate;
            poHeader.SupplierID      = supplierID;
            poHeader.Notes           = notes;
            poHeader.DiscountValue   = discValue;
            poHeader.Terms           = terms;
            poHeader.EmployeeID      = emp.GetEmployee(CurrentUserName).ID;
            poHeader.Status          = "O";
            poHeader.StatusReceiving = "O";
            EntityHelper.SetAuditFields(id, poHeader, CurrentUserName);

            //context.SubmitChanges();

            context.PurchaseOrderDetails.DeleteAllOnSubmit(poHeader.PurchaseOrderDetails);

            foreach (var detailLine in detail)
            {
                PurchaseOrderDetailViewModel line = detailLine;
                var     itemProvider = new ItemProvider(context, principal);
                decimal ratio        = itemProvider.GetItemUnitRatio(detailLine.ItemID, detailLine.UnitName);

                var poDetail = new PurchaseOrderDetail
                {
                    ItemID       = detailLine.ItemID,
                    UnitPrice    = detailLine.UnitPrice,
                    Quantity     = detailLine.Quantity,
                    IsTaxed      = detailLine.IsTaxed,
                    DiscountRate = detailLine.DiscountRate,
                    UnitName     = detailLine.UnitName,
                    UnitRatio    = ratio,
                    Notes        = String.Empty
                };
                poDetail.PurchaseOrderHeader = poHeader;
                //poHeader.PurchaseOrderDetails.Add(poDetail);
                context.PurchaseOrderDetails.InsertOnSubmit(poDetail);
            }

            if (id == 0)
            {
                context.PurchaseOrderHeaders.InsertOnSubmit(poHeader);
            }
            context.SubmitChanges();

            return(poHeader.DocumentNo);
        }
コード例 #2
0
        public string AddOrUpdateReceiving(int id, int poID, DateTime date, string goodIssueNo,
                                           string freightInfo, string notes, List <ReceivingDetailViewModel> detail)
        {
            List <ReceivingDetail> receivingDetails =
                context.ReceivingHeaders
                .Where(rcv => rcv.PurchaseOrderID == poID && rcv.ID != id && !rcv.VoidDate.HasValue)
                .SelectMany(x => x.ReceivingDetails)
                .ToList();
            var order = context.PurchaseOrderHeaders.Single(po => po.ID == poID && !po.VoidDate.HasValue);
            List <PurchaseOrderDetail> orderDetails = order.PurchaseOrderDetails.ToList();

            var receiveHeader = id == 0
              ? new ReceivingHeader()
              : context.ReceivingHeaders.Single(rh => rh.ID == id);

            var autoNo = new AutoNumberProvider(context, principal);
            var emp    = new EmployeeProvider(context, principal);

            if (id == 0)
            {
                receiveHeader.DocumentNo = autoNo.GenerateReceivingRunningNumber(CurrentCompanyCode, date);
            }

            receiveHeader.PurchaseOrderID = poID;
            receiveHeader.Date            = date;
            receiveHeader.GoodIssueNo     = goodIssueNo;
            receiveHeader.FreightInfo     = freightInfo;
            receiveHeader.Notes           = notes;
            receiveHeader.Status          = "O";
            EntityHelper.SetAuditFields(id, receiveHeader, CurrentUserName);


            context.ReceivingDetails.DeleteAllOnSubmit(receiveHeader.ReceivingDetails);

            bool isFullReceived = true;

            foreach (var newReceivingDetail in detail)
            {
                var totalQtyPO  = orderDetails.Where(po => po.ItemID == newReceivingDetail.ItemID).Sum(po => po.Quantity);
                var totalQtyRcv = receivingDetails.Where(rcv => rcv.ItemID == newReceivingDetail.ItemID).Sum(rcv => rcv.QtyReceived);
                if (totalQtyPO - totalQtyRcv - newReceivingDetail.QtyReceived < 0)
                {
                    throw new Exception(String.Format("Qty for item {0} exceeds Qty in Purchase Order. Item:{1}, QtyPO:{2}, QtyRcv:{3}", newReceivingDetail.ItemCode, newReceivingDetail.ItemCode, Convert.ToInt32(totalQtyPO), totalQtyRcv + newReceivingDetail.QtyReceived));
                }
                else if (totalQtyPO - totalQtyRcv - newReceivingDetail.QtyReceived > 0)
                {
                    isFullReceived = false;
                }

                var receiveDetail = new ReceivingDetail
                {
                    ItemID          = newReceivingDetail.ItemID,
                    UnitName        = newReceivingDetail.UnitName,
                    QtyReceived     = newReceivingDetail.QtyReceived,
                    Notes           = newReceivingDetail.Notes,
                    ReceivingHeader = receiveHeader
                };
                context.ReceivingDetails.InsertOnSubmit(receiveDetail);
            }

            if (id == 0)
            {
                context.ReceivingHeaders.InsertOnSubmit(receiveHeader);
            }

            order.IsFullReceived  = isFullReceived;
            order.StatusReceiving = isFullReceived ? "F" : "P";


            //var receiveDetails = new List<ReceivingDetailViewModel>();
            //foreach (var item in query)
            //    receiveDetails.Add(new ReceivingDetailViewModel { ItemID = item.ItemID, QtyReceived = item.QtyReceived });

            //foreach (var lineDetail in detail)
            //    receiveDetails.Add(lineDetail);


            //var sumReceiveUnit = detail.GroupBy(x => x.ItemID).Select(y => new { item = y.Key, qty = receiveDetails.Where(x => x.ItemID == y.Key).Sum(x => x.QtyReceived) });


            //var sumPOUnit = context.PurchaseOrderHeaders.Where(x => x.ID == poID).SelectMany(x => x.PurchaseOrderDetails).
            //                                                GroupBy(x => x.ItemID).Select(z => new { item = z.Key, qty = z.Sum(p => p.Quantity) });

            //bool isAllqtySame = true;
            //bool isItemCountSame = true;

            //foreach (var itemReceive in sumReceiveUnit)
            //{
            //    var itemPO = sumPOUnit.SingleOrDefault(x => x.item == itemReceive.item);
            //    if (itemPO.qty != itemReceive.qty)
            //        isAllqtySame = false;
            //}

            //if (sumReceiveUnit.Count() != sumPOUnit.Count())
            //    isItemCountSame = false;

            //PurchaseOrderHeader purchaseOrderHeader = context.PurchaseOrderHeaders.SingleOrDefault(x => x.ID == poID);
            //if (purchaseOrderHeader != null)
            //{
            //    if (isAllqtySame && isItemCountSame)
            //    {
            //        var allReceiving = context.ReceivingHeaders.Where(x => x.PurchaseOrderID == poID && x.Status == "O");
            //        foreach (var item in allReceiving)
            //        {
            //            ReceivingHeader headerReceive = context.ReceivingHeaders.SingleOrDefault(x => x.ID == item.ID);
            //            headerReceive.Status = "F";
            //            EntityHelper.SetAuditFieldsForUpdate(headerReceive, CurrentUserName);
            //        }
            //        purchaseOrderHeader.IsFullReceived = true;
            //        purchaseOrderHeader.StatusReceiving = "F";
            //        receiveHeader.Status = "F";
            //        EntityHelper.SetAuditFieldsForUpdate(purchaseOrderHeader, CurrentUserName);
            //    }
            //    else
            //    {
            //        purchaseOrderHeader.IsFullReceived = false;
            //        purchaseOrderHeader.StatusReceiving = "P";
            //    }
            //}
            //#endregion

            context.SubmitChanges();

            return(receiveHeader.DocumentNo);
        }