BrightSide Workbench Full Report + Source Code
ProspectStageWrapper.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.crm.zul.sale;
20 
21 import java.util.ArrayList;
22 import java.util.Date;
23 import java.util.EnumSet;
24 import org.turro.crm.db.CrmPU;
25 import org.turro.crm.entity.ProspectStage;
26 import org.turro.crm.entity.ProspectType;
27 import org.turro.crm.entity.SaleAction;
28 import org.turro.crm.entity.SaleProspect;
29 import org.turro.crm.entity.TouchPoint;
30 import org.turro.elephant.util.Messages;
31 import org.turro.i18n.I_;
32 import org.turro.jpa.Dao;
33 import org.turro.status.Status;
34 
39 public class ProspectStageWrapper extends Status<ProspectStage> {
40 
41  private SaleProspect saleProspect;
42 
43  public ProspectStageWrapper(SaleProspect saleProspect, Date now) {
44  super(saleProspect.getStage());
45  this.saleProspect = saleProspect;
46  checkStatus(now);
47  }
48 
49  @Override
50  protected ProspectStage doCheckStatus(Date now) {
51  ProspectStage stage = current;
52  for(SaleAction sa : saleProspect.getSaleActions()) {
53  TouchPoint tp = sa.getEndingTouchPoint();
54  if(tp == null) {
55  // nothing
56  } else if(tp.isPositive()) {
57  stage = ProspectStage.CLOSED_WON;
58  } else if(!tp.isPositive()) {
59  stage = ProspectStage.CLOSED_LOST;
60  }
61  }
62  return stage;
63  }
64 
65 
66  @Override
67  protected ProspectStage doChangeTo(final ProspectStage newStage) {
68  final ArrayList<SaleAction> saleActions = new ArrayList<SaleAction>();
69  final Dao dao = new CrmPU();
70  boolean shouldSave;
71  for(SaleAction sa : saleProspect.getSaleActions()) {
72  shouldSave = false;
73  TouchPoint tp = sa.getEndingTouchPoint();
74  if(tp == null) {
75 
76  } else if(tp.isPositive()) {
77  if(newStage.equals(ProspectStage.CLOSED_LOST)) {
78  sa.setEndingTouchPoint(null);
79  shouldSave = true;
80  }
81  } else if(!tp.isPositive()) {
82  if(newStage.equals(ProspectStage.CLOSED_WON)) {
83  sa.setEndingTouchPoint(null);
84  shouldSave = true;
85  }
86  }
87  if(EnumSet.of(ProspectStage.CLOSED_WON,
88  ProspectStage.CLOSED_LOST).contains(newStage)) {
89  if(sa.getStatus() == SaleAction.SA_PENDING) {
90  sa.setStatus(SaleAction.SA_CANCELLED);
91  shouldSave = true;
92  }
93  }
94  if(shouldSave) {
95  saleActions.add(sa);
96  }
97  }
98  if(!saleActions.isEmpty()) {
99  Messages.confirmProcess().paragraph().add(I_.get("Affected") + ": ")
100  .add(saleActions.size() + " " + I_.get("Sale actions")).show(() -> {
101  for(SaleAction sa : saleActions) {
102  dao.saveObject(sa);
103  }
104  saleProspect.setStage(newStage);
105  });
106  } else {
107  saleProspect.setStage(newStage);
108  }
109  return saleProspect.getStage();
110  }
111 
112  @Override
113  public boolean canChangeTo(ProspectStage newStage) {
114  if(ProspectType.TRACING.equals(saleProspect.getType())) return false;
115  if(current.equals(ProspectStage.STARTED)) {
116  return EnumSet.of(
120  ProspectStage.CLOSED_LOST).contains(newStage);
121  } else if(current.equals(ProspectStage.BUDGETED)) {
122  return EnumSet.of(
125  ProspectStage.CLOSED_LOST).contains(newStage);
126  } else if(current.equals(ProspectStage.NEGOTIATING)) {
127  return EnumSet.of(
129  ProspectStage.CLOSED_LOST).contains(newStage);
130  }
131  return false;
132  }
133 
134 }
void setStage(ProspectStage stage)
ProspectStageWrapper(SaleProspect saleProspect, Date now)
ProspectStage doChangeTo(final ProspectStage newStage)
static Messages confirmProcess()
Definition: Messages.java:95
Messages add(String word)
Definition: Messages.java:50
static String get(String msg)
Definition: I_.java:41