OLDataProvider.m 556 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016
  1. //
  2. // OLDataProvider.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 2/2/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "OLDataProvider.h"
  9. #import "iSalesDB.h"
  10. #import "RAUtils.h"
  11. #import "AESCrypt.h"
  12. #import "AppDelegate.h"
  13. #import "ZipArchive.h"
  14. //#import "UIProgressView+AFNetworking.h"
  15. //#import "AFHTTPSessionManager.h"
  16. #import "AppDelegate.h"
  17. #import "pdfCreator.h"
  18. #import "Singleton.h"
  19. #import "QRCodeGenerator.h"
  20. #import "config.h"
  21. #import "ImageUtils.h"
  22. @interface OLDataProvider ()
  23. @end
  24. @implementation OLDataProvider
  25. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  26. {
  27. NSString * sqlQuery = [NSString stringWithFormat:@"select decrypt(price) from model_price where item_id=%d and price_name in (select name from price where type=%d) ",item_id,price_id];
  28. NSString* ret=@"No Price.";
  29. sqlite3_stmt * statement;
  30. // int count=0;
  31. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  32. {
  33. if (sqlite3_step(statement) == SQLITE_ROW)
  34. {
  35. char *price = (char*)sqlite3_column_text(statement, 0);
  36. if(price==nil)
  37. price="";
  38. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  39. double dp= [nsprice doubleValue];
  40. ret=[NSString stringWithFormat:@"%.2f",dp];
  41. }
  42. sqlite3_finalize(statement);
  43. }
  44. return ret;
  45. // // [iSalesDB close_db:db];
  46. //
  47. // return nil;
  48. //// if(dprice==DBL_MAX)
  49. //// ret= nil;
  50. //// else
  51. //// ret= [NSNumber numberWithDouble:dprice];
  52. //// return ret;
  53. }
  54. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  55. {
  56. NSString* ret= nil;
  57. NSString *sqlQuery = nil;
  58. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.item_id=%@ order by i.default_img desc, i._id asc limit 1;",item_id];// select i.url from model m
  59. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ and i.type<=1 order by i.default_img desc, i.type desc limit 1;",item_id];
  60. sqlite3_stmt * statement;
  61. // int count=0;
  62. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  63. {
  64. if (sqlite3_step(statement) == SQLITE_ROW)
  65. {
  66. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  67. if(imgurl==nil)
  68. imgurl="";
  69. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  70. ret=nsimgurl;
  71. }
  72. sqlite3_finalize(statement);
  73. }
  74. else
  75. {
  76. ret=@"";
  77. }
  78. // [iSalesDB close_db:db];
  79. // DebugLog(@"data string: %@",ret );
  80. return ret;
  81. }
  82. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  83. {
  84. NSMutableDictionary* values=params[@"replaceValue"]; // 离线下发的参数
  85. //生成portfolio pdf需要的数据
  86. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  87. data[@"npd_url"]=COMPANY_WEB;
  88. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  89. [formatter setDateFormat:@"MMMM yyyy"];
  90. NSString* date = [formatter stringFromDate:[NSDate date]];
  91. data[@"create_month"]=date;
  92. data[@"company_name"]=COMPANY_FULL_NAME;
  93. data[@"catalog_name"]=params[@"catalog_name"];
  94. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  95. data[@"TOTAL_PAGE"]=0;
  96. data[@"CURRENT_PAGE"]=0;
  97. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  98. BOOL show_group_by = [[params objectForKey:@"show_group_by"] boolValue];
  99. bool show_stockout=[params[@"show_stockout"] boolValue];
  100. NSString *sql = nil;
  101. // v1.90 more color
  102. if(values==nil)
  103. {
  104. sql=[NSString stringWithFormat:@"select a.product_id,a.name,a.description,a.price,a.sheet_discount,a.qty,a.is_percentage,a.item_id,a.fashion_id,a.line_note,a.percent,a._id,b.availability,b.color,b.legcolor,b.demension,b.seat_height,b.material,b.box_dim,b.volume,b.weight,stockuom,b.load_ability,b.fabric_content,b.assembling,b.made_in,b.more_color from (select _id, product_id,name,description,ifnull(sheet_price,'null') as price,sheet_discount,ifnull(available_qty,'null') as qty,percentage as is_percentage,item_id,fashion_id,line_note,percent from offline_portfolio where product_id in(%@)) a left join (select * from product where product_id in(%@)) b on a.product_id=b.product_id order by a.name",params[@"product_ids"],params[@"product_ids"]];
  105. }
  106. else
  107. {
  108. sql=[NSString stringWithFormat:@"select product_id,name,description,null,null,null,null,item_id,null,null,null,-1,availability,color,legcolor,demension,seat_height,material,box_dim,volume,weight,stockuom,load_ability,fabric_content,assembling,made_in,more_color from product where product_id in(%@) order by name",params[@"product_ids"]];
  109. }
  110. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  111. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  112. sqlite3 *db = [iSalesDB get_db];
  113. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  114. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  115. int product_id = sqlite3_column_int(stmt, 0);
  116. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  117. NSString *name = [self textAtColumn:1 statement:stmt];
  118. NSString *description = [self textAtColumn:2 statement:stmt];
  119. double price = sqlite3_column_double(stmt, 3);
  120. NSString *s_price = [self textAtColumn:3 statement:stmt];
  121. double discount = sqlite3_column_double(stmt,4);
  122. int qty = sqlite3_column_int(stmt, 5);
  123. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  124. int is_percentage = sqlite3_column_int(stmt, 6);
  125. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  126. int item_id = sqlite3_column_int(stmt, 7);
  127. // int fashion_id = sqlite3_column_int(stmt, 8);
  128. NSString *line_note = [self textAtColumn:9 statement:stmt];
  129. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  130. double percent = sqlite3_column_double(stmt, 10);
  131. // int portfolio_id = sqlite3_column_int(stmt, 11);
  132. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  133. int availability = sqlite3_column_int(stmt, 12);
  134. NSString *color = [self textAtColumn:13 statement:stmt];
  135. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  136. NSString *demension = [self textAtColumn:15 statement:stmt];
  137. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  138. NSString *material = [self textAtColumn:17 statement:stmt];
  139. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  140. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  141. double volume = sqlite3_column_double(stmt, 19);
  142. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  143. double weight = sqlite3_column_double(stmt, 20);
  144. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  145. int model_set = sqlite3_column_int(stmt, 21);
  146. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  147. double load_ability = sqlite3_column_double(stmt, 22);
  148. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  149. NSString *assembling = [self textAtColumn:24 statement:stmt];
  150. NSString *made_in = [self textAtColumn:25 statement:stmt];
  151. NSInteger more_color_int = sqlite3_column_int(stmt, 26);
  152. NSString* gprice = [self get_portfolio_price :appDelegate.contact_id item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  153. float bprice=0;
  154. NSDictionary* bundle=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  155. for(int b=0;b< [bundle[@"count"] intValue];b++)
  156. {
  157. NSDictionary* bi=bundle[[NSString stringWithFormat:@"item_%d",b]];
  158. bprice+= [bi[@"modulus"] intValue] * [bi[@"unit_price"] floatValue];
  159. }
  160. if(gprice!=nil)
  161. gprice = [NSString stringWithFormat:@"%.2f",[gprice floatValue]+bprice];
  162. if(s_percent.length==0||is_percentage==0)
  163. {
  164. percent=100.0;
  165. if([s_qty isEqualToString:@"null"])
  166. qty=availability;
  167. }
  168. else
  169. {
  170. qty=availability;
  171. }
  172. qty=qty*percent/100+0.5;
  173. for(int i=0;i<[values[@"count"] intValue];i++)
  174. {
  175. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  176. if([valueitem[@"product_id"] intValue]==product_id)
  177. {
  178. line_note=valueitem[@"line_note"];
  179. qty=[valueitem[@"available_qty"] intValue];
  180. if([[valueitem allKeys] containsObject:@"available_qty"])
  181. s_qty=[NSString stringWithFormat:@"%d",qty];
  182. else
  183. s_qty=@"null";
  184. if(valueitem[@"regular_price"]!=nil)
  185. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  186. s_price=valueitem[@"special_price"];
  187. price=[valueitem[@"special_price"] floatValue];
  188. discount=0;
  189. // v1.90
  190. more_color_int = [[valueitem objectForKey:@"more_color"] integerValue];
  191. }
  192. }
  193. if(is_percentage==0&&qty==0&&!show_stockout)
  194. {
  195. return;
  196. }
  197. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  198. {
  199. return;
  200. }
  201. NSString* set_price=@"";
  202. if([params[@"entered_price"] boolValue])
  203. {
  204. if (s_price==nil ) {
  205. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  206. }
  207. else
  208. {
  209. if(price*(1-discount/100.0)!=gprice.floatValue)
  210. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  211. }
  212. }
  213. NSString* get_price=@"";
  214. {
  215. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  216. // DebugLog(@"price time interval");
  217. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  218. if(gprice==nil)
  219. get_price=@"Price:No Price.";
  220. else
  221. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  222. }
  223. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  224. // if ([qty_null isEqualToString:@"null"]) {
  225. // qty=availability;
  226. // }
  227. //
  228. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  229. // if ([qty_percent_null isEqualToString:@"null"]) {
  230. // percentage=1;
  231. // }
  232. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  233. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  234. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  235. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  236. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  237. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  238. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  239. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  240. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  241. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  242. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  243. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  244. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  245. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  246. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  247. if([params[@"availability"] boolValue]==false)
  248. str_availability=@"";
  249. else
  250. {
  251. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  252. str_availability=@"<b>Availability:</b> In Production";
  253. }
  254. if([params[@"color"] boolValue]==false || color.length==0 )
  255. str_color=@"";
  256. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  257. str_model_set=@"";
  258. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  259. str_legcolor=@"";
  260. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  261. str_demension=@"";
  262. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  263. str_seat_height=@"";
  264. if([params[@"material"] boolValue]==false || material.length==0 )
  265. str_meterial=@"";
  266. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  267. str_box_dim=@"";
  268. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  269. str_volume=@"";
  270. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  271. str_weight=@"";
  272. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  273. str_load_ability=@"";
  274. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  275. str_fabric_content=@"";
  276. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  277. str_assembling=@"";
  278. if([params[@"made"] boolValue]==false || made_in.length==0 )
  279. str_made_in=@"";
  280. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  281. str_line_note=@"";
  282. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  283. [arr_detail addObject:str_availability];
  284. [arr_detail addObject:str_color];
  285. [arr_detail addObject:str_model_set];
  286. [arr_detail addObject:str_legcolor];
  287. [arr_detail addObject:str_demension];
  288. [arr_detail addObject:str_seat_height];
  289. [arr_detail addObject:str_meterial];
  290. [arr_detail addObject:str_box_dim];
  291. [arr_detail addObject:str_volume];
  292. [arr_detail addObject:str_weight];
  293. [arr_detail addObject:str_load_ability];
  294. [arr_detail addObject:str_fabric_content];
  295. [arr_detail addObject:str_assembling];
  296. [arr_detail addObject:str_made_in];
  297. [arr_detail addObject:str_line_note];
  298. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  299. NSString* detail =[RAUtils arr2string:arr_detail separator:@" " trim:true];
  300. //model image;
  301. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  302. // qr image
  303. NSString* qrpath=nil;
  304. if([params[@"show_barcode"] boolValue])
  305. {
  306. NSString* temp = NSTemporaryDirectory();
  307. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  308. qrpath=[temp stringByAppendingPathComponent:filename];
  309. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  310. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  311. }
  312. NSString *more_color_path = [[NSBundle mainBundle] pathForResource:@"more_color_64" ofType:@"png"];
  313. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  314. item[@"img"]=img_path;
  315. item[@"img_qr"]=qrpath;
  316. item[@"special_price"]=set_price;
  317. item[@"price"]=get_price;
  318. item[@"name"]=name;
  319. item[@"description"]=description;
  320. item[@"detail"]=detail;
  321. item[@"img"]=img_path;
  322. if (show_group_by && more_color_int != 0) {
  323. item[@"more_color"] = more_color_path;
  324. }
  325. // @{
  326. // //@"linenotes": line_note,
  327. //// @"product_id": product_id_string,
  328. //// @"available_qty": @(qty),
  329. //// @"available_percent" : @(percent),
  330. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  331. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  332. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  333. // @"": ,
  334. // @"": ,
  335. // @"": ,
  336. // @"": ,
  337. // @"": ,
  338. // @"": ,
  339. // @"":
  340. // }.mutableCopy;
  341. // if (percentage) {
  342. // [item removeObjectForKey:@"available_qty"];
  343. // } else {
  344. // [item removeObjectForKey:@"available_percent"];
  345. // }
  346. //
  347. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  348. // if ([qty_null isEqualToString:@"null"]) {
  349. // [item removeObjectForKey:@"available_qty"];
  350. // }
  351. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  352. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  353. }];
  354. [iSalesDB close_db:db];
  355. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  356. // [dic setValue:@"" forKey:@"email_content"];
  357. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  358. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  359. //
  360. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  361. // cell[@"count"]=[NSNumber numberWithInt:10];
  362. grid[@"cell0"]=dic;
  363. data[@"grid0"]=grid;
  364. return data;
  365. }
  366. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  367. {
  368. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  369. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  370. NSString *cache_folder=[paths objectAtIndex:0];
  371. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  372. BOOL bdir=NO;
  373. NSFileManager* fileManager = [NSFileManager defaultManager];
  374. if(PDF_DEBUG)
  375. {
  376. NSData *data = [NSData dataWithContentsOfFile:default_path];
  377. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  378. return ret;
  379. }
  380. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  381. {
  382. NSError * error=nil;
  383. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  384. {
  385. return nil;
  386. }
  387. }
  388. NSData *data = [NSData dataWithContentsOfFile:template_path];
  389. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  390. return ret;
  391. }
  392. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  393. {
  394. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  395. NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username,can_update_contact_info,first_name from offline_login where lower(username)='%@' ",user.lowercaseString ];
  396. DebugLog(@"offline_login sql:%@",sqlQuery);
  397. sqlite3_stmt * statement;
  398. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  399. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  400. {
  401. if (sqlite3_step(statement) == SQLITE_ROW)
  402. {
  403. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  404. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  405. int can_show_price = sqlite3_column_int(statement, 0);
  406. int can_see_price = sqlite3_column_int(statement, 1);
  407. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  408. if(contact_id==nil)
  409. contact_id="";
  410. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  411. int user_type = sqlite3_column_int(statement, 3);
  412. int can_cancel_order = sqlite3_column_int(statement, 4);
  413. int can_set_cart_price = sqlite3_column_int(statement, 5);
  414. int can_create_portfolio = sqlite3_column_int(statement, 6);
  415. int can_delete_order = sqlite3_column_int(statement, 7);
  416. int can_submit_order = sqlite3_column_int(statement, 8);
  417. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  418. int can_create_order = sqlite3_column_int(statement, 10);
  419. char *mode = (char*)sqlite3_column_text(statement, 11);
  420. if(mode==nil)
  421. mode="";
  422. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  423. char *username = (char*)sqlite3_column_text(statement, 12);
  424. if(username==nil)
  425. username="";
  426. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  427. int can_update_contact_info = sqlite3_column_int(statement, 13);
  428. char *first_name = (char*)sqlite3_column_text(statement, 14);
  429. if(first_name==nil)
  430. first_name="";
  431. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  432. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  433. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  434. // [header setValue:nscontact_id forKey:@"contact_id"];
  435. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  436. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  437. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  438. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  439. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  440. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  441. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  442. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  443. //
  444. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  445. //
  446. // [header setValue:nsusername forKey:@"username"];
  447. //
  448. // NSError* error=nil;
  449. //
  450. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  451. // [header setValue:statusFilter forKey:@"statusFilter"];
  452. //
  453. //
  454. //
  455. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  456. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  457. //
  458. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  459. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  460. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  461. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  462. //
  463. // [ret setObject:header forKey:@"header"];
  464. [ret setValue:nsfirst_name forKey:@"first_name"];
  465. }
  466. sqlite3_finalize(statement);
  467. }
  468. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  469. return ret;
  470. }
  471. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code type:(int) type
  472. {
  473. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  474. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  475. data[@"npd_url"]=COMPANY_WEB;
  476. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  477. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  478. NSString* date = [formatter stringFromDate:[NSDate date]];
  479. data[@"print_date"]=date;
  480. data[@"company_name"]=COMPANY_FULL_NAME;
  481. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  482. data[@"TOTAL_PAGE"]=0;
  483. data[@"CURRENT_PAGE"]=0;
  484. // [formatter setDateFormat:@"MM/dd/yyyy"];
  485. // date = [formatter stringFromDate:[NSDate date]];
  486. // data[@"creat_date"]=date;
  487. NSString* temp = NSTemporaryDirectory();
  488. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  489. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  490. UIImage* qrimg=[ImageUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  491. bool bwrite=[UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  492. data[@"img_barcode"]=qrpath;
  493. data[@"so_id"]=order_code;
  494. data[@"company"]=COMPANY_FULL_NAME;
  495. // NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where so_id=%@) o left join offline_contact c on o.customer_cid=c.contact_id",order_code];
  496. NSString *sql = [NSString stringWithFormat:@"select erpOrderStatus,sender_name,sender_ext,sender_phone,sender_fax,sender_email,warehouse_name,billing_name,billing_ext,billing_phone,billing_fax,billing_email,receive_name,receive_ext,receive_phone,receive_fax,receive_email,poNumber,sales_rep,carrier,vendor_no,terms,sales_terms,port_destination,etd,sign_picpath,status,create_time,shipping,lift_gate_value,handling_fee_value,billing_contact,receive_contact,general_notes,customer_cid from offline_order where so_id='%@'",order_code];
  497. sqlite3 *db = [iSalesDB get_db];
  498. data[@"print_by"]= [self query_login:appDelegate.user db:db][@"first_name"];
  499. sqlite3_stmt * statement;
  500. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  501. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  502. row0[@"count"]=[NSNumber numberWithInt:1];
  503. datasource[@"row0"]=row0;
  504. NSString* print_status=@"";
  505. NSString* order_type=@"Offline Quote";
  506. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  507. {
  508. if (sqlite3_step(statement) == SQLITE_ROW)
  509. {
  510. int erpOrderStatus = sqlite3_column_int(statement, 0);
  511. int status = sqlite3_column_int(statement, 26);
  512. if(status==2)
  513. {
  514. switch (erpOrderStatus) {
  515. case 10:
  516. // order_type=@"Quoted Order";
  517. print_status=@"Quote";
  518. break;
  519. case 11:
  520. print_status=@"Booked";
  521. break;
  522. case 12:
  523. print_status=@"Picked";
  524. break;
  525. case 13:
  526. print_status=@"Shipped";
  527. break;
  528. case 14:
  529. print_status=@"Closed";
  530. break;
  531. case 15:
  532. print_status=@"Canceled";
  533. break;
  534. case 16:
  535. print_status=@"All";
  536. break;
  537. case 18:
  538. print_status=@"Processing";
  539. break;
  540. default:
  541. break;
  542. }
  543. }
  544. else{
  545. switch (status) {
  546. case 0:
  547. {
  548. print_status=@"Quote";
  549. break;
  550. }
  551. case 1:
  552. {
  553. print_status=@"Saved";
  554. break;
  555. }
  556. case 3:
  557. {
  558. //canceled
  559. print_status=@"Canceled";
  560. break;
  561. }
  562. default:
  563. break;
  564. }
  565. }
  566. NSString* sender_name = [self textAtColumn:1 statement:statement];
  567. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  568. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  569. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  570. NSString* sender_email = [self textAtColumn:5 statement:statement];
  571. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  572. NSString* billing_name = [self textAtColumn:7 statement:statement];
  573. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  574. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  575. if(billing_phone.length>0)
  576. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  577. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  578. if(billing_fax.length>0)
  579. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  580. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  581. [billto_arr0 addObject:billing_phone];
  582. [billto_arr0 addObject:billing_fax];
  583. NSString* billing_pf=[RAUtils arr2string:billto_arr0 separator:@" " trim:true];
  584. NSString* billing_email = [self textAtColumn:11 statement:statement];
  585. if(billing_email.length>0)
  586. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  587. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  588. if(billing_contact.length>0)
  589. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  590. NSString* receive_name = [self textAtColumn:12 statement:statement];
  591. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  592. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  593. if(receive_phone.length>0)
  594. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  595. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  596. if(receive_fax.length>0)
  597. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  598. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  599. [receive_arr0 addObject:receive_phone];
  600. [receive_arr0 addObject:receive_fax];
  601. NSString* receive_email = [self textAtColumn:16 statement:statement];
  602. NSString* receive_pf=[RAUtils arr2string:billto_arr0 separator:@" " trim:true];
  603. if(receive_email.length>0)
  604. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  605. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  606. if(receive_contact.length>0)
  607. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  608. NSString* poNumber = [self textAtColumn:17 statement:statement];
  609. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  610. NSString* carrier = [self textAtColumn:19 statement:statement];
  611. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  612. NSString* terms = [self textAtColumn:21 statement:statement];
  613. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  614. NSString* port_destination = [self textAtColumn:23 statement:statement];
  615. NSString* etd = [self textAtColumn:24 statement:statement];
  616. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  617. if(sign_picpath.length==0)
  618. sign_picpath=nil;
  619. NSString* create_date = [self textAtColumn:27 statement:statement];
  620. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  621. NSString* general_notes=[self textAtColumn:33 statement:statement];
  622. NSString *customerID = [self textAtColumn:34 statement:statement];
  623. if (customerID.length == 0) {
  624. customerID = nil;
  625. }
  626. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  627. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  628. if(general_notes.length>0)
  629. {
  630. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  631. row2[@"item_0"]=@{@"g_notes":general_notes };
  632. row2[@"count"]=[NSNumber numberWithInt:1];
  633. datasource[@"row2"]=row2;
  634. }
  635. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  636. [billto_arr addObject:billing_ext];
  637. [billto_arr addObject:billing_pf];
  638. [billto_arr addObject:billing_contact];
  639. [billto_arr addObject:billing_email];
  640. NSString* billto=[RAUtils arr2string:billto_arr separator:@"\r\n" trim:true];
  641. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  642. [receive_arr addObject:receive_ext];
  643. [receive_arr addObject:receive_pf];
  644. [receive_arr addObject:receive_contact];
  645. [receive_arr addObject:receive_email];
  646. NSString* receive=[RAUtils arr2string:receive_arr separator:@"\r\n" trim:true];
  647. float shipping = sqlite3_column_double(statement, 28);
  648. float lift_gate_value = sqlite3_column_double(statement, 29);
  649. float handling_fee_value = sqlite3_column_double(statement, 30);
  650. data[@"company"]=sender_name;
  651. data[@"shipfrom_addr"]=sender_ext;
  652. data[@"shipfrom_phone"]=sender_phone;
  653. data[@"shipfrom_fax"]=sender_fax;
  654. data[@"shipfrom_email"]=sender_email;
  655. data[@"warehouse_name"]=warehouse_name;
  656. data[@"bill_to_name"]=billing_name;
  657. data[@"bill_to_ext"]=billto;
  658. data[@"ship_to_name"]=receive_name;
  659. data[@"ship_to_ext"]=receive;
  660. data[@"po_no"]=poNumber;
  661. data[@"rep"]=sales_rep;
  662. data[@"e_schdule"]=etd;
  663. data[@"sales_terms"]=sales_terms;
  664. data[@"port_destination"]=port_destination;
  665. data[@"terms"]=terms;
  666. data[@"carrier"]=carrier;
  667. data[@"vendor_no"]=vendor_no;
  668. data[@"customer_sign"]=sign_picpath;
  669. data[@"print_status"]=print_status;
  670. data[@"create_date"]=create_date;
  671. NSString *sql_cart=[ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id ",order_code];;
  672. sqlite3_stmt * statement_cart;
  673. // NSDate *date1 = [NSDate date];
  674. int count=0;
  675. int cart_count=0;
  676. float credits=0;
  677. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  678. if ( dbresult== SQLITE_OK)
  679. {
  680. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  681. int row1count=0;
  682. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  683. {
  684. NSDate *row_date = [NSDate date];
  685. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  686. // int product_id = sqlite3_column_int(statement_cart, 0);
  687. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  688. int item_id = sqlite3_column_int(statement_cart, 7);
  689. double discount = sqlite3_column_double(statement_cart, 2);
  690. NSString* Price=nil;
  691. if(str_price==nil)
  692. {
  693. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  694. // NSDate *price_date = [NSDate date];
  695. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  696. // DebugLog(@"price time interval");
  697. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  698. if(price==nil)
  699. Price=@"No Price.";
  700. else
  701. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  702. }
  703. else
  704. {
  705. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  706. }
  707. float dunitprice=0;
  708. int item_count = sqlite3_column_int(statement_cart, 3);
  709. NSString* Amount=@"";
  710. if(![Price isEqualToString:@"No Price."])
  711. {
  712. dunitprice=[Price floatValue];
  713. dunitprice=dunitprice* (1-discount/100.0);
  714. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  715. credits+=dunitprice*item_count;
  716. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  717. }
  718. else
  719. {
  720. Price=@"";
  721. }
  722. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  723. // NSString *nsline_note=nil;
  724. // if(line_note!=nil)
  725. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  726. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  727. NSString *nsname=nil;
  728. if(name!=nil)
  729. nsname= [[NSString alloc]initWithUTF8String:name];
  730. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  731. NSString *nsdescription=nil;
  732. if(description!=nil)
  733. nsdescription= [[NSString alloc]initWithUTF8String:description];
  734. // int stockUom = sqlite3_column_int(statement_cart, 8);
  735. // int _id = sqlite3_column_int(statement_cart, 9);
  736. // NSDate *subtotal_date = [NSDate date];
  737. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  738. // DebugLog(@"subtotal_date time interval");
  739. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  740. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  741. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  742. // int carton=[bsubtotaljson[@"carton"] intValue];
  743. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  744. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  745. // NSDate *img_date = [NSDate date];
  746. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  747. // DebugLog(@"img_date time interval");
  748. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  749. itemjson[@"model"]=nsname;
  750. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  751. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  752. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  753. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  754. // itemjson[@"check"]=@"true";
  755. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  756. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  757. itemjson[@"unit_price"]=Price;
  758. itemjson[@"amount"]=Amount;
  759. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  760. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  761. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  762. // itemjson[@"note"]=nsline_note;
  763. // NSDate *date2 = [NSDate date];
  764. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  765. // DebugLog(@"model_bundle time interval");
  766. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  767. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  768. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  769. row1count++;
  770. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  771. for(int c=0;c<[combine[@"count"] intValue];c++)
  772. {
  773. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  774. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  775. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  776. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  777. credits+=combine_amount;
  778. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  779. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  780. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  781. row1count++;
  782. }
  783. count++;
  784. // DebugLog(@"row time interval");
  785. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  786. }
  787. row1[@"count"]=[NSNumber numberWithInt:row1count];
  788. datasource[@"row1"]=row1;
  789. data[@"table_model"]=datasource;
  790. sqlite3_finalize(statement_cart);
  791. }
  792. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  793. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  794. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  795. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  796. // if(credits>0)
  797. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  798. // else
  799. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  800. if(shipping>0)
  801. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  802. else
  803. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  804. if(lift_gate_value>0)
  805. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  806. else
  807. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  808. if(handling_fee_value>0)
  809. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  810. else
  811. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  812. float total = credits+shipping+lift_gate_value+handling_fee_value;
  813. if(total>0)
  814. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  815. else
  816. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  817. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  818. fee[@"row0"]=fee_row0;
  819. data[@"table_fee"]=fee;
  820. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  821. // ret[@"sign_url"] = sign_url;
  822. //
  823. // customer_contact = [self textAtColumn:52 statement:statement];
  824. // customer_email = [self textAtColumn:53 statement:statement];
  825. // customer_phone = [self textAtColumn:54 statement:statement];
  826. // customer_fax = [self textAtColumn:55 statement:statement];
  827. //
  828. //
  829. // int offline_edit=sqlite3_column_int(statement, 56);
  830. //
  831. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  832. //
  833. // char *soid = (char*)sqlite3_column_text(statement, 1);
  834. // if(soid==nil)
  835. // soid= "";
  836. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  837. // // so#
  838. // ret[@"so#"] = nssoid;
  839. //
  840. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  841. // if(poNumber==nil)
  842. // poNumber= "";
  843. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  844. //
  845. //
  846. //
  847. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  848. // if(create_time==nil)
  849. // create_time= "";
  850. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  851. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  852. //
  853. // int status = sqlite3_column_int(statement, 4);
  854. // int erpStatus = sqlite3_column_int(statement, 49);
  855. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  856. //
  857. // // status
  858. // if (status > 1 && status != 3) {
  859. // status = erpStatus;
  860. // } else if (status == 3) {
  861. // status = 15;
  862. // }
  863. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  864. // ret[@"order_status"] = nsstatus;
  865. //
  866. //
  867. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  868. // if(company_name==nil)
  869. // company_name= "";
  870. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  871. // // company name
  872. // ret[@"company_name"] = nscompany_name;
  873. //
  874. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  875. // if(customer_contact==nil)
  876. // customer_contact= "";
  877. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  878. //
  879. //
  880. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  881. // if(addr_1==nil)
  882. // addr_1="";
  883. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  884. //
  885. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  886. // if(addr_2==nil)
  887. // addr_2="";
  888. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  889. //
  890. //
  891. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  892. // if(addr_3==nil)
  893. // addr_3="";
  894. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  895. //
  896. //
  897. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  898. // if(addr_4==nil)
  899. // addr_4="";
  900. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  901. //
  902. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  903. // [arr_addr addObject:nsaddr_1];
  904. // [arr_addr addObject:nsaddr_2];
  905. // [arr_addr addObject:nsaddr_3];
  906. // [arr_addr addObject:nsaddr_4];
  907. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  908. //
  909. //
  910. // char *logist = (char*)sqlite3_column_text(statement, 11);
  911. // if(logist==nil)
  912. // logist= "";
  913. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  914. // if (status == -11 || status == 10 || status == 11) {
  915. // nslogist = [self textAtColumn:59 statement:statement];
  916. // };
  917. //
  918. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  919. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  920. // shipping = @"Shipping To Be Quoted";
  921. // } else {
  922. // shippingFee = sqlite3_column_double(statement, 12);
  923. // }
  924. //
  925. // // Shipping
  926. // ret[@"Shipping"] = shipping;
  927. //
  928. // int have_lift_gate = sqlite3_column_int(statement, 17);
  929. // lift_gate = sqlite3_column_double(statement, 13);
  930. // // Liftgate Fee(No loading dock)
  931. // if (!have_lift_gate) {
  932. // lift_gate = 0;
  933. // }
  934. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  935. // if (sqlite3_column_int(statement, 57)) {
  936. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  937. // }
  938. //
  939. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  940. // if(general_notes==nil)
  941. // general_notes= "";
  942. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  943. //
  944. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  945. // if(internal_notes==nil)
  946. // internal_notes= "";
  947. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  948. //
  949. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  950. // if(payment_type==nil)
  951. // payment_type= "";
  952. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  953. //
  954. //
  955. //
  956. // // order info
  957. // orderinfo = [self textFileName:@"order_info.html"];
  958. //
  959. //
  960. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  961. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  962. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  963. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  964. //
  965. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  966. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  967. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  968. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  969. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  970. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  971. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  972. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  973. //
  974. // NSString *payment = nil;
  975. // if([nspayment_type isEqualToString:@"Credit Card"])
  976. // {
  977. // payment = [self textFileName:@"creditcardpayment.html"];
  978. //
  979. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  980. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  981. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  982. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  983. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  984. // NSString *card_type = [self textAtColumn:42 statement:statement];
  985. // if (card_type.length > 0) { // 显示星号
  986. // card_type = @"****";
  987. // }
  988. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  989. // if (card_number.length > 0 && card_number.length > 4) {
  990. // for (int i = 0; i < card_number.length - 4; i++) {
  991. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  992. // }
  993. // } else {
  994. // card_number = @"";
  995. // }
  996. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  997. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  998. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  999. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  1000. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  1001. // card_expiration = @"****";
  1002. // }
  1003. //
  1004. //
  1005. // NSString *card_city = [self textAtColumn:46 statement:statement];
  1006. // NSString *card_state = [self textAtColumn:47 statement:statement];
  1007. //
  1008. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  1009. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  1010. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  1011. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  1012. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  1013. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  1014. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  1015. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  1016. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  1017. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  1018. //
  1019. //
  1020. // }
  1021. // else
  1022. // {
  1023. // payment=[self textFileName:@"normalpayment.html"];
  1024. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1025. // }
  1026. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1027. //
  1028. // ret[@"result"]= [NSNumber numberWithInt:2];
  1029. //
  1030. // // more info
  1031. // moreInfo = [self textFileName:@"more_info.html"];
  1032. //
  1033. //
  1034. // // ShipToCompany_or_&nbsp
  1035. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1036. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1037. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1038. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1039. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1040. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1041. //
  1042. //
  1043. // // ShipFromCompany_or_&nbsp
  1044. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1045. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1046. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1047. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1048. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1049. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1050. //
  1051. //
  1052. // // FreightBillToCompany_or_&nbsp
  1053. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1054. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1055. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1056. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1057. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1058. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1059. //
  1060. //
  1061. // // MerchandiseBillToCompany_or_&nbsp
  1062. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1063. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1064. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1065. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1066. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1067. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1068. //
  1069. //
  1070. // // ReturnToCompany_or_&nbsp
  1071. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1072. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1073. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1074. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1075. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1076. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1077. // //
  1078. //
  1079. // // DebugLog(@"more info : %@",moreInfo);
  1080. //
  1081. // // handling fee
  1082. // handlingFee = sqlite3_column_double(statement, 33);
  1083. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1084. // if (sqlite3_column_int(statement, 58)) {
  1085. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1086. // }
  1087. // //
  1088. //
  1089. // // customer info
  1090. // customerID = [self textAtColumn:36 statement:statement];
  1091. //
  1092. // // mode
  1093. // ret[@"mode"] = appDelegate.mode;
  1094. //
  1095. // // model_count
  1096. // ret[@"model_count"] = @(0);
  1097. }
  1098. sqlite3_finalize(statement);
  1099. }
  1100. [iSalesDB close_db:db];
  1101. data[@"order_type"]=order_type;
  1102. /*
  1103. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1104. // "customer_email" = "Shui Hu";
  1105. // "customer_fax" = "";
  1106. // "customer_first_name" = F;
  1107. // "customer_last_name" = L;
  1108. // "customer_name" = ",da He Xiang Dong Liu A";
  1109. // "customer_phone" = "Hey Xuan Feng";
  1110. contactInfo[@"customer_phone"] = customer_phone;
  1111. contactInfo[@"customer_fax"] = customer_fax;
  1112. contactInfo[@"customer_email"] = customer_email;
  1113. NSString *first_name = @"";
  1114. NSString *last_name = @"";
  1115. if ([customer_contact isEqualToString:@""]) {
  1116. } else if ([customer_contact containsString:@" "]) {
  1117. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1118. first_name = [customer_contact substringToIndex:first_space_index];
  1119. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1120. }
  1121. contactInfo[@"customer_first_name"] = first_name;
  1122. contactInfo[@"customer_last_name"] = last_name;
  1123. ret[@"customerInfo"] = contactInfo;
  1124. // models
  1125. if (nssoid) {
  1126. __block double TotalCuft = 0;
  1127. __block double TotalWeight = 0;
  1128. __block int TotalCarton = 0;
  1129. __block double allItemPrice = 0;
  1130. NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",nssoid];
  1131. sqlite3 *db1 = [iSalesDB get_db];
  1132. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1133. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1134. int product_id = sqlite3_column_int(stmt, 0);
  1135. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1136. int item_id = sqlite3_column_int(stmt, 7);
  1137. NSString* Price=nil;
  1138. if(str_price==nil)
  1139. {
  1140. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1141. if(price==nil)
  1142. Price=@"No Price.";
  1143. else
  1144. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1145. }
  1146. else
  1147. {
  1148. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1149. }
  1150. double discount = sqlite3_column_double(stmt, 2);
  1151. int item_count = sqlite3_column_int(stmt, 3);
  1152. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1153. NSString *nsline_note=nil;
  1154. if(line_note!=nil)
  1155. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1156. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1157. // NSString *nsname = nil;
  1158. // if(name!=nil)
  1159. // nsname= [[NSString alloc]initWithUTF8String:name];
  1160. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1161. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1162. // NSString *nsdescription=nil;
  1163. // if(description!=nil)
  1164. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1165. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1166. // int stockUom = sqlite3_column_int(stmt, 8);
  1167. // int _id = sqlite3_column_int(stmt, 9);
  1168. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1169. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1170. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1171. int carton=[bsubtotaljson[@"carton"] intValue];
  1172. TotalCuft += cuft;
  1173. TotalWeight += weight;
  1174. TotalCarton += carton;
  1175. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1176. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1177. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1178. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1179. itemjson[@"note"]=nsline_note;
  1180. itemjson[@"origin_price"] = Price;
  1181. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1182. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1183. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1184. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1185. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1186. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1187. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1188. if(itemjson[@"combine"] != nil)
  1189. {
  1190. // int citem=0;
  1191. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1192. for(int bc=0;bc<bcount;bc++)
  1193. {
  1194. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1195. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1196. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1197. subTotal += uprice * modulus * item_count;
  1198. }
  1199. }
  1200. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1201. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1202. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1203. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1204. allItemPrice += subTotal;
  1205. }];
  1206. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1207. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1208. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1209. // payments/Credits
  1210. // payments_and_credist = sqlite3_column_double(statement, 34);
  1211. payments_and_credist = allItemPrice;
  1212. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1213. // // total
  1214. // totalPrice = sqlite3_column_double(statement, 35);
  1215. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1216. } else {
  1217. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1218. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1219. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1220. // payments/Credits
  1221. payments_and_credist = 0;
  1222. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1223. }
  1224. // total
  1225. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1226. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1227. ret[@"order_info"]= orderinfo;
  1228. ret[@"more_order_info"] = moreInfo;
  1229. return [RAUtils dict2data:ret];
  1230. */
  1231. DebugLog(@"debug sales order data:%@", [RAUtils dict2string:data]);
  1232. return data;
  1233. }
  1234. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1235. {
  1236. NSString* template_file=nil;
  1237. switch([params[@"thumb"] intValue])
  1238. {
  1239. case 0:
  1240. template_file= @"so.json";
  1241. break;
  1242. case 1:
  1243. template_file= @"so_thumb.json";
  1244. break;
  1245. default:
  1246. template_file= @"so.json";
  1247. }
  1248. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] type:[params[@"thumb"] intValue]];
  1249. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1250. if(data[@"table_model"][@"row2"]==nil)
  1251. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1252. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1253. DebugLog(@"%@",file);
  1254. // return nil;
  1255. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1256. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1257. if (file) {
  1258. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1259. [dic setObject:file forKey:@"pdf_path"];
  1260. dic[@"isLocalFile"]=@"true";
  1261. return [RAUtils dict2data:dic];
  1262. }
  1263. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1264. dic[@"isLocalFile"]=@"true";
  1265. return [RAUtils dict2data:dic];
  1266. }
  1267. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1268. {
  1269. NSString* template_file=nil;
  1270. switch([params[@"pdf_style"] intValue])
  1271. {
  1272. case 0:
  1273. template_file= @"portfolio_2x3.json";
  1274. break;
  1275. case 1:
  1276. template_file= @"portfolio_3x2.json";
  1277. break;
  1278. default:
  1279. template_file= @"portfolio_3x2.json";
  1280. }
  1281. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1282. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1283. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1284. DebugLog(@"%@",file);
  1285. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1286. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1287. if (file) {
  1288. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1289. [dic setObject:file forKey:@"pdf_path"];
  1290. dic[@"isLocalFile"]=@"true";
  1291. return [RAUtils dict2data:dic];
  1292. }
  1293. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1294. dic[@"isLocalFile"]=@"true";
  1295. return [RAUtils dict2data:dic];
  1296. }
  1297. +(NSString*) get_offline_soid:(sqlite3*)db
  1298. {
  1299. NSString* soid=nil;
  1300. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1301. [formatter setDateFormat:@"yyMMdd"];
  1302. NSString* date = [formatter stringFromDate:[NSDate date]];
  1303. for(int i=1;i<999;i++)
  1304. {
  1305. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1306. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1307. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1308. if(count==0)
  1309. return soid;
  1310. }
  1311. return nil;
  1312. }
  1313. +(NSArray*) enumOfflineOrder
  1314. {
  1315. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1316. sqlite3 *db = [iSalesDB get_db];
  1317. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1318. sqlite3_stmt * statement;
  1319. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1320. if ( dbresult== SQLITE_OK)
  1321. {
  1322. while (sqlite3_step(statement) == SQLITE_ROW)
  1323. {
  1324. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1325. NSString* nsso_id=nil;
  1326. if(so_id!=nil)
  1327. {
  1328. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1329. }
  1330. [ret addObject:nsso_id];
  1331. }
  1332. sqlite3_finalize(statement);
  1333. }
  1334. [iSalesDB close_db:db];
  1335. return ret;
  1336. }
  1337. //+(void) uploadFile:(NSString*) file
  1338. //{
  1339. // NSData* data = [NSData dataWithContentsOfFile: file];
  1340. // UIApplication * app = [UIApplication sharedApplication];
  1341. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1342. //
  1343. //
  1344. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1345. //
  1346. //
  1347. //
  1348. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1349. //
  1350. // if(appDelegate.user!=nil)
  1351. // [params setValue:appDelegate.user forKey:@"user"];
  1352. // // if(appDelegate.contact_id!=nil)
  1353. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1354. // if(appDelegate.password!=nil)
  1355. // [params setValue:appDelegate.password forKey:@"password"];
  1356. //
  1357. //
  1358. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1359. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1360. // } error:nil];
  1361. //
  1362. //
  1363. //
  1364. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1365. //
  1366. // NSProgress *progress = nil;
  1367. //
  1368. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1369. //
  1370. //
  1371. //
  1372. // if (error) {
  1373. //
  1374. // NSString* err_msg = [error localizedDescription];
  1375. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1376. //
  1377. //
  1378. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1379. //
  1380. // DebugLog(@"data string: %@",str);
  1381. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1382. //
  1383. // } else {
  1384. // DebugLog(@"response ");
  1385. //
  1386. //
  1387. //
  1388. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1389. //
  1390. // // 再将NSData转为字符串
  1391. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1392. //
  1393. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1394. //
  1395. // DebugLog(@"data string: %@",jsonStr);
  1396. //
  1397. // NSDictionary* json = responseObject;
  1398. //
  1399. //
  1400. // if([[json valueForKey:@"result"] intValue]==2)
  1401. // {
  1402. //// NSString* img_url_down = json[@"img_url_aname"];
  1403. //// NSString* img_url_up = json[@"img_url"];
  1404. //
  1405. //
  1406. // }
  1407. // else
  1408. // {
  1409. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1410. // }
  1411. //
  1412. //
  1413. // }
  1414. // // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1415. // }];
  1416. //
  1417. //
  1418. //
  1419. //
  1420. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1421. // //
  1422. //
  1423. //
  1424. //
  1425. //
  1426. //
  1427. //
  1428. // [uploadTask resume];
  1429. //
  1430. //}
  1431. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  1432. {
  1433. NSString* serial= [[NSUUID UUID] UUIDString];
  1434. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1435. NSString *cachefolder = [paths objectAtIndex:0];
  1436. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1437. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1438. NSFileManager* fileManager = [NSFileManager defaultManager];
  1439. BOOL bdir=YES;
  1440. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  1441. {
  1442. NSError *error = nil;
  1443. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  1444. if(!bsuccess)
  1445. DebugLog(@"Create UPLOAD folder failed");
  1446. }
  1447. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1448. ret[@"contact"]=[self prepareContact:serial];
  1449. ret[@"wishlist"]=[self prepareWishlist:serial];
  1450. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  1451. ret[@"portfolio"] = [self preparePortfolio:serial];
  1452. ret[@"view_portfolio"] = [self preparePDF:serial];
  1453. NSString* str= [RAUtils dict2string:ret];
  1454. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  1455. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  1456. NSError *error=nil;
  1457. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  1458. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  1459. ZipArchive* zip = [[ZipArchive alloc] init];
  1460. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  1461. for(int i=0;i<arr_files.count;i++)
  1462. {
  1463. NSString* file=arr_files[i];
  1464. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  1465. }
  1466. if( ![zip CloseZipFile2] )
  1467. {
  1468. zippath = @"";
  1469. }
  1470. ret[@"file"]=zippath;
  1471. return ret;
  1472. }
  1473. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  1474. {
  1475. if(filename.length==0)
  1476. return false;
  1477. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1478. bool ret=false;
  1479. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1480. NSString *cachefolder = [paths objectAtIndex:0];
  1481. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1482. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1483. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1484. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1485. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1486. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  1487. NSFileManager* fileManager = [NSFileManager defaultManager];
  1488. BOOL bdir=NO;
  1489. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1490. {
  1491. NSError *error = nil;
  1492. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1493. {
  1494. ret=false;
  1495. }
  1496. else
  1497. {
  1498. ret=true;
  1499. }
  1500. }
  1501. return ret;
  1502. }
  1503. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  1504. {
  1505. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1506. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  1507. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  1508. NSString* where=@" 1=1";
  1509. if (ver!=nil) {
  1510. where=@"is_dirty=1";
  1511. }
  1512. sqlite3 *db = [iSalesDB get_db];
  1513. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  1514. sqlite3_stmt * statement;
  1515. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1516. int count=0;
  1517. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1518. if ( dbresult== SQLITE_OK)
  1519. {
  1520. while (sqlite3_step(statement) == SQLITE_ROW)
  1521. {
  1522. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1523. int _id = sqlite3_column_int(statement, 0);
  1524. int product_id = sqlite3_column_int(statement, 1);
  1525. int item_id = sqlite3_column_int(statement, 2);
  1526. int qty = sqlite3_column_int(statement, 3);
  1527. char *create_time = (char*)sqlite3_column_text(statement, 4);
  1528. NSString* nscreate_time=nil;
  1529. if(create_time!=nil)
  1530. {
  1531. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  1532. }
  1533. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  1534. NSString* nsmodify_time=nil;
  1535. if(modify_time!=nil)
  1536. {
  1537. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1538. }
  1539. int is_delete = sqlite3_column_int(statement, 6);
  1540. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1541. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1542. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1543. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1544. itemjson[@"create_time"]=nscreate_time;
  1545. itemjson[@"modify_time"]=nsmodify_time;
  1546. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1547. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1548. count++;
  1549. }
  1550. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1551. sqlite3_finalize(statement);
  1552. }
  1553. ret[@"count"]=[NSNumber numberWithInt:count ];
  1554. [iSalesDB close_db:db];
  1555. return ret;
  1556. }
  1557. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  1558. {
  1559. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1560. UIApplication * app = [UIApplication sharedApplication];
  1561. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1562. NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id,o.customer_cid from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,so_no from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id left join offline_order as o on c.so_no = o.so_id;",orderCode ];
  1563. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  1564. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1565. DebugLog(@"offline_login sql:%@",sqlQuery);
  1566. sqlite3_stmt * statement;
  1567. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1568. int count=0;
  1569. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1570. if ( dbresult== SQLITE_OK)
  1571. {
  1572. while (sqlite3_step(statement) == SQLITE_ROW)
  1573. {
  1574. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1575. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  1576. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  1577. int product_id = sqlite3_column_int(statement, 0);
  1578. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1579. int item_id = sqlite3_column_int(statement, 7);
  1580. NSString* Price=nil;
  1581. if(str_price==nil)
  1582. {
  1583. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1584. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  1585. if(price==nil)
  1586. Price=@"No Price.";
  1587. else
  1588. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1589. }
  1590. else
  1591. {
  1592. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1593. }
  1594. double discount = sqlite3_column_double(statement, 2);
  1595. int item_count = sqlite3_column_int(statement, 3);
  1596. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1597. NSString *nsline_note=nil;
  1598. if(line_note!=nil)
  1599. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1600. char *name = (char*)sqlite3_column_text(statement, 5);
  1601. NSString *nsname=nil;
  1602. if(name!=nil)
  1603. nsname= [[NSString alloc]initWithUTF8String:name];
  1604. char *description = (char*)sqlite3_column_text(statement, 6);
  1605. NSString *nsdescription=nil;
  1606. if(description!=nil)
  1607. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1608. // int stockUom = sqlite3_column_int(statement, 8);
  1609. // int _id = sqlite3_column_int(statement, 9);
  1610. //
  1611. //
  1612. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1613. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1614. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1615. // int carton=[bsubtotaljson[@"carton"] intValue];
  1616. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1617. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1618. itemjson[@"model"]=nsname;
  1619. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1620. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1621. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1622. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1623. // itemjson[@"check"]=@"true";
  1624. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1625. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1626. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1627. itemjson[@"unit_price"]=Price;
  1628. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1629. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1630. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1631. itemjson[@"note"]=nsline_note;
  1632. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  1633. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1634. count++;
  1635. }
  1636. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1637. sqlite3_finalize(statement);
  1638. }
  1639. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1640. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1641. return ret;
  1642. }
  1643. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  1644. {
  1645. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1646. sqlite3 *db = [iSalesDB get_db];
  1647. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  1648. // for(int i=0;i<arr_soid.count;i++)
  1649. sqlite3_stmt * statement;
  1650. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1651. //int count=0;
  1652. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1653. if ( dbresult== SQLITE_OK)
  1654. {
  1655. int count=0;
  1656. while (sqlite3_step(statement) == SQLITE_ROW)
  1657. {
  1658. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1659. int _id = sqlite3_column_int(statement, 0);
  1660. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  1661. NSString* nssync_data=nil;
  1662. if(sync_data!=nil)
  1663. {
  1664. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1665. }
  1666. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1667. NSString* nsimg_1=nil;
  1668. if(img_1!=nil)
  1669. {
  1670. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1671. [self copy_upImg:serial file:nsimg_1];
  1672. }
  1673. char *so_no = (char*)sqlite3_column_text(statement, 3);
  1674. NSString* nsso_no=nil;
  1675. if(so_no!=nil)
  1676. {
  1677. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  1678. }
  1679. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  1680. itemjson[@"order_type"]=@"submit order";
  1681. else
  1682. itemjson[@"order_type"]=@"archive order";
  1683. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1684. itemjson[@"json_data"]= nssync_data;
  1685. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  1686. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1687. count++;
  1688. }
  1689. ret[@"count"]=[NSNumber numberWithInt:count ];
  1690. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1691. }
  1692. sqlite3_finalize(statement);
  1693. [iSalesDB close_db:db];
  1694. return ret;
  1695. }
  1696. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  1697. {
  1698. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1699. sqlite3 *db = [iSalesDB get_db];
  1700. // UIApplication * app = [UIApplication sharedApplication];
  1701. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1702. NSString *sqlQuery =@"select _id,img_0,img_1,img_2,sync_data from offline_contact where is_active=1 and not sync_data is null";
  1703. sqlite3_stmt * statement;
  1704. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1705. int count=0;
  1706. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1707. if ( dbresult== SQLITE_OK)
  1708. {
  1709. while (sqlite3_step(statement) == SQLITE_ROW)
  1710. {
  1711. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1712. int _id = sqlite3_column_int(statement, 0);
  1713. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  1714. NSString* nsimg_0=nil;
  1715. if(img_0!=nil)
  1716. {
  1717. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1718. [self copy_upImg:serial file:nsimg_0];
  1719. }
  1720. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1721. NSString* nsimg_1=nil;
  1722. if(img_1!=nil)
  1723. {
  1724. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1725. [self copy_upImg:serial file:nsimg_1];
  1726. }
  1727. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  1728. NSString* nsimg_2=nil;
  1729. if(img_2!=nil)
  1730. {
  1731. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1732. [self copy_upImg:serial file:nsimg_2];
  1733. }
  1734. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  1735. NSString* nssync_data=nil;
  1736. if(sync_data!=nil)
  1737. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1738. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1739. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1740. itemjson[@"json_data"]= nssync_data;
  1741. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1742. count++;
  1743. }
  1744. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1745. sqlite3_finalize(statement);
  1746. }
  1747. ret[@"count"]=[NSNumber numberWithInt:count ];
  1748. [iSalesDB close_db:db];
  1749. return ret;
  1750. }
  1751. +(bool) check_offlinedata
  1752. {
  1753. UIApplication * app = [UIApplication sharedApplication];
  1754. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1755. sqlite3 *db = [iSalesDB get_db];
  1756. NSString * where=@"1=1";
  1757. // if(appDelegate.user!=nil)
  1758. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  1759. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  1760. [iSalesDB close_db:db];
  1761. if(count==0)
  1762. {
  1763. return false;
  1764. }
  1765. return true;
  1766. //
  1767. //[iSalesDB close_db:db];
  1768. }
  1769. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  1770. {
  1771. UIApplication * app = [UIApplication sharedApplication];
  1772. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1773. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1774. sqlite3 *db = [iSalesDB get_db];
  1775. NSString* collectId=params[@"collectId"];
  1776. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  1777. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  1778. [iSalesDB execSql:sqlQuery db:db];
  1779. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1780. [iSalesDB close_db:db];
  1781. appDelegate.wish_count =count;
  1782. [appDelegate update_count_mark];
  1783. ret[@"result"]= [NSNumber numberWithInt:2];
  1784. return ret;
  1785. }
  1786. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  1787. //{
  1788. //
  1789. // UIApplication * app = [UIApplication sharedApplication];
  1790. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1791. //
  1792. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1793. // sqlite3 *db = [iSalesDB get_db];
  1794. // NSString* product_id=params[@"product_id"];
  1795. //
  1796. //
  1797. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  1798. //
  1799. //
  1800. // for(int i=0;i<arr.count;i++)
  1801. // {
  1802. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  1803. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  1804. // if(count==0)
  1805. // {
  1806. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  1807. // [iSalesDB execSql:sqlQuery db:db];
  1808. // }
  1809. // }
  1810. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  1811. // [iSalesDB close_db:db];
  1812. //
  1813. // appDelegate.wish_count =count;
  1814. //
  1815. // [appDelegate update_count_mark];
  1816. // ret[@"result"]= [NSNumber numberWithInt:2];
  1817. // return ret;
  1818. // //
  1819. // //return ret;
  1820. //}
  1821. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  1822. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  1823. DebugLog(@"time interval: %lf",interval);
  1824. }
  1825. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  1826. {
  1827. UIApplication * app = [UIApplication sharedApplication];
  1828. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1829. int sort = [[params objectForKey:@"sort"] intValue];
  1830. NSString *sort_str = @"";
  1831. switch (sort) {
  1832. case 0:{
  1833. sort_str = @"order by w.modify_time desc";
  1834. }
  1835. break;
  1836. case 1:{
  1837. sort_str = @"order by w.modify_time asc";
  1838. }
  1839. break;
  1840. case 2:{
  1841. sort_str = @"order by m.name asc";
  1842. }
  1843. break;
  1844. case 3:{
  1845. sort_str = @"order by m.name desc";
  1846. }
  1847. break;
  1848. case 4:{
  1849. sort_str = @"order by m.description asc";
  1850. }
  1851. break;
  1852. default:
  1853. break;
  1854. }
  1855. NSString* user = appDelegate.user;
  1856. sqlite3 *db = [iSalesDB get_db];
  1857. // order by w.create_time
  1858. NSString* sqlQuery = [NSString stringWithFormat:@"select w.product_id,m.name||'\n'||m.description,w._id,w.qty from wishlist as w left join product as m on w.product_id=m.product_id where w.is_delete is NULL or w.is_delete != 1 %@ ",sort_str];
  1859. // NSString* sqlQuery = [NSString stringWithFormat:@"select m.product_id,(m.name||'\n'||m.description) as des,w._id from product as m,wishlist as w where m.product_id = w.product_id %@",sort_str];
  1860. sqlite3_stmt * statement;
  1861. NSDate *date1 = [NSDate date];
  1862. NSDate *date2 = nil;
  1863. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1864. int count=0;
  1865. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1866. {
  1867. while (sqlite3_step(statement) == SQLITE_ROW)
  1868. {
  1869. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1870. int product_id = sqlite3_column_double(statement, 0);
  1871. char *description = (char*)sqlite3_column_text(statement, 1);
  1872. if(description==nil)
  1873. description= "";
  1874. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  1875. int item_id = sqlite3_column_double(statement, 2);
  1876. NSDate *date_image = [NSDate date];
  1877. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  1878. printf("image : ");
  1879. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  1880. // char *url = (char*)sqlite3_column_text(statement, 3);
  1881. // if(url==nil)
  1882. // url="";
  1883. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1884. int qty = sqlite3_column_int(statement, 3);
  1885. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  1886. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  1887. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  1888. item[@"description"]= nsdescription;
  1889. item[@"img"]= nsurl;
  1890. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1891. count++;
  1892. }
  1893. printf("total time:");
  1894. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1895. ret[@"count"]= [NSNumber numberWithInt:count];
  1896. ret[@"total_count"]= [NSNumber numberWithInt:count];
  1897. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  1898. ret[@"result"]= [NSNumber numberWithInt:2];
  1899. appDelegate.wish_count =count;
  1900. [appDelegate update_count_mark];
  1901. sqlite3_finalize(statement);
  1902. }
  1903. [iSalesDB close_db:db];
  1904. return ret;
  1905. }
  1906. +(NSDictionary*) offline_notimpl
  1907. {
  1908. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1909. ret[@"result"]=@"8";
  1910. ret[@"err_msg"]=@"offline mode does not support this function.";
  1911. return ret;
  1912. }
  1913. +(NSDictionary*) offline_home
  1914. {
  1915. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1916. NSString *cachefolder = [paths objectAtIndex:0];
  1917. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  1918. NSData* json =nil;
  1919. json=[NSData dataWithContentsOfFile:img_cache];
  1920. if(json==nil)
  1921. return nil;
  1922. NSError *error=nil;
  1923. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1924. return menu;
  1925. }
  1926. +(NSDictionary*) offline_category_menu
  1927. {
  1928. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1929. NSString *cachefolder = [paths objectAtIndex:0];
  1930. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  1931. NSData* json =nil;
  1932. json=[NSData dataWithContentsOfFile:img_cache];
  1933. if(json==nil)
  1934. return nil;
  1935. NSError *error=nil;
  1936. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1937. return menu;
  1938. }
  1939. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  1940. {
  1941. NSString* offline_command=params[@"offline_Command"];
  1942. NSDictionary* ret=nil;
  1943. if([offline_command isEqualToString:@"model_NIYMAL"])
  1944. {
  1945. NSString* category = params[@"category"];
  1946. ret = [self refresh_model_NIYMAL:category];
  1947. }
  1948. return ret;
  1949. }
  1950. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  1951. {
  1952. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1953. [ret setValue:@"2" forKey:@"result"];
  1954. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1955. sqlite3* db= [iSalesDB get_db];
  1956. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1957. [iSalesDB close_db:db];
  1958. [ret setObject:detail1_section forKey:@"detail_1"];
  1959. return ret;
  1960. }
  1961. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  1962. {
  1963. UIApplication * app = [UIApplication sharedApplication];
  1964. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1965. // NSArray* arr1 = [self get_user_all_price_type];
  1966. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1967. // NSSet *set1 = [NSSet setWithArray:arr1];
  1968. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1969. // if(appDelegate.contact_id==nil)
  1970. // set2=[set1 mutableCopy];
  1971. // else
  1972. // [set2 intersectsSet:set1];
  1973. // NSArray *retarr = [set2 allObjects];
  1974. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1975. // sqlite3 *db = [iSalesDB get_db];
  1976. NSString* sqlQuery = nil;
  1977. if(appDelegate.contact_id==nil)
  1978. {
  1979. if(!appDelegate.bLogin)
  1980. return nil;
  1981. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type from price as a left join model_price as b on a.name=b.price_name and b.item_id=%d order by a.order_by;",item_id];
  1982. }
  1983. else
  1984. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type from price as a left join model_price as b on a.name=b.price_name and b.item_id=%d and b.price_name in(%@) order by a.order_by;",item_id,whereprice];
  1985. sqlite3_stmt * statement;
  1986. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1987. int count=0;
  1988. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1989. {
  1990. while (sqlite3_step(statement) == SQLITE_ROW)
  1991. {
  1992. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1993. char *name = (char*)sqlite3_column_text(statement, 0);
  1994. if(name==nil)
  1995. name="";
  1996. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1997. // double price = sqlite3_column_double(statement, 1);
  1998. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  1999. // if(isnull==nil)
  2000. // item[nsname]= @"No Price";
  2001. // else
  2002. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  2003. char *price = (char*)sqlite3_column_text(statement, 1);
  2004. if(price!=nil)
  2005. {
  2006. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2007. nsprice=[AESCrypt fastdecrypt:nsprice];
  2008. if(nsprice.length>0)
  2009. {
  2010. double dp= [nsprice doubleValue];
  2011. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2012. }
  2013. }
  2014. else
  2015. {
  2016. item[nsname]= @"No Price";
  2017. }
  2018. // int type= sqlite3_column_int(statement, 2);
  2019. //item[@"type"]=@"price";
  2020. // item[nsname]= nsprice;
  2021. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2022. count++;
  2023. }
  2024. ret[@"count"]= [NSNumber numberWithInt:count];
  2025. sqlite3_finalize(statement);
  2026. }
  2027. // [iSalesDB close_db:db];
  2028. return ret;
  2029. }
  2030. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2031. //{
  2032. // NSArray* arr1 = [self get_user_all_price_type:db];
  2033. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2034. //
  2035. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2036. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2037. // // [set2 intersectsSet:set1];
  2038. // //
  2039. // //
  2040. // // NSArray *retarr = [set2 allObjects];
  2041. //
  2042. // NSString* whereprice=nil;
  2043. // if(contact_id==nil)
  2044. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2045. // else
  2046. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2047. //
  2048. //
  2049. // // sqlite3 *db = [iSalesDB get_db];
  2050. //
  2051. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2052. // sqlite3_stmt * statement;
  2053. //
  2054. //
  2055. // NSNumber* ret = nil;
  2056. // double dprice=DBL_MAX;
  2057. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2058. // {
  2059. //
  2060. //
  2061. // while (sqlite3_step(statement) == SQLITE_ROW)
  2062. // {
  2063. //
  2064. // // double val = sqlite3_column_double(statement, 0);
  2065. // char *price = (char*)sqlite3_column_text(statement, 0);
  2066. // if(price!=nil)
  2067. // {
  2068. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2069. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2070. // if(nsprice.length>0)
  2071. // {
  2072. // double dp= [nsprice doubleValue];
  2073. // if(dp<dprice)
  2074. // dprice=dp;
  2075. // }
  2076. // }
  2077. // }
  2078. //
  2079. //
  2080. //
  2081. //
  2082. // sqlite3_finalize(statement);
  2083. //
  2084. //
  2085. //
  2086. //
  2087. // }
  2088. //
  2089. // // [iSalesDB close_db:db];
  2090. //
  2091. // if(dprice==DBL_MAX)
  2092. // ret= nil;
  2093. // else
  2094. // ret= [NSNumber numberWithDouble:dprice];
  2095. // return ret;
  2096. //}
  2097. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2098. {
  2099. NSArray* arr1 = [self get_user_all_price_type:db];
  2100. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2101. NSString* whereprice=nil;
  2102. if(contact_id==nil)
  2103. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2104. else
  2105. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2106. // sqlite3 *db = [iSalesDB get_db];
  2107. NSString *productIdCondition = @"1 = 1";
  2108. if (product_id) {
  2109. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2110. }
  2111. NSString *itemIdCondition = @"";
  2112. if (item_id) {
  2113. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2114. }
  2115. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2116. sqlite3_stmt * statement;
  2117. NSNumber* ret = nil;
  2118. double dprice=DBL_MAX;
  2119. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2120. {
  2121. while (sqlite3_step(statement) == SQLITE_ROW)
  2122. {
  2123. // double val = sqlite3_column_double(statement, 0);
  2124. char *price = (char*)sqlite3_column_text(statement, 0);
  2125. if(price!=nil)
  2126. {
  2127. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2128. nsprice=[AESCrypt fastdecrypt:nsprice];
  2129. if(nsprice.length>0)
  2130. {
  2131. double dp= [nsprice doubleValue];
  2132. if(dp<dprice)
  2133. dprice=dp;
  2134. }
  2135. }
  2136. }
  2137. sqlite3_finalize(statement);
  2138. }
  2139. // [iSalesDB close_db:db];
  2140. if(dprice==DBL_MAX)
  2141. ret= nil;
  2142. else
  2143. ret= [NSNumber numberWithDouble:dprice];
  2144. return ret;
  2145. }
  2146. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  2147. {
  2148. NSArray* ret=nil;
  2149. // sqlite3 *db = [iSalesDB get_db];
  2150. // no customer assigned , use login user contact_id
  2151. UIApplication * app = [UIApplication sharedApplication];
  2152. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2153. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  2154. sqlite3_stmt * statement;
  2155. // int count=0;
  2156. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2157. {
  2158. if (sqlite3_step(statement) == SQLITE_ROW)
  2159. {
  2160. char *val = (char*)sqlite3_column_text(statement, 0);
  2161. if(val==nil)
  2162. val="";
  2163. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2164. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2165. }
  2166. sqlite3_finalize(statement);
  2167. }
  2168. // [iSalesDB close_db:db];
  2169. return ret;
  2170. }
  2171. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  2172. {
  2173. // sqlite3 *db = [iSalesDB get_db];
  2174. if(contact_id==nil)
  2175. {
  2176. // no customer assigned , use login user contact_id
  2177. UIApplication * app = [UIApplication sharedApplication];
  2178. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2179. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  2180. sqlite3_stmt * statement;
  2181. // int count=0;
  2182. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2183. {
  2184. if (sqlite3_step(statement) == SQLITE_ROW)
  2185. {
  2186. char *val = (char*)sqlite3_column_text(statement, 0);
  2187. if(val==nil)
  2188. val="";
  2189. contact_id = [[NSString alloc]initWithUTF8String:val];
  2190. }
  2191. sqlite3_finalize(statement);
  2192. }
  2193. if(contact_id.length<=0)
  2194. {
  2195. // [iSalesDB close_db:db];
  2196. return nil;
  2197. }
  2198. }
  2199. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2200. sqlite3_stmt * statement;
  2201. NSArray* ret=nil;
  2202. // int count=0;
  2203. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2204. {
  2205. if (sqlite3_step(statement) == SQLITE_ROW)
  2206. {
  2207. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2208. if(price_type==nil)
  2209. price_type="";
  2210. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2211. if(nsprice_type.length>0)
  2212. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2213. else
  2214. ret=nil;
  2215. }
  2216. sqlite3_finalize(statement);
  2217. }
  2218. // [iSalesDB close_db:db];
  2219. return ret;
  2220. }
  2221. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2222. {
  2223. NSString* ret= nil;
  2224. // sqlite3 *db = [iSalesDB get_db];
  2225. NSString *sqlQuery = nil;
  2226. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2227. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;",model_name];// select i.url from model m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;
  2228. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.product_id=%@ order by i.default_img desc, i._id asc limit 1;",product_id];// select i.url from model m
  2229. if(product_id==nil && model_name)
  2230. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image i where product_id in (select product_id from product where name='%@') order by i.default_img desc, i._id asc limit 1;",model_name];// select i.url from model m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;
  2231. else if (product_id)
  2232. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image i where i.product_id=%@ order by i.default_img desc, i._id asc limit 1;",product_id];// select i.url from model m
  2233. sqlite3_stmt * statement;
  2234. // int count=0;
  2235. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2236. {
  2237. if (sqlite3_step(statement) == SQLITE_ROW)
  2238. {
  2239. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2240. if(imgurl==nil)
  2241. imgurl="";
  2242. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2243. ret=nsimgurl;
  2244. }
  2245. sqlite3_finalize(statement);
  2246. }
  2247. else
  2248. {
  2249. [ret setValue:@"8" forKey:@"result"];
  2250. }
  2251. // [iSalesDB close_db:db];
  2252. DebugLog(@"data string: %@",ret );
  2253. return ret;
  2254. }
  2255. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  2256. {
  2257. NSString* ret= nil;
  2258. sqlite3 *db = [iSalesDB get_db];
  2259. NSString *sqlQuery = nil;
  2260. if(product_id==nil)
  2261. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2262. else
  2263. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2264. sqlite3_stmt * statement;
  2265. // int count=0;
  2266. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2267. {
  2268. if (sqlite3_step(statement) == SQLITE_ROW)
  2269. {
  2270. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2271. if(default_category==nil)
  2272. default_category="";
  2273. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2274. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2275. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2276. ret=nsdefault_category;
  2277. }
  2278. sqlite3_finalize(statement);
  2279. }
  2280. else
  2281. {
  2282. [ret setValue:@"8" forKey:@"result"];
  2283. }
  2284. [iSalesDB close_db:db];
  2285. DebugLog(@"data string: %@",ret );
  2286. return ret;
  2287. }
  2288. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2289. {
  2290. NSString* model_name = [params valueForKey:@"product_name"];
  2291. NSString* product_id = [params valueForKey:@"product_id"];
  2292. NSString* category = [params valueForKey:@"category"];
  2293. if(category==nil)
  2294. category = [self model_default_category:product_id model_name:model_name];
  2295. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2296. sqlite3 *db = [iSalesDB get_db];
  2297. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2298. NSString *sqlQuery = nil;
  2299. if(product_id==nil)
  2300. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging,ETA,item_id,seat_dimension,product_content_writing from product where name='%@';",model_name]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where name='%@';
  2301. else
  2302. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging,ETA,item_id,seat_dimension,product_content_writing from product where product_id=%@;",product_id]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where product_id=%@;
  2303. sqlite3_stmt * statement;
  2304. [ret setValue:@"2" forKey:@"result"];
  2305. [ret setValue:@"3" forKey:@"detail_section_count"];
  2306. // int count=0;
  2307. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2308. {
  2309. if (sqlite3_step(statement) == SQLITE_ROW)
  2310. {
  2311. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2312. char *name = (char*)sqlite3_column_text(statement, 0);
  2313. if(name==nil)
  2314. name="";
  2315. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2316. char *description = (char*)sqlite3_column_text(statement, 1);
  2317. if(description==nil)
  2318. description="";
  2319. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2320. int product_id = sqlite3_column_int(statement, 2);
  2321. char *color = (char*)sqlite3_column_text(statement, 3);
  2322. if(color==nil)
  2323. color="";
  2324. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  2325. //
  2326. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  2327. // if(legcolor==nil)
  2328. // legcolor="";
  2329. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  2330. //
  2331. //
  2332. int availability = sqlite3_column_int(statement, 5);
  2333. //
  2334. int incoming_stock = sqlite3_column_int(statement, 6);
  2335. char *demension = (char*)sqlite3_column_text(statement, 7);
  2336. if(demension==nil)
  2337. demension="";
  2338. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  2339. // ,,,,,,,,,
  2340. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  2341. if(seat_height==nil)
  2342. seat_height="";
  2343. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  2344. char *material = (char*)sqlite3_column_text(statement, 9);
  2345. if(material==nil)
  2346. material="";
  2347. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  2348. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  2349. if(box_dim==nil)
  2350. box_dim="";
  2351. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  2352. char *volume = (char*)sqlite3_column_text(statement, 11);
  2353. if(volume==nil)
  2354. volume="";
  2355. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  2356. double weight = sqlite3_column_double(statement, 12);
  2357. char *model_set = (char*)sqlite3_column_text(statement, 13);
  2358. if(model_set==nil)
  2359. model_set="";
  2360. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  2361. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  2362. if(load_ability==nil)
  2363. load_ability="";
  2364. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  2365. char *default_category = (char*)sqlite3_column_text(statement, 15);
  2366. if(default_category==nil)
  2367. default_category="";
  2368. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2369. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  2370. if(fabric_content==nil)
  2371. fabric_content="";
  2372. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  2373. char *assembling = (char*)sqlite3_column_text(statement, 17);
  2374. if(assembling==nil)
  2375. assembling="";
  2376. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  2377. char *made_in = (char*)sqlite3_column_text(statement, 18);
  2378. if(made_in==nil)
  2379. made_in="";
  2380. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  2381. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  2382. if(special_remarks==nil)
  2383. special_remarks="";
  2384. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  2385. int stockUcom = sqlite3_column_double(statement, 20);
  2386. char *product_group = (char*)sqlite3_column_text(statement, 21);
  2387. if(product_group==nil)
  2388. product_group="";
  2389. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  2390. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  2391. // if(fashion_selector==nil)
  2392. // fashion_selector="";
  2393. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  2394. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  2395. if(selector_field==nil)
  2396. selector_field="";
  2397. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  2398. char *property_field = (char*)sqlite3_column_text(statement, 23);
  2399. if(property_field==nil)
  2400. property_field="";
  2401. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  2402. char *packaging = (char*)sqlite3_column_text(statement, 24);
  2403. if(packaging==nil)
  2404. packaging="";
  2405. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  2406. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2407. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  2408. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  2409. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  2410. [img_section setValue:model_s_img forKey:@"model_s_img"];
  2411. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  2412. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  2413. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  2414. NSString* Availability=nil;
  2415. if(availability>0)
  2416. Availability=[NSString stringWithFormat:@"%d",availability];
  2417. else
  2418. Availability = @"Out of Stock";
  2419. [img_section setValue:Availability forKey:@"Availability"];
  2420. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  2421. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  2422. char *eta = (char*)sqlite3_column_text(statement, 25);
  2423. if(eta==nil)
  2424. eta="";
  2425. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  2426. if ([nseta isEqualToString:@"null"]) {
  2427. nseta = @"";
  2428. }
  2429. if (availability <= 0) {
  2430. [img_section setValue:nseta forKey:@"ETA"];
  2431. }
  2432. int item_id = sqlite3_column_int(statement, 26);
  2433. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  2434. NSString *content_writing = [self textAtColumn:28 statement:statement];
  2435. NSString* Price=nil;
  2436. if(appDelegate.bLogin==false)
  2437. Price=@"Must Sign in.";
  2438. else
  2439. {
  2440. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2441. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2442. if(price==nil)
  2443. Price=@"No Price.";
  2444. else
  2445. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2446. }
  2447. [img_section setObject:content_writing forKey:@"product_content_writing"];
  2448. [img_section setValue:Price forKey:@"price"];
  2449. [img_section setValue:nsname forKey:@"model_name"];
  2450. [img_section setValue:nsdescription forKey:@"model_descrition"];
  2451. if (appDelegate.order_code) { // 离线order code即so#
  2452. NSString *cartQtySql = [NSString stringWithFormat:@"select c.item_count from offline_cart as c left join offline_order as o on c.so_no = o.so_id where o.so_id = '%@' and c.item_id = %d;",appDelegate.order_code,item_id];
  2453. __block int cartQTY = 0;
  2454. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2455. cartQTY = sqlite3_column_int(stmt, 0);
  2456. }];
  2457. if (cartQTY > 0) {
  2458. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  2459. }
  2460. }
  2461. [ret setObject:img_section forKey:@"img_section"];
  2462. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  2463. int detail0_item_count=0;
  2464. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2465. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2466. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2467. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2468. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2469. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2470. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2471. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2472. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2473. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2474. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2475. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2476. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2477. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2478. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  2479. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  2480. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  2481. {
  2482. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  2483. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2484. }
  2485. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  2486. [detail0_section setValue:@"kv" forKey:@"type"];
  2487. [detail0_section setValue:@"Product Information" forKey:@"title"];
  2488. [ret setObject:detail0_section forKey:@"detail_0"];
  2489. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2490. // [detail1_section setValue:@"detail" forKey:@"target"];
  2491. // [detail1_section setValue:@"popup" forKey:@"action"];
  2492. // [detail1_section setValue:@"content" forKey:@"type"];
  2493. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  2494. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2495. // [detail1_section setValue:@"true" forKey:@"single_row"];
  2496. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  2497. [ret setObject:detail1_section forKey:@"detail_1"];
  2498. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  2499. [detail2_section setValue:@"detail" forKey:@"target"];
  2500. [detail2_section setValue:@"popup" forKey:@"action"];
  2501. [detail2_section setValue:@"content" forKey:@"type"];
  2502. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  2503. [detail2_section setValue:@"true" forKey:@"single_row"];
  2504. [detail2_section setValue:@"local" forKey:@"data"];
  2505. [ret setObject:detail2_section forKey:@"detail_2"];
  2506. }
  2507. sqlite3_finalize(statement);
  2508. }
  2509. else
  2510. {
  2511. [ret setValue:@"8" forKey:@"result"];
  2512. }
  2513. DebugLog(@"count:%d",count);
  2514. [iSalesDB close_db:db];
  2515. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2516. return ret;
  2517. }
  2518. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  2519. NSString* orderCode = [params valueForKey:@"orderCode"];
  2520. NSString* keyword = [params valueForKey:@"keyword"];
  2521. keyword=keyword.lowercaseString;
  2522. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  2523. int limit = [[params valueForKey:@"limit"] intValue];
  2524. int offset = [[params valueForKey:@"offset"] intValue];
  2525. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2526. NSString *limit_str = @"";
  2527. if (limited) {
  2528. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  2529. }
  2530. sqlite3 *db = [iSalesDB get_db];
  2531. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  2532. UIApplication * app = [UIApplication sharedApplication];
  2533. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2534. NSString *sqlQuery = nil;
  2535. if(exactMatch )
  2536. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where lower(name) like'%@%%' and m.is_active = 1 order by m.name %@;",appDelegate.order_code, keyword,limit_str]; // select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%@%%' order by m.name limit %d offset %d ;
  2537. else
  2538. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where (lower(name) like'%%%@%%' or lower(description) like'%%%@%%' or lower(made_in) like '%%%@%%' or lower(material) like '%%%@%%' or lower(packaging) like '%%%@%%') and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, keyword,keyword,keyword,keyword,keyword,limit_str];// select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%%%@%%' or
  2539. DebugLog(@"offline_search sql:%@",sqlQuery);
  2540. sqlite3_stmt * statement;
  2541. [ret setValue:@"2" forKey:@"result"];
  2542. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2543. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2544. // int count=0;
  2545. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2546. {
  2547. int i=0;
  2548. while (sqlite3_step(statement) == SQLITE_ROW)
  2549. {
  2550. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2551. // char *name = (char*)sqlite3_column_text(statement, 1);
  2552. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2553. char *name = (char*)sqlite3_column_text(statement, 0);
  2554. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2555. char *description = (char*)sqlite3_column_text(statement, 1);
  2556. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2557. int product_id = sqlite3_column_int(statement, 2);
  2558. // char *url = (char*)sqlite3_column_text(statement, 3);
  2559. // if(url==nil)
  2560. // url="";
  2561. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2562. int wid = sqlite3_column_int(statement, 3);
  2563. int closeout = sqlite3_column_int(statement, 4);
  2564. int cid = sqlite3_column_int(statement, 5);
  2565. int wisdelete = sqlite3_column_int(statement, 6);
  2566. int more_color = sqlite3_column_int(statement, 7);
  2567. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2568. if(wid !=0 && wisdelete != 1)
  2569. [item setValue:@"true" forKey:@"wish_exists"];
  2570. else
  2571. [item setValue:@"false" forKey:@"wish_exists"];
  2572. if(closeout==0)
  2573. [item setValue:@"false" forKey:@"is_closeout"];
  2574. else
  2575. [item setValue:@"true" forKey:@"is_closeout"];
  2576. if(cid==0)
  2577. [item setValue:@"false" forKey:@"cart_exists"];
  2578. else
  2579. [item setValue:@"true" forKey:@"cart_exists"];
  2580. if (more_color == 0) {
  2581. [item setObject:@(false) forKey:@"more_color"];
  2582. } else if (more_color == 1) {
  2583. [item setObject:@(true) forKey:@"more_color"];
  2584. }
  2585. [item addEntriesFromDictionary:imgjson];
  2586. // [item setValue:nsurl forKey:@"img"];
  2587. [item setValue:nsname forKey:@"fash_name"];
  2588. [item setValue:nsdescription forKey:@"description"];
  2589. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2590. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2591. i++;
  2592. }
  2593. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2594. [ret setObject:items forKey:@"items"];
  2595. sqlite3_finalize(statement);
  2596. }
  2597. DebugLog(@"count:%d",count);
  2598. [iSalesDB close_db:db];
  2599. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2600. return ret;
  2601. }
  2602. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  2603. {
  2604. return [self search:params limited:YES];
  2605. }
  2606. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  2607. {
  2608. UIApplication * app = [UIApplication sharedApplication];
  2609. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2610. appDelegate.disable_trigger=true;
  2611. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2612. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2613. appDelegate.disable_trigger=false;
  2614. //
  2615. // NSString* user = [params valueForKey:@"user"];
  2616. //
  2617. // NSString* password = [params valueForKey:@"password"];
  2618. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2619. ret[@"result"]=[NSNumber numberWithInt:2 ];
  2620. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  2621. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  2622. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  2623. [appDelegate update_count_mark];
  2624. appDelegate.can_show_price =false;
  2625. appDelegate.can_see_price =false;
  2626. appDelegate.can_create_portfolio =false;
  2627. appDelegate.can_create_order =false;
  2628. appDelegate.can_cancel_order =false;
  2629. appDelegate.can_set_cart_price =false;
  2630. appDelegate.can_delete_order =false;
  2631. appDelegate.can_submit_order =false;
  2632. appDelegate.can_set_tearsheet_price =false;
  2633. appDelegate.can_update_contact_info = false;
  2634. appDelegate.save_order_logout = false;
  2635. appDelegate.submit_order_logout = false;
  2636. appDelegate.alert_sold_in_quantities = false;
  2637. appDelegate.ipad_perm =nil ;
  2638. appDelegate.user_type = USER_ROLE_UNKNOWN;
  2639. appDelegate.OrderFilter= nil;
  2640. [appDelegate SetSo:nil];
  2641. [appDelegate set_main_button_panel];
  2642. // sqlite3 *db = [iSalesDB get_db];
  2643. //
  2644. //
  2645. //
  2646. //
  2647. //
  2648. // NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username from offline_login where lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]];
  2649. //
  2650. //
  2651. //
  2652. //
  2653. // DebugLog(@"offline_login sql:%@",sqlQuery);
  2654. // sqlite3_stmt * statement;
  2655. //
  2656. //
  2657. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2658. //
  2659. //
  2660. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2661. // {
  2662. //
  2663. //
  2664. // if (sqlite3_step(statement) == SQLITE_ROW)
  2665. // {
  2666. //
  2667. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2668. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2669. //
  2670. //
  2671. //
  2672. // int can_show_price = sqlite3_column_int(statement, 0);
  2673. // int can_see_price = sqlite3_column_int(statement, 1);
  2674. //
  2675. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2676. // if(contact_id==nil)
  2677. // contact_id="";
  2678. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2679. //
  2680. // int user_type = sqlite3_column_int(statement, 3);
  2681. //
  2682. // int can_cancel_order = sqlite3_column_int(statement, 4);
  2683. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  2684. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  2685. // int can_delete_order = sqlite3_column_int(statement, 7);
  2686. // int can_submit_order = sqlite3_column_int(statement, 8);
  2687. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2688. // int can_create_order = sqlite3_column_int(statement, 10);
  2689. //
  2690. //
  2691. // char *mode = (char*)sqlite3_column_text(statement, 11);
  2692. // if(mode==nil)
  2693. // mode="";
  2694. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2695. //
  2696. //
  2697. // char *username = (char*)sqlite3_column_text(statement, 12);
  2698. // if(username==nil)
  2699. // username="";
  2700. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2701. //
  2702. //
  2703. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2704. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2705. // [header setValue:nscontact_id forKey:@"contact_id"];
  2706. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2707. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2708. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2709. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2710. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2711. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2712. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2713. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2714. //
  2715. // [header setValue:nsusername forKey:@"username"];
  2716. //
  2717. //
  2718. // [ret setObject:header forKey:@"header"];
  2719. // [ret setValue:nsmode forKey:@"mode"];
  2720. //
  2721. //
  2722. // }
  2723. //
  2724. //
  2725. //
  2726. // sqlite3_finalize(statement);
  2727. // }
  2728. //
  2729. //
  2730. //
  2731. // [iSalesDB close_db:db];
  2732. //
  2733. //
  2734. //
  2735. //
  2736. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2737. return ret;
  2738. }
  2739. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  2740. {
  2741. UIApplication * app = [UIApplication sharedApplication];
  2742. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2743. appDelegate.disable_trigger=true;
  2744. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2745. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2746. appDelegate.disable_trigger=false;
  2747. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2748. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2749. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  2750. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  2751. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  2752. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  2753. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  2754. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  2755. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  2756. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  2757. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  2758. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  2759. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  2760. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  2761. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  2762. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  2763. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2764. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  2765. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  2766. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  2767. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  2768. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  2769. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  2770. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2771. [arr_name addObject:customer_first_name];
  2772. [arr_name addObject:customer_last_name];
  2773. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2774. // default ship from
  2775. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,decrypt(phone) from offline_contact where contact_id = 'NPD';";
  2776. __block NSString *cid = @"";
  2777. __block NSString *name = @"";
  2778. __block NSString *ext = @"";
  2779. __block NSString *contact = @"";
  2780. __block NSString *email = @"";
  2781. __block NSString *fax = @"";
  2782. __block NSString *phone = @"";
  2783. sqlite3 *db = [iSalesDB get_db];
  2784. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  2785. cid = [self textAtColumn:0 statement:statment];
  2786. name = [self textAtColumn:1 statement:statment];
  2787. ext = [self textAtColumn:2 statement:statment];
  2788. contact = [self textAtColumn:3 statement:statment];
  2789. email = [self textAtColumn:4 statement:statment];
  2790. fax = [self textAtColumn:5 statement:statment];
  2791. phone = [self textAtColumn:6 statement:statment];
  2792. }];
  2793. NSString* so_id = [self get_offline_soid:db];
  2794. if(so_id==nil)
  2795. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  2796. NSString* sql_neworder=[NSString stringWithFormat:@"insert into offline_order(so_id,status,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,sales_rep,create_by,customer_contact,must_call,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,sales_terms) values('%@',0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','FOB')",so_id,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,customer_sales_rep,create_by ,contact_name,cid,name,ext,contact,phone,fax,email,customer_cid,customer_name,customer_contact_ext,customer_contact,customer_phone,customer_fax,customer_email];
  2797. int result =[iSalesDB execSql:sql_neworder db:db];
  2798. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  2799. //insert into offline_order(so_id,status,contact_id,sales_rep,create_by,customer_contact) select 'OFFLINE'||hex(randomblob(16)),-1,contact_id,sales_rep,company_name from offline_contact where contact_id='99FURO0002'
  2800. //soId
  2801. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  2802. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  2803. sqlite3_stmt * statement;
  2804. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2805. {
  2806. if (sqlite3_step(statement) == SQLITE_ROW)
  2807. {
  2808. // char *name = (char*)sqlite3_column_text(statement, 1);
  2809. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2810. //ret = sqlite3_column_int(statement, 0);
  2811. char *soId = (char*)sqlite3_column_text(statement, 0);
  2812. if(soId==nil)
  2813. soId="";
  2814. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  2815. [ret setValue:nssoId forKey:@"soId"];
  2816. [ret setValue:nssoId forKey:@"orderCode"];
  2817. }
  2818. sqlite3_finalize(statement);
  2819. }
  2820. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  2821. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2822. [iSalesDB close_db:db];
  2823. return [RAUtils dict2data:ret];
  2824. }
  2825. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  2826. {
  2827. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2828. NSString* orderCode = [params valueForKey:@"orderCode"];
  2829. UIApplication * app = [UIApplication sharedApplication];
  2830. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2831. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  2832. {
  2833. appDelegate.disable_trigger=true;
  2834. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2835. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2836. appDelegate.disable_trigger=false;
  2837. }
  2838. sqlite3 *db = [iSalesDB get_db];
  2839. int cart_count=[self query_ordercartcount:orderCode db:db];
  2840. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2841. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2842. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2843. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2844. [iSalesDB close_db:db];
  2845. return [RAUtils dict2data:ret];
  2846. }
  2847. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  2848. {
  2849. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2850. sqlite3 *db = [iSalesDB get_db];
  2851. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2852. int count =0;
  2853. if(params[@"count"]!=nil)
  2854. {
  2855. count = [params[@"count"] intValue];
  2856. }
  2857. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  2858. NSString* product_id=params[@"product_id"];
  2859. NSString* orderCode=params[@"orderCode"];
  2860. NSString *qty = params[@"qty"];
  2861. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  2862. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  2863. __block int number_of_outOfStock = 0;
  2864. for(int i=0;i<arr_id.count;i++)
  2865. {
  2866. NSInteger item_qty= count;
  2867. if (qty) {
  2868. item_qty = [qty_arr[i] integerValue];
  2869. }
  2870. if(item_qty==0)
  2871. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  2872. // 检查新加Model数量是否大于库存
  2873. if (!appDelegate.can_create_backorder) {
  2874. __block BOOL needContinue = NO;
  2875. [iSalesDB jk_query:[NSString stringWithFormat:@"select availability from product where product_id = %@;",arr_id[i]] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2876. int availability = sqlite3_column_int(stmt, 0);
  2877. // 库存小于购买量为缺货
  2878. if (availability < item_qty || availability <= 0) {
  2879. number_of_outOfStock++;
  2880. needContinue = YES;
  2881. }
  2882. }];
  2883. if (needContinue) {
  2884. continue;
  2885. }
  2886. }
  2887. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  2888. NSString* sql=nil;
  2889. sqlite3_stmt *stmt = nil;
  2890. BOOL shouldStep = NO;
  2891. if(_id<0)
  2892. {
  2893. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  2894. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  2895. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2896. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  2897. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  2898. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  2899. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  2900. shouldStep = YES;
  2901. }
  2902. else
  2903. {
  2904. if (qty) { // wish list move to cart
  2905. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  2906. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2907. sqlite3_bind_int(stmt, 1, _id);
  2908. shouldStep = YES;
  2909. } else {
  2910. sql = [NSString stringWithFormat:@"select c.item_count + %ld,p.availability from product as p left join offline_cart as c on c.item_id = p.item_id where c._id = %d;",item_qty,_id];
  2911. __block BOOL update = YES;
  2912. if (!appDelegate.can_create_backorder) {
  2913. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2914. int newQTY = sqlite3_column_int(stmt, 0);
  2915. int availability = sqlite3_column_int(stmt, 1);
  2916. if (newQTY > availability) { // 库存不够
  2917. update = NO;
  2918. number_of_outOfStock++;
  2919. }
  2920. }];
  2921. }
  2922. if (update) {
  2923. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  2924. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2925. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  2926. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  2927. shouldStep = YES;
  2928. }
  2929. }
  2930. }
  2931. if (shouldStep) {
  2932. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2933. [iSalesDB execSql:@"ROLLBACK" db:db];
  2934. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2935. [iSalesDB close_db:db];
  2936. DebugLog(@"add to cart error");
  2937. return [RAUtils dict2data:ret];
  2938. }
  2939. }
  2940. }
  2941. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2942. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2943. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2944. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2945. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2946. if (!appDelegate.can_create_backorder) {
  2947. if (number_of_outOfStock > 0) {
  2948. ret[@"result"]=[NSNumber numberWithInt:8];
  2949. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  2950. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  2951. }
  2952. }
  2953. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2954. [iSalesDB close_db:db];
  2955. return [RAUtils dict2data:ret];
  2956. }
  2957. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID
  2958. {
  2959. // UIApplication * app = [UIApplication sharedApplication];
  2960. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2961. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2962. NSString *sqlQuery = [NSString stringWithFormat:@"select b.item_id,b.qty,c.name,c.description,b.unit_price,b.use_unitprice from (select item_id,qty,decrypt(unit_price) as unit_price,use_unitprice from bundle where product_item_id=%d) b left join catalog c on b.item_id=c.item_id;",item_id];
  2963. sqlite3_stmt * statement;
  2964. int count=0;
  2965. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2966. {
  2967. while (sqlite3_step(statement) == SQLITE_ROW)
  2968. {
  2969. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  2970. int bitem_id = sqlite3_column_int(statement, 0);
  2971. int bitem_qty = sqlite3_column_int(statement, 1);
  2972. char *name = (char*)sqlite3_column_text(statement, 2);
  2973. if(name==nil)
  2974. name="";
  2975. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  2976. char *description = (char*)sqlite3_column_text(statement, 3);
  2977. if(description==nil)
  2978. description="";
  2979. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  2980. double unit_price = sqlite3_column_double(statement, 4);
  2981. int use_unitprice = sqlite3_column_int(statement, 5);
  2982. if(use_unitprice!=1)
  2983. {
  2984. unit_price=[self get_model_default_price:contactID product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  2985. }
  2986. itemjson[@"model"]=nsname;
  2987. itemjson[@"description"]=nsdescription;
  2988. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  2989. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  2990. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  2991. if(compute)
  2992. {
  2993. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID];
  2994. }
  2995. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2996. count++;
  2997. }
  2998. sqlite3_finalize(statement);
  2999. }
  3000. ret[@"count"]=@(count);
  3001. if(count==0)
  3002. return nil;
  3003. else
  3004. return ret;
  3005. }
  3006. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  3007. {
  3008. //compute: add part to subtotal;
  3009. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  3010. dict_item[@(item_id)]=@"1";
  3011. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3012. double cuft=0;
  3013. double weight=0;
  3014. int carton=0;
  3015. NSString *sqlQuery = [NSString stringWithFormat:@"select ulength ,uwidth ,uheight ,uweight ,mlength ,mwidth ,mheight ,mweigth ,ilength ,iwidth ,iheight , iweight ,pcs ,mpack ,ipack,ucbf,icbf,mcbf from catalog where item_id=%d;",item_id];
  3016. sqlite3_stmt * statement;
  3017. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3018. {
  3019. if (sqlite3_step(statement) == SQLITE_ROW)
  3020. {
  3021. double ulength = sqlite3_column_double(statement, 0);
  3022. double uwidth = sqlite3_column_double(statement, 1);
  3023. double uheight = sqlite3_column_double(statement, 2);
  3024. double uweight = sqlite3_column_double(statement, 3);
  3025. double mlength = sqlite3_column_double(statement, 4);
  3026. double mwidth = sqlite3_column_double(statement, 5);
  3027. double mheight = sqlite3_column_double(statement, 6);
  3028. double mweight = sqlite3_column_double(statement, 7);
  3029. double ilength = sqlite3_column_double(statement, 8);
  3030. double iwidth = sqlite3_column_double(statement, 9);
  3031. double iheight = sqlite3_column_double(statement, 10);
  3032. double iweight = sqlite3_column_double(statement, 11);
  3033. int pcs = sqlite3_column_int(statement,12);
  3034. int mpack = sqlite3_column_int(statement, 13);
  3035. int ipack = sqlite3_column_int(statement, 14);
  3036. double ucbf = sqlite3_column_double(statement, 15);
  3037. double icbf = sqlite3_column_double(statement, 16);
  3038. double mcbf = sqlite3_column_double(statement, 17);
  3039. if(ipack==0)
  3040. {
  3041. carton= count/mpack ;
  3042. weight = mweight*carton;
  3043. cuft= carton*(mlength*mwidth*mheight);
  3044. int remain=count%mpack;
  3045. if(remain==0)
  3046. {
  3047. //do nothing;
  3048. }
  3049. else
  3050. {
  3051. carton++;
  3052. weight += uweight*remain;
  3053. cuft += (ulength*uwidth*uheight)*remain;
  3054. }
  3055. }
  3056. else
  3057. {
  3058. carton = count/(mpack*ipack);
  3059. weight = mweight*carton;
  3060. cuft= carton*(mlength*mwidth*mheight);
  3061. int remain=count%(mpack*ipack);
  3062. if(remain==0)
  3063. {
  3064. // do nothing;
  3065. }
  3066. else
  3067. {
  3068. carton++;
  3069. int icarton =remain/ipack;
  3070. int iremain=remain%ipack;
  3071. weight += iweight*icarton;
  3072. cuft += (ilength*iwidth*iheight)*icarton;
  3073. if(iremain==0)
  3074. {
  3075. //do nothing;
  3076. }
  3077. else
  3078. {
  3079. weight += uweight*iremain;
  3080. cuft += (ulength*uwidth*uheight)*iremain;
  3081. }
  3082. }
  3083. }
  3084. #ifdef BUILD_NPD
  3085. cuft=ucbf*count;
  3086. weight= uweight*count;
  3087. #endif
  3088. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  3089. cuft=ucbf*count;
  3090. weight= uweight*count;
  3091. #endif
  3092. }
  3093. sqlite3_finalize(statement);
  3094. }
  3095. if(compute)
  3096. {
  3097. NSArray * arr_count=nil;
  3098. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  3099. for(int i=0;i<arr_bundle.count;i++)
  3100. {
  3101. dict_item[arr_bundle[i]]=@"1";
  3102. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  3103. cuft+=[bundlejson[@"cuft"] doubleValue];
  3104. weight+=[bundlejson[@"weight"] doubleValue];
  3105. carton+=[bundlejson[@"carton"] intValue];
  3106. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  3107. }
  3108. }
  3109. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  3110. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  3111. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  3112. ret[@"items"]=dict_item;
  3113. return ret;
  3114. }
  3115. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  3116. {
  3117. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  3118. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  3119. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3120. DebugLog(@"offline_login sql:%@",sqlQuery);
  3121. sqlite3_stmt * statement;
  3122. int cart_count=0;
  3123. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3124. if ( dbresult== SQLITE_OK)
  3125. {
  3126. while (sqlite3_step(statement) == SQLITE_ROW)
  3127. {
  3128. int item_id = sqlite3_column_int(statement, 0);
  3129. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  3130. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3131. }
  3132. sqlite3_finalize(statement);
  3133. }
  3134. return cart_count;
  3135. }
  3136. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  3137. {
  3138. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3139. sqlite3 *db = [iSalesDB get_db];
  3140. UIApplication * app = [UIApplication sharedApplication];
  3141. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3142. NSString* orderCode=params[@"orderCode"];
  3143. int sort = [[params objectForKey:@"sort"] intValue];
  3144. NSString *sort_str = @"";
  3145. switch (sort) {
  3146. case 0:{
  3147. sort_str = @"order by c.modify_time desc";
  3148. }
  3149. break;
  3150. case 1:{
  3151. sort_str = @"order by c.modify_time asc";
  3152. }
  3153. break;
  3154. case 2:{
  3155. sort_str = @"order by m.name asc";
  3156. }
  3157. break;
  3158. case 3:{
  3159. sort_str = @"order by m.name desc";
  3160. }
  3161. break;
  3162. case 4:{
  3163. sort_str = @"order by m.description asc";
  3164. }
  3165. break;
  3166. default:
  3167. break;
  3168. }
  3169. NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id,m.availability from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id %@",orderCode,sort_str ];
  3170. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  3171. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3172. DebugLog(@"offline_login sql:%@",sqlQuery);
  3173. sqlite3_stmt * statement;
  3174. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3175. NSDate *date1 = [NSDate date];
  3176. int count=0;
  3177. int cart_count=0;
  3178. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3179. if ( dbresult== SQLITE_OK)
  3180. {
  3181. while (sqlite3_step(statement) == SQLITE_ROW)
  3182. {
  3183. // NSDate *row_date = [NSDate date];
  3184. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  3185. int product_id = sqlite3_column_int(statement, 0);
  3186. char *str_price = (char*)sqlite3_column_text(statement, 1);
  3187. int item_id = sqlite3_column_int(statement, 7);
  3188. NSString* Price=nil;
  3189. if(str_price==nil)
  3190. {
  3191. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3192. // NSDate *price_date = [NSDate date];
  3193. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3194. // DebugLog(@"price time interval");
  3195. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  3196. if(price==nil)
  3197. Price=@"No Price.";
  3198. else
  3199. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3200. }
  3201. else
  3202. {
  3203. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  3204. }
  3205. double discount = sqlite3_column_double(statement, 2);
  3206. int item_count = sqlite3_column_int(statement, 3);
  3207. char *line_note = (char*)sqlite3_column_text(statement, 4);
  3208. NSString *nsline_note=nil;
  3209. if(line_note!=nil)
  3210. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  3211. char *name = (char*)sqlite3_column_text(statement, 5);
  3212. NSString *nsname=nil;
  3213. if(name!=nil)
  3214. nsname= [[NSString alloc]initWithUTF8String:name];
  3215. char *description = (char*)sqlite3_column_text(statement, 6);
  3216. NSString *nsdescription=nil;
  3217. if(description!=nil)
  3218. nsdescription= [[NSString alloc]initWithUTF8String:description];
  3219. int stockUom = sqlite3_column_int(statement, 8);
  3220. int _id = sqlite3_column_int(statement, 9);
  3221. int availability = sqlite3_column_int(statement, 10);
  3222. // NSDate *subtotal_date = [NSDate date];
  3223. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  3224. // DebugLog(@"subtotal_date time interval");
  3225. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  3226. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  3227. double weight=[bsubtotaljson[@"weight"] doubleValue];
  3228. int carton=[bsubtotaljson[@"carton"] intValue];
  3229. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3230. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  3231. // NSDate *img_date = [NSDate date];
  3232. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  3233. // DebugLog(@"img_date time interval");
  3234. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  3235. itemjson[@"model"]=nsname;
  3236. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  3237. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  3238. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  3239. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  3240. itemjson[@"check"]=@"true";
  3241. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  3242. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  3243. itemjson[@"unit_price"]=Price;
  3244. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  3245. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  3246. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  3247. itemjson[@"note"]=nsline_note;
  3248. if (!appDelegate.can_create_backorder) {
  3249. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  3250. }
  3251. // NSDate *date2 = [NSDate date];
  3252. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  3253. // DebugLog(@"model_bundle time interval");
  3254. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  3255. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3256. count++;
  3257. // DebugLog(@"row time interval");
  3258. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  3259. }
  3260. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3261. sqlite3_finalize(statement);
  3262. }
  3263. DebugLog(@"request cart total time interval");
  3264. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3265. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3266. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  3267. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3268. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3269. ret[@"count"]=[NSNumber numberWithInt:count ];
  3270. ret[@"mode"]=@"Regular Mode";
  3271. [iSalesDB close_db:db];
  3272. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  3273. DebugLog(@"general notes :%@",general_note);
  3274. ret[@"general_note"]= general_note;
  3275. return [RAUtils dict2data:ret];
  3276. }
  3277. +(NSData*) offline_login :(NSMutableDictionary *) params
  3278. {
  3279. NSString* user = [params valueForKey:@"user"];
  3280. NSString* password = [params valueForKey:@"password"];
  3281. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3282. sqlite3 *db = [iSalesDB get_db];
  3283. NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username,can_update_contact_info from offline_login where lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]];
  3284. DebugLog(@"offline_login sql:%@",sqlQuery);
  3285. sqlite3_stmt * statement;
  3286. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3287. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3288. {
  3289. if (sqlite3_step(statement) == SQLITE_ROW)
  3290. {
  3291. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3292. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3293. int can_show_price = sqlite3_column_int(statement, 0);
  3294. int can_see_price = sqlite3_column_int(statement, 1);
  3295. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3296. if(contact_id==nil)
  3297. contact_id="";
  3298. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3299. int user_type = sqlite3_column_int(statement, 3);
  3300. int can_cancel_order = sqlite3_column_int(statement, 4);
  3301. int can_set_cart_price = sqlite3_column_int(statement, 5);
  3302. int can_create_portfolio = sqlite3_column_int(statement, 6);
  3303. int can_delete_order = sqlite3_column_int(statement, 7);
  3304. int can_submit_order = sqlite3_column_int(statement, 8);
  3305. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3306. int can_create_order = sqlite3_column_int(statement, 10);
  3307. char *mode = (char*)sqlite3_column_text(statement, 11);
  3308. if(mode==nil)
  3309. mode="";
  3310. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3311. char *username = (char*)sqlite3_column_text(statement, 12);
  3312. if(username==nil)
  3313. username="";
  3314. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3315. int can_update_contact_info = sqlite3_column_int(statement, 13);
  3316. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3317. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3318. [header setValue:nscontact_id forKey:@"contact_id"];
  3319. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3320. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3321. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3322. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3323. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3324. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3325. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3326. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3327. [header setObject:@(YES) forKey:@"can_create_backorder"];
  3328. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  3329. [header setValue:nsusername forKey:@"username"];
  3330. NSError* error=nil;
  3331. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  3332. [header setValue:statusFilter forKey:@"statusFilter"];
  3333. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3334. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  3335. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  3336. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  3337. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  3338. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  3339. [ret setObject:header forKey:@"header"];
  3340. [ret setValue:nsmode forKey:@"mode"];
  3341. }
  3342. sqlite3_finalize(statement);
  3343. }
  3344. [iSalesDB close_db:db];
  3345. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3346. return [RAUtils dict2data:ret];
  3347. }
  3348. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  3349. {
  3350. NSString* contactId = [params valueForKey:@"contactId"];
  3351. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3352. sqlite3 *db = [iSalesDB get_db];
  3353. NSString *sqlQuery = nil;
  3354. {
  3355. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email,img_0,img_1,img_2,price_type,notes,sales_rep,type from offline_contact where contact_id='%@'",contactId];
  3356. }
  3357. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3358. sqlite3_stmt * statement;
  3359. [ret setValue:@"2" forKey:@"result"];
  3360. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3361. {
  3362. //int i = 0;
  3363. if (sqlite3_step(statement) == SQLITE_ROW)
  3364. {
  3365. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3366. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3367. // int editable = sqlite3_column_int(statement, 0);
  3368. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3369. NSString *nscompany_name =nil;
  3370. if(company_name==nil)
  3371. nscompany_name=@"";
  3372. else
  3373. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  3374. char *country = (char*)sqlite3_column_text(statement, 2);
  3375. if(country==nil)
  3376. country="";
  3377. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3378. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3379. // if(addr==nil)
  3380. // addr="";
  3381. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3382. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3383. if(zipcode==nil)
  3384. zipcode="";
  3385. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3386. char *state = (char*)sqlite3_column_text(statement, 5);
  3387. if(state==nil)
  3388. state="";
  3389. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3390. char *city = (char*)sqlite3_column_text(statement, 6);
  3391. if(city==nil)
  3392. city="";
  3393. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3394. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3395. // NSString *nscontact_name = nil;
  3396. // if(contact_name==nil)
  3397. // nscontact_name=@"";
  3398. // else
  3399. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3400. char *phone = (char*)sqlite3_column_text(statement, 8);
  3401. NSString *nsphone = nil;
  3402. if(phone==nil)
  3403. nsphone=@"";
  3404. else
  3405. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3406. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3407. if(contact_id==nil)
  3408. contact_id="";
  3409. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3410. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3411. if(addr_1==nil)
  3412. addr_1="";
  3413. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3414. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3415. if(addr_2==nil)
  3416. addr_2="";
  3417. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3418. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3419. if(addr_3==nil)
  3420. addr_3="";
  3421. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3422. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3423. if(addr_4==nil)
  3424. addr_4="";
  3425. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3426. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3427. if(first_name==nil)
  3428. first_name="";
  3429. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3430. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3431. if(last_name==nil)
  3432. last_name="";
  3433. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3434. char *fax = (char*)sqlite3_column_text(statement, 16);
  3435. NSString *nsfax = nil;
  3436. if(fax==nil)
  3437. nsfax=@"";
  3438. else
  3439. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3440. char *email = (char*)sqlite3_column_text(statement, 17);
  3441. NSString *nsemail = nil;
  3442. if(email==nil)
  3443. nsemail=@"";
  3444. else
  3445. nsemail= [[NSString alloc]initWithUTF8String:email];
  3446. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  3447. NSString *nsimg_0 = nil;
  3448. if(img_0==nil)
  3449. nsimg_0=@"";
  3450. else
  3451. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  3452. [self copy_bcardImg:nsimg_0];
  3453. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  3454. NSString *nsimg_1 = nil;
  3455. if(img_1==nil)
  3456. nsimg_1=@"";
  3457. else
  3458. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  3459. [self copy_bcardImg:nsimg_1];
  3460. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  3461. NSString *nsimg_2 = nil;
  3462. if(img_2==nil)
  3463. nsimg_2=@"";
  3464. else
  3465. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  3466. [self copy_bcardImg:nsimg_2];
  3467. char *price_type = (char*)sqlite3_column_text(statement, 21);
  3468. NSString *nsprice_type = nil;
  3469. if(price_type==nil)
  3470. nsprice_type=@"";
  3471. else
  3472. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  3473. char *notes = (char*)sqlite3_column_text(statement, 22);
  3474. NSString *nsnotes = nil;
  3475. if(notes==nil)
  3476. nsnotes=@"";
  3477. else
  3478. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  3479. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  3480. NSString *nssalesrep = nil;
  3481. if(salesrep==nil)
  3482. nssalesrep=@"";
  3483. else
  3484. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  3485. NSString *contact_type = [self textAtColumn:24 statement:statement];
  3486. {
  3487. // decrypt
  3488. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3489. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3490. nsphone=[AESCrypt fastdecrypt:nsphone];
  3491. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3492. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3493. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3494. }
  3495. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3496. [arr_name addObject:nsfirst_name];
  3497. [arr_name addObject:nslast_name];
  3498. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  3499. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  3500. [arr_ext addObject:nsaddr_1];
  3501. [arr_ext addObject:nsaddr_2];
  3502. [arr_ext addObject:nsaddr_3];
  3503. [arr_ext addObject:nsaddr_4];
  3504. [arr_ext addObject:@"\r\n"];
  3505. [arr_ext addObject:nscity];
  3506. [arr_ext addObject:nsstate];
  3507. [arr_ext addObject:nszipcode];
  3508. [arr_ext addObject:nscountry];
  3509. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  3510. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  3511. [item setValue:nsimg_2 forKey:@"business_card_2"];
  3512. [item setValue:nsimg_0 forKey:@"business_card_0"];
  3513. [item setValue:nscountry forKey:@"customer_country"];
  3514. [item setValue:nsphone forKey:@"customer_phone"];
  3515. [item setValue:nsimg_1 forKey:@"business_card_1"];
  3516. [item setValue:nscompany_name forKey:@"customer_name"];
  3517. [item setValue:nsprice_type forKey:@"customer_price_type"];
  3518. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  3519. [item setValue:nsext forKey:@"customer_contact_ext"];
  3520. [item setValue:nszipcode forKey:@"customer_zipcode"];
  3521. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  3522. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  3523. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  3524. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  3525. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  3526. [item setValue:nslast_name forKey:@"customer_last_name"];
  3527. [item setValue:nscity forKey:@"customer_city"];
  3528. [item setValue:nsstate forKey:@"customer_state"];
  3529. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  3530. [item setValue:contactId forKey:@"customer_cid"];
  3531. [item setValue:nscontact_name forKey:@"customer_contact"];
  3532. [item setValue:nsfax forKey:@"customer_fax"];
  3533. [item setValue:nsemail forKey:@"customer_email"];
  3534. [item setValue:contact_type forKey:@"customer_contact_type"];
  3535. [ret setObject:item forKey:@"customerInfo"];
  3536. // i++;
  3537. }
  3538. UIApplication * app = [UIApplication sharedApplication];
  3539. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3540. [ret setValue:appDelegate.mode forKey:@"mode"];
  3541. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  3542. sqlite3_finalize(statement);
  3543. }
  3544. [iSalesDB close_db:db];
  3545. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3546. return ret;
  3547. }
  3548. + (bool) copy_bcardImg:(NSString*) filename
  3549. {
  3550. if(filename.length==0)
  3551. return false;
  3552. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3553. bool ret=false;
  3554. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3555. NSString *cachefolder = [paths objectAtIndex:0];
  3556. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  3557. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3558. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3559. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  3560. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  3561. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3562. //
  3563. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3564. NSFileManager* fileManager = [NSFileManager defaultManager];
  3565. BOOL bdir=NO;
  3566. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  3567. {
  3568. NSError *error = nil;
  3569. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  3570. {
  3571. ret=false;
  3572. }
  3573. else
  3574. {
  3575. ret=true;
  3576. }
  3577. // NSError *error = nil;
  3578. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3579. //
  3580. // if(!bsuccess)
  3581. // {
  3582. // DebugLog(@"Create offline_createimg folder failed");
  3583. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3584. // return [RAUtils dict2data:ret];
  3585. // }
  3586. // if(bsuccess)
  3587. // {
  3588. // sqlite3 *db = [self get_db];
  3589. //
  3590. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3591. // [iSalesDB close_db:db];
  3592. // }
  3593. }
  3594. return ret;
  3595. //
  3596. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3597. // if(bsuccess)
  3598. // {
  3599. // NSError *error = nil;
  3600. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3601. // {
  3602. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3603. // }
  3604. // else
  3605. // {
  3606. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3607. //
  3608. // ret[@"img_url_aname"]=filename;
  3609. // ret[@"img_url"]=savedImagePath;
  3610. // }
  3611. // }
  3612. }
  3613. +(NSData *) offline_saveBusinesscard:(NSData *) image
  3614. {
  3615. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3616. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3617. NSString *cachefolder = [paths objectAtIndex:0];
  3618. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3619. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3620. NSFileManager* fileManager = [NSFileManager defaultManager];
  3621. BOOL bdir=YES;
  3622. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  3623. {
  3624. NSError *error = nil;
  3625. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3626. if(!bsuccess)
  3627. {
  3628. DebugLog(@"Create offline_createimg folder failed");
  3629. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3630. return [RAUtils dict2data:ret];
  3631. }
  3632. // if(bsuccess)
  3633. // {
  3634. // sqlite3 *db = [self get_db];
  3635. //
  3636. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3637. // [iSalesDB close_db:db];
  3638. // }
  3639. }
  3640. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  3641. //JEPG格式
  3642. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  3643. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  3644. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3645. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3646. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3647. if(bsuccess)
  3648. {
  3649. NSError *error = nil;
  3650. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3651. {
  3652. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3653. }
  3654. else
  3655. {
  3656. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3657. ret[@"img_url_aname"]=filename;
  3658. ret[@"img_url"]=filename;
  3659. }
  3660. }
  3661. else
  3662. {
  3663. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3664. }
  3665. return [RAUtils dict2data:ret];
  3666. }
  3667. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  3668. {
  3669. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  3670. category = [category substringToIndex:3];
  3671. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3672. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  3673. params[@"category"]= category;
  3674. ret[@"params"]= params;
  3675. [ret setValue:@"detail" forKey:@"target"];
  3676. [ret setValue:@"popup" forKey:@"action"];
  3677. [ret setValue:@"content" forKey:@"type"];
  3678. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  3679. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3680. [ret setValue:@"true" forKey:@"single_row"];
  3681. [ret setValue:@"true" forKey:@"partial_refresh"];
  3682. // sqlite3 *db = [iSalesDB get_db];
  3683. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3684. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.product_id,i.url,random() as aa from product m LEFT join model_image i on m.product_id = i.product_id where m.category like'%%#%@#%%' and m.category like'%%#005#%%' and i.default_img = %d order by aa limit 10 ;",category,OFFLINE_IMG_TYPE_CATEGORY]; // select m.name,m.product_id,i.url,random() as aa from model m LEFT join model_image i on m.product_id = i.product_id where m.category like'%%#%@#%%' and m.category like'%%#005#%%' and i.default_img = %d order by aa limit 10 ;
  3685. sqlite3_stmt * statement;
  3686. int count = 0;
  3687. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  3688. // int count=0;
  3689. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3690. {
  3691. int i=0;
  3692. while (sqlite3_step(statement) == SQLITE_ROW)
  3693. {
  3694. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3695. // char *name = (char*)sqlite3_column_text(statement, 1);
  3696. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3697. char *name = (char*)sqlite3_column_text(statement, 0);
  3698. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3699. int product_id = sqlite3_column_int(statement, 1);
  3700. char *url = (char*)sqlite3_column_text(statement, 2);
  3701. if(url==nil)
  3702. url="";
  3703. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3704. [item setValue:nsurl forKey:@"picture_path"];
  3705. [item setValue:nsname forKey:@"fash_name"];
  3706. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3707. [item setValue:category forKey:@"category"];
  3708. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3709. i++;
  3710. }
  3711. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3712. sqlite3_finalize(statement);
  3713. }
  3714. DebugLog(@"count:%d",count);
  3715. // [iSalesDB close_db:db];
  3716. return ret;
  3717. }
  3718. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  3719. {
  3720. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3721. [ret setValue:key forKey:@"key"];
  3722. [ret setValue:value forKey:@"val"];
  3723. [ret setValue:@"price" forKey:@"type"];
  3724. return ret;
  3725. }
  3726. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  3727. {
  3728. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3729. [ret setValue:key forKey:@"key"];
  3730. [ret setValue:value forKey:@"val"];
  3731. return ret;
  3732. }
  3733. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  3734. {
  3735. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3736. [ret setValue:@"0" forKey:@"img_count"];
  3737. // sqlite3 *db = [iSalesDB get_db];
  3738. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,property_display from product where product_id=%d ;",field,product_id]; // select %@,property_display from model where product_id=%d ;
  3739. sqlite3_stmt * statement;
  3740. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3741. {
  3742. int i=0;
  3743. if (sqlite3_step(statement) == SQLITE_ROW)
  3744. {
  3745. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3746. // char *name = (char*)sqlite3_column_text(statement, 1);
  3747. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3748. char *value = (char*)sqlite3_column_text(statement, 0);
  3749. if(value==nil)
  3750. value="";
  3751. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3752. char *key = (char*)sqlite3_column_text(statement, 1);
  3753. if(key==nil)
  3754. key="";
  3755. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  3756. [item setValue:nsvalue forKey:@"val"];
  3757. [item setValue:nskey forKey:@"key"];
  3758. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3759. [ret setValue:@"1" forKey:@"count"];
  3760. i++;
  3761. }
  3762. sqlite3_finalize(statement);
  3763. }
  3764. // [iSalesDB close_db:db];
  3765. return ret;
  3766. }
  3767. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  3768. {
  3769. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3770. [ret setValue:@"0" forKey:@"count"];
  3771. // sqlite3 *db = [iSalesDB get_db];
  3772. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,selector_display,product_id,category from product where product_group='%@' order by name;",field,product_group]; // select %@,selector_display,product_id,category from model where product_group='%@' order by name;
  3773. sqlite3_stmt * statement;
  3774. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3775. {
  3776. int i=0;
  3777. while (sqlite3_step(statement) == SQLITE_ROW)
  3778. {
  3779. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3780. // char *name = (char*)sqlite3_column_text(statement, 1);
  3781. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3782. char *value = (char*)sqlite3_column_text(statement, 0);
  3783. if(value==nil)
  3784. value="";
  3785. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3786. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  3787. if(selector_display==nil)
  3788. selector_display="";
  3789. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  3790. int product_id = sqlite3_column_int(statement, 2);
  3791. char *category = (char*)sqlite3_column_text(statement, 3);
  3792. if(category==nil)
  3793. category="";
  3794. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  3795. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  3796. [item setValue:nsvalue forKey:@"title"];
  3797. [item setValue:url forKey:@"pic_url"];
  3798. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  3799. [params setValue:@"2" forKey:@"count"];
  3800. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  3801. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  3802. [param0 setValue:@"product_id" forKey:@"name"];
  3803. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  3804. [param1 setValue:nscategory forKey:@"val"];
  3805. [param1 setValue:@"category" forKey:@"name"];
  3806. [params setObject:param0 forKey:@"param_0"];
  3807. [params setObject:param1 forKey:@"param_1"];
  3808. [item setObject:params forKey:@"params"];
  3809. [ret setValue:nsselector_display forKey:@"name"];
  3810. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3811. i++;
  3812. }
  3813. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3814. [ret setValue:@"switch" forKey:@"action"];
  3815. sqlite3_finalize(statement);
  3816. }
  3817. // [iSalesDB close_db:db];
  3818. return ret;
  3819. }
  3820. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  3821. {
  3822. // model 在 category search 显示的图片。
  3823. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  3824. // sqlite3 *db = [iSalesDB get_db];
  3825. NSString *sqlQuery = [NSString stringWithFormat:@"select url,type from model_image where product_id=%d and type<2 order by default_img desc , _id asc limit 2;",product_id];
  3826. sqlite3_stmt * statement;
  3827. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3828. {
  3829. while (sqlite3_step(statement) == SQLITE_ROW)
  3830. {
  3831. char *url = (char*)sqlite3_column_text(statement, 0);
  3832. if(url==nil)
  3833. url="";
  3834. int type = sqlite3_column_int(statement, 1);
  3835. if(type==0)
  3836. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  3837. else
  3838. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  3839. }
  3840. sqlite3_finalize(statement);
  3841. }
  3842. // [iSalesDB close_db:db];
  3843. return ret;
  3844. }
  3845. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  3846. {
  3847. int item_id=-1;
  3848. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  3849. sqlite3_stmt * statement;
  3850. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3851. {
  3852. if (sqlite3_step(statement) == SQLITE_ROW)
  3853. {
  3854. item_id = sqlite3_column_int(statement, 0);
  3855. }
  3856. sqlite3_finalize(statement);
  3857. }
  3858. return item_id;
  3859. }
  3860. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  3861. {
  3862. // NSString* ret = @"";
  3863. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  3864. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  3865. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  3866. sqlite3_stmt * statement;
  3867. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3868. {
  3869. while (sqlite3_step(statement) == SQLITE_ROW)
  3870. {
  3871. int bitem_id = sqlite3_column_int(statement, 0);
  3872. int bitem_qty = sqlite3_column_int(statement, 1);
  3873. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  3874. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  3875. }
  3876. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  3877. sqlite3_finalize(statement);
  3878. }
  3879. // if(ret==nil)
  3880. // ret=@"";
  3881. *count=arr_count;
  3882. return arr_bundle;
  3883. }
  3884. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  3885. {
  3886. // get default sold qty, return -1 if model not found;
  3887. int ret = -1;
  3888. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  3889. sqlite3_stmt * statement;
  3890. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3891. {
  3892. if (sqlite3_step(statement) == SQLITE_ROW)
  3893. {
  3894. ret = sqlite3_column_int(statement, 0);
  3895. }
  3896. sqlite3_finalize(statement);
  3897. }
  3898. return ret;
  3899. }
  3900. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  3901. {
  3902. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3903. [ret setValue:@"0" forKey:@"img_count"];
  3904. // sqlite3 *db = [iSalesDB get_db];
  3905. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  3906. sqlite3_stmt * statement;
  3907. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3908. {
  3909. int i=0;
  3910. while (sqlite3_step(statement) == SQLITE_ROW)
  3911. {
  3912. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3913. // char *name = (char*)sqlite3_column_text(statement, 1);
  3914. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3915. char *url = (char*)sqlite3_column_text(statement, 0);
  3916. if(url==nil)
  3917. url="";
  3918. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3919. [item setValue:nsurl forKey:@"s"];
  3920. [item setValue:nsurl forKey:@"l"];
  3921. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  3922. i++;
  3923. }
  3924. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  3925. sqlite3_finalize(statement);
  3926. }
  3927. // [iSalesDB close_db:db];
  3928. return ret;
  3929. }
  3930. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  3931. UIApplication * app = [UIApplication sharedApplication];
  3932. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3933. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3934. sqlite3 *db = [iSalesDB get_db];
  3935. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3936. NSString* product_id=params[@"product_id"];
  3937. NSString *item_count_str = params[@"item_count"];
  3938. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  3939. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  3940. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3941. // NSString *sql = @"";
  3942. for(int i=0;i<arr.count;i++)
  3943. {
  3944. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  3945. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  3946. __block int cart_count = 0;
  3947. if (!item_count_str) {
  3948. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  3949. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3950. NSString *model_set = [self textAtColumn:0 statement:stmt];
  3951. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  3952. cart_count = [[model_set_components lastObject] integerValue];
  3953. }];
  3954. }
  3955. if(count==0)
  3956. {
  3957. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3958. sqlite3_stmt *stmt;
  3959. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  3960. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  3961. if (item_count_arr) {
  3962. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  3963. } else {
  3964. sqlite3_bind_int(stmt,2,cart_count);
  3965. }
  3966. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3967. [iSalesDB execSql:@"ROLLBACK" db:db];
  3968. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3969. [iSalesDB close_db:db];
  3970. DebugLog(@"add to wishlist error");
  3971. return ret;
  3972. }
  3973. } else {
  3974. int qty = 0;
  3975. if (item_count_arr) {
  3976. qty = [item_count_arr[i] integerValue];
  3977. } else {
  3978. qty = cart_count;
  3979. }
  3980. sqlQuery = [NSString stringWithFormat:@"update wishlist set qty = 1 where product_id = %@ and (is_delete is null or is_delete != 1);update wishlist set qty = %d,is_delete = 0 where product_id = %@ and is_delete = 1;",arr[i],qty,arr[i]];
  3981. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  3982. [iSalesDB execSql:@"ROLLBACK" db:db];
  3983. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  3984. [iSalesDB close_db:db];
  3985. DebugLog(@"add to wishlist error");
  3986. return ret;
  3987. }
  3988. }
  3989. }
  3990. // [iSalesDB execSql:sql db:db];
  3991. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3992. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3993. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  3994. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  3995. [iSalesDB close_db:db];
  3996. appDelegate.wish_count =count;
  3997. [appDelegate update_count_mark];
  3998. ret[@"result"]= [NSNumber numberWithInt:2];
  3999. return ret;
  4000. }
  4001. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  4002. // 0 category
  4003. // 1 search
  4004. // 2 itemsearch
  4005. NSData *ret = nil;
  4006. NSDictionary *items = nil;
  4007. switch (from) {
  4008. case 0:{
  4009. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  4010. }
  4011. break;
  4012. case 1:{
  4013. items = [[self search:params limited:NO] objectForKey:@"items"];
  4014. }
  4015. break;
  4016. case 2:{
  4017. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  4018. }
  4019. break;
  4020. default:
  4021. break;
  4022. }
  4023. if (!items) {
  4024. return ret;
  4025. }
  4026. int count = [[items objectForKey:@"count"] intValue];
  4027. NSMutableString *product_id_str = [@"" mutableCopy];
  4028. for (int i = 0; i < count; i++) {
  4029. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  4030. NSDictionary *item = [items objectForKey:key];
  4031. NSString *product_id = [item objectForKey:@"product_id"];
  4032. if (i == 0) {
  4033. [product_id_str appendString:product_id];
  4034. } else {
  4035. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  4036. }
  4037. }
  4038. NSString *add_to = [self valueInParams:params key:@"addTo"];
  4039. if ([add_to isEqualToString:@"cart"]) {
  4040. NSString *order_code = [params objectForKey:@"orderCode"];
  4041. if (order_code.length) {
  4042. NSDictionary *newParams = @{
  4043. @"product_id" : product_id_str,
  4044. @"orderCode" : order_code
  4045. };
  4046. ret = [self offline_add2cart:newParams.mutableCopy];
  4047. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4048. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4049. ret = [RAUtils dict2data:retDic];
  4050. }
  4051. } else if([add_to isEqualToString:@"wishlist"]) {
  4052. NSDictionary *newParams = @{
  4053. @"product_id" : product_id_str
  4054. };
  4055. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  4056. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4057. ret = [RAUtils dict2data:retDic];
  4058. } else if([add_to isEqualToString:@"portfolio"]) {
  4059. NSDictionary *newParams = @{
  4060. @"product_id" : product_id_str
  4061. };
  4062. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  4063. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4064. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4065. ret = [RAUtils dict2data:retDic];
  4066. }
  4067. return ret;
  4068. }
  4069. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  4070. {
  4071. return [self addAll:params from:0];
  4072. }
  4073. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  4074. {
  4075. return [self addAll:params from:1];
  4076. }
  4077. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  4078. {
  4079. return [self addAll:params from:2];
  4080. }
  4081. #pragma mark - Jack
  4082. #warning 做SQL操作时转义!!
  4083. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  4084. // "val_227" : {
  4085. // "check" : 1,
  4086. // "value" : "US United States",
  4087. // "value_id" : "228"
  4088. // },
  4089. if (!countryCode) {
  4090. countryCode = @"US";
  4091. }
  4092. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  4093. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  4094. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  4095. int code_id = sqlite3_column_int(stmt, 3); // id
  4096. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4097. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  4098. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  4099. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4100. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  4101. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4102. }
  4103. long n = *count;
  4104. *count = n + 1;
  4105. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  4106. [container setValue:countryDic forKey:key];
  4107. }] mutableCopy];
  4108. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4109. return ret;
  4110. }
  4111. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  4112. countryCode = [self translateSingleQuote:countryCode];
  4113. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  4114. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4115. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  4116. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  4117. if (name == NULL) {
  4118. name = "";
  4119. }
  4120. if (code == NULL) {
  4121. code = "";
  4122. }
  4123. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4124. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4125. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4126. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4127. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  4128. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4129. }
  4130. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4131. [container setValue:stateDic forKey:key];
  4132. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  4133. DebugLog(@"query all state error: %@",err_msg);
  4134. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4135. // [stateDic setValue:@"Other" forKey:@"value"];
  4136. // [stateDic setValue:@"" forKey:@"value_id"];
  4137. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4138. //
  4139. // if (state_code && [@"" isEqualToString:state_code]) {
  4140. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4141. // }
  4142. //
  4143. // NSString *key = [NSString stringWithFormat:@"val_0"];
  4144. // [container setValue:stateDic forKey:key];
  4145. }] mutableCopy];
  4146. [ret removeObjectForKey:@"result"];
  4147. // failure 可以不用了,一样的
  4148. if (ret.allKeys.count == 0) {
  4149. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4150. [stateDic setValue:@"Other" forKey:@"value"];
  4151. [stateDic setValue:@"" forKey:@"value_id"];
  4152. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4153. if (state_code && [@"" isEqualToString:state_code]) {
  4154. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4155. }
  4156. NSString *key = [NSString stringWithFormat:@"val_0"];
  4157. [ret setValue:stateDic forKey:key];
  4158. }
  4159. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4160. return ret;
  4161. }
  4162. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  4163. codeId = [self translateSingleQuote:codeId];
  4164. NSString *sql = [NSString stringWithFormat:@"select name,code from offline_state where country_code = (select code as country_code FROM offline_country where countrycode_id = %@);",codeId];
  4165. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4166. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  4167. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  4168. if (name == NULL) {
  4169. name = "";
  4170. }
  4171. if (code == NULL) {
  4172. code = "";
  4173. }
  4174. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4175. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4176. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4177. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4178. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  4179. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4180. }
  4181. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4182. [container setValue:stateDic forKey:key];
  4183. }] mutableCopy];
  4184. [ret removeObjectForKey:@"result"];
  4185. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4186. return ret;
  4187. }
  4188. + (NSDictionary *)offline_getPrice {
  4189. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  4190. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4191. char *name = (char *) sqlite3_column_text(stmt, 1);
  4192. int type = sqlite3_column_int(stmt, 2);
  4193. int orderBy = sqlite3_column_int(stmt, 3);
  4194. if (name == NULL) {
  4195. name = "";
  4196. }
  4197. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  4198. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4199. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  4200. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4201. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4202. if (orderBy == 0) {
  4203. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4204. }
  4205. [container setValue:priceDic forKey:key];
  4206. }] mutableCopy];
  4207. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4208. return ret;
  4209. }
  4210. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  4211. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  4212. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  4213. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  4214. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4215. int _id = sqlite3_column_int(stmt, 0);
  4216. NSString *name = [self textAtColumn:1 statement:stmt];
  4217. NSDictionary *typeDic = @{
  4218. @"value_id" : name,
  4219. @"value" : name,
  4220. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  4221. };
  4222. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  4223. [ret setValue:[NSNumber numberWithInt:++(*count)] forKey:@"count"];
  4224. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  4225. }];
  4226. return ret;
  4227. }
  4228. + (NSDictionary *)offline_getSalesRep {
  4229. // 首先从offline_login表中取出sales_code
  4230. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4231. NSString *user = app.user;
  4232. user = [self translateSingleQuote:user];
  4233. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  4234. __block NSString *user_code = @"";
  4235. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4236. char *code = (char *)sqlite3_column_text(stmt, 0);
  4237. if (code == NULL) {
  4238. code = "";
  4239. }
  4240. user_code = [NSString stringWithUTF8String:code];
  4241. }];
  4242. // 再取所有salesRep
  4243. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  4244. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4245. // 1 name 2 code 3 salesrep_id
  4246. char *name = (char *)sqlite3_column_text(stmt, 1);
  4247. char *code = (char *)sqlite3_column_text(stmt, 2);
  4248. int salesrep_id = sqlite3_column_int(stmt, 3);
  4249. if (name == NULL) {
  4250. name = "";
  4251. }
  4252. if (code == NULL) {
  4253. code = "";
  4254. }
  4255. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  4256. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  4257. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  4258. // 比较code 相等则check
  4259. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  4260. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4261. }
  4262. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  4263. }] mutableCopy];
  4264. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4265. return ret;
  4266. }
  4267. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  4268. zipcode = [self translateSingleQuote:zipcode];
  4269. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  4270. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4271. char *country = (char *)sqlite3_column_text(stmt, 0);
  4272. char *state = (char *)sqlite3_column_text(stmt, 1);
  4273. char *city = (char *)sqlite3_column_text(stmt, 2);
  4274. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  4275. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  4276. if (country == NULL) {
  4277. country = "";
  4278. }
  4279. if (state == NULL) {
  4280. state = "";
  4281. }
  4282. if (city == NULL) {
  4283. city = "";
  4284. }
  4285. if (country_code == NULL) {
  4286. country_code = "";
  4287. }
  4288. if (state_code == NULL) {
  4289. state_code = "";
  4290. }
  4291. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  4292. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  4293. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  4294. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  4295. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  4296. }] mutableCopy];
  4297. return ret;
  4298. }
  4299. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4300. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  4301. [item setValue:value forKey:valueKey];
  4302. [dic setValue:item forKey:itemKey];
  4303. }
  4304. + (NSString *)countryCodeByid:(NSString *)code_id {
  4305. NSString *ret = nil;
  4306. code_id = [self translateSingleQuote:code_id];
  4307. sqlite3 *db = [iSalesDB get_db];
  4308. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  4309. sqlite3_stmt * statement;
  4310. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4311. while (sqlite3_step(statement) == SQLITE_ROW) {
  4312. char *code = (char *)sqlite3_column_text(statement, 0);
  4313. if (code == NULL) {
  4314. code = "";
  4315. }
  4316. ret = [NSString stringWithUTF8String:code];
  4317. }
  4318. sqlite3_finalize(statement);
  4319. }
  4320. [iSalesDB close_db:db];
  4321. return ret;
  4322. }
  4323. + (NSString *)countryCodeIdByCode:(NSString *)code {
  4324. NSString *ret = nil;
  4325. code = [self translateSingleQuote:code];
  4326. sqlite3 *db = [iSalesDB get_db];
  4327. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  4328. sqlite3_stmt * statement;
  4329. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4330. while (sqlite3_step(statement) == SQLITE_ROW) {
  4331. char *_id = (char *)sqlite3_column_text(statement, 0);
  4332. if (_id == NULL) {
  4333. _id = "";
  4334. }
  4335. ret = [NSString stringWithFormat:@"%s",_id];
  4336. }
  4337. sqlite3_finalize(statement);
  4338. }
  4339. [iSalesDB close_db:db];
  4340. return ret;
  4341. }
  4342. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  4343. NSString *name = nil;
  4344. codeId = [self translateSingleQuote:codeId];
  4345. sqlite3 *db = [iSalesDB get_db];
  4346. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  4347. sqlite3_stmt * statement;
  4348. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4349. while (sqlite3_step(statement) == SQLITE_ROW) {
  4350. char *value = (char *)sqlite3_column_text(statement, 0);
  4351. if (value == NULL) {
  4352. value = "";
  4353. }
  4354. name = [NSString stringWithUTF8String:value];
  4355. }
  4356. sqlite3_finalize(statement);
  4357. }
  4358. [iSalesDB close_db:db];
  4359. return name;
  4360. }
  4361. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  4362. NSString *ret = nil;
  4363. sqlite3 *db = [iSalesDB get_db];
  4364. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  4365. sqlite3_stmt * statement;
  4366. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4367. while (sqlite3_step(statement) == SQLITE_ROW) {
  4368. char *name = (char *)sqlite3_column_text(statement, 0);
  4369. if (name == NULL) {
  4370. name = "";
  4371. }
  4372. ret = [NSString stringWithUTF8String:name];
  4373. }
  4374. sqlite3_finalize(statement);
  4375. }
  4376. [iSalesDB close_db:db];
  4377. return ret;
  4378. }
  4379. + (NSString *)salesRepCodeById:(NSString *)_id {
  4380. NSString *ret = nil;
  4381. _id = [self translateSingleQuote:_id];
  4382. sqlite3 *db = [iSalesDB get_db];
  4383. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  4384. sqlite3_stmt * statement;
  4385. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4386. while (sqlite3_step(statement) == SQLITE_ROW) {
  4387. char *rep = (char *)sqlite3_column_text(statement, 0);
  4388. if (rep == NULL) {
  4389. rep = "";
  4390. }
  4391. ret = [NSString stringWithUTF8String:rep];
  4392. }
  4393. sqlite3_finalize(statement);
  4394. }
  4395. [iSalesDB close_db:db];
  4396. return ret;
  4397. }
  4398. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  4399. char *tx = (char *)sqlite3_column_text(stmt, col);
  4400. if (tx == NULL) {
  4401. tx = "";
  4402. }
  4403. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  4404. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  4405. if (!text) {
  4406. text = @"";
  4407. }
  4408. // 将字符全部为' '的字符串干掉
  4409. int spaceCount = 0;
  4410. for (int i = 0; i < text.length; i++) {
  4411. if ([text characterAtIndex:i] == ' ') {
  4412. spaceCount++;
  4413. }
  4414. }
  4415. if (spaceCount == text.length) {
  4416. text = @"";
  4417. }
  4418. return text;
  4419. }
  4420. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  4421. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4422. NSData *data = [NSData dataWithContentsOfFile:path];
  4423. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4424. return ret;
  4425. }
  4426. + (NSString *)textFileName:(NSString *)name {
  4427. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4428. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  4429. if (!text) {
  4430. text = @"";
  4431. }
  4432. return text;
  4433. }
  4434. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  4435. return [[dic objectForKey:key] mutableCopy];
  4436. }
  4437. + (id)translateSingleQuote:(NSString *)string {
  4438. if ([string isKindOfClass:[NSString class]])
  4439. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4440. return string;
  4441. }
  4442. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  4443. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  4444. }
  4445. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  4446. NSString* ret= nil;
  4447. NSString *sqlQuery = nil;
  4448. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.item_id=%@ order by i.default_img desc, i._id asc limit 1;",item_id];// select i.url from model m
  4449. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ order by i.default_img desc, i._id asc limit 1;",item_id];
  4450. sqlite3_stmt * statement;
  4451. // int count=0;
  4452. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4453. {
  4454. if (sqlite3_step(statement) == SQLITE_ROW)
  4455. {
  4456. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  4457. if(imgurl==nil)
  4458. imgurl="";
  4459. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  4460. ret=nsimgurl;
  4461. }
  4462. sqlite3_finalize(statement);
  4463. }
  4464. else
  4465. {
  4466. [ret setValue:@"8" forKey:@"result"];
  4467. }
  4468. // [iSalesDB close_db:db];
  4469. // DebugLog(@"data string: %@",ret );
  4470. return ret;
  4471. }
  4472. #pragma mark contact list
  4473. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  4474. {
  4475. // contactType = "Sales_Order_Customer";
  4476. // limit = 25;
  4477. // offset = 0;
  4478. // password = 123456;
  4479. // "price_name" = 16;
  4480. // user = EvanK;
  4481. sqlite3 *db = [iSalesDB get_db];
  4482. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  4483. if (contactType) {
  4484. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  4485. } else {
  4486. contactType = @"1 = 1";
  4487. }
  4488. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  4489. DebugLog(@"offline contact list keyword: %@",keyword);
  4490. // advanced search
  4491. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  4492. if (contact_name) {
  4493. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4494. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  4495. } else {
  4496. contact_name = @"";
  4497. }
  4498. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  4499. if (customer_phone) {
  4500. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4501. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  4502. } else {
  4503. customer_phone = @"";
  4504. }
  4505. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  4506. if (customer_fax) {
  4507. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4508. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  4509. } else {
  4510. customer_fax = @"";
  4511. }
  4512. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  4513. if (customer_zipcode) {
  4514. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4515. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  4516. } else {
  4517. customer_zipcode = @"";
  4518. }
  4519. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  4520. if (customer_sales_rep) {
  4521. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4522. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  4523. } else {
  4524. customer_sales_rep = @"";
  4525. }
  4526. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  4527. if (customer_state) {
  4528. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4529. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  4530. } else {
  4531. customer_state = @"";
  4532. }
  4533. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  4534. if (customer_name) {
  4535. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4536. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  4537. } else {
  4538. customer_name = @"";
  4539. }
  4540. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  4541. if (customer_country) {
  4542. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4543. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  4544. } else {
  4545. customer_country = @"";
  4546. }
  4547. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  4548. if (customer_cid) {
  4549. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4550. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  4551. } else {
  4552. customer_cid = @"";
  4553. }
  4554. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  4555. if (customer_city) {
  4556. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4557. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  4558. } else {
  4559. customer_city = @"";
  4560. }
  4561. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  4562. if (customer_address) {
  4563. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4564. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  4565. } else {
  4566. customer_address = @"";
  4567. }
  4568. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  4569. if (customer_email) {
  4570. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4571. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  4572. } else {
  4573. customer_email = @"";
  4574. }
  4575. NSString *price_name = [params valueForKey:@"price_name"];
  4576. if (price_name) {
  4577. if ([price_name containsString:@","]) {
  4578. // 首先从 price表中查处name
  4579. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  4580. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  4581. for (int i = 1;i < pArray.count;i++) {
  4582. NSString *p = pArray[i];
  4583. [mutablePStr appendFormat:@" or type = %@ ",p];
  4584. }
  4585. [mutablePStr appendString:@";"];
  4586. __block NSMutableArray *price_name_array = [NSMutableArray array];
  4587. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4588. char *name = (char *)sqlite3_column_text(stmt, 0);
  4589. if (!name)
  4590. name = "";
  4591. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  4592. }];
  4593. // 再根据name 拼sql
  4594. NSMutableString *mutable_price_name = [NSMutableString string];
  4595. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  4596. for (int i = 1; i < price_name_array.count; i++) {
  4597. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  4598. }
  4599. [mutable_price_name appendString:@")"];
  4600. price_name = mutable_price_name;
  4601. } else {
  4602. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  4603. if ([price_name isEqualToString:@""]) {
  4604. price_name = @"";
  4605. } else {
  4606. __block NSString *price;
  4607. price_name = [self translateSingleQuote:price_name];
  4608. [iSalesDB jk_query:[NSString stringWithFormat:@"SELECT name FROM price where type = %@ ;",price_name] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4609. char *p = (char *)sqlite3_column_text(stmt, 0);
  4610. if (p == NULL) {
  4611. p = "";
  4612. }
  4613. price = [NSString stringWithUTF8String:p];
  4614. }];
  4615. if ([price isEqualToString:@""]) {
  4616. price_name = @"";
  4617. } else {
  4618. price = [self translateSingleQuote:price];
  4619. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  4620. }
  4621. }
  4622. }
  4623. } else {
  4624. price_name = @"";
  4625. }
  4626. int limit = [[params valueForKey:@"limit"] intValue];
  4627. int offset = [[params valueForKey:@"offset"] intValue];
  4628. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4629. NSString *where = [NSString stringWithFormat:@" %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ and is_active=1 ",contactType,contact_name,customer_phone,customer_fax,customer_zipcode,customer_sales_rep,customer_state,customer_name,customer_country,customer_cid,customer_city,customer_address,customer_email,price_name];
  4630. NSString *sql = [NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@ order by decrypt(company_name) limit %d offset %d",where,limit,offset];
  4631. // int result= [iSalesDB AddExFunction:db];
  4632. int count =0;
  4633. NSString *sqlQuery = nil;
  4634. if(keyword.length==0)
  4635. {
  4636. // sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@=1 order by decrypt(company_name) limit %d offset %d",contactType , limit, offset];
  4637. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  4638. sqlQuery = sql;
  4639. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  4640. }
  4641. else
  4642. {
  4643. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  4644. keyword = keyword.lowercaseString;
  4645. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4646. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@ and (lower(contact_id ) like'%%%@%%' or lower(decrypt(company_name)) like '%%%@%%' or lower(decrypt(phone)) like '%%%@%%' or lower(decrypt(addr)) like '%%%@%%') and is_active=1 order by decrypt(company_name) limit %d offset %d",contactType , keyword,keyword,keyword,keyword,limit, offset];
  4647. count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@=1 and (lower(contact_id ) like'%@' or lower(company_name) like'%@') and is_active=1",contactType,keyword,keyword]];
  4648. }
  4649. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4650. sqlite3_stmt * statement;
  4651. [ret setValue:@"2" forKey:@"result"];
  4652. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  4653. // sqlQuery = @"select editable,decrypt(company_name),country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where Sales_Order_Customer = 1 and is_active=1 order by decrypt(company_name) limit 25 offset 0";
  4654. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4655. {
  4656. int i = 0;
  4657. while (sqlite3_step(statement) == SQLITE_ROW)
  4658. {
  4659. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4660. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4661. int editable = sqlite3_column_int(statement, 0);
  4662. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4663. NSString *nscompany_name =nil;
  4664. if(company_name==nil)
  4665. nscompany_name=@"";
  4666. else
  4667. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  4668. char *country = (char*)sqlite3_column_text(statement, 2);
  4669. if(country==nil)
  4670. country="";
  4671. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4672. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4673. // if(addr==nil)
  4674. // addr="";
  4675. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4676. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4677. if(zipcode==nil)
  4678. zipcode="";
  4679. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4680. char *state = (char*)sqlite3_column_text(statement, 5);
  4681. if(state==nil)
  4682. state="";
  4683. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4684. char *city = (char*)sqlite3_column_text(statement, 6);
  4685. if(city==nil)
  4686. city="";
  4687. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4688. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4689. // NSString *nscontact_name = nil;
  4690. // if(contact_name==nil)
  4691. // nscontact_name=@"";
  4692. // else
  4693. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4694. char *phone = (char*)sqlite3_column_text(statement, 8);
  4695. NSString *nsphone = nil;
  4696. if(phone==nil)
  4697. nsphone=@"";
  4698. else
  4699. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4700. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4701. if(contact_id==nil)
  4702. contact_id="";
  4703. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4704. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4705. if(addr_1==nil)
  4706. addr_1="";
  4707. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4708. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4709. if(addr_2==nil)
  4710. addr_2="";
  4711. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4712. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4713. if(addr_3==nil)
  4714. addr_3="";
  4715. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4716. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4717. if(addr_4==nil)
  4718. addr_4="";
  4719. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4720. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4721. if(first_name==nil)
  4722. first_name="";
  4723. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4724. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4725. if(last_name==nil)
  4726. last_name="";
  4727. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4728. char *fax = (char*)sqlite3_column_text(statement, 16);
  4729. NSString *nsfax = nil;
  4730. if(fax==nil)
  4731. nsfax=@"";
  4732. else
  4733. {
  4734. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4735. if(nsfax.length>0)
  4736. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  4737. }
  4738. char *email = (char*)sqlite3_column_text(statement, 17);
  4739. NSString *nsemail = nil;
  4740. if(email==nil)
  4741. nsemail=@"";
  4742. else
  4743. {
  4744. nsemail= [[NSString alloc]initWithUTF8String:email];
  4745. if(nsemail.length>0)
  4746. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  4747. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  4748. }
  4749. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4750. [arr_name addObject:nsfirst_name];
  4751. [arr_name addObject:nslast_name];
  4752. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  4753. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4754. {
  4755. // decrypt
  4756. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4757. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4758. nsphone=[AESCrypt fastdecrypt:nsphone];
  4759. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4760. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4761. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4762. }
  4763. [arr_addr addObject:nscompany_name];
  4764. [arr_addr addObject:nscontact_name];
  4765. [arr_addr addObject:@"<br>"];
  4766. [arr_addr addObject:nsaddr_1];
  4767. [arr_addr addObject:nsaddr_2];
  4768. [arr_addr addObject:nsaddr_3];
  4769. [arr_addr addObject:nsaddr_4];
  4770. //[arr_addr addObject:nsaddr];
  4771. [arr_addr addObject:nszipcode];
  4772. [arr_addr addObject:nscity];
  4773. [arr_addr addObject:nsstate];
  4774. [arr_addr addObject:nscountry];
  4775. [arr_addr addObject:@"<br>"];
  4776. [arr_addr addObject:nsphone];
  4777. [arr_addr addObject:nsfax];
  4778. [arr_addr addObject:nsemail];
  4779. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  4780. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  4781. [item setValue:name forKey:@"name"];
  4782. [item setValue:nscontact_id forKey:@"contact_id"];
  4783. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  4784. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4785. i++;
  4786. }
  4787. UIApplication * app = [UIApplication sharedApplication];
  4788. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4789. [ret setValue:appDelegate.mode forKey:@"mode"];
  4790. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  4791. sqlite3_finalize(statement);
  4792. }
  4793. [iSalesDB close_db:db];
  4794. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4795. return ret;
  4796. }
  4797. #pragma mark contact Advanced search
  4798. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  4799. {
  4800. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  4801. return [RAUtils dict2data:contactAdvanceDic];
  4802. }
  4803. #pragma mark create new contact
  4804. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  4805. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  4806. NSData *data = [NSData dataWithContentsOfFile:path];
  4807. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4808. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4809. NSString *countryCode = nil;
  4810. NSString *countryCode_id = nil;
  4811. NSString *stateCode = nil;
  4812. NSString *city = nil;
  4813. NSString *zipCode = nil;
  4814. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4815. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  4816. NSString *code_id = params[@"country"];
  4817. countryCode_id = code_id;
  4818. countryCode = [self countryCodeByid:code_id];
  4819. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  4820. NSString *zip_code = params[@"zipcode"];
  4821. // 剔除全部为空格
  4822. int spaceCount = 0;
  4823. for (int i = 0; i < zip_code.length; i++) {
  4824. if ([zip_code characterAtIndex:i] == ' ') {
  4825. spaceCount++;
  4826. }
  4827. }
  4828. if (spaceCount == zip_code.length) {
  4829. zip_code = @"";
  4830. }
  4831. zipCode = zip_code;
  4832. if (zipCode.length > 0) {
  4833. countryCode_id = params[@"country"];
  4834. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4835. countryCode = [dic valueForKey:@"country_code"];
  4836. if (!countryCode) {
  4837. // countryCode = @"US";
  4838. NSString *code_id = params[@"country"];
  4839. countryCode = [self countryCodeByid:code_id];
  4840. }
  4841. stateCode = [dic valueForKey:@"state_code"];
  4842. if (!stateCode.length) {
  4843. stateCode = params[@"state"];
  4844. }
  4845. city = [dic valueForKey:@"city"];
  4846. if (!city.length) {
  4847. city = params[@"city"];
  4848. }
  4849. // zip code
  4850. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  4851. [zipDic setValue:zipCode forKey:@"value"];
  4852. [section_0 setValue:zipDic forKey:@"item_11"];
  4853. } else {
  4854. NSString *code_id = params[@"country"];
  4855. countryCode = [self countryCodeByid:code_id];
  4856. stateCode = params[@"state"];
  4857. city = params[@"city"];
  4858. }
  4859. }
  4860. } else {
  4861. // default: US United States
  4862. countryCode = @"US";
  4863. countryCode_id = @"228";
  4864. }
  4865. // country
  4866. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4867. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  4868. // state
  4869. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4870. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  4871. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  4872. // city
  4873. if (city) {
  4874. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  4875. [cityDic setValue:city forKey:@"value"];
  4876. [section_0 setValue:cityDic forKey:@"item_13"];
  4877. }
  4878. // price type
  4879. NSDictionary *priceDic = [self offline_getPrice];
  4880. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  4881. // contact type
  4882. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  4883. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  4884. // Sales Rep
  4885. NSDictionary *repDic = [self offline_getSalesRep];
  4886. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4887. [ret setValue:section_0 forKey:@"section_0"];
  4888. return [RAUtils dict2data:ret];
  4889. }
  4890. #pragma mark save
  4891. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  4892. NSString *addr = nil;
  4893. NSString *contact_name = nil;
  4894. NSString *companyName = [params objectForKey:@"company"];
  4895. if (companyName) {
  4896. companyName = [AESCrypt fastencrypt:companyName];
  4897. } else {
  4898. companyName = @"";
  4899. }
  4900. DebugLog(@"company");
  4901. companyName = [self translateSingleQuote:companyName];
  4902. NSString *addr1 = [params objectForKey:@"address"];
  4903. NSString *addr2 = [params objectForKey:@"address2"];
  4904. NSString *addr3 = [params objectForKey:@"address_3"];
  4905. NSString *addr4 = [params objectForKey:@"address_4"];
  4906. if (!addr2) {
  4907. addr2 = @"";
  4908. }
  4909. if (!addr3) {
  4910. addr3 = @"";
  4911. }
  4912. if (!addr4) {
  4913. addr4 = @"";
  4914. }
  4915. if (!addr1) {
  4916. addr1 = @"";
  4917. }
  4918. DebugLog(@"addr");
  4919. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  4920. addr = [AESCrypt fastencrypt:addr];
  4921. addr = [self translateSingleQuote:addr];
  4922. if (addr1 && ![addr1 isEqualToString:@""]) {
  4923. addr1 = [AESCrypt fastencrypt:addr1];
  4924. }
  4925. addr1 = [self translateSingleQuote:addr1];
  4926. addr2 = [self translateSingleQuote:addr2];
  4927. addr3 = [self translateSingleQuote:addr3];
  4928. addr4 = [self translateSingleQuote:addr4];
  4929. NSString *country = [params objectForKey:@"country"];
  4930. if (country) {
  4931. country = [self countryNameByCountryCodeId:country];
  4932. } else {
  4933. country = @"";
  4934. }
  4935. DebugLog(@"country");
  4936. country = [self translateSingleQuote:country];
  4937. NSString *state = [params objectForKey:@"state"];
  4938. if (!state) {
  4939. state = @"";
  4940. }
  4941. DebugLog(@"state");
  4942. state = [self translateSingleQuote:state];
  4943. NSString *city = [params objectForKey:@"city"];
  4944. if (!city) {
  4945. city = @"";
  4946. }
  4947. city = [self translateSingleQuote:city];
  4948. NSString *zipcode = [params objectForKey:@"zipcode"];
  4949. if (!zipcode) {
  4950. zipcode = @"";
  4951. }
  4952. DebugLog(@"zip");
  4953. zipcode = [self translateSingleQuote:zipcode];
  4954. NSString *fistName = [params objectForKey:@"firstname"];
  4955. if (!fistName) {
  4956. fistName = @"";
  4957. }
  4958. NSString *lastName = [params objectForKey:@"lastname"];
  4959. if (!lastName) {
  4960. lastName = @"";
  4961. }
  4962. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  4963. DebugLog(@"contact_name");
  4964. contact_name = [self translateSingleQuote:contact_name];
  4965. fistName = [self translateSingleQuote:fistName];
  4966. lastName = [self translateSingleQuote:lastName];
  4967. NSString *phone = [params objectForKey:@"phone"];
  4968. if (phone) {
  4969. phone = [AESCrypt fastencrypt:phone];
  4970. } else {
  4971. phone = @"";
  4972. }
  4973. DebugLog(@"PHONE");
  4974. phone = [self translateSingleQuote:phone];
  4975. NSString *fax = [params objectForKey:@"fax"];
  4976. if (!fax) {
  4977. fax = @"";
  4978. }
  4979. DebugLog(@"FAX");
  4980. fax = [self translateSingleQuote:fax];
  4981. NSString *email = [params objectForKey:@"email"];
  4982. if (!email) {
  4983. email = @"";
  4984. }
  4985. DebugLog(@"EMAIL:%@",email);
  4986. email = [self translateSingleQuote:email];
  4987. NSString *notes = [params objectForKey:@"contact_notes"];
  4988. if (!notes) {
  4989. notes = @"";
  4990. }
  4991. DebugLog(@"NOTE:%@",notes);
  4992. notes = [self translateSingleQuote:notes];
  4993. NSString *price = [params objectForKey:@"price_name"];
  4994. if (price) {
  4995. price = [self priceNameByPriceId:price];
  4996. } else {
  4997. price = @"";
  4998. }
  4999. DebugLog(@"PRICE");
  5000. price = [self translateSingleQuote:price];
  5001. NSString *salesRep = [params objectForKey:@"sales_rep"];
  5002. if (salesRep) {
  5003. salesRep = [self salesRepCodeById:salesRep];
  5004. } else {
  5005. salesRep = @"";
  5006. }
  5007. salesRep = [self translateSingleQuote:salesRep];
  5008. NSString *img = [params objectForKey:@"business_card"];
  5009. NSArray *array = [img componentsSeparatedByString:@","];
  5010. NSString *img_0 = array[0];
  5011. if (!img_0) {
  5012. img_0 = @"";
  5013. }
  5014. img_0 = [self translateSingleQuote:img_0];
  5015. NSString *img_1 = array[1];
  5016. if (!img_1) {
  5017. img_1 = @"";
  5018. }
  5019. img_1 = [self translateSingleQuote:img_1];
  5020. NSString *img_2 = array[2];
  5021. if (!img_2) {
  5022. img_2 = @"";
  5023. }
  5024. img_2 = [self translateSingleQuote:img_2];
  5025. NSString *contact_id = [NSUUID UUID].UUIDString;
  5026. NSString *contact_type = [params objectForKey:@"type_name"];
  5027. if (!contact_type) {
  5028. contact_type = @"";
  5029. }
  5030. contact_type = [self translateSingleQuote:contact_type];
  5031. // 判断更新时是否为customer
  5032. if (update) {
  5033. contact_id = [params objectForKey:@"contact_id"];
  5034. if (!contact_id) {
  5035. contact_id = @"";
  5036. }
  5037. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5038. __block int customer = 0;
  5039. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5040. customer = sqlite3_column_int(stmt, 0);
  5041. }];
  5042. isCustomer = customer ? YES : NO;
  5043. }
  5044. NSMutableDictionary *sync_dic = [params mutableCopy];
  5045. if (isCustomer) {
  5046. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5047. } else {
  5048. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5049. }
  5050. NSString *sync_data = nil;
  5051. NSString *sql = nil;
  5052. if (update){
  5053. contact_id = [params objectForKey:@"contact_id"];
  5054. if (!contact_id) {
  5055. contact_id = @"";
  5056. }
  5057. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5058. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5059. sync_data = [RAUtils dict2string:sync_dic];
  5060. sync_data = [self translateSingleQuote:sync_data];
  5061. sql = [NSString stringWithFormat:@"update offline_contact set company_name = '%@',addr_1 = '%@',addr_2 = '%@',addr_3 = '%@',addr_4 = '%@',country = '%@',state = '%@',city = '%@',zipcode = '%@',first_name = '%@',last_name = '%@',phone = '%@',fax = '%@',email = '%@',notes = '%@',price_type = '%@',sales_rep = '%@',img_0 = '%@',img_1 = '%@',img_2 = '%@',sync_data = '%@',contact_name = '%@',addr = '%@',type = '%@' where contact_id = '%@';",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,sync_data,contact_name,addr,contact_type,contact_id];
  5062. } else {
  5063. contact_id = [self translateSingleQuote:contact_id];
  5064. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5065. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5066. sync_data = [RAUtils dict2string:sync_dic];
  5067. sync_data = [self translateSingleQuote:sync_data];
  5068. sql = [NSString stringWithFormat:@"insert into offline_contact (is_active,company_name,addr_1,addr_2,addr_3,addr_4,country,state,city,zipcode,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,editable,contact_id,Sales_Order_Customer,Sales_Order_Freight_Bill_To,Sales_Order_Ship_From,Sales_Order_Merchandise_Bill_To,Contact_Return_To,Sales_Order_Ship_To,sync_data,contact_name,addr,type) values (1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',1,'%@',%d,%d,0,%d,0,1,'%@','%@','%@','%@')",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,contact_id,isCustomer ? 1 : 0,isCustomer ? 1 : 0,isCustomer ? 1 : 0,sync_data,contact_name,addr,contact_type];
  5069. }
  5070. int result = [iSalesDB execSql:sql];
  5071. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5072. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5073. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5074. }
  5075. #pragma mark save new contact
  5076. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5077. {
  5078. return [self offline_saveContact:params update:NO isCustomer:YES];
  5079. }
  5080. #pragma mark edit contact
  5081. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5082. {
  5083. // {
  5084. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5085. // password = 123456;
  5086. // user = EvanK;
  5087. // }
  5088. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5089. NSData *data = [NSData dataWithContentsOfFile:path];
  5090. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5091. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5092. NSString *countryCode = nil;
  5093. NSString *countryCode_id = nil;
  5094. NSString *stateCode = nil;
  5095. /*------contact infor------*/
  5096. __block NSString *country = nil;
  5097. __block NSString *company_name = nil;
  5098. __block NSString *contact_id = params[@"contact_id"];
  5099. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5100. __block NSString *zipcode = nil;
  5101. __block NSString *state = nil; // state_code
  5102. __block NSString *city = nil; //
  5103. __block NSString *firt_name,*last_name;
  5104. __block NSString *phone,*fax,*email;
  5105. __block NSString *notes,*price_type,*sales_rep;
  5106. __block NSString *img_0,*img_1,*img_2;
  5107. __block NSString *contact_type;
  5108. contact_id = [self translateSingleQuote:contact_id];
  5109. NSString *sql = [NSString stringWithFormat:@"select country,company_name,addr_1,addr_2,addr_3,addr_4,zipcode,state,city,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,type from offline_contact where contact_id = '%@';",contact_id];
  5110. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5111. country = [self textAtColumn:0 statement:stmt]; // country name
  5112. company_name = [self textAtColumn:1 statement:stmt];
  5113. addr_1 = [self textAtColumn:2 statement:stmt];
  5114. addr_2 = [self textAtColumn:3 statement:stmt];
  5115. addr_3 = [self textAtColumn:4 statement:stmt];
  5116. addr_4 = [self textAtColumn:5 statement:stmt];
  5117. zipcode = [self textAtColumn:6 statement:stmt];
  5118. state = [self textAtColumn:7 statement:stmt]; // state code
  5119. city = [self textAtColumn:8 statement:stmt];
  5120. firt_name = [self textAtColumn:9 statement:stmt];
  5121. last_name = [self textAtColumn:10 statement:stmt];
  5122. phone = [self textAtColumn:11 statement:stmt];
  5123. fax = [self textAtColumn:12 statement:stmt];
  5124. email = [self textAtColumn:13 statement:stmt];
  5125. notes = [self textAtColumn:14 statement:stmt];
  5126. price_type = [self textAtColumn:15 statement:stmt]; // name
  5127. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  5128. img_0 = [self textAtColumn:17 statement:stmt];
  5129. img_1 = [self textAtColumn:18 statement:stmt];
  5130. img_2 = [self textAtColumn:19 statement:stmt];
  5131. contact_type = [self textAtColumn:20 statement:stmt];
  5132. }];
  5133. // decrypt
  5134. if (company_name) {
  5135. company_name = [AESCrypt fastdecrypt:company_name];
  5136. }
  5137. if (addr_1) {
  5138. addr_1 = [AESCrypt fastdecrypt:addr_1];
  5139. }
  5140. if (phone) {
  5141. phone = [AESCrypt fastdecrypt:phone];
  5142. }
  5143. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  5144. countryCode = [iSalesDB jk_queryText:countrySql];
  5145. stateCode = state;
  5146. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5147. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  5148. NSString *code_id = params[@"country"];
  5149. countryCode_id = code_id;
  5150. countryCode = [self countryCodeByid:code_id];
  5151. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  5152. NSString *zip_code = params[@"zipcode"];
  5153. // 剔除全部为空格
  5154. int spaceCount = 0;
  5155. for (int i = 0; i < zip_code.length; i++) {
  5156. if ([zip_code characterAtIndex:i] == ' ') {
  5157. spaceCount++;
  5158. }
  5159. }
  5160. if (spaceCount == zip_code.length) {
  5161. zip_code = @"";
  5162. }
  5163. if (zipcode.length > 0) {
  5164. zipcode = zip_code;
  5165. countryCode_id = params[@"country"];
  5166. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5167. countryCode = [dic valueForKey:@"country_code"];
  5168. if (!countryCode) {
  5169. NSString *code_id = params[@"country"];
  5170. countryCode = [self countryCodeByid:code_id];
  5171. }
  5172. stateCode = [dic valueForKey:@"state_code"];
  5173. if (!stateCode.length) {
  5174. stateCode = params[@"state"];
  5175. }
  5176. city = [dic valueForKey:@"city"];
  5177. if (!city.length) {
  5178. city = params[@"city"];
  5179. }
  5180. // zip code
  5181. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5182. [zipDic setValue:zipcode forKey:@"value"];
  5183. [section_0 setValue:zipDic forKey:@"item_8"];
  5184. } else {
  5185. NSString *code_id = params[@"country"];
  5186. countryCode = [self countryCodeByid:code_id];
  5187. stateCode = params[@"state"];
  5188. city = params[@"city"];
  5189. }
  5190. }
  5191. }
  5192. // 0 Country
  5193. // 1 Company Name
  5194. // 2 Contact ID
  5195. // 3 Picture
  5196. // 4 Address 1
  5197. // 5 Address 2
  5198. // 6 Address 3
  5199. // 7 Address 4
  5200. // 8 Zip Code
  5201. // 9 State/Province
  5202. // 10 City
  5203. // 11 Contact First Name
  5204. // 12 Contact Last Name
  5205. // 13 Phone
  5206. // 14 Fax
  5207. // 15 Email
  5208. // 16 Contact Notes
  5209. // 17 Price Type
  5210. // 18 Contact Type
  5211. // 19 Sales Rep
  5212. // country
  5213. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5214. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5215. // company
  5216. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5217. // contact_id
  5218. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5219. // picture
  5220. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5221. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5222. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5223. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5224. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5225. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5226. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5227. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5228. // addr 1 2 3 4
  5229. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5230. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5231. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5232. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5233. // zip code
  5234. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5235. // state
  5236. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5237. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5238. // city
  5239. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5240. // first last
  5241. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5242. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5243. // phone fax email
  5244. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5245. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5246. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5247. // notes
  5248. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5249. // price
  5250. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5251. for (NSString *key in priceDic.allKeys) {
  5252. if ([key containsString:@"val_"]) {
  5253. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5254. if ([dic[@"value"] isEqualToString:price_type]) {
  5255. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5256. [priceDic setValue:dic forKey:key];
  5257. }
  5258. }
  5259. }
  5260. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5261. // Contact Rep
  5262. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5263. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5264. // Sales Rep
  5265. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  5266. for (NSString *key in repDic.allKeys) {
  5267. if ([key containsString:@"val_"]) {
  5268. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5269. NSString *value = dic[@"value"];
  5270. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5271. if (code && [code isEqualToString:sales_rep]) {
  5272. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5273. [repDic setValue:dic forKey:key];
  5274. }
  5275. }
  5276. }
  5277. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5278. [ret setValue:section_0 forKey:@"section_0"];
  5279. return [RAUtils dict2data:ret];
  5280. }
  5281. #pragma mark save contact
  5282. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5283. {
  5284. return [self offline_saveContact:params update:YES isCustomer:YES];
  5285. }
  5286. #pragma mark category
  5287. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5288. if (ck) {
  5289. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5290. for (NSString *key in res.allKeys) {
  5291. if (![key isEqualToString:@"count"]) {
  5292. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5293. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5294. if ([val[@"value"] isEqualToString:ck]) {
  5295. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5296. }
  5297. [res setValue:val forKey:key];
  5298. }
  5299. }
  5300. [dic setValue:res forKey:valueKey];
  5301. }
  5302. }
  5303. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5304. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5305. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5306. NSString* category = [params valueForKey:@"category"];
  5307. if (!category || [category isEqualToString:@""]) {
  5308. category = @"%";
  5309. }
  5310. category = [self translateSingleQuote:category];
  5311. int limit = [[params valueForKey:@"limit"] intValue];
  5312. int offset = [[params valueForKey:@"offset"] intValue];
  5313. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5314. NSString *limit_str = @"";
  5315. if (limited) {
  5316. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5317. }
  5318. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5319. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5320. sqlite3 *db = [iSalesDB get_db];
  5321. // [iSalesDB AddExFunction:db];
  5322. int count;
  5323. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5324. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.category,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where m.category like'%%#%@#%%' and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, category,limit_str];
  5325. double price_min = 0;
  5326. double price_max = 0;
  5327. if ([params.allKeys containsObject:@"alert"]) {
  5328. // alert
  5329. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5330. NSString *alert = params[@"alert"];
  5331. if ([alert isEqualToString:@"Display All"]) {
  5332. alert = [NSString stringWithFormat:@""];
  5333. } else {
  5334. alert = [self translateSingleQuote:alert];
  5335. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5336. }
  5337. // available
  5338. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5339. NSString *available = params[@"available"];
  5340. NSString *available_condition;
  5341. if ([available isEqualToString:@"Display All"]) {
  5342. available_condition = @"";
  5343. } else if ([available isEqualToString:@"Available Now"]) {
  5344. available_condition = @"and availability > 0";
  5345. } else {
  5346. available_condition = @"and availability == 0";
  5347. }
  5348. // best seller
  5349. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5350. NSString *best_seller = @"";
  5351. NSString *order_best_seller = @"m.name asc";
  5352. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5353. best_seller = @"and best_seller > 0";
  5354. order_best_seller = @"m.best_seller desc,m.name asc";
  5355. }
  5356. // price
  5357. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5358. NSString *price = params[@"price"];
  5359. price_min = 0;
  5360. price_max = MAXFLOAT;
  5361. if (appDelegate.user && price != nil) {
  5362. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5363. NSMutableString *priceName = [NSMutableString string];
  5364. for (int i = 0; i < priceTypeArray.count; i++) {
  5365. NSString *pricetype = priceTypeArray[i];
  5366. pricetype = [self translateSingleQuote:pricetype];
  5367. if (i == 0) {
  5368. [priceName appendFormat:@"'%@'",pricetype];
  5369. } else {
  5370. [priceName appendFormat:@",'%@'",pricetype];
  5371. }
  5372. }
  5373. if ([price isEqualToString:@"Display All"]) {
  5374. price = [NSString stringWithFormat:@""];
  5375. } else if([price containsString:@"+"]){
  5376. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5377. price_min = [price doubleValue];
  5378. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  5379. } else {
  5380. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5381. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5382. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5383. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  5384. }
  5385. } else {
  5386. price = @"";
  5387. }
  5388. // sold_by_qty : Sold in quantities of %@
  5389. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5390. NSString *qty = params[@"sold_by_qty"];
  5391. if ([qty isEqualToString:@"Display All"]) {
  5392. qty = @"";
  5393. } else {
  5394. qty = [self translateSingleQuote:qty];
  5395. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5396. }
  5397. // cate
  5398. // category = [self translateSingleQuote:category];
  5399. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  5400. // cate mutiple selection
  5401. NSString *category_id = params[@"category"];
  5402. NSMutableArray *cate_id_array = nil;
  5403. NSMutableString *cateWhere = [NSMutableString string];
  5404. if ([category_id isEqualToString:@""] || !category_id) {
  5405. [cateWhere appendString:@"1 = 1"];
  5406. } else {
  5407. if ([category_id containsString:@","]) {
  5408. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5409. } else {
  5410. cate_id_array = [@[category_id] mutableCopy];
  5411. }
  5412. [cateWhere appendString:@"("];
  5413. for (int i = 0; i < cate_id_array.count; i++) {
  5414. if (i == 0) {
  5415. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5416. } else {
  5417. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5418. }
  5419. }
  5420. [cateWhere appendString:@")"];
  5421. }
  5422. // where bestseller > 0 order by bestseller desc
  5423. // sql query: alert availability(int) best_seller(int) price qty
  5424. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.category,m.more_color from (select name,description,product_id,closeout,best_seller,category,more_color from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;",cateWhere,best_seller,alert,available_condition,qty,price,appDelegate.order_code,order_best_seller,limit_str];
  5425. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5426. }
  5427. DebugLog(@"offline category where: %@",where);
  5428. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5429. if (!appDelegate.user) {
  5430. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5431. }
  5432. [ret setValue:filter forKey:@"filter"];
  5433. DebugLog(@"offline_category sql:%@",sqlQuery);
  5434. sqlite3_stmt * statement;
  5435. [ret setValue:@"2" forKey:@"result"];
  5436. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5437. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5438. // int count=0;
  5439. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5440. {
  5441. int i=0;
  5442. while (sqlite3_step(statement) == SQLITE_ROW)
  5443. {
  5444. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5445. char *name = (char*)sqlite3_column_text(statement, 0);
  5446. if(name==nil)
  5447. name="";
  5448. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5449. char *description = (char*)sqlite3_column_text(statement, 1);
  5450. if(description==nil)
  5451. description="";
  5452. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5453. int product_id = sqlite3_column_int(statement, 2);
  5454. int wid = sqlite3_column_int(statement, 3);
  5455. int closeout = sqlite3_column_int(statement, 4);
  5456. int cid = sqlite3_column_int(statement, 5);
  5457. int wisdelete = sqlite3_column_int(statement, 6);
  5458. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  5459. int more_color = sqlite3_column_int(statement, 8);
  5460. // Defaul Category ID
  5461. __block NSString *categoryID = nil;
  5462. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  5463. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5464. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  5465. if(default_category==nil)
  5466. default_category="";
  5467. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  5468. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  5469. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  5470. categoryID = nsdefault_category;
  5471. }];
  5472. if (!categoryID.length) {
  5473. NSString *cateIDs = params[@"category"];
  5474. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  5475. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  5476. for (NSString *cateID in requestCategoryArr) {
  5477. BOOL needBreak = NO;
  5478. for (NSString *itemCateIDBox in itemCategoryArr) {
  5479. if (itemCateIDBox.length > 4) {
  5480. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  5481. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  5482. if ([itemCategoryID isEqualToString:cateID]) {
  5483. needBreak = YES;
  5484. categoryID = itemCategoryID;
  5485. break;
  5486. }
  5487. }
  5488. }
  5489. if (needBreak) {
  5490. break;
  5491. }
  5492. }
  5493. }
  5494. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5495. if(wid !=0 && wisdelete != 1)
  5496. [item setValue:@"true" forKey:@"wish_exists"];
  5497. else
  5498. [item setValue:@"false" forKey:@"wish_exists"];
  5499. if(closeout==0)
  5500. [item setValue:@"false" forKey:@"is_closeout"];
  5501. else
  5502. [item setValue:@"true" forKey:@"is_closeout"];
  5503. if(cid==0)
  5504. [item setValue:@"false" forKey:@"cart_exists"];
  5505. else
  5506. [item setValue:@"true" forKey:@"cart_exists"];
  5507. if (more_color == 0) {
  5508. [item setObject:@(false) forKey:@"more_color"];
  5509. } else if (more_color == 1) {
  5510. [item setObject:@(true) forKey:@"more_color"];
  5511. }
  5512. [item addEntriesFromDictionary:imgjson];
  5513. // [item setValue:nsurl forKey:@"img"];
  5514. [item setValue:nsname forKey:@"name"];
  5515. [item setValue:nsdescription forKey:@"description"];
  5516. if (categoryID) {
  5517. [item setValue:categoryID forKey:@"item_category_id"];
  5518. }
  5519. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5520. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5521. i++;
  5522. }
  5523. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5524. [ret setObject:items forKey:@"items"];
  5525. sqlite3_finalize(statement);
  5526. } else {
  5527. DebugLog(@"nothing...");
  5528. }
  5529. DebugLog(@"count:%d",count);
  5530. [iSalesDB close_db:db];
  5531. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5532. return ret;
  5533. }
  5534. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  5535. {
  5536. return [self categoryList:params limited:YES];
  5537. }
  5538. # pragma mark item search
  5539. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  5540. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5541. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  5542. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5543. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5544. // category
  5545. NSDictionary *category_menu = [self offline_category_menu];
  5546. [filter setValue:category_menu forKey:@"category"];
  5547. NSString* where= nil;
  5548. NSString* orderby= @"m.name";
  5549. if (!filterSearch) {
  5550. int covertype = [[params valueForKey:@"covertype"] intValue];
  5551. switch (covertype) {
  5552. case 0:
  5553. {
  5554. where=@"m.category like'%%#005#%%'";
  5555. break;
  5556. }
  5557. case 1:
  5558. {
  5559. where=@"m.alert like '%QS%'";
  5560. break;
  5561. }
  5562. case 2:
  5563. {
  5564. where=@"m.availability>0";
  5565. break;
  5566. }
  5567. case 3:
  5568. {
  5569. where=@"m.best_seller>0";
  5570. orderby=@"m.best_seller desc,m.name asc";
  5571. break;
  5572. }
  5573. default:
  5574. where=@"1=1";
  5575. break;
  5576. }
  5577. }
  5578. int limit = [[params valueForKey:@"limit"] intValue];
  5579. int offset = [[params valueForKey:@"offset"] intValue];
  5580. NSString *limit_str = @"";
  5581. if (limited) {
  5582. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  5583. }
  5584. sqlite3 *db = [iSalesDB get_db];
  5585. // [iSalesDB AddExFunction:db];
  5586. int count;
  5587. NSString *sqlQuery = nil;
  5588. where = [where stringByAppendingString:@" and m.is_active = 1"];
  5589. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where %@ order by %@ %@;",appDelegate.order_code, where,orderby,limit_str];
  5590. double price_min = 0;
  5591. double price_max = 0;
  5592. if (filterSearch) {
  5593. // alert
  5594. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5595. NSString *alert = params[@"alert"];
  5596. if ([alert isEqualToString:@"Display All"]) {
  5597. alert = [NSString stringWithFormat:@""];
  5598. } else {
  5599. alert = [self translateSingleQuote:alert];
  5600. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5601. }
  5602. // available
  5603. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5604. NSString *available = params[@"available"];
  5605. NSString *available_condition;
  5606. if ([available isEqualToString:@"Display All"]) {
  5607. available_condition = @"";
  5608. } else if ([available isEqualToString:@"Available Now"]) {
  5609. available_condition = @"and availability > 0";
  5610. } else {
  5611. available_condition = @"and availability == 0";
  5612. }
  5613. // best seller
  5614. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5615. NSString *best_seller = @"";
  5616. NSString *order_best_seller = @"m.name asc";
  5617. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5618. best_seller = @"and best_seller > 0";
  5619. order_best_seller = @"m.best_seller desc,m.name asc";
  5620. }
  5621. // price
  5622. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5623. NSString *price = params[@"price"];
  5624. price_min = 0;
  5625. price_max = MAXFLOAT;
  5626. if (appDelegate.user) {
  5627. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5628. NSMutableString *priceName = [NSMutableString string];
  5629. for (int i = 0; i < priceTypeArray.count; i++) {
  5630. NSString *pricetype = priceTypeArray[i];
  5631. pricetype = [self translateSingleQuote:pricetype];
  5632. if (i == 0) {
  5633. [priceName appendFormat:@"'%@'",pricetype];
  5634. } else {
  5635. [priceName appendFormat:@",'%@'",pricetype];
  5636. }
  5637. }
  5638. if ([price isEqualToString:@"Display All"]) {
  5639. price = [NSString stringWithFormat:@""];
  5640. } else if([price containsString:@"+"]){
  5641. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5642. price_min = [price doubleValue];
  5643. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  5644. } else {
  5645. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5646. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5647. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5648. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  5649. }
  5650. } else {
  5651. price = @"";
  5652. }
  5653. // sold_by_qty : Sold in quantities of %@
  5654. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5655. NSString *qty = params[@"sold_by_qty"];
  5656. if ([qty isEqualToString:@"Display All"]) {
  5657. qty = @"";
  5658. } else {
  5659. qty = [self translateSingleQuote:qty];
  5660. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5661. }
  5662. // category
  5663. NSString *category_id = params[@"ctgId"];
  5664. NSMutableArray *cate_id_array = nil;
  5665. NSMutableString *cateWhere = [NSMutableString string];
  5666. if ([category_id isEqualToString:@""] || !category_id) {
  5667. [cateWhere appendString:@"1 = 1"];
  5668. } else {
  5669. if ([category_id containsString:@","]) {
  5670. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5671. } else {
  5672. cate_id_array = [@[category_id] mutableCopy];
  5673. }
  5674. /*-----------*/
  5675. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  5676. [cateWhere appendString:@"("];
  5677. for (int i = 0; i < cate_id_array.count; i++) {
  5678. for (NSString *key0 in cateDic.allKeys) {
  5679. if ([key0 containsString:@"category_"]) {
  5680. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  5681. for (NSString *key1 in category0.allKeys) {
  5682. if ([key1 containsString:@"category_"]) {
  5683. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  5684. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5685. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5686. cate_id_array[i] = [category1 objectForKey:@"id"];
  5687. if (i == 0) {
  5688. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5689. } else {
  5690. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5691. }
  5692. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5693. [category0 setValue:category1 forKey:key1];
  5694. [cateDic setValue:category0 forKey:key0];
  5695. }
  5696. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  5697. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5698. cate_id_array[i] = [category0 objectForKey:@"id"];
  5699. if (i == 0) {
  5700. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5701. } else {
  5702. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5703. }
  5704. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5705. [cateDic setValue:category0 forKey:key0];
  5706. }
  5707. }
  5708. }
  5709. }
  5710. }
  5711. }
  5712. [cateWhere appendString:@")"];
  5713. [filter setValue:cateDic forKey:@"category"];
  5714. }
  5715. // where bestseller > 0 order by bestseller desc
  5716. // sql query: alert availability(int) best_seller(int) price qty
  5717. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from (select name,description,product_id,closeout,best_seller,more_color from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;", cateWhere,best_seller,alert,available_condition,qty,price,appDelegate.order_code,order_best_seller,limit_str];
  5718. // count
  5719. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5720. }
  5721. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  5722. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5723. if (!appDelegate.user) {
  5724. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5725. }
  5726. [ret setValue:filter forKey:@"filter"];
  5727. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  5728. sqlite3_stmt * statement;
  5729. [ret setValue:@"2" forKey:@"result"];
  5730. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5731. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5732. // int count=0;
  5733. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5734. {
  5735. int i=0;
  5736. while (sqlite3_step(statement) == SQLITE_ROW)
  5737. {
  5738. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5739. char *name = (char*)sqlite3_column_text(statement, 0);
  5740. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5741. char *description = (char*)sqlite3_column_text(statement, 1);
  5742. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5743. int product_id = sqlite3_column_int(statement, 2);
  5744. int wid = sqlite3_column_int(statement, 3);
  5745. int closeout = sqlite3_column_int(statement, 4);
  5746. int cid = sqlite3_column_int(statement, 5);
  5747. int wisdelete = sqlite3_column_int(statement, 6);
  5748. int more_color = sqlite3_column_int(statement, 7);
  5749. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5750. if(wid !=0 && wisdelete != 1)
  5751. [item setValue:@"true" forKey:@"wish_exists"];
  5752. else
  5753. [item setValue:@"false" forKey:@"wish_exists"];
  5754. if(closeout==0)
  5755. [item setValue:@"false" forKey:@"is_closeout"];
  5756. else
  5757. [item setValue:@"true" forKey:@"is_closeout"];
  5758. if(cid==0)
  5759. [item setValue:@"false" forKey:@"cart_exists"];
  5760. else
  5761. [item setValue:@"true" forKey:@"cart_exists"];
  5762. if (more_color == 0) {
  5763. [item setObject:@(false) forKey:@"more_color"];
  5764. } else if (more_color == 1) {
  5765. [item setObject:@(true) forKey:@"more_color"];
  5766. }
  5767. [item addEntriesFromDictionary:imgjson];
  5768. // [item setValue:nsurl forKey:@"img"];
  5769. [item setValue:nsname forKey:@"fash_name"];
  5770. [item setValue:nsdescription forKey:@"description"];
  5771. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5772. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5773. i++;
  5774. }
  5775. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5776. [ret setObject:items forKey:@"items"];
  5777. sqlite3_finalize(statement);
  5778. }
  5779. [iSalesDB close_db:db];
  5780. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5781. return ret;
  5782. }
  5783. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  5784. {
  5785. return [self itemsearch:params limited:YES];
  5786. }
  5787. #pragma mark order detail
  5788. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  5789. if (str1.length == 0) {
  5790. str1 = @"&nbsp";
  5791. }
  5792. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  5793. return str;
  5794. }
  5795. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  5796. {
  5797. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  5798. [fromformatter setDateFormat:from];
  5799. NSDate *date = [fromformatter dateFromString:datetime];
  5800. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  5801. [toformatter setDateFormat:to];
  5802. NSString * ret = [toformatter stringFromDate:date];
  5803. return ret;
  5804. }
  5805. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  5806. // 把毫秒去掉
  5807. if ([dateTime containsString:@"."]) {
  5808. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  5809. }
  5810. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  5811. formatter.dateFormat = formate;
  5812. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  5813. NSDate *date = [formatter dateFromString:dateTime];
  5814. formatter.dateFormat = newFormate;
  5815. NSString *result = [formatter stringFromDate:date];
  5816. return result ? result : @"";
  5817. }
  5818. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  5819. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  5820. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  5821. }
  5822. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  5823. {
  5824. DebugLog(@"offline oderdetail params: %@",params);
  5825. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5826. int orderId = [params[@"orderId"] intValue];
  5827. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  5828. // decrypt card number and card security code
  5829. // NSString* sql=[NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,o.TotalCuft,o.TotalWeight,o.TotalCarton,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),o.credit_card_expiration,o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus from (select _id,so_id,poNumber,create_time,status,TotalCuft,TotalWeight,TotalCarton,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId ];
  5830. NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via,c.city,c.state,c.zipcode,c.country from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  5831. sqlite3 *db = [iSalesDB get_db];
  5832. sqlite3_stmt * statement;
  5833. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  5834. NSString *nssoid = @"";
  5835. NSString* orderinfo = @"";
  5836. NSString *moreInfo = @"";
  5837. double handlingFee = 0;
  5838. double payments_and_credist = 0;
  5839. double totalPrice = 0;
  5840. double shippingFee = 0;
  5841. double lift_gate = 0;
  5842. NSString *customer_contact = @"";
  5843. NSString *customer_email = @"";
  5844. NSString *customer_fax = @"";
  5845. NSString *customer_phone = @"";
  5846. NSString *customer_city = @"";
  5847. NSString *customer_state = @"";
  5848. NSString *customer_zipcode = @"";
  5849. NSString *customer_country = @"";
  5850. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  5851. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  5852. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  5853. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  5854. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  5855. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  5856. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  5857. {
  5858. if (sqlite3_step(statement) == SQLITE_ROW)
  5859. {
  5860. int order_id = sqlite3_column_int(statement, 0);
  5861. NSString *sign_url = [self textAtColumn:51 statement:statement];
  5862. // ret[@"sign_url"] = sign_url;
  5863. section_1 = @{
  5864. @"data":sign_url,
  5865. @"title":@"Signature",
  5866. @"type":@"sign_url"
  5867. }.mutableCopy;
  5868. [ret setObject:section_1 forKey:@"section_1"];
  5869. customer_contact = [self textAtColumn:52 statement:statement];
  5870. customer_email = [self textAtColumn:53 statement:statement];
  5871. customer_phone = [self textAtColumn:54 statement:statement];
  5872. customer_fax = [self textAtColumn:55 statement:statement];
  5873. customer_city = [self textAtColumn:60 statement:statement];
  5874. customer_state = [self textAtColumn:61 statement:statement];
  5875. customer_zipcode = [self textAtColumn:62 statement:statement];
  5876. customer_country = [self textAtColumn:63 statement:statement];
  5877. int offline_edit=sqlite3_column_int(statement, 56);
  5878. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  5879. char *soid = (char*)sqlite3_column_text(statement, 1);
  5880. if(soid==nil)
  5881. soid= "";
  5882. nssoid= [[NSString alloc]initWithUTF8String:soid];
  5883. // so#
  5884. ret[@"so#"] = nssoid;
  5885. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  5886. if(poNumber==nil)
  5887. poNumber= "";
  5888. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  5889. char *create_time = (char*)sqlite3_column_text(statement, 3);
  5890. if(create_time==nil)
  5891. create_time= "";
  5892. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5893. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  5894. int status = sqlite3_column_int(statement, 4);
  5895. int erpStatus = sqlite3_column_int(statement, 49);
  5896. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5897. // status
  5898. if (status > 1 && status != 3) {
  5899. status = erpStatus;
  5900. } else if (status == 3) {
  5901. status = 15;
  5902. }
  5903. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  5904. ret[@"order_status"] = nsstatus;
  5905. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5906. if(company_name==nil)
  5907. company_name= "";
  5908. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5909. // company name
  5910. ret[@"company_name"] = nscompany_name;
  5911. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  5912. if(customer_contact==nil)
  5913. customer_contact= "";
  5914. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  5915. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  5916. if(addr_1==nil)
  5917. addr_1="";
  5918. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  5919. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  5920. if(addr_2==nil)
  5921. addr_2="";
  5922. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  5923. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  5924. if(addr_3==nil)
  5925. addr_3="";
  5926. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  5927. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  5928. if(addr_4==nil)
  5929. addr_4="";
  5930. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  5931. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  5932. [arr_addr addObject:nsaddr_1];
  5933. [arr_addr addObject:nsaddr_2];
  5934. [arr_addr addObject:nsaddr_3];
  5935. [arr_addr addObject:nsaddr_4];
  5936. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  5937. [arr_addr addObject:customer_state];
  5938. [arr_addr addObject:customer_zipcode];
  5939. [arr_addr addObject:customer_country];
  5940. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@" " trim:true];
  5941. char *logist = (char*)sqlite3_column_text(statement, 11);
  5942. if(logist==nil)
  5943. logist= "";
  5944. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  5945. if (/*status == -11 || */status == 10 || status == 11) {
  5946. nslogist = [self textAtColumn:59 statement:statement];
  5947. };
  5948. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  5949. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  5950. shipping = @"Shipping To Be Quoted";
  5951. } else {
  5952. shippingFee = sqlite3_column_double(statement, 12);
  5953. }
  5954. // Shipping
  5955. // ret[@"Shipping"] = shipping;
  5956. NSDictionary *shipping_item = @{
  5957. @"title":@"Shipping",
  5958. @"value":shipping
  5959. };
  5960. [price_data setObject:shipping_item forKey:@"item_1"];
  5961. int have_lift_gate = sqlite3_column_int(statement, 17);
  5962. lift_gate = sqlite3_column_double(statement, 13);
  5963. // Liftgate Fee(No loading dock)
  5964. if (!have_lift_gate) {
  5965. lift_gate = 0;
  5966. }
  5967. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  5968. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  5969. if (sqlite3_column_int(statement, 57)) {
  5970. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  5971. liftgate_value = @"Shipping To Be Quoted";
  5972. }
  5973. NSDictionary *liftgate_item = @{
  5974. @"title":@"Liftgate Fee(No loading dock)",
  5975. @"value":liftgate_value
  5976. };
  5977. [price_data setObject:liftgate_item forKey:@"item_2"];
  5978. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  5979. // [ret removeObjectForKey:@"Shipping"];
  5980. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  5981. // }
  5982. //
  5983. // if (have_lift_gate) {
  5984. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  5985. // }
  5986. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  5987. if(general_notes==nil)
  5988. general_notes= "";
  5989. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  5990. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  5991. if(internal_notes==nil)
  5992. internal_notes= "";
  5993. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  5994. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  5995. if(payment_type==nil)
  5996. payment_type= "";
  5997. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  5998. // order info
  5999. orderinfo = [self textFileName:@"order_info.html"];
  6000. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  6001. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  6002. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  6003. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  6004. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  6005. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  6006. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  6007. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  6008. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  6009. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  6010. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  6011. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  6012. NSString *payment = nil;
  6013. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  6014. {
  6015. payment = [self textFileName:@"creditcardpayment.html"];
  6016. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  6017. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  6018. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  6019. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  6020. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  6021. NSString *card_type = [self textAtColumn:42 statement:statement];
  6022. if (card_type.length > 0) { // 显示星号
  6023. card_type = @"****";
  6024. }
  6025. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  6026. if (card_number.length > 0 && card_number.length > 4) {
  6027. for (int i = 0; i < card_number.length - 4; i++) {
  6028. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  6029. }
  6030. } else {
  6031. card_number = @"";
  6032. }
  6033. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  6034. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  6035. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  6036. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  6037. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  6038. card_expiration = @"****";
  6039. }
  6040. NSString *card_city = [self textAtColumn:46 statement:statement];
  6041. NSString *card_state = [self textAtColumn:47 statement:statement];
  6042. payment = [self replaceHtml:payment String:@"Payment_Type" withString:nspayment_type];
  6043. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  6044. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  6045. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  6046. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  6047. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  6048. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  6049. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  6050. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  6051. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  6052. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  6053. }
  6054. else
  6055. {
  6056. payment=[self textFileName:@"normalpayment.html"];
  6057. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  6058. }
  6059. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  6060. ret[@"result"]= [NSNumber numberWithInt:2];
  6061. // more info
  6062. moreInfo = [self textFileName:@"more_info.html"];
  6063. /*****ship to******/
  6064. // ShipToCompany_or_&nbsp
  6065. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  6066. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  6067. NSString *shipToName = [self textAtColumn:19 statement:statement];
  6068. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  6069. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  6070. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  6071. /*****ship from******/
  6072. // ShipFromCompany_or_&nbsp
  6073. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  6074. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  6075. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  6076. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  6077. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  6078. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  6079. /*****freight to******/
  6080. // FreightBillToCompany_or_&nbsp
  6081. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  6082. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  6083. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  6084. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  6085. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  6086. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  6087. /*****merchandise to******/
  6088. // MerchandiseBillToCompany_or_&nbsp
  6089. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  6090. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  6091. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  6092. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  6093. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  6094. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  6095. /*****return to******/
  6096. // ReturnToCompany_or_&nbsp
  6097. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  6098. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  6099. NSString *returnToName = [self textAtColumn:31 statement:statement];
  6100. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  6101. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  6102. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  6103. //
  6104. // DebugLog(@"more info : %@",moreInfo);
  6105. // handling fee
  6106. handlingFee = sqlite3_column_double(statement, 33);
  6107. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6108. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6109. if (sqlite3_column_int(statement, 58)) {
  6110. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  6111. handling_fee_value = @"Shipping To Be Quoted";
  6112. }
  6113. NSDictionary *handling_fee_item = @{
  6114. @"title":@"Handling Fee",
  6115. @"value":handling_fee_value
  6116. };
  6117. [price_data setObject:handling_fee_item forKey:@"item_3"];
  6118. //
  6119. // customer info
  6120. customerID = [self textAtColumn:36 statement:statement];
  6121. // mode
  6122. ret[@"mode"] = appDelegate.mode;
  6123. // model_count
  6124. ret[@"model_count"] = @(0);
  6125. }
  6126. sqlite3_finalize(statement);
  6127. }
  6128. [iSalesDB close_db:db];
  6129. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  6130. // "customer_email" = "Shui Hu";
  6131. // "customer_fax" = "";
  6132. // "customer_first_name" = F;
  6133. // "customer_last_name" = L;
  6134. // "customer_name" = ",da He Xiang Dong Liu A";
  6135. // "customer_phone" = "Hey Xuan Feng";
  6136. contactInfo[@"customer_phone"] = customer_phone;
  6137. contactInfo[@"customer_fax"] = customer_fax;
  6138. contactInfo[@"customer_email"] = customer_email;
  6139. NSString *first_name = @"";
  6140. NSString *last_name = @"";
  6141. if ([customer_contact isEqualToString:@""]) {
  6142. } else if ([customer_contact containsString:@" "]) {
  6143. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  6144. first_name = [customer_contact substringToIndex:first_space_index];
  6145. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  6146. }
  6147. contactInfo[@"customer_first_name"] = first_name;
  6148. contactInfo[@"customer_last_name"] = last_name;
  6149. ret[@"customerInfo"] = contactInfo;
  6150. // models
  6151. if (nssoid) {
  6152. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  6153. __block double TotalCuft = 0;
  6154. __block double TotalWeight = 0;
  6155. __block int TotalCarton = 0;
  6156. __block double allItemPrice = 0;
  6157. NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",nssoid];
  6158. sqlite3 *db1 = [iSalesDB get_db];
  6159. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6160. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6161. int product_id = sqlite3_column_int(stmt, 0);
  6162. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6163. int item_id = sqlite3_column_int(stmt, 7);
  6164. NSString* Price=nil;
  6165. if(str_price==nil)
  6166. {
  6167. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  6168. if(price==nil)
  6169. Price=@"No Price.";
  6170. else
  6171. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6172. }
  6173. else
  6174. {
  6175. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6176. }
  6177. double discount = sqlite3_column_double(stmt, 2);
  6178. int item_count = sqlite3_column_int(stmt, 3);
  6179. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  6180. NSString *nsline_note=nil;
  6181. if(line_note!=nil)
  6182. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  6183. // char *name = (char*)sqlite3_column_text(stmt, 5);
  6184. // NSString *nsname = nil;
  6185. // if(name!=nil)
  6186. // nsname= [[NSString alloc]initWithUTF8String:name];
  6187. NSString *nsname = [self textAtColumn:5 statement:stmt];
  6188. // char *description = (char*)sqlite3_column_text(stmt, 6);
  6189. // NSString *nsdescription=nil;
  6190. // if(description!=nil)
  6191. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  6192. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  6193. // int stockUom = sqlite3_column_int(stmt, 8);
  6194. // int _id = sqlite3_column_int(stmt, 9);
  6195. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  6196. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6197. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6198. int carton=[bsubtotaljson[@"carton"] intValue];
  6199. TotalCuft += cuft;
  6200. TotalWeight += weight;
  6201. TotalCarton += carton;
  6202. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6203. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  6204. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  6205. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  6206. itemjson[@"note"]=nsline_note;
  6207. itemjson[@"origin_price"] = Price;
  6208. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6209. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6210. itemjson[@"order_item_status"] = @""; // 暂时不处理
  6211. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6212. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6213. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  6214. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6215. if(itemjson[@"combine"] != nil)
  6216. {
  6217. // int citem=0;
  6218. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6219. for(int bc=0;bc<bcount;bc++)
  6220. {
  6221. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6222. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6223. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6224. subTotal += uprice * modulus * item_count;
  6225. }
  6226. }
  6227. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6228. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  6229. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  6230. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6231. itemjson[@"type"] = @"order_item";
  6232. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  6233. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  6234. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6235. allItemPrice += subTotal;
  6236. }];
  6237. section_3[@"data"] = model_data;
  6238. section_3[@"type"] = @"sub_order";
  6239. section_3[@"title"] = @"Models";
  6240. section_3[@"switch"] = @(false);
  6241. ret[@"section_3"] = section_3;
  6242. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  6243. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  6244. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  6245. // payments/Credits
  6246. // payments_and_credist = sqlite3_column_double(statement, 34);
  6247. payments_and_credist = allItemPrice;
  6248. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6249. NSDictionary *sub_total_item = @{
  6250. @"title":@"Sub-Total",
  6251. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6252. };
  6253. [price_data setObject:sub_total_item forKey:@"item_0"];
  6254. // // total
  6255. // totalPrice = sqlite3_column_double(statement, 35);
  6256. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6257. } else {
  6258. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  6259. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  6260. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  6261. // payments/Credits
  6262. payments_and_credist = 0;
  6263. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6264. NSDictionary *sub_total_item = @{
  6265. @"title":@"Sub-Total",
  6266. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6267. };
  6268. [price_data setObject:sub_total_item forKey:@"item_0"];
  6269. }
  6270. // total
  6271. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6272. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6273. NSDictionary *total_item = @{
  6274. @"title":@"Total",
  6275. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  6276. };
  6277. [price_data setObject:total_item forKey:@"item_4"];
  6278. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  6279. section_4 = @{
  6280. @"data":price_data,
  6281. @"title":@"Price Info",
  6282. @"type":@"price_info"
  6283. }.mutableCopy;
  6284. ret[@"section_4"] = section_4;
  6285. // ret[@"order_info"]= orderinfo;
  6286. section_0 = @{
  6287. @"data":orderinfo,
  6288. @"title":@"Order Info",
  6289. @"type":@"order_info"
  6290. }.mutableCopy;
  6291. ret[@"section_0"] = section_0;
  6292. // ret[@"more_order_info"] = moreInfo;
  6293. section_2 = @{
  6294. @"data":moreInfo,
  6295. @"title":@"More Info",
  6296. @"type":@"more_order_info"
  6297. }.mutableCopy;
  6298. ret[@"section_2"] = section_2;
  6299. ret[@"count"] = @(5);
  6300. return [RAUtils dict2data:ret];
  6301. }
  6302. #pragma mark order list
  6303. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  6304. NSString *nsstatus = @"";
  6305. switch (status) {
  6306. case 0:
  6307. {
  6308. nsstatus=@"Temp Order";
  6309. break;
  6310. }
  6311. case 1:
  6312. {
  6313. nsstatus=@"Saved Order";
  6314. break;
  6315. }
  6316. case 2: {
  6317. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  6318. break;
  6319. }
  6320. case 3:
  6321. case 15:
  6322. {
  6323. nsstatus=@"Cancelled";
  6324. break;
  6325. }
  6326. case 10:
  6327. {
  6328. nsstatus=@"Quote Submitted";
  6329. break;
  6330. }
  6331. case 11:
  6332. {
  6333. nsstatus=@"Sales Order Submitted";
  6334. break;
  6335. }
  6336. case 12:
  6337. {
  6338. nsstatus=@"Processing";
  6339. break;
  6340. }
  6341. case 13:
  6342. {
  6343. nsstatus=@"Shipped";
  6344. break;
  6345. }
  6346. case 14:
  6347. {
  6348. nsstatus=@"Closed";
  6349. break;
  6350. }
  6351. case -11:
  6352. {
  6353. nsstatus = @"Ready For Submit";
  6354. break;
  6355. }
  6356. default:
  6357. break;
  6358. }
  6359. return nsstatus;
  6360. }
  6361. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  6362. double total = 0;
  6363. __block double payments_and_credist = 0;
  6364. __block double allItemPrice = 0;
  6365. // sqlite3 *db1 = [iSalesDB get_db];
  6366. if (so_id) {
  6367. // NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",so_id];
  6368. NSString *modelSql = [NSString stringWithFormat:@"select product_id,price,discount,item_count,item_id,customer_cid from (select c.product_id,decrypt(c.str_price) as price,c.discount,c.item_count,c.item_id,o.customer_cid from offline_cart as c left join offline_order as o on c.so_no = o.so_id where c.so_no='%@');",so_id];
  6369. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6370. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6371. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6372. int product_id = sqlite3_column_int(stmt, 0);
  6373. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6374. int discount = sqlite3_column_int(stmt, 2);
  6375. int item_count = sqlite3_column_int(stmt, 3);
  6376. // int item_id = sqlite3_column_int(stmt, 7);
  6377. int item_id = sqlite3_column_int(stmt, 4);
  6378. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  6379. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  6380. NSString* Price=nil;
  6381. if(str_price==nil)
  6382. {
  6383. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  6384. if(price==nil)
  6385. Price=@"No Price.";
  6386. else
  6387. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6388. }
  6389. else
  6390. {
  6391. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6392. }
  6393. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6394. itemjson[@"The unit price"]=Price;
  6395. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6396. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6397. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6398. if(itemjson[@"combine"] != nil)
  6399. {
  6400. // int citem=0;
  6401. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6402. for(int bc=0;bc<bcount;bc++)
  6403. {
  6404. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6405. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6406. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6407. subTotal += uprice * modulus * item_count;
  6408. }
  6409. }
  6410. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6411. allItemPrice += subTotal;
  6412. }];
  6413. payments_and_credist = allItemPrice;
  6414. } else {
  6415. // payments/Credits
  6416. payments_and_credist = 0;
  6417. }
  6418. // lift_gate handlingFee shippingFee
  6419. __block double lift_gate = 0;
  6420. __block double handlingFee = 0;
  6421. __block double shippingFee = 0;
  6422. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  6423. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6424. int have_lift_gate = sqlite3_column_int(stmt, 0);
  6425. if (have_lift_gate) {
  6426. lift_gate = sqlite3_column_double(stmt, 1);
  6427. }
  6428. handlingFee = sqlite3_column_double(stmt, 2);
  6429. shippingFee = sqlite3_column_double(stmt, 3);
  6430. }];
  6431. // total
  6432. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6433. if (close) {
  6434. [iSalesDB close_db:db1];
  6435. }
  6436. return total;
  6437. }
  6438. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  6439. {
  6440. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6441. int limit = [[params valueForKey:@"limit"] intValue];
  6442. int offset = [[params valueForKey:@"offset"] intValue];
  6443. NSString* keyword = [params valueForKey:@"keyWord"];
  6444. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  6445. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  6446. NSString* where=@"1 = 1";
  6447. if(keyword.length>0)
  6448. where=[where stringByAppendingString:[NSString stringWithFormat:@" and (lower(o.so_id) like '%%%@%%' or lower(o.sales_rep) like '%%%@%%' or lower(decrypt(c.company_name)) like '%%%@%%' or lower(o.create_by) like '%%%@%%')",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString]];
  6449. if (orderStatus.length > 0) {
  6450. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  6451. if (order_status_array.count == 1) {
  6452. int status_value = [[order_status_array firstObject] integerValue];
  6453. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  6454. if (status_value == 15 || status_value == 3) {
  6455. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  6456. } else {
  6457. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  6458. }
  6459. } else {
  6460. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  6461. }
  6462. } else if (order_status_array.count > 1) {
  6463. for (int i = 0; i < order_status_array.count;i++) {
  6464. NSString *status = order_status_array[i];
  6465. NSString *condition = @" or";
  6466. if (i == 0) {
  6467. condition = @" and (";
  6468. }
  6469. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  6470. int status_value = [status integerValue];
  6471. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  6472. if (status_value == 15 || status_value == 3) {
  6473. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  6474. } else {
  6475. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  6476. }
  6477. } else {
  6478. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  6479. }
  6480. }
  6481. where = [where stringByAppendingString:@" )"];
  6482. }
  6483. }
  6484. NSString* sqlQuery=[NSString stringWithFormat:@"SELECT o._id,o.so_id,o.status,o.sales_rep,o.create_by,decrypt(c.company_name),o.create_time,o.total_price,o.customer_contact,o.erpOrderStatus,o.order_id is null from offline_order o left join offline_contact c on o.customer_cid=c.contact_id where %@ order by o.create_time desc limit %d offset %d" ,where, limit, offset];
  6485. // DebugLog(@"order list sql: %@",sqlQuery);
  6486. sqlite3 *db = [iSalesDB get_db];
  6487. sqlite3_stmt * statement;
  6488. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6489. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6490. {
  6491. int count=0;
  6492. while (sqlite3_step(statement) == SQLITE_ROW)
  6493. {
  6494. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  6495. int order_id = sqlite3_column_double(statement, 0);
  6496. char *soid = (char*)sqlite3_column_text(statement, 1);
  6497. if(soid==nil)
  6498. soid= "";
  6499. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  6500. int status = sqlite3_column_double(statement, 2);
  6501. int erpStatus = sqlite3_column_double(statement, 9);
  6502. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  6503. if(sales_rep==nil)
  6504. sales_rep= "";
  6505. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  6506. char *create_by = (char*)sqlite3_column_text(statement, 4);
  6507. if(create_by==nil)
  6508. create_by= "";
  6509. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  6510. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6511. if(company_name==nil)
  6512. company_name= "";
  6513. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6514. char *create_time = (char*)sqlite3_column_text(statement, 6);
  6515. if(create_time==nil)
  6516. create_time= "";
  6517. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6518. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  6519. // double total_price = sqlite3_column_double(statement, 7);
  6520. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  6521. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  6522. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6523. int offline_edit = sqlite3_column_int(statement, 10);
  6524. // ": "JH",
  6525. // "": "$8307.00",
  6526. // "": "MOB1608050001",
  6527. // "": "ArpithaT",
  6528. // "": "1st Stage Property Transformations",
  6529. // "": "JANICE SUTTON",
  6530. // "": 2255,
  6531. // "": "08/02/2016 09:49:18",
  6532. // "": 1,
  6533. // "": "Saved Order"
  6534. // "": "1470384050483",
  6535. // "model_count": "6 / 28"
  6536. item[@"sales_rep"]= nssales_rep;
  6537. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  6538. item[@"so#"]= nssoid;
  6539. item[@"create_by"]= nscreate_by;
  6540. item[@"customer_name"]= nscompany_name;
  6541. item[@"customer_contact"] = customer_contact;
  6542. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  6543. item[@"purchase_time"]= nscreate_time;
  6544. int statusCode = status;
  6545. if (statusCode == 2) {
  6546. statusCode = erpStatus;
  6547. } else if (statusCode == 3) {
  6548. statusCode = 15;
  6549. }
  6550. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  6551. item[@"order_status"]= nsstatus;
  6552. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  6553. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  6554. // item[@"model_count"]
  6555. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  6556. count++;
  6557. }
  6558. ret[@"count"]= [NSNumber numberWithInt:count];
  6559. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  6560. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  6561. ret[@"result"]= [NSNumber numberWithInt:2];
  6562. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  6563. ret[@"time_zone"] = @"PST";
  6564. sqlite3_finalize(statement);
  6565. }
  6566. int total_count = [iSalesDB get_recordcount:db table:@"offline_order o left join offline_contact c on o.customer_cid=c.contact_id" where:where];
  6567. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  6568. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6569. [iSalesDB close_db:db];
  6570. return [RAUtils dict2data:ret];
  6571. }
  6572. #pragma mark update gnotes
  6573. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  6574. {
  6575. DebugLog(@"params: %@",params);
  6576. // comments = Meyoyoyoyoyoyoy;
  6577. // orderCode = MOB1608110001;
  6578. // password = 123456;
  6579. // user = EvanK;
  6580. NSString *sql = [NSString stringWithFormat:@"update offline_order set general_notes = '%@' where so_id = '%@';",[self valueInParams:params key:@"comments" translateSingleQuote:YES],[self valueInParams:params key:@"orderCode" translateSingleQuote:YES]];
  6581. int ret = [iSalesDB execSql:sql];
  6582. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6583. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6584. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6585. // [dic setValue:@"160409" forKey:@"min_ver"];
  6586. return [RAUtils dict2data:dic];
  6587. }
  6588. #pragma mark move to wishlist
  6589. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  6590. {
  6591. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6592. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6593. _id = [NSString stringWithFormat:@"(%@)",_id];
  6594. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  6595. sqlite3 *db = [iSalesDB get_db];
  6596. __block NSString *product_id = @"";
  6597. __block NSString *item_count_str = @"";
  6598. // __block NSString *item_id = nil;
  6599. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6600. // product_id = [self textAtColumn:0 statement:stmt];
  6601. int item_count = sqlite3_column_int(stmt, 1);
  6602. // item_id = [self textAtColumn:2 statement:stmt];
  6603. NSString *p_id = [self textAtColumn:0 statement:stmt];
  6604. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  6605. if (p_id.length) {
  6606. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  6607. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  6608. }
  6609. }];
  6610. [iSalesDB close_db:db];
  6611. // 去除第一个,
  6612. if (product_id.length > 1) {
  6613. product_id = [product_id substringFromIndex:1];
  6614. }
  6615. if (item_count_str.length > 1) {
  6616. item_count_str = [item_count_str substringFromIndex:1];
  6617. }
  6618. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6619. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  6620. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  6621. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  6622. sqlite3 *db1 = [iSalesDB get_db];
  6623. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  6624. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6625. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  6626. // 删除
  6627. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  6628. int ret = [iSalesDB execSql:deleteSql db:db1];
  6629. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6630. [iSalesDB close_db:db1];
  6631. return [RAUtils dict2data:dic];
  6632. }
  6633. #pragma mark cart delete
  6634. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  6635. {
  6636. // cartItemId = 548;
  6637. // orderCode = MOB1608110001;
  6638. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6639. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6640. _id = [NSString stringWithFormat:@"(%@)",_id];
  6641. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  6642. int ret = [iSalesDB execSql:sql];
  6643. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6644. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6645. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6646. // [dic setValue:@"160409" forKey:@"min_ver"];
  6647. return [RAUtils dict2data:dic];
  6648. }
  6649. #pragma mark set price
  6650. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  6651. {
  6652. DebugLog(@"cart set price params: %@",params);
  6653. // "cartitem_id" = 1;
  6654. // discount = "0.000000";
  6655. // "item_note" = "";
  6656. // price = "269.000000";
  6657. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6658. NSString *notes = [self valueInParams:params key:@"item_note"];
  6659. NSString *discount = [self valueInParams:params key:@"discount"];
  6660. NSString *price = [self valueInParams:params key:@"price"];
  6661. // bool badd_price_changed=false;
  6662. // sqlite3* db=[iSalesDB get_db];
  6663. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6664. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  6665. // NSRange range;
  6666. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  6667. //
  6668. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  6669. // badd_price_changed=true;
  6670. // [iSalesDB close_db:db];
  6671. //
  6672. // if(badd_price_changed)
  6673. // {
  6674. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  6675. // }
  6676. //
  6677. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  6678. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  6679. int ret = [iSalesDB execSql:sql];
  6680. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6681. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6682. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6683. // [dic setValue:@"160409" forKey:@"min_ver"];
  6684. return [RAUtils dict2data:dic];
  6685. }
  6686. #pragma mark set line notes
  6687. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  6688. {
  6689. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6690. NSString *notes = [self valueInParams:params key:@"notes"];
  6691. notes = [self translateSingleQuote:notes];
  6692. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  6693. int ret = [iSalesDB execSql:sql];
  6694. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6695. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6696. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6697. // [dic setValue:@"160409" forKey:@"min_ver"];
  6698. return [RAUtils dict2data:dic];
  6699. }
  6700. #pragma mark set qty
  6701. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  6702. {
  6703. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6704. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6705. int item_count = [params[@"inputInt"] intValue];
  6706. // 购买检查数量大于库存
  6707. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6708. if (!appDelegate.can_create_backorder) {
  6709. NSString *checkSql = [NSString stringWithFormat:@"select p.availability from product as p left join offline_cart as c on c.item_id = p.item_id where c._id = %@;",_id];
  6710. __block BOOL out_of_stock = NO;
  6711. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6712. int availability = sqlite3_column_int(stmt, 0);
  6713. if (availability < item_count) {
  6714. out_of_stock = YES;
  6715. }
  6716. }];
  6717. if (out_of_stock) { // 缺货
  6718. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  6719. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  6720. return [RAUtils dict2data:dic];
  6721. }
  6722. }
  6723. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  6724. int ret = [iSalesDB execSql:sql];
  6725. __block int item_id = 0;
  6726. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6727. item_id = sqlite3_column_int(stmt, 0);
  6728. }];
  6729. sqlite3 *db = [iSalesDB get_db];
  6730. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6731. [iSalesDB close_db:db];
  6732. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6733. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6734. // [dic setValue:@"160409" forKey:@"min_ver"];
  6735. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  6736. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  6737. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  6738. return [RAUtils dict2data:dic];
  6739. }
  6740. #pragma mark place order
  6741. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  6742. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6743. if (aname) {
  6744. [dic setValue:aname forKey:@"aname"];
  6745. }
  6746. if (control) {
  6747. [dic setValue:control forKey:@"control"];
  6748. }
  6749. if (keyboard) {
  6750. [dic setValue:keyboard forKey:@"keyboard"];
  6751. }
  6752. if (name) {
  6753. [dic setValue:name forKey:@"name"];
  6754. }
  6755. if (value) {
  6756. [dic setValue:value forKey:@"value"];
  6757. }
  6758. return dic;
  6759. }
  6760. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  6761. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6762. orderCode = [self translateSingleQuote:orderCode];
  6763. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  6764. NSString *customerSql = [NSString stringWithFormat:@"select c.contact_id,c.img_0,c.img_1,c.img_2,decrypt(c.company_name),decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,c.first_name,c.last_name,o.customer_email,o.customer_phone,o.customer_fax,c.zipcode,c.city,c.state,c.country,o.customer_contact from offline_contact as c join offline_order as o on c.contact_id = o.customer_cid where o.so_id = '%@';",orderCode];
  6765. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6766. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  6767. NSString *img0 = [self textAtColumn:1 statement:stmt];
  6768. NSString *img1 = [self textAtColumn:2 statement:stmt];
  6769. NSString *img2 = [self textAtColumn:3 statement:stmt];
  6770. NSString *company_name = [self textAtColumn:4 statement:stmt];
  6771. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  6772. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  6773. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  6774. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  6775. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  6776. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  6777. NSString *email = [self textAtColumn:11 statement:stmt];
  6778. NSString *phone = [self textAtColumn:12 statement:stmt];
  6779. NSString *fax = [self textAtColumn:13 statement:stmt];
  6780. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  6781. NSString *city = [self textAtColumn:15 statement:stmt];
  6782. NSString *state = [self textAtColumn:16 statement:stmt];
  6783. NSString *country = [self textAtColumn:17 statement:stmt];
  6784. NSString *name = [self textAtColumn:18 statement:stmt];
  6785. // contact id
  6786. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  6787. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  6788. [contact_id_dic setValue:@"text" forKey:@"control"];
  6789. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  6790. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  6791. [contact_id_dic setValue:@"true" forKey:@"required"];
  6792. [contact_id_dic setValue:contact_id forKey:@"value"];
  6793. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  6794. // business card
  6795. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  6796. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  6797. [business_card_dic setValue:@"img" forKey:@"control"];
  6798. [business_card_dic setValue:@"1" forKey:@"disable"];
  6799. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  6800. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  6801. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  6802. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  6803. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  6804. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  6805. [business_card_dic setValue:@"business_card" forKey:@"name"];
  6806. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  6807. // fax
  6808. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6809. [customer_dic setValue:fax_dic forKey:@"item_10"];
  6810. // zipcode
  6811. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  6812. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  6813. // city
  6814. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  6815. [customer_dic setValue:city_dic forKey:@"item_12"];
  6816. // state
  6817. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  6818. [customer_dic setValue:state_dic forKey:@"item_13"];
  6819. // country
  6820. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  6821. [customer_dic setValue:country_dic forKey:@"item_14"];
  6822. // company name
  6823. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  6824. [customer_dic setValue:company_dic forKey:@"item_2"];
  6825. // addr_1
  6826. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  6827. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  6828. // addr_2
  6829. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  6830. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  6831. // addr_3
  6832. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  6833. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  6834. // addr_4
  6835. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  6836. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  6837. // Contact
  6838. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  6839. [customer_dic setValue:contact_dic forKey:@"item_7"];
  6840. // email
  6841. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6842. [customer_dic setValue:email_dic forKey:@"item_8"];
  6843. // phone
  6844. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6845. [customer_dic setValue:phone_dic forKey:@"item_9"];
  6846. // title
  6847. [customer_dic setValue:@"Customer" forKey:@"title"];
  6848. // count
  6849. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  6850. }];
  6851. // setting
  6852. NSDictionary *setting = params[@"setting"];
  6853. NSNumber *hide = setting[@"CustomerHide"];
  6854. [customer_dic setValue:hide forKey:@"hide"];
  6855. return customer_dic;
  6856. }
  6857. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6858. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6859. // setting
  6860. NSDictionary *setting = params[@"setting"];
  6861. NSNumber *hide = setting[@"ShipToHide"];
  6862. [dic setValue:hide forKey:@"hide"];
  6863. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6864. orderCode = [self translateSingleQuote:orderCode];
  6865. NSString *sql = [NSString stringWithFormat:@"select receive_cid,receive_name,receive_ext,receive_contact,receive_email,receive_fax,receive_phone from offline_order where so_id = '%@';",orderCode];
  6866. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6867. NSString *cid = [self textAtColumn:0 statement:stmt];
  6868. NSString *name = [self textAtColumn:1 statement:stmt];
  6869. NSString *ext = [self textAtColumn:2 statement:stmt];
  6870. NSString *contact = [self textAtColumn:3 statement:stmt];
  6871. NSString *email = [self textAtColumn:4 statement:stmt];
  6872. NSString *fax = [self textAtColumn:5 statement:stmt];
  6873. NSString *phone = [self textAtColumn:6 statement:stmt];
  6874. // count
  6875. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6876. // title
  6877. [dic setValue:@"Ship To" forKey:@"title"];
  6878. // choose
  6879. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6880. [choose_dic setValue:@"choose" forKey:@"aname"];
  6881. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6882. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6883. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6884. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6885. @"receive_contact" : @"customer_contact",
  6886. @"receive_email" : @"customer_email",
  6887. @"receive_ext" : @"customer_contact_ext",
  6888. @"receive_fax" : @"customer_fax",
  6889. @"receive_name" : @"customer_name",
  6890. @"receive_phone" : @"customer_phone"},
  6891. @"refresh" : [NSNumber numberWithInteger:1],
  6892. @"type" : @"pull"};
  6893. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  6894. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  6895. @"name" : @"Add new address",
  6896. @"refresh" : [NSNumber numberWithInteger:1],
  6897. @"value" : @"new_addr"
  6898. };
  6899. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  6900. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  6901. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6902. @"receive_contact" : @"customer_contact",
  6903. @"receive_email" : @"customer_email",
  6904. @"receive_ext" : @"customer_contact_ext",
  6905. @"receive_fax" : @"customer_fax",
  6906. @"receive_name" : @"customer_name",
  6907. @"receive_phone" : @"customer_phone"},
  6908. @"name" : @"select_ship_to",
  6909. @"refresh" : [NSNumber numberWithInteger:1],
  6910. @"value" : @"Sales_Order_Ship_To"};
  6911. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  6912. [dic setValue:choose_dic forKey:@"item_0"];
  6913. // contact id
  6914. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6915. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6916. [contact_id_dic setValue:@"true" forKey:@"required"];
  6917. [dic setValue:contact_id_dic forKey:@"item_1"];
  6918. // company name
  6919. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6920. [dic setValue:company_name_dic forKey:@"item_2"];
  6921. // address
  6922. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6923. NSNumber *required = setting[@"ShippingToAddressRequire"];
  6924. if ([required integerValue]) {
  6925. [ext_dic setValue:@"true" forKey:@"required"];
  6926. }
  6927. [dic setValue:ext_dic forKey:@"item_3"];
  6928. // contact
  6929. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6930. [dic setValue:contact_dic forKey:@"item_4"];
  6931. // phone
  6932. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6933. [dic setValue:phone_dic forKey:@"item_5"];
  6934. // fax
  6935. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6936. [dic setValue:fax_dic forKey:@"item_6"];
  6937. // email
  6938. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6939. [dic setValue:email_dic forKey:@"item_7"];
  6940. }];
  6941. return dic;
  6942. }
  6943. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  6944. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6945. // setting
  6946. NSDictionary *setting = params[@"setting"];
  6947. NSNumber *hide = setting[@"ShipFromHide"];
  6948. [dic setValue:hide forKey:@"hide"];
  6949. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6950. orderCode = [self translateSingleQuote:orderCode];
  6951. NSString *sql = [NSString stringWithFormat:@"select sender_cid,sender_name,sender_ext,sender_contact,sender_email,sender_fax,sender_phone from offline_order where so_id = '%@';",orderCode];
  6952. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,phone from offline_contact where contact_id = 'NPD';";
  6953. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6954. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  6955. __block NSString *name = [self textAtColumn:1 statement:stmt];
  6956. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  6957. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  6958. __block NSString *email = [self textAtColumn:4 statement:stmt];
  6959. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  6960. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  6961. if (!cid.length) {
  6962. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  6963. cid = [self textAtColumn:0 statement:statment];
  6964. name = [self textAtColumn:1 statement:statment];
  6965. ext = [self textAtColumn:2 statement:statment];
  6966. contact = [self textAtColumn:3 statement:statment];
  6967. email = [self textAtColumn:4 statement:statment];
  6968. fax = [self textAtColumn:5 statement:statment];
  6969. phone = [self textAtColumn:6 statement:statment];
  6970. }];
  6971. }
  6972. // count
  6973. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6974. // title
  6975. [dic setValue:@"Ship From" forKey:@"title"];
  6976. // hide
  6977. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6978. // choose
  6979. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6980. [choose_dic setValue:@"choose" forKey:@"aname"];
  6981. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6982. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6983. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  6984. @"key_map" : @{@"sender_cid" : @"customer_cid",
  6985. @"sender_contact" : @"customer_contact",
  6986. @"sender_email" : @"customer_email",
  6987. @"sender_ext" : @"customer_contact_ext",
  6988. @"sender_fax" : @"customer_fax",
  6989. @"sender_name" : @"customer_name",
  6990. @"sender_phone" : @"customer_phone"},
  6991. @"name" : @"select_cid",
  6992. @"refresh" : [NSNumber numberWithInteger:0],
  6993. @"value" : @"Sales_Order_Ship_From"};
  6994. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  6995. [dic setValue:choose_dic forKey:@"item_0"];
  6996. // contact id
  6997. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6998. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6999. [contact_id_dic setValue:@"true" forKey:@"required"];
  7000. [dic setValue:contact_id_dic forKey:@"item_1"];
  7001. // company name
  7002. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7003. [dic setValue:company_name_dic forKey:@"item_2"];
  7004. // address
  7005. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7006. [dic setValue:ext_dic forKey:@"item_3"];
  7007. // contact
  7008. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7009. [dic setValue:contact_dic forKey:@"item_4"];
  7010. // phone
  7011. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7012. [dic setValue:phone_dic forKey:@"item_5"];
  7013. // fax
  7014. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7015. [dic setValue:fax_dic forKey:@"item_6"];
  7016. // email
  7017. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7018. [dic setValue:email_dic forKey:@"item_7"];
  7019. }];
  7020. return dic;
  7021. }
  7022. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  7023. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7024. // setting
  7025. NSDictionary *setting = params[@"setting"];
  7026. NSNumber *hide = setting[@"FreightBillToHide"];
  7027. [dic setValue:hide forKey:@"hide"];
  7028. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7029. orderCode = [self translateSingleQuote:orderCode];
  7030. NSString *sql = [NSString stringWithFormat:@"select shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_email,shipping_billto_fax,shipping_billto_phone from offline_order where so_id = '%@';",orderCode];
  7031. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7032. NSString *cid = [self textAtColumn:0 statement:stmt];
  7033. NSString *name = [self textAtColumn:1 statement:stmt];
  7034. NSString *ext = [self textAtColumn:2 statement:stmt];
  7035. NSString *contact = [self textAtColumn:3 statement:stmt];
  7036. NSString *email = [self textAtColumn:4 statement:stmt];
  7037. NSString *fax = [self textAtColumn:5 statement:stmt];
  7038. NSString *phone = [self textAtColumn:6 statement:stmt];
  7039. // count
  7040. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7041. // title
  7042. [dic setValue:@"Freight Bill To" forKey:@"title"];
  7043. // hide
  7044. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7045. // choose
  7046. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7047. [choose_dic setValue:@"choose" forKey:@"aname"];
  7048. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7049. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7050. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7051. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  7052. @"shipping_billto_contact" : @"receive_contact",
  7053. @"shipping_billto_email" : @"receive_email",
  7054. @"shipping_billto_ext" : @"receive_ext",
  7055. @"shipping_billto_fax" : @"receive_fax",
  7056. @"shipping_billto_name" : @"receive_name",
  7057. @"shipping_billto_phone" : @"receive_phone"},
  7058. @"type" : @"pull"};
  7059. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7060. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7061. @"type" : @"pull",
  7062. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  7063. @"shipping_billto_contact" : @"customer_contact",
  7064. @"shipping_billto_email" : @"customer_email",
  7065. @"shipping_billto_ext" : @"customer_contact_ext",
  7066. @"shipping_billto_fax" : @"customer_fax",
  7067. @"shipping_billto_name" : @"customer_name",
  7068. @"shipping_billto_phone" : @"customer_phone"}
  7069. };
  7070. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7071. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7072. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  7073. @"shipping_billto_contact" : @"sender_contact",
  7074. @"shipping_billto_email" : @"sender_email",
  7075. @"shipping_billto_ext" : @"sender_ext",
  7076. @"shipping_billto_fax" : @"sender_fax",
  7077. @"shipping_billto_name" : @"sender_name",
  7078. @"shipping_billto_phone" : @"sender_phone"},
  7079. @"type" : @"pull"
  7080. };
  7081. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  7082. NSDictionary *select_freight_bill_to_dic = @{
  7083. @"aname" : @"Select freight bill to",
  7084. @"name" : @"select_cid",
  7085. @"refresh" : [NSNumber numberWithInteger:0],
  7086. @"value" : @"Sales_Order_Freight_Bill_To",
  7087. @"key_map" : @{
  7088. @"shipping_billto_cid" : @"customer_cid",
  7089. @"shipping_billto_contact" : @"customer_contact",
  7090. @"shipping_billto_email" : @"customer_email",
  7091. @"shipping_billto_ext" : @"customer_contact_ext",
  7092. @"shipping_billto_fax" : @"customer_fax",
  7093. @"shipping_billto_name" : @"customer_name",
  7094. @"shipping_billto_phone" : @"customer_phone"
  7095. }
  7096. };
  7097. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  7098. [dic setValue:choose_dic forKey:@"item_0"];
  7099. // contact id
  7100. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7101. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7102. [contact_id_dic setValue:@"true" forKey:@"required"];
  7103. [dic setValue:contact_id_dic forKey:@"item_1"];
  7104. // company name
  7105. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7106. [dic setValue:company_name_dic forKey:@"item_2"];
  7107. // address
  7108. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7109. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  7110. if ([ext_required integerValue]) {
  7111. [ext_dic setValue:@"true" forKey:@"required"];
  7112. }
  7113. [dic setValue:ext_dic forKey:@"item_3"];
  7114. // contact
  7115. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7116. [dic setValue:contact_dic forKey:@"item_4"];
  7117. // phone
  7118. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7119. [dic setValue:phone_dic forKey:@"item_5"];
  7120. // fax
  7121. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7122. [dic setValue:fax_dic forKey:@"item_6"];
  7123. // email
  7124. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7125. [dic setValue:email_dic forKey:@"item_7"];
  7126. }];
  7127. return dic;
  7128. }
  7129. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7130. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7131. // setting
  7132. NSDictionary *setting = params[@"setting"];
  7133. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  7134. [dic setValue:hide forKey:@"hide"];
  7135. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7136. orderCode = [self translateSingleQuote:orderCode];
  7137. NSString *sql = [NSString stringWithFormat:@"select billing_cid,billing_name,billing_ext,billing_contact,billing_email,billing_fax,billing_phone from offline_order where so_id = '%@';",orderCode];
  7138. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7139. NSString *cid = [self textAtColumn:0 statement:stmt];
  7140. NSString *name = [self textAtColumn:1 statement:stmt];
  7141. NSString *ext = [self textAtColumn:2 statement:stmt];
  7142. NSString *contact = [self textAtColumn:3 statement:stmt];
  7143. NSString *email = [self textAtColumn:4 statement:stmt];
  7144. NSString *fax = [self textAtColumn:5 statement:stmt];
  7145. NSString *phone = [self textAtColumn:6 statement:stmt];
  7146. // count
  7147. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7148. // title
  7149. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  7150. // hide
  7151. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7152. // choose
  7153. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7154. [choose_dic setValue:@"choose" forKey:@"aname"];
  7155. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7156. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7157. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7158. @"key_map" : @{@"billing_cid" : @"receive_cid",
  7159. @"billing_contact" : @"receive_contact",
  7160. @"billing_email" : @"receive_email",
  7161. @"billing_ext" : @"receive_ext",
  7162. @"billing_fax" : @"receive_fax",
  7163. @"billing_name" : @"receive_name",
  7164. @"billing_phone" : @"receive_phone"},
  7165. @"type" : @"pull"};
  7166. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7167. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7168. @"type" : @"pull",
  7169. @"key_map" : @{@"billing_cid" : @"customer_cid",
  7170. @"billing_contact" : @"customer_contact",
  7171. @"billing_email" : @"customer_email",
  7172. @"billing_ext" : @"customer_contact_ext",
  7173. @"billing_fax" : @"customer_fax",
  7174. @"billing_name" : @"customer_name",
  7175. @"billing_phone" : @"customer_phone"}
  7176. };
  7177. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7178. NSDictionary *select_bill_to_dic = @{
  7179. @"aname" : @"Select bill to",
  7180. @"name" : @"select_cid",
  7181. @"refresh" : [NSNumber numberWithInteger:0],
  7182. @"value" : @"Sales_Order_Merchandise_Bill_To",
  7183. @"key_map" : @{
  7184. @"billing_cid" : @"customer_cid",
  7185. @"billing_contact" : @"customer_contact",
  7186. @"billing_email" : @"customer_email",
  7187. @"billing_ext" : @"customer_contact_ext",
  7188. @"billing_fax" : @"customer_fax",
  7189. @"billing_name" : @"customer_name",
  7190. @"billing_phone" : @"customer_phone"
  7191. }
  7192. };
  7193. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  7194. [dic setValue:choose_dic forKey:@"item_0"];
  7195. // contact id
  7196. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7197. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7198. [contact_id_dic setValue:@"true" forKey:@"required"];
  7199. [dic setValue:contact_id_dic forKey:@"item_1"];
  7200. // company name
  7201. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7202. [dic setValue:company_name_dic forKey:@"item_2"];
  7203. // address
  7204. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7205. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  7206. if ([ext_required integerValue]) {
  7207. [ext_dic setValue:@"true" forKey:@"required"];
  7208. }
  7209. [dic setValue:ext_dic forKey:@"item_3"];
  7210. // contact
  7211. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7212. [dic setValue:contact_dic forKey:@"item_4"];
  7213. // phone
  7214. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7215. [dic setValue:phone_dic forKey:@"item_5"];
  7216. // fax
  7217. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7218. [dic setValue:fax_dic forKey:@"item_6"];
  7219. // email
  7220. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7221. [dic setValue:email_dic forKey:@"item_7"];
  7222. }];
  7223. return dic;
  7224. }
  7225. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7226. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7227. // setting
  7228. NSDictionary *setting = params[@"setting"];
  7229. NSNumber *hide = setting[@"ReturnToHide"];
  7230. [dic setValue:hide forKey:@"hide"];
  7231. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7232. orderCode = [self translateSingleQuote:orderCode];
  7233. NSString *sql = [NSString stringWithFormat:@"select returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_email,returnto_fax,returnto_phone from offline_order where so_id = '%@';",orderCode];
  7234. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7235. NSString *cid = [self textAtColumn:0 statement:stmt];
  7236. NSString *name = [self textAtColumn:1 statement:stmt];
  7237. NSString *ext = [self textAtColumn:2 statement:stmt];
  7238. NSString *contact = [self textAtColumn:3 statement:stmt];
  7239. NSString *email = [self textAtColumn:4 statement:stmt];
  7240. NSString *fax = [self textAtColumn:5 statement:stmt];
  7241. NSString *phone = [self textAtColumn:6 statement:stmt];
  7242. // count
  7243. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7244. // title
  7245. [dic setValue:@"Return To" forKey:@"title"];
  7246. // hide
  7247. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7248. // choose
  7249. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7250. [choose_dic setValue:@"choose" forKey:@"aname"];
  7251. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7252. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  7253. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7254. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  7255. @"returnto_contact" : @"sender_contact",
  7256. @"returnto_email" : @"sender_email",
  7257. @"returnto_ext" : @"sender_ext",
  7258. @"returnto_fax" : @"sender_fax",
  7259. @"returnto_name" : @"sender_name",
  7260. @"returnto_phone" : @"sender_phone"},
  7261. @"type" : @"pull"};
  7262. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  7263. NSDictionary *select_return_to_dic = @{
  7264. @"aname" : @"Select return to",
  7265. @"name" : @"select_cid",
  7266. @"refresh" : [NSNumber numberWithInteger:0],
  7267. @"value" : @"Contact_Return_To",
  7268. @"key_map" : @{
  7269. @"returnto_cid" : @"customer_cid",
  7270. @"returnto_contact" : @"customer_contact",
  7271. @"returnto_email" : @"customer_email",
  7272. @"returnto_ext" : @"customer_contact_ext",
  7273. @"returnto_fax" : @"customer_fax",
  7274. @"returnto_name" : @"customer_name",
  7275. @"returnto_phone" : @"customer_phone"
  7276. }
  7277. };
  7278. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  7279. [dic setValue:choose_dic forKey:@"item_0"];
  7280. // contact id
  7281. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7282. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7283. [contact_id_dic setValue:@"true" forKey:@"required"];
  7284. [dic setValue:contact_id_dic forKey:@"item_1"];
  7285. // company name
  7286. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7287. [dic setValue:company_name_dic forKey:@"item_2"];
  7288. // address
  7289. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7290. [dic setValue:ext_dic forKey:@"item_3"];
  7291. // contact
  7292. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7293. [dic setValue:contact_dic forKey:@"item_4"];
  7294. // phone
  7295. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7296. [dic setValue:phone_dic forKey:@"item_5"];
  7297. // fax
  7298. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7299. [dic setValue:fax_dic forKey:@"item_6"];
  7300. // email
  7301. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7302. [dic setValue:email_dic forKey:@"item_7"];
  7303. }];
  7304. return dic;
  7305. }
  7306. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7307. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7308. orderCode = [self translateSingleQuote:orderCode];
  7309. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7310. __block double TotalCuft = 0;
  7311. __block double TotalWeight = 0;
  7312. __block int TotalCarton = 0;
  7313. __block double payments = 0;
  7314. // setting
  7315. NSDictionary *setting = params[@"setting"];
  7316. NSNumber *hide = setting[@"ModelInformationHide"];
  7317. [dic setValue:hide forKey:@"hide"];
  7318. NSString *sql = [NSString stringWithFormat:@"select c.item_id,c.item_count,m.stockUom,c.price,c.discount,m.name,m.description,c.line_note,m.availability from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id;",orderCode];
  7319. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7320. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7321. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  7322. // item id
  7323. int item_id = sqlite3_column_int(stmt, 0);
  7324. // count
  7325. int item_count = sqlite3_column_int(stmt, 1);
  7326. // stockUom
  7327. int stockUom = sqlite3_column_int(stmt, 2);
  7328. // unit price
  7329. NSString *str_price = [self textAtColumn:3 statement:stmt];
  7330. NSString* Price=nil;
  7331. if([str_price isEqualToString:@""])
  7332. {
  7333. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  7334. if(price==nil)
  7335. Price=@"No Price.";
  7336. else
  7337. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7338. }
  7339. else
  7340. {
  7341. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  7342. }
  7343. // discount
  7344. double discount = sqlite3_column_double(stmt, 4);
  7345. // name
  7346. NSString *name = [self textAtColumn:5 statement:stmt];
  7347. // description
  7348. NSString *description = [self textAtColumn:6 statement:stmt];
  7349. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  7350. // line note
  7351. NSString *line_note = [self textAtColumn:7 statement:stmt];
  7352. int avaulability = sqlite3_column_int(stmt, 8);
  7353. // img
  7354. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  7355. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  7356. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7357. if(combine != nil)
  7358. {
  7359. // int citem=0;
  7360. int bcount=[[combine valueForKey:@"count"] intValue];
  7361. for(int bc=0;bc<bcount;bc++)
  7362. {
  7363. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7364. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7365. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7366. subTotal += uprice * modulus * item_count;
  7367. }
  7368. }
  7369. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  7370. [model_dic setValue:@"model" forKey:@"control"];
  7371. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  7372. [model_dic setValue:description forKey:@"description"];
  7373. [model_dic setValue:line_note forKey:@"note"];
  7374. [model_dic setValue:img forKey:@"img_url"];
  7375. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  7376. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  7377. [model_dic setValue:Price forKey:@"unit_price"];
  7378. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  7379. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  7380. if (combine) {
  7381. [model_dic setValue:combine forKey:@"combine"];
  7382. }
  7383. // well,what under the row is the info for total
  7384. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7385. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7386. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7387. int carton=[bsubtotaljson[@"carton"] intValue];
  7388. TotalCuft += cuft;
  7389. TotalWeight += weight;
  7390. TotalCarton += carton;
  7391. payments += subTotal;
  7392. //---------------------------
  7393. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7394. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  7395. }];
  7396. [dic setValue:@"Model Information" forKey:@"title"];
  7397. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  7398. return dic;
  7399. }
  7400. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  7401. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7402. [dic setValue:@"Remarks Content" forKey:@"title"];
  7403. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7404. // setting
  7405. NSDictionary *setting = params[@"setting"];
  7406. NSNumber *hide = setting[@"RemarksContentHide"];
  7407. [dic setValue:hide forKey:@"hide"];
  7408. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7409. orderCode = [self translateSingleQuote:orderCode];
  7410. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  7411. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7412. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  7413. int mustCall = sqlite3_column_int(stmt, 1);
  7414. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  7415. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  7416. NSDictionary *po_dic = @{
  7417. @"aname" : @"PO#",
  7418. @"control" : @"edit",
  7419. @"keyboard" : @"text",
  7420. @"name" : @"poNumber",
  7421. @"value" : poNumber
  7422. };
  7423. NSDictionary *must_call_dic = @{
  7424. @"aname" : @"MUST CALL BEFORE DELIVERY",
  7425. @"control" : @"switch",
  7426. @"name" : @"must_call",
  7427. @"value" : mustCall ? @"true" : @"false"
  7428. };
  7429. NSDictionary *general_notes_dic = @{
  7430. @"aname" : @"General notes",
  7431. @"control" : @"text_view",
  7432. @"keyboard" : @"text",
  7433. @"name" : @"comments",
  7434. @"value" : generalNotes
  7435. };
  7436. NSDictionary *internal_notes_dic = @{
  7437. @"aname" : @"Internal notes",
  7438. @"control" : @"text_view",
  7439. @"keyboard" : @"text",
  7440. @"name" : @"internal_notes",
  7441. @"value" : internalNotes
  7442. };
  7443. [dic setValue:po_dic forKey:@"item_0"];
  7444. [dic setValue:must_call_dic forKey:@"item_1"];
  7445. [dic setValue:general_notes_dic forKey:@"item_2"];
  7446. [dic setValue:internal_notes_dic forKey:@"item_3"];
  7447. }];
  7448. return dic;
  7449. }
  7450. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  7451. // params
  7452. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7453. orderCode = [self translateSingleQuote:orderCode];
  7454. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7455. // setting
  7456. NSDictionary *setting = params[@"setting"];
  7457. NSNumber *hide = setting[@"OrderTotalHide"];
  7458. [dic setValue:hide forKey:@"hide"];
  7459. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  7460. __block double lift_gate_value = 0;
  7461. __block double handling_fee = 0;
  7462. __block double shipping = 0;
  7463. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7464. int lift_gate = sqlite3_column_int(stmt, 0);
  7465. lift_gate_value = sqlite3_column_double(stmt, 1);
  7466. shipping = sqlite3_column_double(stmt, 2);
  7467. handling_fee = sqlite3_column_double(stmt, 3);
  7468. if (!lift_gate) {
  7469. lift_gate_value = 0;
  7470. }
  7471. }];
  7472. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  7473. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  7474. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  7475. double payments = [[total valueForKey:@"payments"] doubleValue];
  7476. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  7477. double totalPrice = payments;
  7478. [dic setValue:@"Order Total" forKey:@"title"];
  7479. NSDictionary *payments_dic = @{
  7480. @"align" : @"right",
  7481. @"aname" : @"Payments/Credits",
  7482. @"control" : @"text",
  7483. @"name" : @"paymentsAndCredits",
  7484. @"type" : @"price",
  7485. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  7486. };
  7487. [dic setValue:payments_dic forKey:@"item_0"];
  7488. // version 1.71 remove
  7489. // NSDictionary *handling_fee_dic = @{
  7490. // @"align" : @"right",
  7491. // @"aname" : @"Handling Fee",
  7492. // @"control" : @"text",
  7493. // @"name" : @"handling_fee_value",
  7494. // @"required" : @"true",
  7495. // @"type" : @"price",
  7496. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  7497. // };
  7498. NSDictionary *shipping_dic = @{
  7499. @"align" : @"right",
  7500. @"aname" : @"Shipping*",
  7501. @"control" : @"text",
  7502. @"name" : @"shipping",
  7503. @"required" : @"true",
  7504. @"type" : @"price",
  7505. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  7506. };
  7507. NSDictionary *lift_gate_dic = @{
  7508. @"align" : @"right",
  7509. @"aname" : @"Liftgate Fee(No Loading Dock)",
  7510. @"control" : @"text",
  7511. @"name" : @"lift_gate_value",
  7512. @"required" : @"true",
  7513. @"type" : @"price",
  7514. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  7515. };
  7516. int item_count = 1;
  7517. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  7518. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  7519. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  7520. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7521. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7522. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  7523. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7524. } else {
  7525. }
  7526. }
  7527. // version 1.71 remove
  7528. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7529. NSDictionary *total_price_dic = @{
  7530. @"align" : @"right",
  7531. @"aname" : @"Total",
  7532. @"control" : @"text",
  7533. @"name" : @"totalPrice",
  7534. @"type" : @"price",
  7535. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  7536. };
  7537. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7538. NSDictionary *total_cuft_dic = @{
  7539. @"align" : @"right",
  7540. @"aname" : @"Total Cuft",
  7541. @"control" : @"text",
  7542. @"name" : @"",
  7543. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  7544. };
  7545. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7546. NSDictionary *total_weight_dic = @{
  7547. @"align" : @"right",
  7548. @"aname" : @"Total Weight",
  7549. @"control" : @"text",
  7550. @"name" : @"",
  7551. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  7552. };
  7553. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7554. NSDictionary *total_carton_dic = @{
  7555. @"align" : @"right",
  7556. @"aname" : @"Total Carton",
  7557. @"control" : @"text",
  7558. @"name" : @"",
  7559. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  7560. };
  7561. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7562. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  7563. return dic;
  7564. }
  7565. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  7566. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7567. orderCode = [self translateSingleQuote:orderCode];
  7568. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7569. // setting
  7570. NSDictionary *setting = params[@"setting"];
  7571. NSNumber *hide = setting[@"SignatureHide"];
  7572. [dic setValue:hide forKey:@"hide"];
  7573. [dic setValue:@"Signature" forKey:@"title"];
  7574. __block NSString *pic_path = @"";
  7575. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  7576. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7577. pic_path = [self textAtColumn:0 statement:stmt];
  7578. }];
  7579. NSDictionary *pic_dic = @{
  7580. @"aname" : @"Signature",
  7581. @"avalue" :pic_path,
  7582. @"control" : @"signature",
  7583. @"name" : @"sign_picpath",
  7584. @"value" : pic_path
  7585. };
  7586. [dic setValue:pic_dic forKey:@"item_0"];
  7587. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7588. return dic;
  7589. }
  7590. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  7591. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7592. // setting
  7593. NSDictionary *setting = params[@"setting"];
  7594. NSNumber *hide = setting[@"ShippingMethodHide"];
  7595. [dic setValue:hide forKey:@"hide"];
  7596. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7597. orderCode = [self translateSingleQuote:orderCode];
  7598. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  7599. __block NSString *logist = @"";
  7600. __block NSString *lift_gate = @"";
  7601. __block int lift_gate_integer = 0;
  7602. __block NSString *logistic_note = @"";
  7603. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7604. logist = [self textAtColumn:0 statement:stmt];
  7605. lift_gate_integer = sqlite3_column_int(stmt, 1);
  7606. logistic_note = [self textAtColumn:2 statement:stmt];
  7607. }];
  7608. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7609. [dic setValue:@"Shipping Method" forKey:@"title"];
  7610. // val_0
  7611. int PERSONAL_PICK_UP_check = 0;
  7612. int USE_MY_CARRIER_check = 0;
  7613. NSString *logistic_note_text = @"";
  7614. int will_call_check = 0;
  7615. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  7616. will_call_check = 1;
  7617. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  7618. PERSONAL_PICK_UP_check = 1;
  7619. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  7620. USE_MY_CARRIER_check = 1;
  7621. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  7622. logistic_note = [logistic_note_array firstObject];
  7623. if (logistic_note_array.count > 1) {
  7624. logistic_note_text = [logistic_note_array lastObject];
  7625. }
  7626. }
  7627. }
  7628. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  7629. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  7630. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  7631. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  7632. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  7633. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7634. NSDictionary *val0_subItem_item0 = @{
  7635. @"aname" : @"Option",
  7636. @"cadedate" : @{
  7637. @"count" : [NSNumber numberWithInteger:2],
  7638. @"val_0" : @{
  7639. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  7640. @"refresh" : [NSNumber numberWithInteger:0],
  7641. @"value" : @"PERSONAL PICK UP",
  7642. @"value_id" : @"PERSONAL PICK UP"
  7643. },
  7644. @"val_1" : @{
  7645. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  7646. @"refresh" : [NSNumber numberWithInteger:0],
  7647. @"sub_item" : @{
  7648. @"count" : [NSNumber numberWithInteger:1],
  7649. @"item_0" : @{
  7650. @"aname" : @"BOL",
  7651. @"control" : @"edit",
  7652. @"keyboard" : @"text",
  7653. @"name" : @"logist_note_text",
  7654. @"refresh" : [NSNumber numberWithInteger:0],
  7655. @"required" : @"false",
  7656. @"value" : logistic_note_text
  7657. }
  7658. },
  7659. @"value" : @"USE MY CARRIER",
  7660. @"value_id" : @"USE MY CARRIER"
  7661. }
  7662. },
  7663. @"control" : @"enum",
  7664. @"name" : @"logistic_note",
  7665. @"required" : @"true",
  7666. @"single_select" : @"true"
  7667. };
  7668. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  7669. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  7670. // val_1
  7671. lift_gate = [self valueInParams:params key:@"lift_gate"];
  7672. if([lift_gate isEqualToString:@""]) {
  7673. if (lift_gate_integer == 1) {
  7674. lift_gate = @"true";
  7675. } else {
  7676. lift_gate = @"false";
  7677. }
  7678. }
  7679. int common_carrier_check = 0;
  7680. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  7681. common_carrier_check = 1;
  7682. [params setValue:lift_gate forKey:@"lift_gate"];
  7683. }
  7684. NSDictionary *val_1 = @{
  7685. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  7686. @"sub_item" : @{
  7687. @"count" : [NSNumber numberWithInteger:1],
  7688. @"item_0" : @{
  7689. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  7690. @"control" : @"switch",
  7691. @"name" : @"lift_gate",
  7692. @"refresh" : [NSNumber numberWithInteger:1],
  7693. @"required" : @"true",
  7694. @"value" : lift_gate
  7695. }
  7696. },
  7697. @"value" : @"COMMON CARRIER",
  7698. @"value_id" : @"COMMON CARRIER"
  7699. };
  7700. // cadedate
  7701. NSDictionary *cadedate = @{
  7702. @"count" : [NSNumber numberWithInteger:2],
  7703. @"val_0" : val_0,
  7704. @"val_1" : val_1
  7705. };
  7706. // item_0
  7707. NSMutableDictionary *item_0 = @{
  7708. @"aname" : @"Shipping",
  7709. @"cadedate" : cadedate,
  7710. @"control" : @"enum",
  7711. @"name" : @"logist",
  7712. @"refresh" : [NSNumber numberWithInteger:1],
  7713. @"single_select" : @"true",
  7714. }.mutableCopy;
  7715. NSNumber *required = setting[@"ShippingMethodRequire"];
  7716. if ([required integerValue]) {
  7717. [item_0 setValue:@"true" forKey:@"required"];
  7718. }
  7719. [dic setValue:item_0 forKey:@"item_0"];
  7720. if ([logist isEqualToString:@"WILL CALL"]) {
  7721. [dic removeObjectForKey:@"lift_gate"];
  7722. }
  7723. return dic;
  7724. }
  7725. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  7726. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7727. orderCode = [self translateSingleQuote:orderCode];
  7728. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  7729. __block int submit_as_integer = 0;
  7730. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7731. submit_as_integer = sqlite3_column_int(stmt, 0);
  7732. }];
  7733. int check11 = 0;
  7734. int check10 = 0;
  7735. if (submit_as_integer == 11) {
  7736. check11 = 1;
  7737. }
  7738. if (submit_as_integer == 10) {
  7739. check10 = 1;
  7740. }
  7741. // section_0
  7742. NSMutableDictionary *dic = @{
  7743. @"count" : @(1),
  7744. @"item_0" : @{
  7745. @"aname" : @"Submit Order As",
  7746. @"cadedate" : @{
  7747. @"count" : @(2),
  7748. @"val_0" : @{
  7749. @"check" : [NSNumber numberWithInteger:check11],
  7750. @"value" : @"Sales Order",
  7751. @"value_id" : @(11)
  7752. },
  7753. @"val_1" : @{
  7754. @"check" : [NSNumber numberWithInteger:check10],
  7755. @"value" : @"Quote",
  7756. @"value_id" : @(10)
  7757. }
  7758. },
  7759. @"control" : @"enum",
  7760. @"name" : @"erpOrderStatus",
  7761. @"required" : @"true",
  7762. @"single_select" : @"true"
  7763. },
  7764. @"title" : @"Order Type"
  7765. }.mutableCopy;
  7766. // setting
  7767. NSDictionary *setting = params[@"setting"];
  7768. NSNumber *hide = setting[@"OrderTypeHide"];
  7769. [dic setValue:hide forKey:@"hide"];
  7770. return dic;
  7771. }
  7772. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  7773. countryCode = [self translateSingleQuote:countryCode];
  7774. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  7775. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7776. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  7777. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  7778. if (name == NULL) {
  7779. name = "";
  7780. }
  7781. if (code == NULL) {
  7782. code = "";
  7783. }
  7784. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7785. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  7786. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  7787. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7788. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  7789. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7790. }
  7791. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  7792. [container setValue:stateDic forKey:key];
  7793. }] mutableCopy];
  7794. [ret removeObjectForKey:@"result"];
  7795. // failure 可以不用了,一样的
  7796. if (ret.allKeys.count == 0) {
  7797. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7798. [stateDic setValue:@"Other" forKey:@"value"];
  7799. [stateDic setValue:@"" forKey:@"value_id"];
  7800. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7801. if (state_code && [@"" isEqualToString:state_code]) {
  7802. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7803. }
  7804. NSString *key = [NSString stringWithFormat:@"val_0"];
  7805. [ret setValue:stateDic forKey:key];
  7806. }
  7807. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  7808. return ret;
  7809. }
  7810. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7811. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7812. orderCode = [self translateSingleQuote:orderCode];
  7813. NSString *sql = [NSString stringWithFormat:@"select paymentType,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,decrypt(credit_card_number),decrypt(credit_card_security_code),decrypt(credit_card_expiration_month),decrypt(credit_card_expiration_year),credit_card_city,credit_card_state from offline_order where so_id = '%@';",orderCode];
  7814. __block NSString *payType = @"";
  7815. __block NSString *firstName = @"";
  7816. __block NSString *lastName = @"";
  7817. __block NSString *addr1 = @"";
  7818. __block NSString *addr2 = @"";
  7819. __block NSString *zipcode = @"";
  7820. __block NSString *cardType = @"";
  7821. __block NSString *cardNumber = @"";
  7822. __block NSString *securityCode = @"";
  7823. __block NSString *month = @"";
  7824. __block NSString *year = @"";
  7825. __block NSString *city = @"";
  7826. __block NSString *state = @"";
  7827. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7828. payType = [self textAtColumn:0 statement:stmt];
  7829. firstName = [self textAtColumn:1 statement:stmt];
  7830. lastName = [self textAtColumn:2 statement:stmt];
  7831. addr1 = [self textAtColumn:3 statement:stmt];
  7832. addr2 = [self textAtColumn:4 statement:stmt];
  7833. zipcode = [self textAtColumn:5 statement:stmt];
  7834. cardType = [self textAtColumn:6 statement:stmt];
  7835. cardNumber = [self textAtColumn:7 statement:stmt];
  7836. securityCode = [self textAtColumn:8 statement:stmt];
  7837. month = [self textAtColumn:9 statement:stmt];
  7838. year = [self textAtColumn:10 statement:stmt];
  7839. city = [self textAtColumn:11 statement:stmt];
  7840. state = [self textAtColumn:12 statement:stmt];
  7841. }];
  7842. NSString *required = @"true";
  7843. // setting
  7844. NSDictionary *setting = params[@"setting"];
  7845. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  7846. if ([requiredNumber integerValue]) {
  7847. required = @"true";
  7848. } else {
  7849. required = @"false";
  7850. }
  7851. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  7852. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  7853. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  7854. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7855. NSString *type_id = [self textAtColumn:0 statement:stmt];
  7856. NSString *type = [self textAtColumn:1 statement:stmt];
  7857. NSMutableDictionary *val = @{
  7858. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  7859. @"value" : type,
  7860. @"value_id" : type_id
  7861. }.mutableCopy;
  7862. if ([type_id isEqualToString:@"Visa/Master"] || [type_id isEqualToString:@"Credit Card"]) {
  7863. NSDictionary *sub_item = @{
  7864. @"count" : @(3),
  7865. @"item_0" : @{
  7866. @"aname" : @"choose",
  7867. @"control" : @"multi_action",
  7868. @"count" : @(1),
  7869. @"item_0" : @{
  7870. @"aname" : @"Same as customer",
  7871. @"key_map" : @{
  7872. @"credit_card_address1" : @"customer_address1",
  7873. @"credit_card_address2" : @"customer_address2",
  7874. @"credit_card_city" : @"customer_city",
  7875. @"credit_card_first_name" : @"customer_first_name",
  7876. @"credit_card_last_name" : @"customer_last_name",
  7877. @"credit_card_state" : @"customer_state",
  7878. @"credit_card_zipcode" : @"customer_zipcode"
  7879. },
  7880. @"type" : @"pull"
  7881. }
  7882. },
  7883. @"item_1" : @{
  7884. @"aname" : @"",
  7885. @"color" : @"red",
  7886. @"control" : @"text",
  7887. @"name" : @"",
  7888. @"value" : @"USA Credit cards only"
  7889. },
  7890. @"item_2" : @{
  7891. @"aname" : @"Fill",
  7892. @"cadedate" : @{
  7893. @"count" : @(2),
  7894. @"val_0" : @{
  7895. @"check" : @(1),
  7896. @"sub_item" : @{
  7897. @"count" : @(11),
  7898. @"item_0" : @{
  7899. @"aname" : @"Type",
  7900. @"cadedate" : @{
  7901. @"count" : @(2),
  7902. @"val_0" : @{
  7903. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  7904. @"value" : @"VISA",
  7905. @"value_id" : @"VISA"/*@(0)*/
  7906. },
  7907. @"val_1" : @{
  7908. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  7909. @"value" : @"MASTER CARD",
  7910. @"value_id" : @"MASTER CARD"/*@(1)*/
  7911. }
  7912. },
  7913. @"control" : @"enum",
  7914. @"name" : @"credit_card_type",
  7915. @"required" : @"true",
  7916. @"single_select" : @"true"
  7917. },
  7918. @"item_1" : @{
  7919. @"aname" : @"Number",
  7920. @"control" : @"edit",
  7921. @"keyboard" : @"int",
  7922. @"length" : @"16",
  7923. @"name" : @"credit_card_number",
  7924. @"required" : @"true",
  7925. @"value" : cardNumber
  7926. },
  7927. @"item_10" : @{
  7928. @"aname" : @"State",
  7929. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  7930. @"control" : @"enum",
  7931. @"enum" : @"true",
  7932. @"name" : @"credit_card_state",
  7933. @"required" : @"true",
  7934. @"single_select" : @"true"
  7935. },
  7936. @"item_2" : @{
  7937. @"aname" : @"Expiration Date",
  7938. @"control" : @"monthpicker",
  7939. @"name" : @"credit_card_expiration",
  7940. @"required" : @"true",
  7941. @"type" : @"date",
  7942. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  7943. },
  7944. @"item_3" : @{
  7945. @"aname" : @"Security Code",
  7946. @"control" : @"edit",
  7947. @"keyboard" : @"int",
  7948. @"length" : @"3",
  7949. @"name" : @"credit_card_security_code",
  7950. @"required" : @"true",
  7951. @"value" : securityCode
  7952. },
  7953. @"item_4" : @{
  7954. @"aname" : @"First Name",
  7955. @"control" : @"edit",
  7956. @"keyboard" : @"text",
  7957. @"name" : @"credit_card_first_name",
  7958. @"required" : @"true",
  7959. @"value" : firstName
  7960. },
  7961. @"item_5" : @{
  7962. @"aname" : @"Last Name",
  7963. @"control" : @"edit",
  7964. @"keyboard" : @"text",
  7965. @"name" : @"credit_card_last_name",
  7966. @"required" : @"true",
  7967. @"value" : lastName
  7968. },
  7969. @"item_6" : @{
  7970. @"aname" : @"Address 1",
  7971. @"control" : @"edit",
  7972. @"keyboard" : @"text",
  7973. @"name" : @"credit_card_address1",
  7974. @"required" : @"true",
  7975. @"value" : addr1
  7976. },
  7977. @"item_7" : @{
  7978. @"aname" : @"Address 2",
  7979. @"control" : @"edit",
  7980. @"keyboard" : @"text",
  7981. @"name" : @"credit_card_address2",
  7982. @"value" : addr2
  7983. },
  7984. @"item_8" : @{
  7985. @"aname" : @"zip code",
  7986. @"control" : @"edit",
  7987. @"keyboard" : @"text",
  7988. @"name" : @"credit_card_zipcode",
  7989. @"required" : @"true",
  7990. @"value" : zipcode
  7991. },
  7992. @"item_9" : @{
  7993. @"aname" : @"City",
  7994. @"control" : @"edit",
  7995. @"keyboard" : @"text",
  7996. @"name" : @"credit_card_city",
  7997. @"required" : @"true",
  7998. @"value" : city
  7999. }
  8000. },
  8001. @"value" : @"Fill Now",
  8002. @"value_id" : @""
  8003. },
  8004. @"val_1" : @{
  8005. @"check" : @(0),
  8006. @"value" : @"Fill Later",
  8007. @"value_id" : @""
  8008. }
  8009. },
  8010. @"control" : @"enum",
  8011. @"name" : @"",
  8012. @"single_select" : @"true"
  8013. }
  8014. };
  8015. [val setObject:sub_item forKey:@"sub_item"];
  8016. }
  8017. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  8018. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  8019. }];
  8020. // "section_2"
  8021. NSMutableDictionary *dic = @{
  8022. @"count" : @(1),
  8023. @"item_0" : @{
  8024. @"aname" : @"Payment",
  8025. @"required" : required,
  8026. @"cadedate" : cadedate,
  8027. // @{
  8028. // @"count" : @(6),
  8029. // @"val_3" : @{
  8030. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  8031. // @"value" : @"Check",
  8032. // @"value_id" : @"Check"
  8033. // },
  8034. // @"val_2" : @{
  8035. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  8036. // @"value" : @"Cash",
  8037. // @"value_id" : @"Cash"
  8038. // },
  8039. // @"val_1" : @{
  8040. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  8041. // @"value" : @"NET 60",
  8042. // @"value_id" : @"NET 30"
  8043. // },
  8044. // @"val_4" : @{
  8045. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  8046. // @"value" : @"Wire Transfer",
  8047. // @"value_id" : @"Wire Transfer"
  8048. // },
  8049. // @"val_0" : @{
  8050. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  8051. // @"sub_item" : @{
  8052. // @"count" : @(3),
  8053. // @"item_0" : @{
  8054. // @"aname" : @"choose",
  8055. // @"control" : @"multi_action",
  8056. // @"count" : @(1),
  8057. // @"item_0" : @{
  8058. // @"aname" : @"Same as customer",
  8059. // @"key_map" : @{
  8060. // @"credit_card_address1" : @"customer_address1",
  8061. // @"credit_card_address2" : @"customer_address2",
  8062. // @"credit_card_city" : @"customer_city",
  8063. // @"credit_card_first_name" : @"customer_first_name",
  8064. // @"credit_card_last_name" : @"customer_last_name",
  8065. // @"credit_card_state" : @"customer_state",
  8066. // @"credit_card_zipcode" : @"customer_zipcode"
  8067. // },
  8068. // @"type" : @"pull"
  8069. // }
  8070. // },
  8071. // @"item_1" : @{
  8072. // @"aname" : @"",
  8073. // @"color" : @"red",
  8074. // @"control" : @"text",
  8075. // @"name" : @"",
  8076. // @"value" : @"USA Credit cards only"
  8077. // },
  8078. // @"item_2" : @{
  8079. // @"aname" : @"Fill",
  8080. // @"cadedate" : @{
  8081. // @"count" : @(2),
  8082. // @"val_0" : @{
  8083. // @"check" : @(1),
  8084. // @"sub_item" : @{
  8085. // @"count" : @(11),
  8086. // @"item_0" : @{
  8087. // @"aname" : @"Type",
  8088. // @"cadedate" : @{
  8089. // @"count" : @(2),
  8090. // @"val_0" : @{
  8091. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8092. // @"value" : @"VISA",
  8093. // @"value_id" : @(0)
  8094. // },
  8095. // @"val_1" : @{
  8096. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8097. // @"value" : @"MASTER CARD",
  8098. // @"value_id" : @(1)
  8099. // }
  8100. // },
  8101. // @"control" : @"enum",
  8102. // @"name" : @"credit_card_type",
  8103. // @"required" : @"true",
  8104. // @"single_select" : @"true"
  8105. // },
  8106. // @"item_1" : @{
  8107. // @"aname" : @"Number",
  8108. // @"control" : @"edit",
  8109. // @"keyboard" : @"int",
  8110. // @"length" : @"16",
  8111. // @"name" : @"credit_card_number",
  8112. // @"required" : @"true",
  8113. // @"value" : cardNumber
  8114. // },
  8115. // @"item_10" : @{
  8116. // @"aname" : @"State",
  8117. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8118. // @"control" : @"enum",
  8119. // @"enum" : @"true",
  8120. // @"name" : @"credit_card_state",
  8121. // @"required" : @"true",
  8122. // @"single_select" : @"true"
  8123. // },
  8124. // @"item_2" : @{
  8125. // @"aname" : @"Expiration Date",
  8126. // @"control" : @"monthpicker",
  8127. // @"name" : @"credit_card_expiration",
  8128. // @"required" : @"true",
  8129. // @"type" : @"date",
  8130. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8131. // },
  8132. // @"item_3" : @{
  8133. // @"aname" : @"Security Code",
  8134. // @"control" : @"edit",
  8135. // @"keyboard" : @"int",
  8136. // @"length" : @"3",
  8137. // @"name" : @"credit_card_security_code",
  8138. // @"required" : @"true",
  8139. // @"value" : securityCode
  8140. // },
  8141. // @"item_4" : @{
  8142. // @"aname" : @"First Name",
  8143. // @"control" : @"edit",
  8144. // @"keyboard" : @"text",
  8145. // @"name" : @"credit_card_first_name",
  8146. // @"required" : @"true",
  8147. // @"value" : firstName
  8148. // },
  8149. // @"item_5" : @{
  8150. // @"aname" : @"Last Name",
  8151. // @"control" : @"edit",
  8152. // @"keyboard" : @"text",
  8153. // @"name" : @"credit_card_last_name",
  8154. // @"required" : @"true",
  8155. // @"value" : lastName
  8156. // },
  8157. // @"item_6" : @{
  8158. // @"aname" : @"Address 1",
  8159. // @"control" : @"edit",
  8160. // @"keyboard" : @"text",
  8161. // @"name" : @"credit_card_address1",
  8162. // @"required" : @"true",
  8163. // @"value" : addr1
  8164. // },
  8165. // @"item_7" : @{
  8166. // @"aname" : @"Address 2",
  8167. // @"control" : @"edit",
  8168. // @"keyboard" : @"text",
  8169. // @"name" : @"credit_card_address2",
  8170. // @"value" : addr2
  8171. // },
  8172. // @"item_8" : @{
  8173. // @"aname" : @"zip code",
  8174. // @"control" : @"edit",
  8175. // @"keyboard" : @"text",
  8176. // @"name" : @"credit_card_zipcode",
  8177. // @"required" : @"true",
  8178. // @"value" : zipcode
  8179. // },
  8180. // @"item_9" : @{
  8181. // @"aname" : @"City",
  8182. // @"control" : @"edit",
  8183. // @"keyboard" : @"text",
  8184. // @"name" : @"credit_card_city",
  8185. // @"required" : @"true",
  8186. // @"value" : city
  8187. // }
  8188. // },
  8189. // @"value" : @"Fill Now",
  8190. // @"value_id" : @""
  8191. // },
  8192. // @"val_1" : @{
  8193. // @"check" : @(0),
  8194. // @"value" : @"Fill Later",
  8195. // @"value_id" : @""
  8196. // }
  8197. // },
  8198. // @"control" : @"enum",
  8199. // @"name" : @"",
  8200. // @"single_select" : @"true"
  8201. // }
  8202. // },
  8203. // @"value" : @"Visa/Master",
  8204. // @"value_id" : @"Credit Card"
  8205. // },
  8206. // @"val_5" : @{
  8207. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  8208. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  8209. // @"value_id" : @"FOLLOW EXISTING"
  8210. // }
  8211. // },
  8212. @"control" : @"enum",
  8213. @"name" : @"paymentType",
  8214. @"single_select" : @"true"
  8215. },
  8216. @"title" : @"Payment Information"
  8217. }.mutableCopy;
  8218. NSNumber *hide = setting[@"PaymentInformationHide"];
  8219. [dic setValue:hide forKey:@"hide"];
  8220. return dic;
  8221. }
  8222. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  8223. // params
  8224. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8225. orderCode = [self translateSingleQuote:orderCode];
  8226. // 缺货检查
  8227. NSString *out_of_stock_SQL = [NSString stringWithFormat:@"select p.availability,c.item_count from product as p left join offline_cart as c on p.product_id = c.product_id where c.so_no = '%@';",orderCode];
  8228. __block BOOL outOfStock = NO;
  8229. sqlite3 *database = db;
  8230. if (!db) {
  8231. database = [iSalesDB get_db];
  8232. }
  8233. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8234. int availability = sqlite3_column_int(stmt, 0);
  8235. int item_qty = sqlite3_column_int(stmt, 1);
  8236. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  8237. }];
  8238. if (!db) {
  8239. [iSalesDB close_db:database];
  8240. }
  8241. return outOfStock;
  8242. }
  8243. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  8244. {
  8245. sqlite3 *db = [iSalesDB get_db];
  8246. // params
  8247. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8248. orderCode = [self translateSingleQuote:orderCode];
  8249. // 缺货检查
  8250. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8251. if (!appDelegate.can_create_backorder) {
  8252. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  8253. if (out_of_stock) {
  8254. [iSalesDB close_db:db];
  8255. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  8256. [resultDic setObject:[NSString stringWithFormat:@"some item(s) are out of stock,please remove them from cart before placing order with %@ or try again after sync",COMPANY_SHORT_NAME] forKey:@"err_msg"];
  8257. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  8258. return [RAUtils dict2data:resultDic];
  8259. }
  8260. }
  8261. // UISetting
  8262. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8263. NSString *cachefolder = [paths objectAtIndex:0];
  8264. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  8265. NSData* json =nil;
  8266. json=[NSData dataWithContentsOfFile:img_cache];
  8267. NSError *error=nil;
  8268. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  8269. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  8270. [params setObject:setting forKey:@"setting"];
  8271. int section_count = 0;
  8272. // 0 Order Type 1 Shipping Method 2 Payment Information
  8273. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  8274. // 0 Order Type
  8275. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  8276. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  8277. [ret setValue:order_type_dic forKey:key0];
  8278. // 1 Shipping Method
  8279. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  8280. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  8281. [ret setValue:shipping_method_dic forKey:key1];
  8282. // 2 Payment Information
  8283. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  8284. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  8285. [ret setValue:payment_info_dic forKey:key2];
  8286. // 3 Customer
  8287. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  8288. NSDictionary *customer_dic = [self customerDic:params db:db];
  8289. [ret setValue:customer_dic forKey:key3];
  8290. // 4 Ship To
  8291. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  8292. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  8293. [ret setValue:ship_to_dic forKey:key4];
  8294. // 5 Ship From
  8295. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  8296. if (![shipFromDisable integerValue]) {
  8297. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  8298. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  8299. [ret setValue:ship_from_dic forKey:key5];
  8300. }
  8301. // 6 Freight Bill To
  8302. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  8303. if (![freightBillToDisable integerValue]) {
  8304. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  8305. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  8306. [ret setValue:freight_bill_to forKey:key6];
  8307. }
  8308. // 7 Merchandise Bill To
  8309. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  8310. if (![merchandiseBillToDisable integerValue]) {
  8311. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  8312. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  8313. [ret setValue:merchandise_bill_to_dic forKey:key7];
  8314. }
  8315. // 8 Return To
  8316. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  8317. if (![returnToDisable integerValue]) {
  8318. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  8319. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  8320. [ret setValue:return_to_dic forKey:key8];
  8321. }
  8322. // 9 Model Information
  8323. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  8324. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  8325. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  8326. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  8327. [ret setValue:model_info_dic forKey:key9];
  8328. // 10 Remarks Content
  8329. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  8330. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  8331. [ret setValue:remarks_content_dic forKey:key10];
  8332. // 11 Order Total
  8333. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  8334. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  8335. [ret setValue:order_total_dic forKey:key11];
  8336. // 12 Signature
  8337. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  8338. NSDictionary *sign_dic = [self signatureDic:params db:db];
  8339. [ret setValue:sign_dic forKey:key12];
  8340. [ret setValue:@(section_count) forKey:@"section_count"];
  8341. [iSalesDB close_db:db];
  8342. return [RAUtils dict2data:ret];
  8343. // return nil;
  8344. }
  8345. #pragma mark addr editor
  8346. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  8347. NSMutableDictionary *new_item = [item mutableCopy];
  8348. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  8349. return new_item;
  8350. }
  8351. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  8352. {
  8353. // "is_subaction" = true;
  8354. // orderCode = MOB1608240002;
  8355. // password = 123456;
  8356. // "subaction_tag" = 1;
  8357. // user = EvanK;
  8358. // {
  8359. // "is_subaction" = true;
  8360. // orderCode = MOB1608240002;
  8361. // password = 123456;
  8362. // "refresh_trigger" = zipcode;
  8363. // "subaction_tag" = 1;
  8364. // user = EvanK;
  8365. // }
  8366. NSString *country_code = nil;
  8367. NSString *zipCode = nil;
  8368. NSString *stateCode = nil;
  8369. NSString *city = nil;
  8370. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  8371. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  8372. NSString *code_id = params[@"country"];
  8373. country_code = [self countryCodeByid:code_id];
  8374. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  8375. NSString *zip_code = params[@"zipcode"];
  8376. // 剔除全部为空格
  8377. int spaceCount = 0;
  8378. for (int i = 0; i < zip_code.length; i++) {
  8379. if ([zip_code characterAtIndex:i] == ' ') {
  8380. spaceCount++;
  8381. }
  8382. }
  8383. if (spaceCount == zip_code.length) {
  8384. zip_code = @"";
  8385. }
  8386. zipCode = zip_code;
  8387. if (zipCode.length > 0) {
  8388. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  8389. country_code = [dic valueForKey:@"country_code"];
  8390. if (!country_code) {
  8391. // country_code = @"US";
  8392. NSString *code_id = params[@"country"];
  8393. country_code = [self countryCodeByid:code_id];
  8394. }
  8395. stateCode = [dic valueForKey:@"state_code"];
  8396. if(!stateCode.length) {
  8397. stateCode = params[@"state"];
  8398. }
  8399. city = [dic valueForKey:@"city"];
  8400. if (!city.length) {
  8401. city = params[@"city"];
  8402. }
  8403. // zip code
  8404. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  8405. // [zipDic setValue:zipCode forKey:@"value"];
  8406. // [section_0 setValue:zipDic forKey:@"item_11"];
  8407. } else {
  8408. NSString *code_id = params[@"country"];
  8409. country_code = [self countryCodeByid:code_id];
  8410. stateCode = params[@"state"];
  8411. city = params[@"city"];
  8412. }
  8413. }
  8414. }
  8415. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  8416. // [ret removeObjectForKey:@"up_params"];
  8417. [ret setObject:@"New Address" forKey:@"title"];
  8418. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  8419. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  8420. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  8421. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  8422. // [country_dic removeObjectForKey:@"refresh"];
  8423. // [country_dic removeObjectForKey:@"restore"];
  8424. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  8425. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  8426. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  8427. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  8428. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  8429. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  8430. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  8431. // [zip_code_dic removeObjectForKey:@"refresh"];
  8432. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  8433. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  8434. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  8435. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  8436. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  8437. [cityDic setValue:city ? city : @"" forKey:@"value"];
  8438. [new_section_0 setObject:cityDic forKey:@"item_8"];
  8439. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  8440. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  8441. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  8442. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  8443. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  8444. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  8445. // country
  8446. NSString *countryCode = country_code == nil ? @"US" : country_code;
  8447. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  8448. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  8449. // state
  8450. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  8451. // NSDictionary *tmpDic = @{
  8452. // @"value" : @"Other",
  8453. // @"value_id" : @"",
  8454. // @"check" : [NSNumber numberWithInteger:0]
  8455. // };
  8456. //
  8457. // for (int i = 0; i < allState.allKeys.count; i++) {
  8458. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  8459. //
  8460. // NSDictionary *tmp = allState[key];
  8461. // [allState setValue:tmpDic forKey:key];
  8462. // tmpDic = tmp;
  8463. // }
  8464. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  8465. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  8466. [ret setValue:new_section_0 forKey:@"section_0"];
  8467. return [RAUtils dict2data:ret];
  8468. }
  8469. #pragma mark save addr
  8470. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  8471. {
  8472. // NSString *companyName = [self valueInParams:params key:@"company"];
  8473. // NSString *addr1 = [self valueInParams:params key:@"address"];
  8474. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  8475. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  8476. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  8477. // NSString *countryId = [self valueInParams:params key:@"country"];
  8478. // NSString *stateCode = [self valueInParams:params key:@"state"];
  8479. // NSString *city = [self valueInParams:params key:@"city"];
  8480. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  8481. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  8482. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  8483. // NSString *phone = [self valueInParams:params key:@"phone"];
  8484. // NSString *fax = [self valueInParams:params key:@"fax"];
  8485. // NSString *email = [self valueInParams:params key:@"email"];
  8486. return [self offline_saveContact:params update:NO isCustomer:NO];
  8487. }
  8488. #pragma mark cancel order
  8489. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  8490. {
  8491. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8492. NSString *order_id = [self valueInParams:params key:@"orderId"];
  8493. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  8494. if (order_id.length) {
  8495. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  8496. }
  8497. int ret = [iSalesDB execSql:sql];
  8498. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8499. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8500. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8501. // [dic setValue:@"160409" forKey:@"min_ver"];
  8502. return [RAUtils dict2data:dic];
  8503. }
  8504. #pragma mark sign order
  8505. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  8506. {
  8507. //参考 offline_saveBusinesscard
  8508. DebugLog(@"sign order params: %@",params);
  8509. // orderCode = MOB1608240002;
  8510. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  8511. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8512. orderCode = [self translateSingleQuote:orderCode];
  8513. NSString *picPath = [self valueInParams:params key:@"picpath"];
  8514. picPath = [self translateSingleQuote:picPath];
  8515. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  8516. int ret = [iSalesDB execSql:sql];
  8517. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8518. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8519. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8520. // [dic setValue:@"160409" forKey:@"min_ver"];
  8521. return [RAUtils dict2data:dic];
  8522. }
  8523. #pragma mark save order
  8524. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  8525. NSString *ret = [self valueInParams:params key:key];
  8526. if (translate) {
  8527. ret = [self translateSingleQuote:ret];
  8528. }
  8529. return ret;
  8530. }
  8531. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  8532. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  8533. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  8534. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  8535. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  8536. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  8537. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  8538. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  8539. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  8540. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  8541. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  8542. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  8543. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  8544. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  8545. if (![total_price isEqualToString:@""]) {
  8546. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  8547. } else {
  8548. total_price = @"";
  8549. }
  8550. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  8551. if ([paymentsAndCredits isEqualToString:@""]) {
  8552. paymentsAndCredits = @"";
  8553. } else {
  8554. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  8555. }
  8556. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  8557. if ([handling_fee_value isEqualToString:@""]) {
  8558. handling_fee_value = @"";
  8559. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  8560. handling_fee_value = @"";
  8561. } else {
  8562. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  8563. }
  8564. NSString *handling_fee_placeholder = handling_fee_value;
  8565. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  8566. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  8567. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  8568. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  8569. NSString *lift_gate_placeholder = @"";
  8570. if ([lift_gate_value isEqualToString:@""]) {
  8571. lift_gate_placeholder = @"";
  8572. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  8573. lift_gate_placeholder = @"";
  8574. } else {
  8575. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  8576. }
  8577. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  8578. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  8579. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  8580. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  8581. logistic_note = [logistic_note isEqualToString:@"USE MY CARRIER"] ? [NSString stringWithFormat:@"logistic_note = '%@\n\t%@',",logistic_note,logistic_note_text] : [NSString stringWithFormat:@"logistic_note = '%@',",logistic_note];
  8582. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  8583. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  8584. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  8585. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  8586. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  8587. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  8588. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  8589. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  8590. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  8591. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  8592. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  8593. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  8594. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  8595. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  8596. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  8597. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  8598. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  8599. BOOL number_nil = [credit_card_number isEqualToString:@""];
  8600. if (!number_nil) {
  8601. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  8602. }
  8603. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  8604. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  8605. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  8606. if (!security_code_nil) {
  8607. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  8608. }
  8609. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  8610. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  8611. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  8612. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  8613. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  8614. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  8615. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  8616. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  8617. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  8618. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  8619. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  8620. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  8621. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  8622. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  8623. NSString *contact_id = customer_cid;
  8624. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  8625. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  8626. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  8627. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  8628. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  8629. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  8630. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  8631. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  8632. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  8633. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  8634. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  8635. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  8636. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  8637. //
  8638. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  8639. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  8640. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  8641. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  8642. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  8643. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  8644. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  8645. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  8646. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  8647. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  8648. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  8649. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  8650. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  8651. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  8652. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  8653. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  8654. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  8655. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  8656. NSString *contactSql = [NSString stringWithFormat:@"update offline_contact set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ where contact_id = '%@'",contact_contact,contact_email,contact_phone,contact_fax,customer_name,customer_address1,customer_address2,customer_address3,customer_address4,customer_country,customer_state,customer_city,customer_zipcode,contact_id];
  8657. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  8658. if (receive_cid.length) {
  8659. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  8660. }
  8661. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  8662. if (receive_name.length) {
  8663. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  8664. }
  8665. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  8666. if (receive_ext.length) {
  8667. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  8668. }
  8669. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  8670. if (receive_contact.length) {
  8671. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  8672. }
  8673. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  8674. if (receive_phone.length) {
  8675. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  8676. }
  8677. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  8678. if (receive_email.length) {
  8679. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  8680. }
  8681. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  8682. if (receive_fax.length) {
  8683. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  8684. }
  8685. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  8686. if (sender_cid.length) {
  8687. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  8688. }
  8689. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  8690. if (sender_name.length) {
  8691. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  8692. }
  8693. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  8694. if (sender_ext.length) {
  8695. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  8696. }
  8697. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  8698. if(sender_contact.length) {
  8699. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  8700. }
  8701. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  8702. if (sender_phone.length) {
  8703. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  8704. }
  8705. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  8706. if (sender_fax.length) {
  8707. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  8708. }
  8709. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  8710. if (sender_email.length) {
  8711. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  8712. }
  8713. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  8714. if (shipping_billto_cid.length) {
  8715. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  8716. }
  8717. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  8718. if (shipping_billto_name.length) {
  8719. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  8720. }
  8721. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  8722. if (shipping_billto_ext.length) {
  8723. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  8724. }
  8725. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  8726. if (shipping_billto_contact.length) {
  8727. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  8728. }
  8729. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  8730. if (shipping_billto_phone.length) {
  8731. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  8732. }
  8733. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  8734. if (shipping_billto_fax.length) {
  8735. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  8736. }
  8737. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  8738. if (shipping_billto_email.length) {
  8739. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  8740. }
  8741. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  8742. if (billing_cid.length) {
  8743. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  8744. }
  8745. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  8746. if (billing_name.length) {
  8747. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  8748. }
  8749. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  8750. if (billing_ext.length) {
  8751. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  8752. }
  8753. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  8754. if (billing_contact.length) {
  8755. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  8756. }
  8757. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  8758. if (billing_phone.length) {
  8759. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  8760. }
  8761. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  8762. if (billing_fax.length) {
  8763. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  8764. }
  8765. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  8766. if (billing_email.length) {
  8767. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  8768. }
  8769. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  8770. if (returnto_cid.length) {
  8771. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  8772. }
  8773. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  8774. if (returnto_name.length) {
  8775. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  8776. }
  8777. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  8778. if (returnto_ext.length) {
  8779. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  8780. }
  8781. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  8782. if (returnto_contact.length) {
  8783. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  8784. }
  8785. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  8786. if (returnto_phone.length) {
  8787. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  8788. }
  8789. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  8790. if (returnto_fax.length) {
  8791. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  8792. }
  8793. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  8794. if (returnto_email.length) {
  8795. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  8796. }
  8797. NSString *order_status = @"status = 1,";
  8798. if (submit) {
  8799. order_status = @"status = -11,";
  8800. }
  8801. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  8802. NSString *sync_sql = @"";
  8803. if (submit) {
  8804. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  8805. [param setValue:sales_rep forKey:@"sales_rep"];
  8806. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  8807. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  8808. }
  8809. NSString *orderSql = [NSString stringWithFormat:@"update offline_order set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ so_id = '%@' %@ where so_id = '%@';",order_status,general_notes,internal_notes,poNumber,must_call,sign_picpath,total_price,paymentsAndCredits,handling_fee_placeholder,lift_gate,lift_gate_placeholder,logist,logistic_note,erpOrderStatus,paymentType,credit_card_address1,credit_card_address2,credit_card_city,credit_card_first_name,credit_card_last_name,credit_card_number,credit_card_security_code,credit_card_state,credit_card_type,credit_card_zipcode,credit_card_expiration_year,credit_card_expiration_month,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_email,receive_fax,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,so_id,sync_sql,so_id];
  8810. DebugLog(@"save order contactSql: %@",contactSql);
  8811. DebugLog(@"save order orderSql: %@",orderSql);
  8812. // int contact_ret = [iSalesDB execSql:contactSql];
  8813. int order_ret = [iSalesDB execSql:orderSql];
  8814. int ret = order_ret;
  8815. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8816. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8817. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8818. // [dic setValue:@"160409" forKey:@"min_ver"];
  8819. [dic setObject:so_id forKey:@"so#"];
  8820. return [RAUtils dict2data:dic];
  8821. }
  8822. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  8823. {
  8824. // id foo = nil;
  8825. // NSMutableArray *a = @[].mutableCopy;
  8826. // [a addObject:foo];
  8827. return [self saveorder:param submit:NO];
  8828. }
  8829. #pragma mark add to cart by name
  8830. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  8831. {
  8832. NSString *orderCode = [params objectForKey:@"orderCode"];
  8833. NSString *product_name = [params objectForKey:@"product_name"];
  8834. product_name = [self translateSingleQuote:product_name];
  8835. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  8836. sqlite3 *db = [iSalesDB get_db];
  8837. __block NSMutableString *product_id_string = [NSMutableString string];
  8838. for (int i = 0; i < product_name_array.count; i++) {
  8839. NSString *name = [product_name_array objectAtIndex:i];
  8840. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  8841. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8842. int product_id = sqlite3_column_int(stmt, 0);
  8843. if (i == product_name_array.count - 1) {
  8844. [product_id_string appendFormat:@"%d",product_id];
  8845. } else {
  8846. [product_id_string appendFormat:@"%d,",product_id];
  8847. }
  8848. }];
  8849. }
  8850. NSDictionary *newParams = @{
  8851. @"product_id" : product_id_string,
  8852. @"orderCode" : orderCode
  8853. };
  8854. [iSalesDB close_db:db];
  8855. return [self offline_add2cart:[newParams mutableCopy]];
  8856. }
  8857. #pragma mark reset order
  8858. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  8859. {
  8860. UIApplication * app = [UIApplication sharedApplication];
  8861. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  8862. appDelegate.disable_trigger=true;
  8863. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  8864. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  8865. appDelegate.disable_trigger=false;
  8866. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8867. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  8868. return [RAUtils dict2data:dic];
  8869. }
  8870. #pragma mark submit order
  8871. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  8872. {
  8873. return [self saveorder:params submit:YES];
  8874. }
  8875. #pragma mark copy order
  8876. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  8877. {
  8878. NSMutableDictionary *ret = @{}.mutableCopy;
  8879. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  8880. sqlite3 *db = [iSalesDB get_db];
  8881. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  8882. // 首先查看联系人是否active
  8883. NSString *customer_sql = [NSString stringWithFormat:@"select is_active from offline_contact where contact_id in (select customer_cid from offline_order where so_id = '%@');",order_code];
  8884. __block int customer_is_active = 1;
  8885. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8886. customer_is_active = sqlite3_column_int(stmt, 0);
  8887. }];
  8888. if (!customer_is_active) {
  8889. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8890. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8891. [iSalesDB close_db:db];
  8892. return [RAUtils dict2data:ret];
  8893. }
  8894. // new order
  8895. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  8896. NSString *new_order_code = [self get_offline_soid:db];
  8897. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  8898. user = [self translateSingleQuote:user];
  8899. NSString *insert_order_sql = [NSString stringWithFormat:@"insert into offline_order (so_id,status,submit_as,internal_notes,sales_rep,create_by,logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,must_call,poNumber,comments,comments_ext,sales_terms) select '%@',1,submit_as,internal_notes,sales_rep,'%@',logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,must_call,poNumber,comments,comments_ext,sales_terms from offline_order where so_id = '%@';",new_order_code,user,order_code]; // ,paymentsAndCredits,shipping,lift_gate_value,handling_fee_value
  8900. __block int result = 1;
  8901. result = [iSalesDB execSql:insert_order_sql db:db];
  8902. if (!result) {
  8903. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8904. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8905. [iSalesDB close_db:db];
  8906. return [RAUtils dict2data:ret];
  8907. }
  8908. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  8909. // __block NSString *product_id = @"";
  8910. __weak typeof(self) weakSelf = self;
  8911. NSString *product_id_sql = [NSString stringWithFormat:@"select product_id,is_active from product WHERE product_id in (select product_id from offline_cart where so_no = '%@');",order_code];
  8912. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8913. int is_active = sqlite3_column_int(stmt, 1);
  8914. if (is_active) {
  8915. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  8916. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  8917. NSString *insert_cart_sql = [NSString stringWithFormat:@"insert into offline_cart (so_no,orderitem_id,product_id,item_id,item_count,bundle_item,type) select '%@',orderitem_id,product_id,item_id,item_count,bundle_item,type from offline_cart where product_id = %@ and so_no = '%@'",new_order_code,product_id,order_code];
  8918. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  8919. }
  8920. }];
  8921. // product_id = [product_id substringFromIndex:1];
  8922. //
  8923. // [self offline_add2cart:@{}.mutableCopy];
  8924. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8925. [iSalesDB close_db:db];
  8926. if (result) {
  8927. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8928. } else {
  8929. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8930. }
  8931. [ret setObject:new_order_code forKey:@"so_id"];
  8932. return [RAUtils dict2data:ret];
  8933. }
  8934. #pragma mark move wish list to cart
  8935. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  8936. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  8937. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8938. NSString *collectId = params[@"collectId"];
  8939. NSString *sql = [NSString stringWithFormat:@"select w.product_id,w.qty,p.availability,w._id from wishlist as w left join product as p on w.product_id = p.product_id where w._id in (%@);",collectId];
  8940. __block NSString *product_id = @"";
  8941. __block NSString *qty = @"";
  8942. __block int number_of_outOfStock = 0;
  8943. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  8944. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8945. int productId = sqlite3_column_int(stmt, 0);
  8946. int item_qty = sqlite3_column_int(stmt, 1);
  8947. int availability = sqlite3_column_int(stmt, 2);
  8948. int _id = sqlite3_column_int(stmt, 3);
  8949. if (!appDelegate.can_create_backorder) {
  8950. // 库存小于购买量为缺货
  8951. if (availability >= item_qty) {
  8952. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  8953. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  8954. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  8955. } else {
  8956. number_of_outOfStock++;
  8957. }
  8958. } else {
  8959. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  8960. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  8961. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  8962. }
  8963. }];
  8964. NSMutableDictionary *retDic = nil;
  8965. if (!appDelegate.can_create_backorder) {
  8966. if (delete_collectId.count == 0) {
  8967. retDic = [NSMutableDictionary dictionary];
  8968. retDic[@"result"] = [NSNumber numberWithInteger:8];
  8969. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  8970. return [RAUtils dict2data:retDic];
  8971. }
  8972. }
  8973. if (product_id.length > 1) {
  8974. product_id = [product_id substringFromIndex:1];
  8975. }
  8976. if (qty.length > 1) {
  8977. qty = [qty substringFromIndex:1];
  8978. }
  8979. NSDictionary *newParams = @{
  8980. @"product_id" : product_id,
  8981. @"orderCode" : params[@"orderCode"],
  8982. @"qty" : qty
  8983. };
  8984. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  8985. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  8986. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  8987. NSInteger ret = [[[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] valueForKey:@"result"] integerValue];
  8988. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  8989. }
  8990. if (!appDelegate.can_create_backorder) {
  8991. if (number_of_outOfStock > 0) {
  8992. retDic[@"result"] = [NSNumber numberWithInteger:8];
  8993. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  8994. }
  8995. }
  8996. return [RAUtils dict2data:retDic];
  8997. }
  8998. #pragma mark - portfolio
  8999. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  9000. int sort = [[params valueForKey:@"sort"] intValue];
  9001. int offset = [[params valueForKey:@"offset"] intValue];
  9002. int limit = [[params valueForKey:@"limit"] intValue];
  9003. NSString *orderBy = @"";
  9004. switch (sort) {
  9005. case 0:{
  9006. orderBy = @"p.modify_time desc";
  9007. }
  9008. break;
  9009. case 1:{
  9010. orderBy = @"modify_time asc";
  9011. }
  9012. break;
  9013. case 2:{
  9014. orderBy = @"p.name asc";
  9015. }
  9016. break;
  9017. case 3:{
  9018. orderBy = @"p.name desc";
  9019. }
  9020. break;
  9021. case 4:{
  9022. orderBy = @"p.description asc";
  9023. }
  9024. break;
  9025. case 5: {
  9026. orderBy = @"m.default_category asc";
  9027. }
  9028. default:
  9029. break;
  9030. }
  9031. // NSString *sql = [NSString stringWithFormat:@"select p.product_id,p.name,p.description,ifnull(p.sheet_price,'null'),p.sheet_discount,ifnull(p.available_qty,'null'),p.percentage,p.item_id,p.fashion_id,i.url,p.line_note,p.percent from offline_portfolio as p left join model_image as i on i.product_id = p.product_id where i.type = 1 and (p.is_delete is null or p.is_delete = 0) group by p.product_id order by %@ limit %d offset %d;",orderBy,limit,offset];
  9032. NSString *sql = [NSString stringWithFormat:@"select * from (select p.product_id,p.name,p.description,ifnull(p.sheet_price,'null'),p.sheet_discount,ifnull(p.available_qty,'null'),p.percentage,p.item_id,p.fashion_id,i.url,p.line_note,p.percent,p.modify_time from offline_portfolio as p left join model_image as i on i.product_id = p.product_id where i.type = 1 and (p.is_delete is null or p.is_delete = 0) group by p.product_id limit %d offset %d) as p left join product as m on p.product_id = m.product_id order by %@;",limit,offset,orderBy];
  9033. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9034. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9035. sqlite3 *db = [iSalesDB get_db];
  9036. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9037. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9038. int product_id = sqlite3_column_int(stmt, 0);
  9039. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9040. NSString *name = [self textAtColumn:1 statement:stmt];
  9041. NSString *description = [self textAtColumn:2 statement:stmt];
  9042. double price = sqlite3_column_double(stmt, 3);
  9043. double discount = sqlite3_column_double(stmt,4);
  9044. int qty = sqlite3_column_int(stmt, 5);
  9045. int percentage = sqlite3_column_int(stmt, 6);
  9046. int item_id = sqlite3_column_int(stmt, 7);
  9047. // int fashion_id = sqlite3_column_int(stmt, 8);
  9048. NSString *img_path = [self textAtColumn:9 statement:stmt];
  9049. NSString *line_note = [self textAtColumn:10 statement:stmt];
  9050. double percent = sqlite3_column_double(stmt, 11);
  9051. NSString *price_null = [self textAtColumn:3 statement:stmt];
  9052. if ([price_null isEqualToString:@"null"]) {
  9053. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9054. }
  9055. NSMutableDictionary *item = @{
  9056. @"linenotes": line_note,
  9057. @"check": @(1),
  9058. @"product_id": product_id_string,
  9059. @"available_qty": @(qty),
  9060. @"available_percent" : @(percent),
  9061. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  9062. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9063. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  9064. @"img": img_path,
  9065. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  9066. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  9067. }.mutableCopy;
  9068. if (percentage) {
  9069. [item removeObjectForKey:@"available_qty"];
  9070. } else {
  9071. [item removeObjectForKey:@"available_percent"];
  9072. }
  9073. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  9074. if ([qty_null isEqualToString:@"null"]) {
  9075. [item removeObjectForKey:@"available_qty"];
  9076. }
  9077. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9078. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9079. }];
  9080. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9081. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9082. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9083. }
  9084. [iSalesDB close_db:db];
  9085. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  9086. [dic setValue:@"" forKey:@"email_content"];
  9087. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  9088. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9089. return [RAUtils dict2data:dic];
  9090. }
  9091. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  9092. __block int qty = 0;
  9093. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9094. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9095. qty = sqlite3_column_int(stmt, 0);
  9096. }];
  9097. return qty;
  9098. }
  9099. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  9100. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  9101. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  9102. NSMutableDictionary * values = params[@"replaceValue"];
  9103. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  9104. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  9105. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  9106. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  9107. NSString *pdf_path = @"";
  9108. if (direct) {
  9109. configureParams = [self valueInParams:params key:@"pdfUrl"];
  9110. } else {
  9111. pdf_path = [self valueInParams:params key:@"pdfPath"];
  9112. }
  9113. NSString *create_user = [self valueInParams:params key:@"user"];
  9114. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  9115. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  9116. // model info
  9117. // NSString *sql = [NSString stringWithFormat:@"select product_id,ifnull(sheet_price,'null'),ifnull(sheet_discount,'null'),ifnull(available_qty,'null'),percentage,item_id,line_note,percent from offline_portfolio where product_id in (%@);",product_ids];
  9118. // V1.90 more color
  9119. NSString *sql = [NSString stringWithFormat:@"select p.product_id,ifnull(p.sheet_price,'null'),ifnull(p.sheet_discount,'null'),ifnull(p.available_qty,'null'),p.percentage,p.item_id,p.line_note,p.percent,m.more_color from offline_portfolio as p left join product as m on p.product_id = m.product_id where p.product_id in (%@);",product_ids];
  9120. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9121. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  9122. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9123. sqlite3 *db = [iSalesDB get_db];
  9124. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9125. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9126. int product_id = sqlite3_column_int(stmt, 0);
  9127. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9128. double price = sqlite3_column_double(stmt, 1);
  9129. double discount = sqlite3_column_double(stmt,2);
  9130. int qty = sqlite3_column_int(stmt, 3);
  9131. int percentage = sqlite3_column_int(stmt, 4);
  9132. int item_id = sqlite3_column_int(stmt, 5);
  9133. NSString *line_note = [self textAtColumn:6 statement:stmt];
  9134. double percent = sqlite3_column_double(stmt, 7);
  9135. int more_color = sqlite3_column_int(stmt, 8);
  9136. NSString *price_null = [self textAtColumn:1 statement:stmt];
  9137. /* if ([price_null isEqualToString:@"null"]) {
  9138. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9139. }
  9140. */
  9141. [product_ids_string appendFormat:@"%@,",product_id_string];
  9142. // Regular Price
  9143. int regular_price = [[params objectForKey:@"regular_price"] integerValue];
  9144. NSString *regular_price_str = [self get_portfolio_price:appDelegate.contact_id item_id:item_id price:regular_price db:db];
  9145. // QTY
  9146. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  9147. if ([qty_null isEqualToString:@"null"]) {
  9148. // 查available
  9149. qty = [self model_QTY:product_id_string db:db];
  9150. }
  9151. if (percentage) {
  9152. qty = qty * percent / 100;
  9153. }
  9154. // Special Price
  9155. if ([price_null isEqualToString:@"null"]) {
  9156. // price = regular price
  9157. price = [regular_price_str doubleValue];
  9158. }
  9159. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  9160. if (![discount_null isEqualToString:@"null"]) {
  9161. price = price * (1 - discount / 100.0);
  9162. }
  9163. NSMutableDictionary *item = @{
  9164. @"line_note": line_note,
  9165. @"product_id": product_id_string,
  9166. @"available_qty": @(qty),
  9167. @"more_color":@(more_color),
  9168. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9169. @"regular_price" : regular_price_str,
  9170. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  9171. }.mutableCopy;
  9172. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9173. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9174. }];
  9175. [iSalesDB close_db:db];
  9176. if (product_ids_string.length > 0) {
  9177. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  9178. }
  9179. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  9180. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  9181. return [RAUtils dict2data:resultDictionary];
  9182. }
  9183. NSString *model_info = [RAUtils dict2string:dic];
  9184. // 创建PDF
  9185. // 在preview情况下保存,则不需要新建了
  9186. if (direct) {
  9187. NSMutableDictionary *tear_sheet_params = params;
  9188. // if (tear_sheet_id) {
  9189. // tear_sheet_params = values;
  9190. // }
  9191. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  9192. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  9193. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  9194. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  9195. resultDictionary = pdfInfo.mutableCopy;
  9196. } else { // 创建PDF失败
  9197. return pdfData;
  9198. }
  9199. } else {
  9200. // pdf_path 就是本地路径
  9201. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  9202. }
  9203. // 将文件移动到PDF Cache文件夹
  9204. NSString *newPath = [pdf_path lastPathComponent];
  9205. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9206. NSString *cachefolder = [paths objectAtIndex:0];
  9207. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9208. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  9209. NSFileManager *fileManager = [NSFileManager defaultManager];
  9210. NSError *error = nil;
  9211. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  9212. if (error) { // 移动文件失败
  9213. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9214. return [RAUtils dict2data:resultDictionary];
  9215. }
  9216. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  9217. pdf_path = [newPath lastPathComponent];
  9218. // 保存信息
  9219. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  9220. NSString *off_params = [RAUtils dict2string:params];
  9221. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  9222. pdf_path = [self translateSingleQuote:pdf_path];
  9223. create_user = [self translateSingleQuote:create_user];
  9224. tear_note = [self translateSingleQuote:tear_note];
  9225. tear_name = [self translateSingleQuote:tear_name];
  9226. model_info = [self translateSingleQuote:model_info];
  9227. configureParams = [self translateSingleQuote:configureParams];
  9228. off_params = [self translateSingleQuote:off_params];
  9229. NSString *save_pdf_sql = [NSString stringWithFormat:@"insert into offline_pdf (pdf_path,create_user,tear_note,tear_name,model_info,configureParams,is_local,off_params) values ('%@','%@','%@','%@','%@','%@',1,'%@');",pdf_path,create_user,tear_note,tear_name,model_info,configureParams,off_params];
  9230. if (tear_sheet_id) {
  9231. int _id = [tear_sheet_id integerValue];
  9232. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  9233. }
  9234. int result = [iSalesDB execSql:save_pdf_sql];
  9235. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9236. //
  9237. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  9238. if (remove_Item) {
  9239. // portfolioId
  9240. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  9241. }
  9242. return [RAUtils dict2data:resultDictionary];
  9243. }
  9244. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  9245. return [self offline_savePDF:params direct:YES];
  9246. }
  9247. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  9248. return [self offline_savePDF:params direct:NO];
  9249. }
  9250. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  9251. int offset = [[params valueForKey:@"offset"] integerValue];
  9252. int limit = [[params valueForKey:@"limit"] integerValue];
  9253. NSString *keyword = [params valueForKey:@"keyWord"];
  9254. NSString *where = @"where is_delete is null or is_delete = 0";
  9255. if (keyword.length) {
  9256. keyword = [self translateSingleQuote:keyword];
  9257. where = [NSString stringWithFormat:@"where (is_delete is null or is_delete = 0 ) and lower(create_user) like '%%%@%%' or lower(tear_note) like '%%%@%%' or lower(tear_name) like '%%%@%%'",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString];
  9258. }
  9259. NSString *sql = [NSString stringWithFormat:@"select tear_name,tear_note,createtime,create_user,pdf_path,_id,model_info,off_params from offline_pdf %@ order by createtime desc limit %d offset %d;",where,limit,offset];// tearsheets_id ---> _id
  9260. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9261. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9262. NSString *cachefolder = [paths objectAtIndex:0];
  9263. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9264. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9265. NSString *name = [self textAtColumn:0 statement:stmt];
  9266. NSString *note = [self textAtColumn:1 statement:stmt];
  9267. NSString *time = [self textAtColumn:2 statement:stmt];
  9268. NSString *user = [self textAtColumn:3 statement:stmt];
  9269. NSString *path = [self textAtColumn:4 statement:stmt];
  9270. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9271. NSString *off_params = [self textAtColumn:7 statement:stmt];
  9272. path = [pdfFolder stringByAppendingPathComponent:path];
  9273. BOOL bdir=NO;
  9274. NSFileManager* fileManager = [NSFileManager defaultManager];
  9275. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  9276. {
  9277. //pdf文件不存在
  9278. path=nil;
  9279. }
  9280. time = [self changeDateTimeFormate:time];
  9281. time = [time stringByAppendingString:@" PST"];
  9282. int sheet_id = sqlite3_column_int(stmt, 5);
  9283. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  9284. item[@"tearsheetsId"]=@(sheet_id);
  9285. item[@"pdf_path"]=path;
  9286. item[@"create_time"]=time;
  9287. item[@"create_user"]=user;
  9288. item[@"tear_note"]=note;
  9289. item[@"tear_name"]=name;
  9290. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  9291. item[@"model_info"]=model_info;
  9292. item[@"off_params"]=off_params;
  9293. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9294. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9295. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9296. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9297. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9298. }];
  9299. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9300. return [RAUtils dict2data:dic];
  9301. }
  9302. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  9303. NSString *prodct_ids = [params objectForKey:@"product_id"];
  9304. // NSString *user = [params objectForKey:@"user"];
  9305. // NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and (select count(0) from offline_portfolio where product_id in (%@) and (is_delete is null or is_delete = 0)) = 0);",prodct_ids,prodct_ids];
  9306. NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and p.product_id not in (select product_id from offline_portfolio));insert into offline_portfolio (product_id,name,description,item_id,is_delete) select * from (select p.product_id,p.name,p.description,p.item_id,0 from product as p left join offline_portfolio as f on p.product_id = f.product_id where p.product_id in (%@) and p.product_id in (select product_id from offline_portfolio) and f.is_delete = 1);delete from offline_portfolio where (select count(0) from offline_portfolio where product_id in (%@)) > 1 and is_delete = 1;",prodct_ids,prodct_ids,prodct_ids];
  9307. int result = [iSalesDB execSql:sql];
  9308. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9309. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  9310. if (result == RESULT_TRUE) {
  9311. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  9312. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  9313. sqlite3 *db = [iSalesDB get_db];
  9314. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  9315. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  9316. [iSalesDB close_db:db];
  9317. }
  9318. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9319. return [RAUtils dict2data:dic];
  9320. }
  9321. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  9322. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  9323. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  9324. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  9325. // Regurlar Price
  9326. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  9327. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  9328. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9329. __block long val_count = 0;
  9330. NSString *sql = @"select name,type,order_by from price order by order_by";
  9331. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9332. NSString *name = [self textAtColumn:0 statement:stmt];
  9333. int type = sqlite3_column_int(stmt, 1);
  9334. int order_by = sqlite3_column_int(stmt, 2);
  9335. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9336. NSDictionary *price_dic = @{
  9337. @"value" : name,
  9338. @"value_id" : [NSNumber numberWithInteger:type],
  9339. @"check" : order_by == 0 ? @(1) : @(0)
  9340. };
  9341. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9342. val_count = ++(*count);
  9343. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9344. val_count = 0;
  9345. }];
  9346. [cadedate setObject:@{@"value" : @"None",
  9347. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9348. val_count++;
  9349. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  9350. [price setObject:cadedate forKey:@"cadedate"];
  9351. [section1 setObject:price forKey:@"item_2"];
  9352. [dic setObject:section1 forKey:@"section_1"];
  9353. return [RAUtils dict2data:dic];
  9354. }
  9355. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  9356. NSString *product_id = [params objectForKey:@"fashionId"];
  9357. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9358. __block int result = RESULT_TRUE;
  9359. __block int qty = 0;
  9360. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9361. qty = sqlite3_column_int(stmt, 0);
  9362. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9363. result = RESULT_FALSE;
  9364. }];
  9365. NSMutableDictionary *dic = @{
  9366. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  9367. @"mode" : @"Regular Mode",
  9368. @"quantity_available" : @(qty),
  9369. @"result" : @(result),
  9370. }.mutableCopy;
  9371. return [RAUtils dict2data:dic];
  9372. }
  9373. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  9374. NSString *item_ids = [params objectForKey:@"item_id"];
  9375. NSString *line_notes = [params objectForKey:@"notes"];
  9376. NSString *price_str = [params objectForKey:@"price"];
  9377. NSString *discount_str = [params objectForKey:@"discount"];
  9378. NSString *percent = [params objectForKey:@"available_percent"];
  9379. NSString *qty = [params objectForKey:@"available_qty"];
  9380. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  9381. int dot = 0;
  9382. if (line_notes) {
  9383. line_notes = [self translateSingleQuote:line_notes];
  9384. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  9385. sql = [sql stringByAppendingString:line_notes];
  9386. dot = 1;
  9387. } else {
  9388. line_notes = @"";
  9389. }
  9390. if (price_str) {
  9391. if (dot) {
  9392. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  9393. } else {
  9394. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  9395. dot = 1;
  9396. }
  9397. sql = [sql stringByAppendingString:price_str];
  9398. } else {
  9399. price_str = @"";
  9400. }
  9401. if (discount_str) {
  9402. if (dot) {
  9403. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  9404. } else {
  9405. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  9406. dot = 1;
  9407. }
  9408. sql = [sql stringByAppendingString:discount_str];
  9409. } else {
  9410. discount_str = @"";
  9411. }
  9412. if (percent) {
  9413. if (dot) {
  9414. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  9415. } else {
  9416. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  9417. dot = 1;
  9418. }
  9419. sql = [sql stringByAppendingString:percent];
  9420. } else {
  9421. percent = @"";
  9422. }
  9423. if (qty) {
  9424. if (dot) {
  9425. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  9426. } else {
  9427. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  9428. dot = 1;
  9429. }
  9430. sql = [sql stringByAppendingString:qty];
  9431. } else {
  9432. qty = @"";
  9433. }
  9434. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  9435. sql = [sql stringByAppendingString:where];
  9436. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9437. int result = [iSalesDB execSql:sql];
  9438. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9439. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9440. return [RAUtils dict2data:dic];
  9441. }
  9442. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  9443. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  9444. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9445. sqlite3 *db = [iSalesDB get_db];
  9446. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set is_delete = 1 where item_id in (%@) and (is_delete is null or is_delete = 0);",portfolio_ids];
  9447. int result = [iSalesDB execSql:sql db:db];
  9448. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9449. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9450. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  9451. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  9452. [iSalesDB close_db:db];
  9453. return [RAUtils dict2data:dic];
  9454. }
  9455. + (void)offline_removePDFWithName:(NSString *)name {
  9456. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9457. NSString *cachefolder = [paths objectAtIndex:0];
  9458. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9459. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  9460. NSFileManager *fileManager = [NSFileManager defaultManager];
  9461. [fileManager removeItemAtPath:path error:nil];
  9462. }
  9463. + (void)offline_clear_PDFCache {
  9464. NSFileManager *fileManager = [NSFileManager defaultManager];
  9465. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9466. NSString *cachefolder = [paths objectAtIndex:0];
  9467. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9468. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  9469. for (NSString *path in pdf_files) {
  9470. [self offline_removePDFWithName:[path lastPathComponent]];
  9471. }
  9472. }
  9473. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  9474. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9475. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  9476. NSString *user = [params objectForKey:@"user"];
  9477. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %d;",tearsheetsId]]; // tearsheets_id ---> _id
  9478. if (![create_user isEqualToString:user]) {
  9479. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9480. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9481. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  9482. return [RAUtils dict2data:dic];
  9483. }
  9484. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  9485. __block int is_local = 0;
  9486. __block NSString *path = @"";
  9487. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9488. is_local = sqlite3_column_int(stmt, 0);
  9489. path = [self textAtColumn:1 statement:stmt];
  9490. }];
  9491. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  9492. if (is_local == 1) {
  9493. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  9494. // 删除文件
  9495. [self offline_removePDFWithName:path];
  9496. }
  9497. int result = [iSalesDB execSql:sql];
  9498. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9499. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9500. return [RAUtils dict2data:dic];
  9501. }
  9502. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  9503. {
  9504. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9505. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9506. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9507. NSString* where=@"1=1";
  9508. if (ver!=nil) {
  9509. where=@"is_dirty=1";
  9510. }
  9511. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,name,description,item_id,fashion_id,ifnull(available_qty,'null'),ifnull(percentage,'null'),ifnull(percent,'null'),ifnull(sheet_price,'null'),sheet_discount,img,line_note,is_delete,createtime,modify_time from offline_portfolio where %@",where];
  9512. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9513. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9514. sqlite3 *db = [iSalesDB get_db];
  9515. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9516. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9517. NSInteger _id = sqlite3_column_int(stmt, 0);
  9518. NSInteger product_id = sqlite3_column_int(stmt, 1);
  9519. NSString *name = [self textAtColumn:2 statement:stmt];
  9520. NSString *desc = [self textAtColumn:3 statement:stmt];
  9521. NSInteger item_id = sqlite3_column_int(stmt, 4);
  9522. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  9523. NSInteger qty = sqlite3_column_int(stmt, 6);
  9524. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  9525. double percent = sqlite3_column_double(stmt, 8);
  9526. double price = sqlite3_column_double(stmt, 9);
  9527. double discount = sqlite3_column_double(stmt, 10);
  9528. NSString *img = [self textAtColumn:11 statement:stmt];
  9529. NSString *line_note = [self textAtColumn:12 statement:stmt];
  9530. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  9531. NSString *create_time = [self textAtColumn:14 statement:stmt];
  9532. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  9533. NSString *price_null = [self textAtColumn:9 statement:stmt];
  9534. if ([price_null isEqualToString:@"null"]) {
  9535. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9536. }
  9537. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  9538. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  9539. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  9540. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9541. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  9542. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  9543. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  9544. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  9545. if ([qty_null isEqualToString:@"null"]) {
  9546. // [item setValue:@"null" forKey:@"available_qty"];
  9547. } else {
  9548. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  9549. }
  9550. if ([is_percent_null isEqualToString:@"null"]) {
  9551. // [item setValue:@"null" forKey:@"percentage"];
  9552. } else {
  9553. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  9554. }
  9555. if ([percent_null isEqualToString:@"null"]) {
  9556. // [item setValue:@"null" forKey:@"percent"];
  9557. } else {
  9558. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  9559. }
  9560. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  9561. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  9562. [item setValue:name forKey:@"name"];
  9563. [item setValue:desc forKey:@"description"];
  9564. [item setValue:img forKey:@"img"];
  9565. [item setValue:line_note forKey:@"line_note"];
  9566. [item setValue:create_time forKey:@"createtime"];
  9567. [item setValue:modify_time forKey:@"modifytime"];
  9568. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9569. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9570. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9571. }];
  9572. [iSalesDB close_db:db];
  9573. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9574. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9575. }
  9576. return ret;
  9577. }
  9578. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  9579. {
  9580. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9581. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9582. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9583. NSString* where=@"1=1";
  9584. if (ver!=nil) {
  9585. where=@"is_dirty=1";
  9586. }
  9587. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,ifnull(tearsheets_id,'null'),pdf_path,create_user,tear_note,tear_name,model_info,createtime,modify_time,configureParams,off_params,is_delete,is_local from offline_pdf where %@",where];
  9588. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9589. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9590. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9591. NSInteger _id = sqlite3_column_int(stmt, 0);
  9592. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  9593. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  9594. NSString *create_user = [self textAtColumn:3 statement:stmt];
  9595. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  9596. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  9597. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9598. NSString *createtime = [self textAtColumn:7 statement:stmt];
  9599. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  9600. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  9601. NSString *off_params = [self textAtColumn:10 statement:stmt];
  9602. NSString *uuid = [NSUUID UUID].UUIDString;
  9603. int is_delete = sqlite3_column_int(stmt, 11);
  9604. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  9605. int is_local = sqlite3_column_int(stmt, 12);
  9606. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9607. if (![tearsheet_id_null isEqualToString:@"null"]) {
  9608. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  9609. }
  9610. [item setObject:pdf_path forKey:@"pdf_path"];
  9611. [item setObject:create_user forKey:@"create_user"];
  9612. [item setObject:tear_note forKey:@"tear_note"];
  9613. [item setObject:tear_name forKey:@"tear_name"];
  9614. [item setObject:model_info forKey:@"model_info"];
  9615. [item setObject:createtime forKey:@"createtime"];
  9616. [item setObject:modifytime forKey:@"modifytime"];
  9617. [item setObject:urlParams forKey:@"urlParams"];
  9618. [item setObject:off_params forKey:@"off_params"];
  9619. [item setObject:uuid forKey:@"pdf_token"];
  9620. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  9621. NSDictionary *off_Params_dic = [RAUtils string2dict:off_params];
  9622. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  9623. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  9624. } else {
  9625. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9626. }
  9627. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9628. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9629. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9630. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9631. }];
  9632. return ret;
  9633. }
  9634. @end