Skip to content

Commit

Permalink
atomic lock + last invoice id
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaiCraciun88 committed Oct 9, 2024
1 parent 4016f7c commit cc4146d
Show file tree
Hide file tree
Showing 3 changed files with 304 additions and 257 deletions.
42 changes: 42 additions & 0 deletions src/Lock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace OblioSoftware;

class Lock {
protected $_path;
protected $_timeout = 30;
protected $_lockfileName = '.lockfile';
protected $_file;

public function __construct($path, $timeout = 30)
{
$this->_path = $path;
$this->_timeout = $timeout;
$this->_file = rtrim($this->_path, '/') . '/' . $this->_lockfileName;
$now = time();
while (file_exists($this->_file)) {
if (time() > $now + $timeout) {
return;
}
usleep(100000);
}
file_put_contents($this->_file, time());
}

public function __destruct()
{
$this->close();
}

public function close()
{
if (file_exists($this->_file)) {
unlink($this->_file);
}
}

public static function open()
{
return new self(WP_OBLIO_DIR);
}
}
9 changes: 7 additions & 2 deletions view/components/order_details_invoice_box.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@

$order_meta_table = OblioSoftware\Order::get_meta_table_name();
$field_name = OblioSoftware\Order::get_meta_table_field_name();
$sql = "SELECT {$field_name} FROM `{$order_meta_table}` WHERE meta_key='{$number_key}' AND meta_value<>'' ORDER BY `meta_value` DESC LIMIT 1";
$lastInvoice = $wpdb->get_var($sql);
$sql = "SELECT pmn.{$field_name} " .
"FROM `{$order_meta_table}` pmn " .
"JOIN `{$order_meta_table}` pms ON(pmn.post_id=pms.post_id AND pmn.meta_key='{$number_key}' AND pms.meta_key='{$series_name_key}') " .
"WHERE pmn.meta_value<>'' AND pms.meta_value='{$series_name}' " .
"ORDER BY pmn.`meta_value` DESC " .
"LIMIT 1";
$lastInvoice = !$link ? null : $wpdb->get_var($sql);
if ($link) {
echo sprintf('<p><a class="button" href="%s" target="_blank">%s</a></p>',
_wp_oblio_build_url('oblio-view-' . $options['docType'], $post), sprintf(__('Vezi %s %s %d', 'woocommerce-oblio'), $options['name'], $series_name, $number));
Expand Down
Loading

0 comments on commit cc4146d

Please sign in to comment.