function save($check_notify = FALSE)
 {
     // For quick create.
     if (!empty($_REQUEST['relate_to']) && $_REQUEST['relate_to'] == 'reg_invoices' && !empty($_REQUEST['relate_id'])) {
         $this->invoice_id = $_REQUEST['relate_id'];
     }
     // Antes de guardar, si no tiene un "Peso" que indique el orden, le ponemos el último
     if (!$this->ordered && !empty($_POST['relate_id'])) {
         $sql = " SELECT MAX(ordered) ordered" . " FROM reg_items " . " WHERE invoice_id = '{$_POST['relate_id']}' AND deleted = 0 ";
         $result = $this->db->query($sql);
         $row = $this->db->fetchByAssoc($result);
         $ordered = $row['ordered'];
         if (is_numeric($ordered) && $ordered > 0) {
             $this->ordered = $ordered + 1;
         } else {
             $this->ordered = 1;
         }
     }
     // Calculamos el Total antes de impuestos
     if ($this->discount && $this->discount != 0) {
         $preDescuento = $this->qty * $this->unit_price;
         $discount = 0;
         if ($this->discount[strlen($this->discount) - 1] == '%') {
             $discount = substr($this->discount, 0, strlen($this->discount) - 1);
             $this->total_discount = $preDescuento * ($discount + 0) / 100;
             $this->total_base = $preDescuento - $this->total_discount;
         } else {
             if ($this->discount + 0 >= $preDescuento) {
                 $this->total_base = 0;
                 $this->total_discount = $preDescuento;
             } else {
                 //$preDescuento = $this->discount;
                 $this->total_base = $preDescuento - ($this->discount + 0);
                 $this->total_discount = $this->discount;
             }
         }
     } else {
         $this->total_base = $this->qty * $this->unit_price;
         $this->total_discount = 0;
     }
     // Calculamos el total de Impuestos (si los hubiera)
     if ($this->tax && is_numeric($this->tax) && $this->tax > 0) {
         $this->total_tax = $this->total_base * ($this->tax / 100);
     } else {
         $this->total_tax = '';
         $this->tax = '';
     }
     // Calculamos la retención
     if ($this->retention && is_numeric($this->retention) && $this->retention > 0) {
         $this->total_retention = $this->total_base * ($this->retention / 100);
     } else {
         $this->total_retention = '';
         $this->retention = '';
     }
     // Si se está ordenando, los datos están como se guardan en la base de datos
     // hay que desformatearlos para que al llamar a save() no se estropicien.
     if (!empty($_REQUEST['ordenar']) && strtolower($_REQUEST['ordenar']) == 'up') {
         $this->format_all_fields();
     }
     parent::save($check_notify);
     // Asociamos con la Factura correspondiente.
     if (!empty($_POST['relate_id'])) {
         $invoice = new reg_invoices();
         $invoice->retrieve($_POST['relate_id']);
         $invoice->calculateTotal();
         $invoice->save();
     }
 }
 protected function duplicateItemsFromInvoiceId($id)
 {
     $previousInvoice = new reg_invoices();
     $previousInvoice->retrieve($id);
     $previousInvoice->load_relationship('items');
     $this->load_relationship('items');
     foreach ($previousInvoice->items->getBeans() as $item) {
         $item->id = null;
         // This creates a copy of the item
         $item->relate_id = null;
         // Prevent múltiple total calculation
         $item->save();
         $this->items->add($item->id);
     }
 }
    $record = $_REQUEST['record'];
    $sugarbean->retrieve($record);
    if (!$sugarbean->ACLAccess('Delete')) {
        ACLController::displayNoAccess(true);
        sugar_cleanup(true);
    }
    $GLOBALS['log']->info("deleting Item: {$record}");
    // Antes de borrar, obtenemos el ID de la factura
    $sql = " select invoice_id from invoice_items where deleted=0 and id = '{$record}' ";
    $result = $sugarbean->db->query($sql);
    $row = $sugarbean->db->fetchByAssoc($result);
    $invoice_id = $row['invoice_id'];
    // Borramos el Item
    $sugarbean->mark_deleted($record);
    // Actualizamos la factura asociada.
    if ($invoice_id) {
        $invoice = new reg_invoices();
        $invoice->retrieve($invoice_id);
        $invoice->calcularTotal();
        $invoice->save();
    }
}
// handle the return location variables
if (!empty($_REQUEST['return_url'])) {
    $_REQUEST['return_url'] = urldecode($_REQUEST['return_url']);
}
$GLOBALS['log']->debug("deleted Item: bean: {$bean_name}, ");
if (empty($_REQUEST['refresh_page'])) {
    handleRedirect();
}
exit;