本文整理汇总了PHP中WarehouseProductLocation::save方法的典型用法代码示例。如果您正苦于以下问题:PHP WarehouseProductLocation::save方法的具体用法?PHP WarehouseProductLocation::save怎么用?PHP WarehouseProductLocation::save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WarehouseProductLocation
的用法示例。
在下文中一共展示了WarehouseProductLocation::save方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addAttribute
public function addAttribute($price, $weight, $unit_impact, $ecotax, $id_images, $reference, $ean13, $default, $location = null, $upc = null, $minimal_quantity = 1, array $id_shop_list = array(), $available_date = null)
{
if (!$this->id) {
return;
}
$price = str_replace(',', '.', $price);
$weight = str_replace(',', '.', $weight);
$combination = new Combination();
$combination->id_product = (int) $this->id;
$combination->price = (double) $price;
$combination->ecotax = (double) $ecotax;
$combination->quantity = 0;
$combination->weight = (double) $weight;
$combination->unit_price_impact = (double) $unit_impact;
$combination->reference = pSQL($reference);
$combination->location = pSQL($location);
$combination->ean13 = pSQL($ean13);
$combination->upc = pSQL($upc);
$combination->default_on = (int) $default;
$minimal_quantity = str_replace(',', '.', $minimal_quantity);
$this->setMinQty($minimal_quantity, $combination);
$combination->available_date = $available_date;
if (count($id_shop_list)) {
$combination->id_shop_list = array_unique($id_shop_list);
}
$combination->add();
if (!$combination->id) {
return false;
}
$total_quantity = (int) Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
SELECT SUM(quantity + quantity_remainder) as quantity
FROM ' . _DB_PREFIX_ . 'stock_available
WHERE id_product = ' . (int) $this->id . '
AND id_product_attribute <> 0 ');
if (!$total_quantity) {
Db::getInstance()->update('stock_available', array('quantity' => 0, 'quantity_remainder' => 0), '`id_product` = ' . $this->id);
}
$id_default_attribute = Product::updateDefaultAttribute($this->id);
if ($id_default_attribute) {
$this->cache_default_attribute = $id_default_attribute;
if (!$combination->available_date) {
$this->setAvailableDate();
}
}
if (!empty($id_images)) {
$combination->setImages($id_images);
}
Tools::clearColorListCache($this->id);
if (Configuration::get('PS_DEFAULT_WAREHOUSE_NEW_PRODUCT') != 0 && Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')) {
$warehouse_location_entity = new WarehouseProductLocation();
$warehouse_location_entity->id_product = $this->id;
$warehouse_location_entity->id_product_attribute = (int) $combination->id;
$warehouse_location_entity->id_warehouse = Configuration::get('PS_DEFAULT_WAREHOUSE_NEW_PRODUCT');
$warehouse_location_entity->location = pSQL('');
$warehouse_location_entity->save();
}
return (int) $combination->id;
}
示例2: postProcess
/**
* AdminController::postProcess() override
* @see AdminController::postProcess()
*/
public function postProcess()
{
parent::postProcess();
// Checks access
if (Tools::isSubmit('addStock') && !($this->tabAccess['add'] === '1')) {
$this->errors[] = Tools::displayError('You do not have the required permission to add stock.');
}
if (Tools::isSubmit('removeStock') && !($this->tabAccess['delete'] === '1')) {
$this->errors[] = Tools::displayError('You do not have the required permission to delete stock');
}
if (Tools::isSubmit('transferStock') && !($this->tabAccess['edit'] === '1')) {
$this->errors[] = Tools::displayError('You do not have the required permission to transfer stock.');
}
if (count($this->errors)) {
return;
}
// Global checks when add / remove / transfer product
if ((Tools::isSubmit('addstock') || Tools::isSubmit('removestock') || Tools::isSubmit('transferstock')) && Tools::isSubmit('is_post')) {
// get product ID
$id_product = (int) Tools::getValue('id_product', 0);
if ($id_product <= 0) {
$this->errors[] = Tools::displayError('The selected product is not valid.');
}
// get product_attribute ID
$id_product_attribute = (int) Tools::getValue('id_product_attribute', 0);
// check the product hash
$check = Tools::getValue('check', '');
$check_valid = md5(_COOKIE_KEY_ . $id_product . $id_product_attribute);
if ($check != $check_valid) {
$this->errors[] = Tools::displayError('The selected product is not valid.');
}
// get quantity and check that the post value is really an integer
// If it's not, we have nothing to do
$quantity = Tools::getValue('quantity', 0);
if (!is_numeric($quantity) || (int) $quantity <= 0) {
$this->errors[] = Tools::displayError('The quantity value is not valid.');
}
$quantity = (int) $quantity;
$token = Tools::getValue('token') ? Tools::getValue('token') : $this->token;
$redirect = self::$currentIndex . '&token=' . $token;
}
// Global checks when add / remove product
if ((Tools::isSubmit('addstock') || Tools::isSubmit('removestock')) && Tools::isSubmit('is_post')) {
// get warehouse id
$id_warehouse = (int) Tools::getValue('id_warehouse', 0);
if ($id_warehouse <= 0 || !Warehouse::exists($id_warehouse)) {
$this->errors[] = Tools::displayError('The selected warehouse is not valid.');
}
// get stock movement reason id
$id_stock_mvt_reason = (int) Tools::getValue('id_stock_mvt_reason', 0);
if ($id_stock_mvt_reason <= 0 || !StockMvtReason::exists($id_stock_mvt_reason)) {
$this->errors[] = Tools::displayError('The reason is not valid.');
}
// get usable flag
$usable = Tools::getValue('usable', null);
if (is_null($usable)) {
$this->errors[] = Tools::displayError('You have to specify whether the product quantity is usable for sale on shops or not.');
}
$usable = (bool) $usable;
}
if (Tools::isSubmit('addstock') && Tools::isSubmit('is_post')) {
// get product unit price
$price = str_replace(',', '.', Tools::getValue('price', 0));
if (!is_numeric($price)) {
$this->errors[] = Tools::displayError('The product price is not valid.');
}
$price = round(floatval($price), 6);
// get product unit price currency id
$id_currency = (int) Tools::getValue('id_currency', 0);
if ($id_currency <= 0 || (!($result = Currency::getCurrency($id_currency)) || empty($result))) {
$this->errors[] = Tools::displayError('The selected currency is not valid.');
}
// if all is ok, add stock
if (count($this->errors) == 0) {
$warehouse = new Warehouse($id_warehouse);
// convert price to warehouse currency if needed
if ($id_currency != $warehouse->id_currency) {
// First convert price to the default currency
$price_converted_to_default_currency = Tools::convertPrice($price, $id_currency, false);
// Convert the new price from default currency to needed currency
$price = Tools::convertPrice($price_converted_to_default_currency, $warehouse->id_currency, true);
}
// add stock
$stock_manager = StockManagerFactory::getManager();
if ($stock_manager->addProduct($id_product, $id_product_attribute, $warehouse, $quantity, $id_stock_mvt_reason, $price, $usable)) {
// Create warehouse_product_location entry if we add stock to a new warehouse
$id_wpl = (int) WarehouseProductLocation::getIdByProductAndWarehouse($id_product, $id_product_attribute, $id_warehouse);
if (!$id_wpl) {
$wpl = new WarehouseProductLocation();
$wpl->id_product = (int) $id_product;
$wpl->id_product_attribute = (int) $id_product_attribute;
$wpl->id_warehouse = (int) $id_warehouse;
$wpl->save();
}
StockAvailable::synchronize($id_product);
if (Tools::isSubmit('addstockAndStay')) {
//.........这里部分代码省略.........
示例3: attributeImportOne
protected function attributeImportOne($info, $default_language, &$groups, &$attributes, $regenerate, $shop_is_feature_active, $validateOnly = false)
{
AdminImportController::setDefaultValues($info);
if (!$shop_is_feature_active) {
$info['shop'] = 1;
} elseif (!isset($info['shop']) || empty($info['shop'])) {
$info['shop'] = implode($this->multiple_value_separator, Shop::getContextListShopID());
}
// Get shops for each attributes
$info['shop'] = explode($this->multiple_value_separator, $info['shop']);
$id_shop_list = array();
if (is_array($info['shop']) && count($info['shop'])) {
foreach ($info['shop'] as $shop) {
if (!empty($shop) && !is_numeric($shop)) {
$id_shop_list[] = Shop::getIdByName($shop);
} elseif (!empty($shop)) {
$id_shop_list[] = $shop;
}
}
}
if (isset($info['id_product']) && $info['id_product']) {
$product = new Product((int) $info['id_product'], false, $default_language);
} elseif (Tools::getValue('match_ref') && isset($info['product_reference']) && $info['product_reference']) {
$datas = Db::getInstance()->getRow('
SELECT p.`id_product`
FROM `' . _DB_PREFIX_ . 'product` p
' . Shop::addSqlAssociation('product', 'p') . '
WHERE p.`reference` = "' . pSQL($info['product_reference']) . '"
', false);
if (isset($datas['id_product']) && $datas['id_product']) {
$product = new Product((int) $datas['id_product'], false, $default_language);
}
} else {
return;
}
$id_image = array();
if (isset($info['image_url']) && $info['image_url']) {
$info['image_url'] = explode($this->multiple_value_separator, $info['image_url']);
if (is_array($info['image_url']) && count($info['image_url'])) {
foreach ($info['image_url'] as $key => $url) {
$url = trim($url);
$product_has_images = (bool) Image::getImages($this->context->language->id, $product->id);
$image = new Image();
$image->id_product = (int) $product->id;
$image->position = Image::getHighestPosition($product->id) + 1;
$image->cover = !$product_has_images ? true : false;
if (isset($info['image_alt'])) {
$alt = self::split($info['image_alt']);
if (isset($alt[$key]) && strlen($alt[$key]) > 0) {
$alt = self::createMultiLangField($alt[$key]);
$image->legend = $alt;
}
}
$field_error = $image->validateFields(UNFRIENDLY_ERROR, true);
$lang_field_error = $image->validateFieldsLang(UNFRIENDLY_ERROR, true);
if ($field_error === true && $lang_field_error === true && !$validateOnly && $image->add()) {
$image->associateTo($id_shop_list);
// FIXME: 2s/image !
if (!AdminImportController::copyImg($product->id, $image->id, $url, 'products', !$regenerate)) {
$this->warnings[] = sprintf($this->trans('Error copying image: %s', array(), 'Admin.Parameters.Notification'), $url);
$image->delete();
} else {
$id_image[] = (int) $image->id;
}
// until here
} else {
if (!$validateOnly) {
$this->warnings[] = sprintf($this->trans('%s cannot be saved', array(), 'Admin.Parameters.Notification'), isset($image->id_product) ? ' (' . $image->id_product . ')' : '');
}
if ($field_error !== true || $lang_field_error !== true) {
$this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . mysql_error();
}
}
}
}
} elseif (isset($info['image_position']) && $info['image_position']) {
$info['image_position'] = explode($this->multiple_value_separator, $info['image_position']);
if (is_array($info['image_position']) && count($info['image_position'])) {
foreach ($info['image_position'] as $position) {
// choose images from product by position
$images = $product->getImages($default_language);
if ($images) {
foreach ($images as $row) {
if ($row['position'] == (int) $position) {
$id_image[] = (int) $row['id_image'];
break;
}
}
}
if (empty($id_image)) {
$this->warnings[] = sprintf($this->trans('No image was found for combination with id_product = %s and image position = %s.', array(), 'Admin.Parameters.Notification'), $product->id, (int) $position);
}
}
}
}
$id_attribute_group = 0;
// groups
$groups_attributes = array();
if (isset($info['group'])) {
foreach (explode($this->multiple_value_separator, $info['group']) as $key => $group) {
//.........这里部分代码省略.........
示例4: attributeImport
public function attributeImport()
{
$default_language = Configuration::get('PS_LANG_DEFAULT');
$groups = array();
foreach (AttributeGroup::getAttributesGroups($default_language) as $group) {
$groups[$group['name']] = (int) $group['id_attribute_group'];
}
$attributes = array();
foreach (Attribute::getAttributes($default_language) as $attribute) {
$attributes[$attribute['attribute_group'] . '_' . $attribute['name']] = (int) $attribute['id_attribute'];
}
$this->receiveTab();
$handle = $this->openCsvFile();
AdminImportController::setLocale();
for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, $this->separator); $current_line++) {
if (count($line) == 1 && empty($line[0])) {
continue;
}
if (Tools::getValue('convert')) {
$line = $this->utf8EncodeArray($line);
}
$info = AdminImportController::getMaskedRow($line);
$info = array_map('trim', $info);
AdminImportController::setDefaultValues($info);
if (!Shop::isFeatureActive()) {
$info['shop'] = 1;
} elseif (!isset($info['shop']) || empty($info['shop'])) {
$info['shop'] = implode($this->multiple_value_separator, Shop::getContextListShopID());
}
// Get shops for each attributes
$info['shop'] = explode($this->multiple_value_separator, $info['shop']);
$id_shop_list = array();
if (is_array($info['shop']) && count($info['shop'])) {
foreach ($info['shop'] as $shop) {
if (!empty($shop) && !is_numeric($shop)) {
$id_shop_list[] = Shop::getIdByName($shop);
} elseif (!empty($shop)) {
$id_shop_list[] = $shop;
}
}
}
if (isset($info['id_product'])) {
$product = new Product((int) $info['id_product'], false, $default_language);
} else {
continue;
}
$id_image = null;
//delete existing images if "delete_existing_images" is set to 1
if (array_key_exists('delete_existing_images', $info) && $info['delete_existing_images'] && !isset($this->cache_image_deleted[(int) $product->id])) {
$product->deleteImages();
$this->cache_image_deleted[(int) $product->id] = true;
}
if (isset($info['image_url']) && $info['image_url']) {
$product_has_images = (bool) Image::getImages($this->context->language->id, $product->id);
$url = $info['image_url'];
$image = new Image();
$image->id_product = (int) $product->id;
$image->position = Image::getHighestPosition($product->id) + 1;
$image->cover = !$product_has_images ? true : false;
$field_error = $image->validateFields(UNFRIENDLY_ERROR, true);
$lang_field_error = $image->validateFieldsLang(UNFRIENDLY_ERROR, true);
if ($field_error === true && $lang_field_error === true && $image->add()) {
$image->associateTo($id_shop_list);
if (!AdminImportController::copyImg($product->id, $image->id, $url, 'products', !Tools::getValue('regenerate'))) {
$this->warnings[] = sprintf(Tools::displayError('Error copying image: %s'), $url);
$image->delete();
} else {
$id_image = array($image->id);
}
} else {
$this->warnings[] = sprintf(Tools::displayError('%s cannot be saved'), isset($image->id_product) ? ' (' . $image->id_product . ')' : '');
$this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . mysql_error();
}
} elseif (isset($info['image_position']) && $info['image_position']) {
$images = $product->getImages($default_language);
if ($images) {
foreach ($images as $row) {
if ($row['position'] == (int) $info['image_position']) {
$id_image = array($row['id_image']);
break;
}
}
}
if (!$id_image) {
$this->warnings[] = sprintf(Tools::displayError('No image was found for combination with id_product = %s and image position = %s.'), $product->id, (int) $info['image_position']);
}
}
$id_attribute_group = 0;
// groups
$groups_attributes = array();
if (isset($info['group'])) {
foreach (explode($this->multiple_value_separator, $info['group']) as $key => $group) {
if (empty($group)) {
continue;
}
$tab_group = explode(':', $group);
$group = trim($tab_group[0]);
if (!isset($tab_group[1])) {
$type = 'select';
} else {
//.........这里部分代码省略.........
示例5: productImport
//.........这里部分代码省略.........
} else {
$product->id_shop_default = (int) Context::getContext()->shop->id;
}
// link product to shops
$product->id_shop_list = array();
foreach (explode($this->multiple_value_separator, $product->shop) as $shop) {
if (!empty($shop) && !is_numeric($shop)) {
$product->id_shop_list[] = Shop::getIdByName($shop);
} elseif (!empty($shop)) {
$product->id_shop_list[] = $shop;
}
}
if ((int) $product->id_tax_rules_group != 0) {
if (Validate::isLoadedObject(new TaxRulesGroup($product->id_tax_rules_group))) {
$address = $this->context->shop->getAddress();
$tax_manager = TaxManagerFactory::getManager($address, $product->id_tax_rules_group);
$product_tax_calculator = $tax_manager->getTaxCalculator();
$product->tax_rate = $product_tax_calculator->getTotalRate();
} else {
$this->addProductWarning('id_tax_rules_group', $product->id_tax_rules_group, Tools::displayError('Invalid tax rule group ID. You first need to create a group with this ID.'));
}
}
if (isset($product->manufacturer) && is_numeric($product->manufacturer) && Manufacturer::manufacturerExists((int) $product->manufacturer)) {
$product->id_manufacturer = (int) $product->manufacturer;
} elseif (isset($product->manufacturer) && is_string($product->manufacturer) && !empty($product->manufacturer)) {
if ($manufacturer = Manufacturer::getIdByName($product->manufacturer)) {
$product->id_manufacturer = (int) $manufacturer;
} else {
$manufacturer = new Manufacturer();
$manufacturer->name = $product->manufacturer;
if (($field_error = $manufacturer->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $manufacturer->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && $manufacturer->add()) {
$product->id_manufacturer = (int) $manufacturer->id;
} else {
$this->errors[] = sprintf(Tools::displayError('%1$s (ID: %2$s) cannot be saved'), $manufacturer->name, isset($manufacturer->id) && !empty($manufacturer->id) ? $manufacturer->id : 'null');
$this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
}
}
}
if (isset($product->supplier) && is_numeric($product->supplier) && Supplier::supplierExists((int) $product->supplier)) {
$product->id_supplier = (int) $product->supplier;
} elseif (isset($product->supplier) && is_string($product->supplier) && !empty($product->supplier)) {
if ($supplier = Supplier::getIdByName($product->supplier)) {
$product->id_supplier = (int) $supplier;
} else {
$supplier = new Supplier();
$supplier->name = $product->supplier;
$supplier->active = true;
if (($field_error = $supplier->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $supplier->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && $supplier->add()) {
$product->id_supplier = (int) $supplier->id;
$supplier->associateTo($product->id_shop_list);
} else {
$this->errors[] = sprintf(Tools::displayError('%1$s (ID: %2$s) cannot be saved'), $supplier->name, isset($supplier->id) && !empty($supplier->id) ? $supplier->id : 'null');
$this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
}
}
}
if (isset($product->price_tex) && !isset($product->price_tin)) {
$product->price = $product->price_tex;
} elseif (isset($product->price_tin) && !isset($product->price_tex)) {
$product->price = $product->price_tin;
// If a tax is already included in price, withdraw it from price
if ($product->tax_rate) {
$product->price = (double) number_format($product->price / (1 + $product->tax_rate / 100), 6, '.', '');
}
} elseif (isset($product->price_tin) && isset($product->price_tex)) {
$product->price = $product->price_tex;
示例6: attributeImport
public function attributeImport()
{
$default_language = Configuration::get('PS_LANG_DEFAULT');
$groups = array();
foreach (AttributeGroup::getAttributesGroups($default_language) as $group) {
$groups[$group['name']] = (int) $group['id_attribute_group'];
}
$attributes = array();
foreach (Attribute::getAttributes($default_language) as $attribute) {
$attributes[$attribute['attribute_group'] . '_' . $attribute['name']] = (int) $attribute['id_attribute'];
}
$this->receiveTab();
$handle = $this->openCsvFile();
AdminImportController::setLocale();
for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, $this->separator); $current_line++) {
if (count($line) == 1 && empty($line[0])) {
continue;
}
if (Tools::getValue('convert')) {
$line = $this->utf8EncodeArray($line);
}
$info = AdminImportController::getMaskedRow($line);
$info = array_map('trim', $info);
if (self::ignoreRow($info)) {
continue;
}
AdminImportController::setDefaultValues($info);
if (!Shop::isFeatureActive()) {
$info['shop'] = 1;
} elseif (!isset($info['shop']) || empty($info['shop'])) {
$info['shop'] = implode($this->multiple_value_separator, Shop::getContextListShopID());
}
$info['shop'] = explode($this->multiple_value_separator, $info['shop']);
$id_shop_list = array();
if (is_array($info['shop']) && count($info['shop'])) {
foreach ($info['shop'] as $shop) {
if (!empty($shop) && !is_numeric($shop)) {
$id_shop_list[] = Shop::getIdByName($shop);
} elseif (!empty($shop)) {
$id_shop_list[] = $shop;
}
}
}
if (isset($info['id_product']) && is_string($info['id_product'])) {
$prod = self::findProductByName($default_language, $info['id_product']);
if ($prod['id_product']) {
$info['id_product'] = $prod['id_product'];
} else {
unset($info['id_product']);
}
}
if (!isset($info['id_product']) && Tools::getValue('match_ref') && isset($info['product_reference']) && $info['product_reference']) {
$datas = Db::getInstance()->getRow('
SELECT p.`id_product`
FROM `' . _DB_PREFIX_ . 'product` p
' . Shop::addSqlAssociation('product', 'p') . '
WHERE p.`reference` = "' . pSQL($info['product_reference']) . '"
');
if (isset($datas['id_product']) && $datas['id_product']) {
$info['id_product'] = $datas['id_product'];
}
}
if (isset($info['id_product'])) {
$product = new Product((int) $info['id_product'], false, $default_language);
} else {
continue;
}
$id_image = array();
if (array_key_exists('delete_existing_images', $info) && $info['delete_existing_images'] && !isset($this->cache_image_deleted[(int) $product->id])) {
$product->deleteImages();
$this->cache_image_deleted[(int) $product->id] = true;
}
if (isset($info['image_url']) && $info['image_url']) {
$info['image_url'] = explode(',', $info['image_url']);
if (is_array($info['image_url']) && count($info['image_url'])) {
foreach ($info['image_url'] as $url) {
$url = trim($url);
$product_has_images = (bool) Image::getImages($this->context->language->id, $product->id);
$image = new Image();
$image->id_product = (int) $product->id;
$image->position = Image::getHighestPosition($product->id) + 1;
$image->cover = !$product_has_images ? true : false;
$field_error = $image->validateFields(UNFRIENDLY_ERROR, true);
$lang_field_error = $image->validateFieldsLang(UNFRIENDLY_ERROR, true);
if ($field_error === true && $lang_field_error === true && $image->add()) {
$image->associateTo($id_shop_list);
if (!AdminImportController::copyImg($product->id, $image->id, $url, 'products', !Tools::getValue('regenerate'))) {
$this->warnings[] = sprintf(Tools::displayError('Error copying image: %s'), $url);
$image->delete();
} else {
$id_image[] = (int) $image->id;
}
} else {
$this->warnings[] = sprintf(Tools::displayError('%s cannot be saved'), isset($image->id_product) ? ' (' . $image->id_product . ')' : '');
$this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . mysql_error();
}
}
}
} elseif (isset($info['image_position']) && $info['image_position']) {
$info['image_position'] = explode(',', $info['image_position']);
//.........这里部分代码省略.........
示例7: postProcess
public function postProcess()
{
$this->adminControllerPostProcess();
if (Tools::isSubmit('addStock') && !($this->tabAccess['add'] === '1')) {
$this->errors[] = Tools::displayError('You do not have the required permission to add stock.');
}
if (Tools::isSubmit('removeStock') && !($this->tabAccess['delete'] === '1')) {
$this->errors[] = Tools::displayError('You do not have the required permission to delete stock');
}
if (Tools::isSubmit('transferStock') && !($this->tabAccess['edit'] === '1')) {
$this->errors[] = Tools::displayError('You do not have the required permission to transfer stock.');
}
if (count($this->errors)) {
return;
}
if ((Tools::isSubmit('addstock') || Tools::isSubmit('removestock') || Tools::isSubmit('transferstock')) && Tools::isSubmit('is_post')) {
$id_product = (int) Tools::getValue('id_product', 0);
if ($id_product <= 0) {
$this->errors[] = Tools::displayError('The selected product is not valid.');
}
$id_product_attribute = (int) Tools::getValue('id_product_attribute', 0);
$check = Tools::getValue('check', '');
$check_valid = md5(_COOKIE_KEY_ . $id_product . $id_product_attribute);
if ($check != $check_valid) {
$this->errors[] = Tools::displayError('The selected product is not valid.');
}
$quantity = Tools::getValue('quantity', 0);
$quantity = PP::normalizeProductQty($quantity, $id_product);
if (!is_numeric($quantity) || $quantity <= 0) {
$this->errors[] = Tools::displayError('The quantity value is not valid.');
}
$token = Tools::getValue('token') ? Tools::getValue('token') : $this->token;
$redirect = self::$currentIndex . '&token=' . $token;
}
if ((Tools::isSubmit('addstock') || Tools::isSubmit('removestock')) && Tools::isSubmit('is_post')) {
$id_warehouse = (int) Tools::getValue('id_warehouse', 0);
if ($id_warehouse <= 0 || !Warehouse::exists($id_warehouse)) {
$this->errors[] = Tools::displayError('The selected warehouse is not valid.');
}
$id_stock_mvt_reason = (int) Tools::getValue('id_stock_mvt_reason', 0);
if ($id_stock_mvt_reason <= 0 || !StockMvtReason::exists($id_stock_mvt_reason)) {
$this->errors[] = Tools::displayError('The reason is not valid.');
}
$usable = Tools::getValue('usable', null);
if (is_null($usable)) {
$this->errors[] = Tools::displayError('You have to specify whether the product quantity is usable for sale on shops or not.');
}
$usable = (bool) $usable;
}
if (Tools::isSubmit('addstock') && Tools::isSubmit('is_post')) {
$price = str_replace(',', '.', Tools::getValue('price', 0));
if (!is_numeric($price)) {
$this->errors[] = Tools::displayError('The product price is not valid.');
}
$price = round((double) $price, 6);
$id_currency = (int) Tools::getValue('id_currency', 0);
if ($id_currency <= 0 || (!($result = Currency::getCurrency($id_currency)) || empty($result))) {
$this->errors[] = Tools::displayError('The selected currency is not valid.');
}
if (count($this->errors) == 0) {
$warehouse = new Warehouse($id_warehouse);
if ($id_currency != $warehouse->id_currency) {
$price_converted_to_default_currency = Tools::convertPrice($price, $id_currency, false);
$price = Tools::convertPrice($price_converted_to_default_currency, $warehouse->id_currency, true);
}
$stock_manager = StockManagerFactory::getManager();
if ($stock_manager->addProduct($id_product, $id_product_attribute, $warehouse, $quantity, $id_stock_mvt_reason, $price, $usable)) {
$id_wpl = (int) WarehouseProductLocation::getIdByProductAndWarehouse($id_product, $id_product_attribute, $id_warehouse);
if (!$id_wpl) {
$wpl = new WarehouseProductLocation();
$wpl->id_product = (int) $id_product;
$wpl->id_product_attribute = (int) $id_product_attribute;
$wpl->id_warehouse = (int) $id_warehouse;
$wpl->save();
}
StockAvailable::synchronize($id_product);
if (Tools::isSubmit('addstockAndStay')) {
$redirect = self::$currentIndex . '&id_product=' . (int) $id_product;
if ($id_product_attribute) {
$redirect .= '&id_product_attribute=' . (int) $id_product_attribute;
}
$redirect .= '&addstock&token=' . $token;
}
Tools::redirectAdmin($redirect . '&conf=1');
} else {
$this->errors[] = Tools::displayError('An error occurred. No stock was added.');
}
}
}
if (Tools::isSubmit('removestock') && Tools::isSubmit('is_post')) {
if (count($this->errors) == 0) {
$warehouse = new Warehouse($id_warehouse);
$stock_manager = StockManagerFactory::getManager();
$removed_products = $stock_manager->removeProduct($id_product, $id_product_attribute, $warehouse, $quantity, $id_stock_mvt_reason, $usable);
if (count($removed_products) > 0) {
StockAvailable::synchronize($id_product);
Tools::redirectAdmin($redirect . '&conf=2');
} else {
$physical_quantity_in_stock = (int) $stock_manager->getProductPhysicalQuantities($id_product, $id_product_attribute, array($warehouse->id), false);
$usable_quantity_in_stock = (int) $stock_manager->getProductPhysicalQuantities($id_product, $id_product_attribute, array($warehouse->id), true);
//.........这里部分代码省略.........