Fix item number lookup in sales/receivings (#4212) (#4250)

* Fix item number lookup in sales/receivings (#4212)

* Remove item_number check in exists()
This commit is contained in:
jekkos
2025-05-30 22:29:35 +02:00
committed by GitHub
parent e1fedab9b7
commit 29c3c55fcc
4 changed files with 61 additions and 77 deletions

View File

@@ -168,7 +168,7 @@ class Receivings extends Secure_Controller
$data = []; $data = [];
$mode = $this->receiving_lib->get_mode(); $mode = $this->receiving_lib->get_mode();
$item_id_or_number_or_item_kit_or_receipt = (int)$this->request->getPost('item', FILTER_SANITIZE_FULL_SPECIAL_CHARS); $item_id_or_number_or_item_kit_or_receipt = $this->request->getPost('item', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$this->token_lib->parse_barcode($quantity, $price, $item_id_or_number_or_item_kit_or_receipt); $this->token_lib->parse_barcode($quantity, $price, $item_id_or_number_or_item_kit_or_receipt);
$quantity = ($mode == 'receive' || $mode == 'requisition') ? $quantity : -$quantity; $quantity = ($mode == 'receive' || $mode == 'requisition') ? $quantity : -$quantity;
$item_location = $this->receiving_lib->get_stock_source(); $item_location = $this->receiving_lib->get_stock_source();

View File

@@ -256,117 +256,99 @@ class Receiving_lib
// TODO: This array signature needs to be reworked. It's way too long. Perhaps an object needs to be passed rather than these? // TODO: This array signature needs to be reworked. It's way too long. Perhaps an object needs to be passed rather than these?
/** /**
* @param int $item_id * @param string $itemId
* @param int $quantity * @param int $quantity
* @param int|null $item_location * @param int|null $itemLocation
* @param float $discount * @param float $discount
* @param int $discount_type * @param int $discountType
* @param float|null $price * @param float|null $price
* @param string|null $description * @param string|null $description
* @param string|null $serialnumber * @param string|null $serialNumber
* @param float|null $receiving_quantity * @param float|null $receivingQuantity
* @param int|null $receiving_id * @param int|null $receivingId
* @param bool $include_deleted * @param bool $includeDeleted
* @return bool * @return bool
*/ */
public function add_item(int $item_id, int $quantity = 1, ?int $item_location = null, float $discount = 0, int $discount_type = 0, ?float $price = null, ?string $description = null, ?string $serialnumber = null, ?float $receiving_quantity = null, ?int $receiving_id = null, bool $include_deleted = false): bool public function add_item(string $itemId, int $quantity = 1, ?int $itemLocation = null, float $discount = 0, int $discountType = 0, ?float $price = null, ?string $description = null, ?string $serialNumber = null, ?float $receivingQuantity = null, ?int $receivingId = null, bool $includeDeleted = false): bool
{ {
$config = config(OSPOS::class)->settings; $config = config(OSPOS::class)->settings;
$itemInfo = $this->item->get_info_by_id_or_number($itemId, $includeDeleted);
// Make sure item exists in database. if (empty($itemInfo)) {
if (!$this->item->exists($item_id, $include_deleted)) { return false;
// Try to get item id given an item_number
$item_id = $this->item->get_item_id($item_id, $include_deleted);
if (!$item_id) {
return false;
}
} }
// Get items in the receiving so far. $itemId = $itemInfo->item_id;
$items = $this->get_cart(); $items = $this->get_cart();
// We need to loop through all items in the cart. $maxKey = 0;
// If the item is already there, get it's key($updatekey). $itemAlreadyInSale = false;
// We also need to get the next key that we are going to use in case we need to add the $updateKey = 0;
// item to the list. Since items can be deleted, we can't use a count. we use the highest key + 1.
$maxkey = 0; // Highest key so far
$itemalreadyinsale = false; // We did not find the item yet.
$updatekey = 0; // Key to use to update(quantity)
foreach ($items as $item) { foreach ($items as $item) {
// We primed the loop so maxkey is 0 the first time. if ($maxKey <= $item['line']) {
// Also, we have stored the key in the element itself, so we can compare. $maxKey = $item['line'];
// There is an array public function to get the associated key for an element, but I like it better
// like that!
if ($maxkey <= $item['line']) {
$maxkey = $item['line'];
} }
if ($item['item_id'] == $item_id && $item['item_location'] == $item_location) { if ($item['item_id'] == $itemId && $item['item_location'] == $itemLocation) {
$itemalreadyinsale = true; $itemAlreadyInSale = true;
$updatekey = $item['line']; $updateKey = $item['line'];
} }
} }
$insertkey = $maxkey + 1; $insertKey = $maxKey + 1;
$item_info = $this->item->get_info($item_id); $itemInfo = $this->item->get_info((int) $itemId);
// Array records are identified by $insertkey and item_id is just another field. $price = $price != null ? $price : $itemInfo->cost_price;
$price = $price != null ? $price : $item_info->cost_price;
if ($config['multi_pack_enabled']) { if ($config['multi_pack_enabled']) {
$item_info->name .= NAME_SEPARATOR . $item_info->pack_name; $itemInfo->name .= NAME_SEPARATOR . $itemInfo->pack_name;
} }
if ($item_info->receiving_quantity == 0 || $item_info->receiving_quantity == 1) { if ($itemInfo->receiving_quantity == 0 || $itemInfo->receiving_quantity == 1) {
$receiving_quantity_choices = [1 => 'x1']; $receivingQuantityChoices = [1 => 'x1'];
} else { } else {
$receiving_quantity_choices = [ $receivingQuantityChoices = [
to_quantity_decimals($item_info->receiving_quantity) => 'x' . to_quantity_decimals($item_info->receiving_quantity), to_quantity_decimals($itemInfo->receiving_quantity) => 'x' . to_quantity_decimals($itemInfo->receiving_quantity),
1 => 'x1' 1 => 'x1'
]; ];
} }
if (is_null($receiving_quantity)) { if (is_null($receivingQuantity)) {
$receiving_quantity = $item_info->receiving_quantity; $receivingQuantity = $itemInfo->receiving_quantity;
} }
$attribute_links = $this->attribute->get_link_values($item_id, 'receiving_id', $receiving_id, Attribute::SHOW_IN_RECEIVINGS)->getRowObject(); $attributeLinks = $this->attribute->get_link_values((int) $itemId, 'receiving_id', $receivingId, Attribute::SHOW_IN_RECEIVINGS)->getRowObject();
$item = [ $item = [
$insertkey => [ $insertKey => [
'item_id' => $item_id, 'item_id' => $itemId,
'item_location' => $item_location, 'item_location' => $itemLocation,
'item_number' => $item_info->item_number, 'item_number' => $itemInfo->item_number,
'stock_name' => $this->stock_location->get_location_name($item_location), 'stock_name' => $this->stock_location->get_location_name($itemLocation),
'line' => $insertkey, 'line' => $insertKey,
'name' => $item_info->name, 'name' => $itemInfo->name,
'description' => $description != null ? $description : $item_info->description, 'description' => $description != null ? $description : $itemInfo->description,
'serialnumber' => $serialnumber != null ? $serialnumber : '', 'serialnumber' => $serialNumber != null ? $serialNumber : '',
'attribute_values' => $attribute_links->attribute_values, 'attribute_values' => $attributeLinks->attribute_values,
'attribute_dtvalues' => $attribute_links->attribute_dtvalues, 'attribute_dtvalues' => $attributeLinks->attribute_dtvalues,
'allow_alt_description' => $item_info->allow_alt_description, 'allow_alt_description' => $itemInfo->allow_alt_description,
'is_serialized' => $item_info->is_serialized, 'is_serialized' => $itemInfo->is_serialized,
'quantity' => $quantity, 'quantity' => $quantity,
'discount' => $discount, 'discount' => $discount,
'discount_type' => $discount_type, 'discount_type' => $discountType,
'in_stock' => $this->item_quantity->get_item_quantity($item_id, $item_location)->quantity, 'in_stock' => $this->item_quantity->get_item_quantity((int) $itemId, $itemLocation)->quantity,
'price' => $price, 'price' => $price,
'receiving_quantity' => $receiving_quantity, 'receiving_quantity' => $receivingQuantity,
'receiving_quantity_choices' => $receiving_quantity_choices, 'receiving_quantity_choices' => $receivingQuantityChoices,
'total' => $this->get_item_total($quantity, $price, $discount, $discount_type, $receiving_quantity) 'total' => $this->get_item_total($quantity, $price, $discount, $discountType, $receivingQuantity)
] ]
]; ];
// Item already exists if ($itemAlreadyInSale) {
if ($itemalreadyinsale) { // TODO: This variable does not adhere to naming conventions. $items[$updateKey]['quantity'] += $quantity;
$items[$updatekey]['quantity'] += $quantity; $items[$updateKey]['total'] = $this->get_item_total($items[$updateKey]['quantity'], $price, $discount, $discountType, $items[$updateKey]['receiving_quantity']);
$items[$updatekey]['total'] = $this->get_item_total($items[$updatekey]['quantity'], $price, $discount, $discount_type, $items[$updatekey]['receiving_quantity']);
} else { } else {
// Add to existing array
$items += $item; $items += $item;
} }

View File

@@ -944,7 +944,7 @@ class Sale_lib
// TODO: this function needs to be reworked... way too many parameters. Also, optional parameters must go after mandatory parameters. // TODO: this function needs to be reworked... way too many parameters. Also, optional parameters must go after mandatory parameters.
/** /**
* @param int $item_id * @param string $item_id
* @param int $item_location * @param int $item_location
* @param string $quantity * @param string $quantity
* @param string $discount * @param string $discount
@@ -961,7 +961,7 @@ class Sale_lib
* @param bool|null $line * @param bool|null $line
* @return bool * @return bool
*/ */
public function add_item(int &$item_id, int $item_location, string $quantity = '1', string &$discount = '0.0', int $discount_type = 0, int $price_mode = PRICE_MODE_STANDARD, ?int $kit_price_option = null, ?int $kit_print_option = null, ?string $price_override = null, ?string $description = null, ?string $serialnumber = null, ?int $sale_id = null, bool $include_deleted = false, ?bool $print_option = null, ?bool $line = null): bool public function add_item(string &$item_id, int $item_location, string $quantity = '1', string &$discount = '0.0', int $discount_type = 0, int $price_mode = PRICE_MODE_STANDARD, ?int $kit_price_option = null, ?int $kit_print_option = null, ?string $price_override = null, ?string $description = null, ?string $serialnumber = null, ?int $sale_id = null, bool $include_deleted = false, ?bool $print_option = null, ?bool $line = null): bool
{ {
$item_info = $this->item->get_info_by_id_or_number($item_id, $include_deleted); $item_info = $this->item->get_info_by_id_or_number($item_id, $include_deleted);

View File

@@ -47,10 +47,11 @@ class Item extends Model
/** /**
* Determines if a given item_id is an item * Determines if a given item_id is an item
*/ */
public function exists(int $item_id, bool $ignore_deleted = false, bool $deleted = false): bool public function exists(string $item_id, bool $ignore_deleted = false, bool $deleted = false): bool
{ {
$builder = $this->db->table('items'); $builder = $this->db->table('items');
$builder->where('item_id', $item_id); $builder->where('item_id', $item_id);
$builder->orWhere('item_number', $item_id);
if (!$ignore_deleted) { if (!$ignore_deleted) {
$builder->where('deleted', $deleted); $builder->where('deleted', $deleted);
@@ -336,7 +337,7 @@ class Item extends Model
/** /**
* Gets information about a particular item by item id or number * Gets information about a particular item by item id or number
*/ */
public function get_info_by_id_or_number(int $item_id, bool $include_deleted = true) public function get_info_by_id_or_number(string $item_id, bool $include_deleted = true)
{ {
$builder = $this->db->table('items'); $builder = $this->db->table('items');
$builder->groupStart(); $builder->groupStart();
@@ -370,11 +371,12 @@ class Item extends Model
/** /**
* Get an item id given an item number * Get an item id given an item number
*/ */
public function get_item_id(string $item_number, bool $ignore_deleted = false, bool $deleted = false): bool public function get_item_id(string $item_number, bool $ignore_deleted = false, bool $deleted = false): bool|int
{ {
$builder = $this->db->table('items'); $builder = $this->db->table('items');
$builder->join('suppliers', 'suppliers.person_id = items.supplier_id', 'left'); $builder->join('suppliers', 'suppliers.person_id = items.supplier_id', 'left');
$builder->where('item_number', $item_number); $builder->where('item_number', $item_number);
$builder->orWhere('item_id', $item_number);
if (!$ignore_deleted) { if (!$ignore_deleted) {
$builder->where('items.deleted', $deleted); $builder->where('items.deleted', $deleted);