brightpearl-cli 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,151 @@
1
+ require 'rest_client'
2
+ require 'mysql'
3
+ require 'json'
4
+
5
+ module BrightpearlCommand
6
+
7
+ class DummyOrder < ::Convoy::ActionCommand::Base
8
+
9
+ SALES_ORDER = 'so'
10
+ SALES_CREDIT = 'sc'
11
+ PURCHASE_ORDER = 'po'
12
+ PURCHASE_CREDIT = 'pc'
13
+
14
+ def execute
15
+
16
+ @opts = command_options
17
+ # @database = Brightpearl::MySQL::get_database_connection
18
+ opts_validate
19
+ opts_routing
20
+
21
+ end
22
+
23
+ def opts_validate
24
+
25
+ if @opts[:salesOrder]
26
+ if @opts[:salesCredit] || @opts[:purchaseOrder] || @opts[:purchaseCredit]
27
+ error_multi_order
28
+ end
29
+ elsif @opts[:salesCredit]
30
+ if @opts[:salesOrder] || @opts[:purchaseOrder] || @opts[:purchaseCredit]
31
+ error_multi_order
32
+ end
33
+ elsif @opts[:purchaseOrder]
34
+ if @opts[:salesCredit] || @opts[:salesOrder] || @opts[:purchaseCredit]
35
+ error_multi_order
36
+ end
37
+ elsif @opts[:purchaseCredit]
38
+ if @opts[:salesCredit] || @opts[:salesOrder] || @opts[:purchaseOrder]
39
+ error_multi_order
40
+ end
41
+ else
42
+ @opts[:salesOrder] = true
43
+ end
44
+
45
+ if @opts[:times].nil?
46
+ @opts[:times] = 1
47
+ elsif @opts[:times] <= 0 || @opts[:times] > 50
48
+ error_invalid_quantity
49
+ end
50
+
51
+ end
52
+
53
+ def opts_routing
54
+
55
+ if @opts[:salesOrder]
56
+ create_order(SALES_ORDER)
57
+ elsif @opts[:salesCredit]
58
+ create_order(SALES_CREDIT)
59
+ elsif @opts[:purchaseOrder]
60
+ create_order(PURCHASE_ORDER)
61
+ elsif @opts[:purchaseCredit]
62
+ create_order(PURCHASE_CREDIT)
63
+ else
64
+ create_order(SALES_ORDER)
65
+ end
66
+
67
+ end
68
+
69
+ def create_order(type = SALES_ORDER)
70
+
71
+ validate_type(type)
72
+
73
+ post_data =
74
+ {
75
+ 'orderTypeCode' => 'SO',
76
+ 'reference' => 'order#001',
77
+ 'priceListId' => 2,
78
+ 'invoices' => [
79
+ {
80
+ 'taxDate' => '2014-10-19T12:57:25+00:00'
81
+ }
82
+ ],
83
+ 'placedOn' => '2014-10-19T11:12:24.000+01:00',
84
+ 'orderStatus' => {
85
+ 'orderStatusId' => 1
86
+ },
87
+ 'delivery' => {
88
+ 'deliveryDate' => '2014-10-19T11:12:24.000+01:00',
89
+ 'shippingMethodId' => Brightpearl::MySQL::get_random_shipping_id
90
+ },
91
+ 'currency' => {
92
+ 'orderCurrencyCode' => 'GBP'
93
+ },
94
+ 'parties' => {
95
+ 'customer' => {
96
+ 'contactId' => Brightpearl::MySQL::get_random_customer_id
97
+ }
98
+ },
99
+ 'assignment' => {
100
+ 'current' => {
101
+ 'channelId' => 1,
102
+ 'leadSourceId' => 1,
103
+ 'staffOwnerContactId' => 4
104
+ }
105
+ }
106
+ }.to_json
107
+
108
+ puts
109
+ puts "\x1B[33m#{post_data}\x1B[0m"
110
+ puts
111
+ exit
112
+
113
+ begin
114
+ response = RestClient.post(
115
+ "http://#{Brightpearl::Config.param(Brightpearl::Config::VM_IP)}:#{Brightpearl::Enum::PORT_ORDER_SERVICE}/order-service/app/order",
116
+ post_data,
117
+ :content_type => :json, :accept => :json, :'account-version' => 'admindev', :'effective-user-id' => 6
118
+ )
119
+ rescue
120
+ puts response
121
+ ensure
122
+ puts response
123
+ end
124
+
125
+
126
+ end
127
+
128
+ def validate_type(type)
129
+ unless [
130
+ SALES_ORDER,
131
+ SALES_CREDIT,
132
+ PURCHASE_ORDER,
133
+ PURCHASE_CREDIT
134
+ ].include?(type)
135
+ raise RuntimeError, 'Invalid order type.'
136
+ end
137
+ end
138
+
139
+ def error_multi_order
140
+ puts "You can only create \x1B[35mONE\x1B[0m type of order at the time. Please specify \x1B[35mONLY ONE\x1B[0m of the following: \x1B[90m-s, -S, -p, -P\x1B[0m"
141
+ exit
142
+ end
143
+
144
+ def error_invalid_quantity
145
+ puts "You can only create between 1-50 orders at a time. You tried to create #{@opts[:times]}."
146
+ exit
147
+ end
148
+
149
+ end
150
+
151
+ end
@@ -0,0 +1,55 @@
1
+ module BrightpearlCommand
2
+
3
+ class GitBranch < ::Convoy::ActionCommand::Base
4
+
5
+ def execute
6
+
7
+ @opts = command_options
8
+ @sort = Brightpearl::Git::SORT_REFNAME
9
+ @git = Brightpearl::Git.new
10
+ opts_validate
11
+ opts_routing
12
+
13
+ end
14
+
15
+ def opts_validate
16
+
17
+ if @opts[:remote] && @opts[:local]
18
+ # @todo Change to new style error messages.
19
+ abort('Cannot list both local & remote branches simultaneously. Please choose one option only.')
20
+ end
21
+ @sort = Brightpearl::Git::SORT_DATE if @opts[:sortDate]
22
+
23
+ end
24
+
25
+ def opts_routing
26
+
27
+ if @opts[:local]
28
+ list_local_branches
29
+ elsif @opts[:remote]
30
+ list_remote_branches
31
+ else
32
+ if @opts[:sortDate]
33
+ system('bp g b -ld')
34
+ else
35
+ system('bp g b -l')
36
+ end
37
+ end
38
+
39
+ end
40
+
41
+ # List local branches.
42
+ # @return void
43
+ def list_local_branches
44
+ @git.show_branches(@sort, Brightpearl::Git::LOCAL)
45
+ end
46
+
47
+ # List remote branches.
48
+ # @return void
49
+ def list_remote_branches
50
+ @git.show_branches(@sort, Brightpearl::Git::REMOTE)
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,155 @@
1
+ module BrightpearlCommand
2
+
3
+ class GitCheckout < ::Convoy::ActionCommand::Base
4
+
5
+ def execute
6
+
7
+ @opts = command_options
8
+ @args = arguments
9
+ @git = Brightpearl::Git.new
10
+ opts_validate
11
+ opts_routing
12
+
13
+ end
14
+
15
+ def opts_validate
16
+
17
+ unless @args.any?
18
+ if @opts[:branch] || @opts[:branch_origin]
19
+ puts 'You must specify a branch name.'
20
+ else
21
+ puts "\n\x1B[41m ERROR \x1B[0m You must specify a branch to checkout. Here is a list of your \x1B[35mLOCAL BRANCHES\x1B[0m"
22
+ Brightpearl::Git.new.show_branches(Brightpearl::Git::SORT_REFNAME, Brightpearl::Git::LOCAL)
23
+ end
24
+ exit
25
+ end
26
+
27
+ end
28
+
29
+ def opts_routing
30
+
31
+ if @opts[:branch]
32
+ create_branch(false)
33
+ elsif @opts[:branch_origin]
34
+ create_branch(true)
35
+ else
36
+ checkout_branch
37
+ end
38
+
39
+ end
40
+
41
+ # Creates a branch on both the 'code' & 'db' repos.
42
+ # @return void
43
+ def create_branch(push_to_origin = false)
44
+
45
+ new_branch_name = @args[0]
46
+ source_branch_name = @args[1].nil? ? Brightpearl::Git::MASTER : @args[1]
47
+
48
+ Brightpearl::Terminal::info("Checking that branch #{Brightpearl::Terminal::format_branch(new_branch_name)} doesn't already exist")
49
+ if @git.branch_exists_anywhere(new_branch_name)
50
+ Brightpearl::Terminal::error('Target branch already exists', "Cannot create branch because a branch named #{Brightpearl::Terminal::format_branch(new_branch_name)}\x1B[38;5;240m is already in use somewhere.", true)
51
+ end
52
+
53
+ # Validate that source branch (if not MASTER) exists locally.
54
+ if source_branch_name != Brightpearl::Git::MASTER
55
+ # Check branch exists anywhere.
56
+ unless @git.branch_exists_anywhere(source_branch_name)
57
+ Brightpearl::Terminal::error("Source branch doesn't exist", "Cannot cut from branch #{Brightpearl::Terminal::format_branch(source_branch_name)}\x1B[38;5;240m because it doesn't exist.", true)
58
+ end
59
+ end
60
+
61
+ @git.stash_staged_changes
62
+ if Brightpearl::Terminal::prompt_yes_no('Create new branch?', "You're about to cut branch #{Brightpearl::Terminal::format_branch(new_branch_name)}\x1B[38;5;240m from #{Brightpearl::Terminal::format_branch(source_branch_name)}\x1B[38;5;240m#{push_to_origin ? ' and push it to origin.' : ''}")
63
+ source_branch_final = []
64
+ source_branch_data = @git.branch_data(source_branch_name)
65
+ source_branch_data.each do |branch_data|
66
+ if branch_data[:"#{Brightpearl::Git::BRANCH_EXISTS}"]
67
+ sb = source_branch_name
68
+ source_branch_final << source_branch_name
69
+ else
70
+ sb = Brightpearl::Git::MASTER
71
+ source_branch_final << Brightpearl::Git::MASTER
72
+ if source_branch_name != Brightpearl::Git::MASTER
73
+ Brightpearl::Terminal::warning('Branch not found', ["The repo: #{Brightpearl::Terminal::format_directory(branch_data[:"#{Brightpearl::Git::BRANCH_LOCATION}"])}\x1B[38;5;240m doesn't have have a branch called #{Brightpearl::Terminal::format_branch(source_branch_name)}", "Because of this, branch for this repo will be cut from #{Brightpearl::Terminal::format_branch(Brightpearl::Git::MASTER)}\x1B[38;5;240m instead."])
74
+ end
75
+ end
76
+ commands = [
77
+ "git checkout #{sb}"
78
+ ]
79
+ if branch_data[:"#{Brightpearl::Git::BRANCH_HAS_UPSTREAM}"]
80
+ commands << 'git pull'
81
+ end
82
+ commands << "git checkout -b #{new_branch_name}"
83
+ if push_to_origin
84
+ commands << "git push --set-upstream origin #{new_branch_name}"
85
+ end
86
+ Brightpearl::Terminal::command(commands, branch_data[:"#{Brightpearl::Git::BRANCH_LOCATION}"])
87
+ end
88
+
89
+ if source_branch_final[0] == source_branch_final[1]
90
+ source_branch_final = source_branch_final[0]
91
+ else
92
+ source_branch_final = "#{source_branch_final[0]}/#{source_branch_final[1]}"
93
+ end
94
+ Brightpearl::Terminal::success('Branch created!', "Branch #{Brightpearl::Terminal::format_branch(new_branch_name)}\x1B[38;5;240m has been successfully cut from #{Brightpearl::Terminal::format_branch(source_branch_final)}")
95
+ else
96
+ Brightpearl::Terminal::abort(nil, nil, true, false)
97
+ end
98
+
99
+ end
100
+
101
+ # Checks out a branch for both /code & /db.
102
+ # @return void
103
+ def checkout_branch
104
+ # todo
105
+ puts
106
+ puts "\x1B[31mTODO!\x1B[0m Must alert user that if DB branch doesn't exist, A) Create it? and B) If not, inform you are on 2 different branches."
107
+ puts
108
+ branch_to_checkout = @args[0]
109
+ branch_data = @git.branch_data(branch_to_checkout)
110
+ # If branch is already checked out.
111
+ if branch_data[0][:"#{Brightpearl::Git::BRANCH_IS_CURRENT}"] && branch_data[1][:"#{Brightpearl::Git::BRANCH_IS_CURRENT}"]
112
+ Brightpearl::Terminal::info("You are already on branch #{Brightpearl::Terminal::format_branch(branch_to_checkout)}")
113
+ if @opts[:update] || @opts[:updatePush]
114
+ Brightpearl::Terminal::info("To update the branch, run: #{Brightpearl::Terminal::format_command('bp g u')}", nil, false)
115
+ end
116
+ @git.check_for_stash
117
+ exit
118
+ end
119
+ @git.stash_staged_changes
120
+ # If branch doesn't exist.
121
+ if branch_data[0][:"#{Brightpearl::Git::BRANCH_EXISTS}"] == false && branch_data[1][:"#{Brightpearl::Git::BRANCH_EXISTS}"] == false
122
+ Brightpearl::Terminal::error("The branch you're trying to checkout #{Brightpearl::Terminal::format_branch(branch_to_checkout)} doesn't exist", 'Please check your spelling and try again.', true, false)
123
+ end
124
+ count = 0
125
+ branch_data.each do |branch_data_inner|
126
+ if branch_data_inner[:"#{Brightpearl::Git::BRANCH_EXISTS}"] == false
127
+ next
128
+ end
129
+ commands = []
130
+ if @opts[:update] || @opts[:updatePush]
131
+ commands << "git checkout #{Brightpearl::Git::MASTER}"
132
+ commands << 'git pull'
133
+ end
134
+ unless branch_data_inner[:"#{Brightpearl::Git::BRANCH_IS_CURRENT}"]
135
+ commands << "git checkout #{branch_to_checkout}"
136
+ end
137
+ if @opts[:update] || @opts[:updatePush]
138
+ if branch_data[count][:"#{Brightpearl::Git::BRANCH_HAS_UPSTREAM}"]
139
+ commands << 'git pull'
140
+ else
141
+ Brightpearl::Terminal::warning('Branch has no upstream', ["You can push to origin by running: #{Brightpearl::Terminal::format_command("git push --set-upstream origin #{branch_data_inner[:"#{Brightpearl::Git::BRANCH_NAME}"]}")}"])
142
+ end
143
+ commands << "git merge #{Brightpearl::Git::MASTER}"
144
+ end
145
+ if @opts[:updatePush]
146
+ commands << 'git push'
147
+ end
148
+ Brightpearl::Terminal::command(commands, branch_data_inner[:"#{Brightpearl::Git::BRANCH_LOCATION}"])
149
+ count = count + 1
150
+ end
151
+ @git.check_for_stash
152
+ end
153
+ end
154
+
155
+ end
@@ -0,0 +1,120 @@
1
+ module BrightpearlCommand
2
+
3
+ class GitDelete < ::Convoy::ActionCommand::Base
4
+
5
+ def execute
6
+ @opts = command_options
7
+ @args = arguments
8
+ @git = Brightpearl::Git.new
9
+ opts_validate
10
+ opts_routing
11
+
12
+ end
13
+
14
+ def opts_validate
15
+
16
+ if @opts[:delete_local] == false && @opts[:delete_remote] == false && @opts[:delete_both] == false
17
+ Brightpearl::Terminal::error('You must specify one of the following flags:', [
18
+ "-l --delete-local \xe2\x86\x92 Delete local branch",
19
+ "-r --delete-remote \xe2\x86\x92 Delete remote branch",
20
+ "-b --delete-both \xe2\x86\x92 Delete both local & remote branches"
21
+ ], true)
22
+
23
+ end
24
+
25
+ unless @args.any?
26
+ @args[0] = @git.current_branch_for_repo(Brightpearl::Config.param(Brightpearl::Config::WORKSTATION_PATH_TO_BP_CODE))
27
+ end
28
+
29
+ if @args[0] == 'master'
30
+ puts "You cannot delete \x1B[32m[master]\x1B[0m"
31
+ exit
32
+ end
33
+
34
+ end
35
+
36
+ def opts_routing
37
+
38
+ if @opts[:delete_local]
39
+ delete_local
40
+ elsif @opts[:delete_remote]
41
+ delete_remote
42
+ elsif @opts[:delete_both]
43
+ delete_both
44
+ end
45
+
46
+ end
47
+
48
+ def delete_local
49
+ # todo
50
+ puts
51
+ puts "\x1B[31mTODO!\x1B[0m Must check that branch is up-to-date with it's remote counter-part, otherwise DO NOT delete!"
52
+ puts "\x1B[31mTODO!\x1B[0m Must check branch even exists."
53
+ Brightpearl::Terminal::prompt_yes("You're about to \x1B[35mLOCALLY DELETE\x1B[0m branch \x1B[32m[#{@args[0]}]\x1B[0m")
54
+ @git.repo_loop.each do |repo_dir|
55
+ commands = []
56
+ if @git.current_branch_for_repo(repo_dir) == @args[0]
57
+ commands << 'git checkout master'
58
+ end
59
+ commands << "git branch -d #{@args[0]}"
60
+ Brightpearl::Terminal::command(commands, repo_dir)
61
+ end
62
+ puts "\nSuccessfully \x1B[35mLOCALLY DELETED\x1B[0m branch \x1B[32m[#{@args[0]}]\x1B[0m\n\n"
63
+ end
64
+
65
+ def delete_remote
66
+ # todo
67
+ puts
68
+ puts "\x1B[31mTODO!\x1B[0m Must check branch even exists."
69
+ Brightpearl::Terminal::prompt_yes("You're about to \x1B[35mREMOTELY DELETE\x1B[0m branch \x1B[32m[#{@args[0]}]\x1B[0m")
70
+ @git.repo_loop.each do |repo_dir|
71
+ unless @git.branch_exists(@args[0], repo_dir, Brightpearl::Git::REMOTE)
72
+ puts "Branch \x1B[32m[#{@args[0]}]\x1B[0m not found in: \x1B[33m#{repo_dir}\x1B[0m. Aborting \x1B[35mREMOTE DELETE\x1B[0m for this repo."
73
+ next
74
+ end
75
+ commands = [
76
+ "git branch --unset-upstream #{@args[0]}",
77
+ "git push origin --delete #{@args[0]}",
78
+ ]
79
+ Brightpearl::Terminal::command(commands, repo_dir)
80
+ end
81
+
82
+ puts "\nSuccessfully \x1B[35mREMOTELY DELETED\x1B[0m branch \x1B[32m[#{@args[0]}]\x1B[0m\n\n"
83
+ new_branch_name = @args[0].split('-')
84
+ new_branch_name = "MERGED-#{new_branch_name[new_branch_name.count - 1]}"
85
+ Brightpearl::Terminal::prompt_yes("Would you like to \x1B[35mRENAME\x1B[0m \x1B[32m[#{@args[0]}]\x1B[0m to \x1B[32m[#{new_branch_name}]\x1B[0m")
86
+
87
+ @git.repo_loop.each do |repo_dir|
88
+ unless @git.branch_exists(@args[0], repo_dir, Brightpearl::Git::LOCAL)
89
+ puts "Branch \x1B[32m[#{@args[0]}]\x1B[0m not found in: \x1B[33m#{repo_dir}\x1B[0m. Aborting \x1B[35mRENAME\x1B[0m for this repo."
90
+ next
91
+ end
92
+ commands = [
93
+ "git branch -m #{@args[0]} #{new_branch_name}",
94
+ ]
95
+ Brightpearl::Terminal::command(commands, repo_dir)
96
+ end
97
+ puts
98
+ end
99
+
100
+ def delete_both
101
+ # todo
102
+ puts
103
+ puts "\x1B[31mTODO!\x1B[0m Must check that branch is up-to-date with it's remote counter-part, otherwise DO NOT delete!"
104
+ puts "\x1B[31mTODO!\x1B[0m Must check branch even exists."
105
+ Brightpearl::Terminal::prompt_yes("You're about to \x1B[35mLOCALLY & REMOTELY DELETE\x1B[0m branch \x1B[32m[#{@args[0]}]\x1B[0m")
106
+ @git.repo_loop.each do |repo_dir|
107
+ commands = []
108
+ if @git.current_branch_for_repo(repo_dir) == @args[0]
109
+ commands << 'git checkout master'
110
+ end
111
+ commands << "git branch -d #{@args[0]}"
112
+ commands << "git push origin --delete #{@args[0]}"
113
+ Brightpearl::Terminal::command(commands, repo_dir)
114
+ end
115
+ puts "\nSuccessfully \x1B[35mLOCALLY & REMOTELY DELETED\x1B[0m branch \x1B[32m[#{@args[0]}]\x1B[0m\n\n"
116
+ end
117
+
118
+ end
119
+
120
+ end