changes
This commit is contained in:
@@ -10,70 +10,68 @@ class ProductProduct(models.Model):
|
||||
_inherit = 'product.product'
|
||||
|
||||
def get_adp_device_code(self):
|
||||
"""
|
||||
Get ADP device code from the field mapped in fusion settings.
|
||||
|
||||
The field name is configured in Settings → Sales → Fusion Central →
|
||||
Field Mappings → Product ADP Code Field.
|
||||
|
||||
Checks the mapped field on the product variant first, then on template.
|
||||
Returns the value from the mapped field, or empty string if not found.
|
||||
"""Get ADP device code, preferring the linked device code record.
|
||||
|
||||
Checks in order:
|
||||
1. Linked Many2one device code record on template
|
||||
2. x_fc_adp_device_code char field on template
|
||||
3. Mapped field from fusion settings (legacy)
|
||||
4. default_code
|
||||
"""
|
||||
self.ensure_one()
|
||||
|
||||
# Get the mapped field name from fusion settings
|
||||
tmpl = self.product_tmpl_id
|
||||
|
||||
if tmpl and tmpl.x_fc_adp_device_code_id:
|
||||
return tmpl.x_fc_adp_device_code_id.device_code or ''
|
||||
|
||||
if tmpl and tmpl.x_fc_adp_device_code:
|
||||
return tmpl.x_fc_adp_device_code
|
||||
|
||||
ICP = self.env['ir.config_parameter'].sudo()
|
||||
field_name = ICP.get_param('fusion_claims.field_product_code', 'x_fc_adp_device_code')
|
||||
|
||||
if not field_name:
|
||||
return ''
|
||||
|
||||
# Check if the mapped field exists on the product variant (product.product)
|
||||
if field_name in self._fields:
|
||||
value = getattr(self, field_name, '') or ''
|
||||
if value:
|
||||
return value
|
||||
|
||||
# Check if the mapped field exists on the product template
|
||||
if self.product_tmpl_id and field_name in self.product_tmpl_id._fields:
|
||||
value = getattr(self.product_tmpl_id, field_name, '') or ''
|
||||
if value:
|
||||
return value
|
||||
|
||||
return ''
|
||||
if field_name and field_name != 'x_fc_adp_device_code':
|
||||
if field_name in self._fields:
|
||||
value = getattr(self, field_name, '') or ''
|
||||
if value:
|
||||
return value
|
||||
if tmpl and field_name in tmpl._fields:
|
||||
value = getattr(tmpl, field_name, '') or ''
|
||||
if value:
|
||||
return value
|
||||
|
||||
return self.default_code or ''
|
||||
|
||||
def get_adp_price(self):
|
||||
"""
|
||||
Get ADP price from the field mapped in fusion settings.
|
||||
|
||||
The field name is configured in Settings → Sales → Fusion Central →
|
||||
Field Mappings → Product ADP Price Field.
|
||||
|
||||
Checks the mapped field on the product variant first, then on template.
|
||||
Returns the value from the mapped field, or 0.0 if not found.
|
||||
"""Get ADP price, preferring the linked device code record.
|
||||
|
||||
Checks in order:
|
||||
1. Linked Many2one device code record price on template
|
||||
2. x_fc_adp_price field on template
|
||||
3. Mapped field from fusion settings (legacy)
|
||||
4. list_price
|
||||
"""
|
||||
self.ensure_one()
|
||||
|
||||
# Get the mapped field name from fusion settings
|
||||
tmpl = self.product_tmpl_id
|
||||
|
||||
if tmpl and tmpl.x_fc_adp_device_code_id and tmpl.x_fc_adp_device_code_id.adp_price:
|
||||
return tmpl.x_fc_adp_device_code_id.adp_price
|
||||
|
||||
if tmpl and tmpl.x_fc_adp_price:
|
||||
return tmpl.x_fc_adp_price
|
||||
|
||||
ICP = self.env['ir.config_parameter'].sudo()
|
||||
field_name = ICP.get_param('fusion_claims.field_product_adp_price', 'x_fc_adp_price')
|
||||
|
||||
if not field_name:
|
||||
return 0.0
|
||||
|
||||
# Check if the mapped field exists on the product variant (product.product)
|
||||
if field_name in self._fields:
|
||||
value = getattr(self, field_name, 0.0) or 0.0
|
||||
if value:
|
||||
return value
|
||||
|
||||
# Check if the mapped field exists on the product template
|
||||
if self.product_tmpl_id and field_name in self.product_tmpl_id._fields:
|
||||
value = getattr(self.product_tmpl_id, field_name, 0.0) or 0.0
|
||||
if value:
|
||||
return value
|
||||
|
||||
return 0.0
|
||||
if field_name and field_name != 'x_fc_adp_price':
|
||||
if field_name in self._fields:
|
||||
value = getattr(self, field_name, 0.0) or 0.0
|
||||
if value:
|
||||
return value
|
||||
if tmpl and field_name in tmpl._fields:
|
||||
value = getattr(tmpl, field_name, 0.0) or 0.0
|
||||
if value:
|
||||
return value
|
||||
|
||||
return tmpl.list_price if tmpl else 0.0
|
||||
|
||||
def is_non_adp_funded(self):
|
||||
"""
|
||||
@@ -114,65 +112,71 @@ class ProductProduct(models.Model):
|
||||
return False
|
||||
|
||||
def action_sync_adp_price_from_database(self):
|
||||
"""
|
||||
Update product's ADP price from the device codes database.
|
||||
|
||||
Looks up the product's ADP device code in the fusion.adp.device.code table
|
||||
and updates the product's x_fc_adp_price field with the database value.
|
||||
|
||||
Returns a notification with the result.
|
||||
"""Sync product ADP data from the device codes database.
|
||||
|
||||
Looks up the product's device code in fusion.adp.device.code and
|
||||
populates the Many2one link, price, and device code string.
|
||||
"""
|
||||
ADPDevice = self.env['fusion.adp.device.code'].sudo()
|
||||
updated = []
|
||||
not_found = []
|
||||
no_code = []
|
||||
|
||||
|
||||
for product in self:
|
||||
device_code = product.get_adp_device_code()
|
||||
product_tmpl = product.product_tmpl_id
|
||||
|
||||
if product_tmpl.x_fc_adp_device_code_id:
|
||||
adp_device = product_tmpl.x_fc_adp_device_code_id
|
||||
device_code = adp_device.device_code
|
||||
else:
|
||||
device_code = product.get_adp_device_code()
|
||||
|
||||
if not device_code:
|
||||
no_code.append(product.name)
|
||||
continue
|
||||
|
||||
|
||||
adp_device = ADPDevice.search([
|
||||
('device_code', '=', device_code),
|
||||
('active', '=', True)
|
||||
], limit=1)
|
||||
|
||||
if adp_device and adp_device.adp_price:
|
||||
# Update product template
|
||||
product_tmpl = product.product_tmpl_id
|
||||
old_price = 0
|
||||
|
||||
if hasattr(product_tmpl, 'x_fc_adp_price'):
|
||||
old_price = getattr(product_tmpl, 'x_fc_adp_price', 0) or 0
|
||||
product_tmpl.sudo().write({'x_fc_adp_price': adp_device.adp_price})
|
||||
updated.append({
|
||||
'name': product.name,
|
||||
'code': device_code,
|
||||
'old_price': old_price,
|
||||
'new_price': adp_device.adp_price,
|
||||
})
|
||||
|
||||
if adp_device:
|
||||
old_price = product_tmpl.x_fc_adp_price or 0
|
||||
write_vals = {
|
||||
'x_fc_adp_device_code_id': adp_device.id,
|
||||
'x_fc_adp_device_code': adp_device.device_code,
|
||||
'x_fc_adp_price': adp_device.adp_price,
|
||||
'x_fc_is_adp_product': True,
|
||||
}
|
||||
product_tmpl.sudo().write(write_vals)
|
||||
updated.append({
|
||||
'name': product.name,
|
||||
'code': device_code,
|
||||
'old_price': old_price,
|
||||
'new_price': adp_device.adp_price,
|
||||
})
|
||||
else:
|
||||
not_found.append(f"{product.name} ({device_code})")
|
||||
|
||||
# Build result message
|
||||
|
||||
message_parts = []
|
||||
if updated:
|
||||
msg = f"<strong>Updated {len(updated)} product(s):</strong><ul>"
|
||||
msg = f"<strong>Synced {len(updated)} product(s):</strong><ul>"
|
||||
for u in updated:
|
||||
msg += f"<li>{u['name']}: ${u['old_price']:.2f} → ${u['new_price']:.2f}</li>"
|
||||
msg += f"<li>{u['name']}: ${u['old_price']:.2f} -> ${u['new_price']:.2f}</li>"
|
||||
msg += "</ul>"
|
||||
message_parts.append(msg)
|
||||
|
||||
|
||||
if not_found:
|
||||
message_parts.append(f"<strong>Not found in database:</strong> {', '.join(not_found)}")
|
||||
|
||||
message_parts.append(
|
||||
f"<strong>Not found in database:</strong> {', '.join(not_found)}"
|
||||
)
|
||||
if no_code:
|
||||
message_parts.append(f"<strong>No ADP code:</strong> {', '.join(no_code)}")
|
||||
|
||||
message_parts.append(
|
||||
f"<strong>No ADP code:</strong> {', '.join(no_code)}"
|
||||
)
|
||||
if not message_parts:
|
||||
message_parts.append("No products to process.")
|
||||
|
||||
|
||||
return {
|
||||
'type': 'ir.actions.client',
|
||||
'tag': 'display_notification',
|
||||
|
||||
Reference in New Issue
Block a user