Skip to content

Commit

Permalink
💣 Added packages
Browse files Browse the repository at this point in the history
Signed-off-by: Vildan Safin <[email protected]>
  • Loading branch information
Enigma228322 committed May 2, 2020
1 parent 06040ad commit 67ab586
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 16 deletions.
32 changes: 27 additions & 5 deletions saas_apps/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ class SaaSAppsController(Controller):
@route('/price', type='http', auth='public', website=True)
def user_page(self, **kw):
apps = request.env['saas.line'].sudo()
packages = request.env['saas.template'].sudo()
# apps.delete_app_duplicates()
if not apps.search_count([]):
apps.refresh_lines()
return request.render('saas_apps.index', {
'apps': apps.search([('allow_to_sell', '=', True)])
'apps': apps.search([('allow_to_sell', '=', True)]),
'packages': packages.search([('set_as_package', '=', True)])
})

@route(['/refresh'], type='json', auth='public')
Expand All @@ -36,7 +38,7 @@ def search_incoming_app_dependencies(self, **kw):
}

@route(['/check_currency'], type='json', auth='public')
def what_company_cuurency_to_use(self, **kw):
def what_company_curency_to_use(self, **kw):
apps = request.env['saas.line'].sudo()
return {
'currency': apps.search([])[0].currency_id.display_name,
Expand Down Expand Up @@ -87,12 +89,26 @@ def take_product_ids(self, **kw):
modules = request.env['saas.line'].sudo()
apps_product_ids = []
apps = modules.search([('name', 'in', module_names), ('application', '=', True)])
for app in apps:
apps_product_ids.append(app.product_id.id)
templates = request.env['saas.template'].search([('name', 'in', module_names)])
for app in apps.product_id + templates.product_id:
apps_product_ids.append(app.id)

return {
'ids': apps_product_ids
}

@route(['/price/get_package_modules'], type='json', auth='public')
def take_product_ids(self, **kw):
packages = kw.get('packages', [])
templates = request.env['saas.template'].search([('name', 'in', packages)])
modules = []
for template in templates:
for module in template.template_module_ids:
modules.append(module.name)
return {
'modules': modules
}


class SaasAppsCart(WebsiteSale):

Expand All @@ -105,9 +121,12 @@ def cart_update(self, **kw):
# Adding user as product in cart
user_product = request.env.ref("saas_apps.product_user").sudo()
user_product.price = kw.get('user_price')
old_user_cnt = kw.get('old_user_cnt')
if not old_user_cnt:
old_user_cnt = '0'
sale_order._cart_update(
product_id=int(user_product.id),
add_qty=(float(kw.get('user_cnt')) - float(kw.get('old_user_cnt')))
add_qty=(float(kw.get('user_cnt')) - float(old_user_cnt))
)

# Delete old products from cart
Expand All @@ -123,10 +142,13 @@ def cart_update(self, **kw):
for id in product_ids:
product = pr_pr.browse(id)
app = request.env['saas.line'].sudo().search([('module_name', '=', product.name)])
packages = request.env['saas.template'].sudo().search([('name', '=', product.name)])
if period == 'm':
app.change_product_price(app, app.month_price)
packages.change_product_price(packages, packages.month_price)
else:
app.change_product_price(app, app.year_price)
packages.change_product_price(packages, packages.year_price)

# Add new ones
sale_order = request.website.sale_get_order(force_create=True)
Expand Down
38 changes: 37 additions & 1 deletion saas_apps/models/saas_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def write(self, vals):
if 'month_price' in vals or 'year_price' in vals:
for line in self.saas_modules:
line.compute_price()
for template in self.template_ids:
template.compute_price()
return res

def refresh_modules(self):
Expand Down Expand Up @@ -187,18 +189,52 @@ def random_ready_operator_check(self):
return False


class SAASTemplate(models.Model):
class SAASAppsTemplate(models.Model):
_inherit = 'saas.template'

set_as_base = fields.Boolean("Base template")
set_as_package = fields.Boolean("Package")
month_price = fields.Float()
year_price = fields.Float()
icon_path = fields.Char(string="Icon path")
product_id = fields.Many2many('product.product', ondelete='cascade')

@api.onchange('set_as_base')
def change_base_template(self):
old_base_template = self.search([('set_as_base', '=', True)])
if old_base_template:
old_base_template.write({'set_as_base': False})

def compute_price(self):
sum = 0
for module in self.template_module_ids:
sum += module.year_price
self.year_price = sum
self.change_product_price(self, sum)
sum = 0
for module in self.template_module_ids:
sum += module.month_price
self.month_price = sum

def change_product_price(self, package, price):
package.product_id.price = price

@api.model
def create(self, vals):
res = super(SAASAppsTemplate, self).create(vals)
if res.set_as_package:
res.compute_price()
if res.name == 'Starter pack':
res.icon_path = 'saas_apps/static/src/img/starter_pack.png'
res.product_id += self.env['product.product'].create({
'name': res.name,
'price': res.year_price
})
return res


class SAASProduct(models.Model):
_inherit = 'product.product'

application = fields.Many2many('saas.line')
package = fields.Many2many('saas.template')
Binary file added saas_apps/static/src/img/starter_pack.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 40 additions & 9 deletions saas_apps/static/src/js/apps.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ odoo.define('saas_apps.model', function (require){
choosen = new Map(),
parent_tree = new Map(),
child_tree = new Map(),
packages = new Map(),
apps_in_basket = 0,
currency = "",
currency_symbol = "",
Expand All @@ -33,18 +34,48 @@ odoo.define('saas_apps.model', function (require){
return calc_apps_price() + parseInt($('#users')[0].value, 10)*user_price();
}

function get_choosen_packages(){
var packages = [];
$.each($('.package'), function( index, value ) {
packages.push(value.children[2].innerText);
});
choosen_packages = [];
for (var key of choosen.keys()) {
if(packages.includes(key)){
choosen_packages.push(key);
choosen.delete(key);
}
}
return choosen_packages;
}

function redirect_to_build(modules_to_install){
if(!modules_to_install){
modules_to_install = '?installing_modules=['
// Collecting choosen modules in string
for (var key of choosen.keys()) {
modules_to_install += ','
modules_to_install += '"' + String(key) + '"';
}
modules_to_install += ']';
// Deleting extra coma
modules_to_install = modules_to_install.replace(',', '');
modules_to_install = '?installing_modules=[';
session.rpc('/price/get_package_modules', {
packages: get_choosen_packages()
}).then(function (resp) {
resp.modules.forEach(elem => {
modules_to_install += ','
modules_to_install += '"' + elem + '"';
});
packages = get_choosen_packages();
// Collecting choosen modules in string
for (var key of choosen.keys()) {
if(resp.modules.includes(key)) continue;
modules_to_install += ','
modules_to_install += '"' + String(key) + '"';
}
modules_to_install += ']';
// Deleting extra coma
modules_to_install = modules_to_install.replace(',', '');
go_to_build(modules_to_install);
});
}
else go_to_build(modules_to_install);
}

function go_to_build(modules_to_install){
if(!choosen.size){
modules_to_install = '?installing_modules=["mail"]';
}
Expand Down
35 changes: 34 additions & 1 deletion saas_apps/views/calculator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
</label>
</div>
</div>
<div class="openerp_enterprise_pricing_step">
<!-- App list -->
<div class="openerp_enterprise_pricing_step">
<h3 class="mt16 mb16">Choose your Apps</h3>
<div class="openerp_enterprise_pricing_step_body mb24">
<div class="form-row">
Expand Down Expand Up @@ -55,6 +55,39 @@
</div>
</div>
</div>
<!-- Packages -->
<div class="openerp_enterprise_pricing_step">
<t t-if="len(packages)">
<h3 class="mt16 mb16">Choose your Packages</h3>
</t>
<div class="openerp_enterprise_pricing_step_body mb24">
<div class="form-row">
<t t-foreach="packages" t-as="package">
<div class="col-12 col-sm-6 col-lg-4 shadow p-3 bg-white rounded price-window text-truncate transition app package">
<div class="app-icon">
<img t-if="package.icon_path"
t-att-src="to_text(package.icon_path)"
style="max-width: 40px;max-height: 40px;"/>
</div>
<div class="app-data">
<div><t t-esc="package.name"/></div>
<div id="app-price">
<div class="yearly-price">
<div class="price-value"><t t-esc="package.year_price"/></div>
<div class="period"> <t t-esc="apps[0].currency_id.symbol"/> / year</div>
</div>
<div class="monthly-price hid">
<div class="price-value"><t t-esc="package.month_price"/></div>
<div class="period"> <t t-esc="apps[0].currency_id.symbol"/> / month</div>
</div>
</div>
</div>
<div class="app_tech_name" style="display: none"><t t-esc="package.name"/></div>
</div>
</t>
</div>
</div>
</div>
<!-- Price window -->
<div>
<span id="price-window" class="shadow mb-5 bg-white rounded">
Expand Down
3 changes: 3 additions & 0 deletions saas_apps/views/manage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
<field name="arch" type="xml">
<xpath expr="//form/sheet/group" position="inside">
<field name="set_as_base"/>
<field name="set_as_package"/>
<field attrs="{'invisible':[('set_as_package','=',False)]}" name="month_price"/>
<field attrs="{'invisible':[('set_as_package','=',False)]}" name="year_price"/>
</xpath>
</field>
</record>
Expand Down

0 comments on commit 67ab586

Please sign in to comment.