Repository navigation
Expand file tree
/
Copy pathBaseShipmentController.cs
More file actions
660 lines (556 loc) · 26 KB
/
Copy pathBaseShipmentController.cs
File metadata and controls
660 lines (556 loc) · 26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
using Grand.Business.Core.Commands.Checkout.Shipping;
using Grand.Business.Core.Interfaces.Checkout.Orders;
using Grand.Business.Core.Interfaces.Checkout.Shipping;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Pdf;
using Grand.Domain.Orders;
using Grand.Domain.Permissions;
using Grand.Domain.Shipping;
using Grand.Infrastructure;
using Grand.Mediator;
using Grand.Web.AdminShared.Interfaces;
using Grand.Web.AdminShared.Models.Orders;
using Grand.Web.Common.Controllers;
using Grand.Web.Common.DataSource;
using Grand.Web.Common.Extensions;
using Grand.Web.Common.Filters;
using Grand.Web.Common.Security.Authorization;
using Microsoft.AspNetCore.Mvc;
using Grand.Web.Common.Localization;
namespace Grand.Web.AdminShared.Controllers;
[PermissionAuthorize(PermissionSystemName.Shipments)]
[AutoValidateAntiforgeryToken]
public abstract class BaseShipmentController(
IShipmentViewModelService shipmentViewModelService,
IOrderService orderService,
IRequestTranslationService translationService,
IContextAccessor contextAccessor,
IPdfService pdfService,
IShipmentService shipmentService,
IDateTimeService dateTimeService,
IMediator mediator,
IAdminDataScope<Shipment> scope,
IAdminDataScope<Order> orderScope)
: BaseController
{
// Exposed for host-specific concrete subclasses (Admin's EditUserFields action needs these
// same fields — primary-constructor parameters aren't visible to derived classes by name in
// C#).
protected IShipmentViewModelService ShipmentViewModelService => shipmentViewModelService;
protected IOrderService OrderService => orderService;
protected IRequestTranslationService TranslationService => translationService;
protected IContextAccessor ContextAccessor => contextAccessor;
protected IPdfService PdfService => pdfService;
protected IShipmentService ShipmentService => shipmentService;
protected IDateTimeService DateTimeService => dateTimeService;
protected IMediator Mediator => mediator;
protected IAdminDataScope<Shipment> Scope => scope;
protected IAdminDataScope<Order> OrderScope => orderScope;
/// <summary>DRY replacement for the repeated "load shipment, redirect to List if not found or
/// not authorized" pattern found in all 3 original controllers. Not a behavior change — every
/// call site below still individually returns RedirectToAction("List") exactly as the
/// originals did.</summary>
protected async Task<(Shipment shipment, IActionResult denied)> LoadAuthorizedShipment(string id)
{
var shipment = await shipmentService.GetShipmentById(id);
if (shipment == null) return (null, RedirectToAction("List"));
if (!await scope.HasAccess(shipment)) return (null, RedirectToAction("List"));
return (shipment, null);
}
#region Shipments
public async Task<IActionResult> List()
{
var model = await shipmentViewModelService.PrepareShipmentListModel();
return View(model);
}
[PermissionAuthorizeAction(PermissionActionName.List)]
[HttpPost]
public async Task<IActionResult> ShipmentListSelect(DataSourceRequest command, ShipmentListModel model)
{
if (scope.DefaultStoreId is not null) model.StoreId = scope.DefaultStoreId;
if (scope.DefaultVendorId is not null) model.VendorId = scope.DefaultVendorId;
var shipments = await shipmentViewModelService.PrepareShipments(model, command.Page, command.PageSize);
var items = new List<ShipmentModel>();
foreach (var item in shipments.shipments)
items.Add(await shipmentViewModelService.PrepareShipmentModel(item, false));
var gridModel = new DataSourceResult {
Data = items,
Total = shipments.totalCount
};
return Json(gridModel);
}
/// <summary>Filters per-shipment via scope.HasAccess rather than gating on the parent order.
/// Admin: GlobalAdminDataScope.HasAccess is always true, so this is a no-op filter — matches
/// Admin's original, which had no check at all. Store: every shipment under a given order
/// always shares that order's StoreId (PrepareShipment always sets StoreId = order.StoreId),
/// so per-shipment filtering produces the same user-visible result as Store's original
/// whole-order Content("") denial, with no possible mixed-store shipment set under one order.
/// Vendor: this is the literal mechanical equivalent of Vendor's original per-shipment
/// HasAccessToShipment loop.</summary>
[PermissionAuthorizeAction(PermissionActionName.List)]
[HttpPost]
public async Task<IActionResult> ShipmentsByOrder(string orderId, DataSourceRequest command)
{
var order = await orderService.GetOrderById(orderId);
if (order == null || order.Deleted)
throw new ArgumentException("No order found with the specified id");
//shipments
var shipmentModels = new List<ShipmentModel>();
var shipments = (await shipmentService.GetShipmentsByOrder(orderId))
.OrderBy(s => s.CreatedOnUtc)
.ToList();
var accessibleShipments = new List<Shipment>();
foreach (var shipment in shipments)
if (await scope.HasAccess(shipment))
accessibleShipments.Add(shipment);
foreach (var shipment in accessibleShipments)
shipmentModels.Add(await shipmentViewModelService.PrepareShipmentModel(shipment, false));
var gridModel = new DataSourceResult {
Data = shipmentModels,
Total = shipmentModels.Count
};
return Json(gridModel);
}
/// <summary>Deliberate behavior change for Store only: Store's original returned a soft
/// Content("") on a store mismatch; Admin/Vendor's originals both threw ArgumentException.
/// Unified on the throwing form (2 of 3 hosts' original shape) rather than using
/// LoadAuthorizedShipment (which redirects — wrong fit for a JSON-grid endpoint, since a
/// redirect response is meaningless to the AJAX grid caller expecting JSON).</summary>
[PermissionAuthorizeAction(PermissionActionName.List)]
[HttpPost]
public async Task<IActionResult> ShipmentsItemsByShipmentId(string shipmentId, DataSourceRequest command)
{
var shipment = await shipmentService.GetShipmentById(shipmentId) ?? throw new ArgumentException("No shipment found with the specified id");
if (!await scope.HasAccess(shipment))
throw new ArgumentException("No shipment found with the specified id");
var order = await orderService.GetOrderById(shipment.OrderId) ?? throw new ArgumentException("No order found with the specified id");
//shipments
var shipmentModel = await shipmentViewModelService.PrepareShipmentModel(shipment, true);
var gridModel = new DataSourceResult {
Data = shipmentModel.Items,
Total = shipmentModel.Items.Count
};
return Json(gridModel);
}
#endregion
#region AddShipment
/// <summary>
/// An order awaiting approval may still be rejected or withdrawn, and a shipment would block the cancel
/// that both run; nothing ships before the approver decides
/// </summary>
private IActionResult RefuseOrderAwaitingApproval(Order order)
{
if (order.OrderStatusId != (int)OrderStatusSystem.AwaitingApproval)
return null;
Error(translationService.GetResource("Admin.Orders.Shipments.OrderAwaitsApproval"));
return RedirectToAction("Edit", "Order", new { id = order.Id });
}
[PermissionAuthorizeAction(PermissionActionName.Create)]
public async Task<IActionResult> AddShipment(string orderId)
{
var order = await orderService.GetOrderById(orderId);
if (order == null || order.Deleted || !await orderScope.HasAccess(order))
//No order found with the specified id
return RedirectToAction("List");
var awaitingApproval = RefuseOrderAwaitingApproval(order);
if (awaitingApproval != null) return awaitingApproval;
var model = await shipmentViewModelService.PrepareShipmentModel(order);
return View(model);
}
[PermissionAuthorizeAction(PermissionActionName.Create)]
[HttpPost]
[ArgumentNameFilter(KeyName = "save-continue", Argument = "continueEditing")]
public async Task<IActionResult> AddShipment(AddShipmentModel model, bool continueEditing)
{
var order = await orderService.GetOrderById(model.OrderId);
if (order == null || order.Deleted || !await orderScope.HasAccess(order))
//No order found with the specified id
return RedirectToAction("List");
var awaitingApproval = RefuseOrderAwaitingApproval(order);
if (awaitingApproval != null) return awaitingApproval;
var orderItems = scope.FilterOrderItems(order.OrderItems).ToList();
var (shipment, totalWeight) = await shipmentViewModelService.PrepareShipment(order, orderItems, model);
if (shipment == null || !shipment.ShipmentItems.Any())
{
Error(translationService.GetResource("Admin.Orders.Shipments.NoProductsSelected"));
return RedirectToAction("AddShipment", new { orderId = model.OrderId });
}
//check stock
var (valid, message) = await shipmentViewModelService.ValidStockShipment(shipment);
if (!valid)
{
Error(message);
return RedirectToAction("AddShipment", new { orderId = model.OrderId });
}
//the batches and serial numbers it names, checked against the stored units before it is stored
var unitErrors = await shipmentViewModelService.PrepareShipmentUnits(shipment);
if (unitErrors.Count > 0)
{
foreach (var unitError in unitErrors)
Error(unitError);
return RedirectToAction("AddShipment", new { orderId = model.OrderId });
}
shipment.TotalWeight = totalWeight;
await shipmentService.InsertShipment(shipment);
//add a note
await orderService.InsertOrderNote(new OrderNote {
Note = $"A shipment #{shipment.ShipmentNumber} has been added",
DisplayToCustomer = false,
OrderId = order.Id
});
Success(translationService.GetResource("Admin.Orders.Shipments.Added"));
return continueEditing
? RedirectToAction("ShipmentDetails", new { id = shipment.Id })
: RedirectToAction("List", new { id = shipment.Id });
}
#endregion
#region Shipment details
[PermissionAuthorizeAction(PermissionActionName.Preview)]
public async Task<IActionResult> ShipmentDetails(string id)
{
var (shipment, denied) = await LoadAuthorizedShipment(id);
if (denied != null) return denied;
var order = await orderService.GetOrderById(shipment.OrderId);
if (order == null)
//No order found with the specified id
return RedirectToAction("List");
var model = await shipmentViewModelService.PrepareShipmentModel(shipment, true, true);
return View(model);
}
[PermissionAuthorizeAction(PermissionActionName.Delete)]
[HttpPost]
public async Task<IActionResult> DeleteShipment(string id)
{
var (shipment, denied) = await LoadAuthorizedShipment(id);
if (denied != null) return denied;
var order = await orderService.GetOrderById(shipment.OrderId);
if (order == null)
//No order found with the specified id
return RedirectToAction("List");
await shipmentService.DeleteShipment(shipment);
//add a note
await orderService.InsertOrderNote(new OrderNote {
Note = $"A shipment #{shipment.ShipmentNumber} has been deleted",
DisplayToCustomer = false,
OrderId = order.Id
});
Success(translationService.GetResource("Admin.Orders.Shipments.Deleted"));
return RedirectToAction("Edit", "Order", new { order.Id });
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> SetTrackingNumber(ShipmentTrackingModel model)
{
var (shipment, denied) = await LoadAuthorizedShipment(model.Id);
if (denied != null) return denied;
shipment.TrackingNumber = model.TrackingNumber;
await shipmentService.UpdateShipment(shipment);
return RedirectToAction("ShipmentDetails", new { id = shipment.Id });
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> SetShipmentAdminComment(ShipmentAdminCommentModel model)
{
var (shipment, denied) = await LoadAuthorizedShipment(model.Id);
if (denied != null) return denied;
shipment.AdminComment = model.AdminComment;
await shipmentService.UpdateShipment(shipment);
return RedirectToAction("ShipmentDetails", new { id = shipment.Id });
}
/// <summary>The one Save of the shipment info form. The per-field actions above are kept for
/// callers outside the panel; shipping and delivering stay separate because they notify the
/// customer. A date is only written when the shipment already has one — the first value comes
/// from ShipCommand / DeliveryCommand, which the form must not trigger.</summary>
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> SaveShipmentInfo(ShipmentInfoModel model)
{
var (shipment, denied) = await LoadAuthorizedShipment(model.Id);
if (denied != null) return denied;
try
{
if (shipment.ShippedDateUtc.HasValue)
{
if (!model.ShippedDate.HasValue) throw new Exception("Enter shipped date");
shipment.ShippedDateUtc = model.ShippedDate.ConvertToUtcTime(dateTimeService);
}
if (shipment.DeliveryDateUtc.HasValue)
{
if (!model.DeliveryDate.HasValue) throw new Exception("Enter delivery date");
shipment.DeliveryDateUtc = model.DeliveryDate.ConvertToUtcTime(dateTimeService);
}
shipment.TrackingNumber = model.TrackingNumber;
shipment.AdminComment = model.AdminComment;
await shipmentService.UpdateShipment(shipment);
Success(translationService.GetResource("Admin.Orders.Shipments.Updated"));
}
catch (Exception exc)
{
Error(exc);
}
return RedirectToAction("ShipmentDetails", new { id = shipment.Id });
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
/// <summary>Takes the whole info form, because its shipped date - which the screen offers
/// before a shipment is shipped - is the date to stamp. An empty one means now.</summary>
public async Task<IActionResult> SetAsShipped(ShipmentInfoModel model)
{
var (shipment, denied) = await LoadAuthorizedShipment(model.Id);
if (denied != null) return denied;
try
{
await mediator.Send(new ShipCommand {
Shipment = shipment, NotifyCustomer = true,
ShippedDateUtc = model.ShippedDate?.ConvertToUtcTime(dateTimeService)
});
return RedirectToAction("ShipmentDetails", new { id = shipment.Id });
}
catch (Exception exc)
{
Error(exc);
return RedirectToAction("ShipmentDetails", new { id = shipment.Id });
}
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> EditShippedDate(ShipmentShippedDateModel model)
{
var (shipment, denied) = await LoadAuthorizedShipment(model.Id);
if (denied != null) return denied;
try
{
if (!model.ShippedDate.HasValue) throw new Exception("Enter shipped date");
shipment.ShippedDateUtc = model.ShippedDate.ConvertToUtcTime(dateTimeService);
await shipmentService.UpdateShipment(shipment);
return RedirectToAction("ShipmentDetails", new { id = shipment.Id });
}
catch (Exception exc)
{
Error(exc);
return RedirectToAction("ShipmentDetails", new { id = shipment.Id });
}
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
/// <summary>Same as SetAsShipped: the delivery date of the info form is the date to stamp,
/// and an empty one means now.</summary>
public async Task<IActionResult> SetAsDelivered(ShipmentInfoModel model)
{
var (shipment, denied) = await LoadAuthorizedShipment(model.Id);
if (denied != null) return denied;
try
{
await mediator.Send(new DeliveryCommand {
Shipment = shipment, NotifyCustomer = true,
DeliveryDateUtc = model.DeliveryDate?.ConvertToUtcTime(dateTimeService)
});
return RedirectToAction("ShipmentDetails", new { id = shipment.Id });
}
catch (Exception exc)
{
Error(exc);
return RedirectToAction("ShipmentDetails", new { id = shipment.Id });
}
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> EditDeliveryDate(ShipmentDeliveryDateModel model)
{
var (shipment, denied) = await LoadAuthorizedShipment(model.Id);
if (denied != null) return denied;
try
{
if (!model.DeliveryDate.HasValue) throw new Exception("Enter delivery date");
shipment.DeliveryDateUtc = model.DeliveryDate.ConvertToUtcTime(dateTimeService);
await shipmentService.UpdateShipment(shipment);
return RedirectToAction("ShipmentDetails", new { id = shipment.Id });
}
catch (Exception exc)
{
Error(exc);
return RedirectToAction("ShipmentDetails", new { id = shipment.Id });
}
}
/// <summary>Shared across all 3 hosts: the "User fields" tab on ShipmentDetails.cshtml posts
/// here with no host gating, so Store and Vendor need this action too, not just Admin.</summary>
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> EditUserFields(string id, ShipmentModel model)
{
var (shipment, denied) = await LoadAuthorizedShipment(id);
if (denied != null) return denied;
shipment.UserFields = model.UserFields;
await shipmentService.UpdateShipment(shipment);
//selected tab
await SaveSelectedTabIndex();
return RedirectToAction("ShipmentDetails", new { id = shipment.Id });
}
#endregion
#region PDF export and bulk actions
[PermissionAuthorizeAction(PermissionActionName.Preview)]
[AuditExport("Shipment")]
public async Task<IActionResult> PdfPackagingSlip(string shipmentId)
{
var (shipment, denied) = await LoadAuthorizedShipment(shipmentId);
if (denied != null) return denied;
var order = await orderService.GetOrderById(shipment.OrderId);
if (order == null)
//No order found with the specified id
return RedirectToAction("List");
var shipments = new List<Shipment> { shipment };
byte[] bytes;
using (var stream = new MemoryStream())
{
await pdfService.PrintPackagingSlipsToPdf(stream, shipments, contextAccessor.WorkContext.WorkingLanguage.Id,
cancellationToken: HttpContext.RequestAborted);
bytes = stream.ToArray();
}
return File(bytes, "application/pdf", $"packagingslip_{shipment.Id}.pdf");
}
[PermissionAuthorizeAction(PermissionActionName.Export)]
[HttpPost]
[AuditExport("Shipment")]
public async Task<IActionResult> PdfPackagingSlipAll(ShipmentListModel model)
{
if (scope.DefaultStoreId is not null) model.StoreId = scope.DefaultStoreId;
if (scope.DefaultVendorId is not null) model.VendorId = scope.DefaultVendorId;
//load shipments
var shipments = await shipmentViewModelService.PrepareShipments(model, 1, 100);
//ensure that we at least one shipment selected
if (shipments.totalCount == 0)
{
Error(translationService.GetResource($"{scope.ResourceKeyPrefix}.Orders.Shipments.NoShipmentsSelected"));
return RedirectToAction("List");
}
byte[] bytes;
using (var stream = new MemoryStream())
{
await pdfService.PrintPackagingSlipsToPdf(stream, shipments.shipments.ToList(),
contextAccessor.WorkContext.WorkingLanguage.Id, HttpContext.RequestAborted);
bytes = stream.ToArray();
}
return File(bytes, "application/pdf", "packagingslips.pdf");
}
[PermissionAuthorizeAction(PermissionActionName.Export)]
[HttpPost]
[AuditExport("Shipment")]
public async Task<IActionResult> PdfPackagingSlipSelected(string selectedIds)
{
var shipments = new List<Shipment>();
if (selectedIds != null)
{
var ids = selectedIds
.Split([','], StringSplitOptions.RemoveEmptyEntries)
.Select(x => x)
.ToArray();
shipments.AddRange(await shipmentService.GetShipmentsByIds(ids));
}
var accessibleShipments = new List<Shipment>();
foreach (var s in shipments)
if (await scope.HasAccess(s))
accessibleShipments.Add(s);
//ensure that we at least one shipment selected — checks the unfiltered count (not
//accessibleShipments.Count) so a request that only names shipments the caller can't
//access reports "no shipments selected" rather than silently producing an empty PDF,
//matching all 3 originals' pre-existing (and slightly inconsistent) behavior
if (shipments.Count == 0)
{
Error(translationService.GetResource($"{scope.ResourceKeyPrefix}.Orders.Shipments.NoShipmentsSelected"));
return RedirectToAction("List");
}
byte[] bytes;
using (var stream = new MemoryStream())
{
await pdfService.PrintPackagingSlipsToPdf(stream, accessibleShipments, contextAccessor.WorkContext.WorkingLanguage.Id,
HttpContext.RequestAborted);
bytes = stream.ToArray();
}
return File(bytes, "application/pdf", "packagingslips.pdf");
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> SetAsShippedSelected(ICollection<string> selectedIds)
{
var shipments = new List<Shipment>();
if (selectedIds != null) shipments.AddRange(await shipmentService.GetShipmentsByIds(selectedIds.ToArray()));
var accessibleShipments = new List<Shipment>();
foreach (var s in shipments)
if (await scope.HasAccess(s))
accessibleShipments.Add(s);
foreach (var shipment in accessibleShipments)
try
{
await mediator.Send(new ShipCommand { Shipment = shipment, NotifyCustomer = true });
}
catch
{
//ignore any exception
}
return Json(new { Result = true });
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> SetAsDeliveredSelected(ICollection<string> selectedIds)
{
var shipments = new List<Shipment>();
if (selectedIds != null) shipments.AddRange(await shipmentService.GetShipmentsByIds(selectedIds.ToArray()));
var accessibleShipments = new List<Shipment>();
foreach (var s in shipments)
if (await scope.HasAccess(s))
accessibleShipments.Add(s);
foreach (var shipment in accessibleShipments)
try
{
await mediator.Send(new DeliveryCommand { Shipment = shipment, NotifyCustomer = true });
}
catch
{
//ignore any exception
}
return Json(new { Result = true });
}
#endregion
#region Shipment notes
[PermissionAuthorizeAction(PermissionActionName.Preview)]
[HttpPost]
public async Task<IActionResult> ShipmentNotesSelect(string shipmentId, DataSourceRequest command)
{
var shipment = await shipmentService.GetShipmentById(shipmentId);
if (shipment == null || !await scope.HasAccess(shipment))
throw new ArgumentException("No shipment found with the specified id");
//shipment notes
var shipmentNoteModels = await shipmentViewModelService.PrepareShipmentNotes(shipment);
var gridModel = new DataSourceResult {
Data = shipmentNoteModels,
Total = shipmentNoteModels.Count
};
return Json(gridModel);
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> ShipmentNoteAdd(string shipmentId, string downloadId, bool displayToCustomer,
string message)
{
var shipment = await shipmentService.GetShipmentById(shipmentId);
if (shipment == null || !await scope.HasAccess(shipment))
return Json(new { Result = false });
if (!await shipmentViewModelService.InsertShipmentNote(shipment, downloadId, displayToCustomer, message))
return Json(new {
Result = false,
Message = translationService.GetResource("Admin.Orders.Shipments.ShipmentNotes.DownloadNotOfShipment")
});
return Json(new { Result = true });
}
[PermissionAuthorizeAction(PermissionActionName.Delete)]
[HttpPost]
public async Task<IActionResult> ShipmentNoteDelete(string id, string shipmentId)
{
var shipment = await shipmentService.GetShipmentById(shipmentId);
if (shipment == null || !await scope.HasAccess(shipment))
throw new ArgumentException("No shipment found with the specified id");
await shipmentViewModelService.DeleteShipmentNote(shipment, id);
return new JsonResult("");
}
#endregion
}