19 package org.turro.financials.cart.www;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.util.Collection;
25 import java.util.Currency;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import org.turro.string.Strings;
29 import org.apache.commons.lang3.LocaleUtils;
30 import org.apache.commons.mail.EmailException;
31 import org.turro.action.Contacts;
32 import org.turro.action.MailAttachment;
33 import org.turro.action.MailSenders;
34 import org.turro.attach.search.AttachCollection;
35 import org.turro.auth.Authentication;
36 import org.turro.contacts.Address;
37 import org.turro.contacts.Contact;
38 import org.turro.contacts.form.ContactWrapper;
39 import org.turro.elephant.context.ElephantContext;
40 import org.turro.elephant.context.IConstructor;
41 import org.turro.elephant.impl.util.StringParser;
42 import org.turro.elephant.security.IUser;
43 import org.turro.financials.cart.Cart;
44 import org.turro.financials.cart.CartItem;
45 import org.turro.financials.cart.RedsysPOS;
46 import org.turro.financials.cart.ShopContext;
47 import org.turro.financials.cart.ShoppingCart;
48 import org.turro.financials.cart.delivery.DeliveryContexts;
49 import org.turro.financials.db.FinancialsPU;
50 import org.turro.financials.entity.Document;
51 import org.turro.financials.entity.DocumentLine;
52 import org.turro.financials.entity.Product;
53 import org.turro.financials.model.document.GenerateDocument;
54 import org.turro.jpa.Dao;
55 import org.turro.log.SystemLogType;
56 import org.turro.log.SystemLogger;
57 import org.turro.marker.ElephantMarker;
58 import org.turro.plugin.contacts.IContact;
59 import org.turro.util.PhraseBuilder;
69 private final Cart cart;
72 this.constructor = constructor;
80 marker.
put(
"contact", contact);
81 marker.
put(
"checkout",
this);
82 marker.
put(
"cart", cart);
89 marker.
process(
"cart",
"confirm");
95 marker.
process(
"cart",
"delivery");
99 marker.
process(
"cart",
"billing");
105 if(delivery !=
null && !delivery.
isEmpty()) {
108 if(product !=
null) {
112 if(product !=
null) {
116 if(product ==
null) {
118 marker.
process(
"cart",
"outofscope");
122 marker.
put(
"address", delivery);
132 marker.
process(
"cart",
"self-test");
136 marker.
process(
"cart",
"proceed");
151 private void checkCheckout() {
164 sendDocumentToLogistic(doc);
165 sendDocumentToCustomer(doc);
168 sendCartToAdmin(notifCart);
170 }
catch(Exception ex) {
172 MailSenders.getPool()
174 .send(ElephantContext.getSiteName() +
":SHOP-MAIL-NOT-SENT",
"Problems while sending mail");
178 MailSenders.getPool()
180 .send(ElephantContext.getSiteName() +
":SHOP-DESERIALIZE",
"Deserializing");
182 }
else if(contact !=
null && contact.
isWebUser()) {
183 Contact c = getContact(contact);
184 Address d = getAddress(ShopContext.getInstance().getDelivery());
185 Address f = getAddress(ShopContext.getInstance().getFiscal());
186 boolean needSave =
false;
190 if(!Strings.isBlank(gid)) {
191 c.setGlobalIdentifier(gid);
195 if(!Strings.isBlank(dstreet)) {
199 c.getAddresses().add(d);
200 d.setDescription(ShopContext.getInstance().getDelivery());
201 d.setOnlyOwner(
false);
202 d.setOwner(contact.
getId());
204 d.setStreet(dstreet);
208 if(!Strings.isBlank(dcity)) {
214 if(!Strings.isBlank(dzipcode)) {
216 d.setZipCode(dzipcode);
219 String dprovince = constructor.
getParameter(
"dprovince");
220 if(!Strings.isBlank(dprovince)) {
222 d.setProvince(dprovince);
226 if(!Strings.isBlank(dvalue)) {
230 if(!Strings.isBlank(mobile)) {
231 c.getIContact().setConnector(ShopContext.getInstance().getContactPhone(), mobile);
235 if(!Strings.isBlank(fstreet)) {
239 c.getAddresses().add(f);
240 f.setDescription(ShopContext.getInstance().getFiscal());
241 f.setOnlyOwner(
false);
242 f.setOwner(contact.
getId());
244 f.setStreet(fstreet);
248 if(!Strings.isBlank(fcity)) {
254 if(!Strings.isBlank(fzipcode)) {
256 f.setZipCode(fzipcode);
259 String fprovince = constructor.
getParameter(
"fprovince");
260 if(!Strings.isBlank(fprovince)) {
262 f.setProvince(fprovince);
266 c.setLocale(LocaleUtils.toLocale(ShopContext.getInstance().getLocale()));
267 c.setCurrency(Currency.getInstance(ShopContext.getInstance().getCurrency()));
268 contact.
setContact(
new ContactWrapper(c).save());
279 int max = checkMaximumStatus();
287 }
else if(
"1".equals(constructor.
getParameter(
"prevStep"))) {
297 private int checkMaximumStatus() {
298 if(contact ==
null || !contact.
isWebUser() || cart ==
null || cart.
isEmpty()) {
302 Address delivery = getAddress(ShopContext.getInstance().getDelivery());
303 if(!checkValidAddress(delivery)) {
308 Address fiscal = getAddress(ShopContext.getInstance().getFiscal());
309 if(Strings.isBlank(contact.
getGlobalId()) || !checkValidAddress(fiscal)) {
316 private Address getAddress(String name) {
317 Address address =
null;
318 if(contact !=
null && contact.
isValid()) {
319 Contact c = getContact(contact);
320 if(Strings.isBlank(name)) {
321 address = c.getAddressMap().get(ShopContext.getInstance().getDelivery());
322 if(address ==
null) {
323 address = c.getAddressMap().get(ShopContext.getInstance().getFiscal());
326 address = c.getAddressMap().get(name);
334 private Dao getDao() {
336 _dao =
new FinancialsPU();
341 private Contact getContact(IContact contact) {
342 return (Contact) contact.getContact();
345 private boolean checkValidAddress(Address address) {
346 return address !=
null && !address.isEmpty() && !Strings.isBlank(address.getCity()) && !Strings.isBlank(address.getZipCode());
349 private void sendCartToAdmin(Cart notifCart) {
350 StringBuilder sb =
new StringBuilder();
351 sb.append(notifCart.getContact().getName())
353 .append(notifCart.getAmounts().getTotal())
355 .append(notifCart.getContact()
356 .getConnector(ShopContext.getInstance().getContactPhone()))
358 for(CartItem ci : cart.
getItems()) {
360 .append(ci.getQuantity())
362 .append(ci.getProduct().getProductCode())
364 .append(ci.getProduct().getDescription())
366 .append(ci.getPrice())
368 .append(ci.getAmount());
370 MailSenders.getPool()
372 .send(ElephantContext.getSiteName() +
":SHOP-ERROR", sb.toString());
375 private void sendDocumentToLogistic(Document doc) {
376 StringBuilder sb =
new StringBuilder();
377 sb.append(doc.getContract().getFullDescription())
379 .append(doc.getDocumentString())
381 .append(doc.getAmounts().getTotal())
383 .append(doc.getContract().getIContractor()
384 .getConnector(ShopContext.getInstance().getContactPhone()))
386 for(DocumentLine di : doc.getDocumentLines()) {
388 .append(di.getQuantity())
390 .append(di.getProduct().getProductCode())
392 .append(di.getProduct().getDescription())
394 .append(di.getPrice())
396 .append(di.getAmount());
398 sb.append(doc.getNotes());
399 MailSenders.getPool()
400 .addUser(
"Logistic", ShopContext.getInstance().getLogistic())
401 .send(ElephantContext.getSiteName() +
":SHOP", sb.toString());
404 private void sendDocumentToCustomer(Document doc)
throws EmailException {
405 Collection<IContact> emails = doc.getContract().getIParticipantsEmail();
407 String mailString = getMailString(emails);
408 if(!Strings.isBlank(mailString)) {
410 AttachCollection amc =
new AttachCollection();
411 amc.loadAttachments(FinancialsPU.getObjectPath(doc),
true);
412 amc.loadAttachments(FinancialsPU.getObjectPath(doc.getContract()) +
"/Mail/" + doc.getDocumentDefinition().getName(),
true);
413 Collection<MailAttachment> attachments = amc.getAttachments();
414 File attach = File.createTempFile(
"attach_",
"_mail");
415 FileOutputStream fos =
new FileOutputStream(attach);
416 GenerateDocument gd =
new GenerateDocument();
417 gd.outputDocument(doc,
true, fos);
418 MailAttachment ma =
new MailAttachment(attach.getAbsolutePath(), doc.getDocDescription() +
".pdf",
null);
419 ma.attachment = attach;
421 MailSenders.getPool()
422 .setRoot(
"/financials")
424 .addMailAttachments(attachments)
426 SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, doc,
"sent", mailString);
427 amc.removeAttachments();
429 .put(
"comment", StringParser.toHTML(doc.getFullNotes()))
430 .put(
"title", doc.getDocDescription())
431 .put(
"mas", attachments)
432 .sendTemplate(
"document", ElephantContext.getSiteName() +
": " + doc.getDocDescription());
433 }
catch (IOException ex) {
434 Logger.getLogger(
Checkout.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(mailString), ex);
440 private String getMailString(Collection<IContact> iParticipantsEmail) {
441 PhraseBuilder pb =
new PhraseBuilder();
442 for(IContact c : iParticipantsEmail) {
443 String email = c.getConnector(IUser.CONNECTOR_EMAIL);
444 if(Contacts.isValidEmail(email)) {
445 pb.addWord(c.getName() +
": " + email);
446 pb.addPendingSeparator(
",");
449 return pb.toString();
static IContact getIContact()
static String getServerUrl(String scheme)
static String getRootWebPath()
String getDeliveryValue()
static Cart deserializeForOrder(String order)
void setDeliveryValue(String deliveryValue)
void setStatus(int status)
boolean addDeliveryItem(long productId, double quantity, double tax, double price)
void setPickup(boolean pickup)
void setWantBill(boolean wantBill)
void removeDeliveryItem()
void serializeForOrder(IContact contact)
boolean isOnlineNotification(IConstructor constructor)
void addParameters(ElephantMarker marker, double amount, String order)
Cart isAccepted(IConstructor constructor)
DeliveryContexts getDeliveryContexts()
boolean isSelfTestScope()
String getNotificationPath()
static ShopContext getInstance()
Product getDeliveryProduct(Address address)
Checkout(IConstructor constructor)
String getUrlForCheckout()
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)
String getParameter(String param)