public IHttpActionResult GetInstantInventory(string appKey, string customerCode, string requestId, string version, string sign)
        {
            var customerInDb = _context.UpperVendors.SingleOrDefault(x => x.CustomerCode == customerCode);
            var jsonResult   = _validator.ValidateSign(appKey, customerInDb, requestId, version, sign);

            if (jsonResult.Code != 200)
            {
                return(Json(jsonResult));
            }

            var info = _inventoryHelper.GetFBAInventoryResidualInfo(customerCode, new DateTime(1992, 11, 17), DateTime.Now);

            var result = new InventoryBody
            {
                CustomerCode   = customerCode,
                DateRange      = info.StartDate.ToString("yyyy-MM-dd") + " ~ " + info.CloseDate.ToString("yyyy-MM-dd"),
                ReportDate     = DateTime.Now.ToString("yyyy-MM-dd"),
                InstockCtns    = (int)info.CurrentTotalCtns,
                InstockPlts    = info.CurrentTotalPlts,
                ProcessingCtns = info.TotalPickingCtns,
                ProcessingPlts = info.TotalPickingPlts
            };

            var inventoryCtns = new List <InventoryCtns>();

            foreach (var c in info.FBACtnInventories)
            {
                inventoryCtns.Add(new InventoryCtns {
                    Container          = c.Container,
                    StorageType        = c.Type,
                    SubCustomer        = c.SubCustomer,
                    ShipmentId         = c.ShipmentId,
                    AmzRefId           = c.AmzRefId,
                    WarehouseCode      = c.WarehouseCode,
                    InboundDate        = c.InboundDate,
                    GrossWeightPerCtn  = c.GrossWeightPerCtn,
                    CBMPerCtn          = c.CBMPerCtn,
                    OriginalQuantity   = c.OriginalQuantity,
                    ProcessingQuantity = c.PickingCtns,
                    InstockQuantity    = c.ResidualQuantity,
                    HoldCtns           = c.HoldQuantity,
                    Location           = c.Location
                });
            }

            result.InventoryCtns = inventoryCtns;

            jsonResult.Body = result;

            return(Ok(jsonResult));
        }
        public async Task <IHttpActionResult> CreateOutboundOrderFromExternalRequest([FromUri] string appKey, [FromUri] string customerCode, [FromUri] string requestId, [FromUri] string version, [FromUri] string sign, [FromBody] FBAOutboundOrder order)
        {
            // 验证签名
            var customerInDb = _context.UpperVendors.SingleOrDefault(x => x.CustomerCode == customerCode);
            var jsonResult   = _validator.ValidateSign(appKey, customerInDb, requestId, version, sign);

            if (jsonResult.Code != 200)
            {
                return(Ok(jsonResult));
            }

            // 检查推送过来的Order的字段完整性,缺省的字段套用默认值
            var pickingStatus = await CreateShipOrderAsync(order, customerInDb, requestId);

            jsonResult.PickingStatus = new { Status = CheckStatus(pickingStatus) ? "Success" : "Failed", Details = pickingStatus };
            jsonResult.Code          = jsonResult.PickingStatus.Status == "Failed" ? 507 : jsonResult.Code;
            return(Created(Request.RequestUri, jsonResult));
        }
        public async Task <IHttpActionResult> DeleteOrder([FromUri] string appKey, [FromUri] string customerCode, [FromUri] string requestId, [FromUri] string version, [FromUri] string sign, [FromBody] DeleteRequestBody body)
        {
            // 验证签名
            var customerInDb = _context.UpperVendors.SingleOrDefault(x => x.CustomerCode == customerCode);
            var jsonResult   = _validator.ValidateSign(appKey, customerInDb, requestId, version, sign);

            if (jsonResult.Code != 200)
            {
                return(Json(jsonResult));
            }

            if (body.OrderType == "Inbound")
            {
                var inboundOrderInDb = _context.FBAMasterOrders.SingleOrDefault(x => x.Container == body.Reference && x.Status == FBAStatus.Draft);

                if (inboundOrderInDb != null)
                {
                    _moController.DeleteMasterOrderById(inboundOrderInDb.Id);
                }
                else
                {
                    return(Json(new { Code = "404", Message = "Cannot find inbound order# " + body.Reference + " or its stauts is not 'Draft'." }));
                }
            }
            else if (body.OrderType == "Outbound")
            {
                var outboundOrderInDb = _context.FBAShipOrders.SingleOrDefault(x => x.ShipOrderNumber == body.Reference && x.Status == FBAStatus.Draft);

                if (outboundOrderInDb != null)
                {
                    _callbackManager.CallBackWhenOutboundOrderCancelled(outboundOrderInDb);
                    await _soController.DeleteShipOrder(outboundOrderInDb.Id);
                }
                else
                {
                    return(Json(new { Code = "404", Message = "Cannot find outbound order# " + body.Reference + " or its stauts is not 'Draft'." }));
                }
            }

            return(Json(new { Code = "200", Message = "Delete Success!" }));
        }
        public async Task <IHttpActionResult> CreateInboundOrderFromExternalRequest([FromUri] string appKey, [FromUri] string customerCode, [FromUri] string requestId, [FromUri] string version, [FromUri] string sign, [FromBody] FBAInboundOrder order)
        {
            // 检查Container是否重复,否则返回错误
            var masterOrderInDb = _context.FBAMasterOrders.SingleOrDefault(x => x.Container == order.Container);

            if (masterOrderInDb != null)
            {
                return(Json(new JsonResponse {
                    Code = 506, ValidationStatus = "Failed", Message = "Container No. " + order.Container + " already existed in system. Please report this contianer No. to CSR of Grand Channel for more support."
                }));
            }

            var customerInDb = _context.UpperVendors.SingleOrDefault(x => x.CustomerCode == customerCode);

            var jsonResult = _validator.ValidateSign(appKey, customerInDb, requestId, version, sign);

            if (jsonResult.Code != 200)
            {
                return(Json(jsonResult));
            }

            // 创建订单逻辑
            if (version == "V1")
            {
                //建立主单并记录成功的操作,写入日志
                await CreateInboundOrderByAgentRequestV1(customerInDb, customerCode, order, requestId);

                //建立分单并记录成功的操作,写入日志
                //await CreateOutboundOrdersByAgentRequestV1(customerCode, order, requestId);
                return(Created(Request.RequestUri, new JsonResponse {
                    Code = 200, ValidationStatus = "Success", Message = "Success!"
                }));
            }

            return(Ok(new JsonResponse {
                Code = 200, ValidationStatus = "Success", Message = "No operation applied."
            }));
        }
        public IHttpActionResult GetOrderStatus([FromUri] string appKey, [FromUri] string customerCode, [FromUri] string requestId, [FromUri] string version, [FromUri] string sign, [FromBody] OrderStatusQureyBody body)
        {
            // 验证签名
            var customerInDb = _context.UpperVendors.SingleOrDefault(x => x.CustomerCode == customerCode);
            var jsonResult   = _validator.ValidateSign(appKey, customerInDb, requestId, version, sign);

            if (jsonResult.Code != 200)
            {
                return(Ok(jsonResult));
            }

            var qureyStauts = new List <QureyStatus>();

            if (body.OrderType == FBAOrderType.Inbound)
            {
                var qureyResults = QureyInboundOrders(body, out qureyStauts).Select(x => new { x.Container, x.Status });
                jsonResult.QureyStatus  = qureyStauts;
                jsonResult.QureyResults = new QureyResults {
                    InboundOrders = qureyResults
                };
            }
            else if (body.OrderType == FBAOrderType.Outbound)
            {
                var qureyResults = QureyOutboundOrders(body, out qureyStauts).Select(x => new { x.ShipOrderNumber, x.Status });
                jsonResult.QureyStatus  = qureyStauts;
                jsonResult.QureyResults = new QureyResults {
                    OutboundOrders = qureyResults
                };
            }
            else
            {
                jsonResult.Message = "No operation applied.";
            }

            return(Ok(jsonResult));
        }