BrightSide Workbench Full Report + Source Code
HandshakeItem.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 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.financials.handshake;
20 
21 import java.io.File;
22 import java.util.Set;
23 import org.turro.string.Strings;
24 import org.turro.command.Command;
25 import org.turro.command.Context;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.elephant.util.DateFormats;
28 import org.turro.elephant.util.Images;
29 import org.turro.financials.contract.ContractCombobox;
30 import org.turro.financials.db.FinancialsPU;
31 import org.turro.financials.entity.Contract;
32 import org.turro.financials.entity.ContractHandshake;
33 import org.turro.financials.entity.Document;
34 import org.turro.financials.entity.DocumentDefinition;
35 import org.turro.financials.entity.DocumentLine;
36 import org.turro.financials.entity.DocumentRelation;
37 import org.turro.financials.entity.Product;
38 import org.turro.financials.entity.ProductByContractor;
39 import org.turro.financials.menu.FinancialsMenu;
40 import org.turro.financials.model.contract.ContractUsualSet;
41 import org.turro.financials.product.ProductCombobox;
42 import org.turro.http.ElephantResponse;
43 import org.turro.i18n.I_;
44 import org.turro.jpa.Dao;
45 import org.turro.zkoss.dialog.InputDialog;
46 import org.turro.zkoss.dialog.InputField;
47 import org.turro.zul.frame.Framework;
48 import org.zkoss.zk.ui.HtmlBasedComponent;
49 
54 public class HandshakeItem {
55 
56  private final File file;
57  private final Object handshake;
58 
59  public HandshakeItem(File file, Object handshake) {
60  this.file = file;
61  this.handshake = handshake;
62  }
63 
64  public File getFile() {
65  return file;
66  }
67 
68  public String getFrom() {
69  if(handshake instanceof HandshakeContract) {
70  HandshakeContract hc = (HandshakeContract) handshake;
71  return hc.petitioner;
72  } else if(handshake instanceof HandshakeDocument) {
73  HandshakeDocument hd = (HandshakeDocument) handshake;
75  return hc.petitioner;
76  } else if(handshake instanceof HandshakeProduct) {
77  HandshakeProduct hp = (HandshakeProduct) handshake;
78  return hp.petitioner;
79  }
80  return null;
81  }
82 
83  public String getDescription() {
84  if(handshake instanceof HandshakeContract) {
85  HandshakeContract hc = (HandshakeContract) handshake;
86  return hc.remoteName;
87  } else if(handshake instanceof HandshakeDocument) {
88  HandshakeDocument hd = (HandshakeDocument) handshake;
89  return hd.documentName + " " + hd.documentNumber + ", " + DateFormats.format(hd.documentDate, true);
90  } else if(handshake instanceof HandshakeProduct) {
91  HandshakeProduct hp = (HandshakeProduct) handshake;
92  return hp.description;
93  }
94  return null;
95  }
96 
98  if(handshake instanceof HandshakeContract) {
99  HandshakeContract hc = (HandshakeContract) handshake;
101  if(contract != null) {
103  } else {
105  }
106  } else if(handshake instanceof HandshakeDocument) {
107  HandshakeDocument hd = (HandshakeDocument) handshake;
109  if(contract != null) {
110  Document document = HandshakeDocument.getDocumentFromHandshake(contract, hd);
111  if(document != null) {
113  } else {
115  }
116  }
117  } else if(handshake instanceof HandshakeProduct) {
118  HandshakeProduct hp = (HandshakeProduct) handshake;
120  if(product != null) {
122  } else {
124  }
125  }
126  return null;
127  }
128 
129  public String getConnectImage() {
130  if(handshake instanceof HandshakeContract) {
131  return Images.getImage("contract");
132  } else if(handshake instanceof HandshakeDocument) {
133  // no connection to document
134  } else if(handshake instanceof HandshakeProduct) {
135  return Images.getImage("product");
136  }
137  return null;
138  }
139 
140  public String getCreateImage() {
141  if(handshake instanceof HandshakeContract) {
142  // no new contract
143  } else if(handshake instanceof HandshakeDocument) {
144  return Images.getImage("document_new");
145  } else if(handshake instanceof HandshakeProduct) {
146  return Images.getImage("product_new");
147  }
148  return null;
149  }
150 
151  public boolean deleteFile() {
152  return file.delete();
153  }
154 
155  public void connect(Command command) {
156  if(handshake instanceof HandshakeContract) {
157  HandshakeContract hc = (HandshakeContract) handshake;
158  connectContract(hc, command);
159  } else if(handshake instanceof HandshakeDocument) {
160  // no connection to document
161  } else if(handshake instanceof HandshakeProduct) {
162  HandshakeProduct hp = (HandshakeProduct) handshake;
163  connectProduct(hp, command);
164  }
165  }
166 
167  private void connectContract(final HandshakeContract hc, final Command command) {
169  Framework.getCurrent().getPage(),
170  I_.get("Connect to..."),
171  new InputField[] {
172  new InputField("Contract", null, null, 0) {
173  @Override
174  protected HtmlBasedComponent createEditor() {
175  ContractCombobox ccb = new ContractCombobox();
176  ccb.setOnlyActive(true);
177  ccb.setDocumentDefinition(DocumentDefinition.getHandshakeReceiver());
178  return ccb;
179  }
180  }
181  }, new Command() {
182  @Override
183  public Object execute(Context context) {
184  InputField[] fields = (InputField[]) context.get("fields");
185  if (fields.length > 0) {
186  for (InputField f : fields) {
187  if ("Contract".equals(f.getLabel())) {
188  Contract contract = (Contract) f.getValue();
189  ContractHandshake ch = new ContractHandshake();
190  ch.setContract(contract);
191  ch.setRemoteId(hc.remoteId);
192  ch.setRemoteName(hc.remoteName);
193  ch.setRemoteServer(hc.remoteServer);
194  contract.setContractHandshake(ch);
195  ElephantResponse er = HandshakeUtil.sendAcceptance(contract, ElephantContext.getServerUrl("http"), hc.remoteId);
196  if(ElephantResponse.isCorrect(er)) {
197  new FinancialsPU().saveObject(contract);
198  if(command != null) command.execute(context);
199  }
200  }
201  }
202  }
203  return null;
204  }
205  });
206  }
207 
208  private void connectProduct(final HandshakeProduct hp, final Command command) {
209  InputDialog.getInput(
210  Framework.getCurrent().getPage(),
211  I_.get("Connect to..."),
212  new InputField[] {
213  new InputField("Product", null, null, 0) {
214  @Override
215  protected HtmlBasedComponent createEditor() {
216  return new ProductCombobox();
217  }
218  }
219  }, new Command() {
220  @Override
221  public Object execute(Context context) {
222  InputField[] fields = (InputField[]) context.get("fields");
223  if (fields.length > 0) {
224  for (InputField f : fields) {
225  if ("Product".equals(f.getLabel())) {
226  Product product = (Product) f.getValue();
227  ProductByContractor pbc = new ProductByContractor();
228  pbc.setContract(hp.contract);
229  pbc.setProduct(product);
230  pbc.setContractorCode(hp.code);
231  pbc.setDescription(hp.description);
232  pbc.setPrice(hp.price);
233  pbc.setTax(hp.tax);
234  new FinancialsPU().saveObject(pbc);
235  if(command != null) command.execute(context);
236  }
237  }
238  }
239  return null;
240  }
241  });
242  }
243 
244  public boolean create() {
245  if(handshake instanceof HandshakeContract) {
246  // no new contract
247  } else if(handshake instanceof HandshakeDocument) {
248  if(createDocument((HandshakeDocument) handshake)) return true;
249  } else if(handshake instanceof HandshakeProduct) {
250  HandshakeProduct hp = (HandshakeProduct) handshake;
251  if(createProduct(hp)) return true;
252  }
253  return false;
254  }
255 
256  private boolean createDocument(HandshakeDocument hsdoc) {
257  Dao dao = new FinancialsPU();
258  Document document = processDocument(dao, hsdoc);
259  if(document != null && !document.isEmpty()) {
260  FinancialsMenu.showDocument(document);
261  return true;
262  }
263  return false;
264  }
265 
266  private Document processDocument(Dao dao, HandshakeDocument hsdoc) {
267  Contract contract = HandshakeContract.getContractFromHandshake(hsdoc.contract);
268  Document document = new Document();
269  document.setContract(contract);
270  document.setDocumentDefinition(dao.find(DocumentDefinition.class, (Long) hsdoc.definitionId));
271  document.setDocumentNumber(hsdoc.documentNumber);
272  document.setDocumentDate(hsdoc.documentDate);
273  document.setReceiptDate(document.getDocumentDate());
274  Set<Contract> usualStores = new ContractUsualSet(document.getStores(), true, document.getUsualPath() + "st:");
275  Contract store = usualStores.iterator().next();
276  if(store == null) {
277  store = document.getDefaultStore();
278  }
279  int count = 1;
280  for(HandshakeDocLine hsdl : hsdoc.lines) {
281  DocumentLine dl = new DocumentLine();
282  dl.setDocument(document);
283  dl.setLineOrder(count++);
284  dl.setConcept(hsdl.concept);
285  dl.setDiscountMoney(hsdl.discountMoney);
286  dl.setDiscountPerCent(hsdl.discountPerCent);
287  dl.setEquivalenceSurcharge(hsdl.equivalenceSurcharge);
288  dl.setPrice(hsdl.price);
289  if(hsdl.product != null) setDocumentLineProduct(dl, hsdl.product);
290  dl.setQuantity(hsdl.quantity);
291  dl.setRetention(hsdl.retention);
292  dl.setTax(hsdl.tax);
293  dl.setLineType(document.getDefaultLineType());
294  dl.setStore(store);
295  document.getDocumentLines().add(dl);
296  }
297  for(HandshakeDocument hsd : hsdoc.expiries) {
298  Document d = processDocument(dao, hsd);
299  if(!d.isEmpty()) {
300  DocumentRelation dr = new DocumentRelation();
301  dr.setAncestor(document);
302  dr.setDescendant(d);
303  document.getDescendants().add(dr);
304  }
305  }
306  return document;
307  }
308 
309  private void setDocumentLineProduct(DocumentLine dl, HandshakeProduct product) {
310  product.contract = dl.getDocument().getContract();
311  ProductByContractor pbc = HandshakeProduct.getProductByContractorFromHandshake(product);
312  if(pbc != null) {
313  dl.setProductByContractor(pbc);
314  dl.setProduct(pbc.getProduct());
315  } else if(!Strings.isBlank(product.description)) {
316  if(Strings.isBlank(dl.getConcept())) {
317  dl.setConcept(product.description);
318  } else {
319  dl.setConcept(product.description + "\n" + dl.getConcept());
320  }
321  }
322  }
323 
324  private boolean createProduct(HandshakeProduct hp) {
325  Dao dao = new FinancialsPU();
326  Product product = new Product();
327  product.setDescription(hp.description);
328  product.setPrice(hp.price);
329  product.setTax(hp.tax);
330  ProductByContractor pbc = new ProductByContractor();
331  pbc.setContract(hp.contract);
332  pbc.setProduct(product);
333  pbc.setContractorCode(hp.code);
334  pbc.setDescription(hp.description);
335  pbc.setPrice(hp.price);
336  pbc.setTax(hp.tax);
337  product.getProductByContractors().add(pbc);
338  product.prepareSave();
339  dao.saveObject(product);
340  return true;
341  }
342 
343 }
static final String format(Date d, boolean dateOnly)
static String getImage(String image)
Definition: Images.java:36
static Contract getContractFromHandshake(HandshakeContract hc)
static Document getDocumentFromHandshake(Contract contract, HandshakeDocument hd)
HandshakeItem(File file, Object handshake)
static Product getProductFromHandshake(HandshakeProduct hp)
static String get(String msg)
Definition: I_.java:41
static void getInput(Page page, String title, String label, Object value, String format, int scale, final Command onOk)
static Framework getCurrent()
Definition: Framework.java:203