104 lines
5.0 KiB
Python
104 lines
5.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import api, models,fields
|
|
|
|
|
|
|
|
class CampaignCampaign(models.Model):
|
|
_name = 'campaign.campaign'
|
|
_inherit = 'mail.thread'
|
|
_rec_name = 'campaign_name'
|
|
seq = fields.Char(readonly=True,string='الرقم التسلسلي')
|
|
image = fields.Binary(default='')
|
|
description =fields.Text(String="Description",default="عند ضغط على زر 'Activate' سيتم تنشيط الحملة وإنشاء تبرع جديد يعرض في الصفحة الرئيسية",readonly=True)
|
|
#donation_field = fields.Selection([('كفاله يتيم','كفاله يتيم')],default='كفاله يتيم',string='مجال التبرع')
|
|
category_id = fields.Many2one('product.public.category',string="مجال الحمله")
|
|
campaign_name = fields.Char(string='اسم الحمله')
|
|
donation_amount = fields.Float(string='المبلغ المستهدف')
|
|
state = fields.Selection([('بإنتظار الإعتماد','بإنتظار الإعتماد'),
|
|
('نشطة','نشطة'),
|
|
('غير نشطة','غير نشطة')],string='الحالة',default='بإنتظار الإعتماد')
|
|
|
|
confirm_flag = fields.Boolean()
|
|
user_id = fields.Many2one('res.users',string='مقدم الطلب')
|
|
email = fields.Char(related='user_id.login',string='الايميل')
|
|
remaining_amount = fields.Float()
|
|
# def _get_remaining_amount(self):
|
|
# for rec in self:
|
|
# template = rec.env['product.template'].search([('campaign_id','=',rec.id)])
|
|
# if template:
|
|
# rec.remaining_amount = template.remaining_amount
|
|
# else:
|
|
# rec.remaining_amount = 0
|
|
|
|
|
|
|
|
def action_activate(self):
|
|
for rec in self:
|
|
rec .state = 'نشطة'
|
|
campaigns = rec.env['product.template'].search([('campaign_id','=',rec.id)])
|
|
if not campaigns :
|
|
rec.env['product.template'].create({'name':rec.campaign_name,
|
|
'target_amount':rec.donation_amount,
|
|
'remaining_amount':rec.donation_amount,
|
|
'sale_ok':True,
|
|
'website_published':True,
|
|
'image_1920':rec.image,
|
|
'campaign_id':rec.id,
|
|
"public_categ_ids": [rec.category_id.id]})
|
|
if rec.user_id.partner_id.phone:
|
|
sms_template_id = self.env.ref('ensan_campaign.sms_campaign')
|
|
# print(sms_template_id.body)
|
|
sms_template_id.body = 'تم تنشيط الحملة ({seq}) بنجاح'.format(seq=rec.seq)
|
|
# print(sms_template_id.body)
|
|
rec._message_sms_with_template(
|
|
template=sms_template_id,
|
|
put_in_queue=False,
|
|
sms_numbers=[rec.user_id.partner_id.phone]
|
|
)
|
|
|
|
def action_deactivate(self):
|
|
for rec in self:
|
|
rec.state = 'غير نشطة'
|
|
rec.env['product.template'].search([('campaign_id','=',rec.id)]).unlink()
|
|
|
|
@api.model
|
|
def create(self, vals):
|
|
vals['seq'] = self.env['ir.sequence'].next_by_code('campaign.campaign')
|
|
campaign = super(CampaignCampaign, self).create(vals)
|
|
|
|
# Send notifications here (use Odoo's messaging system or other notification mechanisms)
|
|
campaign.notification_create_campaign(campaign)
|
|
|
|
return campaign
|
|
|
|
def notification_create_campaign(self, campaign):
|
|
for rec in self:
|
|
|
|
users = rec.env.ref('website.group_website_designer').users
|
|
notification_ids = []
|
|
for user in users:
|
|
notification_ids.append((0, 0, {
|
|
'res_partner_id': user.partner_id.id,
|
|
'notification_type': 'inbox'
|
|
}))
|
|
|
|
base_url = self.env['ir.config_parameter'].get_param('web.base.url')
|
|
base_url += '/web#id=%d&model=%s&view_type=form' % (self.id, self._name)
|
|
body = "Campaign " + campaign.campaign_name + " Added " + base_url
|
|
rec.message_post(record_name='campaign', body=body
|
|
, message_type="notification",
|
|
subtype_xmlid="mail.mt_comment",
|
|
author_id=rec.create_uid.partner_id.id,
|
|
notification_ids=notification_ids)
|
|
|
|
# @api.model_create_multi
|
|
# def create(self, vals_list):
|
|
# # Call the original create method to create the record
|
|
# campaign = super(CampaignCampaign, self).create(vals_list)
|
|
#
|
|
# # Send notifications here (use Odoo's messaging system or other notification mechanisms)
|
|
# campaign.notification_create_campaign(campaign)
|
|
#
|
|
# return campaign |