project_property_decimal_view.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * Created by Zhong on 2017/11/13.
  3. */
  4. //default setting
  5. let defaultDecimal = {
  6. min: 0,
  7. max: 6,
  8. _def: {//editable: 开放给用户编辑的
  9. bills: {editable: true, data: {unitPrice: 2, totalPrice: 2}},
  10. ration: {editable: true, data: {quantity: 4, unitPrice: 2, totalPrice: 2}},
  11. glj: {editable: true, data: {quantity: 4, unitPriceHasMix: 2, unitPrice: 3}},
  12. material: {editable: false, data: 5},
  13. feeRate: {editable: true, data: 2},
  14. quantity_detail: {editable: false, data: 4},
  15. process: {editable: false, data: 6}
  16. }
  17. };
  18. let decimalObj = Object.create(null);
  19. decimalObj.decimal = function (field, node) {
  20. if(field.sameText('unitFee')){
  21. field = 'unitPrice';
  22. }
  23. else if(field.sameText('totalFee')){
  24. field = 'totalPrice';
  25. }
  26. if(!isDef(node) && isDef(field)){
  27. let fields = field.split('.');
  28. let fieldDecimal = fields.length === 2 ? this[fields[0]][fields[1]] : this[fields[0]];
  29. return fieldDecimal ? fieldDecimal : this.process;
  30. }
  31. else {
  32. if(node.sourceType === projectObj.project.Bills.getSourceType()){
  33. if(field === 'quantity'){
  34. return billsQuanDecimal.decimal(node.data.unit);
  35. }
  36. else {
  37. return returnV(this['bills'][field], this.process);
  38. }
  39. }
  40. else if(node.sourceType === projectObj.project.Ration.getSourceType()){
  41. return returnV(this['ration'][field], this.process);
  42. }
  43. else if(node.sourceType === projectObj.project.ration_glj.getSourceType()){
  44. return returnV(this['glj'][field], this.process);
  45. }
  46. }
  47. };
  48. /*decimalObj.decimal = function (field, node) {
  49. if(isDef(field)){
  50. if(field === 'feeRate'||field==="quantity_detail"){
  51. return this[field];
  52. }
  53. else if (field.sameText('unitFee')) field = 'unitPrice'
  54. else if (field.sameText('totalFee')) field = 'totalPrice';
  55. if(isDef(node)){
  56. if(node.sourceType === projectObj.project.Bills.getSourceType()){
  57. if(field === 'quantity'){
  58. return billsQuanDecimal.decimal(node.data.unit);
  59. }
  60. else {
  61. return returnV(this['bills'][field], this.process);
  62. }
  63. }
  64. else if(node.sourceType === projectObj.project.Ration.getSourceType()){
  65. return returnV(this['ration'][field], this.process);
  66. }
  67. else if(node.sourceType === projectObj.project.ration_glj.getSourceType()){
  68. return returnV(this['glj'][field], this.process);
  69. }
  70. }
  71. }
  72. return this[field]?this[field]:this.process;
  73. };*/
  74. function returnV(v, r){
  75. if(isDef(v)){
  76. return v;
  77. }
  78. return r;
  79. }
  80. function isUndef(v) {
  81. return v === undefined || v === null;
  82. }
  83. function isDef(v){
  84. return v !== undefined && v !== null;
  85. }
  86. function isObj(v){
  87. return isDef(v) && typeof v === 'object';
  88. }
  89. function isNum(v){
  90. return isDef(v) && !isNaN(v);
  91. }
  92. function isInt(v){
  93. return isNum(v) && v % 1 === 0;
  94. }
  95. function isValidDigit(v){
  96. return isInt(v) && v >= defaultDecimal.min && v <= defaultDecimal.max;
  97. }
  98. //newV用户可编辑数据
  99. function toUpdateDecimal(orgV, newV){
  100. let rst = false;
  101. reCompare(orgV, newV);
  102. function reCompare(orgV, newV){
  103. for(let attr in newV){
  104. if(Object.keys(newV[attr]).length > 0){
  105. if(isUndef(orgV[attr])){
  106. rst = true;
  107. return;
  108. }
  109. else{
  110. reCompare(orgV[attr], newV[attr]);
  111. }
  112. }
  113. else {
  114. if(isUndef(orgV[attr]) || parseInt(newV[attr]) !== parseInt(orgV[attr])){
  115. rst = true;
  116. return;
  117. }
  118. }
  119. }
  120. }
  121. return rst;
  122. }
  123. function setDecimal(_digits, data){
  124. console.log(_digits);
  125. console.log(data)
  126. if(isDef(data)){
  127. for(let attr in data){
  128. _digits[attr] = returnV(data[attr], defaultDecimal['_def'][attr]['data']);
  129. }
  130. }
  131. else {
  132. for(let attr in defaultDecimal['_def']){
  133. _digits[attr] = defaultDecimal['_def'][attr]['data'];
  134. }
  135. }
  136. }
  137. //获取decimalPanel中要展示的数据
  138. function m_getInitData(data){
  139. let rst = Object.create(null);
  140. for(let attr in defaultDecimal['_def']){
  141. if(defaultDecimal['_def'][attr]['editable'] && isDef(data[attr])){
  142. rst[attr] = data[attr];
  143. }
  144. }
  145. return rst;
  146. }
  147. //获取小数位数panel里的数据
  148. function m_getDecimalData(inputs){
  149. let rst = Object.create(null);
  150. for(let i = 0, len = inputs.length ; i < len; i++){
  151. let name = $(inputs[i]).attr('name');
  152. let attrs = name.split('-');
  153. if(attrs.length > 1){
  154. if(isUndef(rst[attrs[0]])){
  155. rst[attrs[0]] = Object.create(null);
  156. }
  157. if(isDef(rst[attrs[0]])) {
  158. rst[attrs[0]][attrs[1]] = parseInt($(inputs[i]).val());
  159. }
  160. }
  161. else {
  162. rst[attrs[0]] = parseInt($(inputs[i]).val());
  163. }
  164. }
  165. for(let attr in defaultDecimal['_def']){
  166. if(!defaultDecimal['_def'][attr]['editable']){
  167. rst[attr] = defaultDecimal['_def'][attr]['data'];
  168. }
  169. }
  170. return rst;
  171. }
  172. function v_initPanel(data){
  173. if(this.isDef(data)){
  174. for(let attr in data){
  175. if(this.isObj(data[attr])){
  176. for(let subAttr in data[attr]){
  177. let str = attr + '-' + subAttr;
  178. let jqs = 'input[name="' + str + '"]';
  179. $(jqs).val(data[attr][subAttr]);
  180. $(jqs).attr('min', defaultDecimal.min);
  181. $(jqs).attr('max', defaultDecimal.max);
  182. }
  183. }
  184. else {
  185. let str = attr + '';
  186. let jqs = 'input[name="' + str + '"]';
  187. $(jqs).val(data[attr]);
  188. $(jqs).attr('min', defaultDecimal.min);
  189. $(jqs).attr('max', defaultDecimal.max);
  190. }
  191. }
  192. }
  193. }
  194. function e_validIn(inputs){
  195. for(let i = 0, len = inputs.length; i < len; i++){
  196. let orgV = $(inputs[i]).val();
  197. $(inputs[i]).bind('input', function () {
  198. let v = $(this).val();
  199. let inputName = $(this).attr('name');
  200. if(v.trim().length === 0 || !isValidDigit(v)){
  201. alert('小数位数范围在0-6!');
  202. $(this).val(orgV);
  203. }
  204. else{
  205. orgV = v;
  206. //清单单价、合价 与定额单价、合价同步
  207. if(inputName === 'bills-unitPrice'){
  208. $("input[name='ration-unitPrice']").val(v);
  209. }
  210. else if(inputName === 'bills-totalPrice'){
  211. $("input[name='ration-totalPrice']").val(v);
  212. }
  213. else if(inputName === 'ration-unitPrice'){
  214. $("input[name='bills-unitPrice']").val(v);
  215. }
  216. else if(inputName === 'ration-totalPrice'){
  217. $("input[name='bills-totalPrice']").val(v);
  218. }
  219. }
  220. });
  221. }
  222. }
  223. function e_bindCof(btn){
  224. btn.bind('click', function () {
  225. //获取更新的数据
  226. let updateDecimal = m_getDecimalData($('input', '#poj-settings-decimal'));
  227. if(toUpdateDecimal(decimalObj, updateDecimal)){
  228. a_updateDigits(updateDecimal);
  229. }
  230. });
  231. }
  232. function e_unbindCof(btn){
  233. btn.unbind();
  234. }
  235. function a_updateDigits(updateDecimal){
  236. let url = '/pm/api/updateProjects';
  237. let data = {
  238. updateType: 'update',
  239. updateData: {
  240. ID: parseInt(scUrlUtil.GetQueryString('project')),
  241. 'property.decimal': updateDecimal
  242. }
  243. };
  244. let postData = {
  245. user_id: userID,
  246. updateData: [data]
  247. };
  248. let scCaller = function (resultData) {
  249. //update data
  250. setDecimal(decimalObj, updateDecimal);
  251. let v_data = m_getInitData(decimalObj);
  252. v_initPanel(v_data);
  253. };
  254. let errCaller = function () {
  255. alert('更新小数位数失败!');
  256. let v_data = m_getInitData(decimalObj);
  257. v_initPanel(v_data);
  258. };
  259. CommonAjax.post(url, postData, scCaller, errCaller);
  260. }
  261. $(document).ready(function () {
  262. $('#poj-set').on('shown.bs.modal', function (e) {
  263. let v_data = m_getInitData(decimalObj);
  264. v_initPanel(v_data);
  265. });
  266. //绑定确定按钮
  267. //e_bindCof($('#property_ok'));
  268. //绑定小数位数输入控制
  269. e_validIn($('input', '#poj-settings-decimal'));
  270. });