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 Societe($db); $form = new Form($db); $favoriteProduct = new FavoriteProduct($db); // Load thirdparty 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 = $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 = GETPOSTFLOAT('qty'); if ($favid > 0 && $qty > 0) { $favoriteProduct->fetch($favid); $favoriteProduct->qty = $qty; $result = $favoriteProduct->update($user); } header('Location: '.$_SERVER['PHP_SELF'].'?id='.$id); exit; } if ($action == 'moveup' && $permissiontoadd) { $favid = GETPOSTINT('favid'); if ($favid > 0) { $favoriteProduct->moveUp($favid, $id); } header('Location: '.$_SERVER['PHP_SELF'].'?id='.$id); exit; } if ($action == 'movedown' && $permissiontoadd) { $favid = GETPOSTINT('favid'); if ($favid > 0) { $favoriteProduct->moveDown($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->generateOrder($user, $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 */ $title = $langs->trans('FavoriteProducts').' - '.$object->name; llxHeader('', $title); // Fetch favorites $favorites = $favoriteProduct->fetchAllBySociete($id); // Prepare tabs $head = societe_prepare_head($object); print dol_get_fiche_head($head, 'favoriteproducts', $langs->trans("ThirdParty"), -1, 'company'); // Thirdparty card $linkback = ''.$langs->trans("BackToList").''; dol_banner_tab($object, 'socid', $linkback, ($user->socid ? 0 : 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: show as integer if whole number, otherwise 2 decimals // Use '.' as decimal separator for HTML number input compatibility $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();