1 | /** |
---|
2 | * Provides a service class for the PurchaseOrder and PurchaseOrderNumber domain classes. |
---|
3 | */ |
---|
4 | class PurchaseOrderService { |
---|
5 | |
---|
6 | boolean transactional = false |
---|
7 | |
---|
8 | def savePurchaseOrderNumberRange(prefix, startOfRange, endOfRange, suffix="") { |
---|
9 | PurchaseOrderNumber.withTransaction { status -> |
---|
10 | def result = [:] |
---|
11 | |
---|
12 | def fail = { Map m -> |
---|
13 | status.setRollbackOnly() |
---|
14 | if(result.purchaseOrderNumber && m.field) |
---|
15 | result.purchaseOrderNumber.errors.rejectValue(m.field, m.code) |
---|
16 | result.error = [ code: m.code, args: ["PurchaseOrderNumber"] ] |
---|
17 | return result |
---|
18 | } |
---|
19 | |
---|
20 | // Sanity checks. |
---|
21 | if(startOfRange < 0) |
---|
22 | return fail(code:"default.create.failure") |
---|
23 | if(endOfRange < 0) |
---|
24 | return fail(code:"default.create.failure") |
---|
25 | // Auto swap range. |
---|
26 | if(endOfRange < startOfRange) |
---|
27 | (startOfRange, endOfRange) = [endOfRange, startOfRange] |
---|
28 | |
---|
29 | def r |
---|
30 | def p = [:] |
---|
31 | for(i in startOfRange..endOfRange) { |
---|
32 | p.value= "${prefix}${i}${suffix}" |
---|
33 | r = savePurchaseOrderNumber(p) |
---|
34 | if(r.error) |
---|
35 | return fail(code: r.code) |
---|
36 | |
---|
37 | } |
---|
38 | |
---|
39 | // Success. |
---|
40 | return result |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | PurchaseOrderNumber findNextUnusedPurchaseOrderNumber() { |
---|
45 | def empty = PurchaseOrderNumber.list().find{it.purchaseOrder==null} |
---|
46 | return empty |
---|
47 | } |
---|
48 | |
---|
49 | PurchaseOrder getOrCreatePurchaseOrder(params) { |
---|
50 | def id = params.purchaseOrderNumber.id |
---|
51 | if (params.purchaseOrderNumber.id instanceof Long) { |
---|
52 | id = params.purchaseOrderNumber.id |
---|
53 | } else { |
---|
54 | id = params.purchaseOrderNumber.id.toString().toLong() |
---|
55 | } |
---|
56 | def pon = PurchaseOrderNumber.get(id) |
---|
57 | def po = pon?.purchaseOrder |
---|
58 | if (!po) { |
---|
59 | po = createPurchaseOrder(pon,params) |
---|
60 | } |
---|
61 | return po |
---|
62 | } |
---|
63 | |
---|
64 | PurchaseOrder createPurchaseOrder(pon,params) { |
---|
65 | def supplier = Supplier.get(params.supplier.id) |
---|
66 | def po = new PurchaseOrder(supplier:supplier) |
---|
67 | pon.purchaseOrder = po |
---|
68 | po.purchaseOrderNumber = pon |
---|
69 | pon.save(failOnError:true) // should cascade to save the PO as well. I wonder why it doesn't. |
---|
70 | po.save(failOnError:true) |
---|
71 | return po |
---|
72 | } |
---|
73 | |
---|
74 | List<PurchaseOrder> findDraftPurchaseOrderNumbers() { |
---|
75 | def drafts = PurchaseOrder.list().findAll{it.ordered == null}.collect{it.purchaseOrderNumber} |
---|
76 | return drafts |
---|
77 | } |
---|
78 | |
---|
79 | def savePurchaseOrderNumber(params) { |
---|
80 | PurchaseOrderNumber.withTransaction { status -> |
---|
81 | def result = [:] |
---|
82 | |
---|
83 | def fail = { Map m -> |
---|
84 | status.setRollbackOnly() |
---|
85 | if(result.purchaseOrderNumber && m.field) |
---|
86 | result.purchaseOrderNumber.errors.rejectValue(m.field, m.code) |
---|
87 | result.error = [ code: m.code, args: ["PurchaseOrderNumber", params.id] ] |
---|
88 | return result |
---|
89 | } |
---|
90 | |
---|
91 | result.purchaseOrderNumber = new PurchaseOrderNumber(params) |
---|
92 | |
---|
93 | if(result.purchaseOrderNumber.hasErrors() || !result.purchaseOrderNumber.save()) |
---|
94 | return fail(code:"default.create.failure") |
---|
95 | |
---|
96 | // Success. |
---|
97 | return result |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | } |
---|