diff --git a/public/vendor/exment/js/common.js b/public/vendor/exment/js/common.js index e5005e2c5..1a1f08814 100644 --- a/public/vendor/exment/js/common.js +++ b/public/vendor/exment/js/common.js @@ -48,11 +48,6 @@ var Exment; linkElem.closest('a').click(); }); }; - /** - * add comma and remove comma if focus - */ - CommonEvent.numberComma = function () { - }; /** * Set changedata event */ @@ -76,7 +71,7 @@ var Exment; // get element value from model var from = modeldata['value'][changedata_from]; // copy to element from model - var val = $elem.prop('number_format') ? comma(from) : from; + var val = $elem.attr('number_format') ? comma(from) : from; $elem.val(val); } CommonEvent.setFormFilter($target); @@ -101,7 +96,7 @@ var Exment; if ($e.data('getitem')) { continue; } - var val = $e.prop('number_format') ? comma(data[key]) : data[key]; + var val = $e.attr('number_format') ? comma(data[key]) : data[key]; $e.val(val); // if target-item is "iconpicker-input", set icon if ($e.hasClass('iconpicker-input')) { @@ -218,6 +213,7 @@ var Exment; if (prefix === void 0) { prefix = ''; } return '.' + prefix + key + ',.' + prefix + 'value_' + key; }; + CommonEvent.calcDataList = []; /** * 日付の計算 */ @@ -474,13 +470,18 @@ var Exment; * Set calc event */ CommonEvent.setCalcEvent = function (datalist) { + // set datalist for after flow. + CommonEvent.calcDataList = []; // loop "data-calc" targets for (var key in datalist) { var data = datalist[key]; // set data to element - $(CommonEvent.getClassKey(key)).data('calc_data', data); + // cannot use because cannot fire new row + //$(CommonEvent.getClassKey(key)).data('calc_data', data); + // set calcDataList array. key is getClassKey. data is data + CommonEvent.calcDataList.push({ "classKey": CommonEvent.getClassKey(key), "data": data }); // set calc event - $(document).on('change', CommonEvent.getClassKey(key), { data: data }, function (ev) { + $(document).on('change', CommonEvent.getClassKey(key), { data: data, key: key }, function (ev) { CommonEvent.setCalc($(ev.target), ev.data.data); }); // set event for plus minus button @@ -555,11 +556,17 @@ var Exment; } break; } - $to.val(bn.toPrecision()); + var precision = bn.toPrecision(); + $to.val($to.attr('number_format') ? comma(precision) : precision); // if $to has "calc_data" data, execute setcalc function again - var to_data = $to.data('calc_data'); - if (hasValue(to_data)) { - CommonEvent.setCalc($to, to_data); + //var to_data = $to.data('calc_data'); + for (var key in CommonEvent.calcDataList) { + var calcData = CommonEvent.calcDataList[key]; + // filter $to obj + var $filterTo = $to.filter(calcData.classKey); + if (hasValue($filterTo)) { + CommonEvent.setCalc($filterTo, calcData.data); + } } } }; diff --git a/src/Controllers/DocumentForm.php b/src/Controllers/DocumentForm.php index e36c22788..6375bf05a 100644 --- a/src/Controllers/DocumentForm.php +++ b/src/Controllers/DocumentForm.php @@ -29,10 +29,12 @@ public function getDocumentForm(Request $request, $id){ $isMitsumori = $table_name == 'estimate'; $documentItems = [ ['y' => 0, 'fixWidth' => true, 'text' => $isMitsumori ? '見積書' : '請求書', 'align' => 'C', 'font_size' => 20, 'border' => 'TLBR'], - ['x' => 0, 'y' => 20, 'width' => 95, 'text' => '${value:company-customer:company-name}', 'font_size' => 15, 'border' => 'B'], + + // TO:customer + ['x' => 0, 'y' => 20, 'width' => 95, 'text' => '${value:deal:company_customer:company_name}', 'font_size' => 15, 'border' => 'B'], ['x' => 95, 'y' => 20, 'text' => '様'], - ['x' => 0, 'y' => 35, 'width' => 30, 'text' => '${value:'.$table_name.'-amount}', 'border' => 'B'], - ['x' => 30, 'y' => 35, 'text' => '税込'], + ['x' => 0, 'y' => 35, 'width' => 30, 'text' => '${sum:'.$table_name.'_detail:sum_tax_price}', 'align' => 'R', 'border' => 'B'], + ['x' => 30, 'y' => 35, 'text' => '円(税込)'], // company info ['x' => 0, 'y' => 35, 'width' => 20, 'image' => '${base_info:company_stamp}', 'align' => 'R'], @@ -43,17 +45,17 @@ public function getDocumentForm(Request $request, $id){ ['y' => 65, 'fixWidth' => true, 'text' => 'TEL:${base_info:tel01}-${base_info:tel02}-${base_info:tel03}', 'align' => 'R', 'font_size' => 10], // tables - ['y' => 90, 'fixWidth' => true, 'document_item_type' => 'table', 'font_size' => 8, 'target_table' => $table_name.'-detail', 'table_count' => 10, 'target_columns' => [ + ['y' => 90, 'fixWidth' => true, 'document_item_type' => 'table', 'font_size' => 8, 'target_table' => $table_name.'_detail', 'table_count' => 10, 'target_columns' => [ ['column_name' => 'merchandise', 'width' => '*'], - ['column_name' => 'unit-price', 'width' => '20', 'align' => 'R'], - ['column_name' => 'unit', 'width' => '20', 'align' => 'C'], + ['column_name' => 'price', 'width' => '20', 'align' => 'R'], + ['column_name' => 'num', 'width' => '20', 'align' => 'C'], ['column_name' => 'quantity', 'width' => '20', 'align' => 'C'], - ['column_name' => 'amount', 'width' => '20', 'align' => 'R'], + ['column_name' => 'sum_price', 'width' => '20', 'align' => 'R'], ]], // comment ['y' => 200, 'fixWidth' => true, 'text' => '備考', 'font_size' => 10], - ['y' => 205, 'fixWidth' => true, 'height' => 50, 'border' => 'TLBR'], + ['y' => 205, 'fixWidth' => true, 'height' => 50, 'border' => 'TLBR', 'text' => '${value:tekiyou}'], ]; if($isMitsumori){ $documentItems[] = ['x' => 0, 'y' => 45, 'text' => '有効期限:'.Carbon::today()->addMonth()->addDay(-1)->format('Y/m/d')]; @@ -67,11 +69,12 @@ public function getDocumentForm(Request $request, $id){ $document_attachment_file = $service->getPdfPath(); // save pdf $path = $this->savePdfInServer($document_attachment_file, $service); + $filename = $service->getPdfFileName(); return response()->file($path, [ 'Content-Type' => 'application/pdf', //'Content-Disposition' => 'inline; filename="'.$filename.'"' - 'Content-Disposition' => 'inline;' + 'Content-Disposition' => 'inline; filename="'.$filename.'"' ]); diff --git a/src/Services/DocumentPdfService.php b/src/Services/DocumentPdfService.php index d758f4a80..c3ee2ad08 100644 --- a/src/Services/DocumentPdfService.php +++ b/src/Services/DocumentPdfService.php @@ -208,14 +208,39 @@ public function makeContractPdf($model, $documentInfo, $documentItems = []) // get value from model if (count($length_array) <= 1) { $str = ''; - }else if(count($length_array) == 2) { - $str = getValue($model, $length_array[1], true); - }else{ - $str = getValue($model, $length_array[1], $length_array[2]); + } + // elseif(count($length_array) == 2) { + // $str = getValue($model, $length_array[1], true); + // } + //else, getting value recursively + else{ + // get comma string from index 1. + $length_array = array_slice($length_array, 1); + $str = getValue($model, implode(',', $length_array), true); + } + $text = str_replace($matches[0][$i], $str, $text); + } + ///// sum + elseif (strpos($match, "sum") !== false) { + // get sum value from children model + if (count($length_array) <= 2) { + $str = ''; + } + //else, getting value using cihldren + else{ + // get children values + $children = getChildrenValues($model, $length_array[1]); + // looping + $sum = 0; + foreach($children as $child){ + $sum += intval($child->getValue($length_array[2])); + } + $str = strval($sum); } $text = str_replace($matches[0][$i], $str, $text); } - else if(strpos($match, "base_info") !== false){ + // base_info + elseif(strpos($match, "base_info") !== false){ $base_info = getModelName(Define::SYSTEM_TABLE_NAME_BASEINFO)::first(); // get value from model if (count($length_array) <= 1) { diff --git a/src/Services/Helpers.php b/src/Services/Helpers.php index 16adbdd19..f61a6343e 100644 --- a/src/Services/Helpers.php +++ b/src/Services/Helpers.php @@ -471,8 +471,11 @@ function getRelationName($obj) */ function getRelationNamebyObjs($parent, $child) { - $parent_suuid = CustomTable::getEloquent($parent)->suuid; - $child_suuid = CustomTable::getEloquent($child)->suuid; + $parent_suuid = CustomTable::getEloquent($parent)->suuid ?? null; + $child_suuid = CustomTable::getEloquent($child)->suuid ?? null; + if(is_null($parent_suuid) || is_null($child_suuid)){ + return null; + } return "pivot_{$parent_suuid}_{$child_suuid}"; } } @@ -566,32 +569,83 @@ function getValue($custom_value, $column = null, $isonly_label = false) if (!function_exists('getValueUseTable')) { /** * Get Custom Value - * @param $value - * @param string|array|CustomColumn $column + * @param array|CustomValue $value trget value + * @param string|array|CustomColumn $column target column_name or CustomColumn object. If null, it's label column * @param mixin $label if column_type is select_table or select_valtext, only get label * @return string */ function getValueUseTable($custom_table, $value, $column = null, $label = false) { if (is_null($value)) { - return $nullvalue; + return null; } - + $custom_table = CustomTable::getEloquent($custom_table); if(is_null($column)){ $column = getLabelColumn($custom_table); } - - // get custom column as array + + // if $column is string and and contains comma + if (is_string($column) && str_contains($column, ',')) { + ///// getting value Recursively + // split comma + $columns = explode(",", $column); + // if $columns count >= 2, loop columns + if (count($columns) >= 2) { + $loop_value = $value; + $loop_custom_table = $custom_table; + foreach ($columns as $k => $c) { + $lastIndex = ($k != count($columns) - 1); + // if $k is not last index, $loop_label is false(because using CustomValue Object) + if (!$lastIndex) { + $loop_label = false; + } + // if last index, $loop_label is called $label + else { + $loop_label = $label; + } + // get value using $c + $loop_value = getValueUseTable($loop_custom_table, $loop_value, $c, $loop_label); + // if null, return + if (is_null($loop_value)) { + return null; + } + + // if last index, return value + if($lastIndex){ + return $loop_value; + } + // get custom table. if CustomValue + if($loop_value instanceof CustomValue){ + $loop_custom_table = $loop_value->getCustomTable(); + } + // else, something wrong, so return null + else{ + return null; + } + } + return $loop_value; + } + // if length <= 1, set normal getValueUseTable flow, so $column = $columns[0] + else{ + $column = $columns[0]; + } + } + + ///// get custom column as array + // if string if (is_string($column)) { $column_first = CustomColumn ::where('column_name', $column) - ->where('custom_table_id', $custom_table->id) + ->where('custom_table_id', array_get($custom_table, 'id')) ->first(); if(is_null($column_first)){return null;} $column_array = $column_first->toArray() ?? null; - } elseif ($column instanceof CustomValue) { + } + // if $column is CustomColumn, convert to array. + elseif ($column instanceof CustomColumn) { $column_array = $column->toArray(); - } else { + } + else { $column_array = $column; } @@ -600,6 +654,9 @@ function getValueUseTable($custom_table, $value, $column = null, $label = false) if(is_array($value)){ $key = array_get($column_array, 'column_name'); $val = array_get($value, $key); + }elseif($value instanceof CustomValue){ + $key = array_get($column_array, 'column_name'); + $val = array_get($value->value, $key); }else{ $val = $value; } diff --git a/src/Services/TemplateInstaller.php b/src/Services/TemplateInstaller.php index 6f4f5ee36..1ef101113 100644 --- a/src/Services/TemplateInstaller.php +++ b/src/Services/TemplateInstaller.php @@ -236,6 +236,10 @@ protected static function install($basePath, $system_flg=false) // if column type is calc, set dynamic val if(array_get($column, 'column_type') == 'calc'){ $calc_formula = array_get($options, 'calc_formula'); + // if $calc_formula is string, convert to json + if(is_string($calc_formula)){ + $calc_formula = json_decode($calc_formula, true); + } if(is_array($calc_formula)){ foreach($calc_formula as &$c){ // if not dynamic, continue @@ -243,9 +247,13 @@ protected static function install($basePath, $system_flg=false) continue; } // set id - $c['val'] = CustomColumn::where('column_name', array_get($c, 'val'))->first()->id ?? null; + //*caution!! Don't use where 'column_name' because if same name but other table, wrong match. + //$c['val'] = CustomColumn::where('column_name', array_get($c, 'val'))->first()->id ?? null; + $c['val'] = $obj_table->custom_columns()->where('column_name', array_get($c, 'val'))->first()->id ?? null; } } + // set as json string + $options['calc_formula'] = json_encode($calc_formula); } $obj_column->options = $options; @@ -404,12 +412,17 @@ protected static function install($basePath, $system_flg=false) $options = null; } // if has changedata_column_name and changedata_target_column_name, set id - if(array_key_value_exists('changedata_column_name', $options)){ - $options['changedata_column_id'] = CustomColumn::where('column_name', $options['changedata_column_name'])->first()->id?? null; + if(array_key_value_exists('changedata_column_name', $options) && array_key_value_exists('changedata_column_table_name', $options)){ + //*caution!! Don't use where 'column_name' because if same name but other table, wrong match. + //$options['changedata_column_id'] = CustomColumn::where('column_name', $options['changedata_column_name'])->first()->id?? null; + // get using changedata_column_table_name + $options['changedata_column_id'] = CustomTable::findByName($options['changedata_column_table_name'])->custom_columns()->where('column_name', $options['changedata_column_name'])->first()->id?? null; array_forget($options, 'changedata_column_name'); } if(array_key_value_exists('changedata_target_column_name', $options)){ - $options['changedata_target_column_id'] = CustomColumn::where('column_name', $options['changedata_target_column_name'])->first()->id?? null; + //*caution!! Don't use where 'column_name' because if same name but other table, wrong match. + //$options['changedata_target_column_id'] = CustomColumn::where('column_name', $options['changedata_target_column_name'])->first()->id?? null; + $options['changedata_target_column_id'] = $target_table->custom_columns()->where('column_name', $options['changedata_target_column_name'])->first()->id?? null; array_forget($options, 'changedata_target_column_name'); } @@ -842,6 +855,10 @@ protected static function setTemplateTable(&$config, $target_tables){ // if column_type is calc, change value dynamic name using calc_formula property if (array_get($custom_column, 'column_type') == 'calc') { $calc_formula = array_get($custom_column['options'], 'calc_formula'); + // if $calc_formula is string, convert to json + if(is_string($calc_formula)){ + $calc_formula = json_decode($calc_formula, true); + } if(is_array($calc_formula)){ foreach($calc_formula as &$c){ // if not dynamic, continue @@ -851,14 +868,18 @@ protected static function setTemplateTable(&$config, $target_tables){ // get custom column name $calc_formula_column_name = CustomColumn::find(array_get($c, 'val'))->column_name ?? null; // set value + \Log::debug('calc_formula_column_name:'.$calc_formula_column_name); $c['val'] = $calc_formula_column_name; } } - $select_target_table = CustomTable::find(array_get($custom_column['options'], 'select_target_table')); - if (isset($select_target_table)) { - $custom_column['options']['select_target_table_name'] = CustomTable::find(array_get($custom_column['options'], 'select_target_table'))->table_name; - array_forget($custom_column['options'], 'select_target_table'); - } + // set options + $custom_column['options']['calc_formula'] = $calc_formula; + } + + $select_target_table = CustomTable::find(array_get($custom_column['options'], 'select_target_table')); + if (isset($select_target_table)) { + $custom_column['options']['select_target_table_name'] = CustomTable::find(array_get($custom_column['options'], 'select_target_table'))->table_name; + array_forget($custom_column['options'], 'select_target_table'); } $custom_column = array_only($custom_column, [ @@ -923,14 +944,17 @@ protected static function setTemplateTable(&$config, $target_tables){ } // set as changedata_column_id to changedata_column_name if(array_key_value_exists('changedata_column_id', $custom_form_column['options'])){ - $custom_form_column['options']['changedata_column_name'] = CustomColumn::find($custom_form_column['options']['changedata_column_id'])->column_name ?? null; - array_forget($custom_form_column['options'], 'changedata_column_id'); + $changedata_column = CustomColumn::find($custom_form_column['options']['changedata_column_id']); + $custom_form_column['options']['changedata_column_name'] = $changedata_column->column_name ?? null; + // set changedata_column table name + $custom_form_column['options']['changedata_column_table_name'] = $changedata_column->custom_table->table_name ?? null; } + array_forget($custom_form_column['options'], 'changedata_column_id'); // set as changedata_target_column_id to changedata_target_column_name if(array_key_value_exists('changedata_target_column_id', $custom_form_column['options'])){ $custom_form_column['options']['changedata_target_column_name'] = CustomColumn::find($custom_form_column['options']['changedata_target_column_id'])->column_name ?? null; - array_forget($custom_form_column['options'], 'changedata_target_column_id'); } + array_forget($custom_form_column['options'], 'changedata_target_column_id'); $custom_form_column = array_only($custom_form_column, [ 'form_column_type', diff --git a/src/Web/ts/common.ts b/src/Web/ts/common.ts index aea946c57..c8ada1cfa 100644 --- a/src/Web/ts/common.ts +++ b/src/Web/ts/common.ts @@ -1,6 +1,7 @@ namespace Exment { export class CommonEvent { + protected static calcDataList = []; /** * Call only once. It's $(document).on event. */ @@ -50,13 +51,6 @@ namespace Exment { }); } - /** - * add comma and remove comma if focus - */ - private static numberComma() { - - } - /** * 日付の計算 */ @@ -200,7 +194,7 @@ namespace Exment { // get element value from model var from = modeldata['value'][changedata_from]; // copy to element from model - var val = $elem.prop('number_format') ? comma(from) : from; + var val = $elem.attr('number_format') ? comma(from) : from; $elem.val(val); } CommonEvent.setFormFilter($target); @@ -227,7 +221,7 @@ namespace Exment { if ($e.data('getitem')) { continue; } - var val = $e.prop('number_format') ? comma(data[key]) : data[key]; + var val = $e.attr('number_format') ? comma(data[key]) : data[key]; $e.val(val); // if target-item is "iconpicker-input", set icon @@ -464,15 +458,20 @@ namespace Exment { * Set calc event */ public static setCalcEvent = (datalist) => { + // set datalist for after flow. + CommonEvent.calcDataList = []; // loop "data-calc" targets for(var key in datalist){ var data = datalist[key]; // set data to element - $(CommonEvent.getClassKey(key)).data('calc_data', data); + // cannot use because cannot fire new row + //$(CommonEvent.getClassKey(key)).data('calc_data', data); + // set calcDataList array. key is getClassKey. data is data + CommonEvent.calcDataList.push({"classKey" : CommonEvent.getClassKey(key), "data": data}); // set calc event - $(document).on('change', CommonEvent.getClassKey(key), { data: data }, (ev) => { + $(document).on('change', CommonEvent.getClassKey(key), { data: data, key:key }, (ev) => { CommonEvent.setCalc($(ev.target), ev.data.data); }); // set event for plus minus button @@ -541,12 +540,18 @@ namespace Exment { } break; } - $to.val(bn.toPrecision()); + var precision = bn.toPrecision(); + $to.val($to.attr('number_format') ? comma(precision) : precision); // if $to has "calc_data" data, execute setcalc function again - var to_data = $to.data('calc_data'); - if(hasValue(to_data)){ - CommonEvent.setCalc($to, to_data); + //var to_data = $to.data('calc_data'); + for(var key in CommonEvent.calcDataList){ + var calcData = CommonEvent.calcDataList[key]; + // filter $to obj + var $filterTo = $to.filter(calcData.classKey); + if(hasValue($filterTo)){ + CommonEvent.setCalc($filterTo, calcData.data); + } } } }