public IHttpActionResult CreateInstructionByCustomer([FromUri] string reference, [FromUri] string invoiceType, [FromUri] string description)
        {
            var instruction = new ChargingItemDetail
            {
                CreateBy            = _userName,
                Description         = description,
                CreateDate          = DateTime.Now,
                OriginalDescription = description,
                HandlingStatus      = "Draft",
                IsInstruction       = true,
                Status = "TBD"
            };

            if (invoiceType == FBAOrderType.MasterOrder)
            {
                var masterOrderInDb = _context.FBAMasterOrders.SingleOrDefault(x => x.Container == reference);
                instruction.FBAMasterOrder = masterOrderInDb;
            }
            else
            {
                var shipOrderInDb = _context.FBAShipOrders.SingleOrDefault(x => x.ShipOrderNumber == reference);
                instruction.FBAShipOrder = shipOrderInDb;
            }

            _context.ChargingItemDetails.Add(instruction);
            _context.SaveChanges();

            var id = _context.ChargingItemDetails.OrderByDescending(x => x.Id).First().Id;

            return(Created(Request.RequestUri, new { Id = id, Description = description, HandlingStatus = "Draft" }));
        }
        // POST /api/fba/FBAInvoiceDetail/?reference={reference}&invoiceType={invoiceType}&description={description}&isChargingItem={isChargingItem}
        public IHttpActionResult CreateChargingItemByAccounting([FromUri] string reference, [FromUri] string invoiceType, [FromUri] string description, [FromUri] bool isChargingItem)
        {
            var chargingItem = new ChargingItemDetail
            {
                CreateBy            = _userName,
                Description         = description,
                CreateDate          = DateTime.Now,
                OriginalDescription = description,
                HandlingStatus      = "N/A",
                IsCharging          = isChargingItem
            };

            chargingItem.Status = isChargingItem ? FBAStatus.WaitingForCharging : FBAStatus.NoNeedForCharging;

            if (invoiceType == FBAOrderType.MasterOrder)
            {
                var masterOrderInDb = _context.FBAMasterOrders.SingleOrDefault(x => x.Container == reference);
                chargingItem.FBAMasterOrder = masterOrderInDb;
            }
            else
            {
                var shipOrderInDb = _context.FBAShipOrders.SingleOrDefault(x => x.ShipOrderNumber == reference);
                chargingItem.FBAShipOrder = shipOrderInDb;
            }

            _context.ChargingItemDetails.Add(chargingItem);
            _context.SaveChanges();

            var id = _context.ChargingItemDetails.OrderByDescending(x => x.Id).First().Id;

            return(Created(Request.RequestUri, new { Id = id, Description = description, HandlingStatus = "Draft" }));
        }
        public async Task <IHttpActionResult> CreateNewCommentFromWarehouse([FromUri] int shipOrderId, [FromUri] string comment, [FromUri] string operation)
        {
            if (operation == "AddNewComment")
            {
                var shipOrderInDb = _context.FBAShipOrders.Find(shipOrderId);
                shipOrderInDb.Status = FBAStatus.Pending;
                var newComment = new ChargingItemDetail
                {
                    CreateBy       = _userName,
                    Comment        = comment,
                    CreateDate     = DateTime.Now,
                    Description    = "Extral comment from warehouse",
                    HandlingStatus = FBAStatus.Pending,
                    Status         = FBAStatus.Unhandled,
                    FBAShipOrder   = shipOrderInDb
                };

                _context.ChargingItemDetails.Add(newComment);
                _context.SaveChanges();

                var result = Mapper.Map <ChargingItemDetail, ChargingItemDetailDto>(_context.ChargingItemDetails.Include(x => x.FBAShipOrder).OrderByDescending(x => x.Id).First());

                await _logger.AddCreatedLogAsync <ChargingItemDetail>(null, result, "Added a new WO comment from warehouse", null, OperationLevel.Normal);

                return(Created(Request.RequestUri + "/" + result.Id, result));
            }
            else
            {
                return(Ok("Invaild operation."));
            }
        }
        public async Task CreateOutboundOrdersByAgentRequestV1(string customerCode, FBAInboundOrder order, string requestId)
        {
            // 添加Request日志
            var logger = new Logger(_context, order.Agency);

            var index = 1;

            foreach (var j in order.FBAJobs)
            {
                var shipOrder = new FBAShipOrder();

                shipOrder.ShipOrderNumber = order.Container + "-" + index + "-" + j.ShipmentId;
                shipOrder.CreateBy        = order.Agency;
                shipOrder.Carrier         = "Unknow";
                shipOrder.CustomerCode    = customerCode;
                shipOrder.Agency          = order.Agency;
                shipOrder.OrderType       = "Standard";
                shipOrder.ETSTimeRange    = "Unknow";
                shipOrder.Destination     = j.WarehouseCode;
                shipOrder.SubCustomer     = j.Subcustomer;
                shipOrder.Instruction     = "This order is generated by api automatically. Some fields are pending. Please fill out them once all information is clear.";
                shipOrder.OperationLog    = "Created by agency via API";

                _context.FBAShipOrders.Add(shipOrder);

                var instruction = new ChargingItemDetail();

                instruction.Description         = "To CSR: Please pick the following quantity of sku in system: " + j.Quantity + j.PackageType + " of shipmentID: " + j.ShipmentId + ", amz Ref Id: " + j.AmzRefId + ", warehouse code: " + j.WarehouseCode;
                instruction.HandlingStatus      = "N/A";
                instruction.Status              = FBAStatus.NoNeedForCharging;
                instruction.IsCharging          = false;
                instruction.IsInstruction       = true;
                instruction.IsOperation         = false;
                instruction.OriginalDescription = instruction.Description;
                instruction.FBAShipOrder        = shipOrder;

                _context.ChargingItemDetails.Add(instruction);

                await logger.AddCreatedLogAsync <FBAShipOrder>(null, Mapper.Map <FBAShipOrder, FBAShipOrderDto>(shipOrder), "Created by agency from api.", null, OperationLevel.Mediunm);

                var logInDb = _context.OperationLogs.OrderByDescending(x => x.Id).First();

                logInDb.RequestId = requestId;

                index += 1;
            }

            _context.SaveChanges();
        }
        public IHttpActionResult CreateInstructionByModel([FromBody] Instruction obj)
        {
            var detail = new ChargingItemDetail();

            if (obj.OrderType == FBAInvoiceType.MasterOrder)
            {
                var masterOrderInDb = _context.FBAMasterOrders.SingleOrDefault(x => x.Container == obj.Reference);

                if (masterOrderInDb.InvoiceStatus == FBAStatus.Closed || masterOrderInDb.InvoiceStatus == FBAStatus.Generated)
                {
                    throw new Exception("Cannot add any items in a closed order");
                }

                var newDetail = new ChargingItemDetail
                {
                    Status              = FBAStatus.Unhandled,
                    HandlingStatus      = obj.IsInstruction || obj.IsOperation ? FBAStatus.New : FBAStatus.Na,
                    CreateBy            = _userName,
                    OriginalDescription = obj.Description,
                    IsOperation         = obj.IsOperation,
                    IsCharging          = obj.IsChargingItem,
                    IsInstruction       = obj.IsInstruction,
                    VisibleToAgent      = obj.VisibleToAgent,
                    CreateDate          = DateTime.Now,
                    Description         = obj.Description,
                    FBAMasterOrder      = masterOrderInDb
                };

                if (masterOrderInDb.Status == FBAStatus.Pending)
                {
                    masterOrderInDb.Status = FBAStatus.Updated;
                }

                if (obj.IsChargingItem)
                {
                    newDetail.Status = FBAStatus.WaitingForCharging;
                }
                else
                {
                    newDetail.Status = FBAStatus.NoNeedForCharging;
                }

                detail = newDetail;
            }
            else if (obj.OrderType == FBAInvoiceType.ShipOrder)
            {
                var shipOrderInDb = _context.FBAShipOrders.SingleOrDefault(x => x.ShipOrderNumber == obj.Reference);

                if (shipOrderInDb.InvoiceStatus == FBAStatus.Generated || shipOrderInDb.InvoiceStatus == FBAStatus.Closed)
                {
                    throw new Exception("Cannot add any items in a closed order");
                }

                var newDetail = new ChargingItemDetail
                {
                    Status              = FBAStatus.Unhandled,
                    HandlingStatus      = obj.IsInstruction || obj.IsOperation ? FBAStatus.New : FBAStatus.Na,
                    CreateBy            = _userName,
                    CreateDate          = DateTime.Now,
                    OriginalDescription = obj.Description,
                    IsOperation         = obj.IsOperation,
                    IsCharging          = obj.IsChargingItem,
                    IsInstruction       = obj.IsInstruction,
                    VisibleToAgent      = obj.VisibleToAgent,
                    Description         = obj.Description,
                    FBAShipOrder        = shipOrderInDb
                };

                if (shipOrderInDb.Status == FBAStatus.Pending)
                {
                    shipOrderInDb.Status = FBAStatus.Updated;
                }

                if (obj.IsChargingItem)
                {
                    newDetail.Status = FBAStatus.WaitingForCharging;
                }
                else
                {
                    newDetail.Status = FBAStatus.NoNeedForCharging;
                }

                detail = newDetail;
            }

            _context.ChargingItemDetails.Add(detail);
            _context.SaveChanges();

            return(Created(Request.RequestUri + "/", Mapper.Map <ChargingItemDetail, ChargingItemDetailDto>(detail)));
        }
        public IHttpActionResult CreateChargingItemRef([FromUri] string reference, [FromUri] string invoiceType, [FromUri] string description, [FromUri] bool isChargingItem, [FromUri] bool isInstruction, [FromBody] ObjectBody obj)
        {
            var detail = new ChargingItemDetail();

            if (invoiceType == FBAInvoiceType.MasterOrder)
            {
                var masterOrderInDb = _context.FBAMasterOrders.SingleOrDefault(x => x.Container == reference);

                var newDetail = new ChargingItemDetail {
                    Status              = FBAStatus.Unhandled,
                    HandlingStatus      = isInstruction == true ? FBAStatus.New : FBAStatus.Na,
                    CreateBy            = _userName,
                    OriginalDescription = description,
                    CreateDate          = DateTime.Now,
                    Description         = description,
                    IsCharging          = isChargingItem,
                    FBAMasterOrder      = masterOrderInDb
                };

                if (masterOrderInDb.Status == FBAStatus.Pending)
                {
                    masterOrderInDb.Status = FBAStatus.Updated;
                }

                newDetail.Status = isChargingItem ? FBAStatus.WaitingForCharging : FBAStatus.NoNeedForCharging;

                detail = newDetail;
            }
            else if (invoiceType == FBAInvoiceType.ShipOrder)
            {
                var shipOrderInDb = _context.FBAShipOrders.SingleOrDefault(x => x.ShipOrderNumber == reference);

                var newDetail = new ChargingItemDetail
                {
                    Status              = FBAStatus.Unhandled,
                    HandlingStatus      = isInstruction == true ? FBAStatus.New : FBAStatus.Na,
                    CreateBy            = _userName,
                    CreateDate          = DateTime.Now,
                    OriginalDescription = description,
                    Description         = description,
                    IsCharging          = isChargingItem,
                    FBAShipOrder        = shipOrderInDb
                };

                if (shipOrderInDb.Status == FBAStatus.Pending)
                {
                    shipOrderInDb.Status = FBAStatus.Updated;
                }

                newDetail.Status = isChargingItem ? FBAStatus.WaitingForCharging : FBAStatus.NoNeedForCharging;
                detail           = newDetail;
            }

            if (obj != null)
            {
                detail.HandlingStatus = obj.Content;
            }

            _context.ChargingItemDetails.Add(detail);
            _context.SaveChanges();

            return(Created(Request.RequestUri + "/", Mapper.Map <ChargingItemDetail, ChargingItemDetailDto>(detail)));
        }
        public async Task CreateInboundOrderByAgentRequestV1(UpperVendor customer, string customerCode, FBAInboundOrder order, string requestId)
        {
            // 建立主单
            var newMasterOrder = new FBAMasterOrder();

            newMasterOrder.GrandNumber       = "N/A";
            newMasterOrder.Agency            = order.Agency;
            newMasterOrder.Container         = order.Container;
            newMasterOrder.CreatedBy         = order.Agency;
            newMasterOrder.SubCustomer       = order.Subcustomer;
            newMasterOrder.StorageType       = "SEE INSTRUCTION";
            newMasterOrder.Status            = FBAStatus.Draft;
            newMasterOrder.UnloadingType     = "DROP-OFF";
            newMasterOrder.InboundType       = "FCL";
            newMasterOrder.Palletizing       = "<=80";
            newMasterOrder.TotalCBM          = order.FBAJobs.Sum(x => x.CBM);
            newMasterOrder.TotalCtns         = order.FBAJobs.Sum(x => x.Quantity);
            newMasterOrder.OriginalPlts      = order.FBAJobs.Sum(x => x.PalletQuantity);
            newMasterOrder.CustomerCode      = customerCode;
            newMasterOrder.WarehouseLocation = order.WarehouseLocation == "" ? "W0" : order.WarehouseLocation;
            newMasterOrder.Customer          = customer;
            newMasterOrder.PortOfLoading     = order.PortOfLoading;
            newMasterOrder.ETAPort           = order.ETADate;
            newMasterOrder.ETA             = order.ETADate;
            newMasterOrder.PlaceOfDelivery = order.DeliveryPort;
            newMasterOrder.Vessel          = order.Vessel;
            newMasterOrder.Carrier         = order.Carrier;
            newMasterOrder.ContainerSize   = order.ContainerSize;
            newMasterOrder.SealNumber      = order.SealNumber;
            newMasterOrder.Comment         = "ETL DATE: " + order.ETLDate;
            newMasterOrder.UpdateLog       = "Created by agency via API";

            _context.FBAMasterOrders.Add(newMasterOrder);

            foreach (var j in order.FBAJobs)
            {
                var orderDetail = new FBAOrderDetail();
                orderDetail.Container      = order.Container;
                orderDetail.GrandNumber    = "N/A";
                orderDetail.GrossWeight    = j.GrossWeight;
                orderDetail.CBM            = j.CBM;
                orderDetail.Quantity       = j.Quantity;
                orderDetail.ShipmentId     = j.ShipmentId;
                orderDetail.AmzRefId       = j.AmzRefId;
                orderDetail.WarehouseCode  = j.WarehouseCode;
                orderDetail.Remark         = "包装:" + j.PackageType + ";产品类型:" + j.ProductType + ";打托数量:" + j.PalletQuantity;
                orderDetail.FBAMasterOrder = newMasterOrder;
                _context.FBAOrderDetails.Add(orderDetail);
            }

            var instruction = new ChargingItemDetail();

            instruction.Description         = "To CSR: This inbound order is created by an agency from api. If there is no further customer's instructions below, please contact customer to do a further confirmation.";
            instruction.HandlingStatus      = "N/A";
            instruction.Status              = FBAStatus.NoNeedForCharging;
            instruction.IsCharging          = false;
            instruction.IsInstruction       = true;
            instruction.IsOperation         = false;
            instruction.OriginalDescription = instruction.Description;
            instruction.FBAMasterOrder      = newMasterOrder;
            _context.ChargingItemDetails.Add(instruction);

            if (order.Instructions != null)
            {
                foreach (var i in order.Instructions)
                {
                    var customerInstruction = new ChargingItemDetail();

                    customerInstruction.Description         = i;
                    customerInstruction.HandlingStatus      = "N/A";
                    customerInstruction.Status              = FBAStatus.TBD;
                    customerInstruction.IsCharging          = false;
                    customerInstruction.IsInstruction       = true;
                    customerInstruction.IsOperation         = false;
                    customerInstruction.OriginalDescription = instruction.Description;
                    customerInstruction.FBAMasterOrder      = newMasterOrder;
                    _context.ChargingItemDetails.Add(customerInstruction);
                }
            }

            // 添加Request日志
            var logger = new Logger(_context, order.Agency);

            await logger.AddCreatedLogAsync <FBAMasterOrder>(null, Mapper.Map <FBAMasterOrder, FBAMasterOrderDto>(newMasterOrder), "Created by agency from api.", null, OperationLevel.Mediunm);

            var logInDb = _context.OperationLogs.OrderByDescending(x => x.Id).First();

            logInDb.RequestId = requestId;

            _context.SaveChanges();
        }