<commit_before>angular.module("admin.lineItems").controller 'LineItemsCtrl', ($scope, $timeout, $http, $q, StatusMessage, Columns, Dereferencer, Orders, LineItems, Enterprises, OrderCycles, blankOption, VariantUnitManager, RequestMonitor) ->
  $scope.initialized = false
  $scope.RequestMonitor = RequestMonitor
  $scope.filteredLineItems = []
  $scope.confirmDelete = true
  $scope.startDate = formatDate daysFromToday -7
  $scope.endDate = formatDate daysFromToday 1
  $scope.bulkActions = [ { name: t("bom_actions_delete"), callback: 'deleteLineItems' } ]
  $scope.selectedUnitsProduct = {};
  $scope.selectedUnitsVariant = {};
  $scope.sharedResource = false
  $scope.columns = Columns.setColumns
    order_no:     { name: t("bom_no"), visible: false }
    full_name:    { name: t("name"),         visible: true }
    email:        { name: t("email"),     visible: false }
    phone:        { name: t("phone"),        visible: false }
    order_date:   { name: t("bom_date"),   visible: true }
    producer:     { name: t("producer"),     visible: true }
    order_cycle:  { name: t("bom_cycle"),  visible: false }
    hub:          { name: t("bom_hub"),          visible: false }
    variant:      { name: t("bom_variant"),      visible: true }
    quantity:     { name: t("bom_quantity"),     visible: true }
    max:          { name: t("bom_max"),          visible: true }
    final_weight_volume:   { name: t("bom_final_weigth_volume"), visible: false }
    price:        { name: t("bom_price"),        visible: false }

  $scope.confirmRefresh = ->
    LineItems.allSaved() || confirm(t("unsaved_changes_warning"))

  $scope.resetSelectFilters = ->
    $scope.distributorFilter = blankOption().id
    $scope.supplierFilter = blankOption().id
    $scope.orderCycleFilter = blankOption().id
    $scope.quickSearch = ""

  $scope.refreshData = ->
    unless !$scope.orderCycleFilter? || $scope.orderCycleFilter == "0"
      $scope.startDate = OrderCycles.orderCyclesByID[$scope.orderCycleFilter].first_order
      $scope.endDate = OrderCycles.orderCyclesByID[$scope.orderCycleFilter].last_order

    RequestMonitor.load $scope.orders = Orders.index("q[state_not_eq]": "canceled", "q[completed_at_not_null]": "true", "q[completed_at_gt]": "#{parseDate($scope.startDate)}", "q[completed_at_lt]": "#{parseDate($scope.endDate)}")
    RequestMonitor.load $scope.lineItems = LineItems.index("q[order][state_not_eq]": "canceled", "q[order][completed_at_not_null]": "true", "q[order][completed_at_gt]": "#{parseDate($scope.startDate)}", "q[order][completed_at_lt]": "#{parseDate($scope.endDate)}")

    unless $scope.initialized
      RequestMonitor.load $scope.distributors = Enterprises.index(includeBlank: true, action: "for_line_items", ams_prefix: "basic", "q[sells_in][]": ["own", "any"])
      RequestMonitor.load $scope.orderCycles = OrderCycles.index(includeBlank: true, ams_prefix: "basic", as: "distributor", "q[orders_close_at_gt]": "#{daysFromToday(-90)}")
      RequestMonitor.load $scope.suppliers = Enterprises.index(includeBlank: true, action: "for_line_items", ams_prefix: "basic", "q[is_primary_producer_eq]": "true")

    RequestMonitor.load $q.all([$scope.orders.$promise, $scope.distributors.$promise, $scope.orderCycles.$promise]).then ->
      Dereferencer.dereferenceAttr $scope.orders, "distributor", Enterprises.enterprisesByID
      Dereferencer.dereferenceAttr $scope.orders, "order_cycle", OrderCycles.orderCyclesByID

    RequestMonitor.load $q.all([$scope.orders.$promise, $scope.suppliers.$promise, $scope.lineItems.$promise]).then ->
      Dereferencer.dereferenceAttr $scope.lineItems, "supplier", Enterprises.enterprisesByID
      Dereferencer.dereferenceAttr $scope.lineItems, "order", Orders.ordersByID
      $scope.bulk_order_form.$setPristine()
      StatusMessage.clear()
      unless $scope.initialized
        $scope.initialized = true
        $timeout ->
          $scope.resetSelectFilters()

  $scope.refreshData()

  $scope.$watch 'bulk_order_form.$dirty', (newVal, oldVal) ->
    if newVal == true
      StatusMessage.display 'notice', "You have unsaved changes"

  $scope.submit = ->
    if $scope.bulk_order_form.$valid
      StatusMessage.display 'progress', "Saving..."
      $q.all(LineItems.saveAll()).then(->
        StatusMessage.display 'success', "All changes saved"
        $scope.bulk_order_form.$setPristine()
      ).catch ->
        StatusMessage.display 'failure', {t("unsaved_changes_warning")}
    else
      StatusMessage.display 'failure', {t("unsaved_changes_warning")}

  $scope.deleteLineItem = (lineItem) ->
    if ($scope.confirmDelete && confirm(t("are_you_sure"))) || !$scope.confirmDelete
      LineItems.delete lineItem, =>
        $scope.lineItems.splice $scope.lineItems.indexOf(lineItem), 1

  $scope.deleteLineItems = (lineItems) ->
    existingState = $scope.confirmDelete
    $scope.confirmDelete = false
    $scope.deleteLineItem lineItem for lineItem in lineItems when lineItem.checked
    $scope.confirmDelete = existingState

  $scope.allBoxesChecked = ->
    checkedCount = $scope.filteredLineItems.reduce (count,lineItem) ->
      count + (if lineItem.checked then 1 else 0 )
    , 0
    checkedCount == $scope.filteredLineItems.length
