Migrate attribute datetime to date (#2441)

This commit is contained in:
jekkos
2019-06-04 23:59:18 +02:00
parent d3d051ee72
commit 0ae6470b35
16 changed files with 78 additions and 64 deletions

View File

@@ -3,7 +3,7 @@
const DEFAULT_LANGUAGE = 'english';
const DEFAULT_LANGUAGE_CODE = 'en-US';
define('DEFAULT_DATETIME', mktime(0, 0, 0, 1, 1, 2010));
define('DEFAULT_DATE', mktime(0, 0, 0, 1, 1, 2010));
/**
* Currency locale helper
@@ -310,11 +310,11 @@ function tax_decimals()
return $config->item('tax_decimals') ? $config->item('tax_decimals') : 0;
}
function to_datetime($datetime)
function to_date($date)
{
$config = get_instance()->config;
return date($config->item('dateformat') . ' ' . $config->item('timeformat'), $datetime);
return date($config->item('dateformat'), $date);
}
function to_currency($number)
@@ -460,7 +460,7 @@ function dateformat_momentjs($php_format)
function dateformat_mysql()
{
$config = get_instance()->config;
$php_format = $config->item('dateformat') . ' ' . $config->item('timeformat');
$php_format = $config->item('dateformat');
$SYMBOLS_MATCHING = array(
// Day
@@ -491,7 +491,7 @@ function dateformat_mysql()
'B' => '',
'g' => '%l',
'G' => '%k',
'h' => '',
'h' => '%H',
'H' => '%k',
'i' => '%i',
's' => '%S',
@@ -542,11 +542,6 @@ function dateformat_bootstrap($php_format)
return strtr($php_format, $SYMBOLS_MATCHING);
}
function valid_datetime($datetime)
{
return preg_match('/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9])(?:( [0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/', $datetime);
}
function valid_decimal($decimal)
{
return preg_match('/^(\d*\.)?\d+$/', $decimal);

View File

@@ -90,7 +90,7 @@ function get_sale_data_row($sale)
$row = array (
'sale_id' => $sale->sale_id,
'sale_time' => to_datetime(strtotime($sale->sale_time)),
'sale_time' => to_date(strtotime($sale->sale_time)),
'customer_name' => $sale->customer_name,
'amount_due' => to_currency($sale->amount_due),
'amount_tendered' => to_currency($sale->amount_tendered),
@@ -444,7 +444,7 @@ function get_item_data_row($item)
);
$attribute_values = (property_exists($item, 'attribute_values')) ? $item->attribute_values : "";
$attribute_values = (property_exists($item, 'attribute_dtvalues')) ? $attribute_values . $item->attribute_dtvalues : $attribute_values;
$attribute_values = (property_exists($item, 'attribute_dtvalues')) ? $attribute_values . '|' . $item->attribute_dtvalues : $attribute_values;
return $columns + expand_attribute_values($definition_names, $attribute_values) + $icons;
}
@@ -659,7 +659,7 @@ function get_expenses_data_row($expense)
$controller_name = strtolower(get_class($CI));
return array (
'expense_id' => $expense->expense_id,
'date' => to_datetime(strtotime($expense->date)),
'date' => to_date(strtotime($expense->date)),
'supplier_name' => $expense->supplier_name,
'supplier_tax_code' => $expense->supplier_tax_code,
'amount' => to_currency($expense->amount),
@@ -750,11 +750,11 @@ function get_cash_up_data_row($cash_up)
$controller_name = strtolower(get_class($CI));
return array (
'cashup_id' => $cash_up->cashup_id,
'open_date' => to_datetime(strtotime($cash_up->open_date)),
'open_date' => to_date(strtotime($cash_up->open_date)),
'open_employee_id' => $cash_up->open_first_name . ' ' . $cash_up->open_last_name,
'open_amount_cash' => to_currency($cash_up->open_amount_cash),
'transfer_amount_cash' => to_currency($cash_up->transfer_amount_cash),
'close_date' => to_datetime(strtotime($cash_up->close_date)),
'close_date' => to_date(strtotime($cash_up->close_date)),
'close_employee_id' => $cash_up->close_first_name . ' ' . $cash_up->close_last_name,
'closed_amount_cash' => to_currency($cash_up->closed_amount_cash),
'note' => $cash_up->note ? '<span class="glyphicon glyphicon-ok"></span>' : '<span class="glyphicon glyphicon-remove"></span>',

View File

@@ -1,6 +1,6 @@
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH . 'libraries/tokens/Token.php');
require_once(APPPATH . 'models/tokens/Token.php');
/**
* Token library

View File

@@ -0,0 +1,20 @@
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Migration_fix_attribute_datetime extends CI_Migration
{
public function __construct()
{
parent::__construct();
}
public function up()
{
execute_script(APPPATH . 'migrations/sqlscripts/3.3.0_fix_attribute_datetime.sql');
}
public function down()
{
}
}
?>

View File

@@ -0,0 +1 @@
ALTER TABLE `ospos_attribute_values` CHANGE `attribute_datetime` `attribute_date` DATE DEFAULT NULL;

View File

@@ -3,10 +3,10 @@
define('GROUP', 'GROUP');
define('DROPDOWN', 'DROPDOWN');
define('DECIMAL', 'DECIMAL');
define('DATETIME', 'DATETIME');
define('DATE', 'DATE');
define('TEXT', 'TEXT');
const DEFINITION_TYPES = [GROUP, DROPDOWN, DECIMAL, TEXT, DATETIME];
const DEFINITION_TYPES = [GROUP, DROPDOWN, DECIMAL, TEXT, DATE];
/**
* Attribute class
@@ -270,11 +270,11 @@ class Attribute extends CI_Model
$this->db->where('definition_id',$definition);
$success = TRUE;
if($to === DATETIME)
if($to === DATE)
{
foreach($this->db->get()->result_array() as $row)
{
if(valid_datetime($row['attribute_value']) === FALSE)
if(valid_date($row['attribute_value']) === FALSE)
{
log_message('ERROR', 'item_id: ' . $row['item_id'] . ' with attribute_value: ' . $row['attribute_value'] . ' cannot be converted to datetime');
$success = FALSE;
@@ -303,9 +303,9 @@ class Attribute extends CI_Model
//From TEXT to DATETIME
if($from_type === TEXT)
{
if($to_type === DATETIME || $to_type === DECIMAL)
if($to_type === DATE || $to_type === DECIMAL)
{
$field = ($to_type === DATETIME ? 'attribute_datetime' : 'attribute_decimal');
$field = ($to_type === DATETIME ? 'attribute_date' : 'attribute_decimal');
if($this->check_data_validity($definition_id, $from_type, $to_type))
{
@@ -454,7 +454,7 @@ class Attribute extends CI_Model
{
$format = $this->db->escape(dateformat_mysql());
$this->db->select("GROUP_CONCAT(attribute_value SEPARATOR ', ') AS attribute_values");
$this->db->select("GROUP_CONCAT(DATE_FORMAT(attribute_datetime, $format) SEPARATOR ', ') AS attribute_dtvalues");
$this->db->select("GROUP_CONCAT(DATE_FORMAT(attribute_date, $format) SEPARATOR ', ') AS attribute_dtvalues");
$this->db->from('attribute_links');
$this->db->join('attribute_values', 'attribute_values.attribute_id = attribute_links.attribute_id');
$this->db->join('attribute_definitions', 'attribute_definitions.definition_id = attribute_links.definition_id');
@@ -521,21 +521,18 @@ class Attribute extends CI_Model
public function save_value($attribute_value, $definition_id, $item_id = FALSE, $attribute_id = FALSE, $definition_type = DROPDOWN)
{
$this->db->trans_start();
$this->db->trans_start();
if(empty($attribute_id) || empty($item_id))
{
if($definition_type == TEXT || $definition_type == DROPDOWN)
{
$attribute_id_check = $this->value_exists($attribute_value);
if(empty($attribute_id_check))
$attribute_id = $this->value_exists($attribute_value);
if(empty($attribute_id))
{
$this->db->insert('attribute_values', array('attribute_value' => $attribute_value));
}
else
{
$attribute_id = $attribute_id_check;
}
}
else if($definition_type == DECIMAL)
{
@@ -543,19 +540,16 @@ class Attribute extends CI_Model
}
else
{
$this->db->insert('attribute_values', array('attribute_datetime' => date('Y-m-d H:i:s', strtotime($attribute_value))));
$this->db->insert('attribute_values', array('attribute_date' => date('Y-m-d', strtotime($attribute_value))));
}
$attribute_id = $this->db->insert_id();
$attribute_id = $attribute_id ? $attribute_id : $this->db->insert_id();
$this->db->insert('attribute_links', array(
'attribute_id' => empty($attribute_id) ? NULL : $attribute_id,
'item_id' => empty($item_id) ? NULL : $item_id,
'definition_id' => $definition_id));
}
else
{
$this->db->where('attribute_id', $attribute_id);
$this->db->trans_complete();

View File

@@ -194,7 +194,7 @@ class Item extends CI_Model
{
$format = $this->db->escape(dateformat_mysql());
$this->db->select('GROUP_CONCAT(DISTINCT CONCAT_WS(\'_\', definition_id, attribute_value) ORDER BY definition_id SEPARATOR \'|\') AS attribute_values');
$this->db->select("GROUP_CONCAT(DISTINCT CONCAT_WS('_', definition_id, DATE_FORMAT(attribute_datetime, $format)) SEPARATOR '|') AS attribute_dtvalues");
$this->db->select("GROUP_CONCAT(DISTINCT CONCAT_WS('_', definition_id, DATE_FORMAT(attribute_date, $format)) SEPARATOR '|') AS attribute_dtvalues");
$this->db->join('attribute_links', 'attribute_links.item_id = items.item_id AND attribute_links.receiving_id IS NULL AND attribute_links.sale_id IS NULL AND definition_id IN (' . implode(',', $filters['definition_ids']) . ')', 'left');
$this->db->join('attribute_values', 'attribute_values.attribute_id = attribute_links.attribute_id', 'left');
}
@@ -281,7 +281,7 @@ class Item extends CI_Model
{
$this->db->select('items.*');
$this->db->select('GROUP_CONCAT(attribute_value SEPARATOR \'|\') AS attribute_values');
$this->db->select('GROUP_CONCAT(attribute_datetime SEPARATOR \'|\') AS attribute_dtvalues');
$this->db->select('GROUP_CONCAT(attribute_date SEPARATOR \'|\') AS attribute_dtvalues');
$this->db->select('suppliers.company_name');
$this->db->from('items');
$this->db->join('suppliers', 'suppliers.person_id = items.supplier_id', 'left');
@@ -379,8 +379,8 @@ class Item extends CI_Model
{
$this->db->select('items.*');
$this->db->select('company_name');
$this->db->select('GROUP_CONCAT(DISTINCT CONCAT_WS(\':\', definition_id, attribute_value) ORDER BY definition_id SEPARATOR \'|\') AS attribute_values');
$this->db->select('GROUP_CONCAT(DISTINCT CONCAT_WS(\':\', definition_id, attribute_datetime) ORDER BY definition_id SEPARATOR \'|\') AS attribute_dtvalues');
$this->db->select('GROUP_CONCAT(DISTINCT CONCAT_WS(\'_\', definition_id, attribute_value) ORDER BY definition_id SEPARATOR \'|\') AS attribute_values');
$this->db->select('GROUP_CONCAT(DISTINCT CONCAT_WS(\'_\', definition_id, attribute_date) ORDER BY definition_id SEPARATOR \'|\') AS attribute_dtvalues');
$this->db->select('quantity');
$this->db->from('items');
$this->db->join('suppliers', 'suppliers.person_id = items.supplier_id', 'left');

View File

@@ -104,7 +104,7 @@ class Detailed_receivings extends Report
{
$format = $this->db->escape(dateformat_mysql());
$this->db->select("GROUP_CONCAT(DISTINCT CONCAT_WS('_', definition_id, attribute_value) ORDER BY definition_id SEPARATOR '|') AS attribute_values");
$this->db->select("GROUP_CONCAT(DISTINCT CONCAT_WS('_', definition_id, DATE_FORMAT(attribute_datetime, $format)) SEPARATOR '|') AS attribute_dtvalues");
$this->db->select("GROUP_CONCAT(DISTINCT CONCAT_WS('_', definition_id, DATE_FORMAT(attribute_date, $format)) SEPARATOR '|') AS attribute_dtvalues");
$this->db->join('attribute_links', 'attribute_links.item_id = items.item_id AND attribute_links.receiving_id = receivings_items_temp.receiving_id AND definition_id IN (' . implode(',', $inputs['definition_ids']) . ')', 'left');
$this->db->join('attribute_values', 'attribute_values.attribute_id = attribute_links.attribute_id', 'left');
$this->db->group_by('receivings_items_temp.receiving_id, receivings_items_temp.item_id');

View File

@@ -150,7 +150,7 @@ class Detailed_sales extends Report
{
$format = $this->db->escape(dateformat_mysql());
$this->db->select("GROUP_CONCAT(DISTINCT CONCAT_WS('_', definition_id, attribute_value) ORDER BY definition_id SEPARATOR '|') AS attribute_values");
$this->db->select("GROUP_CONCAT(DISTINCT CONCAT_WS('_', definition_id, DATE_FORMAT(attribute_datetime, $format)) SEPARATOR '|') AS attribute_datetimevalues");
$this->db->select("GROUP_CONCAT(DISTINCT CONCAT_WS('_', definition_id, DATE_FORMAT(attribute_date, $format)) SEPARATOR '|') AS attribute_dtvalues");
$this->db->join('attribute_links', 'attribute_links.item_id = sales_items_temp.item_id AND attribute_links.sale_id = sales_items_temp.sale_id AND definition_id IN (' . implode(',', $inputs['definition_ids']) . ')', 'left');
$this->db->join('attribute_values', 'attribute_values.attribute_id = attribute_links.attribute_id', 'left');
$this->db->group_by('sales_items_temp.sale_id, sales_items_temp.item_id');

View File

@@ -1,13 +1,13 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH . 'libraries/tokens/Token.php');
require_once(APPPATH . 'libraries/tokens/Token_customer.php');
require_once(APPPATH . 'libraries/tokens/Token_invoice_count.php');
require_once(APPPATH . 'libraries/tokens/Token_invoice_sequence.php');
require_once(APPPATH . 'libraries/tokens/Token_quote_sequence.php');
require_once(APPPATH . 'libraries/tokens/Token_work_order_sequence.php');
require_once(APPPATH . 'libraries/tokens/Token_suspended_invoice_count.php');
require_once(APPPATH . 'libraries/tokens/Token_year_invoice_count.php');
require_once(APPPATH . 'models/tokens/Token.php');
require_once(APPPATH . 'models/tokens/Token_customer.php');
require_once(APPPATH . 'models/tokens/Token_invoice_count.php');
require_once(APPPATH . 'models/tokens/Token_invoice_sequence.php');
require_once(APPPATH . 'models/tokens/Token_quote_sequence.php');
require_once(APPPATH . 'models/tokens/Token_work_order_sequence.php');
require_once(APPPATH . 'models/tokens/Token_suspended_invoice_count.php');
require_once(APPPATH . 'models/tokens/Token_year_invoice_count.php');
/**
* Token class

View File

@@ -82,14 +82,14 @@ $(document).ready(function()
{
var definition_type = $("#definition_type option:selected").text();
if(definition_type == "DATETIME" || (definition_type == "GROUP" && !is_new) || definition_type == "DECIMAL")
if(definition_type == "DATE" || (definition_type == "GROUP" && !is_new) || definition_type == "DECIMAL")
{
$('#definition_type').prop("disabled",true);
}
else if(definition_type == "DROPDOWN")
{
$("#definition_type option:contains('GROUP')").hide();
$("#definition_type option:contains('DATETIME')").hide();
$("#definition_type option:contains('DATE')").hide();
$("#definition_type option:contains('DECIMAL')").hide();
}
else
@@ -184,6 +184,10 @@ $(document).ready(function()
return value.match(/(\||_)/g) == null;
}, "<?php echo $this->lang->line('attributes_attribute_value_invalid_chars'); ?>");
$('form').bind('submit', function () {
$(this).find(':input').prop('disabled', false);
});
$('#attribute_form').validate($.extend({
submitHandler: function(form)
{
@@ -217,4 +221,4 @@ $(document).ready(function()
}
}, form_support.error));
});
</script>
</script>

View File

@@ -22,12 +22,12 @@ foreach($definition_values as $definition_id => $definition_value)
echo form_hidden("attribute_ids[$definition_id]", $definition_value['attribute_id']);
$attribute_value = $definition_value['attribute_value'];
if ($definition_value['definition_type'] == DATETIME)
if ($definition_value['definition_type'] == DATE)
{
$value = (empty($attribute_value) || empty($attribute_value->attribute_datetime)) ? DEFAULT_DATETIME : strtotime($attribute_value->attribute_datetime);
$value = (empty($attribute_value) || empty($attribute_value->attribute_date)) ? DEFAULT_DATE : strtotime($attribute_value->attribute_date);
echo form_input(array(
'name' => "attribute_links[$definition_id]",
'value' => to_datetime($value),
'value' => to_date($value),
'class' => 'form-control input-sm datetime',
'data-definition-id' => $definition_id,
'readonly' => 'true'));
@@ -59,7 +59,7 @@ foreach($definition_values as $definition_id => $definition_value)
<script type="text/javascript">
(function() {
<?php $this->load->view('partial/datepicker_locale'); ?>
<?php $this->load->view('partial/datepicker_locale', array('config' => '{ minView: 2, format: "'.dateformat_bootstrap($this->config->item('dateformat') . '"}'))); ?>
var enable_delete = function() {
$('.remove_attribute_btn').click(function() {

View File

@@ -176,7 +176,7 @@
'name'=>'date',
'id'=>'datetime',
'class'=>'form-control input-sm',
'value'=>to_datetime(strtotime($person_info->date)),
'value'=>to_date(strtotime($person_info->date)),
'readonly'=>'true')
); ?>
</div>

View File

@@ -17,7 +17,7 @@
<?php echo form_input(array(
'name'=>'date',
'class'=>'form-control input-sm datetime',
'value'=>to_datetime(strtotime($expenses_info->date)),
'value'=>to_date(strtotime($expenses_info->date)),
'readonly'=>'readonly')
);?>
</div>

View File

@@ -1,9 +1,9 @@
<?php $this->lang->load('calendar'); $this->lang->load('date'); ?>
var pickerconfig = function(start) {
return {
format: "<?php echo dateformat_bootstrap($this->config->item('dateformat')) . ' ' . dateformat_bootstrap($this->config->item('timeformat'));?>",
startDate: start || "<?php echo to_datetime(DEFAULT_DATETIME); ?>",
var pickerconfig = function(config) {
return $.extend({
format: "<?php echo dateformat_bootstrap($this->config->item('dateformat'));?>",
startDate: "<?php echo to_date(DEFAULT_DATE); ?>",
<?php
$t = $this->config->item('timeformat');
$m = $t[strlen($t)-1];
@@ -26,7 +26,7 @@ var pickerconfig = function(start) {
todayHighlight: true,
bootcssVer: 3,
language: "<?php echo current_language_code(); ?>"
};
}, <?php echo isset($config) ? $config : '{}' ?>);
};
$.fn.datetimepicker.dates['<?php echo $this->config->item("language"); ?>'] = {

View File

@@ -12,7 +12,7 @@
<div class="form-group form-group-sm">
<?php echo form_label($this->lang->line('sales_date'), 'date', array('class'=>'control-label col-xs-3')); ?>
<div class='col-xs-8'>
<?php echo form_input(array('name'=>'date','value'=>to_datetime(strtotime($sale_info['sale_time'])), 'class'=>'datetime form-control input-sm'));?>
<?php echo form_input(array('name'=>'date','value'=>to_date(strtotime($sale_info['sale_time'])), 'class'=>'datetime form-control input-sm'));?>
</div>
</div>