Пример #1
0
        public byte[] GetCMRPDF(int dispatchId, bool ifSendEmail = false)
        {
            if (UserHelper.IsAuthorize(new List <int> {
                (int)UserType.SuperAdmin, (int)UserType.Admin, (int)UserType.Client
            }))
            {
                try
                {
                    Dispatch dispatchToPDF = _context.Dispatches.FirstOrDefault(d => d.Id == dispatchId && d.Deleted_At == null);
                    if (dispatchToPDF != null)
                    {
                        List <Dispatches_Positions> dispatchPositionsFromDB = _context.Dispatches_Positions.Where(d => d.Dispatch_Id == dispatchId && d.Deleted_At == null).ToList();
                        int            CMRId            = Convert.ToInt32(dispatchToPDF.CMR_Id);
                        CMR_Dispatches CMRDispatchToPDF = _context.CMR_Dispatches.FirstOrDefault(c => c.Id == CMRId);
                        List <DispatchPositionsDispatchInfo> listOfDispatchPositionsToPDF = new List <DispatchPositionsDispatchInfo>();
                        foreach (var item in dispatchPositionsFromDB)
                        {
                            Orders_Positions orderPosition = _context.Orders_Positions.FirstOrDefault(o => o.Id == item.Order_Position_Id && o.Deleted_At == null);
                            if (orderPosition != null)
                            {
                                DispatchPositionsDispatchInfo dispatchPosition = new DispatchPositionsDispatchInfo();
                                dispatchPosition.Amount       = item.Amount;
                                dispatchPosition.Weight_Gross = item.Weight_Gross;
                                dispatchPosition.Name         = orderPosition.Name;
                                listOfDispatchPositionsToPDF.Add(dispatchPosition);
                            }
                            else
                            {
                                throw new Exception("Not found orderPosition for dispatchPosition");
                            }
                        }

                        byte[] result = _pdfManager.GenerateCMR(dispatchToPDF, listOfDispatchPositionsToPDF, CMRDispatchToPDF);
                        if (ifSendEmail)
                        {
                            _pdfManager.SendEmail("CMR" + dispatchToPDF.Dispatch_Number, result);
                        }
                        return(result);
                    }
                    else
                    {
                        throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Dispatch not found"));
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.ToString());
                }
            }

            else
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "User don't have acces to this method"));
            }
        }
