OrderReportContainer MapOrder(MainOrder mainorder)
        {
            var docData = new OrderReportContainer();

            var gross =(mainorder.ItemSummary.Sum(x => (x.Qty*x.Value).GetTruncatedValue() + (x.Qty*x.VatValue).GetTruncatedValue())).GetTotalGross();
            var docHeader = new OrderHeader()
            {
                AmountPaid = mainorder.PaidAmount,
                Comment = mainorder.Note,
                DatePrinted = DateTime.Now,
                DateRequired = mainorder.DateRequired,
                DocumentIssuerUserName = mainorder.DocumentIssuerUser.Username,
                DocumentIssuerCCName = mainorder.DocumentIssuerCostCentre.Name,
                DocumentReference = mainorder.DocumentReference,
                IssuedOnBehalfOfName = mainorder.IssuedOnBehalfOf != null ?mainorder.IssuedOnBehalfOf.Name:"",
                OrderTypeStr = mainorder.OrderType.ToString(),
                OutstandingAmount = mainorder.OutstandingAmount >0?mainorder.OutstandingAmount:0,
                ParentId = mainorder.ParentId,
                SaleDiscount = mainorder.SaleDiscount,
                SalesmanName = mainorder.DocumentRecipientCostCentre.Name,
                ShipToAddress = mainorder.ShipToAddress,
                StatusStr = mainorder.Status.ToString(),
                TotalGross =gross, //mainorder.TotalGross,
                TotalNet = (gross-mainorder.SaleDiscount).GetTotalGross(),//mainorder.TotalNet,
                TotalProductDiscount = mainorder.TotalDiscount,
                TotalSaleDiscount = mainorder.TotalDiscount,
                TotalVat = mainorder.TotalVat,
                ExternalDocumentReference = mainorder.ExternalDocumentReference,
                
            };

            if (mainorder.IssuedOnBehalfOf is Outlet)
                docHeader.RouteName = ((Outlet) mainorder.IssuedOnBehalfOf).Route.Name;

            docHeader.DocumentIssuerDetails = "Generated by: " + docHeader.DocumentIssuerUserName + " ; Cost centre: " +
                                             docHeader.DocumentIssuerCCName + "; Date: " +
                                             docHeader.DatePrinted.ToShortDateString();
            docData.DocumentHeader = docHeader;
            int i = 0;
            foreach (var item in mainorder.ItemSummary)
            {
                var li = new OrderReportLineItem()
                             {
                                 ApprovedQuantity = item.ApprovedQuantity,
                                 BackOrderQuantity = item.BackOrderQuantity,
                                 DiscountType =
                                     StringUtils.BreakUpperCB(item.Product.GetType().ToString().Split('.').Last()),
                                 GrossAmount = item.TotalGross,
                                 LineItemTypeStr = item.LineItemType.ToString(),
                                 LostSaleQuantity = item.LostSaleQuantity,
                                 ProductName = item.Product.Description,
                                 ProductCode = item.Product.ProductCode,
                                 Qty = item.Qty,
                                 RowNumber = i + 1,
                                 TotalNet = item.TotalNet,
                                 TotalVat = item.TotalVat,
                                 UnitDiscount = item.ProductDiscount,
                                 UnitPrice = item.Value,
                                 UnitVat = item.VatValue,
                             };
                docData.LineItems.Add(li);
                i++;
            }
            foreach (var payment in mainorder.GetPayments)
            {
                string bankinfo = "";
               
                using (StructureMap.IContainer c = NestedContainer)
                {
                    if (payment.PaymentModeUsed == PaymentMode.Cheque)
                    {
                        var bank = Using<IBankRepository>(c).GetByCode(payment.Bank);
                        var bankbranch = Using<IBankBranchRepository>(c).GetByCode(payment.BankBranch);
                        
                        if (bank != null)
                            bankinfo = bank.Name + "(" + bank.Code + ")" ;
                         if (bankbranch != null)
                             bankinfo +=" -- "+ bankbranch.Name + " (" + bankbranch.Code + ")";
                        
                    }
                }
               
                docData.PaymentInfoList.Add(new OrderReportPaymentInfo()
                {
                    Amount = payment.ConfirmedAmount,
                    PaymentModeUsed = payment.PaymentModeUsed.ToString(),
                    PaymentRefId =payment.PaymentRefId,
                    BankInfo = bankinfo,
                    ChequeDueDate= (payment.PaymentModeUsed == PaymentMode.Cheque && payment.DueDate.HasValue)?payment.DueDate.Value.ToShortDateString(): "" 
                    
                });
            }

            return docData;
        }
        OrderReportContainer MapDispatchNote(DispatchNote dispatchNote)
        {
            using (StructureMap.IContainer c = NestedContainer)
            {
                OrderReportContainer docData = new OrderReportContainer();
                MainOrder mainOrder = Using<IMainOrderRepository>(c).GetById(dispatchNote.OrderId);
                OrderHeader docHeader = new OrderHeader()
                                               {
                                                   DatePrinted = DateTime.Now,
                                                   DateRequired = dispatchNote.StartDate,
                                                   DocumentIssuerUserName = dispatchNote.DocumentIssuerUser.Username,
                                                   DocumentIssuerCCName = dispatchNote.DocumentIssuerCostCentre.Name,
                                                   DocumentReference = dispatchNote.DocumentReference,
                                                   RouteName = ((Outlet) mainOrder.IssuedOnBehalfOf).Route.Name,
                                                   SalesmanName = dispatchNote.DocumentRecipientCostCentre.Name,
                                                   StatusStr = dispatchNote.Status.ToString(),
                                                   TotalNet = dispatchNote.TotalNet,
                                                  
                                               };
                docHeader.DocumentIssuerDetails = "Generated by: " + docHeader.DocumentIssuerUserName +
                                                  " ; Cost centre: " +
                                                  docHeader.DocumentIssuerCCName + "; Date: " +
                                                  docHeader.DatePrinted.ToShortDateString();
                docData.DocumentHeader = docHeader;
                int i = 0;

                foreach (var item in dispatchNote.LineItems)
                {
                    var li = new OrderReportLineItem()
                                                 {
                                                     DiscountType =
                                                         (item.DiscountType == DiscountType.None
                                                              ? StringUtils.BreakUpperCB(
                                                                  item.Product.GetType().ToString().Split('.').Last())
                                                              : StringUtils.BreakUpperCB(item.DiscountType.ToString())),

                                                     LineItemTypeStr = item.LineItemType.ToString(),
                                                     ProductName = item.Product.Description,
                                                     ProductCode = item.Product.ProductCode,
                                                     Qty = item.Qty,
                                                     RowNumber = i + 1,
                                                     TotalVat = item.LineItemVatTotal,
                                                     UnitDiscount = item.ProductDiscount,
                                                     UnitPrice = item.Value,
                                                     UnitVat = item.LineItemVatValue,
                                                     TotalNet = item.LineItemTotal,
                                                     GrossAmount = item.LineItemTotal + item.LineItemVatTotal,
                                                 };
                    docData.LineItems.Add(li);
                    i++;
                }
                return docData;
            }
        }