From 9b8081191d180348009db4e6904e294ec1a78009 Mon Sep 17 00:00:00 2001 From: Steve Ireland Date: Thu, 20 Apr 2023 20:43:06 -0400 Subject: [PATCH] For #3698. I'm pleased to announce that this set of changes seems to allow a simple order to be completed. --- app/Controllers/Sales.php | 3 +- .../20230412000000_add_missing_config.php | 1 + app/Helpers/locale_helper.php | 2 +- app/Libraries/Barcode_lib.php | 6 ++- .../Barcodes}/BarcodeBase.php | 4 +- .../Barcodes}/Code128.php | 4 +- .../Barcodes}/Code39.php | 4 +- .../barcodes => Libraries/Barcodes}/Ean13.php | 2 +- .../barcodes => Libraries/Barcodes}/Ean8.php | 2 +- app/Libraries/Sale_lib.php | 4 +- app/Models/Attribute.php | 38 ++++++++++++++++--- app/Models/Sale.php | 19 ++++------ app/Views/sales/register.php | 6 +-- 13 files changed, 61 insertions(+), 34 deletions(-) rename app/{Views/barcodes => Libraries/Barcodes}/BarcodeBase.php (99%) rename app/{Views/barcodes => Libraries/Barcodes}/Code128.php (99%) rename app/{Views/barcodes => Libraries/Barcodes}/Code39.php (99%) rename app/{Views/barcodes => Libraries/Barcodes}/Ean13.php (99%) rename app/{Views/barcodes => Libraries/Barcodes}/Ean8.php (99%) diff --git a/app/Controllers/Sales.php b/app/Controllers/Sales.php index b84dd1675..27bae011b 100644 --- a/app/Controllers/Sales.php +++ b/app/Controllers/Sales.php @@ -634,10 +634,9 @@ class Sales extends Secure_Controller * @return void * @throws ReflectionException */ - public function complete(): void //TODO: this function is huge. Probably should be refactored. + public function postComplete(): void //TODO: this function is huge. Probably should be refactored. { $sale_id = $this->sale_lib->get_sale_id(); - $sale_type = $this->sale_lib->get_sale_type(); //TODO: This variable gets overwritten way down below before being used. $data = []; $data['dinner_table'] = $this->sale_lib->get_dinner_table(); diff --git a/app/Database/Migrations/20230412000000_add_missing_config.php b/app/Database/Migrations/20230412000000_add_missing_config.php index dfbd40b50..c4f019e6d 100644 --- a/app/Database/Migrations/20230412000000_add_missing_config.php +++ b/app/Database/Migrations/20230412000000_add_missing_config.php @@ -9,6 +9,7 @@ class Migration_add_missing_config extends Migration public function up(): void { $image_values = [ + ['key' => 'account_number', 'value' => ''], // This has no current maintenance, but it's used in Sales ['key' => 'category_dropdown', 'value' => ''], ['key' => 'smtp_host', 'value' => ''], ['key' => 'smtp_user', 'value' => ''], diff --git a/app/Helpers/locale_helper.php b/app/Helpers/locale_helper.php index 3f6cc18d3..133de9ae0 100644 --- a/app/Helpers/locale_helper.php +++ b/app/Helpers/locale_helper.php @@ -313,7 +313,7 @@ function tax_decimals(): int function to_date(int $date = DEFAULT_DATE): string { $config = config('OSPOS')->settings; - return date($config['dateformat, $date']); + return date($config['dateformat'], $date); } function to_datetime(int $datetime = DEFAULT_DATETIME): string diff --git a/app/Libraries/Barcode_lib.php b/app/Libraries/Barcode_lib.php index 092ea2473..e24de42ba 100644 --- a/app/Libraries/Barcode_lib.php +++ b/app/Libraries/Barcode_lib.php @@ -1,9 +1,13 @@ \ No newline at end of file +?> diff --git a/app/Views/barcodes/Code128.php b/app/Libraries/Barcodes/Code128.php similarity index 99% rename from app/Views/barcodes/Code128.php rename to app/Libraries/Barcodes/Code128.php index f3e320407..c615cb77b 100644 --- a/app/Views/barcodes/Code128.php +++ b/app/Libraries/Barcodes/Code128.php @@ -9,7 +9,7 @@ * Minimum Requirement: PHP 5.3.0 */ -namespace emberlabs\Barcode; +namespace App\Libraries\Barcodes; /** * emberlabs Barcode Creator - Code128 @@ -322,4 +322,4 @@ class Code128 extends BarcodeBase } } } -?> \ No newline at end of file +?> diff --git a/app/Views/barcodes/Code39.php b/app/Libraries/Barcodes/Code39.php similarity index 99% rename from app/Views/barcodes/Code39.php rename to app/Libraries/Barcodes/Code39.php index 73dded99f..13c163b2a 100644 --- a/app/Views/barcodes/Code39.php +++ b/app/Libraries/Barcodes/Code39.php @@ -9,7 +9,7 @@ * Minimum Requirement: PHP 5.3.0 */ -namespace emberlabs\Barcode; +namespace App\Libraries\Barcodes; /** * emberlabs Barcode Creator - Code39 @@ -181,4 +181,4 @@ class Code39 extends BarcodeBase } } } -?> \ No newline at end of file +?> diff --git a/app/Views/barcodes/Ean13.php b/app/Libraries/Barcodes/Ean13.php similarity index 99% rename from app/Views/barcodes/Ean13.php rename to app/Libraries/Barcodes/Ean13.php index 0d37d2a22..46f1b77f0 100644 --- a/app/Views/barcodes/Ean13.php +++ b/app/Libraries/Barcodes/Ean13.php @@ -30,7 +30,7 @@ * @link http://pear.php.net/package/Image_Barcode2 */ -namespace emberlabs\Barcode; +namespace App\Libraries\Barcodes; /** * emberlabs Barcode Creator - Ean13 diff --git a/app/Views/barcodes/Ean8.php b/app/Libraries/Barcodes/Ean8.php similarity index 99% rename from app/Views/barcodes/Ean8.php rename to app/Libraries/Barcodes/Ean8.php index b6a9e2c6d..0f946885f 100644 --- a/app/Views/barcodes/Ean8.php +++ b/app/Libraries/Barcodes/Ean8.php @@ -30,7 +30,7 @@ * @link http://pear.php.net/package/Image_Barcode2 */ -namespace emberlabs\Barcode; +namespace App\Libraries\Barcodes; /** * emberlabs Barcode Creator - Ean8 diff --git a/app/Libraries/Sale_lib.php b/app/Libraries/Sale_lib.php index 68a153699..3a4c30e48 100644 --- a/app/Libraries/Sale_lib.php +++ b/app/Libraries/Sale_lib.php @@ -774,7 +774,7 @@ class Sale_lib $this->session->set('sales_giftcard_remainder', $value); } - public function get_giftcard_remainder(): string + public function get_giftcard_remainder(): ?string { return $this->session->get('sales_giftcard_remainder'); } @@ -789,7 +789,7 @@ class Sale_lib $this->session->set('sales_rewards_remainder', $value); } - public function get_rewards_remainder(): string + public function get_rewards_remainder(): ?string { return $this->session->get('sales_rewards_remainder'); } diff --git a/app/Models/Attribute.php b/app/Models/Attribute.php index f804d7ad9..a6e78b5b9 100644 --- a/app/Models/Attribute.php +++ b/app/Models/Attribute.php @@ -4,6 +4,7 @@ namespace App\Models; use CodeIgniter\Database\ResultInterface; use CodeIgniter\Model; +use CodeIgniter\Database\RawSql; use DateTime; use stdClass; use ReflectionClass; @@ -683,13 +684,38 @@ class Attribute extends Model public function copy_attribute_links(int $item_id, string $sale_receiving_fk, int $id): void { -//TODO: this likely needs to be rewritten as two different queries rather than a subquery within a query. Then use query_builder for both. - $query = 'INSERT INTO ' . $this->db->prefixTable('attribute_links') . ' (item_id, definition_id, attribute_id, ' . $sale_receiving_fk . ') '; - $query .= 'SELECT ' . $this->db->escape($item_id) . ', definition_id, attribute_id, ' . $this->db->escape($id); - $query .= 'FROM ' . $this->db->prefixTable('attribute_links'); - $query .= 'WHERE item_id = ' . $this->db->escape($item_id); + + // ERROR - 2023-04-19 21:39:45 --> mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'item_id = 1 AND sale_id IS NULL AND receiving_id IS NULL' at line 1 in C:\vroots\osposci4\vendor\codeigniter4\framework\system\Database\MySQLi\Connection.php:295 + //Stack trace: + //#0 C:\vroots\osposci4\vendor\codeigniter4\framework\system\Database\MySQLi\Connection.php(295): mysqli->query('INSERT INTO osp...', 0) + //#1 C:\vroots\osposci4\vendor\codeigniter4\framework\system\Database\BaseConnection.php(691): CodeIgniter\Database\MySQLi\Connection->execute('INSERT INTO osp...') + //#2 C:\vroots\osposci4\vendor\codeigniter4\framework\system\Database\BaseConnection.php(605): CodeIgniter\Database\BaseConnection->simpleQuery('INSERT INTO osp...') + //#3 C:\vroots\osposci4\app\Models\Attribute.php(692): CodeIgniter\Database\BaseConnection->query('INSERT INTO osp...') + //#4 C:\vroots\osposci4\app\Models\Sale.php(814): App\Models\Attribute->copy_attribute_links(1, 'sale_id', 4) + //#5 C:\vroots\osposci4\app\Controllers\Sales.php(874): App\Models\Sale->save_value(4, '0', Array, 2, 1, '', NULL, NULL, NULL, 0, Array, NULL, Array) + //#6 C:\vroots\osposci4\vendor\codeigniter4\framework\system\CodeIgniter.php(934): App\Controllers\Sales->postComplete() + //#7 C:\vroots\osposci4\vendor\codeigniter4\framework\system\CodeIgniter.php(499): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\Sales)) + //#8 C:\vroots\osposci4\vendor\codeigniter4\framework\system\CodeIgniter.php(368): CodeIgniter\CodeIgniter->handleRequest(NULL, Object(Config\Cache), false) + //#9 C:\vroots\osposci4\public\index.php(67): CodeIgniter\CodeIgniter->run() + //#10 {main} + +// $this->db->query( +// 'INSERT INTO ' . $this->db->dbprefix('attribute_links') . ' (item_id, definition_id, attribute_id, ' . $sale_receiving_fk . ') +// SELECT ' . $this->db->escape($item_id) . ', definition_id, attribute_id, ' . $this->db->escape($id) . ' +// FROM ' . $this->db->dbprefix('attribute_links') . ' +// WHERE item_id = ' . $this->db->escape($item_id) . ' AND sale_id IS NULL AND receiving_id IS NULL' +// ); + + + $query = 'SELECT ' . $this->db->escape($item_id) . ', definition_id, attribute_id, ' . $this->db->escape($id); + $query .= ' FROM ' . $this->db->prefixTable('attribute_links'); + $query .= ' WHERE item_id = ' . $this->db->escape($item_id); $query .=' AND sale_id IS NULL AND receiving_id IS NULL'; - $this->db->query($query); + + $builder = $this->db->table('attribute_links'); + $builder->ignore(true)->setQueryAsData(new RawSql($query), null, 'item_id, definition_id, attribute_id, '. $sale_receiving_fk )->insertBatch(); + + } public function get_suggestions(int $definition_id, string $term): array diff --git a/app/Models/Sale.php b/app/Models/Sale.php index 8ced7d308..5e6978a37 100644 --- a/app/Models/Sale.php +++ b/app/Models/Sale.php @@ -45,7 +45,7 @@ class Sale extends Model public function __construct() { parent::__construct(); - + helper('text'); $this->sale_lib = new Sale_lib(); } @@ -640,8 +640,8 @@ class Sale extends Model * The sales_taxes variable needs to be initialized to an empty array before calling * @throws ReflectionException */ - public function save_value(int $sale_id, string &$sale_status, array &$items, int $customer_id, int $employee_id, string $comment, string $invoice_number, - string $work_order_number, string $quote_number, int $sale_type, array $payments, int $dinner_table_id, array &$sales_taxes): int //TODO: this method returns the sale_id but the override is expecting it to return a bool. The signature needs to be reworked. Generally when there are more than 3 maybe 4 parameters, there's a good chance that an object needs to be passed rather than so many params. + public function save_value(int $sale_id, string &$sale_status, array &$items, int $customer_id, int $employee_id, string $comment, ?string $invoice_number, + ?string $work_order_number, ?string $quote_number, int $sale_type, ?array $payments, ?int $dinner_table_id, ?array &$sales_taxes): int //TODO: this method returns the sale_id but the override is expecting it to return a bool. The signature needs to be reworked. Generally when there are more than 3 maybe 4 parameters, there's a good chance that an object needs to be passed rather than so many params. { $config = config('OSPOS')->settings; $attribute = model(Attribute::class); @@ -649,6 +649,7 @@ class Sale extends Model $giftcard = model(Giftcard::class); $inventory = model('Inventory'); $item = model(Item::class); + $item_quantity = model(Item_quantity::class); if($sale_id != NEW_ENTRY) @@ -656,8 +657,6 @@ class Sale extends Model $this->clear_suspended_sale_detail($sale_id); } - $tax_decimals = tax_decimals(); //TODO: $tax_decimals is never used. - if(count($items) == 0) //TODO: === { return -1; //TODO: Replace -1 with a constant @@ -679,15 +678,15 @@ class Sale extends Model // Run these queries as a transaction, we want to make sure we do all or nothing $this->db->transStart(); - $builder = $this->db->table('sales'); - if($sale_id == NEW_ENTRY) { + $builder = $this->db->table('sales'); $builder->insert($sales_data); $sale_id = $this->db->insertID(); } else { + $builder = $this->db->table('sales'); $builder->where('sale_id', $sale_id); $builder->update($sales_data); } @@ -695,8 +694,6 @@ class Sale extends Model $total_amount = 0; $total_amount_used = 0; - $builder = $this->db->table('sales_payments'); - foreach($payments as $payment_id => $payment) { if(!empty(strstr($payment['payment_type'], lang('Sales.giftcard')))) @@ -722,6 +719,7 @@ class Sale extends Model 'employee_id' => $employee_id ]; + $builder = $this->db->table('sales_payments'); $builder->insert($sales_payments_data); $total_amount = floatval($total_amount) + floatval($payment['payment_amount']) - floatval($payment['cash_refund']); @@ -731,8 +729,6 @@ class Sale extends Model $customer = $customer->get_info($customer_id); - $builder = $this->db->table('sales_items'); - foreach($items as $line => $item_data) { $cur_item_info = $item->get_info($item_data['item_id']); @@ -757,6 +753,7 @@ class Sale extends Model 'print_option' => $item_data['print_option'] ]; + $builder = $this->db->table('sales_items'); $builder->insert($sales_items_data); if($cur_item_info->stock_type == HAS_STOCK && $sale_status == COMPLETED) //TODO: === ? diff --git a/app/Views/sales/register.php b/app/Views/sales/register.php index 83e8944fb..278f47152 100644 --- a/app/Views/sales/register.php +++ b/app/Views/sales/register.php @@ -873,12 +873,12 @@ $(document).ready(function() }); $('#finish_sale_button').click(function() { - $('#buttons_form').attr('action', ""); + $('#buttons_form').attr('action', ""); $('#buttons_form').submit(); }); $('#finish_invoice_quote_button').click(function() { - $('#buttons_form').attr('action', ""); + $('#buttons_form').attr('action', ""); $('#buttons_form').submit(); }); @@ -1024,7 +1024,7 @@ document.body.onkeyup = function(e) break; case 55: // Alt + 7 Add Payment and Complete Sales/Invoice $("#add_payment_button").click(); - window.location.href = ""; + window.location.href = ""; break; case 56: // Alt + 8 Finish Quote/Invoice without payment $("#finish_invoice_quote_button").click();