diff --git a/src/app/Console/Commands/DiscountsCommand.php b/src/app/Console/Commands/DiscountsCommand.php index ad469186..8fc87112 100644 --- a/src/app/Console/Commands/DiscountsCommand.php +++ b/src/app/Console/Commands/DiscountsCommand.php @@ -1,12 +1,35 @@ 158f660b-e992-4fb9-ac12-5173b5f33807 \ + * > 62af659f-17d8-4527-87c1-c69eaa26653c \ + * > --description="Employee discount" + * ``` + */ +class MergeCommand extends Command +{ + /** + * The name and signature of the console command. + * + * @var string + */ + protected $signature = 'scalpel:discount:merge {source} {target} {--description*}'; + + /** + * The console command description. + * + * @var string + */ + protected $description = 'Merge one discount in to another discount, ' . + 'optionally set the description, and delete the source discount'; + + /** + * Create a new command instance. + * + * @return void + */ + public function __construct() + { + parent::__construct(); + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + $source = \App\Discount::find($this->argument('source')); + + if (!$source) { + $this->error("No such source discount: {$source}"); + return 1; + } + + $target = \App\Discount::find($this->argument('target')); + + if (!$target) { + $this->error("No such target discount: {$target}"); + return 1; + } + + if ($source->discount !== $target->discount) { + $this->error("Can't merge two discounts that have different rates"); + return 1; + } + + foreach ($source->wallets as $wallet) { + $wallet->discount = $target; + $wallet->timestamps = false; + $wallet->save(); + } + + if ($this->option('description')) { + $target->{'description'} = $this->option('description'); + $target->save(); + } + + $source->delete(); + } +} diff --git a/src/app/Console/Commands/Scalpel/Discount/UpdateCommand.php b/src/app/Console/Commands/Scalpel/Discount/UpdateCommand.php new file mode 100644 index 00000000..b87aa4bb --- /dev/null +++ b/src/app/Console/Commands/Scalpel/Discount/UpdateCommand.php @@ -0,0 +1,14 @@ +description = "Delete a {$this->objectName}"; + $this->signature = sprintf( + "%s%s:delete {%s}", + $this->commandPrefix ? $this->commandPrefix . ":" : "", + $this->objectName, + $this->objectName + ); + + $class = new $this->objectClass(); + + try { + foreach (Schema::getColumnListing($class->getTable()) as $column) { + if ($column == "id") { + continue; + } + + $this->signature .= " {--{$column}=}"; + } + } catch (\Exception $e) { + \Log::error("Could not extract options: {$e->getMessage()}"); + } + + $classes = class_uses_recursive($this->objectClass); + + parent::__construct(); + } + + public function getProperties() + { + if (!empty($this->properties)) { + return $this->properties; + } + + $class = new $this->objectClass(); + + $this->properties = []; + + foreach (Schema::getColumnListing($class->getTable()) as $column) { + if ($column == "id") { + continue; + } + + if (($value = $this->option($column)) !== null) { + $this->properties[$column] = $value; + } + } + + return $this->properties; + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + $argument = $this->argument($this->objectName); + + $object = $this->getObject($this->objectClass, $argument, $this->objectTitle); + + if (!$object) { + $this->error("No such {$this->objectName} {$argument}"); + return 1; + } + + foreach ($this->getProperties() as $property => $value) { + if ($property == "deleted_at" && $value == "null") { + $value = null; + } + + $object->{$property} = $value; + } + + $object->timestamps = false; + + $object->save(['timestamps' => false]); + } +}