loadLangs(array('companies', 'products', 'orders', 'kundenkarte@kundenkarte')); // Get parameters $id = GETPOSTINT('id'); $action = GETPOST('action', 'aZ09'); $confirm = GETPOST('confirm', 'alpha'); // Security check if (!$user->hasRight('kundenkarte', 'read')) { accessforbidden(); } // Initialize objects $object = new Contact($db); $form = new Form($db); $favoriteProduct = new FavoriteProduct($db); // Load contact if ($id > 0) { $result = $object->fetch($id); if ($result <= 0) { dol_print_error($db, $object->error); exit; } } $permissiontoread = $user->hasRight('kundenkarte', 'read'); $permissiontoadd = $user->hasRight('kundenkarte', 'write'); $permissiontodelete = $user->hasRight('kundenkarte', 'delete'); /* * Actions */ if ($action == 'add' && $permissiontoadd) { $productid = GETPOSTINT('productid'); $qty = GETPOSTFLOAT('qty'); if ($productid > 0) { $favoriteProduct->fk_soc = $object->socid; $favoriteProduct->fk_contact = $id; $favoriteProduct->fk_product = $productid; $favoriteProduct->qty = $qty > 0 ? $qty : 1; $favoriteProduct->active = 1; $result = $favoriteProduct->create($user); if ($result > 0) { setEventMessages($langs->trans('RecordSaved'), null, 'mesgs'); } else { setEventMessages($favoriteProduct->error, $favoriteProduct->errors, 'errors'); } } header('Location: '.$_SERVER['PHP_SELF'].'?id='.$id); exit; } if ($action == 'delete' && $permissiontodelete) { $favid = GETPOSTINT('favid'); if ($favid > 0) { $favoriteProduct->fetch($favid); $result = $favoriteProduct->delete($user); if ($result > 0) { setEventMessages($langs->trans('RecordDeleted'), null, 'mesgs'); } else { setEventMessages($favoriteProduct->error, $favoriteProduct->errors, 'errors'); } } header('Location: '.$_SERVER['PHP_SELF'].'?id='.$id); exit; } if ($action == 'updateqty' && $permissiontoadd) { $favid = GETPOSTINT('favid'); $qty = GETPOST('qty', 'alpha'); if ($favid > 0 && is_numeric($qty)) { $favoriteProduct->fetch($favid); $favoriteProduct->qty = (float) $qty; $result = $favoriteProduct->update($user); } header('Location: '.$_SERVER['PHP_SELF'].'?id='.$id); exit; } if ($action == 'moveup' && $permissiontoadd) { $favid = GETPOSTINT('favid'); if ($favid > 0) { $favoriteProduct->moveUpByContact($favid, $id); } header('Location: '.$_SERVER['PHP_SELF'].'?id='.$id); exit; } if ($action == 'movedown' && $permissiontoadd) { $favid = GETPOSTINT('favid'); if ($favid > 0) { $favoriteProduct->moveDownByContact($favid, $id); } header('Location: '.$_SERVER['PHP_SELF'].'?id='.$id); exit; } if ($action == 'confirm_generateorder' && $confirm == 'yes' && $permissiontoadd) { $selected = GETPOST('selected_products', 'array'); $quantities = GETPOST('quantities', 'array'); if (!empty($selected)) { $result = $favoriteProduct->generateOrderByContact($user, $object->socid, $id, $selected, $quantities); if ($result > 0) { setEventMessages($langs->trans('OrderGenerated', $result), null, 'mesgs'); header('Location: '.DOL_URL_ROOT.'/commande/card.php?id='.$result); exit; } else { setEventMessages($favoriteProduct->error, $favoriteProduct->errors, 'errors'); } } else { setEventMessages($langs->trans('NoProductsSelected'), null, 'warnings'); } } /* * View */ // Use Dolibarr standard button classes $title = $langs->trans('FavoriteProducts').' - '.$object->getFullName($langs); llxHeader('', $title, '', '', 0, 0, array('/kundenkarte/js/kundenkarte.js?v=1769963241'), array('/kundenkarte/css/kundenkarte.css?v=1769964233')); // Fetch favorites for this contact $favorites = $favoriteProduct->fetchAllByContact($id); // Prepare tabs $head = contact_prepare_head($object); print dol_get_fiche_head($head, 'favoriteproducts', $langs->trans("ContactAddress"), -1, 'contact'); // Contact card $linkback = ''.$langs->trans("BackToList").''; dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'nom'); print '
'; // Confirmation dialog for order generation if ($action == 'generateorder') { $formquestion = array(); print $form->formconfirm( $_SERVER['PHP_SELF'].'?id='.$id, $langs->trans('GenerateOrder'), $langs->trans('ConfirmGenerateOrder'), 'confirm_generateorder', $formquestion, 'yes', 1 ); } print '
'; // Add product form if ($permissiontoadd) { print '
'; print '
'; print ''; print ''; print ''; print '
'; print ''; print ''; print ''; print '
'; print '
'; print '
'; } // List of favorites print '
'; print ''; print ''; print ''; if (is_array($favorites) && count($favorites) > 0) { print ''; print ''; print ''; print ''; if ($permissiontoadd) { print ''; } print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; $totalHT = 0; $totalItems = count($favorites); $currentIndex = 0; foreach ($favorites as $fav) { $currentIndex++; $lineTotal = $fav->qty * $fav->product_price; $totalHT += $lineTotal; // Format quantity $qtyDisplay = (floor($fav->qty) == $fav->qty) ? (int)$fav->qty : round($fav->qty, 2); print ''; // Checkbox print ''; // Position (up/down buttons) if ($permissiontoadd) { print ''; } // Product print ''; // Quantity print ''; // Unit price print ''; // Total print ''; // Actions print ''; print ''; } print ''; print ''; print ''; $colspan = $permissiontoadd ? 5 : 4; print ''; print ''; print ''; print ''; print ''; print '
'; print ''; print ''.$langs->trans('Position').''.$langs->trans('Product').''.$langs->trans('DefaultQuantity').''.$langs->trans('UnitPriceHT').''.$langs->trans('Total').''.$langs->trans('Actions').'
'; print ''; print ''; if ($currentIndex > 1) { print ''; print ''; print ' '; } else { print ' '; } if ($currentIndex < $totalItems) { print ''; print ''; print ''; } else { print ''; } print ''; print ''; print img_object('', 'product', 'class="pictofixedwidth"'); print dol_escape_htmltag($fav->product_ref).' - '.dol_escape_htmltag($fav->product_label); print ''; print ''; print '
'; print ''; print ''; print '
'; print '
'.price($fav->product_price).''.price($lineTotal).''; if ($permissiontodelete) { print ''; print img_delete(); print ''; } print '
'.$langs->trans('Total').''.price($totalHT).'
'; // Actions bar print '
'; print '
'; print ' '; print ''; print '
'; if ($permissiontoadd) { print ''; } print '
'; } else { print '
'.$langs->trans('NoFavoriteProducts').'
'; } print '
'; print '
'; print dol_get_fiche_end(); // JavaScript for checkbox handling print ''; llxFooter(); $db->close();