U8ServerService.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. <?php
  2. namespace App\Service;
  3. use App\Model\DDEmployee;
  4. use App\Model\FieldData;
  5. use App\Model\Inventory;
  6. use App\Model\Record;
  7. use App\Model\U8State;
  8. use App\Model\Vendor;
  9. use Illuminate\Support\Facades\DB;
  10. class U8ServerService extends Service
  11. {
  12. protected $databaseService;
  13. /**
  14. * @var string|null
  15. */
  16. protected $error;
  17. /**
  18. * U8ServerService constructor.
  19. */
  20. public function __construct($loginUser = [])
  21. {
  22. $service = new U8DatabaseServerService($loginUser);
  23. $this->databaseService = $service->db;
  24. $this->error = $service->error;
  25. }
  26. /**
  27. * 获取错误信息
  28. *
  29. * @return string|null
  30. */
  31. public function getError()
  32. {
  33. return $this->error;
  34. }
  35. public function purchaseOrder($data, $user){
  36. $qx = $user['qx'];
  37. $order_date = $data['order_date'] ?? [];
  38. $order_date = array_filter($order_date);
  39. $order_number = $data['order_number'] ?? "";
  40. $model = $this->databaseService->table('PO_Pomain as a')
  41. ->leftJoin('Vendor as c', 'c.cVenCode', 'a.cVenCode') // 供应商
  42. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode') // 部门
  43. ->leftJoin('Person as e', 'e.cPersonCode', 'a.cPersonCode')// 业务员
  44. ->leftJoin('PurchaseType as f', 'f.cPTCode', 'a.cPTCode') // 采购类型
  45. ->when(empty($qx), function ($query) use($user){
  46. return $query->where('a.cMaker', $user['username']);
  47. })
  48. ->when(!empty($order_number), function ($query) use($order_number){
  49. return $query->where('a.cPOID', 'LIKE', '%'.$order_number.'%');
  50. })
  51. ->when(!empty($order_date), function ($query) use($order_date){
  52. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  53. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  54. return $query->whereBetween('a.dPODate', [$start, $end]);
  55. })
  56. ->where(function ($query) {
  57. $query->where('a.iverifystateex', 0)
  58. ->orWhereNull('a.iverifystateex');
  59. })
  60. ->select(
  61. DB::raw("ISNULL(a.cBusType, '') as business_type"), // 业务类型
  62. DB::raw("ISNULL(CONVERT(varchar(10), a.dPODate, 120), '') as order_date"), // 订单日期
  63. DB::raw("ISNULL(a.cPOID, '') as order_number"), // 订单编号
  64. DB::raw("ISNULL(f.cPTName, '') as purchase_type"), // 采购类型
  65. DB::raw("ISNULL(c.cVenName, '') as supplier_title"), // 供应商
  66. DB::raw("ISNULL(d.cDepName, '') as department_name"), // 部门
  67. DB::raw("ISNULL(e.cPersonName, '') as person_name"), // 业务员
  68. DB::raw("CAST(ISNULL(a.iTaxRate, 0) AS varchar) as tax_rate"), // 税率(数值转字符,null转0或空)
  69. DB::raw("ISNULL(a.cexch_name, '') as currency_name"), // 币种
  70. DB::raw("CAST(ISNULL(a.nflat, 0) AS varchar) as exchange_rate"), // 汇率
  71. DB::raw("ISNULL(a.cMemo, '') as remark"), // 备注
  72. DB::raw("ISNULL(a.cMaker, '') as crt_name") // 制单人
  73. )
  74. ->orderBy('a.POID', 'desc');
  75. $list = $this->limit($model, '', $data);
  76. $list = $this->fillAll($list, U8State::type_one, $user['login_type']);
  77. return [true, $list];
  78. }
  79. public function purchaseOrderDetail($data, $user){
  80. if(empty($data['order_number'])) return [false, '采购单号不能为空'];
  81. $order = $this->databaseService->table('PO_Pomain as a')
  82. ->leftJoin('Vendor as c', 'c.cVenCode', 'a.cVenCode')
  83. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode')
  84. ->leftJoin('Person as e', 'e.cPersonCode', 'a.cPersonCode')
  85. ->leftJoin('PurchaseType as f', 'f.cPTCode', 'a.cPTCode')
  86. ->where('a.cPOID', $data['order_number'])
  87. ->select(
  88. 'a.POID',
  89. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  90. DB::raw("ISNULL(CONVERT(varchar(10), a.dPODate, 120), '') as order_date"),
  91. DB::raw("ISNULL(a.cPOID, '') as order_number"),
  92. DB::raw("ISNULL(f.cPTName, '') as purchase_type"),
  93. DB::raw("ISNULL(c.cVenName, '') as supplier_title"),
  94. DB::raw("ISNULL(d.cDepName, '') as department_name"),
  95. DB::raw("ISNULL(e.cPersonName, '') as person_name"),
  96. DB::raw("CAST(ISNULL(a.iTaxRate, 0) AS varchar) as tax_rate"),
  97. DB::raw("ISNULL(a.cexch_name, '') as currency_name"),
  98. DB::raw("CAST(ISNULL(a.nflat, 0) AS varchar) as exchange_rate"),
  99. DB::raw("ISNULL(a.cMemo, '') as remark"),
  100. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  101. )
  102. ->first();
  103. if(empty($order)) return [false, '采购单不存在'];
  104. $order = (array) $order;
  105. $order = $this->fillDetail($order, U8State::type_one, $user['login_type']);
  106. // 获取明细
  107. $detail = $this->databaseService->table('PO_Podetails as a')
  108. ->leftJoin('Inventory as b', 'b.cInvCode', 'a.cInvCode')
  109. ->leftJoin('ComputationUnit as c', 'c.cComunitCode', 'b.cComUnitCode')
  110. ->where('a.POID', $order['POID'])
  111. ->select(
  112. DB::raw("ISNULL(b.cInvCode, '') as product_code"),
  113. DB::raw("ISNULL(b.cInvName, '') as product_title"),
  114. DB::raw("ISNULL(b.cInvStd, '') as product_std"),
  115. DB::raw("ISNULL(c.cComUnitName, '') as unit_title"),
  116. // 数字转字符串并去除空格,NULL 则返回 '0.00' 或 ''
  117. DB::raw("ISNULL(LTRIM(STR(a.iQuantity, 20, 2)), '0.00') as quantity"),
  118. DB::raw("ISNULL(LTRIM(STR(a.iTaxPrice, 20, 4)), '0.0000') as tax_unit_price"),
  119. DB::raw("ISNULL(LTRIM(STR(a.iUnitPrice, 20, 4)), '0.0000') as unit_price"),
  120. DB::raw("ISNULL(LTRIM(STR(a.iMoney, 20, 2)), '0.00') as amount"),
  121. DB::raw("ISNULL(LTRIM(STR(a.iSum, 20, 2)), '0.00') as tax_amount"),
  122. DB::raw("ISNULL(LTRIM(STR(a.iPerTaxRate, 20, 2)), '0.00') as tax_rate"),
  123. DB::raw("ISNULL(CONVERT(varchar(10), a.dArriveDate, 120), '') as arrive_date"),
  124. DB::raw("ISNULL(b.cEnterprise, '') as factory_name")
  125. )
  126. ->get();
  127. // 转为数组
  128. $order['detail'] = $detail->map(function ($item) {
  129. return (array) $item;
  130. })->toArray();
  131. // 移除内部 ID 避免暴露
  132. unset($order['POID']);
  133. return [true, $order];
  134. }
  135. public function purchaseRequisition($data, $user){
  136. $qx = $user['qx'];
  137. $order_date = $data['order_date'] ?? [];
  138. $order_date = array_filter($order_date);
  139. $order_number = $data['order_number'] ?? "";
  140. $model = $this->databaseService->table('PU_AppVouch as a')
  141. ->leftJoin('Person as c', 'c.cPersonCode', 'a.cPersonCode') // 请购人
  142. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode') // 请购部门
  143. ->leftJoin('PurchaseType as f', 'f.cPTCode', 'a.cPTCode') // 采购类型
  144. ->when(empty($qx), function ($query) use($user){
  145. return $query->where('a.cMaker',$user['username']);
  146. })
  147. ->when(! empty($order_number), function ($query) use($order_number){
  148. return $query->where('a.cCode','LIKE', '%'.$order_number.'%');
  149. })
  150. ->when(! empty($order_date), function ($query) use($order_date){
  151. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  152. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  153. return $query->whereBetween('a.dDate', [$start, $end]);
  154. })
  155. ->where(function ($query) {
  156. $query->where('a.iverifystateex', 0)
  157. ->orWhereNull('a.iverifystateex');
  158. })
  159. ->select(
  160. DB::raw("ISNULL(a.cBusType, '') as business_type"), // 业务类型
  161. DB::raw("ISNULL(a.cCode, '') as order_number"), // 单据号
  162. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"), // 日期
  163. DB::raw("ISNULL(d.cDepName, '') as department_name"), // 请购部门
  164. DB::raw("ISNULL(c.cPersonName, '') as purchase_name"), // 请购人员
  165. DB::raw("ISNULL(f.cPTName, '') as purchase_type"), // 采购类型
  166. DB::raw("ISNULL(a.cMaker, '') as crt_name") // 制单人
  167. )
  168. ->orderBy('a.ID','desc');
  169. $list = $this->limit($model,'',$data);
  170. $list = $this->fillAll($list, U8State::type_two, $user['login_type']);
  171. return [true , $list];
  172. }
  173. public function purchaseRequisitionDetail($data, $user){
  174. if(empty($data['order_number'])) return [false, '采购请购单号不能为空'];
  175. $order = $this->databaseService->table('PU_AppVouch as a')
  176. ->leftJoin('Person as c', 'c.cPersonCode', 'a.cPersonCode')
  177. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode')
  178. ->leftJoin('PurchaseType as f', 'f.cPTCode', 'a.cPTCode')
  179. ->where('a.cCode', $data['order_number'])
  180. ->select(
  181. 'a.ID', // 用于关联子表
  182. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  183. DB::raw("ISNULL(a.cCode, '') as order_number"),
  184. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"),
  185. DB::raw("ISNULL(d.cDepName, '') as department_name"),
  186. DB::raw("ISNULL(c.cPersonName, '') as purchase_name"),
  187. DB::raw("ISNULL(f.cPTName, '') as purchase_type"),
  188. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  189. )
  190. ->first();
  191. if(empty($order)) return [false, '采购请购单不存在'];
  192. $order = (array) $order;
  193. $order = $this->fillDetail($order, U8State::type_two, $user['login_type']);
  194. $detail = $this->databaseService->table('PU_AppVouchs as a')
  195. ->leftJoin('Inventory as b', 'b.cInvCode', 'a.cInvCode')
  196. ->leftJoin('ComputationUnit as c', 'c.cComunitCode', 'b.cComUnitCode')
  197. ->where('a.ID', $order['ID'])
  198. ->select(
  199. DB::raw("ISNULL(b.cInvCode, '') as product_code"), // 存货编码
  200. DB::raw("ISNULL(b.cInvName, '') as product_title"), // 存货名称
  201. DB::raw("ISNULL(b.cInvStd, '') as product_std"), // 规格型号
  202. DB::raw("ISNULL(c.cComUnitName, '') as unit_title"), // 主计量
  203. DB::raw("ISNULL(LTRIM(STR(a.fQuantity, 20, 2)), '0.00') as quantity"), // 数量
  204. DB::raw("ISNULL(LTRIM(STR(a.fUnitPrice, 20, 4)), '0.0000') as unit_price"), // 本币单价
  205. DB::raw("ISNULL(LTRIM(STR(a.iOriSum, 20, 2)), '0.00') as tax_amount"), // 本币价税合计
  206. DB::raw("ISNULL(CONVERT(varchar(10), a.dRequirDate, 120), '') as need_arrived_date"), // 需求日期
  207. DB::raw("ISNULL(CONVERT(varchar(10), a.dArriveDate, 120), '') as suggest_order_date"), // 建议订货日期
  208. DB::raw("ISNULL(b.cEnterprise, '') as factory_name") // 生产企业
  209. )
  210. ->get();
  211. // 转为数组格式
  212. $order['detail'] = $detail->map(function ($item) {
  213. return (array) $item;
  214. })->toArray();
  215. unset($order['ID']); // 隐藏内部ID
  216. return [true, $order];
  217. }
  218. public function purchaseInOrder($data, $user){
  219. $qx = $user['qx'];
  220. $order_date = $data['order_date'] ?? [];
  221. $order_date = array_filter($order_date);
  222. $order_number = $data['order_number'] ?? "";
  223. $model = $this->databaseService->table('RdRecord01 as a')
  224. ->leftJoin('Vendor as c', 'c.cVenCode', 'a.cVenCode') // 供货单位
  225. ->leftJoin('Warehouse as w', 'w.cWhCode', 'a.cWhCode') // 仓库
  226. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode') // 部门
  227. ->leftJoin('Person as p', 'p.cPersonCode', 'a.cPersonCode')// 业务员
  228. ->leftJoin('PurchaseType as pt', 'pt.cPTCode', 'a.cPTCode')// 采购类型
  229. ->leftJoin('Rd_Style as rs', 'rs.cRdCode', 'a.cRdCode') // 入库类别
  230. ->when(empty($qx), function ($query) use($user){
  231. return $query->where('a.cMaker',$user['username']);
  232. })
  233. ->when(! empty($order_number), function ($query) use($order_number){
  234. return $query->where('a.cCode','LIKE', '%'.$order_number.'%');
  235. })
  236. ->when(! empty($order_date), function ($query) use($order_date){
  237. $start = date('Y-m-d 00:00:00.000', $order_date[0] / 1000);
  238. $end = date('Y-m-d 23:59:59.000', $order_date[1] / 1000);
  239. return $query->whereBetween('a.dDate', [$start, $end]);
  240. })
  241. ->whereNull('a.cHandler') // 未审核
  242. ->select(
  243. DB::raw("ISNULL(a.cCode, '') as order_number"), // 入库单号
  244. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"), // 入库日期
  245. DB::raw("ISNULL(w.cWhName, '') as warehouse_name"), // 仓库
  246. DB::raw("ISNULL(a.cOrderCode, '') as po_number"), // 订单号
  247. DB::raw("ISNULL(a.cARVCode, '') as arrival_number"), // 到货单号
  248. DB::raw("ISNULL(p.cPersonName, '') as person_name"), // 业务员
  249. DB::raw("ISNULL(c.cVenName, '') as supplier_title"), // 供货单位
  250. DB::raw("ISNULL(d.cDepName, '') as department_name"), // 部门
  251. DB::raw("ISNULL(CONVERT(varchar(10), a.dARVDate, 120), '') as arrival_date"), // 到货日期
  252. DB::raw("ISNULL(a.cBusType, '') as business_type"), // 业务类型
  253. DB::raw("ISNULL(pt.cPTName, '') as purchase_type"), // 采购类型
  254. DB::raw("ISNULL(rs.cRdName, '') as rd_style_name"), // 入库类别
  255. DB::raw("ISNULL(CONVERT(varchar(10), a.dVeriDate, 120), '') as audit_date"), // 审核日期
  256. DB::raw("ISNULL(a.cMemo, '') as remark"), // 备注
  257. DB::raw("ISNULL(a.cMaker, '') as crt_name") // 制单人
  258. )
  259. ->orderBy('a.ID','desc');
  260. $list = $this->limit($model,'',$data);
  261. $list = $this->fillAll($list, U8State::type_three, $user['login_type']);
  262. return [true , $list];
  263. }
  264. public function purchaseInOrderDetail($data, $user){
  265. if(empty($data['order_number'])) return [false, '入库单号不能为空'];
  266. $order = $this->databaseService->table('RdRecord01 as a')
  267. ->leftJoin('Vendor as c', 'c.cVenCode', 'a.cVenCode')
  268. ->leftJoin('Warehouse as w', 'w.cWhCode', 'a.cWhCode')
  269. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode')
  270. ->leftJoin('Person as p', 'p.cPersonCode', 'a.cPersonCode')
  271. ->leftJoin('PurchaseType as pt', 'pt.cPTCode', 'a.cPTCode')
  272. ->leftJoin('Rd_Style as rs', 'rs.cRdCode', 'a.cRdCode')
  273. ->where('a.cCode', $data['order_number'])
  274. ->select(
  275. 'a.ID',
  276. DB::raw("ISNULL(a.cCode, '') as order_number"),
  277. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"),
  278. DB::raw("ISNULL(w.cWhName, '') as warehouse_name"),
  279. DB::raw("ISNULL(a.cOrderCode, '') as po_number"),
  280. DB::raw("ISNULL(a.cARVCode, '') as arrival_number"),
  281. DB::raw("ISNULL(p.cPersonName, '') as person_name"),
  282. DB::raw("ISNULL(c.cVenName, '') as supplier_title"),
  283. DB::raw("ISNULL(d.cDepName, '') as department_name"),
  284. DB::raw("ISNULL(CONVERT(varchar(10), a.darvdate, 120), '') as arrival_date"),
  285. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  286. DB::raw("ISNULL(pt.cPTName, '') as purchase_type"),
  287. DB::raw("ISNULL(rs.cRdName, '') as rd_style_name"),
  288. DB::raw("ISNULL(CONVERT(varchar(10), a.dVeriDate, 120), '') as audit_date"),
  289. DB::raw("ISNULL(a.cMemo, '') as remark"),
  290. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  291. )
  292. ->first();
  293. if(empty($order)) return [false, '采购入库单不存在'];
  294. $order = (array) $order;
  295. $order = $this->fillDetail($order, U8State::type_three, $user['login_type']);
  296. $detail = $this->databaseService->table('rdrecords01 as a')
  297. ->leftJoin('Inventory as b', 'b.cInvCode', 'a.cInvCode')
  298. ->leftJoin('ComputationUnit as c', 'c.cComunitCode', 'b.cComUnitCode')
  299. ->where('a.ID', $order['ID'])
  300. ->select(
  301. DB::raw("ISNULL(b.cInvCode, '') as product_code"), // 存货编码
  302. DB::raw("ISNULL(b.cInvName, '') as product_title"), // 存货名称
  303. DB::raw("ISNULL(b.cInvStd, '') as product_std"), // 规格型号
  304. DB::raw("ISNULL(c.cComUnitName, '') as unit_title"), // 主计量单位
  305. DB::raw("ISNULL(LTRIM(STR(a.iQuantity, 20, 2)), '0.00') as quantity"), // 数量
  306. DB::raw("ISNULL(LTRIM(STR(a.iUnitCost, 20, 4)), '0.0000') as unit_price"), // 本币单价
  307. DB::raw("ISNULL(LTRIM(STR(a.iPrice, 20, 2)), '0.00') as amount"), // 本币金额
  308. DB::raw("ISNULL(LTRIM(STR(a.iTax, 20, 2)), '0.00') as tax"), // 税额
  309. DB::raw("ISNULL(LTRIM(STR(a.iTaxPrice, 20, 2)), '0.00') as nat_tax"), // 本币税额 (入库单本币税额通常等于原币税额)
  310. DB::raw("ISNULL(LTRIM(STR(a.iSum, 20, 2)), '0.00') as tax_amount") // 本币价税合计
  311. )
  312. ->get();
  313. $detailArr = $detail->map(function ($item) {
  314. return (array) $item;
  315. })->toArray();
  316. // 计算总金额(价税合计之和)
  317. $order['total_amount'] = number_format(array_sum(array_column($detailArr, 'tax_amount')), 2, '.', '');
  318. $order['detail'] = $detailArr;
  319. unset($order['ID']);
  320. return [true, $order];
  321. }
  322. public function inventoryDetail($data, $user){
  323. if(empty($data['order_number'])) return [false, '流水单号不能为空'];
  324. $order = Inventory::where('del_time',0)
  325. ->where('order_number', $data['order_number'])
  326. ->first();
  327. if(empty($order)) return [false, '存货不存在'];
  328. $order = (array) $order;
  329. return [true, $order];
  330. }
  331. public function vendorDetail($data, $user){
  332. if(empty($data['order_number'])) return [false, '流水单号不能为空'];
  333. $order = Vendor::where('del_time',0)
  334. ->where('order_number', $data['order_number'])
  335. ->first();
  336. if(empty($order)) return [false, '供应商不存在'];
  337. $order = (array) $order;
  338. return [true, $order];
  339. }
  340. public function getOrderDetails($data,$user){
  341. $type = $data['type'];
  342. if($type == 1){
  343. // 采购单
  344. [$success, $order] = $this->purchaseOrderDetail($data,$user);
  345. }elseif ($type == 2){
  346. // 采购请购单
  347. [$success, $order] = $this->purchaseRequisitionDetail($data,$user);
  348. }elseif ($type == 3){
  349. // 采购入库
  350. [$success, $order] = $this->purchaseInOrderDetail($data,$user);
  351. }elseif ($type == 4){
  352. // 存货
  353. [$success, $order] = $this->inventoryDetail($data,$user);
  354. }elseif ($type == 5){
  355. // 供应商
  356. [$success, $order] = $this->vendorDetail($data,$user);
  357. }
  358. return [$success, $order];
  359. }
  360. private function fillAll($list, $type, $login_type){
  361. if(empty($list['data'])) return $list;
  362. $map = U8State::where('del_time', 0)
  363. ->where('type', $type)
  364. ->where('login_type', $login_type)
  365. ->whereIn('order_number', array_column($list['data'], 'order_number'))
  366. ->pluck('state', 'order_number')
  367. ->toArray();
  368. foreach ($list['data'] as $key => $value){
  369. if(isset($map[$value->order_number])) {
  370. $m = $map[$value->order_number];
  371. $state = $m;
  372. $state_title = Record::state_name[$state];
  373. }else{
  374. $state = Record::state_minus_one;
  375. $state_title = Record::state_name[$state];
  376. }
  377. $list['data'][$key]->state = $state;
  378. $list['data'][$key]->state_title = $state_title;
  379. }
  380. return $list;
  381. }
  382. private function fillDetail($list, $type, $login_type){
  383. if(empty($list)) return $list;
  384. $map = U8State::where('del_time', 0)
  385. ->where('type', $type)
  386. ->where('login_type', $login_type)
  387. ->where('order_number', $list['order_number'])
  388. ->pluck('state', 'order_number')
  389. ->toArray();
  390. if(isset($map[$list['order_number']])) {
  391. $m = $map[$list['order_number']];
  392. $state = $m;
  393. $state_title = Record::state_name[$state];
  394. }else{
  395. $state = Record::state_minus_one;
  396. $state_title = Record::state_name[$state];
  397. }
  398. $list['state'] = $state;
  399. $list['state_title'] = $state_title;
  400. return $list;
  401. }
  402. public function stockList($data, $user)
  403. {
  404. try {
  405. $field_list = [];
  406. $employee = DDEmployee::where('userid', $user['userid'])->where('login_type', $user['login_type'])->first();
  407. $qx = $employee['qx'] ?? 0;
  408. if(empty($qx)) {
  409. $field_list = FieldData::where('userid', $user['userid'])
  410. ->where('login_type', $user['login_type'])
  411. ->where('type', FieldData::STATE_ZERO)
  412. ->pluck('key')
  413. ->all();
  414. }
  415. // 1. 构建基础查询:关联现存量表和存货档案表
  416. $query = $this->databaseService->table('CurrentStock as S')
  417. ->select([
  418. 'S.cWhCode', // 仓库编码
  419. 'W.cWhName', // 仓库名称
  420. 'S.cInvCode', // 存货编码
  421. 'I.cInvName', // 存货名称
  422. 'I.cInvStd', // 规格型号
  423. 'I.cInvCCode', // 分类编码
  424. // --- 数量字段对齐你的结构 ---
  425. 'S.iQuantity', // 结存数量 (账面现存量)
  426. 'S.fAvaQuantity', // 可用数量
  427. 'S.fOutQuantity', // 待发货数量 (待出)
  428. 'S.fInQuantity', // 待入库数量 (待入)
  429. 'S.fStopQuantity', // 冻结数量
  430. // --- 批次与日期 ---
  431. 'S.cBatch', // 批号
  432. 'S.dMdate', // 生产日期
  433. 'S.dVDate', // 失效日期
  434. ])
  435. ->join('Inventory as I', 'S.cInvCode', '=', 'I.cInvCode')
  436. ->leftJoin('Warehouse as W', 'S.cWhCode', '=', 'W.cWhCode')
  437. ->leftJoin('InventoryClass as IC', 'I.cInvCCode', '=', 'IC.cInvCCode');
  438. // 2. 过滤条件:存货名称 (支持模糊查询)
  439. if (!empty($data['material_title'])) {
  440. $query->where('I.cInvName', 'like', '%' . $data['material_title'] . '%');
  441. }
  442. // 2. 过滤条件:存货编码 (支持模糊查询)
  443. if (!empty($data['material_code'])) {
  444. $query->where('S.cInvCode', 'like', '%' . $data['material_code'] . '%');
  445. }
  446. // 3. 过滤条件:存货分类 (支持左匹配,即选大类查出所有子类)
  447. if (!empty($data['category_code'])) {
  448. // U8 分类是级次结构,用 like '01%' 可以查出 01 开头的所有子类
  449. $query->where('I.cInvCCode', 'like', $data['category_code'] . '%');
  450. }
  451. // 6. 排序
  452. $query->orderBy('S.cInvCode', 'asc')->orderBy('S.cWhCode', 'asc');
  453. // 7. 调用你定义的分页方法
  454. $columns = ['*'];
  455. $result = $this->limit($query, $columns, $data);
  456. // 注意这里的 &$item,加了 & 符号才能直接修改原数组里的内容
  457. foreach ($result['data'] as &$item) {
  458. $numFields = ['iQuantity', 'fAvaQuantity', 'fOutQuantity', 'fInQuantity', 'fStopQuantity'];
  459. foreach ($numFields as $field) {
  460. if (isset($item->$field)) {
  461. $item->$field = (float)$item->$field;
  462. }
  463. }
  464. $item->dMdate = $item->dMdate ? date('Y-m-d', strtotime($item->dMdate)) : '';
  465. $item->dVDate = $item->dVDate ? date('Y-m-d', strtotime($item->dVDate)) : '';
  466. // 脱敏处理
  467. if (!empty($field_list)) {
  468. foreach ($field_list as $blackField) {
  469. if (isset($item->$blackField)) {
  470. $item->$blackField = '*****';
  471. }
  472. }
  473. }
  474. }
  475. unset($item); // 销毁引用
  476. return [true, $result];
  477. } catch (\Throwable $exception) {
  478. return [false, "查询库存失败: " . $exception->getMessage()];
  479. }
  480. }
  481. public function vendorU8List($data, $user)
  482. {
  483. // 1. 构建基础查询
  484. $query = $this->databaseService->table('Vendor as V')
  485. ->select([
  486. 'V.cVenCode', // 供应商编码
  487. 'V.cVenName', // 供应商名称
  488. 'V.cVenAbbName', // 供应商简称
  489. 'V.cVCCode', // 分类编码
  490. 'VC.cVCName', // 分类名称 (来自 VendorClass)
  491. 'V.cVenAddress', // 地址
  492. 'V.cVenPhone', // 电话
  493. 'V.dVenDevDate', // 发展日期
  494. 'V.cCreatePerson', // 创建人
  495. 'V.bVenTax', // 是否计税
  496. 'V.iId' // 内部ID
  497. ])
  498. // 关联供应商分类表获取分类名称
  499. ->leftJoin('VendorClass as VC', 'V.cVCCode', '=', 'VC.cVCCode');
  500. // 2. 增加搜索逻辑 (可选)
  501. if (!empty($data['keyword'])) {
  502. $keyword = $data['keyword'];
  503. $query->where(function($q) use ($keyword) {
  504. $q->where('V.cVenCode', 'like', "%{$keyword}%")
  505. ->orWhere('V.cVenName', 'like', "%{$keyword}%")
  506. ->orWhere('V.cVenAbbName', 'like', "%{$keyword}%");
  507. });
  508. }
  509. // 3. 排序 (默认按编码排序)
  510. $query->orderBy('V.cVenCode', 'ASC');
  511. // 4. 调用你定义的分页方法
  512. // 注意:limit 方法内部会执行 paginate 并将结果填充到 $data
  513. $columns = ['*']; // select 已经在上面定义过了,这里传 * 即可
  514. $result = $this->limit($query, $columns, $data);
  515. return [true, $result];
  516. }
  517. public function vendorClassTree($data, $user)
  518. {
  519. try {
  520. // 1. 获取所有供应商分类 (表名: VendorClass)
  521. $classes = $this->databaseService->table('VendorClass')
  522. ->select('cVCCode', 'cVCName', 'iVCGrade', 'bVCEnd') // 编码、名称、级次
  523. ->orderBy('cVCCode', 'asc')
  524. ->get();
  525. if ($classes->isEmpty()) {
  526. return [true, []];
  527. }
  528. // 2. 格式化数据,以编码为 Key
  529. $classList = [];
  530. foreach ($classes as $item) {
  531. $classList[$item->cVCCode] = [
  532. 'label' => $item->cVCName,
  533. 'value' => $item->cVCCode, // 前端通常需要 value 字段
  534. 'code' => $item->cVCCode,
  535. 'grade' => $item->iVCGrade,
  536. 'is_end' => $item->bVCEnd,
  537. 'children' => []
  538. ];
  539. }
  540. // 3. 构建引用树
  541. $tree = [];
  542. foreach ($classList as $code => &$node) {
  543. // 获取父级编码
  544. $parentCode = $this->getParentCode($code);
  545. if ($parentCode === null || !isset($classList[$parentCode])) {
  546. // 顶级节点
  547. $tree[] = &$node;
  548. } else {
  549. // 挂载到父节点
  550. $classList[$parentCode]['children'][] = &$node;
  551. }
  552. }
  553. return [true, $tree];
  554. } catch (\Throwable $exception) {
  555. return [false, "获取供应商分类树失败: " . $exception->getMessage()];
  556. }
  557. }
  558. /**
  559. * 辅助函数:根据 U8 编码规则获取父级编码
  560. * U8 的级次通常存储在 GradeDef 表,但通用逻辑是截取末尾
  561. */
  562. private function getParentCode($code)
  563. {
  564. $len = strlen($code);
  565. if ($len <= 2) return null; // 假设第一级是2位,小于等于2位则无父级
  566. // 这里假设级次是 2-2-2-2 (最常见配置)
  567. // 实际生产中,如果级次不固定,建议查询 GradeDef 表
  568. return substr($code, 0, $len - 2);
  569. }
  570. //U8 存货分类树结构
  571. public function inventoryClassTree($data, $user)
  572. {
  573. try {
  574. // 1. 从数据库获取所有存货分类
  575. $classes = $this->databaseService->table('InventoryClass')
  576. ->select('cInvCCode', 'cInvCName', 'iInvCGrade', 'bInvCEnd')
  577. ->orderBy('cInvCCode', 'asc')
  578. ->get();
  579. if ($classes->isEmpty()) return [true, []];
  580. // 2. 将集合转换为数组并以编码作为 Key,方便查找
  581. $classList = [];
  582. foreach ($classes as $item) {
  583. $classList[$item->cInvCCode] = [
  584. 'label' => $item->cInvCName,
  585. 'code' => $item->cInvCCode,
  586. 'grade' => $item->iInvCGrade,
  587. 'is_end' => $item->bInvCEnd,
  588. 'children' => []
  589. ];
  590. }
  591. // 3. 构建树形结构
  592. $tree = [];
  593. foreach ($classList as $code => &$node) {
  594. // 获取当前分类的级次 (U8 逻辑通常根据编码长度判断父级)
  595. // 比如 0101 的父级是 01
  596. $parentCode = $this->getParentCode($code);
  597. if ($parentCode === null || !isset($classList[$parentCode])) {
  598. // 如果没有父级编码,或者父级编码不在列表里,说明是顶级分类
  599. $tree[] = &$node;
  600. } else {
  601. // 将当前节点引用到父节点的 children 数组中
  602. $classList[$parentCode]['children'][] = &$node;
  603. }
  604. }
  605. return [true, $tree];
  606. } catch (\Throwable $exception) {
  607. return [false, "获取分类树失败: " . $exception->getMessage()];
  608. }
  609. }
  610. //U8 计量单位组(带默认主计量单位)
  611. public function getUnitGroups($data, $user)
  612. {
  613. $list = $this->databaseService->select("
  614. SELECT
  615. G.cGroupCode,
  616. G.cGroupName,
  617. G.iGroupType,
  618. U.cComUnitCode,
  619. U.cComUnitName,
  620. U.iNumber
  621. FROM ComputationGroup AS G
  622. OUTER APPLY (
  623. SELECT TOP 1 cComUnitCode, cComUnitName, iNumber
  624. FROM ComputationUnit
  625. WHERE cGroupCode = G.cGroupCode
  626. ORDER BY
  627. bMainUnit DESC, -- 1. 优先主计量
  628. iNumber ASC, -- 2. 序号最小 (若 NULL 会排在最前)
  629. cComUnitCode ASC -- 3. 编码最小
  630. ) AS U
  631. ");
  632. return [true, $list];
  633. }
  634. //U8 计量单位档案
  635. public function getComputationUnitList($data, $user)
  636. {
  637. $list = $this->databaseService->table('ComputationUnit as U')
  638. ->select(
  639. 'U.cComUnitCode', // 单位编码
  640. 'U.cComUnitName', // 单位名称
  641. 'U.cGroupCode', // 所属组编码
  642. 'U.bMainUnit', // 是否主单位
  643. 'U.iNumber' // 排序序号
  644. )
  645. ->orderBy('U.cGroupCode', 'ASC')
  646. ->orderBy('U.iNumber', 'ASC') // 按照你要求的 iNumber 排序
  647. ->get();
  648. return [true, $list];
  649. }
  650. //U8 存货新增到用友
  651. public function inventoryAddToU8($data, $user){
  652. $inventory = Inventory::where('del_time', 0)
  653. ->where('id', $data['id'])
  654. ->first();
  655. if(empty($inventory)) return [false, '存货记录不存在或已被删除'];
  656. $inventory = $inventory->toArray();
  657. $title = $inventory['title'];
  658. $category_code = $inventory['category_code'];
  659. if(! empty($data['code'])){
  660. //用户传入
  661. $no = $inventory['code'];
  662. $flag_title = "重填";
  663. }else{
  664. //生成编码
  665. list($status, $msg) = $this->generate('inventory','1005');
  666. if(! $status) return [false, $msg];
  667. $no = $msg;
  668. $flag_title = "重试";
  669. }
  670. $inventoryData = [
  671. 'cInvCode' => $no,
  672. 'cInvName' => $title,
  673. 'cInvCCode' => $category_code,
  674. 'cInvStd' => $inventory['size'] ?? null, // 规格型号
  675. 'bSale' => $inventory['bSale'], //内销
  676. 'bExpSale' => $inventory['bExpSale'], //外销
  677. 'bPurchase' => $inventory['bPurchase'], // 采购
  678. 'bSelf' => $inventory['bSelf'], // 自制
  679. 'bComsume' => $inventory['bComsume'], // 生产耗材
  680. 'iGroupType' => $inventory['unit_group_type'], // 计量单位组类别
  681. 'cGroupCode' => $inventory['unit_group_code'], //计量单位组
  682. 'cComUnitCode' => $inventory['unit_code'], // 主计量单位
  683. 'cShopUnit' => $inventory['unit_code'], // 零售计量单位
  684. 'iImpTaxRate' => $inventory['iImpTaxRate'], // 进项税率
  685. 'iTaxRate' => $inventory['iTaxRate'], // 销项税率
  686. 'dSDate' => date("Y-m-d 00:00:00.000"), // 启用日期
  687. 'cEnterprise' => $inventory['vendor_code_title'] ?? null,
  688. 'iSupplyType' => '0', // 供应类型
  689. 'fConvertRate' => '1.0', // 转换因子
  690. 'bInTotalCost' => '1', // 成本累计否
  691. 'cPlanMethod' => 'R',
  692. 'cSRPolicy' => 'PE',
  693. 'bBomMain' => '0', // 允许BOM母件
  694. 'bBomSub' => '0', // 允许BOM子件
  695. 'bProductBill' => '0', // 允许生产订单
  696. 'iPlanDefault' => '1',
  697. 'iAllocatePrintDgt' => '4',
  698. 'bService' => '0',
  699. 'bAccessary' => '0',
  700. 'iInvAdvance' => '0.0',
  701. 'bInvQuality' => '0',
  702. 'bInvBatch' => '0',
  703. 'bInvEntrust' => '0',
  704. 'bInvOverStock' => '0',
  705. 'bFree1' => '0',
  706. 'bFree2' => '0',
  707. 'bInvType' => '0',
  708. 'bFree3' => '0',
  709. 'bFree4' => '0',
  710. 'bFree5' => '0',
  711. 'bFree6' => '0',
  712. 'bFree7' => '0',
  713. 'bFree8' => '0',
  714. 'bFree9' => '0',
  715. 'bFree10' => '0',
  716. 'cCreatePerson' => 'demo',
  717. 'cModifyPerson' => 'demo', // 变更
  718. 'dModifyDate' => date("Y-m-d H:i:s.000"),
  719. 'bFixExch' => '0',
  720. 'bTrack' => '0',
  721. 'bSerial' => '0',
  722. 'bBarCode' => '0',
  723. 'bSolitude' => '0',
  724. 'bSpecialties' => '0',
  725. 'bPropertyCheck' => '0',
  726. 'iRecipeBatch' => '0',
  727. 'bPromotSales' => '0',
  728. 'bPlanInv' => '0',
  729. 'bProxyForeign' => '0',
  730. 'bATOModel' => '0',
  731. 'bCheckItem' => '0',
  732. 'bPTOModel' => '0',
  733. 'bEquipment' => '0',
  734. 'bMPS' => '0',
  735. 'bROP' => '0',
  736. 'bRePlan' => '0',
  737. 'bBillUnite' => '0',
  738. 'bCutMantissa' => '0',
  739. 'bConfigFree1' => '0',
  740. 'bConfigFree2' => '0',
  741. 'bConfigFree3' => '0',
  742. 'bConfigFree4' => '0',
  743. 'bConfigFree5' => '0',
  744. 'bConfigFree6' => '0',
  745. 'bConfigFree7' => '0',
  746. 'bConfigFree8' => '0',
  747. 'bConfigFree9' => '0',
  748. 'bConfigFree10' => '0',
  749. 'bPeriodDT' => '0',
  750. 'bOutInvDT' => '0',
  751. 'bBackInvDT' => '0',
  752. 'bDTWarnInv' => '0',
  753. 'bImportMedicine' => '0',
  754. 'bFirstBusiMedicine' => '0',
  755. 'bForeExpland' => '0',
  756. 'bInvModel' => '0',
  757. 'bKCCutMantissa' => '0',
  758. 'bReceiptByDT' => '0',
  759. 'bCheckBSATP' => '0',
  760. 'bCheckFree1' => '0',
  761. 'bCheckFree2' => '0',
  762. 'bCheckFree3' => '0',
  763. 'bCheckFree4' => '0',
  764. 'bCheckFree5' => '0',
  765. 'bCheckFree6' => '0',
  766. 'bCheckFree7' => '0',
  767. 'bCheckFree8' => '0',
  768. 'bCheckFree9' => '0',
  769. 'bCheckFree10' => '0',
  770. 'iCheckATP' => '0',
  771. 'bPiece' => '0',
  772. 'bSrvItem' => '0',
  773. 'bSrvFittings' => '0',
  774. 'bSpecialOrder' => '0',
  775. 'bTrackSaleBill' => '0',
  776. 'bCheckBatch' => '0',
  777. 'bMngOldpart' => '0',
  778. ];
  779. $inventorySubData = [
  780. 'cInvSubCode' => $no,
  781. 'iSurenessType' => '1',
  782. 'bIsAttachFile' => '0',
  783. 'bInByProCheck' => '1',
  784. 'iRequireTrackStyle' => '0',
  785. 'iExpiratDateCalcu' => '0',
  786. 'iBOMExpandUnitType' => '1',
  787. 'iDrawType' => $inventory['iDrawType'] ?? 0, // 领用方式
  788. 'fInvCIQExch' => $inventory['customs_change_rate'] ?? 1, // 海关换算率
  789. 'bInvKeyPart' => '1',
  790. 'iAcceptEarlyDays' => '999',
  791. 'dInvCreateDatetime' => date("Y-m-d H:i:s.000"),
  792. 'bPUQuota' => '0',
  793. 'bInvROHS' => '0',
  794. 'bPrjMat' => '0',
  795. 'bInvAsset' => '0',
  796. 'bSrvProduct' => '0',
  797. 'iAcceptDelayDays' => '0',
  798. 'bSCkeyProjections' => '0',
  799. 'iSupplyPeriodType' => '1',
  800. 'iAvailabilityDate' => '1',
  801. 'bImport' => '0',
  802. 'bCheckSubitemCost' => '1',
  803. 'fRoundFactor' => '0.0',
  804. 'bConsiderFreeStock' => '1',
  805. 'bSuitRetail' => '0',
  806. 'bFeatureMatch' => '0',
  807. 'bProduceByFeatureAllocate' => '0',
  808. 'bMaintenance' => '0',
  809. 'iMaintenanceCycleUnit' => '0',
  810. 'bCoupon' => '0',
  811. 'bStoreCard' => '0',
  812. 'bProcessProduct' => '0',
  813. 'bProcessMaterial' => '0',
  814. // 所有的价格自由项默认设为 '0'
  815. 'bPurPriceFree1' => '0', 'bPurPriceFree2' => '0', 'bPurPriceFree3' => '0', 'bPurPriceFree4' => '0', 'bPurPriceFree5' => '0',
  816. 'bPurPriceFree6' => '0', 'bPurPriceFree7' => '0', 'bPurPriceFree8' => '0', 'bPurPriceFree9' => '0', 'bPurPriceFree10' => '0',
  817. 'bOMPriceFree1' => '0', 'bOMPriceFree2' => '0', 'bOMPriceFree3' => '0', 'bOMPriceFree4' => '0', 'bOMPriceFree5' => '0',
  818. 'bOMPriceFree6' => '0', 'bOMPriceFree7' => '0', 'bOMPriceFree8' => '0', 'bOMPriceFree9' => '0', 'bOMPriceFree10' => '0',
  819. 'bSalePriceFree1' => '0', 'bSalePriceFree2' => '0', 'bSalePriceFree3' => '0', 'bSalePriceFree4' => '0', 'bSalePriceFree5' => '0',
  820. 'bSalePriceFree6' => '0', 'bSalePriceFree7' => '0', 'bSalePriceFree8' => '0', 'bSalePriceFree9' => '0', 'bSalePriceFree10' => '0',
  821. // 所有的控制自由项默认设为 '0'
  822. 'bControlFreeRange1' => '0', 'bControlFreeRange2' => '0', 'bControlFreeRange3' => '0', 'bControlFreeRange4' => '0', 'bControlFreeRange5' => '0',
  823. 'bControlFreeRange6' => '0', 'bControlFreeRange7' => '0', 'bControlFreeRange8' => '0', 'bControlFreeRange9' => '0', 'bControlFreeRange10' => '0',
  824. // 所有的批次属性默认设为 '0'
  825. 'bBatchProperty1' => '0', 'bBatchProperty2' => '0', 'bBatchProperty3' => '0', 'bBatchProperty4' => '0', 'bBatchProperty5' => '0',
  826. 'bBatchProperty6' => '0', 'bBatchProperty7' => '0', 'bBatchProperty8' => '0', 'bBatchProperty9' => '0', 'bBatchProperty10' => '0',
  827. 'bBondedInv' => '0',
  828. 'bBatchCreate' => '0',
  829. ];
  830. try {
  831. $this->databaseService->beginTransaction();
  832. $exists = $this->databaseService->table('Inventory')
  833. ->where('cInvCode', $inventoryData['cInvCode'])
  834. ->lockForUpdate() // 锁定该行,防止并发冲突
  835. ->exists();
  836. if ($exists) return [false, '存货编码已存在,请' . $flag_title];
  837. $this->databaseService->table('Inventory')->insert($inventoryData);
  838. $this->databaseService->table('Inventory_sub')->insert($inventorySubData);
  839. $this->databaseService->commit();
  840. } catch (\Throwable $exception) {
  841. $this->databaseService->rollBack();
  842. return [false, "创建用友失败: " . $exception->getMessage()];
  843. }
  844. return [true, ''];
  845. }
  846. //U8 供应商新增
  847. public function vendorAddToU8($data, $user){
  848. $vendor = Vendor::where('del_time', 0)
  849. ->where('id', $data['id'])
  850. ->first();
  851. if(empty($vendor)) return [false, '供应商记录不存在或已被删除'];
  852. $vendor = $vendor->toArray();
  853. if($vendor['status'] != Vendor::STATE_TWO) return [false, '供应商记录未审核通过'];
  854. if(! empty($vendor['code'])){
  855. //用户传入
  856. $no = $vendor['code'];
  857. $flag_title = "重填";
  858. }else{
  859. //生成编码
  860. list($status, $msg) = $this->generate('vendor');
  861. if(! $status) return [false, $msg];
  862. $no = $msg;
  863. $flag_title = "重试";
  864. }
  865. $vendorData = [
  866. 'cVenCode' => $no,
  867. 'cVenName' => $vendor['title'],
  868. 'cVenAbbName' => $vendor['easy_title'],
  869. 'cVCCode' => $vendor['category_code'],
  870. 'dVenDevDate' => date("Y-m-d 00:00:00.000"),
  871. 'iVenDisRate' => '0.0',
  872. 'iVenCreLine' => '0.0',
  873. 'iVenCreDate' => '0',
  874. 'cVenHeadCode' => $no,
  875. 'iAPMoney' => '0.0',
  876. 'iLastMoney' => '0.0',
  877. 'iLRMoney' => '0.0',
  878. 'iFrequency' => '0',
  879. 'bVenTax' => '1',
  880. 'cCreatePerson' => 'demo',
  881. 'cModifyPerson' => 'demo',
  882. 'dModifyDate' => date("Y-m-d H:i:s.000"),
  883. 'iGradeABC' => '-1',
  884. 'bLicenceDate' => '0',
  885. 'bBusinessDate' => '0',
  886. 'bProxyDate' => '0',
  887. 'bPassGMP' => '0',
  888. 'bVenCargo' => '1', // 采购
  889. 'bProxyForeign' => '0', // 委外
  890. 'bVenService' => '0', // 服务
  891. 'bVenOverseas' => '0', // 国外
  892. 'cVenExch_name' => '人民币',
  893. 'iVenGSPType' => '0',
  894. 'iVenGSPAuth' => '-1',
  895. 'bVenAccPeriodMng' => '0',
  896. 'bVenHomeBranch' => '0',
  897. 'dVenCreateDatetime' => date("Y-m-d H:i:s.000"),
  898. 'bIsVenAttachFile' => '0',
  899. 'bRetail' => '0',
  900. ];
  901. try {
  902. $this->databaseService->beginTransaction();
  903. $exists = $this->databaseService->table('Vendor')
  904. ->where('cVenCode', $vendorData['cVenCode'])
  905. ->lockForUpdate() // 锁定该行,防止并发冲突
  906. ->exists();
  907. if ($exists) return [false, '供应商编码已存在,请' . $flag_title];
  908. $this->databaseService->table('Vendor')->insert($vendorData);
  909. $this->databaseService->commit();
  910. } catch (\Throwable $exception) {
  911. $this->databaseService->rollBack();
  912. return [false, "创建供应商失败: " . $exception->getMessage()];
  913. }
  914. return [true, ''];
  915. }
  916. // 生成编码
  917. public function generate($type, $classCode = "")
  918. {
  919. try {
  920. // 1. 确定对象映射
  921. $cardNumber = ($type === 'inventory') ? 'inventory' : 'Vendor';
  922. $cardNumber_name = ($type === 'inventory') ? '存货' : '供应商';
  923. $table = ($type === 'inventory') ? 'Inventory' : 'Vendor';
  924. $codeField = ($type === 'inventory') ? 'cInvCode' : 'cVenCode';
  925. // 2. 获取 U8 规则定义
  926. $rule = $this->databaseService->table('VoucherNumber')
  927. ->where('CardNumber', $cardNumber)
  928. ->first();
  929. if (!$rule) return [false, "未找到 {$cardNumber_name} 的规则定义"];
  930. /**
  931. * 3. 动态解析前缀 (Prefix1, Prefix2, Prefix3)
  932. * U8 的规则通常存放在 PrefixRule 或 Prefix 字段中
  933. */
  934. $prefix = "";
  935. for ($i = 1; $i <= 3; $i++) {
  936. $ruleField = "Prefix{$i}Rule";
  937. $valField = "Prefix{$i}";
  938. // 检查规则描述
  939. $ruleDesc = $rule->$ruleField ?? ''; // 例如 "存货分类编码" 或 "GYS"
  940. $staticVal = $rule->$valField ?? '';
  941. if (str_contains($ruleDesc, '分类') || str_contains($staticVal, '分类')) {
  942. // 如果规则提到“分类”,则填入传入的分类编码
  943. $prefix .= $classCode;
  944. } elseif (!empty($ruleDesc)) {
  945. // 如果规则是具体的字符(如 "GYS"),直接拼接
  946. $prefix .= $ruleDesc;
  947. } elseif (!empty($staticVal) && $staticVal !== '手工输入' && $staticVal !== '存货分类编码') {
  948. // 如果静态值不是提示语,则作为前缀
  949. $prefix .= $staticVal;
  950. }
  951. }
  952. // 4. 获取流水号配置 (Glide)
  953. $glideLen = $rule->GlideLen > 0 ? $rule->GlideLen : 4;
  954. $startNum = $rule->iStartNumber ?? 1;
  955. // 5. 查找当前最大编码
  956. $expectedLen = strlen($prefix) + $glideLen;
  957. $lastCode = $this->databaseService->table($table)
  958. ->where($codeField, 'like', $prefix . '%')
  959. ->whereRaw("LEN($codeField) = $expectedLen")
  960. ->orderBy($codeField, 'desc')
  961. ->value($codeField);
  962. // 6. 生成编码
  963. if (!$lastCode) {
  964. // 初始:前缀 + 起始值补零
  965. $finalCode = $prefix . str_pad($startNum, $glideLen, '0', STR_PAD_LEFT);
  966. } else {
  967. // 截取流水号自增
  968. $lastSerial = substr($lastCode, -$glideLen);
  969. $nextSerial = ++$lastSerial;
  970. // 溢出检查
  971. if (strlen($nextSerial) > $glideLen) {
  972. return [false, "流水号已溢出"];
  973. }
  974. $finalCode = $prefix . str_pad($nextSerial, $glideLen, '0', STR_PAD_LEFT);
  975. }
  976. return [true, $finalCode];
  977. } catch (\Throwable $e) {
  978. return [false, "生成失败: " . $e->getMessage()];
  979. }
  980. }
  981. }