Class: ECpayInvoice::InvoiceParamVerify

Inherits:
InvoiceVerifyBase show all
Includes:
ECpayErrorDefinition
Defined in:
lib/ecpay_invoice/verification.rb

Instance Method Summary collapse

Methods inherited from InvoiceVerifyBase

#get_all_pattern, #get_basic_params, #get_cond_param, #get_depopt_param_pattern, #get_int_param_pattern, #get_opt_param_pattern, #get_param_type, #get_special_encode_param, #get_str_param_pattern, #get_svc_url, #verify_param_by_pattern

Constructor Details

#initialize(apiname) ⇒ InvoiceParamVerify

Returns a new instance of InvoiceParamVerify.



203
204
205
206
207
# File 'lib/ecpay_invoice/verification.rb', line 203

def initialize(apiname)
    @inv_basic_param = self.get_basic_params(apiname).freeze
    @inv_conditional_param = self.get_cond_param(apiname).freeze
    @all_param_pattern = self.get_all_pattern(apiname).freeze
end

Instance Method Details

#verify_inv_allowance_invalid_param(params) ⇒ Object



955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
# File 'lib/ecpay_invoice/verification.rb', line 955

def verify_inv_allowance_invalid_param(params)
    if params.is_a?(Hash)
        param_diff = @inv_basic_param - params.keys
        unless param_diff == []
            raise ECpayInvalidParam, "Lack required param #{param_diff}"
        end

        #Verify Value pattern of each param
        self.verify_param_by_pattern(params, @all_param_pattern)

    else
        raise TypeError, "Recieved argument is not a hash"
    end

end

#verify_inv_allowance_param(params) ⇒ Object



825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
# File 'lib/ecpay_invoice/verification.rb', line 825

