Esempio n. 1
0
        private Error doSplit(CompanyModel company, SplitPurchaseModel model, UserModel user)
        {
            var error = new Error();

            string lgs;

            // Proceed to do the split
            for (int i = 0; i < podList.Count() && !error.IsError; i++)
            {
                var podListItem = podList[i];
                var splitItem   = model.SplitItems[i];

                var updatedItem = updatedDetails.Items
                                  .Where(ud => ud.OriginalRowId == podListItem.Id)
                                  .FirstOrDefault();

                PurchaseOrderDetailModel pod = new PurchaseOrderDetailModel {
                    CompanyId             = company.Id,
                    PurchaseOrderHeaderId = 0,
                    LineNumber            = 0,
                    ProductId             = podListItem.ProductId.Value,
                    ProductDescription    = podListItem.ProductDescription,
                    UnitPriceExTax        = podListItem.UnitPriceExTax,
                    TaxCodeId             = podListItem.TaxCodeId,
                    DiscountPercent       = podListItem.DiscountPercent,
                    //LineStatus = podListItem.LineStatus,
                    //IsReceived = podListItem.IsReceived
                };

                if (splitItem.NewOrderQty > 0)
                {
                    // Add a new line to a new order
                    if (newPoh == null)
                    {
                        // Create the new order first
                        newPoh             = mapToModel(updatedPoh);
                        newPoh.Id          = 0;
                        newPoh.OrderNumber = LookupService.GetNextSequenceNumber(company, SequenceNumberType.PurchaseOrderNumber, 0, false);
                        error = InsertOrUpdatePurchaseOrderHeader(newPoh, user, "");
                    }
                    if (!error.IsError)
                    {
                        pod.PurchaseOrderHeaderId = newPoh.Id;
                        pod.OrderQty   = splitItem.NewOrderQty;
                        pod.LineNumber = db.GetNextPurchaseOrderDetailLineNumber(pod.PurchaseOrderHeaderId, false);
                        InsertOrUpdatePurchaseOrderDetail(pod, user, "");

                        // Move the # of split item allocations across to the new purchase order line
                        pod.OrderQty -= splitItem.NewOrderQty;
                        var allocList1 = updatedAllocations.Where(a => a.PurchaseLineId == pod.Id)
                                         .ToList();
                        AllocationService.AllocateOnPurchaseOrderSplit(updatedItem, allocList1, newPoh, pod, splitItem.NewOrderQty, user);
                    }
                }

                if (!error.IsError && splitItem.TargetOrderQty > 0)
                {
                    // Add a new line to the target order
                    pod.Id = 0;
                    pod.PurchaseOrderHeaderId = splitItem.TargetOrderId;
                    pod.OrderQty   = splitItem.TargetOrderQty;
                    pod.LineNumber = db.GetNextPurchaseOrderDetailLineNumber(pod.PurchaseOrderHeaderId, false);

                    error = InsertOrUpdatePurchaseOrderDetail(pod, user, "");

                    // Move the # of split item allocations across to the new purchase order line
                    pod.OrderQty -= splitItem.TargetOrderQty;
                    var allocList2 = updatedAllocations.Where(a => a.PurchaseLineId == pod.Id)
                                     .ToList();
                    AllocationService.AllocateOnPurchaseOrderSplit(updatedItem, allocList2, newPoh, pod, splitItem.TargetOrderQty, user);
                }

                if (!error.IsError)
                {
                    InsertOrUpdatePurchaseOrderDetail(updatedItem, user, LockPurchaseOrderDetail(updatedItem));
                }
            }

            if (!error.IsError)
            {
                // Obsolete the existing order
                origPoh.POStatus = LookupService.FindPurchaseOrderHeaderStatusByValueModel(PurchaseOrderStatus.Superceded).Id;
                lgs   = LockPurchaseOrderHeader(origPoh);
                error = InsertOrUpdatePurchaseOrderHeader(origPoh, user, lgs);
            }
            return(error);
        }