def full_delete(enterprise_id): from pvscore.model.crm.customer import Customer company_ids = db.get_list("select company_id from crm_company where enterprise_id = '%s'" % enterprise_id) campaign_ids = db.get_list("""select campaign_id from crm_campaign where company_id in (select company_id from crm_company where enterprise_id = '%s')""" % enterprise_id) customer_ids = db.get_list("""select customer_id from crm_customer where campaign_id in (select campaign_id from crm_campaign where company_id in (select company_id from crm_company where enterprise_id = '%s'))""" % enterprise_id) product_ids = db.get_list("""select product_id from crm_product where company_id in (select company_id from crm_company where enterprise_id = '%s')""" % enterprise_id) # KB: [2013-01-15]: pragma no cover on this because it is not possible to create customers or products on one campaign from another. for cid in customer_ids: #pragma: no cover Customer.full_delete(cid[0]) for pid in product_ids: #pragma: no cover product_id = pid[0] Session.execute("delete from crm_product_return where product_id = '%s'" % product_id) Session.execute("delete from crm_product_category_join where product_id = '%s'" % product_id) Session.execute("delete from crm_product_child where parent_id = '%s'" % product_id) Session.execute("delete from crm_product_child where child_id = '%s'" % product_id) Session.execute("delete from crm_product_pricing where product_id = '%s'" % product_id) Session.execute("delete from crm_product_inventory_journal where product_id = '%s'" % product_id) Session.execute("delete from crm_purchase_order_item where product_id = '%s'" % product_id) Session.execute("delete from crm_order_item where product_id = '%s'" % product_id) Session.execute("delete from crm_product where product_id = '%s'" % product_id) for cid in campaign_ids: campaign_id = cid[0] Session.execute("delete from crm_product_pricing where campaign_id = '%s'" % campaign_id) for cid in company_ids: company_id = cid[0] Session.execute("delete from crm_product_category where company_id = '%s'" % company_id) Session.execute("delete from crm_report where company_id = '%s'" % company_id) Session.execute("""delete from cms_content where site_id in (select site_id from cms_site where company_id = '%s')""" % company_id) Session.execute("delete from cms_page where site_id in (select site_id from cms_site where company_id = '%s')" % company_id) Session.execute("delete from cms_site where company_id = '%s'" % company_id) Session.execute("update crm_company set default_campaign_id = null where company_id = '%s'" % company_id) Session.execute("delete from crm_campaign where company_id = '%s'" % company_id) Session.execute("delete from crm_purchase_order where company_id = '%s'" % company_id) Session.execute("delete from core_asset where enterprise_id = '%s'" % enterprise_id) Session.execute("delete from crm_communication where enterprise_id = '%s'" % enterprise_id) Session.execute("delete from core_status where event_id in (select event_id from core_status_event where enterprise_id = '%s')" % enterprise_id) Session.execute("delete from core_status_event_reason where event_id in (select event_id from core_status_event where enterprise_id = '%s')" % enterprise_id) Session.execute("delete from core_status_event where enterprise_id = '%s'" % enterprise_id) Session.execute("delete from cms_template where enterprise_id = '%s'" % enterprise_id) Session.execute("delete from crm_company where enterprise_id = '%s'" % enterprise_id) #Session.execute('update core_user set enterprise_id = null where enterprise_id = '%s'" % enterprise_id) Session.execute("delete from core_status where username in (select user_id from core_user where enterprise_id = '%s')" % enterprise_id) Session.execute("delete from crm_customer where user_created in (select user_id from core_user where enterprise_id = '%s')" % enterprise_id) Session.execute("delete from crm_customer where user_assigned in (select user_id from core_user where enterprise_id = '%s')" % enterprise_id) Session.execute("delete from core_user where enterprise_id = '%s'" % enterprise_id) Session.execute("delete from crm_discount where enterprise_id = '%s'" % enterprise_id) Session.execute("delete from crm_vendor where enterprise_id = '%s'" % enterprise_id) Session.execute("delete from crm_enterprise where enterprise_id = '%s'" % enterprise_id)
def _remember_customer(request): if 'customer_id' in request.session: request.ctx.customer = Customer.load(request.session['customer_id']) elif 'customer_id' in request.params: request.ctx.customer = Customer.load(request.params.get('customer_id')) if request.ctx.customer: if request.ctx.customer.campaign.company.enterprise_id != request.ctx.enterprise.enterprise_id: raise HTTPForbidden() #pragma: no cover
def customer_login(self): """ KB: [2012-09-24]: Log this guy in and redirect him to the location specified in the POST """ uid = self.request.POST.get('username') pwd = self.request.POST.get('password') if uid and pwd and Customer.authenticate(uid, pwd, self.request.ctx.site.company): self.session['username'] = uid cust = Customer.find_by_company(uid, self.request.ctx.site.company) self.session['customer_id'] = cust.customer_id return self.find_redirect() else: self.flash('Invalid User or Password') return self.raise_redirect(self.request.referrer)
def status_dialog(self): customer_id = self.request.matchdict.get('customer_id') customer = Customer.load(customer_id) self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id) order = order_item = events = redir = None if self.request.GET.get('order_id'): order = CustomerOrder.load(self.request.GET.get('order_id')) self.forbid_if(not order or order.campaign.company.enterprise_id != self.enterprise_id) events = util.select_list(StatusEvent.find_all_applicable(self.enterprise_id, order), 'event_id', 'display_name', True) redir = '/crm/customer/show_orders/%s' % customer_id elif self.request.GET.get('order_item_id'): order_item = OrderItem.load(self.request.GET.get('order_item_id')) self.forbid_if(not order_item or order_item.order.campaign.company.enterprise_id != self.enterprise_id) events = util.select_list(StatusEvent.find_all_applicable(self.enterprise_id, order_item), 'event_id', 'display_name', True) redir = '/crm/customer/edit_order_dialog/%s/%s' % (customer_id, order_item.order_id) else: events = util.select_list(StatusEvent.find_all_applicable(self.enterprise_id, customer), 'event_id', 'display_name', True) redir = '/crm/customer/edit/%s' % customer_id return { 'customer' : customer, 'order' : order, 'order_item' : order_item, 'events' : events, 'redir' : redir }
def _edit_impl(self): customer_id = self.request.matchdict.get('customer_id') customer = None if customer_id: customer = Customer.load(customer_id) self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id) self._add_to_recent(customer) else: customer = Customer() customer.campaign = self.request.ctx.site.company.default_campaign return { 'customer' : customer, 'users' : util.select_list(Users.find_all(self.enterprise_id), 'user_id', ['fname', 'lname'], True), 'phases' : util.select_list(CustomerPhase.find_all(self.enterprise_id), 'phase_id', 'display_name', True), 'campaigns' : util.select_list(Campaign.find_all(self.enterprise_id), 'campaign_id', 'name') }
def show_appointments(self): customer_id = self.request.matchdict.get('customer_id') customer = Customer.load(customer_id) return { 'customer' : customer, 'appointments' : Appointment.find_by_customer(customer) }
def _prep_add_order_dialog(self, customer_id): customer = Customer.load(customer_id) self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id) products = Product.find_by_campaign(customer.campaign) return { 'customer' : customer, 'products' : products }
def show_orders(self): customer_id = self.request.matchdict.get('customer_id') customer = Customer.load(customer_id) self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id) return { 'customer' : customer, 'orders' : [order for order in customer.orders if order.delete_dt is None and order.cancel_dt is None] }
def test_create_new(self): listing_id = self._create_new() cust = Customer.find(TEST_CUSTOMER_EMAIL, Campaign.load(self.site.default_campaign_id)) listings = Listing.find_by_customer(cust) assert len(listings) == 1 assert str(listings[0].listing_id) == listing_id Listing.find_last_n_assets(10) self._delete_new(listing_id)
def delete(self): customer_id = self.request.matchdict.get('customer_id') customer = Customer.load(customer_id) self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id) customer.mod_dt = util.now() customer.delete_dt = util.now() Status.add(customer, customer, StatusEvent.find(self.enterprise_id, 'Customer', 'DELETED'), 'Customer Deleted') return 'True'
def show_attributes(self): customer_id = self.request.matchdict.get('customer_id') customer = Customer.load(customer_id) self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id) return { 'customer' : customer, 'attrs' : customer.get_attrs() }
def show_history(self): customer_id = self.request.matchdict.get('customer_id') customer = Customer.load(customer_id) self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id) return { 'customer' : customer, 'history' : Status.find_by_customer(customer, self.offset), 'offset' : self.offset }
def show_billings(self): customer_id = self.request.matchdict.get('customer_id') customer = Customer.load(customer_id) self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id) return { 'customer' : customer, 'billings' : Journal.find_all_by_customer(customer, self.offset), 'offset' : self.offset }
def show_status_dialog(self): customer_id = self.request.matchdict.get('customer_id') status_id = self.request.matchdict.get('status_id') customer = Customer.load(customer_id) self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id) status = Status.load(status_id) self.forbid_if(not status or str(customer.customer_id) != str(customer_id)) return { 'customer' : customer, 'status' : status }
def apply_payment(self): customer_id = self.request.matchdict.get('customer_id') order_id = self.request.matchdict.get('order_id') if 'bill_cc_token' in self.request.POST and self.request.POST['bill_cc_token']: cust = Customer.load(customer_id) order = CustomerOrder.load(order_id) bill = self._create_billing(cust) self._bill_credit_card(cust, order, bill) else: self._apply_payment(customer_id, order_id) return HTTPFound('/crm/customer/edit_order_dialog/%s/%s' % (customer_id, order_id))
def cancel_order_dialog(self): customer_id = self.request.matchdict.get('customer_id') order_id = self.request.matchdict.get('order_id') customer = Customer.load(customer_id) self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id) order = CustomerOrder.load(order_id) self.forbid_if(not order or order.customer_id != customer.customer_id) return { 'customer' : customer, 'order' : order }
def show_billing_dialog(self): customer_id = self.request.matchdict.get('customer_id') journal_id = self.request.matchdict.get('journal_id') customer = Customer.load(customer_id) self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id) journal = Journal.load(journal_id) self.forbid_if(not journal or str(journal.customer_id) != str(customer_id)) return { 'customer' : customer, 'journal' : journal }
def test_customer_forgot_password(self): R = self.post('/crm/customer_forgot_password', {'username': TEST_CUSTOMER_EMAIL}) assert R.status_int == 200 assert "Your new password has been sent" in R.body custs = Customer.find_all_by_email(TEST_CUSTOMER_EMAIL) assert len(custs) > 0 cust = custs[0] assert cust.password != TEST_CUSTOMER_PASSWORD cust.password = TEST_CUSTOMER_PASSWORD cust.save() self.commit()
def return_item_dialog(self): customer_id = self.request.matchdict.get('customer_id') order_id = self.request.matchdict.get('order_id') order_item_id = self.request.matchdict.get('order_item_id') customer = Customer.load(customer_id) self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id) order = customer.get_order(order_id) self.forbid_if(not order) order_item = OrderItem.load(order_item_id) self.forbid_if(not order_item or str(order_item.order.order_id) != str(order.order_id)) return { 'customer' : customer, 'order' : order, 'order_item' : order_item }
def customer_login_to_link(self, ): """ KB: [2011-06-28]: http://ww.wealthmakers.com/crm/customer_login_to_link/fdf774eb58feefd35fc2abab7db194e8/http%3A%7C%7Cww.wealthmakers.com%7Cireport.html%3Firid%3D13235 http://healthyustore.net/crm/customer_login_to_link/47d66cae-7e0f-4111-bd54-7296dc92cbde/http%3A%7C%7Chealthyustore.net """ key = self.request.matchdict.get('key') link = self.request.matchdict.get('link') cust = Customer.find_by_key(key) if cust: self.session['username'] = cust.email self.session['customer_id'] = cust.customer_id url = link.replace('|', '/') log.info('customer %s (%s) login to %s' % (cust.customer_id, cust.email, url)) return HTTPFound(url) else: #pragma: no cover self.flash('Invalid User or Password') return self.find_redirect('/')
def return_item(self): customer_id = self.request.matchdict.get('customer_id') order_id = self.request.matchdict.get('order_id') order_item_id = self.request.matchdict.get('order_item_id') customer = Customer.load(customer_id) self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id) order = customer.get_order(order_id) self.forbid_if(not order) order_item = OrderItem.load(order_item_id) self.forbid_if(not order_item or str(order_item.order.order_id) != str(order.order_id)) user = self.request.ctx.user return_type = self.request.POST.get('rt_refund_type') quantity_returned = float(self.request.POST.get('quantity_returned')) credit_amount = float(self.request.POST.get('credit_amount')) jrnl = Journal.create_new(credit_amount, customer, order, user, return_type) ret = ProductReturn.create_new(order_item.product, order_item.order, quantity_returned, credit_amount, jrnl, user) status_note = "'%s' returned. $%.2f refunded by %s" % (order_item.product.name, credit_amount, return_type) Status.add(customer, order_item, Status.find_event(self.enterprise_id, order_item, 'RETURN'), status_note) order_item.quantity -= quantity_returned if order_item.quantity == 0: order_item.delete_dt = util.today() order_item.save() if order_item.product.track_inventory: InventoryJournal.create_new(order_item.product, 'Return', quantity_returned, order_item, None, None, ret) for attr_kid in order_item.children: Status.add(customer, attr_kid, Status.find_event(self.enterprise_id, attr_kid, 'RETURN'), status_note) attr_kid_prod = attr_kid.product if attr_kid_prod.track_inventory: InventoryJournal.create_new(attr_kid_prod, 'Return', quantity_returned, attr_kid) self.flash(status_note) if len(order.active_items) == 0: # KB: [2012-09-06]: Deleted the one thing out of this # order. Kill the order status_note = 'Only item in order returned. Order cancelled.' self._cancel_order_impl(order_id, status_note, False) self.flash(status_note) ret = HTTPFound('/crm/customer/show_orders/%s' % customer_id) else: ret = HTTPFound('/crm/customer/edit_order_dialog/%s/%s' % (customer_id, order_id)) customer.invalidate_caches() return ret
def edit_order_dialog(self): customer_id = self.request.matchdict.get('customer_id') order_id = self.request.matchdict.get('order_id') customer = Customer.load(customer_id) self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id) order = customer.get_order(order_id) return { 'customer' : customer, 'order' : order, 'comm_packing_slip_id' : order.campaign.comm_packing_slip_id, 'total_price' : order.total_price(), 'total_item_price' : order.total_item_price(), 'total_handling_price' : order.total_handling_price(), 'total_shipping_price' : order.total_shipping_price(), 'total_payments_applied' : order.total_payments_applied(), 'total_discounts_applied' : order.total_discounts_applied(), 'total_due' : order.total_payments_due() }
def customer_forgot_password(self): """ KB: [2011-03-13]: Try to be at least a little sneaky. Don't give any hints as to valid user accounts, etc. If we don't find that email address then just redir back to /. """ uid = self.request.params['username'] cust = Customer.find_by_company(uid, self.request.ctx.site.company) if not cust: self.flash('No user %s on file. Please create a new account.' % uid) raise HTTPFound(self.request.referrer if self.request.referrer else '/') # reset the customer's password to something random. cust.password = '******' % (chr(random.randint(65, 90)), chr(random.randint(97, 122)), str(random.randint(100000, 999999))) cust.save() self.request.ctx.campaign.send_forgot_password_comm(cust) self.flash('Your new password has been sent to the email address you provided.') return self.find_redirect()
def exclog_tween(request, get_logger=logging.getLogger): # getLogger injected for testing purposes try: return handler(request) except ignored: raise except: logger = get_logger('exc_logger') ent = Enterprise.load(request.session['enterprise_id']) if 'enterprise_id' in request.session else None cust = Customer.load(request.session['customer_id']) if 'customer_id' in request.session else None user = Users.load(request.session['user_id']) if 'user_id' in request.session else None if extra_info: message = dedent("""\n %(url)s ENTERPRISE: %(ent)s CUSTOMER: %(cust)s USER: %(user)s SESSION %(sess)s ENVIRONMENT %(env)s PARAMETERS %(params)s """ % dict(url=request.url, sess=pformat(request.session.items()), ent ="%s : %s" % (ent.enterprise_id, ent.name) if ent else None, cust="%s : %s" % (cust.customer_id, cust.email) if cust else None, user="******" % (user.user_id, user.email) if user else None, env=pformat(request.environ), params=pformat(request.params))) else: message = request.url logger.exception(message) raise
def _edit_impl(self): appointment_id = self.request.matchdict.get('appointment_id') customer_id = self.request.matchdict.get('customer_id') if appointment_id: appointment = Appointment.load(appointment_id) self.forbid_if(not appointment) else: appointment = Appointment() hours = util.hours_list() customer = None customer = Customer.load(customer_id) self.forbid_if(customer and customer.campaign.company.enterprise_id != self.enterprise_id) appointment.customer_id = customer_id return { 'today' : util.today_date(), 'tomorrow' : util.today_date() + datetime.timedelta(days=1), 'customer' : customer, 'appointment' : appointment, 'timezones' : country_timezones('US'), 'hours' : hours }
def apply_payment_dialog(self): customer_id = self.request.matchdict.get('customer_id') order_id = self.request.matchdict.get('order_id') customer = Customer.load(customer_id) self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id) order = customer.get_order(order_id) self.forbid_if(not order) total_due = order.total_payments_due() pre_order_balance = customer.get_current_balance() return { 'customer' : customer, 'order' : order, 'total_price' : order.total_price(), 'payment_methods' : Journal.get_payment_methods(self.request.ctx.enterprise), 'total_payments_applied' : order.total_payments_applied(), 'total_discounts_applied' : order.total_discounts_applied(), 'total_due' : total_due, 'pre_order_balance' : pre_order_balance, 'enterprise' : self.request.ctx.enterprise, 'total_due_after_balance' : total_due+pre_order_balance if (total_due+pre_order_balance) > 0 else 0 }
def _apply_payment(self, customer_id, order_id, pmt_amount=None, pmt_method=None, pmt_note=None): #pylint: disable-msg=R0913 """ KB: [2011-03-09]: Check that everything is kosher Create a journal entry for the order for the amount and type specified in the UI Create a status noting the type and amount of the payment applied. """ customer = Customer.load(customer_id) self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id) order = customer.get_order(order_id) self.forbid_if(not order) user = self.request.ctx.user current_customer_balance = customer.get_current_balance() #prior_payments_applied = order.total_payments_applied() prior_total_due = order.total_payments_due() balance_amount_to_apply = float(self.request.POST.get('pmt_balance_amount_to_apply', 0.0)) amt = float(util.nvl(pmt_amount, self.request.POST.get('pmt_amount'))) method = util.nvl(pmt_method, self.request.POST.get('pmt_method')) note = util.nvl(pmt_note, self.request.POST.get('pmt_note')) self.forbid_if(round(amt + balance_amount_to_apply, 2) > round(prior_total_due, 2), "amt + balance_amount_to_apply > prior_total_due") self.forbid_if(current_customer_balance > 0 and round(balance_amount_to_apply, 2) > round(current_customer_balance, 2), "balance_amount_to_apply > current_customer_balance") pmt_type = 'PartialPayment' if amt == prior_total_due: pmt_type = 'FullPayment' Journal.create_new(amt, customer, order, user, pmt_type, method, note) status_note = '%s applied: $%s' % (pmt_type, util.money(amt)) Status.add(customer, order, Status.find_event(self.enterprise_id, order, 'PAYMENT_APPLIED'), status_note) self.flash(status_note) if balance_amount_to_apply > 0: Journal.create_new(balance_amount_to_apply, customer, order, user, 'CreditDecrease') status_note = '%s applied: $%s' % ('CreditDecrease', util.money(balance_amount_to_apply)) Status.add(customer, order, Status.find_event(self.enterprise_id, order, 'PAYMENT_APPLIED'), status_note) self.flash(status_note) customer.invalidate_caches() customer.flush() return 'True'
def contact(self): camp = self.request.ctx.campaign message = self.request.POST.get('message') email = self.request.POST.get('email') msg = "%s %s<br>(%s)<br><br>%s<br><br>%s" % (self.request.POST.get('fname'), self.request.POST.get('lname'), email, self.request.POST.get('phone'), message) if util.nvl(self.request.POST.get('save')): cust = Customer.find(email, camp) if not cust: cust = Customer() cust.campaign = camp cust.bind(self.request.POST) cust.phone = cust.phone[:20] if cust.phone else None # prevents people from putting in "904-716-7487 (mobile)" and it barfs cust.save() Status.add(cust, cust, Status.find_event(self.enterprise_id, cust, 'NOTE'), 'NOTE FROM CUSTOMER\n%s' % message) email_info = camp.get_email_info() mail = UserMail(camp) mail.send(email_info.email, 'SITE CONTACT FORM %s' % self.request.host, msg) return self.find_redirect()
def search(self): ret = { 'company_name' : None, 'fname' : None, 'lname' : None, 'email' : None, 'phone' : None, 'customers' : None, 'user_assigned' : None, 'users' : util.select_list(Users.find_all(self.enterprise_id), 'user_id', ['fname', 'lname'], True) } ret['company_name'] = self.request.POST.get('company_name', self.request.GET.get('company_name')) ret['fname'] = self.request.POST.get('fname', self.request.GET.get('fname')) ret['lname'] = self.request.POST.get('lname', self.request.GET.get('lname')) ret['email'] = self.request.POST.get('email', self.request.GET.get('email')) ret['phone'] = self.request.POST.get('phone', self.request.GET.get('phone')) ret['user_assigned'] = self.request.POST.get('user_assigned', self.request.GET.get('user_assigned')) ret['customers'] = Customer.search(self.enterprise_id, ret['company_name'], ret['fname'], ret['lname'], ret['email'], ret['phone'], ret['user_assigned']) if 'customers' in ret and len(ret['customers']) == 1: ret = HTTPFound('/crm/customer/edit/%s' % ret['customers'][0].customer_id) return ret
def save_status(self): customer_id = self.request.matchdict.get('customer_id') customer = Customer.load(customer_id) self.forbid_if(not customer or customer.campaign.company.enterprise_id != self.enterprise_id) event = StatusEvent.load(self.request.POST.get('event_id')) self.forbid_if(not event or not self.request.POST.get('event_id') or (not event.is_system and event.enterprise_id is not None and event.enterprise_id != self.enterprise_id)) order = None note = self.request.POST.get('note') if self.request.POST.get('order_id'): order = CustomerOrder.load(self.request.POST.get('order_id')) self.forbid_if(not order or order.campaign.company.enterprise_id != self.enterprise_id) Status.add(customer, order, event, note, self.request.ctx.user) self.flash('Statused Order to %s' % event.display_name) elif self.request.POST.get('order_item_id'): order_item = OrderItem.load(self.request.POST.get('order_item_id')) self.forbid_if(not order_item or order_item.order.campaign.company.enterprise_id != self.enterprise_id) Status.add(customer, order_item, event, note, self.request.ctx.user) self.flash('Statused Item to %s' % event.display_name) else: Status.add(customer, customer, event, note, self.request.ctx.user) self.flash('Statused Customer to %s' % event.display_name) customer.invalidate_caches() return self.find_redirect()