public function fetchEmployees()
 {
     $employees = AphEmployeeProduct::getEmployeesByShop(!empty($this->object) ? $this->object->id_shop : Context::getContext()->shop->id);
     $this->context->smarty->assign(array("employees" => $employees));
 }
 public function ajaxProcessEditProductOnOrder()
 {
     // Return value
     $res = true;
     $order = new Order((int) Tools::getValue('id_order'));
     $order_detail = new AphOrderDetail((int) Tools::getValue('product_id_order_detail'));
     if (Tools::isSubmit('product_invoice')) {
         $order_invoice = new OrderInvoice((int) Tools::getValue('product_invoice'));
     }
     // Check fields validity
     $this->doEditProductValidation($order_detail, $order, isset($order_invoice) ? $order_invoice : null);
     // If multiple product_quantity, the order details concern a product customized
     $product_quantity = 0;
     if (is_array(Tools::getValue('product_quantity'))) {
         foreach (Tools::getValue('product_quantity') as $id_customization => $qty) {
             // Update quantity of each customization
             Db::getInstance()->update('customization', array('quantity' => (int) $qty), 'id_customization = ' . (int) $id_customization);
             // Calculate the real quantity of the product
             $product_quantity += $qty;
         }
     } else {
         $product_quantity = Tools::getValue('product_quantity');
     }
     $product_price_tax_incl = Tools::ps_round(Tools::getValue('product_price_tax_incl'), 2);
     $product_price_tax_excl = Tools::ps_round(Tools::getValue('product_price_tax_excl'), 2);
     $total_products_tax_incl = $product_price_tax_incl * $product_quantity;
     $total_products_tax_excl = $product_price_tax_excl * $product_quantity;
     // Calculate differences of price (Before / After)
     $diff_price_tax_incl = $total_products_tax_incl - $order_detail->total_price_tax_incl;
     $diff_price_tax_excl = $total_products_tax_excl - $order_detail->total_price_tax_excl;
     // Apply change on OrderInvoice
     if (isset($order_invoice)) {
         // If OrderInvoice to use is different, we update the old invoice and new invoice
         if ($order_detail->id_order_invoice != $order_invoice->id) {
             $old_order_invoice = new OrderInvoice($order_detail->id_order_invoice);
             // We remove cost of products
             $old_order_invoice->total_products -= $order_detail->total_price_tax_excl;
             $old_order_invoice->total_products_wt -= $order_detail->total_price_tax_incl;
             $old_order_invoice->total_paid_tax_excl -= $order_detail->total_price_tax_excl;
             $old_order_invoice->total_paid_tax_incl -= $order_detail->total_price_tax_incl;
             $res &= $old_order_invoice->update();
             $order_invoice->total_products += $order_detail->total_price_tax_excl;
             $order_invoice->total_products_wt += $order_detail->total_price_tax_incl;
             $order_invoice->total_paid_tax_excl += $order_detail->total_price_tax_excl;
             $order_invoice->total_paid_tax_incl += $order_detail->total_price_tax_incl;
             $order_detail->id_order_invoice = $order_invoice->id;
         }
     }
     if ($diff_price_tax_incl != 0 && $diff_price_tax_excl != 0) {
         $order_detail->unit_price_tax_excl = $product_price_tax_excl;
         $order_detail->unit_price_tax_incl = $product_price_tax_incl;
         $order_detail->total_price_tax_incl += $diff_price_tax_incl;
         $order_detail->total_price_tax_excl += $diff_price_tax_excl;
         if (isset($order_invoice)) {
             // Apply changes on OrderInvoice
             $order_invoice->total_products += $diff_price_tax_excl;
             $order_invoice->total_products_wt += $diff_price_tax_incl;
             $order_invoice->total_paid_tax_excl += $diff_price_tax_excl;
             $order_invoice->total_paid_tax_incl += $diff_price_tax_incl;
         }
         // Apply changes on Order
         $order = new Order($order_detail->id_order);
         $order->total_products += $diff_price_tax_excl;
         $order->total_products_wt += $diff_price_tax_incl;
         $order->total_paid += $diff_price_tax_incl;
         $order->total_paid_tax_excl += $diff_price_tax_excl;
         $order->total_paid_tax_incl += $diff_price_tax_incl;
         $res &= $order->update();
     }
     $old_quantity = $order_detail->product_quantity;
     $delivery_date_lang = Tools::getValue('product_delivery_date_lang');
     $delivery_time_from = Tools::getValue('product_delivery_time_from');
     $delivery_time_to = Tools::getValue('product_delivery_time_to');
     $id_employee = Tools::getValue('product_id_employee');
     $delivery_date = substr($delivery_date_lang, 6, 4) . '-' . substr($delivery_date_lang, 3, 2) . '-' . substr($delivery_date_lang, 0, 2);
     $order_detail->delivery_time_from = $delivery_time_from;
     $order_detail->delivery_time_to = $delivery_time_to;
     $order_detail->delivery_date = $delivery_date;
     $order_detail->id_employee = $id_employee;
     $order_detail->product_quantity = $product_quantity;
     $order_detail->reduction_percent = 0;
     // update taxes
     $res &= $order_detail->updateTaxAmount($order);
     // Save order detail
     $res &= $order_detail->update();
     // Update weight SUM
     $order_carrier = new OrderCarrier((int) $order->getIdOrderCarrier());
     if (Validate::isLoadedObject($order_carrier)) {
         $order_carrier->weight = (double) $order->getTotalWeight();
         $res &= $order_carrier->update();
         if ($res) {
             $order->weight = sprintf("%.3f " . Configuration::get('PS_WEIGHT_UNIT'), $order_carrier->weight);
         }
     }
     // Save order invoice
     if (isset($order_invoice)) {
         $res &= $order_invoice->update();
     }
     // Update product available quantity
     StockAvailable::updateQuantity($order_detail->product_id, $order_detail->product_attribute_id, $old_quantity - $order_detail->product_quantity, $order->id_shop);
     $products = $this->getProducts($order);
     // Get the last product
     $product = $products[$order_detail->id];
     $resume = OrderSlip::getProductSlipResume($order_detail->id);
     $product['quantity_refundable'] = $product['product_quantity'] - $resume['product_quantity'];
     $product['amount_refundable'] = $product['total_price_tax_excl'] - $resume['amount_tax_excl'];
     $product['amount_refund'] = Tools::displayPrice($resume['amount_tax_incl']);
     $product['refund_history'] = OrderSlip::getProductSlipDetail($order_detail->id);
     if ($product['id_warehouse'] != 0) {
         $warehouse = new Warehouse((int) $product['id_warehouse']);
         $product['warehouse_name'] = $warehouse->name;
         $warehouse_location = WarehouseProductLocation::getProductLocation($product['product_id'], $product['product_attribute_id'], $product['id_warehouse']);
         if (!empty($warehouse_location)) {
             $product['warehouse_location'] = $warehouse_location;
         } else {
             $product['warehouse_location'] = false;
         }
     } else {
         $product['warehouse_name'] = '--';
         $product['warehouse_location'] = false;
     }
     // Get invoices collection
     $invoice_collection = $order->getInvoicesCollection();
     $invoice_array = array();
     foreach ($invoice_collection as $invoice) {
         /** @var OrderInvoice $invoice */
         $invoice->name = $invoice->getInvoiceNumberFormatted(Context::getContext()->language->id, (int) $order->id_shop);
         $invoice_array[] = $invoice;
     }
     $options_time = array();
     $time_slice = Configuration::get('APH_CALENDAR_TIME_SLICE');
     for ($hours = 0; $hours < 24; $hours++) {
         // the interval for hours is '1'
         for ($mins = 0; $mins < 60; $mins += $time_slice) {
             // the interval for mins is 'APH_CALENDAR_TIME_SLICE'
             $options_time[str_pad($hours, 2, '0', STR_PAD_LEFT) . ':' . str_pad($mins, 2, '0', STR_PAD_LEFT)] = str_pad($hours, 2, '0', STR_PAD_LEFT) . ':' . str_pad($mins, 2, '0', STR_PAD_LEFT);
         }
     }
     $this->context->smarty->assign(array('options_time' => $options_time));
     $employees = array();
     $e = AphEmployeeProduct::getEmployeesByShop((int) Context::getContext()->shop->id);
     foreach ($e as $employee) {
         $employees[$employee['id_employee']] = $employee['fullName'];
     }
     $this->context->smarty->assign(array('employees' => $employees));
     // Assign to smarty informations in order to show the new product line
     $this->context->smarty->assign(array('product' => $product, 'order' => $order, 'currency' => new Currency($order->id_currency), 'can_edit' => $this->tabAccess['edit'], 'invoices_collection' => $invoice_collection, 'current_id_lang' => Context::getContext()->language->id, 'link' => Context::getContext()->link, 'current_index' => self::$currentIndex, 'display_warehouse' => (int) Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')));
     if (!$res) {
         die(Tools::jsonEncode(array('result' => $res, 'error' => Tools::displayError('An error occurred while editing the product line.'))));
     }
     if (is_array(Tools::getValue('product_quantity'))) {
         $view = $this->createTemplate('_customized_data.tpl')->fetch();
     } else {
         $view = $this->createTemplate('_product_line.tpl')->fetch();
     }
     $this->sendChangedNotification($order);
     die(Tools::jsonEncode(array('result' => $res, 'view' => $view, 'can_edit' => $this->tabAccess['add'], 'invoices_collection' => $invoice_collection, 'order' => $order, 'invoices' => $invoice_array, 'documents_html' => $this->createTemplate('_documents.tpl')->fetch(), 'shipping_html' => $this->createTemplate('_shipping.tpl')->fetch(), 'customized_product' => is_array(Tools::getValue('product_quantity')))));
 }
 public function ajaxProcessEditOrder()
 {
     if ((int) Tools::getValue('id_order_detail') < 1) {
         return 'Appuntamento non trovato.';
     }
     $copy_order = false;
     if ((bool) Tools::getIsset('copyorder')) {
         $copy_order = true;
     }
     $order_detail = new AphOrderDetail(Tools::getValue('id_order_detail'));
     $order = new Order($order_detail->id_order);
     $customer = new Customer($order->id_customer);
     $address = new Address($order->id_address_invoice);
     $this->context->smarty->assign('id_order', empty($copy_order) ? $order_detail->id_order : NULL);
     $this->context->smarty->assign('id_order_detail', empty($copy_order) ? $order_detail->id_order_detail : NULL);
     $this->context->smarty->assign('id_customer', $order->id_customer);
     $this->context->smarty->assign('delivery_date', $order_detail->delivery_date);
     $this->context->smarty->assign('delivery_date_lang', date('d/m/Y', strtotime($order_detail->delivery_date)));
     $this->context->smarty->assign('delivery_time_from', substr($order_detail->delivery_time_from, 0, 5));
     $this->context->smarty->assign('delivery_time_to', substr($order_detail->delivery_time_to, 0, 5));
     $this->context->smarty->assign('id_employee', empty($copy_order) ? $order_detail->id_employee : NULL);
     $this->context->smarty->assign('other', $order_detail->note);
     $this->context->smarty->assign('firstname', $customer->firstname);
     $this->context->smarty->assign('lastname', $customer->lastname);
     $this->context->smarty->assign('id_gender', $customer->id_gender);
     $this->context->smarty->assign('email', $customer->email);
     $this->context->smarty->assign('phone', !empty($address->phone_mobile) ? $address->phone_mobile : $address->phone);
     $selected = array($order_detail->product_id);
     $products = array('selected' => array(), 'unselected' => array());
     $results = Db::getInstance()->executeS('
     SELECT DISTINCT name, p.id_product as id
     FROM ' . _DB_PREFIX_ . 'product p
     LEFT JOIN `' . _DB_PREFIX_ . 'product_lang` pl
         ON (p.`id_product` = pl.`id_product`
         AND pl.`id_lang` = ' . (int) Context::getContext()->language->id . Shop::addSqlRestrictionOnLang('pl') . ')
     ' . Shop::addSqlAssociation('product', 'p') . '
     WHERE id_lang = ' . (int) Context::getContext()->language->id . '
     ORDER BY name');
     foreach ($results as $row) {
         $products[in_array($row['id'], $selected) ? 'selected' : 'unselected'][] = $row;
     }
     $choose_content = Context::getContext()->smarty->createTemplate(_PS_MODULE_DIR_ . $this->module->name . '/views/templates/admin/controllers/calendar/product_itemlist.tpl');
     $choose_content->assign('product_itemlist', $products);
     $this->context->smarty->assign('product_choose_content', $choose_content->fetch());
     $employees = AphEmployeeProduct::getEmployeesByShop((int) $this->context->shop->id);
     $this->context->smarty->assign('employees', $employees);
     $options_time = array();
     $time_slice = Configuration::get('APH_CALENDAR_TIME_SLICE');
     for ($hours = 0; $hours < 24; $hours++) {
         // the interval for hours is '1'
         for ($mins = 0; $mins < 60; $mins += $time_slice) {
             // the interval for mins is 'APH_CALENDAR_TIME_SLICE'
             $options_time[str_pad($hours, 2, '0', STR_PAD_LEFT) . ':' . str_pad($mins, 2, '0', STR_PAD_LEFT)] = str_pad($hours, 2, '0', STR_PAD_LEFT) . ':' . str_pad($mins, 2, '0', STR_PAD_LEFT);
         }
     }
     $this->context->smarty->assign('options_time', $options_time);
     $this->context->smarty->assign(array('current_id_lang' => Context::getContext()->language->id, 'link' => Context::getContext()->link, 'current_index' => self::$currentIndex));
     $jsVars = array('language' => $this->context->language->iso_code, 'slotDuration' => '00:' . Configuration::get('APH_CALENDAR_TIME_SLICE') . ':00');
     $this->setJsVars($jsVars);
     $this->content .= $this->context->smarty->fetch(_PS_MODULE_DIR_ . $this->module->name . '/views/templates/admin/controllers/calendar/order.tpl');
     return parent::initContent();
 }