Пример #2
0
        public DispatchDetailsDispatchInfo GetDispatchDetails(int dispatchId)
        {
            if (UserHelper.IsAuthorize(new List <int> {
                (int)UserType.SuperAdmin, (int)UserType.Admin, (int)UserType.Client
            }))
            {
                DispatchDetailsDispatchInfo result = new DispatchDetailsDispatchInfo();
                List <DispatchOrderList>    listOfDispatchOrders = new List <DispatchOrderList>();

                CarrierDispatch  carrier     = new CarrierDispatch();
                ReceiverDispatch receiver    = new ReceiverDispatch();
                CMRDispatch      cmrToResult = new CMRDispatch();

                try
                {
                    Dispatch dispatchFromDB = _context.Dispatches.FirstOrDefault(d => d.Id == dispatchId && d.Deleted_At == null);
                    if (dispatchFromDB != null)
                    {
                        List <int> listOfDeliveryIdsForDispatch = _dispatchManager.GetListOfDeliveriesIdsForDispatch(dispatchFromDB);
                        foreach (var deliveryId in listOfDeliveryIdsForDispatch)
                        {
                            DispatchOrderList dispatchOrderList = new DispatchOrderList();
                            Delivery          delivery          = _context.Deliveries.FirstOrDefault(d => d.Id == deliveryId && d.Deleted_At == null);
                            //if (delivery != null)
                            //{
                            Order order = _context.Orders.FirstOrDefault(o => o.Id == delivery.Order_Id && o.Deleted_At == null);
                            //}

                            Dispatch dispatch = _context.Dispatches.FirstOrDefault(d => d.Id == dispatchId && d.Deleted_At == null);
                            if (dispatch.CMR_Id != null)
                            {
                                int            cmrId       = Convert.ToInt32(dispatch.CMR_Id);
                                CMR_Dispatches cmrDispatch = _context.CMR_Dispatches.FirstOrDefault(c => c.Id == cmrId && c.Deleted_At == null);
                                cmrToResult.Commodity_Type      = cmrDispatch.Commodity_Type;
                                cmrToResult.Destination         = cmrDispatch.Destination;
                                cmrToResult.Sender_Address      = cmrDispatch.Sender_Address;
                                cmrToResult.Sender_Email        = cmrDispatch.Sender_Email;
                                cmrToResult.Sender_Name         = cmrDispatch.Sender_Name;
                                cmrToResult.Sender_PrefixVat_Id = cmrDispatch.Sender_PrefixVat_Id;
                                cmrToResult.Sender_VAT_Id       = cmrDispatch.Sender_VAT_Id;
                            }

                            List <DispatchPositionsDispatchInfo> listOfDispatchPositions = new List <DispatchPositionsDispatchInfo>();

                            List <Orders_Positions> listOfOrdersPositions = _context.Orders_Positions.Where(o => o.Order_id == order.Id && o.Deleted_At == null).ToList();
                            foreach (var orderPosition in listOfOrdersPositions)
                            {
                                List <Dispatches_Positions> dispatchPositionsFromDB = _context.Dispatches_Positions.Where(d => d.Dispatch_Id == dispatchId && d.Order_Position_Id == orderPosition.Id && d.Deleted_At == null).ToList();
                                foreach (var dispatchPositionFromDB in dispatchPositionsFromDB)
                                {
                                    DispatchPositionsDispatchInfo dispatchPosition = new DispatchPositionsDispatchInfo();
                                    dispatchPosition.Id           = (int)dispatchPositionFromDB.Order_Position_Id;
                                    dispatchPosition.Amount       = dispatchPositionFromDB.Amount;
                                    dispatchPosition.Weight_Gross = dispatchPositionFromDB.Weight_Gross;
                                    dispatchPosition.Name         = _context.Orders_Positions.FirstOrDefault(o => o.Id == dispatchPositionFromDB.Order_Position_Id && o.Deleted_At == null).Name;
                                    listOfDispatchPositions.Add(dispatchPosition);
                                }
                            }
                            dispatchOrderList.Delivery_Id             = delivery.Id;
                            dispatchOrderList.Delivery_Number         = delivery.Delivery_Number;
                            dispatchOrderList.Order_Number            = order.Order_Number;
                            dispatchOrderList.ContainerId             = order.Container_Id;
                            dispatchOrderList.ListOfDispatchPositions = listOfDispatchPositions;
                            listOfDispatchOrders.Add(dispatchOrderList);
                        }

                        carrier.Carrier_Address        = dispatchFromDB.Carrier_Address;
                        carrier.Carrier_Email          = dispatchFromDB.Carrier_Email;
                        carrier.Carrier_PrefixVat_Id   = dispatchFromDB.Carrier_PrefixVat_Id;
                        carrier.Carrier_VAT_Id         = dispatchFromDB.Carrier_VAT_Id;
                        carrier.Carrier_Name           = dispatchFromDB.Carrier_Name;
                        carrier.Carrier_PhoneNumber    = dispatchFromDB.Carrier_PhoneNumber;
                        receiver.Receiver_Address      = dispatchFromDB.Receiver_Address;
                        receiver.Receiver_Email        = dispatchFromDB.Receiver_Email;
                        receiver.Receiver_Name         = dispatchFromDB.Receiver_Name;
                        receiver.Receiver_PrefixVat_Id = dispatchFromDB.Receiver_PrefixVat_Id;
                        receiver.Receiver_VAT_Id       = dispatchFromDB.Receiver_VAT_Id;
                        receiver.Receiver_PhoneNumber  = dispatchFromDB.Receiver_PhoneNumber;
                        result.Dispatch_Number         = dispatchFromDB.Dispatch_Number;
                        result.Carrier = carrier;
                        result.Id      = dispatchFromDB.Id;
                        result.ListOfDispatchOrders = listOfDispatchOrders;
                        result.Receiver             = receiver;
                        result.isCMR         = dispatchFromDB.If_CMR;
                        result.CmrDispatch   = cmrToResult;
                        result.Duty_Doc_Id   = dispatchFromDB.Duty_Doc_Id;
                        result.Car_Id        = dispatchFromDB.Car_Id;
                        result.Creation_Date = dispatchFromDB.Creation_Date == null ? string.Empty : dispatchFromDB.Creation_Date.Value.ToString("dd-MM-yyyy");
                    }
                    return(result);
                }
                catch (Exception ex)
                {
                    throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message));
                }
            }
            else
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "User don't have acces to this method"));
            }
        }