Repository navigation
Expand file tree
/
Copy pathBaseMerchandiseReturnController.cs
More file actions
379 lines (314 loc) · 17.4 KB
/
Copy pathBaseMerchandiseReturnController.cs
File metadata and controls
379 lines (314 loc) · 17.4 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
using Grand.Business.Core.Interfaces.Checkout.Orders;
using Grand.Business.Core.Interfaces.Storage;
using Grand.Domain.Orders;
using Grand.Domain.Permissions;
using Grand.Web.AdminShared.Extensions;
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.Security.Authorization;
using Microsoft.AspNetCore.Mvc;
using Grand.Web.Common.Localization;
namespace Grand.Web.AdminShared.Controllers;
[PermissionAuthorize(PermissionSystemName.MerchandiseReturns)]
[AutoValidateAntiforgeryToken]
public abstract class BaseMerchandiseReturnController(
IMerchandiseReturnViewModelService merchandiseReturnViewModelService,
IRequestTranslationService translationService,
IMerchandiseReturnService merchandiseReturnService,
IOrderService orderService,
IAdminDataScope<MerchandiseReturn> scope)
: BaseController
{
// Exposed for host subclasses: primary-constructor parameters aren't visible to derived classes
// by name in C#.
protected IMerchandiseReturnViewModelService MerchandiseReturnViewModelService => merchandiseReturnViewModelService;
protected IRequestTranslationService TranslationService => translationService;
protected IMerchandiseReturnService MerchandiseReturnService => merchandiseReturnService;
protected IOrderService OrderService => orderService;
protected IAdminDataScope<MerchandiseReturn> Scope => scope;
/// <summary>
/// Whether the panel has inventory documents: the return's receipts are listed and "Receive to stock" is
/// offered while something is left to receive
/// </summary>
protected virtual bool HasInventoryDocuments => true;
/// <summary>
/// Whether the panel gives back the money for a received return through the payment of its order; a panel
/// that does not shows the refund once made
/// </summary>
protected virtual bool RefundsPayments => true;
private async Task PrepareEditModel(MerchandiseReturnModel model, MerchandiseReturn merchandiseReturn)
{
await MerchandiseReturnViewModelService.PrepareMerchandiseReturnModel(model, merchandiseReturn, false);
if (HasInventoryDocuments)
await MerchandiseReturnViewModelService.PrepareReturnReceipts(model, merchandiseReturn);
await MerchandiseReturnViewModelService.PrepareRefund(model, merchandiseReturn, RefundsPayments);
}
#region List
public IActionResult Index() => RedirectToAction("List");
public async Task<IActionResult> List()
{
var model = await merchandiseReturnViewModelService.PrepareReturnRequestListModel();
return View(model);
}
[PermissionAuthorizeAction(PermissionActionName.List)]
[HttpPost]
public async Task<IActionResult> List(DataSourceRequest command, MerchandiseReturnListModel model)
{
// Vendor needs no analogous model.StoreId forcing here (no store concept) - its
// vendor-scoping happens inside the shared service call via scope.DefaultVendorId (Task 4).
if (scope.DefaultStoreId is not null) model.StoreId = scope.DefaultStoreId;
var (merchandiseReturnModels, totalCount) =
await merchandiseReturnViewModelService.PrepareMerchandiseReturnModel(model, command.Page, command.PageSize);
var gridModel = new DataSourceResult {
Data = merchandiseReturnModels,
Total = totalCount
};
return Json(gridModel);
}
#endregion
#region GoToId / Products
[PermissionAuthorizeAction(PermissionActionName.Preview)]
[HttpPost]
public async Task<IActionResult> GoToId(MerchandiseReturnListModel model)
{
if (model.GoDirectlyToId == null)
return RedirectToAction("List");
int.TryParse(model.GoDirectlyToId, out var id);
var merchandiseReturn = await MerchandiseReturnService.GetMerchandiseReturnById(id);
if (merchandiseReturn == null || !await Scope.HasAccess(merchandiseReturn))
return RedirectToAction("List");
return RedirectToAction("Edit", new { id = merchandiseReturn.Id });
}
[PermissionAuthorizeAction(PermissionActionName.Preview)]
[HttpPost]
public async Task<IActionResult> ProductsForMerchandiseReturn(string merchandiseReturnId, DataSourceRequest command)
{
var merchandiseReturn = await MerchandiseReturnService.GetMerchandiseReturnById(merchandiseReturnId);
if (merchandiseReturn == null || !await Scope.HasAccess(merchandiseReturn))
return ErrorForKendoGridJson("Merchandise return not found");
var items = await MerchandiseReturnViewModelService.PrepareMerchandiseReturnItemModel(merchandiseReturnId);
var gridModel = new DataSourceResult {
Data = items,
Total = items.Count
};
return Json(gridModel);
}
#endregion
#region Edit
[PermissionAuthorizeAction(PermissionActionName.Preview)]
public async Task<IActionResult> Edit(string id)
{
var merchandiseReturn = await MerchandiseReturnService.GetMerchandiseReturnById(id);
if (merchandiseReturn == null) return RedirectToAction("List");
if (!await Scope.CanView(merchandiseReturn)) return RedirectToAction("List");
var model = new MerchandiseReturnModel();
await PrepareEditModel(model, merchandiseReturn);
return View(model);
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
[Grand.Web.Common.Filters.ArgumentNameFilter(KeyName = "save-continue", Argument = "continueEditing")]
public async Task<IActionResult> Edit(MerchandiseReturnModel model, bool continueEditing,
[FromServices] Grand.Business.Core.Interfaces.Common.Addresses.IAddressAttributeService addressAttributeService,
[FromServices] Grand.Business.Core.Interfaces.Common.Addresses.IAddressAttributeParser addressAttributeParser,
[FromServices] Grand.Domain.Orders.OrderSettings orderSettings)
{
var merchandiseReturn = await MerchandiseReturnService.GetMerchandiseReturnById(model.Id);
if (merchandiseReturn == null) return RedirectToAction("List");
if (!await Scope.HasAccess(merchandiseReturn)) return RedirectToAction("List");
if (ModelState.IsValid)
{
var customAddressAttributes = new List<Grand.Domain.Common.CustomAttribute>();
if (orderSettings.MerchandiseReturns_AllowToSpecifyPickupAddress)
customAddressAttributes = await model.PickupAddress.ParseCustomAddressAttributes(
addressAttributeParser, addressAttributeService);
var (updated, error) = await MerchandiseReturnViewModelService.UpdateMerchandiseReturnModel(
merchandiseReturn, model, customAddressAttributes);
//a status changed comes back to the return; a refused one shows the form again, with the reason typed
if (model.ChangeStatusTo.HasValue && error == null)
{
Success(TranslationService.GetResource(
$"{Scope.ResourceKeyPrefix}.Orders.MerchandiseReturns.StatusChanged"));
return RedirectToAction("Edit", new { id = merchandiseReturn.Id });
}
if (error == null)
{
Success(TranslationService.GetResource(
$"{Scope.ResourceKeyPrefix}.Orders.MerchandiseReturns.Updated"));
return continueEditing
? RedirectToAction("Edit", new { id = updated.Id })
: RedirectToAction("List");
}
Error(TranslationService.GetResource($"{Scope.ResourceKeyPrefix}.{error}"));
//the status buttons follow the return as stored now: another change may be why this one was refused
merchandiseReturn = await MerchandiseReturnService.GetMerchandiseReturnById(model.Id) ?? merchandiseReturn;
}
await PrepareEditModel(model, merchandiseReturn);
return View(model);
}
#endregion
#region Return method
/// <summary>
/// The label of the return, to print and stick on the parcel
/// </summary>
[PermissionAuthorizeAction(PermissionActionName.Preview)]
public async Task<IActionResult> Label(string id, [FromServices] IDownloadService downloadService)
{
var merchandiseReturn = await MerchandiseReturnService.GetMerchandiseReturnById(id);
if (merchandiseReturn == null) return RedirectToAction("List");
if (!await Scope.CanView(merchandiseReturn)) return RedirectToAction("List");
var shipping = merchandiseReturn.ReturnShipping;
if (shipping?.LabelStatus != ReturnLabelStatus.Created || string.IsNullOrEmpty(shipping.LabelDownloadId))
return RedirectToAction("Edit", new { id = merchandiseReturn.Id });
var download = await downloadService.GetDownloadById(shipping.LabelDownloadId);
if (download?.DownloadBinary == null)
return RedirectToAction("Edit", new { id = merchandiseReturn.Id });
return File(download.DownloadBinary,
string.IsNullOrEmpty(download.ContentType) ? "application/octet-stream" : download.ContentType,
download.Filename + download.Extension);
}
/// <summary>
/// Asks the provider again for a label it has not created; the label service takes the label's lock, so a
/// double click or the customer's retry at the same time buys one label only
/// </summary>
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> RetryLabel(string id,
[FromServices] IMerchandiseReturnLabelService merchandiseReturnLabelService)
{
var merchandiseReturn = await MerchandiseReturnService.GetMerchandiseReturnById(id);
if (merchandiseReturn == null) return RedirectToAction("List");
if (!await Scope.HasAccess(merchandiseReturn)) return RedirectToAction("List");
var error = await merchandiseReturnLabelService.CreateLabel(merchandiseReturn.Id);
if (error != null)
Error(TranslationService.GetResource($"{Scope.ResourceKeyPrefix}.{error}"));
//a label the provider failed again is shown on the return, with the provider's error
return RedirectToAction("Edit", new { id = merchandiseReturn.Id });
}
#endregion
#region Refund
/// <summary>
/// Gives back the money for the received return, through the payment provider or offline, and completes it.
/// Posts the edit form to its own action: what was typed and not saved yet is not saved by it
/// </summary>
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> Refund(string id, double refundAmount, bool offline)
{
var merchandiseReturn = await MerchandiseReturnService.GetMerchandiseReturnById(id);
if (merchandiseReturn == null) return RedirectToAction("List");
if (!RefundsPayments || !await Scope.HasAccess(merchandiseReturn)) return RedirectToAction("List");
var error = await MerchandiseReturnViewModelService.RefundMerchandiseReturn(merchandiseReturn, refundAmount,
offline);
if (error == null)
Success(TranslationService.GetResource(
$"{Scope.ResourceKeyPrefix}.Orders.MerchandiseReturns.Refund.Refunded"));
else
Error(TranslationService.GetResource($"{Scope.ResourceKeyPrefix}.{error}"));
return RedirectToAction("Edit", new { id = merchandiseReturn.Id });
}
/// <summary>
/// Records whether the money of a refund that waits for its check went back: the return is refunded and
/// completed, or it can be refunded again. Posts the edit form to its own action, as the refund does
/// </summary>
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> ResolveRefund(string id, bool wentBack)
{
var merchandiseReturn = await MerchandiseReturnService.GetMerchandiseReturnById(id);
if (merchandiseReturn == null) return RedirectToAction("List");
if (!RefundsPayments || !await Scope.HasAccess(merchandiseReturn)) return RedirectToAction("List");
var error = await MerchandiseReturnViewModelService.ResolveRefund(merchandiseReturn, wentBack);
if (error == null)
Success(TranslationService.GetResource(wentBack
? $"{Scope.ResourceKeyPrefix}.Orders.MerchandiseReturns.Refund.Refunded"
: $"{Scope.ResourceKeyPrefix}.Orders.MerchandiseReturns.Refund.Cleared"));
else
Error(TranslationService.GetResource($"{Scope.ResourceKeyPrefix}.{error}"));
return RedirectToAction("Edit", new { id = merchandiseReturn.Id });
}
#endregion
#region Delete
[PermissionAuthorizeAction(PermissionActionName.Delete)]
[HttpPost]
public async Task<IActionResult> Delete(string id)
{
var merchandiseReturn = await merchandiseReturnService.GetMerchandiseReturnById(id);
if (merchandiseReturn == null) return RedirectToAction("List");
if (!await scope.HasAccess(merchandiseReturn)) return RedirectToAction("List");
if (ModelState.IsValid)
{
var error = await merchandiseReturnViewModelService.DeleteMerchandiseReturn(merchandiseReturn);
if (error == null)
Success(translationService.GetResource(
$"{scope.ResourceKeyPrefix}.Orders.MerchandiseReturns.Deleted"));
else
Error(translationService.GetResource($"{scope.ResourceKeyPrefix}.{error}"));
}
return RedirectToAction("List");
}
#endregion
#region Merchandise return notes
/// <summary>Deliberately preserved host divergence (spec §5/§11, DECIDED — not unified): Admin's
/// and Vendor's original controllers both throw ArgumentException for a not-found-or-denied
/// merchandise return; Store's soft-denies with an empty Content("") response instead, because
/// Store's Kendo grid error path expects a 200 with an empty body, not a 500. This seam is
/// written from scratch for this phase - it is NOT copied from BaseOrderManagementController's
/// OrderNotesSelect, which (as shipped) has no host-differentiated response at all (see this
/// plan's Global Constraints for the full explanation). Default here matches Admin/Vendor; only
/// Grand.Web.Store's concrete MerchandiseReturnController overrides this (Task 10).</summary>
protected virtual IActionResult NotFoundOrDeniedForNotesSelect() =>
throw new ArgumentException("No merchandise return found with the specified id");
[PermissionAuthorizeAction(PermissionActionName.Preview)]
[HttpPost]
public async Task<IActionResult> MerchandiseReturnNotesSelect(string merchandiseReturnId, DataSourceRequest command)
{
var merchandiseReturn = await merchandiseReturnService.GetMerchandiseReturnById(merchandiseReturnId);
if (merchandiseReturn == null || !await scope.HasAccess(merchandiseReturn))
return NotFoundOrDeniedForNotesSelect();
var merchandiseReturnNoteModels = await merchandiseReturnViewModelService.PrepareMerchandiseReturnNotes(merchandiseReturn);
var gridModel = new DataSourceResult {
Data = merchandiseReturnNoteModels,
Total = merchandiseReturnNoteModels.Count
};
return Json(gridModel);
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> MerchandiseReturnNoteAdd(string merchandiseReturnId, string downloadId,
bool displayToCustomer, string message)
{
var merchandiseReturn = await merchandiseReturnService.GetMerchandiseReturnById(merchandiseReturnId);
if (merchandiseReturn == null || !await scope.HasAccess(merchandiseReturn))
return Json(new { Result = false });
// Spec §2.5/§11 (approved): no caller-supplied orderId - always resolve the entity's own
// order server-side. Closes Admin's original gap (never validated a caller-supplied orderId
// matched the merchandise return), makes Store's now-redundant equality check unnecessary,
// and needs no new parameter on Vendor's side.
var order = await orderService.GetOrderById(merchandiseReturn.OrderId);
if (order == null) return Json(new { Result = false });
if (!await merchandiseReturnViewModelService.InsertMerchandiseReturnNote(merchandiseReturn, order, downloadId,
displayToCustomer, message))
return Json(new {
Result = false,
Message = translationService.GetResource(
"Admin.Orders.MerchandiseReturns.MerchandiseReturnNotes.DownloadNotOfMerchandiseReturn")
});
return Json(new { Result = true });
}
[PermissionAuthorizeAction(PermissionActionName.Edit)]
[HttpPost]
public async Task<IActionResult> MerchandiseReturnNoteDelete(string id, string merchandiseReturnId)
{
var merchandiseReturn = await merchandiseReturnService.GetMerchandiseReturnById(merchandiseReturnId);
if (merchandiseReturn == null)
throw new ArgumentException("No merchandise return found with the specified id");
if (!await scope.HasAccess(merchandiseReturn))
return Json(new { Result = false });
await merchandiseReturnViewModelService.DeleteMerchandiseReturnNote(merchandiseReturn, id);
return new JsonResult("");
}
#endregion
}