def verify_inv_allowance_param(params)
    if params.is_a?(Hash)
        #發票所有參數預設要全帶

        if params.has_value?(nil)
            raise ECpayInvalidParam, %Q{Parameter value cannot be nil}
        end
        #1. 比對欄位是否缺乏
        param_diff = @inv_basic_param - params.keys()
        unless param_diff == []
            raise ECpayInvalidParam, %Q{Lack required invoice param #{param_diff}}
        end

        #2. 比對特殊欄位值相依需求
        #NotifyPhone和NotifyMail至少一個有值
        if params['AllowanceNotify'].to_s == 'S'
            if params['NotifyPhone'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[NotifyPhone] cannot be empty."
            end
        elsif params['AllowanceNotify'].to_s == 'E'
            if params['NotifyMail'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[NotifyMail] cannot be empty."
            end
        elsif params['AllowanceNotify'].to_s == 'A'
            if params['NotifyPhone'].to_s.empty? or params['NotifyMail'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[NotifyPhone] and [NotifyMail] can not be empty."
            end
        end

        vat_params = ['ItemCount', 'ItemAmount']
        # 商品價錢含有管線 => 認為是多樣商品 *ItemCount , *ItemPrice , *ItemAmount 逐一用管線分割,計算數量後與第一個比對
        # 驗證單筆ItemAmount = (ItemPrice * ItemCount)
        if !params['ItemPrice'].include?('|')
            unless params['ItemAmount'].to_i == (params['ItemPrice'].to_i * params['ItemCount'].to_i).round
                raise ECpayInvoiceRuleViolate, %Q{[ItemPrice] (#{params['ItemPrice'].to_i}) times [ItemCount] (#{params['ItemCount'].to_i}) subtotal not equal [ItemAmount] (#{params['ItemAmount'].to_i})}
            end
            # 驗證單筆商品合計是否等於發票金額
            unless params['AllowanceAmount'].to_i == params['ItemAmount'].to_i
                raise ECpayInvoiceRuleViolate, %Q{[ItemAmount] (#{params['ItemAmount'].to_i}) not equal [AllowanceAmount] (#{params['AllowanceAmount'].to_i})}
            end
        elsif params['ItemPrice'].include?('|')
            vat_cnt = params['ItemPrice'].split('|').length
            vat_params.each do |param_name|
                # Check if there's empty value.
                unless /(\|\||^\||\|$)/.match(params[param_name]).nil?
                    raise ECpayInvoiceRuleViolate, "[#{param_name}] contains empty value."
                end
                p_cnt = params[param_name].split('|').length
                unless vat_cnt == p_cnt
                    raise ECpayInvoiceRuleViolate, %Q{Count of item info [#{param_name}] (#{p_cnt}) not match item count from [ItemCount] (#{vat_cnt})}
                end
            end
            vat_amount_arr = params['ItemAmount'].split('|')
            vat_price_arr = params['ItemPrice'].split('|')
            vat_count_arr = params['ItemCount'].split('|')
            (1..vat_cnt).each do |index|
                unless vat_amount_arr[index - 1].to_i == (vat_price_arr[index - 1].to_i * vat_count_arr[index - 1].to_i).round
                    raise ECpayInvoiceRuleViolate, %Q{[ItemPrice] (#{vat_price_arr[index - 1]}) times [ItemCount] (#{vat_count_arr[index - 1]}) not match [ItemAmount] (#{vat_amount_arr[index - 1].to_i})}
                end
                #Verify ItemAmount subtotal equal SalesAmount
                chk_amount_subtotal = 0
                vat_amount_arr.each do |val|
                    chk_amount_subtotal += val.to_i
                end
                unless params['AllowanceAmount'].to_i == chk_amount_subtotal
                    raise ECpayInvoiceRuleViolate, %Q{[ItemAmount] (#{vat_amount_arr}) subtotal not equal [AllowanceAmount] (#{params['AllowanceAmount'].to_i})}
                end
            end
        end

        #3. 比對商品名稱,數量,單位,價格,tax,合計,備註項目數量是否一致,欄位是否為空
        if params['ItemName'].to_s.empty? or params['ItemWord'].to_s.empty?
            raise ECpayInvoiceRuleViolate, "[ItemName] or [ItemWord] cannot be empty"
        end
        item_params = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount']
        #商品名稱含有管線 => 認為是多樣商品 *ItemName, *ItemCount ,*ItemWord, *ItemPrice, *ItemAmount逐一用管線分割,計算數量後與第一個比對
        if params['ItemName'].include?('|')
            item_cnt = params['ItemName'].split('|').length
            item_params.each do |param_name|
                # Check if there's empty value.
                unless /(\|\||^\||\|$)/.match(params[param_name]).nil?
                    raise ECpayInvoiceRuleViolate, "[#{param_name}] contains empty value."
                end
                p_cnt = params[param_name].split('|').length
                unless item_cnt == p_cnt
                    raise ECpayInvoiceRuleViolate, %Q{Count of item info [#{param_name}] (#{p_cnt}) not match item count from [ItemCount] (#{item_cnt})}
                end
            end
            # ItemTaxType 能含有1,2 3(and at least contains one 1 and other)
            if params['ItemTaxType'].include?('|')
                item_tax = params['ItemTaxType'].split('|')
                aval_tax_type = ['1', '3']
                vio_tax_t = (item_tax - aval_tax_type)
                unless vio_tax_t == []
                    raise ECpayInvoiceRuleViolate, "Ilegal [ItemTaxType]: #{vio_tax_t}"
                end
            end
        else
            #沒有管線 => 逐一檢查後6項有無管線
            item_params.each do |param_name|
                if params[param_name].include?('|')
                    raise "Item info [#{param_name}] contains pipeline delimiter but there's only one item in param [ItemName]"
                end
            end
        end

        #4 比對所有欄位Pattern
        self.verify_param_by_pattern(params, @all_param_pattern)

    else
        raise TypeError, "Recieved argument is not a hash"
    end
end

#verify_inv_delay_param(params) ⇒ Object



557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
# File 'lib/ecpay_invoice/verification.rb', line 557

def verify_inv_delay_param(params)
    if params.is_a?(Hash)
        #發票所有參數預設要全帶

        if params.has_value?(nil)
            raise ECpayInvalidParam, %Q{Parameter value cannot be nil}
        end
        #1. 比對欄位是否缺乏
        param_diff = @inv_basic_param - params.keys()
        unless param_diff == []
            raise ECpayInvalidParam, %Q{Lack required invoice param #{param_diff}}
        end

        #2. 比對特殊欄位值相依需求
        #a [CarruerType]為 1 => CustomerID 不能為空
        if params['CarruerType'].to_s == '1'
            if params['CustomerID'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[CustomerID] can not be empty when [CarruerType] is 1."
            end
            # [CustomerID]不為空 => CarruerType 不能為空
        elsif params['CarruerType'].to_s == ''
            unless params['CustomerID'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[CarruerType] can not be empty when [CustomerID] is not empty."
            end
        end
        #b 列印註記[Print]為 1 => CustomerName, CustomerAddr
        if params['Print'].to_s == '1'
            if params['CustomerName'].to_s.empty? or params['CustomerAddr'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[CustomerName] and [CustomerAddr] can not be empty when [Print] is 1."
            end
            unless params['CustomerID'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[Print] can not be '1' when [CustomerID] is not empty."
            end
            unless params['CarruerType'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[Print] can not be '1' when [CarruerType] is not empty."
            end
            unless params['CarruerNum'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[Print] can not be '1' when [CarruerNum] is not empty."
            end
        end

        #c CustomerPhone和CustomerEmail至少一個有值
        if  params['CustomerPhone'].to_s.empty? and params['CustomerEmail'].to_s.empty?
            raise ECpayInvoiceRuleViolate, "[CustomerPhone] and [CustomerEmail] can not both be empty."
        end

        #d [TaxType]為 2 => ClearanceMark = 必須為 1 or 2,ItemTaxType 必須為空
        if params['TaxType'].to_s == '2'
            @tax_fee = 1.05
            @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount']
            @vat_params_list = ['ItemCount', 'ItemAmount']
            unless ['1', '2'].include?(params['ClearanceMark'].to_s)
                raise ECpayInvoiceRuleViolate, "[ClearanceMark] has to be 1 or 2 when [TaxType] is 2."
            end
            unless params['ItemTaxType'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[ItemTaxType] must be empty when [TaxType] is 2."
            end
            #d.1 [TaxType]為 1 => ItemTaxType, ClearanceMark 必須為空
        elsif params['TaxType'].to_s == '1'
            @tax_fee = 1
            @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount']
            @vat_params_list = ['ItemCount', 'ItemAmount']
            unless params['ItemTaxType'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[ItemTaxType] must be empty when [TaxType] is 1."
            end
            unless params['ClearanceMark'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[ClearanceMark] must be empty when [TaxType] is 1."
            end
            #d.2 [TaxType]為 3 => ItemTaxType, ClearanceMark 必須為空
        elsif params['TaxType'].to_s == '3'
            @tax_fee = 1.05
            @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount']
            @vat_params_list = ['ItemCount', 'ItemAmount']
            unless params['ItemTaxType'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[ItemTaxType] must be empty when [TaxType] is 3."
            end
            unless params['ClearanceMark'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[ClearanceMark] must be empty when [TaxType] is 3."
            end
            #d.3 [TaxType]為 9 => ItemTaxType 必須為兩項商品(含)以上,且不可為空
        elsif params['TaxType'].to_s == '9'
            @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount', 'ItemTaxType']
            @vat_params_list = ['ItemCount', 'ItemAmount', 'ItemTaxType']
            unless params['ItemTaxType'].include?('|')
                raise ECpayInvoiceRuleViolate, "[ItemTaxType] must contain at lease one '|'."
            end
            if params['ItemTaxType'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[ItemTaxType] can not be empty when [TaxType] is 9."
            end
        end

        #e 統一編號[CustomerIdentifier]有值時 => CarruerType != 1, 2 or 3, *Donation = 2, print = 1
        unless params['CustomerIdentifier'].to_s.empty?
            if ['1', '2', '3'].include?(params['CarruerType'].to_s)
                raise ECpayInvoiceRuleViolate, "[CarruerType] Cannot be 1, 2 or 3 when [CustomerIdentifier] is given."
            end
            unless params['Donation'].to_s == '2' and params['Print'].to_s == '1'
                raise ECpayInvoiceRuleViolate, "[Print] must be 1 and [Donation] must be 2 when [CustomerIdentifier] is given."
            end
        end

        # DelayFlag Rules When [DelayFlag] is '1' the [DelayDay] range be between 1 and 15
        # When [DelayFlag] is '2' the [DelayDay] range be between 0 and 15
        if params['DelayFlag'].to_s == '1'
            if params['DelayDay'].to_i > 15 or params['DelayDay'].to_i < 1
                raise ECpayInvoiceRuleViolate, "[DelayDay] must be between 1 and 15  when [DelayFlag] is '1'."
            end
        elsif params['DelayFlag'].to_s == '2'
            if params['DelayDay'].to_i > 15 or params['DelayDay'].to_i < 0
                raise ECpayInvoiceRuleViolate, "[DelayDay] must be between 0 and 15  when [DelayFlag] is '2'."
            end
        end

        # [CarruerType]為'' or 1 時 => CarruerNum = '', [CarruerType]為 2, CarruerNum = 固定長度為 16 且格式為 2 碼大小寫字母加上 14 碼數字。 [CarruerType]為 3 ,帶固定長度為 8 且格式為 1 碼斜線「/」加上由 7 碼數字及大小寫字母組成
        if ['', '1'].include?(params['CarruerType'].to_s)
            unless params['CarruerNum'].to_s == ''
                raise ECpayInvoiceRuleViolate, "[CarruerNum] must be empty when [CarruerType] is empty or 1."
            end
        elsif params['CarruerType'].to_s == '2'
            unless params['CustomerID'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[CustomerID] must be empty when [CarruerType] is 3."
            end
            if /[A-Za-z]{2}[0-9]{14}/.match(params['CarruerNum']).nil?
                raise ECpayInvoiceRuleViolate, "[CarruerNum] must be 2 alphabets and 14 numbers when [CarruerType] is 2."
            end
        elsif params['CarruerType'].to_s == '3'
            unless params['CustomerID'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[CustomerID] must be empty when [CarruerType] is 3."
            end
            if /^\/[A-Za-z0-9\s+-]{7}$/.match(params['CarruerNum']).nil?
                raise ECpayInvoiceRuleViolate, "[CarruerNum] must start with '/' followed by 7 alphabet and number characters when [CarruerType] is 3."
            end
        else
            raise ECpayInvoiceRuleViolate, "Unexpected value in [CarruerType]."
        end

        # Donation = 1 => LoveCode不能為空, print = 0
        if params['Donation'].to_s == '1'
            if params['LoveCode'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[LoveCode] cannot be empty when [Donation] is 1."
            end
            unless params['Print'].to_s == '0'
                raise ECpayInvoiceRuleViolate, "[Print] must be 0 when [Donation] is 1."
            end
        # Donation = 2 => LoveCode不能有值
        elsif params['Donation'].to_s == '2'
            unless params['LoveCode'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[LoveCode] must be empty when [Donation] is 2."
            end
        end

        vat_params = @vat_params_list
        # 商品價錢含有管線 => 認為是多樣商品 *ItemCount , *ItemPrice , *ItemAmount 逐一用管線分割,計算數量後與第一個比對
        if !params['ItemPrice'].include?('|')
            unless params['ItemAmount'].to_i == (params['ItemPrice'].to_i * params['ItemCount'].to_i / @tax_fee).round
                raise ECpayInvoiceRuleViolate, %Q{[ItemPrice] (#{params['ItemPrice'].to_i}) times [ItemCount] (#{params['ItemCount'].to_i}) '/' tax (#{@tax_fee}) subtotal not equal [ItemAmount] (#{params['ItemAmount'].to_i})}
            end
            # 驗證單筆商品合計是否等於發票金額
            unless params['SalesAmount'].to_i == params['ItemAmount'].to_i
                raise ECpayInvoiceRuleViolate, %Q{[ItemAmount] (#{params['ItemAmount'].to_i}) not equal [SalesAmount] (#{params['SalesAmount'].to_i})}
            end
        elsif params['ItemPrice'].include?('|')
            vat_cnt = params['ItemPrice'].split('|').length
            vat_params.each do |param_name|
                # Check if there's empty value.
                unless /(\|\||^\||\|$)/.match(params[param_name]).nil?
                    raise ECpayInvoiceRuleViolate, "[#{param_name}] contains empty value."
                end
                p_cnt = params[param_name].split('|').length
                unless vat_cnt == p_cnt
                    raise ECpayInvoiceRuleViolate, %Q{Count of item info [#{param_name}] (#{p_cnt}) not match item count from [ItemCount] (#{vat_cnt})}
                end
            end
            vat_amount_arr = params['ItemAmount'].split('|')
            vat_price_arr = params['ItemPrice'].split('|')
            vat_count_arr = params['ItemCount'].split('|')
            (1..vat_cnt).each do |index|
                if @vat_params_list.length == 3
                    vat_tax_arr = params['ItemTaxType'].split('|')
                    if vat_tax_arr[index - 1].to_s == '1'
                        @tax_fee = 1
                    elsif vat_tax_arr[index - 1].to_s == '2' or vat_tax_arr[index - 1].to_s == '3'
                        @tax_fee = 1.05
                    else
                        raise ECpayInvoiceRuleViolate, "[ItemTaxType] can not be (#{vat_tax_arr[index - 1]}). Avaliable option: (1, 2, 3)."
                    end
                end
                unless vat_amount_arr[index - 1].to_i == (vat_price_arr[index - 1].to_i * vat_count_arr[index - 1].to_i / @tax_fee).round
                    raise ECpayInvoiceRuleViolate, %Q{[ItemPrice] (#{vat_price_arr[index - 1]}) times [ItemCount] (#{vat_count_arr[index - 1]}) '/' tax(#{@tax_fee}) not match [ItemAmount] (#{vat_amount_arr[index - 1].to_i})}
                end
                #Verify ItemAmount subtotal equal SalesAmount
                chk_amount_subtotal = 0
                vat_amount_arr.each do |val|
                    chk_amount_subtotal += val.to_i
                end
                unless params['SalesAmount'].to_i == chk_amount_subtotal
                    raise ECpayInvoiceRuleViolate, %Q{[ItemAmount] (#{vat_amount_arr}) subtotal not equal [SalesAmount] (#{params['SalesAmount'].to_i})}
                end
            end
        end

        #3. 比對商品名稱,數量,單位,價格,tax,合計,備註項目數量是否一致,欄位是否為空
        if params['ItemName'].to_s.empty? or params['ItemWord'].to_s.empty?
            raise ECpayInvoiceRuleViolate, "[ItemName] or [ItemWord] cannot be empty"
        end
        item_params = @item_params_list
        #商品名稱含有管線 => 認為是多樣商品 *ItemName, *ItemCount ,*ItemWord, *ItemPrice, *ItemAmount逐一用管線分割,計算數量後與第一個比對
        if params['ItemName'].include?('|')
            item_cnt = params['ItemName'].split('|').length
            item_params.each do |param_name|
                # Check if there's empty value.
                unless /(\|\||^\||\|$)/.match(params[param_name]).nil?
                    raise ECpayInvoiceRuleViolate, "[#{param_name}] contains empty value."
                end
                p_cnt = params[param_name].split('|').length
                unless item_cnt == p_cnt
                    raise ECpayInvoiceRuleViolate, %Q{Count of item info [#{param_name}] (#{p_cnt}) not match item count from [ItemCount] (#{item_cnt})}
                end
            end
            # 課稅類別[TaxType] = 9 時 => ItemTaxType 能含有1,2 3(and at least contains one 1 and other)
            if params['TaxType'].to_s == '9'
                item_tax = params['ItemTaxType'].split('|')
                p item_tax
                aval_tax_type = ['1', '2', '3']
                vio_tax_t = (item_tax - aval_tax_type)
                unless vio_tax_t == []
                    raise ECpayInvoiceRuleViolate, "Ilegal [ItemTaxType]: #{vio_tax_t}"
                end
                unless item_tax.include?('1')
                    raise ECpayInvoiceRuleViolate, "[ItemTaxType] must contain at lease one '1'."
                end
                if !item_tax.include?('2') and !item_tax.include?('3')
                    raise ECpayInvoiceRuleViolate, "[ItemTaxType] cannot be all 1 when [TaxType] is 9."
                end
            end
        else
            #沒有管線 => 逐一檢查後6項有無管線
            item_params.each do |param_name|
                if params[param_name].include?('|')
                    raise "Item info [#{param_name}] contains pipeline delimiter but there's only one item in param [ItemName]"
                end
            end
        end

        #4 比對所有欄位Pattern
        self.verify_param_by_pattern(params, @all_param_pattern)

    else
        raise TypeError, "Recieved argument is not a hash"
    end
end

#verify_inv_issue_invalid_param(params) ⇒ Object



939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
# File 'lib/ecpay_invoice/verification.rb', line 939

def verify_inv_issue_invalid_param(params)
    if params.is_a?(Hash)
        param_diff = @inv_basic_param - params.keys
        unless param_diff == []
            raise ECpayInvalidParam, "Lack required param #{param_diff}"
        end

        #Verify Value pattern of each param
        self.verify_param_by_pattern(params, @all_param_pattern)

    else
        raise TypeError, "Recieved argument is not a hash"
    end

end

#verify_inv_issue_param(params) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/ecpay_invoice/verification.rb', line 209

def verify_inv_issue_param(params)
    if params.is_a?(Hash)
        #發票所有參數預設要全帶

        if params.has_value?(nil)
            raise ECpayInvalidParam, %Q{Parameter value cannot be nil}
        end
        #1. 比對欄位是否缺乏
        param_diff = @inv_basic_param - params.keys()
        unless param_diff == []
            raise ECpayInvalidParam, %Q{Lack required invoice param #{param_diff}}
        end

        #2. 比對特殊欄位值相依需求
        #a [CarruerType]為 1 => CustomerID 不能為空
        if params['CarruerType'].to_s == '1'
            if params['CustomerID'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[CustomerID] can not be empty when [CarruerType] is 1."
            end
        # [CustomerID]不為空 => CarruerType 不能為空
        elsif params['CarruerType'].to_s == ''
            unless params['CustomerID'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[CarruerType] can not be empty when [CustomerID] is not empty."
            end
        end
        #b 列印註記[Print]為 1 => CustomerName, CustomerAddr
        if params['Print'].to_s == '1'
            if params['CustomerName'].to_s.empty? or params['CustomerAddr'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[CustomerName] and [CustomerAddr] can not be empty when [Print] is 1."
            end
            unless params['CustomerID'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[Print] can not be '1' when [CustomerID] is not empty."
            end
            unless params['CarruerType'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[Print] can not be '1' when [CarruerType] is not empty."
            end
            unless params['CarruerNum'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[Print] can not be '1' when [CarruerNum] is not empty."
            end
        end

        #c CustomerPhone和CustomerEmail至少一個有值
        if  params['CustomerPhone'].to_s.empty? and params['CustomerEmail'].to_s.empty?
            raise ECpayInvoiceRuleViolate, "[CustomerPhone] and [CustomerEmail] can not both be empty."
        end

        #d [TaxType]為 2 => ClearanceMark = 必須為 1 or 2,ItemTaxType 必須為空
        if params['TaxType'].to_s == '2'
            if !params['ItemRemark'].to_s.empty?
                @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount', 'ItemRemark']
            else
                @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount']
            end
            @vat_params_list = ['ItemCount', 'ItemAmount']
            unless ['1', '2'].include?(params['ClearanceMark'].to_s)
                raise ECpayInvoiceRuleViolate, "[ClearanceMark] has to be 1 or 2 when [TaxType] is 2."
            end
            unless params['ItemTaxType'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[ItemTaxType] must be empty when [TaxType] is 2."
            end
            # 當[TaxType]為2時為零稅率,vat為0時商品單價為免稅,不須再加稅
            # 若vat為1時商品單價為含稅,須再退稅
            if params['vat'].to_s == '0'
                @tax_fee = 1
            elsif params['vat'].to_s == '1'
                @tax_fee = 1.05
            end
        #d.1 [TaxType]為 1 => ItemTaxType, ClearanceMark 必須為空
        elsif params['TaxType'].to_s == '1'
            if !params['ItemRemark'].to_s.empty?
                @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount', 'ItemRemark']
            else
                @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount']
            end
            @vat_params_list = ['ItemCount', 'ItemAmount']
            unless params['ItemTaxType'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[ItemTaxType] must be empty when [TaxType] is 1."
            end
            unless params['ClearanceMark'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[ClearanceMark] must be empty when [TaxType] is 1."
            end
            # 當[TaxType]為1時為應稅,vat為0時商品單價為免稅,須再加稅
            # 若vat為1時商品單價為含稅,不須再加稅
            if params['vat'].to_s == '0'
                @tax_fee = 1.05
            elsif params['vat'].to_s == '1'
                @tax_fee = 1
            end
        #d.2 [TaxType]為 3 => ItemTaxType, ClearanceMark 必須為空
        elsif params['TaxType'].to_s == '3'
            if !params['ItemRemark'].to_s.empty?
                @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount', 'ItemRemark']
            else
                @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount']
            end
            @vat_params_list = ['ItemCount', 'ItemAmount']
            unless params['ItemTaxType'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[ItemTaxType] must be empty when [TaxType] is 3."
            end
            unless params['ClearanceMark'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[ClearanceMark] must be empty when [TaxType] is 3."
            end
            # 當[TaxType]為3時為免稅,vat為0時商品單價為免稅,不須再加稅
            # 若vat為1時商品單價為含稅,須再退稅
            if params['vat'].to_s == '0'
                @tax_fee = 1
            elsif params['vat'].to_s == '1'
                @tax_fee = 1.05
            end
        #d.3 [TaxType]為 9 => ItemTaxType 必須為兩項商品(含)以上,且不可為空
        elsif params['TaxType'].to_s == '9'
            if !params['ItemRemark'].to_s.empty?
                @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount', 'ItemRemark', 'ItemTaxType']
            else
                @item_params_list = ['ItemCount', 'ItemWord', 'ItemPrice', 'ItemAmount', 'ItemTaxType']
            end
            @vat_params_list = ['ItemCount', 'ItemAmount', 'ItemTaxType']
            unless params['ItemTaxType'].include?('|')
                raise ECpayInvoiceRuleViolate, "[ItemTaxType] must contain at lease one '|'."
            end
            if params['ItemTaxType'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[ItemTaxType] can not be empty when [TaxType] is 9."
            end
            # 當[ItmeTaxType]含2選項的話[ClearanceMark]須為1或2
            if params['ItemTaxType'].include?('2')
                unless ['1', '2'].include?(params['ClearanceMark'].to_s)
                    raise ECpayInvoiceRuleViolate, "[ClearanceMark] has to be 1 or 2 when [ItemTaxType] has 2."
                end
            end
        end

        #e 統一編號[CustomerIdentifier]有值時 => CarruerType != 1, 2 or 3, *Donation = 2, print = 1
        unless params['CustomerIdentifier'].to_s.empty?
            if ['1', '2', '3'].include?(params['CarruerType'].to_s)
                raise ECpayInvoiceRuleViolate, "[CarruerType] Cannot be 1, 2 or 3 when [CustomerIdentifier] is given."
            end
            unless params['Donation'].to_s == '2' and params['Print'].to_s == '1'
                raise ECpayInvoiceRuleViolate, "[Print] must be 1 and [Donation] must be 2 when [CustomerIdentifier] is given."
            end
        end

        # [CarruerType]為'' or 1 時 => CarruerNum = '', [CarruerType]為 2, CarruerNum = 固定長度為 16 且格式為 2 碼大小寫字母加上 14 碼數字。 [CarruerType]為 3 ,帶固定長度為 8 且格式為 1 碼斜線「/」加上由 7 碼數字及大小寫字母組成
        if ['', '1'].include?(params['CarruerType'].to_s)
            unless params['CarruerNum'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[CarruerNum] must be empty when [CarruerType] is empty or 1."
            end
        elsif params['CarruerType'].to_s == '2'
            unless params['CustomerID'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[CustomerID] must be empty when [CarruerType] is 2."
            end
            if /[A-Za-z]{2}[0-9]{14}/.match(params['CarruerNum']).nil?
                raise ECpayInvoiceRuleViolate, "[CarruerNum] must be 2 alphabets and 14 numbers when [CarruerType] is 2."
            end
        elsif params['CarruerType'].to_s == '3'
            unless params['CustomerID'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[CustomerID] must be empty when [CarruerType] is 3."
            end
            if /^\/[A-Za-z0-9\s+-]{7}$/.match(params['CarruerNum']).nil?
                raise ECpayInvoiceRuleViolate, "[CarruerNum] must start with '/' followed by 7 alphabet and number characters when [CarruerType] is 3."
            end
        else
            raise ECpayInvoiceRuleViolate, "Unexpected value in [CarruerType]."
        end

        # Donation = 1 => LoveCode不能為空, print = 0
        if params['Donation'].to_s == '1'
            if params['LoveCode'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[LoveCode] cannot be empty when [Donation] is 1."
            end
            unless params['Print'].to_s == '0'
                raise ECpayInvoiceRuleViolate, "[Print] must be 0 when [Donation] is 1."
            end
        # Donation = 2 => LoveCode不能有值
        elsif params['Donation'].to_s == '2'
            unless params['LoveCode'].to_s.empty?
                raise ECpayInvoiceRuleViolate, "[LoveCode] must be empty when [Donation] is 2."
            end
        end

        # [vat]為0時 => ItemPrice = 未稅, ItemAmount = (ItemPrice * ItemCount) + (ItemPrice * ItemCount * tax(5%))
        # 未稅加稅單一商品時直接四捨五入帶入ItemAmount,且ItemAmount等於SalesAmount
        # 未稅加稅多樣商品時先算稅金加總帶入ItemAmount,且ItemAmount全部金額加總後帶入SalesAmount後四捨五入
        vat_params = @vat_params_list
        # 商品價錢含有管線 => 認為是多樣商品 *ItemCount , *ItemPrice , *ItemAmount 逐一用管線分割,計算數量後與第一個比對
        if params['vat'].to_s == '0'
            if !params['ItemPrice'].include?('|')
                unless params['ItemAmount'].to_i == (params['ItemPrice'].to_i * params['ItemCount'].to_i * @tax_fee).round
                    raise ECpayInvoiceRuleViolate, %Q{[ItemPrice] (#{params['ItemPrice'].to_i}) times [ItemCount] (#{params['ItemCount'].to_i}) '*' tax (#{@tax_fee}) subtotal not equal [ItemAmount] (#{params['ItemAmount'].to_i})}
                end
                # 驗證單筆商品合計是否等於發票金額
                unless params['SalesAmount'].to_i == params['ItemAmount'].to_i
                    raise ECpayInvoiceRuleViolate, %Q{[ItemAmount] (#{params['ItemAmount'].to_i}) not equal [SalesAmount] (#{params['SalesAmount'].to_i})}
                end

            elsif params['ItemPrice'].include?('|')
                vat_cnt = params['ItemPrice'].split('|').length
                vat_params.each do |param_name|
                    # Check if there's empty value.
                    unless /(\|\||^\||\|$)/.match(params[param_name]).nil?
                        raise ECpayInvoiceRuleViolate, "[#{param_name}] contains empty value."
                    end
                    p_cnt = params[param_name].split('|').length
                    unless vat_cnt == p_cnt
                        raise ECpayInvoiceRuleViolate, %Q{Count of item info [#{param_name}] (#{p_cnt}) not match item count from [ItemCount] (#{vat_cnt})}
                    end
                end
                vat_amount_arr = params['ItemAmount'].split('|')
                vat_price_arr = params['ItemPrice'].split('|')
                vat_count_arr = params['ItemCount'].split('|')
                (1..vat_cnt).each do |index|
                    if @vat_params_list.length == 3
                        vat_tax_arr = params['ItemTaxType'].split('|')
                        if vat_tax_arr[index - 1].to_s == '1'
                            @tax_fee = 1.05
                        elsif vat_tax_arr[index - 1].to_s == '2' or vat_tax_arr[index - 1].to_s == '3'
                            @tax_fee = 1
                        else
                            raise ECpayInvoiceRuleViolate, "[ItemTaxType] can not be (#{vat_tax_arr[index - 1]}). Avaliable option: (1, 2, 3)."
                        end
                    end
                    unless vat_amount_arr[index - 1].to_f == (vat_price_arr[index - 1].to_i * vat_count_arr[index - 1].to_i * @tax_fee)
                        raise ECpayInvoiceRuleViolate, %Q{[ItemPrice] (#{vat_price_arr[index - 1].to_i}) times [ItemCount] (#{vat_count_arr[index - 1].to_i}) '*' tax(#{@tax_fee}) not match [ItemAmount] (#{vat_amount_arr[index - 1].to_f})}
                    end
                    #Verify ItemAmount subtotal equal SalesAmount
                    chk_amount_subtotal = 0
                    vat_amount_arr.each do |val|
                        chk_amount_subtotal += val.to_f
                    end
                    unless params['SalesAmount'].to_i == chk_amount_subtotal.round
                        raise ECpayInvoiceRuleViolate, %Q{[ItemAmount] (#{vat_amount_arr}) subtotal not equal [SalesAmount] (#{params['SalesAmount'].to_i})}
                    end
                end
            end
        end

        # [vat]為1時 => ItemPrice = 含稅, ItemAmount = ItemPrice * ItemCount
        # 商品價錢含有管線 => 認為是多樣商品 *ItemCount , *ItemPrice , *ItemAmount 逐一用管線分割,計算數量後與第一個比對
        # 含稅扣稅單一商品時直接四捨五入帶入ItemAmount,且ItemAmount等於SalesAmount
        # 含稅扣稅多樣商品時先算稅金加總四捨五入後帶入ItemAmount,且ItemAmount全部金額加總後等於SalesAmount
        if params['vat'].to_s == '1'
            if !params['ItemPrice'].include?('|')

                unless params['ItemAmount'].to_i == (params['ItemPrice'].to_i * params['ItemCount'].to_i / @tax_fee).round
                    raise ECpayInvoiceRuleViolate, %Q{[ItemPrice] (#{params['ItemPrice'].to_i}) times [ItemCount] (#{params['ItemCount'].to_i}) '/' tax (#{@tax_fee}) subtotal not equal [ItemAmount] (#{params['ItemAmount'].to_i})}
                end
                # 驗證單筆商品合計是否等於發票金額
                unless params['SalesAmount'].to_i == params['ItemAmount'].to_i
                    raise ECpayInvoiceRuleViolate, %Q{[ItemAmount] (#{params['ItemAmount'].to_i}) not equal [SalesAmount] (#{params['SalesAmount'].to_i})}
                end
            elsif params['ItemPrice'].include?('|')
                vat_cnt = params['ItemPrice'].split('|').length
                vat_params.each do |param_name|
                    # Check if there's empty value.
                    unless /(\|\||^\||\|$)/.match(params[param_name]).nil?
                        raise ECpayInvoiceRuleViolate, "[#{param_name}] contains empty value."
                    end
                    p_cnt = params[param_name].split('|').length
                    unless vat_cnt == p_cnt
                        raise ECpayInvoiceRuleViolate, %Q{Count of item info [#{param_name}] (#{p_cnt}) not match item count from [ItemCount] (#{vat_cnt})}
                    end
                end
                vat_amount_arr = params['ItemAmount'].split('|')
                vat_price_arr = params['ItemPrice'].split('|')
                vat_count_arr = params['ItemCount'].split('|')
                (1..vat_cnt).each do |index|
                    if @vat_params_list.length == 3
                        vat_tax_arr = params['ItemTaxType'].split('|')
                        if vat_tax_arr[index - 1].to_s == '1'
                            @tax_fee = 1
                        elsif vat_tax_arr[index - 1].to_s == '2' or vat_tax_arr[index - 1].to_s == '3'
                            @tax_fee = 1.05
                        else
                            raise ECpayInvoiceRuleViolate, "[ItemTaxType] can not be (#{vat_tax_arr[index - 1]}). Avaliable option: (1, 2, 3)."
                        end
                    end
                    unless vat_amount_arr[index - 1].to_i == (vat_price_arr[index - 1].to_i * vat_count_arr[index - 1].to_i / @tax_fee).round
                        raise ECpayInvoiceRuleViolate, %Q{[ItemPrice] (#{vat_price_arr[index - 1]}) times [ItemCount] (#{vat_count_arr[index - 1]}) '/' tax(#{@tax_fee}) not match [ItemAmount] (#{vat_amount_arr[index - 1].to_i})}
                    end
                    #Verify ItemAmount subtotal equal SalesAmount
                    chk_amount_subtotal = 0
                    vat_amount_arr.each do |val|
                        chk_amount_subtotal += val.to_i
                    end
                    unless params['SalesAmount'].to_i == chk_amount_subtotal
                        raise ECpayInvoiceRuleViolate, %Q{[ItemAmount] (#{vat_amount_arr}) subtotal not equal [SalesAmount] (#{params['SalesAmount'].to_i})}
                    end
                end
            end
        end

        #3. 比對商品名稱,數量,單位,價格,tax,合計,備註項目數量是否一致,欄位是否為空
        if params['ItemName'].to_s.empty? or params['ItemWord'].to_s.empty?
            raise ECpayInvoiceRuleViolate, "[ItemName] or [ItemWord] cannot be empty"
        end

        # ItemTaxType and ItemRemark會因為TaxType and ItemRemark is not empty 新增至@item_params_list
        item_params = @item_params_list
        #商品名稱含有管線 => 認為是多樣商品 *ItemName, *ItemCount ,*ItemWord, *ItemPrice, *ItemAmount, *ItemTaxType, *ItemRemark逐一用管線分割,計算數量後與第一個比對
        if params['ItemName'].include?('|')
            item_cnt = params['ItemName'].split('|').length
            item_params.each do |param_name|
                # Check if there's empty value.
                unless /(\|\||^\||\|$)/.match(params[param_name]).nil?
                    raise ECpayInvoiceRuleViolate, "[#{param_name}] contains empty value."
                end
                p_cnt = params[param_name].split('|').length
                unless item_cnt == p_cnt
                    raise ECpayInvoiceRuleViolate, %Q{Count of item info [#{param_name}] (#{p_cnt}) not match item count from [ItemCount] (#{item_cnt})}
                end
            end
            # 課稅類別[TaxType] = 9 時 => ItemTaxType 能含有1,2 3(and at least contains one 1 and other)
            if params['TaxType'].to_s == '9'
                item_tax = params['ItemTaxType'].split('|')
                p item_tax
                aval_tax_type = ['1', '2', '3']
                vio_tax_t = (item_tax - aval_tax_type)
                unless vio_tax_t == []
                    raise ECpayInvoiceRuleViolate, "Ilegal [ItemTaxType]: #{vio_tax_t}"
                end
                unless item_tax.include?('1')
                    raise ECpayInvoiceRuleViolate, "[ItemTaxType] must contain at lease one '1'."
                end
                if item_cnt >= 2
                    if !item_tax.include?('2') and !item_tax.include?('3')
                        raise ECpayInvoiceRuleViolate, "[ItemTaxType] cannot be all 1 when [TaxType] is 9."
                    end
                end
                if item_tax.include?('2') and item_tax.include?('3')
                    raise ECpayInvoiceRuleViolate, "[ItemTaxType] cannot contain 2 and 3 at the same time."
                end
            end
        else
            #沒有管線 => 逐一檢查@item_params_list的欄位有無管線
            item_params.each do |param_name|
                if params[param_name].include?('|')
                    raise "Item info [#{param_name}] contains pipeline delimiter but there's only one item in param [ItemName]"
                end
            end
        end

        #4 比對所有欄位Pattern
        self.verify_param_by_pattern(params, @all_param_pattern)

    else
        raise TypeError, "Recieved argument is not a hash"
    end
end

#verify_inv_trigger_param(params) ⇒ Object



809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
# File 'lib/ecpay_invoice/verification.rb', line 809

def verify_inv_trigger_param(params)
    if params.is_a?(Hash)
        param_diff = @inv_basic_param - params.keys
        unless param_diff == []
            raise ECpayInvalidParam, "Lack required param #{param_diff}"
        end

        #Verify Value pattern of each param
        self.verify_param_by_pattern(params, @all_param_pattern)

    else
        raise TypeError, "Recieved argument is not a hash"
    end

end