project_property_decimal_view.js 8.8 KB

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