<commit_msg>new validation2 02/22/2016<commit_after>angular.module("admin.lineItems").controller 'LineItemsCtrl', ($scope, $timeout, $http, $q, StatusMessage, Columns, Dereferencer, Orders, LineItems, Enterprises, OrderCycles, blankOption, VariantUnitManager, RequestMonitor) ->
  $scope.initialized = false
  $scope.RequestMonitor = RequestMonitor
  $scope.filteredLineItems = []
  $scope.confirmDelete = true
  $scope.startDate = formatDate daysFromToday -7
  $scope.endDate = formatDate daysFromToday 1
  $scope.bulkActions = [ { name: t "bom_actions_delete", callback: 'deleteLineItems' } ]
  $scope.selectedUnitsProduct = {};
  $scope.selectedUnitsVariant = {};
  $scope.sharedResource = false
  $scope.columns = Columns.setColumns
    order_no:     { name: t "bom_no", visible: false }
    full_name:    { name: t "name",         visible: true }
    email:        { name: t "email",     visible: false }
    phone:        { name: t "phone",        visible: false }
    order_date:   { name: t "bom_date",   visible: true }
    producer:     { name: t "producer",     visible: true }
    order_cycle:  { name: t "bom_cycle",  visible: false }
    hub:          { name: t "bom_hub",          visible: false }
    variant:      { name: t "bom_variant",      visible: true }
    quantity:     { name: t "bom_quantity",     visible: true }
    max:          { name: t "bom_max",          visible: true }
    final_weight_volume:   { name: t "bom_final_weigth_volume", visible: false }
    price:        { name: t "bom_price",        visible: false }

  $scope.confirmRefresh = ->
    LineItems.allSaved() || confirm(t "unsaved_changes_warning")

  $scope.resetSelectFilters = ->
    $scope.distributorFilter = blankOption().id
    $scope.supplierFilter = blankOption().id
    $scope.orderCycleFilter = blankOption().id
    $scope.quickSearch = ""

  $scope.refreshData = ->
    unless !$scope.orderCycleFilter? || $scope.orderCycleFilter == "0"
      $scope.startDate = OrderCycles.orderCyclesByID[$scope.orderCycleFilter].first_order
      $scope.endDate = OrderCycles.orderCyclesByID[$scope.orderCycleFilter].last_order

    RequestMonitor.load $scope.orders = Orders.index("q[state_not_eq]": "canceled", "q[completed_at_not_null]": "true", "q[completed_at_gt]": "#{parseDate($scope.startDate)}", "q[completed_at_lt]": "#{parseDate($scope.endDate)}")
    RequestMonitor.load $scope.lineItems = LineItems.index("q[order][state_not_eq]": "canceled", "q[order][completed_at_not_null]": "true", "q[order][completed_at_gt]": "#{parseDate($scope.startDate)}", "q[order][completed_at_lt]": "#{parseDate($scope.endDate)}")

    unless $scope.initialized
      RequestMonitor.load $scope.distributors = Enterprises.index(includeBlank: true, action: "for_line_items", ams_prefix: "basic", "q[sells_in][]": ["own", "any"])
      RequestMonitor.load $scope.orderCycles = OrderCycles.index(includeBlank: true, ams_prefix: "basic", as: "distributor", "q[orders_close_at_gt]": "#{daysFromToday(-90)}")
      RequestMonitor.load $scope.suppliers = Enterprises.index(includeBlank: true, action: "for_line_items", ams_prefix: "basic", "q[is_primary_producer_eq]": "true")

    RequestMonitor.load $q.all([$scope.orders.$promise, $scope.distributors.$promise, $scope.orderCycles.$promise]).then ->
      Dereferencer.dereferenceAttr $scope.orders, "distributor", Enterprises.enterprisesByID
      Dereferencer.dereferenceAttr $scope.orders, "order_cycle", OrderCycles.orderCyclesByID

    RequestMonitor.load $q.all([$scope.orders.$promise, $scope.suppliers.$promise, $scope.lineItems.$promise]).then ->
      Dereferencer.dereferenceAttr $scope.lineItems, "supplier", Enterprises.enterprisesByID
      Dereferencer.dereferenceAttr $scope.lineItems, "order", Orders.ordersByID
      $scope.bulk_order_form.$setPristine()
      StatusMessage.clear()
      unless $scope.initialized
        $scope.initialized = true
        $timeout ->
          $scope.resetSelectFilters()

  $scope.refreshData()

  $scope.$watch 'bulk_order_form.$dirty', (newVal, oldVal) ->
    if newVal == true
      StatusMessage.display 'notice', "You have unsaved changes"

  $scope.submit = ->
    if $scope.bulk_order_form.$valid
      StatusMessage.display 'progress', "Saving..."
      $q.all(LineItems.saveAll()).then(->
        StatusMessage.display 'success', "All changes saved"
        $scope.bulk_order_form.$setPristine()
      ).catch ->
        StatusMessage.display 'failure', {t "unsaved_changes_warning"}
    else
      StatusMessage.display 'failure', {t "unsaved_changes_warning"}

  $scope.deleteLineItem = (lineItem) ->
    if ($scope.confirmDelete && confirm(t "are_you_sure")) || !$scope.confirmDelete
      LineItems.delete lineItem, =>
        $scope.lineItems.splice $scope.lineItems.indexOf(lineItem), 1

  $scope.deleteLineItems = (lineItems) ->
    existingState = $scope.confirmDelete
    $scope.confirmDelete = false
    $scope.deleteLineItem lineItem for lineItem in lineItems when lineItem.checked
    $scope.confirmDelete = existingState

  $scope.allBoxesChecked = ->
    checkedCount = $scope.filteredLineItems.reduce (count,lineItem) ->
      count + (if lineItem.checked then 1 else 0 )
    , 0
    checkedCount == $scope.filteredLineItems.length

{'fraction_non_alphanumeric': 0.14371715241280458, 'fraction_numerical': 0.0025800286669851887, 'mean_word_length': 4.078117418728772, 'pattern_counts': {'":': 22, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4157729', 'n_tokens_mistral': 3387, 'n_tokens_neox': 3284, 'n_words': 596}