OLDataProvider.m 562 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112
  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. #import "RAConvertor.h"
  23. @interface OLDataProvider ()
  24. @end
  25. @implementation OLDataProvider
  26. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  27. {
  28. 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];
  29. NSString* ret=@"No Price.";
  30. sqlite3_stmt * statement;
  31. // int count=0;
  32. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  33. {
  34. if (sqlite3_step(statement) == SQLITE_ROW)
  35. {
  36. char *price = (char*)sqlite3_column_text(statement, 0);
  37. if(price==nil)
  38. price="";
  39. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  40. double dp= [nsprice doubleValue];
  41. ret=[NSString stringWithFormat:@"%.2f",dp];
  42. }
  43. sqlite3_finalize(statement);
  44. }
  45. return ret;
  46. // // [iSalesDB close_db:db];
  47. //
  48. // return nil;
  49. //// if(dprice==DBL_MAX)
  50. //// ret= nil;
  51. //// else
  52. //// ret= [NSNumber numberWithDouble:dprice];
  53. //// return ret;
  54. }
  55. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  56. {
  57. NSString* ret= nil;
  58. NSString *sqlQuery = nil;
  59. // 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
  60. 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];
  61. sqlite3_stmt * statement;
  62. // int count=0;
  63. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  64. {
  65. if (sqlite3_step(statement) == SQLITE_ROW)
  66. {
  67. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  68. if(imgurl==nil)
  69. imgurl="";
  70. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  71. ret=nsimgurl;
  72. }
  73. sqlite3_finalize(statement);
  74. }
  75. else
  76. {
  77. ret=@"";
  78. }
  79. // [iSalesDB close_db:db];
  80. // DebugLog(@"data string: %@",ret );
  81. return ret;
  82. }
  83. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  84. {
  85. NSMutableDictionary* values=params[@"replaceValue"]; // 离线下发的参数
  86. //生成portfolio pdf需要的数据
  87. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  88. data[@"npd_url"]=COMPANY_WEB;
  89. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  90. [formatter setDateFormat:@"MMMM yyyy"];
  91. NSString* date = [formatter stringFromDate:[NSDate date]];
  92. data[@"create_month"]=date;
  93. data[@"company_name"]=COMPANY_FULL_NAME;
  94. data[@"catalog_name"]=params[@"catalog_name"];
  95. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  96. data[@"TOTAL_PAGE"]=0;
  97. data[@"CURRENT_PAGE"]=0;
  98. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  99. BOOL show_group_by = [[params objectForKey:@"show_group_by"] boolValue];
  100. bool show_stockout=[params[@"show_stockout"] boolValue];
  101. NSString *sql = nil;
  102. // v1.90 more color
  103. NSString *group_by = @"";
  104. if(values==nil)
  105. {
  106. if (show_group_by) {
  107. group_by = @"group by b.product_group ";
  108. }
  109. 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"],group_by];
  110. }
  111. else
  112. {
  113. if (show_group_by) {
  114. group_by = @"group by product_group ";
  115. }
  116. 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"],group_by];
  117. }
  118. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  119. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  120. sqlite3 *db = [iSalesDB get_db];
  121. // NSDictionary *resultDic =
  122. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  123. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  124. int product_id = sqlite3_column_int(stmt, 0);
  125. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  126. NSString *name = [self textAtColumn:1 statement:stmt];
  127. NSString *description = [self textAtColumn:2 statement:stmt];
  128. double price = sqlite3_column_double(stmt, 3);
  129. NSString *s_price = [self textAtColumn:3 statement:stmt];
  130. double discount = sqlite3_column_double(stmt,4);
  131. int qty = sqlite3_column_int(stmt, 5);
  132. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  133. int is_percentage = sqlite3_column_int(stmt, 6);
  134. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  135. int item_id = sqlite3_column_int(stmt, 7);
  136. // int fashion_id = sqlite3_column_int(stmt, 8);
  137. NSString *line_note = [self textAtColumn:9 statement:stmt];
  138. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  139. double percent = sqlite3_column_double(stmt, 10);
  140. // int portfolio_id = sqlite3_column_int(stmt, 11);
  141. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  142. int availability = sqlite3_column_int(stmt, 12);
  143. NSString *color = [self textAtColumn:13 statement:stmt];
  144. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  145. NSString *demension = [self textAtColumn:15 statement:stmt];
  146. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  147. NSString *material = [self textAtColumn:17 statement:stmt];
  148. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  149. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  150. double volume = sqlite3_column_double(stmt, 19);
  151. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  152. double weight = sqlite3_column_double(stmt, 20);
  153. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  154. int model_set = sqlite3_column_int(stmt, 21);
  155. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  156. double load_ability = sqlite3_column_double(stmt, 22);
  157. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  158. NSString *assembling = [self textAtColumn:24 statement:stmt];
  159. NSString *made_in = [self textAtColumn:25 statement:stmt];
  160. NSInteger more_color_int = sqlite3_column_int(stmt, 26);
  161. NSString* gprice = [self get_portfolio_price :appDelegate.contact_id item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  162. float bprice=0;
  163. NSDictionary* bundle=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  164. for(int b=0;b< [bundle[@"count"] intValue];b++)
  165. {
  166. NSDictionary* bi=bundle[[NSString stringWithFormat:@"item_%d",b]];
  167. bprice+= [bi[@"modulus"] intValue] * [bi[@"unit_price"] floatValue];
  168. }
  169. if(gprice!=nil)
  170. gprice = [NSString stringWithFormat:@"%.2f",[gprice floatValue]+bprice];
  171. if(s_percent.length==0||is_percentage==0)
  172. {
  173. percent=100.0;
  174. if([s_qty isEqualToString:@"null"])
  175. qty=availability;
  176. }
  177. else
  178. {
  179. qty=availability;
  180. }
  181. qty=qty*percent/100+0.5;
  182. for(int i=0;i<[values[@"count"] intValue];i++)
  183. {
  184. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  185. if([valueitem[@"product_id"] intValue]==product_id)
  186. {
  187. line_note=valueitem[@"line_note"];
  188. qty=[valueitem[@"available_qty"] intValue];
  189. if([[valueitem allKeys] containsObject:@"available_qty"])
  190. s_qty=[NSString stringWithFormat:@"%d",qty];
  191. else
  192. s_qty=@"null";
  193. if(valueitem[@"regular_price"]!=nil)
  194. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  195. s_price=valueitem[@"special_price"];
  196. price=[valueitem[@"special_price"] floatValue];
  197. discount=0;
  198. // v1.90
  199. more_color_int = [[valueitem objectForKey:@"more_color"] integerValue];
  200. }
  201. }
  202. if(is_percentage==0&&qty==0&&!show_stockout)
  203. {
  204. return;
  205. }
  206. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  207. {
  208. return;
  209. }
  210. NSString* set_price=@"";
  211. if([params[@"entered_price"] boolValue])
  212. {
  213. if (s_price==nil ) {
  214. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  215. }
  216. else
  217. {
  218. if(price*(1-discount/100.0)!=gprice.floatValue)
  219. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  220. }
  221. }
  222. NSString* get_price=@"";
  223. {
  224. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  225. // DebugLog(@"price time interval");
  226. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  227. if(gprice==nil)
  228. get_price=@"Price:No Price.";
  229. else
  230. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  231. }
  232. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  233. // if ([qty_null isEqualToString:@"null"]) {
  234. // qty=availability;
  235. // }
  236. //
  237. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  238. // if ([qty_percent_null isEqualToString:@"null"]) {
  239. // percentage=1;
  240. // }
  241. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  242. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  243. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  244. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  245. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  246. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  247. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  248. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  249. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  250. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  251. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  252. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  253. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  254. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  255. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  256. if([params[@"availability"] boolValue]==false)
  257. str_availability=@"";
  258. else
  259. {
  260. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  261. str_availability=@"<b>Availability:</b> In Production";
  262. }
  263. if([params[@"color"] boolValue]==false || color.length==0 )
  264. str_color=@"";
  265. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  266. str_model_set=@"";
  267. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  268. str_legcolor=@"";
  269. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  270. str_demension=@"";
  271. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  272. str_seat_height=@"";
  273. if([params[@"material"] boolValue]==false || material.length==0 )
  274. str_meterial=@"";
  275. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  276. str_box_dim=@"";
  277. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  278. str_volume=@"";
  279. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  280. str_weight=@"";
  281. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  282. str_load_ability=@"";
  283. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  284. str_fabric_content=@"";
  285. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  286. str_assembling=@"";
  287. if([params[@"made"] boolValue]==false || made_in.length==0 )
  288. str_made_in=@"";
  289. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  290. str_line_note=@"";
  291. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  292. [arr_detail addObject:str_availability];
  293. [arr_detail addObject:str_color];
  294. [arr_detail addObject:str_model_set];
  295. [arr_detail addObject:str_legcolor];
  296. [arr_detail addObject:str_demension];
  297. [arr_detail addObject:str_seat_height];
  298. [arr_detail addObject:str_meterial];
  299. [arr_detail addObject:str_box_dim];
  300. [arr_detail addObject:str_volume];
  301. [arr_detail addObject:str_weight];
  302. [arr_detail addObject:str_load_ability];
  303. [arr_detail addObject:str_fabric_content];
  304. [arr_detail addObject:str_assembling];
  305. [arr_detail addObject:str_made_in];
  306. [arr_detail addObject:str_line_note];
  307. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  308. NSString* detail =[RAConvertor arr2string:arr_detail separator:@" " trim:true];
  309. //model image;
  310. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  311. // qr image
  312. NSString* qrpath=nil;
  313. if([params[@"show_barcode"] boolValue])
  314. {
  315. NSString* temp = NSTemporaryDirectory();
  316. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  317. qrpath=[temp stringByAppendingPathComponent:filename];
  318. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  319. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  320. }
  321. NSString *more_color_path = [[NSBundle mainBundle] pathForResource:@"more_color_64" ofType:@"png"];
  322. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  323. item[@"img"]=img_path;
  324. item[@"img_qr"]=qrpath;
  325. item[@"special_price"]=set_price;
  326. item[@"price"]=get_price;
  327. item[@"name"]=name;
  328. item[@"description"]=description;
  329. item[@"detail"]=detail;
  330. item[@"img"]=img_path;
  331. if (show_group_by && more_color_int != 0) {
  332. item[@"more_color"] = more_color_path;
  333. }
  334. // @{
  335. // //@"linenotes": line_note,
  336. //// @"product_id": product_id_string,
  337. //// @"available_qty": @(qty),
  338. //// @"available_percent" : @(percent),
  339. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  340. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  341. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  342. // @"": ,
  343. // @"": ,
  344. // @"": ,
  345. // @"": ,
  346. // @"": ,
  347. // @"": ,
  348. // @"":
  349. // }.mutableCopy;
  350. // if (percentage) {
  351. // [item removeObjectForKey:@"available_qty"];
  352. // } else {
  353. // [item removeObjectForKey:@"available_percent"];
  354. // }
  355. //
  356. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  357. // if ([qty_null isEqualToString:@"null"]) {
  358. // [item removeObjectForKey:@"available_qty"];
  359. // }
  360. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  361. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  362. }];
  363. [iSalesDB close_db:db];
  364. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  365. // [dic setValue:@"" forKey:@"email_content"];
  366. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  367. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  368. //
  369. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  370. // cell[@"count"]=[NSNumber numberWithInt:10];
  371. grid[@"cell0"]=dic;
  372. data[@"grid0"]=grid;
  373. return data;
  374. }
  375. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  376. {
  377. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  378. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  379. NSString *cache_folder=[paths objectAtIndex:0];
  380. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  381. BOOL bdir=NO;
  382. NSFileManager* fileManager = [NSFileManager defaultManager];
  383. if(PDF_DEBUG)
  384. {
  385. NSData *data = [NSData dataWithContentsOfFile:default_path];
  386. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  387. return ret;
  388. }
  389. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  390. {
  391. NSError * error=nil;
  392. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  393. {
  394. return nil;
  395. }
  396. }
  397. NSData *data = [NSData dataWithContentsOfFile:template_path];
  398. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  399. return ret;
  400. }
  401. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  402. {
  403. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  404. 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 ];
  405. DebugLog(@"offline_login sql:%@",sqlQuery);
  406. sqlite3_stmt * statement;
  407. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  408. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  409. {
  410. if (sqlite3_step(statement) == SQLITE_ROW)
  411. {
  412. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  413. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  414. // int can_show_price = sqlite3_column_int(statement, 0);
  415. // int can_see_price = sqlite3_column_int(statement, 1);
  416. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  417. // if(contact_id==nil)
  418. // contact_id="";
  419. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  420. // int user_type = sqlite3_column_int(statement, 3);
  421. // int can_cancel_order = sqlite3_column_int(statement, 4);
  422. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  423. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  424. // int can_delete_order = sqlite3_column_int(statement, 7);
  425. // int can_submit_order = sqlite3_column_int(statement, 8);
  426. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  427. // int can_create_order = sqlite3_column_int(statement, 10);
  428. // char *mode = (char*)sqlite3_column_text(statement, 11);
  429. // if(mode==nil)
  430. // mode="";
  431. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  432. // char *username = (char*)sqlite3_column_text(statement, 12);
  433. // if(username==nil)
  434. // username="";
  435. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  436. // int can_update_contact_info = sqlite3_column_int(statement, 13);
  437. char *first_name = (char*)sqlite3_column_text(statement, 14);
  438. if(first_name==nil)
  439. first_name="";
  440. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  441. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  442. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  443. // [header setValue:nscontact_id forKey:@"contact_id"];
  444. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  445. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  446. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  447. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  448. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  449. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  450. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  451. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  452. //
  453. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  454. //
  455. // [header setValue:nsusername forKey:@"username"];
  456. //
  457. // NSError* error=nil;
  458. //
  459. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  460. // [header setValue:statusFilter forKey:@"statusFilter"];
  461. //
  462. //
  463. //
  464. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  465. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  466. //
  467. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  468. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  469. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  470. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  471. //
  472. // [ret setObject:header forKey:@"header"];
  473. [ret setValue:nsfirst_name forKey:@"first_name"];
  474. }
  475. sqlite3_finalize(statement);
  476. }
  477. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  478. return ret;
  479. }
  480. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code type:(int) type
  481. {
  482. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  483. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  484. data[@"npd_url"]=COMPANY_WEB;
  485. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  486. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  487. NSString* date = [formatter stringFromDate:[NSDate date]];
  488. data[@"print_date"]=date;
  489. data[@"company_name"]=COMPANY_FULL_NAME;
  490. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  491. data[@"TOTAL_PAGE"]=0;
  492. data[@"CURRENT_PAGE"]=0;
  493. // [formatter setDateFormat:@"MM/dd/yyyy"];
  494. // date = [formatter stringFromDate:[NSDate date]];
  495. // data[@"creat_date"]=date;
  496. NSString* temp = NSTemporaryDirectory();
  497. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  498. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  499. UIImage* qrimg=[ImageUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  500. // bool bwrite=
  501. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  502. data[@"img_barcode"]=qrpath;
  503. data[@"so_id"]=order_code;
  504. data[@"company"]=COMPANY_FULL_NAME;
  505. // 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];
  506. 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];
  507. sqlite3 *db = [iSalesDB get_db];
  508. data[@"print_by"]= [self query_login:appDelegate.user db:db][@"first_name"];
  509. sqlite3_stmt * statement;
  510. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  511. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  512. row0[@"count"]=[NSNumber numberWithInt:1];
  513. datasource[@"row0"]=row0;
  514. NSString* print_status=@"";
  515. NSString* order_type=@"Offline Quote";
  516. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  517. {
  518. if (sqlite3_step(statement) == SQLITE_ROW)
  519. {
  520. int erpOrderStatus = sqlite3_column_int(statement, 0);
  521. int status = sqlite3_column_int(statement, 26);
  522. if(status==2)
  523. {
  524. switch (erpOrderStatus) {
  525. case 10:
  526. // order_type=@"Quoted Order";
  527. print_status=@"Quote";
  528. break;
  529. case 11:
  530. print_status=@"Booked";
  531. break;
  532. case 12:
  533. print_status=@"Picked";
  534. break;
  535. case 13:
  536. print_status=@"Shipped";
  537. break;
  538. case 14:
  539. print_status=@"Closed";
  540. break;
  541. case 15:
  542. print_status=@"Canceled";
  543. break;
  544. case 16:
  545. print_status=@"All";
  546. break;
  547. case 18:
  548. print_status=@"Processing";
  549. break;
  550. default:
  551. break;
  552. }
  553. }
  554. else{
  555. switch (status) {
  556. case 0:
  557. {
  558. print_status=@"Quote";
  559. break;
  560. }
  561. case 1:
  562. {
  563. print_status=@"Saved";
  564. break;
  565. }
  566. case 3:
  567. {
  568. //canceled
  569. print_status=@"Canceled";
  570. break;
  571. }
  572. default:
  573. break;
  574. }
  575. }
  576. NSString* sender_name = [self textAtColumn:1 statement:statement];
  577. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  578. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  579. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  580. NSString* sender_email = [self textAtColumn:5 statement:statement];
  581. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  582. NSString* billing_name = [self textAtColumn:7 statement:statement];
  583. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  584. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  585. if(billing_phone.length>0)
  586. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  587. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  588. if(billing_fax.length>0)
  589. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  590. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  591. [billto_arr0 addObject:billing_phone];
  592. [billto_arr0 addObject:billing_fax];
  593. NSString* billing_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  594. NSString* billing_email = [self textAtColumn:11 statement:statement];
  595. if(billing_email.length>0)
  596. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  597. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  598. if(billing_contact.length>0)
  599. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  600. NSString* receive_name = [self textAtColumn:12 statement:statement];
  601. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  602. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  603. if(receive_phone.length>0)
  604. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  605. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  606. if(receive_fax.length>0)
  607. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  608. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  609. [receive_arr0 addObject:receive_phone];
  610. [receive_arr0 addObject:receive_fax];
  611. NSString* receive_email = [self textAtColumn:16 statement:statement];
  612. NSString* receive_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  613. if(receive_email.length>0)
  614. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  615. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  616. if(receive_contact.length>0)
  617. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  618. NSString* poNumber = [self textAtColumn:17 statement:statement];
  619. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  620. NSString* carrier = [self textAtColumn:19 statement:statement];
  621. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  622. NSString* terms = [self textAtColumn:21 statement:statement];
  623. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  624. NSString* port_destination = [self textAtColumn:23 statement:statement];
  625. NSString* etd = [self textAtColumn:24 statement:statement];
  626. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  627. if(sign_picpath.length==0)
  628. sign_picpath=nil;
  629. NSString* create_date = [self textAtColumn:27 statement:statement];
  630. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  631. NSString* general_notes=[self textAtColumn:33 statement:statement];
  632. NSString *customerID = [self textAtColumn:34 statement:statement];
  633. if (customerID.length == 0) {
  634. customerID = nil;
  635. }
  636. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  637. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  638. if(general_notes.length>0)
  639. {
  640. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  641. row2[@"item_0"]=@{@"g_notes":general_notes };
  642. row2[@"count"]=[NSNumber numberWithInt:1];
  643. datasource[@"row2"]=row2;
  644. }
  645. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  646. [billto_arr addObject:billing_ext];
  647. [billto_arr addObject:billing_pf];
  648. [billto_arr addObject:billing_contact];
  649. [billto_arr addObject:billing_email];
  650. NSString* billto=[RAConvertor arr2string:billto_arr separator:@"\r\n" trim:true];
  651. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  652. [receive_arr addObject:receive_ext];
  653. [receive_arr addObject:receive_pf];
  654. [receive_arr addObject:receive_contact];
  655. [receive_arr addObject:receive_email];
  656. NSString* receive=[RAConvertor arr2string:receive_arr separator:@"\r\n" trim:true];
  657. float shipping = sqlite3_column_double(statement, 28);
  658. float lift_gate_value = sqlite3_column_double(statement, 29);
  659. float handling_fee_value = sqlite3_column_double(statement, 30);
  660. data[@"company"]=sender_name;
  661. data[@"shipfrom_addr"]=sender_ext;
  662. data[@"shipfrom_phone"]=sender_phone;
  663. data[@"shipfrom_fax"]=sender_fax;
  664. data[@"shipfrom_email"]=sender_email;
  665. data[@"warehouse_name"]=warehouse_name;
  666. data[@"bill_to_name"]=billing_name;
  667. data[@"bill_to_ext"]=billto;
  668. data[@"ship_to_name"]=receive_name;
  669. data[@"ship_to_ext"]=receive;
  670. data[@"po_no"]=poNumber;
  671. data[@"rep"]=sales_rep;
  672. data[@"e_schdule"]=etd;
  673. data[@"sales_terms"]=sales_terms;
  674. data[@"port_destination"]=port_destination;
  675. data[@"terms"]=terms;
  676. data[@"carrier"]=carrier;
  677. data[@"vendor_no"]=vendor_no;
  678. data[@"customer_sign"]=sign_picpath;
  679. data[@"print_status"]=print_status;
  680. data[@"create_date"]=create_date;
  681. 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];;
  682. sqlite3_stmt * statement_cart;
  683. // NSDate *date1 = [NSDate date];
  684. int count=0;
  685. // int cart_count=0;
  686. float credits=0;
  687. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  688. if ( dbresult== SQLITE_OK)
  689. {
  690. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  691. int row1count=0;
  692. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  693. {
  694. // NSDate *row_date = [NSDate date];
  695. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  696. // int product_id = sqlite3_column_int(statement_cart, 0);
  697. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  698. int item_id = sqlite3_column_int(statement_cart, 7);
  699. double discount = sqlite3_column_double(statement_cart, 2);
  700. NSString* Price=nil;
  701. if(str_price==nil)
  702. {
  703. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  704. // NSDate *price_date = [NSDate date];
  705. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  706. // DebugLog(@"price time interval");
  707. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  708. if(price==nil)
  709. Price=@"No Price.";
  710. else
  711. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  712. }
  713. else
  714. {
  715. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  716. }
  717. float dunitprice=0;
  718. int item_count = sqlite3_column_int(statement_cart, 3);
  719. NSString* Amount=@"";
  720. if(![Price isEqualToString:@"No Price."])
  721. {
  722. dunitprice=[Price floatValue];
  723. dunitprice=dunitprice* (1-discount/100.0);
  724. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  725. credits+=dunitprice*item_count;
  726. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  727. }
  728. else
  729. {
  730. Price=@"";
  731. }
  732. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  733. // NSString *nsline_note=nil;
  734. // if(line_note!=nil)
  735. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  736. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  737. NSString *nsname=nil;
  738. if(name!=nil)
  739. nsname= [[NSString alloc]initWithUTF8String:name];
  740. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  741. NSString *nsdescription=nil;
  742. if(description!=nil)
  743. nsdescription= [[NSString alloc]initWithUTF8String:description];
  744. // int stockUom = sqlite3_column_int(statement_cart, 8);
  745. // int _id = sqlite3_column_int(statement_cart, 9);
  746. // NSDate *subtotal_date = [NSDate date];
  747. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  748. // DebugLog(@"subtotal_date time interval");
  749. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  750. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  751. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  752. // int carton=[bsubtotaljson[@"carton"] intValue];
  753. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  754. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  755. // NSDate *img_date = [NSDate date];
  756. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  757. // DebugLog(@"img_date time interval");
  758. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  759. itemjson[@"model"]=nsname;
  760. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  761. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  762. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  763. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  764. // itemjson[@"check"]=@"true";
  765. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  766. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  767. itemjson[@"unit_price"]=Price;
  768. itemjson[@"amount"]=Amount;
  769. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  770. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  771. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  772. // itemjson[@"note"]=nsline_note;
  773. // NSDate *date2 = [NSDate date];
  774. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  775. // DebugLog(@"model_bundle time interval");
  776. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  777. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  778. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  779. row1count++;
  780. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  781. for(int c=0;c<[combine[@"count"] intValue];c++)
  782. {
  783. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  784. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  785. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  786. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  787. credits+=combine_amount;
  788. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  789. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  790. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  791. row1count++;
  792. }
  793. count++;
  794. // DebugLog(@"row time interval");
  795. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  796. }
  797. row1[@"count"]=[NSNumber numberWithInt:row1count];
  798. datasource[@"row1"]=row1;
  799. data[@"table_model"]=datasource;
  800. sqlite3_finalize(statement_cart);
  801. }
  802. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  803. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  804. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  805. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  806. // if(credits>0)
  807. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  808. // else
  809. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  810. if(shipping>0)
  811. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  812. else
  813. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  814. if(lift_gate_value>0)
  815. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  816. else
  817. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  818. if(handling_fee_value>0)
  819. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  820. else
  821. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  822. float total = credits+shipping+lift_gate_value+handling_fee_value;
  823. if(total>0)
  824. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  825. else
  826. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  827. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  828. fee[@"row0"]=fee_row0;
  829. data[@"table_fee"]=fee;
  830. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  831. // ret[@"sign_url"] = sign_url;
  832. //
  833. // customer_contact = [self textAtColumn:52 statement:statement];
  834. // customer_email = [self textAtColumn:53 statement:statement];
  835. // customer_phone = [self textAtColumn:54 statement:statement];
  836. // customer_fax = [self textAtColumn:55 statement:statement];
  837. //
  838. //
  839. // int offline_edit=sqlite3_column_int(statement, 56);
  840. //
  841. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  842. //
  843. // char *soid = (char*)sqlite3_column_text(statement, 1);
  844. // if(soid==nil)
  845. // soid= "";
  846. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  847. // // so#
  848. // ret[@"so#"] = nssoid;
  849. //
  850. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  851. // if(poNumber==nil)
  852. // poNumber= "";
  853. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  854. //
  855. //
  856. //
  857. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  858. // if(create_time==nil)
  859. // create_time= "";
  860. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  861. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  862. //
  863. // int status = sqlite3_column_int(statement, 4);
  864. // int erpStatus = sqlite3_column_int(statement, 49);
  865. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  866. //
  867. // // status
  868. // if (status > 1 && status != 3) {
  869. // status = erpStatus;
  870. // } else if (status == 3) {
  871. // status = 15;
  872. // }
  873. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  874. // ret[@"order_status"] = nsstatus;
  875. //
  876. //
  877. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  878. // if(company_name==nil)
  879. // company_name= "";
  880. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  881. // // company name
  882. // ret[@"company_name"] = nscompany_name;
  883. //
  884. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  885. // if(customer_contact==nil)
  886. // customer_contact= "";
  887. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  888. //
  889. //
  890. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  891. // if(addr_1==nil)
  892. // addr_1="";
  893. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  894. //
  895. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  896. // if(addr_2==nil)
  897. // addr_2="";
  898. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  899. //
  900. //
  901. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  902. // if(addr_3==nil)
  903. // addr_3="";
  904. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  905. //
  906. //
  907. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  908. // if(addr_4==nil)
  909. // addr_4="";
  910. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  911. //
  912. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  913. // [arr_addr addObject:nsaddr_1];
  914. // [arr_addr addObject:nsaddr_2];
  915. // [arr_addr addObject:nsaddr_3];
  916. // [arr_addr addObject:nsaddr_4];
  917. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  918. //
  919. //
  920. // char *logist = (char*)sqlite3_column_text(statement, 11);
  921. // if(logist==nil)
  922. // logist= "";
  923. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  924. // if (status == -11 || status == 10 || status == 11) {
  925. // nslogist = [self textAtColumn:59 statement:statement];
  926. // };
  927. //
  928. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  929. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  930. // shipping = @"Shipping To Be Quoted";
  931. // } else {
  932. // shippingFee = sqlite3_column_double(statement, 12);
  933. // }
  934. //
  935. // // Shipping
  936. // ret[@"Shipping"] = shipping;
  937. //
  938. // int have_lift_gate = sqlite3_column_int(statement, 17);
  939. // lift_gate = sqlite3_column_double(statement, 13);
  940. // // Liftgate Fee(No loading dock)
  941. // if (!have_lift_gate) {
  942. // lift_gate = 0;
  943. // }
  944. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  945. // if (sqlite3_column_int(statement, 57)) {
  946. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  947. // }
  948. //
  949. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  950. // if(general_notes==nil)
  951. // general_notes= "";
  952. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  953. //
  954. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  955. // if(internal_notes==nil)
  956. // internal_notes= "";
  957. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  958. //
  959. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  960. // if(payment_type==nil)
  961. // payment_type= "";
  962. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  963. //
  964. //
  965. //
  966. // // order info
  967. // orderinfo = [self textFileName:@"order_info.html"];
  968. //
  969. //
  970. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  971. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  972. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  973. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  974. //
  975. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  976. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  977. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  978. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  979. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  980. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  981. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  982. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  983. //
  984. // NSString *payment = nil;
  985. // if([nspayment_type isEqualToString:@"Credit Card"])
  986. // {
  987. // payment = [self textFileName:@"creditcardpayment.html"];
  988. //
  989. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  990. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  991. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  992. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  993. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  994. // NSString *card_type = [self textAtColumn:42 statement:statement];
  995. // if (card_type.length > 0) { // 显示星号
  996. // card_type = @"****";
  997. // }
  998. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  999. // if (card_number.length > 0 && card_number.length > 4) {
  1000. // for (int i = 0; i < card_number.length - 4; i++) {
  1001. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  1002. // }
  1003. // } else {
  1004. // card_number = @"";
  1005. // }
  1006. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  1007. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  1008. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  1009. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  1010. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  1011. // card_expiration = @"****";
  1012. // }
  1013. //
  1014. //
  1015. // NSString *card_city = [self textAtColumn:46 statement:statement];
  1016. // NSString *card_state = [self textAtColumn:47 statement:statement];
  1017. //
  1018. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  1019. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  1020. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  1021. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  1022. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  1023. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  1024. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  1025. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  1026. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  1027. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  1028. //
  1029. //
  1030. // }
  1031. // else
  1032. // {
  1033. // payment=[self textFileName:@"normalpayment.html"];
  1034. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1035. // }
  1036. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1037. //
  1038. // ret[@"result"]= [NSNumber numberWithInt:2];
  1039. //
  1040. // // more info
  1041. // moreInfo = [self textFileName:@"more_info.html"];
  1042. //
  1043. //
  1044. // // ShipToCompany_or_&nbsp
  1045. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1046. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1047. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1048. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1049. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1050. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1051. //
  1052. //
  1053. // // ShipFromCompany_or_&nbsp
  1054. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1055. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1056. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1057. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1058. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1059. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1060. //
  1061. //
  1062. // // FreightBillToCompany_or_&nbsp
  1063. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1064. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1065. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1066. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1067. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1068. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1069. //
  1070. //
  1071. // // MerchandiseBillToCompany_or_&nbsp
  1072. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1073. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1074. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1075. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1076. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1077. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1078. //
  1079. //
  1080. // // ReturnToCompany_or_&nbsp
  1081. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1082. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1083. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1084. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1085. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1086. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1087. // //
  1088. //
  1089. // // DebugLog(@"more info : %@",moreInfo);
  1090. //
  1091. // // handling fee
  1092. // handlingFee = sqlite3_column_double(statement, 33);
  1093. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1094. // if (sqlite3_column_int(statement, 58)) {
  1095. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1096. // }
  1097. // //
  1098. //
  1099. // // customer info
  1100. // customerID = [self textAtColumn:36 statement:statement];
  1101. //
  1102. // // mode
  1103. // ret[@"mode"] = appDelegate.mode;
  1104. //
  1105. // // model_count
  1106. // ret[@"model_count"] = @(0);
  1107. }
  1108. sqlite3_finalize(statement);
  1109. }
  1110. [iSalesDB close_db:db];
  1111. data[@"order_type"]=order_type;
  1112. /*
  1113. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1114. // "customer_email" = "Shui Hu";
  1115. // "customer_fax" = "";
  1116. // "customer_first_name" = F;
  1117. // "customer_last_name" = L;
  1118. // "customer_name" = ",da He Xiang Dong Liu A";
  1119. // "customer_phone" = "Hey Xuan Feng";
  1120. contactInfo[@"customer_phone"] = customer_phone;
  1121. contactInfo[@"customer_fax"] = customer_fax;
  1122. contactInfo[@"customer_email"] = customer_email;
  1123. NSString *first_name = @"";
  1124. NSString *last_name = @"";
  1125. if ([customer_contact isEqualToString:@""]) {
  1126. } else if ([customer_contact containsString:@" "]) {
  1127. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1128. first_name = [customer_contact substringToIndex:first_space_index];
  1129. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1130. }
  1131. contactInfo[@"customer_first_name"] = first_name;
  1132. contactInfo[@"customer_last_name"] = last_name;
  1133. ret[@"customerInfo"] = contactInfo;
  1134. // models
  1135. if (nssoid) {
  1136. __block double TotalCuft = 0;
  1137. __block double TotalWeight = 0;
  1138. __block int TotalCarton = 0;
  1139. __block double allItemPrice = 0;
  1140. 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];
  1141. sqlite3 *db1 = [iSalesDB get_db];
  1142. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1143. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1144. int product_id = sqlite3_column_int(stmt, 0);
  1145. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1146. int item_id = sqlite3_column_int(stmt, 7);
  1147. NSString* Price=nil;
  1148. if(str_price==nil)
  1149. {
  1150. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1151. if(price==nil)
  1152. Price=@"No Price.";
  1153. else
  1154. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1155. }
  1156. else
  1157. {
  1158. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1159. }
  1160. double discount = sqlite3_column_double(stmt, 2);
  1161. int item_count = sqlite3_column_int(stmt, 3);
  1162. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1163. NSString *nsline_note=nil;
  1164. if(line_note!=nil)
  1165. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1166. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1167. // NSString *nsname = nil;
  1168. // if(name!=nil)
  1169. // nsname= [[NSString alloc]initWithUTF8String:name];
  1170. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1171. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1172. // NSString *nsdescription=nil;
  1173. // if(description!=nil)
  1174. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1175. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1176. // int stockUom = sqlite3_column_int(stmt, 8);
  1177. // int _id = sqlite3_column_int(stmt, 9);
  1178. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1179. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1180. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1181. int carton=[bsubtotaljson[@"carton"] intValue];
  1182. TotalCuft += cuft;
  1183. TotalWeight += weight;
  1184. TotalCarton += carton;
  1185. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1186. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1187. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1188. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1189. itemjson[@"note"]=nsline_note;
  1190. itemjson[@"origin_price"] = Price;
  1191. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1192. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1193. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1194. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1195. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1196. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1197. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1198. if(itemjson[@"combine"] != nil)
  1199. {
  1200. // int citem=0;
  1201. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1202. for(int bc=0;bc<bcount;bc++)
  1203. {
  1204. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1205. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1206. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1207. subTotal += uprice * modulus * item_count;
  1208. }
  1209. }
  1210. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1211. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1212. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1213. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1214. allItemPrice += subTotal;
  1215. }];
  1216. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1217. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1218. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1219. // payments/Credits
  1220. // payments_and_credist = sqlite3_column_double(statement, 34);
  1221. payments_and_credist = allItemPrice;
  1222. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1223. // // total
  1224. // totalPrice = sqlite3_column_double(statement, 35);
  1225. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1226. } else {
  1227. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1228. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1229. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1230. // payments/Credits
  1231. payments_and_credist = 0;
  1232. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1233. }
  1234. // total
  1235. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1236. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1237. ret[@"order_info"]= orderinfo;
  1238. ret[@"more_order_info"] = moreInfo;
  1239. return [RAConvertor dict2data:ret];
  1240. */
  1241. DebugLog(@"debug sales order data:%@", [RAConvertor dict2string:data]);
  1242. return data;
  1243. }
  1244. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1245. {
  1246. NSString* template_file=nil;
  1247. switch([params[@"thumb"] intValue])
  1248. {
  1249. case 0:
  1250. template_file= @"so.json";
  1251. break;
  1252. case 1:
  1253. template_file= @"so_thumb.json";
  1254. break;
  1255. default:
  1256. template_file= @"so.json";
  1257. }
  1258. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] type:[params[@"thumb"] intValue]];
  1259. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1260. if(data[@"table_model"][@"row2"]==nil)
  1261. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1262. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1263. DebugLog(@"%@",file);
  1264. // return nil;
  1265. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1266. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1267. if (file) {
  1268. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1269. [dic setObject:file forKey:@"pdf_path"];
  1270. dic[@"isLocalFile"]=@"true";
  1271. return [RAConvertor dict2data:dic];
  1272. }
  1273. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1274. dic[@"isLocalFile"]=@"true";
  1275. return [RAConvertor dict2data:dic];
  1276. }
  1277. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1278. {
  1279. NSString* template_file=nil;
  1280. switch([params[@"pdf_style"] intValue])
  1281. {
  1282. case 0:
  1283. template_file= @"portfolio_2x3.json";
  1284. break;
  1285. case 1:
  1286. template_file= @"portfolio_3x2.json";
  1287. break;
  1288. default:
  1289. template_file= @"portfolio_3x2.json";
  1290. }
  1291. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1292. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1293. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1294. DebugLog(@"%@",file);
  1295. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1296. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1297. if (file) {
  1298. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1299. [dic setObject:file forKey:@"pdf_path"];
  1300. dic[@"isLocalFile"]=@"true";
  1301. return [RAConvertor dict2data:dic];
  1302. }
  1303. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1304. dic[@"isLocalFile"]=@"true";
  1305. return [RAConvertor dict2data:dic];
  1306. }
  1307. +(NSString*) get_offline_soid:(sqlite3*)db
  1308. {
  1309. NSString* soid=nil;
  1310. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1311. [formatter setDateFormat:@"yyMMdd"];
  1312. NSString* date = [formatter stringFromDate:[NSDate date]];
  1313. for(int i=1;i<999;i++)
  1314. {
  1315. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1316. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1317. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1318. if(count==0)
  1319. return soid;
  1320. }
  1321. return nil;
  1322. }
  1323. +(NSArray*) enumOfflineOrder
  1324. {
  1325. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1326. sqlite3 *db = [iSalesDB get_db];
  1327. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1328. sqlite3_stmt * statement;
  1329. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1330. if ( dbresult== SQLITE_OK)
  1331. {
  1332. while (sqlite3_step(statement) == SQLITE_ROW)
  1333. {
  1334. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1335. NSString* nsso_id=nil;
  1336. if(so_id!=nil)
  1337. {
  1338. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1339. }
  1340. [ret addObject:nsso_id];
  1341. }
  1342. sqlite3_finalize(statement);
  1343. }
  1344. [iSalesDB close_db:db];
  1345. return ret;
  1346. }
  1347. //+(void) uploadFile:(NSString*) file
  1348. //{
  1349. // NSData* data = [NSData dataWithContentsOfFile: file];
  1350. // UIApplication * app = [UIApplication sharedApplication];
  1351. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1352. //
  1353. //
  1354. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1355. //
  1356. //
  1357. //
  1358. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1359. //
  1360. // if(appDelegate.user!=nil)
  1361. // [params setValue:appDelegate.user forKey:@"user"];
  1362. // // if(appDelegate.contact_id!=nil)
  1363. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1364. // if(appDelegate.password!=nil)
  1365. // [params setValue:appDelegate.password forKey:@"password"];
  1366. //
  1367. //
  1368. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1369. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1370. // } error:nil];
  1371. //
  1372. //
  1373. //
  1374. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1375. //
  1376. // NSProgress *progress = nil;
  1377. //
  1378. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1379. //
  1380. //
  1381. //
  1382. // if (error) {
  1383. //
  1384. // NSString* err_msg = [error localizedDescription];
  1385. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1386. //
  1387. //
  1388. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1389. //
  1390. // DebugLog(@"data string: %@",str);
  1391. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1392. //
  1393. // } else {
  1394. // DebugLog(@"response ");
  1395. //
  1396. //
  1397. //
  1398. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1399. //
  1400. // // 再将NSData转为字符串
  1401. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1402. //
  1403. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1404. //
  1405. // DebugLog(@"data string: %@",jsonStr);
  1406. //
  1407. // NSDictionary* json = responseObject;
  1408. //
  1409. //
  1410. // if([[json valueForKey:@"result"] intValue]==2)
  1411. // {
  1412. //// NSString* img_url_down = json[@"img_url_aname"];
  1413. //// NSString* img_url_up = json[@"img_url"];
  1414. //
  1415. //
  1416. // }
  1417. // else
  1418. // {
  1419. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1420. // }
  1421. //
  1422. //
  1423. // }
  1424. // // [waitalert dismissViewControllerAnimated:YES completion:nil];
  1425. // }];
  1426. //
  1427. //
  1428. //
  1429. //
  1430. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1431. // //
  1432. //
  1433. //
  1434. //
  1435. //
  1436. //
  1437. //
  1438. // [uploadTask resume];
  1439. //
  1440. //}
  1441. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  1442. {
  1443. NSString* serial= [[NSUUID UUID] UUIDString];
  1444. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1445. NSString *cachefolder = [paths objectAtIndex:0];
  1446. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1447. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1448. NSFileManager* fileManager = [NSFileManager defaultManager];
  1449. BOOL bdir=YES;
  1450. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  1451. {
  1452. NSError *error = nil;
  1453. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  1454. if(!bsuccess)
  1455. DebugLog(@"Create UPLOAD folder failed");
  1456. }
  1457. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1458. ret[@"contact"]=[self prepareContact:serial];
  1459. ret[@"wishlist"]=[self prepareWishlist:serial];
  1460. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  1461. ret[@"portfolio"] = [self preparePortfolio:serial];
  1462. ret[@"view_portfolio"] = [self preparePDF:serial];
  1463. NSString* str= [RAConvertor dict2string:ret];
  1464. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  1465. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  1466. NSError *error=nil;
  1467. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  1468. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  1469. ZipArchive* zip = [[ZipArchive alloc] init];
  1470. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  1471. for(int i=0;i<arr_files.count;i++)
  1472. {
  1473. NSString* file=arr_files[i];
  1474. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  1475. }
  1476. if( ![zip CloseZipFile2] )
  1477. {
  1478. zippath = @"";
  1479. }
  1480. ret[@"file"]=zippath;
  1481. return ret;
  1482. }
  1483. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  1484. {
  1485. if(filename.length==0)
  1486. return false;
  1487. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1488. bool ret=false;
  1489. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1490. NSString *cachefolder = [paths objectAtIndex:0];
  1491. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1492. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1493. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1494. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1495. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1496. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  1497. NSFileManager* fileManager = [NSFileManager defaultManager];
  1498. BOOL bdir=NO;
  1499. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1500. {
  1501. NSError *error = nil;
  1502. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1503. {
  1504. ret=false;
  1505. }
  1506. else
  1507. {
  1508. ret=true;
  1509. }
  1510. }
  1511. return ret;
  1512. }
  1513. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  1514. {
  1515. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1516. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  1517. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  1518. NSString* where=@" 1=1";
  1519. if (ver!=nil) {
  1520. where=@"is_dirty=1";
  1521. }
  1522. sqlite3 *db = [iSalesDB get_db];
  1523. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  1524. sqlite3_stmt * statement;
  1525. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1526. int count=0;
  1527. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1528. if ( dbresult== SQLITE_OK)
  1529. {
  1530. while (sqlite3_step(statement) == SQLITE_ROW)
  1531. {
  1532. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1533. int _id = sqlite3_column_int(statement, 0);
  1534. int product_id = sqlite3_column_int(statement, 1);
  1535. int item_id = sqlite3_column_int(statement, 2);
  1536. int qty = sqlite3_column_int(statement, 3);
  1537. char *create_time = (char*)sqlite3_column_text(statement, 4);
  1538. NSString* nscreate_time=nil;
  1539. if(create_time!=nil)
  1540. {
  1541. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  1542. }
  1543. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  1544. NSString* nsmodify_time=nil;
  1545. if(modify_time!=nil)
  1546. {
  1547. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1548. }
  1549. int is_delete = sqlite3_column_int(statement, 6);
  1550. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1551. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1552. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1553. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1554. itemjson[@"create_time"]=nscreate_time;
  1555. itemjson[@"modify_time"]=nsmodify_time;
  1556. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1557. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1558. count++;
  1559. }
  1560. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1561. sqlite3_finalize(statement);
  1562. }
  1563. ret[@"count"]=[NSNumber numberWithInt:count ];
  1564. [iSalesDB close_db:db];
  1565. return ret;
  1566. }
  1567. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  1568. {
  1569. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1570. // UIApplication * app = [UIApplication sharedApplication];
  1571. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1572. 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 ];
  1573. // 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 ];
  1574. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1575. DebugLog(@"offline_login sql:%@",sqlQuery);
  1576. sqlite3_stmt * statement;
  1577. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1578. int count=0;
  1579. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1580. if ( dbresult== SQLITE_OK)
  1581. {
  1582. while (sqlite3_step(statement) == SQLITE_ROW)
  1583. {
  1584. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1585. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  1586. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  1587. int product_id = sqlite3_column_int(statement, 0);
  1588. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1589. int item_id = sqlite3_column_int(statement, 7);
  1590. NSString* Price=nil;
  1591. if(str_price==nil)
  1592. {
  1593. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1594. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  1595. if(price==nil)
  1596. Price=@"No Price.";
  1597. else
  1598. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1599. }
  1600. else
  1601. {
  1602. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1603. }
  1604. double discount = sqlite3_column_double(statement, 2);
  1605. int item_count = sqlite3_column_int(statement, 3);
  1606. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1607. NSString *nsline_note=nil;
  1608. if(line_note!=nil)
  1609. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1610. char *name = (char*)sqlite3_column_text(statement, 5);
  1611. NSString *nsname=nil;
  1612. if(name!=nil)
  1613. nsname= [[NSString alloc]initWithUTF8String:name];
  1614. char *description = (char*)sqlite3_column_text(statement, 6);
  1615. NSString *nsdescription=nil;
  1616. if(description!=nil)
  1617. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1618. // int stockUom = sqlite3_column_int(statement, 8);
  1619. // int _id = sqlite3_column_int(statement, 9);
  1620. //
  1621. //
  1622. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1623. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1624. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1625. // int carton=[bsubtotaljson[@"carton"] intValue];
  1626. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1627. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1628. itemjson[@"model"]=nsname;
  1629. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1630. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1631. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1632. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1633. // itemjson[@"check"]=@"true";
  1634. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1635. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1636. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1637. itemjson[@"unit_price"]=Price;
  1638. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1639. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1640. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1641. itemjson[@"note"]=nsline_note;
  1642. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  1643. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1644. count++;
  1645. }
  1646. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1647. sqlite3_finalize(statement);
  1648. }
  1649. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1650. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1651. return ret;
  1652. }
  1653. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  1654. {
  1655. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1656. sqlite3 *db = [iSalesDB get_db];
  1657. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  1658. // for(int i=0;i<arr_soid.count;i++)
  1659. sqlite3_stmt * statement;
  1660. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1661. //int count=0;
  1662. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1663. if ( dbresult== SQLITE_OK)
  1664. {
  1665. int count=0;
  1666. while (sqlite3_step(statement) == SQLITE_ROW)
  1667. {
  1668. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1669. int _id = sqlite3_column_int(statement, 0);
  1670. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  1671. NSString* nssync_data=nil;
  1672. if(sync_data!=nil)
  1673. {
  1674. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1675. }
  1676. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1677. NSString* nsimg_1=nil;
  1678. if(img_1!=nil)
  1679. {
  1680. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1681. [self copy_upImg:serial file:nsimg_1];
  1682. }
  1683. char *so_no = (char*)sqlite3_column_text(statement, 3);
  1684. NSString* nsso_no=nil;
  1685. if(so_no!=nil)
  1686. {
  1687. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  1688. }
  1689. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  1690. itemjson[@"order_type"]=@"submit order";
  1691. else
  1692. itemjson[@"order_type"]=@"archive order";
  1693. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1694. itemjson[@"json_data"]= nssync_data;
  1695. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  1696. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1697. count++;
  1698. }
  1699. ret[@"count"]=[NSNumber numberWithInt:count ];
  1700. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1701. }
  1702. sqlite3_finalize(statement);
  1703. [iSalesDB close_db:db];
  1704. return ret;
  1705. }
  1706. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  1707. {
  1708. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1709. sqlite3 *db = [iSalesDB get_db];
  1710. // UIApplication * app = [UIApplication sharedApplication];
  1711. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1712. 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";
  1713. sqlite3_stmt * statement;
  1714. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1715. int count=0;
  1716. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1717. if ( dbresult== SQLITE_OK)
  1718. {
  1719. while (sqlite3_step(statement) == SQLITE_ROW)
  1720. {
  1721. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1722. int _id = sqlite3_column_int(statement, 0);
  1723. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  1724. NSString* nsimg_0=nil;
  1725. if(img_0!=nil)
  1726. {
  1727. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1728. [self copy_upImg:serial file:nsimg_0];
  1729. }
  1730. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1731. NSString* nsimg_1=nil;
  1732. if(img_1!=nil)
  1733. {
  1734. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1735. [self copy_upImg:serial file:nsimg_1];
  1736. }
  1737. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  1738. NSString* nsimg_2=nil;
  1739. if(img_2!=nil)
  1740. {
  1741. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1742. [self copy_upImg:serial file:nsimg_2];
  1743. }
  1744. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  1745. NSString* nssync_data=nil;
  1746. if(sync_data!=nil)
  1747. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1748. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1749. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1750. itemjson[@"json_data"]= nssync_data;
  1751. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1752. count++;
  1753. }
  1754. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1755. sqlite3_finalize(statement);
  1756. }
  1757. ret[@"count"]=[NSNumber numberWithInt:count ];
  1758. [iSalesDB close_db:db];
  1759. return ret;
  1760. }
  1761. +(bool) check_offlinedata
  1762. {
  1763. // UIApplication * app = [UIApplication sharedApplication];
  1764. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1765. sqlite3 *db = [iSalesDB get_db];
  1766. NSString * where=@"1=1";
  1767. // if(appDelegate.user!=nil)
  1768. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  1769. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  1770. [iSalesDB close_db:db];
  1771. if(count==0)
  1772. {
  1773. return false;
  1774. }
  1775. return true;
  1776. //
  1777. //[iSalesDB close_db:db];
  1778. }
  1779. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  1780. {
  1781. UIApplication * app = [UIApplication sharedApplication];
  1782. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1783. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1784. sqlite3 *db = [iSalesDB get_db];
  1785. NSString* collectId=params[@"collectId"];
  1786. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  1787. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  1788. [iSalesDB execSql:sqlQuery db:db];
  1789. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1790. [iSalesDB close_db:db];
  1791. appDelegate.wish_count =count;
  1792. [appDelegate update_count_mark];
  1793. ret[@"result"]= [NSNumber numberWithInt:2];
  1794. return ret;
  1795. }
  1796. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  1797. //{
  1798. //
  1799. // UIApplication * app = [UIApplication sharedApplication];
  1800. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1801. //
  1802. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1803. // sqlite3 *db = [iSalesDB get_db];
  1804. // NSString* product_id=params[@"product_id"];
  1805. //
  1806. //
  1807. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  1808. //
  1809. //
  1810. // for(int i=0;i<arr.count;i++)
  1811. // {
  1812. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  1813. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  1814. // if(count==0)
  1815. // {
  1816. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  1817. // [iSalesDB execSql:sqlQuery db:db];
  1818. // }
  1819. // }
  1820. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  1821. // [iSalesDB close_db:db];
  1822. //
  1823. // appDelegate.wish_count =count;
  1824. //
  1825. // [appDelegate update_count_mark];
  1826. // ret[@"result"]= [NSNumber numberWithInt:2];
  1827. // return ret;
  1828. // //
  1829. // //return ret;
  1830. //}
  1831. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  1832. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  1833. DebugLog(@"time interval: %lf",interval);
  1834. }
  1835. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  1836. {
  1837. UIApplication * app = [UIApplication sharedApplication];
  1838. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1839. int sort = [[params objectForKey:@"sort"] intValue];
  1840. NSString *sort_str = @"";
  1841. switch (sort) {
  1842. case 0:{
  1843. sort_str = @"order by w.modify_time desc";
  1844. }
  1845. break;
  1846. case 1:{
  1847. sort_str = @"order by w.modify_time asc";
  1848. }
  1849. break;
  1850. case 2:{
  1851. sort_str = @"order by m.name asc";
  1852. }
  1853. break;
  1854. case 3:{
  1855. sort_str = @"order by m.name desc";
  1856. }
  1857. break;
  1858. case 4:{
  1859. sort_str = @"order by m.description asc";
  1860. }
  1861. break;
  1862. default:
  1863. break;
  1864. }
  1865. // NSString* user = appDelegate.user;
  1866. sqlite3 *db = [iSalesDB get_db];
  1867. // order by w.create_time
  1868. 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];
  1869. // 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];
  1870. sqlite3_stmt * statement;
  1871. NSDate *date1 = [NSDate date];
  1872. // NSDate *date2 = nil;
  1873. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1874. int count=0;
  1875. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1876. {
  1877. while (sqlite3_step(statement) == SQLITE_ROW)
  1878. {
  1879. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1880. int product_id = sqlite3_column_double(statement, 0);
  1881. char *description = (char*)sqlite3_column_text(statement, 1);
  1882. if(description==nil)
  1883. description= "";
  1884. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  1885. int item_id = sqlite3_column_double(statement, 2);
  1886. NSDate *date_image = [NSDate date];
  1887. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  1888. printf("image : ");
  1889. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  1890. // char *url = (char*)sqlite3_column_text(statement, 3);
  1891. // if(url==nil)
  1892. // url="";
  1893. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1894. int qty = sqlite3_column_int(statement, 3);
  1895. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  1896. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  1897. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  1898. item[@"description"]= nsdescription;
  1899. item[@"img"]= nsurl;
  1900. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1901. count++;
  1902. }
  1903. printf("total time:");
  1904. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1905. ret[@"count"]= [NSNumber numberWithInt:count];
  1906. ret[@"total_count"]= [NSNumber numberWithInt:count];
  1907. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  1908. ret[@"result"]= [NSNumber numberWithInt:2];
  1909. appDelegate.wish_count =count;
  1910. [appDelegate update_count_mark];
  1911. sqlite3_finalize(statement);
  1912. }
  1913. [iSalesDB close_db:db];
  1914. return ret;
  1915. }
  1916. +(NSDictionary*) offline_notimpl
  1917. {
  1918. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1919. ret[@"result"]=@"8";
  1920. ret[@"err_msg"]=@"offline mode does not support this function.";
  1921. return ret;
  1922. }
  1923. +(NSDictionary*) offline_home
  1924. {
  1925. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1926. NSString *cachefolder = [paths objectAtIndex:0];
  1927. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  1928. NSData* json =nil;
  1929. json=[NSData dataWithContentsOfFile:img_cache];
  1930. if(json==nil)
  1931. return nil;
  1932. NSError *error=nil;
  1933. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1934. return menu;
  1935. }
  1936. +(NSDictionary*) offline_category_menu
  1937. {
  1938. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1939. NSString *cachefolder = [paths objectAtIndex:0];
  1940. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  1941. NSData* json =nil;
  1942. json=[NSData dataWithContentsOfFile:img_cache];
  1943. if(json==nil)
  1944. return nil;
  1945. NSError *error=nil;
  1946. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1947. return menu;
  1948. }
  1949. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  1950. {
  1951. NSString* offline_command=params[@"offline_Command"];
  1952. NSDictionary* ret=nil;
  1953. if([offline_command isEqualToString:@"model_NIYMAL"])
  1954. {
  1955. NSString* category = params[@"category"];
  1956. ret = [self refresh_model_NIYMAL:category];
  1957. }
  1958. return ret;
  1959. }
  1960. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  1961. {
  1962. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1963. [ret setValue:@"2" forKey:@"result"];
  1964. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1965. sqlite3* db= [iSalesDB get_db];
  1966. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1967. [iSalesDB close_db:db];
  1968. [ret setObject:detail1_section forKey:@"detail_1"];
  1969. return ret;
  1970. }
  1971. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  1972. {
  1973. UIApplication * app = [UIApplication sharedApplication];
  1974. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1975. // NSArray* arr1 = [self get_user_all_price_type];
  1976. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1977. // NSSet *set1 = [NSSet setWithArray:arr1];
  1978. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1979. // if(appDelegate.contact_id==nil)
  1980. // set2=[set1 mutableCopy];
  1981. // else
  1982. // [set2 intersectsSet:set1];
  1983. // NSArray *retarr = [set2 allObjects];
  1984. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1985. // sqlite3 *db = [iSalesDB get_db];
  1986. NSString* sqlQuery = nil;
  1987. if(appDelegate.contact_id==nil)
  1988. {
  1989. if(!appDelegate.bLogin)
  1990. return nil;
  1991. 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];
  1992. }
  1993. else
  1994. 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];
  1995. sqlite3_stmt * statement;
  1996. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1997. int count=0;
  1998. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1999. {
  2000. while (sqlite3_step(statement) == SQLITE_ROW)
  2001. {
  2002. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2003. char *name = (char*)sqlite3_column_text(statement, 0);
  2004. if(name==nil)
  2005. name="";
  2006. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2007. // double price = sqlite3_column_double(statement, 1);
  2008. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  2009. // if(isnull==nil)
  2010. // item[nsname]= @"No Price";
  2011. // else
  2012. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  2013. char *price = (char*)sqlite3_column_text(statement, 1);
  2014. if(price!=nil)
  2015. {
  2016. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2017. nsprice=[AESCrypt fastdecrypt:nsprice];
  2018. if(nsprice.length>0)
  2019. {
  2020. double dp= [nsprice doubleValue];
  2021. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2022. }
  2023. }
  2024. else
  2025. {
  2026. item[nsname]= @"No Price";
  2027. }
  2028. // int type= sqlite3_column_int(statement, 2);
  2029. //item[@"type"]=@"price";
  2030. // item[nsname]= nsprice;
  2031. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2032. count++;
  2033. }
  2034. ret[@"count"]= [NSNumber numberWithInt:count];
  2035. sqlite3_finalize(statement);
  2036. }
  2037. // [iSalesDB close_db:db];
  2038. return ret;
  2039. }
  2040. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2041. //{
  2042. // NSArray* arr1 = [self get_user_all_price_type:db];
  2043. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2044. //
  2045. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2046. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2047. // // [set2 intersectsSet:set1];
  2048. // //
  2049. // //
  2050. // // NSArray *retarr = [set2 allObjects];
  2051. //
  2052. // NSString* whereprice=nil;
  2053. // if(contact_id==nil)
  2054. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2055. // else
  2056. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2057. //
  2058. //
  2059. // // sqlite3 *db = [iSalesDB get_db];
  2060. //
  2061. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2062. // sqlite3_stmt * statement;
  2063. //
  2064. //
  2065. // NSNumber* ret = nil;
  2066. // double dprice=DBL_MAX;
  2067. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2068. // {
  2069. //
  2070. //
  2071. // while (sqlite3_step(statement) == SQLITE_ROW)
  2072. // {
  2073. //
  2074. // // double val = sqlite3_column_double(statement, 0);
  2075. // char *price = (char*)sqlite3_column_text(statement, 0);
  2076. // if(price!=nil)
  2077. // {
  2078. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2079. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2080. // if(nsprice.length>0)
  2081. // {
  2082. // double dp= [nsprice doubleValue];
  2083. // if(dp<dprice)
  2084. // dprice=dp;
  2085. // }
  2086. // }
  2087. // }
  2088. //
  2089. //
  2090. //
  2091. //
  2092. // sqlite3_finalize(statement);
  2093. //
  2094. //
  2095. //
  2096. //
  2097. // }
  2098. //
  2099. // // [iSalesDB close_db:db];
  2100. //
  2101. // if(dprice==DBL_MAX)
  2102. // ret= nil;
  2103. // else
  2104. // ret= [NSNumber numberWithDouble:dprice];
  2105. // return ret;
  2106. //}
  2107. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2108. {
  2109. NSArray* arr1 = [self get_user_all_price_type:db];
  2110. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2111. NSString* whereprice=nil;
  2112. if(contact_id==nil)
  2113. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2114. else
  2115. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2116. // sqlite3 *db = [iSalesDB get_db];
  2117. NSString *productIdCondition = @"1 = 1";
  2118. if (product_id) {
  2119. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2120. }
  2121. NSString *itemIdCondition = @"";
  2122. if (item_id) {
  2123. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2124. }
  2125. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2126. sqlite3_stmt * statement;
  2127. NSNumber* ret = nil;
  2128. double dprice=DBL_MAX;
  2129. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2130. {
  2131. while (sqlite3_step(statement) == SQLITE_ROW)
  2132. {
  2133. // double val = sqlite3_column_double(statement, 0);
  2134. char *price = (char*)sqlite3_column_text(statement, 0);
  2135. if(price!=nil)
  2136. {
  2137. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2138. nsprice=[AESCrypt fastdecrypt:nsprice];
  2139. if(nsprice.length>0)
  2140. {
  2141. double dp= [nsprice doubleValue];
  2142. if(dp<dprice)
  2143. dprice=dp;
  2144. }
  2145. }
  2146. }
  2147. sqlite3_finalize(statement);
  2148. }
  2149. // [iSalesDB close_db:db];
  2150. if(dprice==DBL_MAX)
  2151. ret= nil;
  2152. else
  2153. ret= [NSNumber numberWithDouble:dprice];
  2154. return ret;
  2155. }
  2156. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  2157. {
  2158. NSArray* ret=nil;
  2159. // sqlite3 *db = [iSalesDB get_db];
  2160. // no customer assigned , use login user contact_id
  2161. UIApplication * app = [UIApplication sharedApplication];
  2162. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2163. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  2164. sqlite3_stmt * statement;
  2165. // int count=0;
  2166. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2167. {
  2168. if (sqlite3_step(statement) == SQLITE_ROW)
  2169. {
  2170. char *val = (char*)sqlite3_column_text(statement, 0);
  2171. if(val==nil)
  2172. val="";
  2173. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2174. ret=[RAConvertor string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2175. }
  2176. sqlite3_finalize(statement);
  2177. }
  2178. // [iSalesDB close_db:db];
  2179. return ret;
  2180. }
  2181. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  2182. {
  2183. // sqlite3 *db = [iSalesDB get_db];
  2184. if(contact_id==nil)
  2185. {
  2186. // no customer assigned , use login user contact_id
  2187. UIApplication * app = [UIApplication sharedApplication];
  2188. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2189. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  2190. sqlite3_stmt * statement;
  2191. // int count=0;
  2192. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2193. {
  2194. if (sqlite3_step(statement) == SQLITE_ROW)
  2195. {
  2196. char *val = (char*)sqlite3_column_text(statement, 0);
  2197. if(val==nil)
  2198. val="";
  2199. contact_id = [[NSString alloc]initWithUTF8String:val];
  2200. }
  2201. sqlite3_finalize(statement);
  2202. }
  2203. if(contact_id.length<=0)
  2204. {
  2205. // [iSalesDB close_db:db];
  2206. return nil;
  2207. }
  2208. }
  2209. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2210. sqlite3_stmt * statement;
  2211. NSArray* ret=nil;
  2212. // int count=0;
  2213. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2214. {
  2215. if (sqlite3_step(statement) == SQLITE_ROW)
  2216. {
  2217. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2218. if(price_type==nil)
  2219. price_type="";
  2220. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2221. if(nsprice_type.length>0)
  2222. ret=[RAConvertor string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2223. else
  2224. ret=nil;
  2225. }
  2226. sqlite3_finalize(statement);
  2227. }
  2228. // [iSalesDB close_db:db];
  2229. return ret;
  2230. }
  2231. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2232. {
  2233. NSString* ret= nil;
  2234. // sqlite3 *db = [iSalesDB get_db];
  2235. NSString *sqlQuery = nil;
  2236. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2237. // 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;
  2238. // 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
  2239. if(product_id==nil && model_name)
  2240. 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;
  2241. else if (product_id)
  2242. 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
  2243. sqlite3_stmt * statement;
  2244. // int count=0;
  2245. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2246. {
  2247. if (sqlite3_step(statement) == SQLITE_ROW)
  2248. {
  2249. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2250. if(imgurl==nil)
  2251. imgurl="";
  2252. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2253. ret=nsimgurl;
  2254. }
  2255. sqlite3_finalize(statement);
  2256. }
  2257. else
  2258. {
  2259. [ret setValue:@"8" forKey:@"result"];
  2260. }
  2261. // [iSalesDB close_db:db];
  2262. DebugLog(@"data string: %@",ret );
  2263. return ret;
  2264. }
  2265. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  2266. {
  2267. NSString* ret= nil;
  2268. sqlite3 *db = [iSalesDB get_db];
  2269. NSString *sqlQuery = nil;
  2270. if(product_id==nil)
  2271. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2272. else
  2273. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2274. sqlite3_stmt * statement;
  2275. // int count=0;
  2276. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2277. {
  2278. if (sqlite3_step(statement) == SQLITE_ROW)
  2279. {
  2280. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2281. if(default_category==nil)
  2282. default_category="";
  2283. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2284. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2285. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2286. ret=nsdefault_category;
  2287. }
  2288. sqlite3_finalize(statement);
  2289. }
  2290. else
  2291. {
  2292. [ret setValue:@"8" forKey:@"result"];
  2293. }
  2294. [iSalesDB close_db:db];
  2295. DebugLog(@"data string: %@",ret );
  2296. return ret;
  2297. }
  2298. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  2299. NSString *sql = nil;
  2300. if (product_id != nil) {
  2301. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  2302. } else {
  2303. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  2304. }
  2305. NSString *result_set = [iSalesDB jk_queryText:sql];
  2306. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  2307. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  2308. return result_set;
  2309. }
  2310. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2311. {
  2312. NSString* model_name = [params valueForKey:@"product_name"];
  2313. NSString* product_id = [params valueForKey:@"product_id"];
  2314. NSString* category = [params valueForKey:@"category"];
  2315. NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  2316. if(category==nil) {
  2317. category = default_category_id;
  2318. } else {
  2319. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  2320. // 参数重有多个category id
  2321. if (arr_0.count > 1) {
  2322. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  2323. // 取交集
  2324. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  2325. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  2326. [set_0 intersectSet:set_1];
  2327. if (set_0.count == 1) {
  2328. category = (NSString *)[set_0 anyObject];
  2329. } else {
  2330. if ([set_0 containsObject:default_category_id]) {
  2331. category = default_category_id;
  2332. } else {
  2333. category = (NSString *)[set_0 anyObject];
  2334. }
  2335. }
  2336. }
  2337. }
  2338. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2339. sqlite3 *db = [iSalesDB get_db];
  2340. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2341. NSString *sqlQuery = nil;
  2342. if(product_id==nil)
  2343. 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='%@';
  2344. else
  2345. 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=%@;
  2346. sqlite3_stmt * statement;
  2347. [ret setValue:@"2" forKey:@"result"];
  2348. [ret setValue:@"3" forKey:@"detail_section_count"];
  2349. // int count=0;
  2350. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2351. {
  2352. if (sqlite3_step(statement) == SQLITE_ROW)
  2353. {
  2354. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2355. char *name = (char*)sqlite3_column_text(statement, 0);
  2356. if(name==nil)
  2357. name="";
  2358. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2359. char *description = (char*)sqlite3_column_text(statement, 1);
  2360. if(description==nil)
  2361. description="";
  2362. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2363. int product_id = sqlite3_column_int(statement, 2);
  2364. char *color = (char*)sqlite3_column_text(statement, 3);
  2365. if(color==nil)
  2366. color="";
  2367. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  2368. //
  2369. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  2370. // if(legcolor==nil)
  2371. // legcolor="";
  2372. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  2373. //
  2374. //
  2375. int availability = sqlite3_column_int(statement, 5);
  2376. //
  2377. int incoming_stock = sqlite3_column_int(statement, 6);
  2378. char *demension = (char*)sqlite3_column_text(statement, 7);
  2379. if(demension==nil)
  2380. demension="";
  2381. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  2382. // ,,,,,,,,,
  2383. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  2384. if(seat_height==nil)
  2385. seat_height="";
  2386. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  2387. char *material = (char*)sqlite3_column_text(statement, 9);
  2388. if(material==nil)
  2389. material="";
  2390. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  2391. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  2392. if(box_dim==nil)
  2393. box_dim="";
  2394. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  2395. char *volume = (char*)sqlite3_column_text(statement, 11);
  2396. if(volume==nil)
  2397. volume="";
  2398. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  2399. double weight = sqlite3_column_double(statement, 12);
  2400. char *model_set = (char*)sqlite3_column_text(statement, 13);
  2401. if(model_set==nil)
  2402. model_set="";
  2403. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  2404. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  2405. if(load_ability==nil)
  2406. load_ability="";
  2407. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  2408. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  2409. // if(default_category==nil)
  2410. // default_category="";
  2411. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2412. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  2413. if(fabric_content==nil)
  2414. fabric_content="";
  2415. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  2416. char *assembling = (char*)sqlite3_column_text(statement, 17);
  2417. if(assembling==nil)
  2418. assembling="";
  2419. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  2420. char *made_in = (char*)sqlite3_column_text(statement, 18);
  2421. if(made_in==nil)
  2422. made_in="";
  2423. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  2424. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  2425. if(special_remarks==nil)
  2426. special_remarks="";
  2427. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  2428. int stockUcom = sqlite3_column_double(statement, 20);
  2429. char *product_group = (char*)sqlite3_column_text(statement, 21);
  2430. if(product_group==nil)
  2431. product_group="";
  2432. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  2433. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  2434. // if(fashion_selector==nil)
  2435. // fashion_selector="";
  2436. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  2437. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  2438. if(selector_field==nil)
  2439. selector_field="";
  2440. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  2441. char *property_field = (char*)sqlite3_column_text(statement, 23);
  2442. if(property_field==nil)
  2443. property_field="";
  2444. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  2445. char *packaging = (char*)sqlite3_column_text(statement, 24);
  2446. if(packaging==nil)
  2447. packaging="";
  2448. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  2449. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2450. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  2451. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  2452. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  2453. [img_section setValue:model_s_img forKey:@"model_s_img"];
  2454. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  2455. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  2456. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  2457. NSString* Availability=nil;
  2458. if(availability>0)
  2459. Availability=[NSString stringWithFormat:@"%d",availability];
  2460. else
  2461. Availability = @"Out of Stock";
  2462. [img_section setValue:Availability forKey:@"Availability"];
  2463. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  2464. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  2465. char *eta = (char*)sqlite3_column_text(statement, 25);
  2466. if(eta==nil)
  2467. eta="";
  2468. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  2469. if ([nseta isEqualToString:@"null"]) {
  2470. nseta = @"";
  2471. }
  2472. if (availability <= 0) {
  2473. [img_section setValue:nseta forKey:@"ETA"];
  2474. }
  2475. int item_id = sqlite3_column_int(statement, 26);
  2476. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  2477. NSString *content_writing = [self textAtColumn:28 statement:statement];
  2478. NSString* Price=nil;
  2479. if(appDelegate.bLogin==false)
  2480. Price=@"Must Sign in.";
  2481. else
  2482. {
  2483. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2484. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2485. if(price==nil)
  2486. Price=@"No Price.";
  2487. else
  2488. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2489. }
  2490. [img_section setObject:content_writing forKey:@"product_content_writing"];
  2491. [img_section setValue:Price forKey:@"price"];
  2492. [img_section setValue:nsname forKey:@"model_name"];
  2493. [img_section setValue:nsdescription forKey:@"model_descrition"];
  2494. if (appDelegate.order_code) { // 离线order code即so#
  2495. 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];
  2496. __block int cartQTY = 0;
  2497. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2498. cartQTY = sqlite3_column_int(stmt, 0);
  2499. }];
  2500. if (cartQTY > 0) {
  2501. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  2502. }
  2503. }
  2504. [ret setObject:img_section forKey:@"img_section"];
  2505. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  2506. int detail0_item_count=0;
  2507. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2508. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2509. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2510. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2511. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2512. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2513. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2514. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2515. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2516. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2517. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2518. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2519. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2520. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2521. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  2522. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  2523. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  2524. {
  2525. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  2526. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2527. }
  2528. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  2529. [detail0_section setValue:@"kv" forKey:@"type"];
  2530. [detail0_section setValue:@"Product Information" forKey:@"title"];
  2531. [ret setObject:detail0_section forKey:@"detail_0"];
  2532. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2533. // [detail1_section setValue:@"detail" forKey:@"target"];
  2534. // [detail1_section setValue:@"popup" forKey:@"action"];
  2535. // [detail1_section setValue:@"content" forKey:@"type"];
  2536. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  2537. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2538. // [detail1_section setValue:@"true" forKey:@"single_row"];
  2539. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  2540. [ret setObject:detail1_section forKey:@"detail_1"];
  2541. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  2542. [detail2_section setValue:@"detail" forKey:@"target"];
  2543. [detail2_section setValue:@"popup" forKey:@"action"];
  2544. [detail2_section setValue:@"content" forKey:@"type"];
  2545. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  2546. [detail2_section setValue:@"true" forKey:@"single_row"];
  2547. [detail2_section setValue:@"local" forKey:@"data"];
  2548. [ret setObject:detail2_section forKey:@"detail_2"];
  2549. }
  2550. sqlite3_finalize(statement);
  2551. }
  2552. else
  2553. {
  2554. [ret setValue:@"8" forKey:@"result"];
  2555. }
  2556. // DebugLog(@"count:%d",count);
  2557. [iSalesDB close_db:db];
  2558. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  2559. return ret;
  2560. }
  2561. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  2562. // NSString* orderCode = [params valueForKey:@"orderCode"];
  2563. NSString* keyword = [params valueForKey:@"keyword"];
  2564. keyword=keyword.lowercaseString;
  2565. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  2566. int limit = [[params valueForKey:@"limit"] intValue];
  2567. int offset = [[params valueForKey:@"offset"] intValue];
  2568. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2569. NSString *limit_str = @"";
  2570. if (limited) {
  2571. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  2572. }
  2573. sqlite3 *db = [iSalesDB get_db];
  2574. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  2575. UIApplication * app = [UIApplication sharedApplication];
  2576. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2577. NSString *sqlQuery = nil;
  2578. if(exactMatch )
  2579. 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 ;
  2580. else
  2581. 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
  2582. DebugLog(@"offline_search sql:%@",sqlQuery);
  2583. sqlite3_stmt * statement;
  2584. [ret setValue:@"2" forKey:@"result"];
  2585. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2586. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2587. // int count=0;
  2588. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2589. {
  2590. int i=0;
  2591. while (sqlite3_step(statement) == SQLITE_ROW)
  2592. {
  2593. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2594. // char *name = (char*)sqlite3_column_text(statement, 1);
  2595. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2596. char *name = (char*)sqlite3_column_text(statement, 0);
  2597. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2598. char *description = (char*)sqlite3_column_text(statement, 1);
  2599. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2600. int product_id = sqlite3_column_int(statement, 2);
  2601. // char *url = (char*)sqlite3_column_text(statement, 3);
  2602. // if(url==nil)
  2603. // url="";
  2604. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2605. int wid = sqlite3_column_int(statement, 3);
  2606. int closeout = sqlite3_column_int(statement, 4);
  2607. int cid = sqlite3_column_int(statement, 5);
  2608. int wisdelete = sqlite3_column_int(statement, 6);
  2609. int more_color = sqlite3_column_int(statement, 7);
  2610. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2611. if(wid !=0 && wisdelete != 1)
  2612. [item setValue:@"true" forKey:@"wish_exists"];
  2613. else
  2614. [item setValue:@"false" forKey:@"wish_exists"];
  2615. if(closeout==0)
  2616. [item setValue:@"false" forKey:@"is_closeout"];
  2617. else
  2618. [item setValue:@"true" forKey:@"is_closeout"];
  2619. if(cid==0)
  2620. [item setValue:@"false" forKey:@"cart_exists"];
  2621. else
  2622. [item setValue:@"true" forKey:@"cart_exists"];
  2623. if (more_color == 0) {
  2624. [item setObject:@(false) forKey:@"more_color"];
  2625. } else if (more_color == 1) {
  2626. [item setObject:@(true) forKey:@"more_color"];
  2627. }
  2628. [item addEntriesFromDictionary:imgjson];
  2629. // [item setValue:nsurl forKey:@"img"];
  2630. [item setValue:nsname forKey:@"fash_name"];
  2631. [item setValue:nsdescription forKey:@"description"];
  2632. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2633. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2634. i++;
  2635. }
  2636. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2637. [ret setObject:items forKey:@"items"];
  2638. sqlite3_finalize(statement);
  2639. }
  2640. DebugLog(@"count:%d",count);
  2641. [iSalesDB close_db:db];
  2642. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  2643. return ret;
  2644. }
  2645. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  2646. {
  2647. return [self search:params limited:YES];
  2648. }
  2649. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  2650. {
  2651. UIApplication * app = [UIApplication sharedApplication];
  2652. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2653. appDelegate.disable_trigger=true;
  2654. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2655. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2656. appDelegate.disable_trigger=false;
  2657. //
  2658. // NSString* user = [params valueForKey:@"user"];
  2659. //
  2660. // NSString* password = [params valueForKey:@"password"];
  2661. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2662. ret[@"result"]=[NSNumber numberWithInt:2 ];
  2663. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  2664. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  2665. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  2666. [appDelegate update_count_mark];
  2667. appDelegate.can_show_price =false;
  2668. appDelegate.can_see_price =false;
  2669. appDelegate.can_create_portfolio =false;
  2670. appDelegate.can_create_order =false;
  2671. appDelegate.can_cancel_order =false;
  2672. appDelegate.can_set_cart_price =false;
  2673. appDelegate.can_delete_order =false;
  2674. appDelegate.can_submit_order =false;
  2675. appDelegate.can_set_tearsheet_price =false;
  2676. appDelegate.can_update_contact_info = false;
  2677. appDelegate.save_order_logout = false;
  2678. appDelegate.submit_order_logout = false;
  2679. appDelegate.alert_sold_in_quantities = false;
  2680. appDelegate.ipad_perm =nil ;
  2681. appDelegate.user_type = USER_ROLE_UNKNOWN;
  2682. appDelegate.OrderFilter= nil;
  2683. [appDelegate SetSo:nil];
  2684. [appDelegate set_main_button_panel];
  2685. // sqlite3 *db = [iSalesDB get_db];
  2686. //
  2687. //
  2688. //
  2689. //
  2690. //
  2691. // 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"]];
  2692. //
  2693. //
  2694. //
  2695. //
  2696. // DebugLog(@"offline_login sql:%@",sqlQuery);
  2697. // sqlite3_stmt * statement;
  2698. //
  2699. //
  2700. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2701. //
  2702. //
  2703. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2704. // {
  2705. //
  2706. //
  2707. // if (sqlite3_step(statement) == SQLITE_ROW)
  2708. // {
  2709. //
  2710. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2711. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2712. //
  2713. //
  2714. //
  2715. // int can_show_price = sqlite3_column_int(statement, 0);
  2716. // int can_see_price = sqlite3_column_int(statement, 1);
  2717. //
  2718. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2719. // if(contact_id==nil)
  2720. // contact_id="";
  2721. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2722. //
  2723. // int user_type = sqlite3_column_int(statement, 3);
  2724. //
  2725. // int can_cancel_order = sqlite3_column_int(statement, 4);
  2726. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  2727. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  2728. // int can_delete_order = sqlite3_column_int(statement, 7);
  2729. // int can_submit_order = sqlite3_column_int(statement, 8);
  2730. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2731. // int can_create_order = sqlite3_column_int(statement, 10);
  2732. //
  2733. //
  2734. // char *mode = (char*)sqlite3_column_text(statement, 11);
  2735. // if(mode==nil)
  2736. // mode="";
  2737. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2738. //
  2739. //
  2740. // char *username = (char*)sqlite3_column_text(statement, 12);
  2741. // if(username==nil)
  2742. // username="";
  2743. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2744. //
  2745. //
  2746. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2747. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2748. // [header setValue:nscontact_id forKey:@"contact_id"];
  2749. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2750. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2751. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2752. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2753. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2754. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2755. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2756. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2757. //
  2758. // [header setValue:nsusername forKey:@"username"];
  2759. //
  2760. //
  2761. // [ret setObject:header forKey:@"header"];
  2762. // [ret setValue:nsmode forKey:@"mode"];
  2763. //
  2764. //
  2765. // }
  2766. //
  2767. //
  2768. //
  2769. // sqlite3_finalize(statement);
  2770. // }
  2771. //
  2772. //
  2773. //
  2774. // [iSalesDB close_db:db];
  2775. //
  2776. //
  2777. //
  2778. //
  2779. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2780. return ret;
  2781. }
  2782. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  2783. {
  2784. UIApplication * app = [UIApplication sharedApplication];
  2785. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2786. appDelegate.disable_trigger=true;
  2787. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2788. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2789. appDelegate.disable_trigger=false;
  2790. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2791. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2792. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  2793. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  2794. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  2795. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  2796. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  2797. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  2798. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  2799. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  2800. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  2801. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  2802. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  2803. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  2804. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  2805. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  2806. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2807. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  2808. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  2809. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  2810. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  2811. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  2812. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  2813. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2814. [arr_name addObject:customer_first_name];
  2815. [arr_name addObject:customer_last_name];
  2816. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  2817. // default ship from
  2818. 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';";
  2819. __block NSString *cid = @"";
  2820. __block NSString *name = @"";
  2821. __block NSString *ext = @"";
  2822. __block NSString *contact = @"";
  2823. __block NSString *email = @"";
  2824. __block NSString *fax = @"";
  2825. __block NSString *phone = @"";
  2826. sqlite3 *db = [iSalesDB get_db];
  2827. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  2828. cid = [self textAtColumn:0 statement:statment];
  2829. name = [self textAtColumn:1 statement:statment];
  2830. ext = [self textAtColumn:2 statement:statment];
  2831. contact = [self textAtColumn:3 statement:statment];
  2832. email = [self textAtColumn:4 statement:statment];
  2833. fax = [self textAtColumn:5 statement:statment];
  2834. phone = [self textAtColumn:6 statement:statment];
  2835. }];
  2836. NSString* so_id = [self get_offline_soid:db];
  2837. if(so_id==nil)
  2838. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  2839. 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];
  2840. int result =[iSalesDB execSql:sql_neworder db:db];
  2841. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  2842. //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'
  2843. //soId
  2844. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  2845. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  2846. sqlite3_stmt * statement;
  2847. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2848. {
  2849. if (sqlite3_step(statement) == SQLITE_ROW)
  2850. {
  2851. // char *name = (char*)sqlite3_column_text(statement, 1);
  2852. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2853. //ret = sqlite3_column_int(statement, 0);
  2854. char *soId = (char*)sqlite3_column_text(statement, 0);
  2855. if(soId==nil)
  2856. soId="";
  2857. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  2858. [ret setValue:nssoId forKey:@"soId"];
  2859. [ret setValue:nssoId forKey:@"orderCode"];
  2860. }
  2861. sqlite3_finalize(statement);
  2862. }
  2863. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  2864. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2865. [iSalesDB close_db:db];
  2866. return [RAConvertor dict2data:ret];
  2867. }
  2868. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  2869. {
  2870. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2871. NSString* orderCode = [params valueForKey:@"orderCode"];
  2872. UIApplication * app = [UIApplication sharedApplication];
  2873. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2874. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  2875. {
  2876. appDelegate.disable_trigger=true;
  2877. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2878. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2879. appDelegate.disable_trigger=false;
  2880. }
  2881. sqlite3 *db = [iSalesDB get_db];
  2882. int cart_count=[self query_ordercartcount:orderCode db:db];
  2883. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2884. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2885. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2886. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2887. [iSalesDB close_db:db];
  2888. return [RAConvertor dict2data:ret];
  2889. }
  2890. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  2891. {
  2892. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2893. sqlite3 *db = [iSalesDB get_db];
  2894. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2895. int count =0;
  2896. if(params[@"count"]!=nil)
  2897. {
  2898. count = [params[@"count"] intValue];
  2899. }
  2900. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  2901. NSString* product_id=params[@"product_id"];
  2902. NSString* orderCode=params[@"orderCode"];
  2903. NSString *qty = params[@"qty"];
  2904. NSArray* arr_id=[RAConvertor string2arr:product_id separator:@","];
  2905. NSArray *qty_arr = [RAConvertor string2arr:qty separator:@","];
  2906. __block int number_of_outOfStock = 0;
  2907. for(int i=0;i<arr_id.count;i++)
  2908. {
  2909. NSInteger item_qty= count;
  2910. if (qty) {
  2911. item_qty = [qty_arr[i] integerValue];
  2912. }
  2913. if(item_qty==0)
  2914. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  2915. // 检查新加Model数量是否大于库存
  2916. if (!appDelegate.can_create_backorder) {
  2917. __block BOOL needContinue = NO;
  2918. [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) {
  2919. int availability = sqlite3_column_int(stmt, 0);
  2920. // 库存小于购买量为缺货
  2921. if (availability < item_qty || availability <= 0) {
  2922. number_of_outOfStock++;
  2923. needContinue = YES;
  2924. }
  2925. }];
  2926. if (needContinue) {
  2927. continue;
  2928. }
  2929. }
  2930. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  2931. NSString* sql=nil;
  2932. sqlite3_stmt *stmt = nil;
  2933. BOOL shouldStep = NO;
  2934. if(_id<0)
  2935. {
  2936. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  2937. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  2938. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2939. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  2940. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  2941. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  2942. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  2943. shouldStep = YES;
  2944. }
  2945. else
  2946. {
  2947. if (qty) { // wish list move to cart
  2948. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  2949. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2950. sqlite3_bind_int(stmt, 1, _id);
  2951. shouldStep = YES;
  2952. } else {
  2953. 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];
  2954. __block BOOL update = YES;
  2955. if (!appDelegate.can_create_backorder) {
  2956. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2957. int newQTY = sqlite3_column_int(stmt, 0);
  2958. int availability = sqlite3_column_int(stmt, 1);
  2959. if (newQTY > availability) { // 库存不够
  2960. update = NO;
  2961. number_of_outOfStock++;
  2962. }
  2963. }];
  2964. }
  2965. if (update) {
  2966. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  2967. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2968. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  2969. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  2970. shouldStep = YES;
  2971. }
  2972. }
  2973. }
  2974. if (shouldStep) {
  2975. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2976. [iSalesDB execSql:@"ROLLBACK" db:db];
  2977. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2978. [iSalesDB close_db:db];
  2979. DebugLog(@"add to cart error");
  2980. return [RAConvertor dict2data:ret];
  2981. }
  2982. }
  2983. }
  2984. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2985. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2986. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2987. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2988. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2989. if (!appDelegate.can_create_backorder) {
  2990. if (number_of_outOfStock > 0) {
  2991. ret[@"result"]=[NSNumber numberWithInt:8];
  2992. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  2993. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  2994. }
  2995. }
  2996. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2997. [iSalesDB close_db:db];
  2998. return [RAConvertor dict2data:ret];
  2999. }
  3000. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID
  3001. {
  3002. // UIApplication * app = [UIApplication sharedApplication];
  3003. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3004. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3005. 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];
  3006. sqlite3_stmt * statement;
  3007. int count=0;
  3008. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3009. {
  3010. while (sqlite3_step(statement) == SQLITE_ROW)
  3011. {
  3012. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  3013. int bitem_id = sqlite3_column_int(statement, 0);
  3014. int bitem_qty = sqlite3_column_int(statement, 1);
  3015. char *name = (char*)sqlite3_column_text(statement, 2);
  3016. if(name==nil)
  3017. name="";
  3018. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  3019. char *description = (char*)sqlite3_column_text(statement, 3);
  3020. if(description==nil)
  3021. description="";
  3022. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  3023. double unit_price = sqlite3_column_double(statement, 4);
  3024. int use_unitprice = sqlite3_column_int(statement, 5);
  3025. if(use_unitprice!=1)
  3026. {
  3027. unit_price=[self get_model_default_price:contactID product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  3028. }
  3029. itemjson[@"model"]=nsname;
  3030. itemjson[@"description"]=nsdescription;
  3031. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  3032. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  3033. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  3034. if(compute)
  3035. {
  3036. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID];
  3037. }
  3038. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3039. count++;
  3040. }
  3041. sqlite3_finalize(statement);
  3042. }
  3043. ret[@"count"]=@(count);
  3044. if(count==0)
  3045. return nil;
  3046. else
  3047. return ret;
  3048. }
  3049. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  3050. {
  3051. //compute: add part to subtotal;
  3052. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  3053. dict_item[@(item_id)]=@"1";
  3054. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3055. double cuft=0;
  3056. double weight=0;
  3057. int carton=0;
  3058. 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];
  3059. sqlite3_stmt * statement;
  3060. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3061. {
  3062. if (sqlite3_step(statement) == SQLITE_ROW)
  3063. {
  3064. double ulength = sqlite3_column_double(statement, 0);
  3065. double uwidth = sqlite3_column_double(statement, 1);
  3066. double uheight = sqlite3_column_double(statement, 2);
  3067. double uweight = sqlite3_column_double(statement, 3);
  3068. double mlength = sqlite3_column_double(statement, 4);
  3069. double mwidth = sqlite3_column_double(statement, 5);
  3070. double mheight = sqlite3_column_double(statement, 6);
  3071. double mweight = sqlite3_column_double(statement, 7);
  3072. double ilength = sqlite3_column_double(statement, 8);
  3073. double iwidth = sqlite3_column_double(statement, 9);
  3074. double iheight = sqlite3_column_double(statement, 10);
  3075. double iweight = sqlite3_column_double(statement, 11);
  3076. // int pcs = sqlite3_column_int(statement,12);
  3077. int mpack = sqlite3_column_int(statement, 13);
  3078. int ipack = sqlite3_column_int(statement, 14);
  3079. double ucbf = sqlite3_column_double(statement, 15);
  3080. // double icbf = sqlite3_column_double(statement, 16);
  3081. // double mcbf = sqlite3_column_double(statement, 17);
  3082. if(ipack==0)
  3083. {
  3084. carton= count/mpack ;
  3085. weight = mweight*carton;
  3086. cuft= carton*(mlength*mwidth*mheight);
  3087. int remain=count%mpack;
  3088. if(remain==0)
  3089. {
  3090. //do nothing;
  3091. }
  3092. else
  3093. {
  3094. carton++;
  3095. weight += uweight*remain;
  3096. cuft += (ulength*uwidth*uheight)*remain;
  3097. }
  3098. }
  3099. else
  3100. {
  3101. carton = count/(mpack*ipack);
  3102. weight = mweight*carton;
  3103. cuft= carton*(mlength*mwidth*mheight);
  3104. int remain=count%(mpack*ipack);
  3105. if(remain==0)
  3106. {
  3107. // do nothing;
  3108. }
  3109. else
  3110. {
  3111. carton++;
  3112. int icarton =remain/ipack;
  3113. int iremain=remain%ipack;
  3114. weight += iweight*icarton;
  3115. cuft += (ilength*iwidth*iheight)*icarton;
  3116. if(iremain==0)
  3117. {
  3118. //do nothing;
  3119. }
  3120. else
  3121. {
  3122. weight += uweight*iremain;
  3123. cuft += (ulength*uwidth*uheight)*iremain;
  3124. }
  3125. }
  3126. }
  3127. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)
  3128. cuft=ucbf*count;
  3129. weight= uweight*count;
  3130. #endif
  3131. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  3132. cuft=ucbf*count;
  3133. weight= uweight*count;
  3134. #endif
  3135. }
  3136. sqlite3_finalize(statement);
  3137. }
  3138. if(compute)
  3139. {
  3140. NSArray * arr_count=nil;
  3141. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  3142. for(int i=0;i<arr_bundle.count;i++)
  3143. {
  3144. dict_item[arr_bundle[i]]=@"1";
  3145. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  3146. cuft+=[bundlejson[@"cuft"] doubleValue];
  3147. weight+=[bundlejson[@"weight"] doubleValue];
  3148. carton+=[bundlejson[@"carton"] intValue];
  3149. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  3150. }
  3151. }
  3152. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  3153. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  3154. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  3155. ret[@"items"]=dict_item;
  3156. return ret;
  3157. }
  3158. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  3159. {
  3160. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  3161. // 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 ];
  3162. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3163. DebugLog(@"offline_login sql:%@",sqlQuery);
  3164. sqlite3_stmt * statement;
  3165. int cart_count=0;
  3166. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3167. if ( dbresult== SQLITE_OK)
  3168. {
  3169. while (sqlite3_step(statement) == SQLITE_ROW)
  3170. {
  3171. int item_id = sqlite3_column_int(statement, 0);
  3172. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  3173. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3174. }
  3175. sqlite3_finalize(statement);
  3176. }
  3177. return cart_count;
  3178. }
  3179. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  3180. {
  3181. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3182. sqlite3 *db = [iSalesDB get_db];
  3183. UIApplication * app = [UIApplication sharedApplication];
  3184. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3185. NSString* orderCode=params[@"orderCode"];
  3186. int sort = [[params objectForKey:@"sort"] intValue];
  3187. NSString *sort_str = @"";
  3188. switch (sort) {
  3189. case 0:{
  3190. sort_str = @"order by c.modify_time desc";
  3191. }
  3192. break;
  3193. case 1:{
  3194. sort_str = @"order by c.modify_time asc";
  3195. }
  3196. break;
  3197. case 2:{
  3198. sort_str = @"order by m.name asc";
  3199. }
  3200. break;
  3201. case 3:{
  3202. sort_str = @"order by m.name desc";
  3203. }
  3204. break;
  3205. case 4:{
  3206. sort_str = @"order by m.description asc";
  3207. }
  3208. break;
  3209. default:
  3210. break;
  3211. }
  3212. 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 ];
  3213. // 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 ];
  3214. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3215. DebugLog(@"offline_login sql:%@",sqlQuery);
  3216. sqlite3_stmt * statement;
  3217. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3218. NSDate *date1 = [NSDate date];
  3219. int count=0;
  3220. int cart_count=0;
  3221. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3222. if ( dbresult== SQLITE_OK)
  3223. {
  3224. while (sqlite3_step(statement) == SQLITE_ROW)
  3225. {
  3226. // NSDate *row_date = [NSDate date];
  3227. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  3228. int product_id = sqlite3_column_int(statement, 0);
  3229. char *str_price = (char*)sqlite3_column_text(statement, 1);
  3230. int item_id = sqlite3_column_int(statement, 7);
  3231. NSString* Price=nil;
  3232. if(str_price==nil)
  3233. {
  3234. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3235. // NSDate *price_date = [NSDate date];
  3236. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3237. // DebugLog(@"price time interval");
  3238. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  3239. if(price==nil)
  3240. Price=@"No Price.";
  3241. else
  3242. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3243. }
  3244. else
  3245. {
  3246. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  3247. }
  3248. double discount = sqlite3_column_double(statement, 2);
  3249. int item_count = sqlite3_column_int(statement, 3);
  3250. char *line_note = (char*)sqlite3_column_text(statement, 4);
  3251. NSString *nsline_note=nil;
  3252. if(line_note!=nil)
  3253. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  3254. char *name = (char*)sqlite3_column_text(statement, 5);
  3255. NSString *nsname=nil;
  3256. if(name!=nil)
  3257. nsname= [[NSString alloc]initWithUTF8String:name];
  3258. char *description = (char*)sqlite3_column_text(statement, 6);
  3259. NSString *nsdescription=nil;
  3260. if(description!=nil)
  3261. nsdescription= [[NSString alloc]initWithUTF8String:description];
  3262. int stockUom = sqlite3_column_int(statement, 8);
  3263. int _id = sqlite3_column_int(statement, 9);
  3264. int availability = sqlite3_column_int(statement, 10);
  3265. // NSDate *subtotal_date = [NSDate date];
  3266. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  3267. // DebugLog(@"subtotal_date time interval");
  3268. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  3269. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  3270. double weight=[bsubtotaljson[@"weight"] doubleValue];
  3271. int carton=[bsubtotaljson[@"carton"] intValue];
  3272. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3273. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  3274. // NSDate *img_date = [NSDate date];
  3275. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  3276. // DebugLog(@"img_date time interval");
  3277. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  3278. itemjson[@"model"]=nsname;
  3279. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  3280. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  3281. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  3282. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  3283. itemjson[@"check"]=@"true";
  3284. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  3285. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  3286. itemjson[@"unit_price"]=Price;
  3287. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  3288. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  3289. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  3290. itemjson[@"note"]=nsline_note;
  3291. if (!appDelegate.can_create_backorder) {
  3292. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  3293. }
  3294. // NSDate *date2 = [NSDate date];
  3295. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  3296. // DebugLog(@"model_bundle time interval");
  3297. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  3298. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3299. count++;
  3300. // DebugLog(@"row time interval");
  3301. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  3302. }
  3303. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3304. sqlite3_finalize(statement);
  3305. }
  3306. DebugLog(@"request cart total time interval");
  3307. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3308. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3309. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  3310. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3311. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3312. ret[@"count"]=[NSNumber numberWithInt:count ];
  3313. ret[@"mode"]=@"Regular Mode";
  3314. [iSalesDB close_db:db];
  3315. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  3316. DebugLog(@"general notes :%@",general_note);
  3317. ret[@"general_note"]= general_note;
  3318. return [RAConvertor dict2data:ret];
  3319. }
  3320. +(NSData*) offline_login :(NSMutableDictionary *) params
  3321. {
  3322. NSString* user = [params valueForKey:@"user"];
  3323. NSString* password = [params valueForKey:@"password"];
  3324. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3325. sqlite3 *db = [iSalesDB get_db];
  3326. 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"]];
  3327. DebugLog(@"offline_login sql:%@",sqlQuery);
  3328. sqlite3_stmt * statement;
  3329. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3330. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3331. {
  3332. if (sqlite3_step(statement) == SQLITE_ROW)
  3333. {
  3334. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3335. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3336. int can_show_price = sqlite3_column_int(statement, 0);
  3337. int can_see_price = sqlite3_column_int(statement, 1);
  3338. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3339. if(contact_id==nil)
  3340. contact_id="";
  3341. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3342. int user_type = sqlite3_column_int(statement, 3);
  3343. int can_cancel_order = sqlite3_column_int(statement, 4);
  3344. int can_set_cart_price = sqlite3_column_int(statement, 5);
  3345. int can_create_portfolio = sqlite3_column_int(statement, 6);
  3346. int can_delete_order = sqlite3_column_int(statement, 7);
  3347. int can_submit_order = sqlite3_column_int(statement, 8);
  3348. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3349. int can_create_order = sqlite3_column_int(statement, 10);
  3350. char *mode = (char*)sqlite3_column_text(statement, 11);
  3351. if(mode==nil)
  3352. mode="";
  3353. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3354. char *username = (char*)sqlite3_column_text(statement, 12);
  3355. if(username==nil)
  3356. username="";
  3357. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3358. int can_update_contact_info = sqlite3_column_int(statement, 13);
  3359. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3360. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3361. [header setValue:nscontact_id forKey:@"contact_id"];
  3362. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3363. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3364. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3365. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3366. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3367. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3368. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3369. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3370. [header setObject:@(YES) forKey:@"can_create_backorder"];
  3371. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  3372. [header setValue:nsusername forKey:@"username"];
  3373. NSError* error=nil;
  3374. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  3375. [header setValue:statusFilter forKey:@"statusFilter"];
  3376. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3377. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  3378. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  3379. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  3380. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  3381. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  3382. [ret setObject:header forKey:@"header"];
  3383. [ret setValue:nsmode forKey:@"mode"];
  3384. }
  3385. sqlite3_finalize(statement);
  3386. }
  3387. [iSalesDB close_db:db];
  3388. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3389. return [RAConvertor dict2data:ret];
  3390. }
  3391. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  3392. {
  3393. NSString* contactId = [params valueForKey:@"contactId"];
  3394. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3395. sqlite3 *db = [iSalesDB get_db];
  3396. NSString *sqlQuery = nil;
  3397. {
  3398. 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];
  3399. }
  3400. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3401. sqlite3_stmt * statement;
  3402. [ret setValue:@"2" forKey:@"result"];
  3403. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3404. {
  3405. //int i = 0;
  3406. if (sqlite3_step(statement) == SQLITE_ROW)
  3407. {
  3408. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3409. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3410. // int editable = sqlite3_column_int(statement, 0);
  3411. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3412. NSString *nscompany_name =nil;
  3413. if(company_name==nil)
  3414. nscompany_name=@"";
  3415. else
  3416. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  3417. char *country = (char*)sqlite3_column_text(statement, 2);
  3418. if(country==nil)
  3419. country="";
  3420. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3421. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3422. // if(addr==nil)
  3423. // addr="";
  3424. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3425. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3426. if(zipcode==nil)
  3427. zipcode="";
  3428. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3429. char *state = (char*)sqlite3_column_text(statement, 5);
  3430. if(state==nil)
  3431. state="";
  3432. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3433. char *city = (char*)sqlite3_column_text(statement, 6);
  3434. if(city==nil)
  3435. city="";
  3436. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3437. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3438. // NSString *nscontact_name = nil;
  3439. // if(contact_name==nil)
  3440. // nscontact_name=@"";
  3441. // else
  3442. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3443. char *phone = (char*)sqlite3_column_text(statement, 8);
  3444. NSString *nsphone = nil;
  3445. if(phone==nil)
  3446. nsphone=@"";
  3447. else
  3448. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3449. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3450. // if(contact_id==nil)
  3451. // contact_id="";
  3452. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3453. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3454. if(addr_1==nil)
  3455. addr_1="";
  3456. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3457. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3458. if(addr_2==nil)
  3459. addr_2="";
  3460. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3461. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3462. if(addr_3==nil)
  3463. addr_3="";
  3464. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3465. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3466. if(addr_4==nil)
  3467. addr_4="";
  3468. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3469. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3470. if(first_name==nil)
  3471. first_name="";
  3472. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3473. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3474. if(last_name==nil)
  3475. last_name="";
  3476. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3477. char *fax = (char*)sqlite3_column_text(statement, 16);
  3478. NSString *nsfax = nil;
  3479. if(fax==nil)
  3480. nsfax=@"";
  3481. else
  3482. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3483. char *email = (char*)sqlite3_column_text(statement, 17);
  3484. NSString *nsemail = nil;
  3485. if(email==nil)
  3486. nsemail=@"";
  3487. else
  3488. nsemail= [[NSString alloc]initWithUTF8String:email];
  3489. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  3490. NSString *nsimg_0 = nil;
  3491. if(img_0==nil)
  3492. nsimg_0=@"";
  3493. else
  3494. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  3495. [self copy_bcardImg:nsimg_0];
  3496. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  3497. NSString *nsimg_1 = nil;
  3498. if(img_1==nil)
  3499. nsimg_1=@"";
  3500. else
  3501. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  3502. [self copy_bcardImg:nsimg_1];
  3503. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  3504. NSString *nsimg_2 = nil;
  3505. if(img_2==nil)
  3506. nsimg_2=@"";
  3507. else
  3508. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  3509. [self copy_bcardImg:nsimg_2];
  3510. char *price_type = (char*)sqlite3_column_text(statement, 21);
  3511. NSString *nsprice_type = nil;
  3512. if(price_type==nil)
  3513. nsprice_type=@"";
  3514. else
  3515. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  3516. char *notes = (char*)sqlite3_column_text(statement, 22);
  3517. NSString *nsnotes = nil;
  3518. if(notes==nil)
  3519. nsnotes=@"";
  3520. else
  3521. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  3522. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  3523. NSString *nssalesrep = nil;
  3524. if(salesrep==nil)
  3525. nssalesrep=@"";
  3526. else
  3527. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  3528. NSString *contact_type = [self textAtColumn:24 statement:statement];
  3529. {
  3530. // decrypt
  3531. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3532. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3533. nsphone=[AESCrypt fastdecrypt:nsphone];
  3534. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3535. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3536. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3537. }
  3538. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3539. [arr_name addObject:nsfirst_name];
  3540. [arr_name addObject:nslast_name];
  3541. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  3542. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  3543. [arr_ext addObject:nsaddr_1];
  3544. [arr_ext addObject:nsaddr_2];
  3545. [arr_ext addObject:nsaddr_3];
  3546. [arr_ext addObject:nsaddr_4];
  3547. [arr_ext addObject:@"\r\n"];
  3548. [arr_ext addObject:nscity];
  3549. [arr_ext addObject:nsstate];
  3550. [arr_ext addObject:nszipcode];
  3551. [arr_ext addObject:nscountry];
  3552. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  3553. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  3554. [item setValue:nsimg_2 forKey:@"business_card_2"];
  3555. [item setValue:nsimg_0 forKey:@"business_card_0"];
  3556. [item setValue:nscountry forKey:@"customer_country"];
  3557. [item setValue:nsphone forKey:@"customer_phone"];
  3558. [item setValue:nsimg_1 forKey:@"business_card_1"];
  3559. [item setValue:nscompany_name forKey:@"customer_name"];
  3560. [item setValue:nsprice_type forKey:@"customer_price_type"];
  3561. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  3562. [item setValue:nsext forKey:@"customer_contact_ext"];
  3563. [item setValue:nszipcode forKey:@"customer_zipcode"];
  3564. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  3565. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  3566. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  3567. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  3568. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  3569. [item setValue:nslast_name forKey:@"customer_last_name"];
  3570. [item setValue:nscity forKey:@"customer_city"];
  3571. [item setValue:nsstate forKey:@"customer_state"];
  3572. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  3573. [item setValue:contactId forKey:@"customer_cid"];
  3574. [item setValue:nscontact_name forKey:@"customer_contact"];
  3575. [item setValue:nsfax forKey:@"customer_fax"];
  3576. [item setValue:nsemail forKey:@"customer_email"];
  3577. [item setValue:contact_type forKey:@"customer_contact_type"];
  3578. [ret setObject:item forKey:@"customerInfo"];
  3579. // i++;
  3580. }
  3581. UIApplication * app = [UIApplication sharedApplication];
  3582. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3583. [ret setValue:appDelegate.mode forKey:@"mode"];
  3584. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  3585. sqlite3_finalize(statement);
  3586. }
  3587. [iSalesDB close_db:db];
  3588. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3589. return ret;
  3590. }
  3591. + (bool) copy_bcardImg:(NSString*) filename
  3592. {
  3593. if(filename.length==0)
  3594. return false;
  3595. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3596. bool ret=false;
  3597. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3598. NSString *cachefolder = [paths objectAtIndex:0];
  3599. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  3600. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3601. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3602. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  3603. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  3604. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3605. //
  3606. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3607. NSFileManager* fileManager = [NSFileManager defaultManager];
  3608. BOOL bdir=NO;
  3609. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  3610. {
  3611. NSError *error = nil;
  3612. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  3613. {
  3614. ret=false;
  3615. }
  3616. else
  3617. {
  3618. ret=true;
  3619. }
  3620. // NSError *error = nil;
  3621. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3622. //
  3623. // if(!bsuccess)
  3624. // {
  3625. // DebugLog(@"Create offline_createimg folder failed");
  3626. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3627. // return [RAConvertor dict2data:ret];
  3628. // }
  3629. // if(bsuccess)
  3630. // {
  3631. // sqlite3 *db = [self get_db];
  3632. //
  3633. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3634. // [iSalesDB close_db:db];
  3635. // }
  3636. }
  3637. return ret;
  3638. //
  3639. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3640. // if(bsuccess)
  3641. // {
  3642. // NSError *error = nil;
  3643. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3644. // {
  3645. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3646. // }
  3647. // else
  3648. // {
  3649. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3650. //
  3651. // ret[@"img_url_aname"]=filename;
  3652. // ret[@"img_url"]=savedImagePath;
  3653. // }
  3654. // }
  3655. }
  3656. +(NSData *) offline_saveBusinesscard:(NSData *) image
  3657. {
  3658. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3659. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3660. NSString *cachefolder = [paths objectAtIndex:0];
  3661. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3662. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3663. NSFileManager* fileManager = [NSFileManager defaultManager];
  3664. BOOL bdir=YES;
  3665. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  3666. {
  3667. NSError *error = nil;
  3668. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3669. if(!bsuccess)
  3670. {
  3671. DebugLog(@"Create offline_createimg folder failed");
  3672. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3673. return [RAConvertor dict2data:ret];
  3674. }
  3675. // if(bsuccess)
  3676. // {
  3677. // sqlite3 *db = [self get_db];
  3678. //
  3679. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3680. // [iSalesDB close_db:db];
  3681. // }
  3682. }
  3683. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  3684. //JEPG格式
  3685. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  3686. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  3687. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3688. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3689. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3690. if(bsuccess)
  3691. {
  3692. NSError *error = nil;
  3693. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3694. {
  3695. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3696. }
  3697. else
  3698. {
  3699. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3700. ret[@"img_url_aname"]=filename;
  3701. ret[@"img_url"]=filename;
  3702. }
  3703. }
  3704. else
  3705. {
  3706. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3707. }
  3708. return [RAConvertor dict2data:ret];
  3709. }
  3710. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  3711. {
  3712. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  3713. category = [category substringToIndex:3];
  3714. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3715. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  3716. params[@"category"]= category;
  3717. ret[@"params"]= params;
  3718. [ret setValue:@"detail" forKey:@"target"];
  3719. [ret setValue:@"popup" forKey:@"action"];
  3720. [ret setValue:@"content" forKey:@"type"];
  3721. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  3722. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3723. [ret setValue:@"true" forKey:@"single_row"];
  3724. [ret setValue:@"true" forKey:@"partial_refresh"];
  3725. // sqlite3 *db = [iSalesDB get_db];
  3726. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3727. 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 ;
  3728. sqlite3_stmt * statement;
  3729. int count = 0;
  3730. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  3731. // int count=0;
  3732. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3733. {
  3734. int i=0;
  3735. while (sqlite3_step(statement) == SQLITE_ROW)
  3736. {
  3737. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3738. // char *name = (char*)sqlite3_column_text(statement, 1);
  3739. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3740. char *name = (char*)sqlite3_column_text(statement, 0);
  3741. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3742. int product_id = sqlite3_column_int(statement, 1);
  3743. char *url = (char*)sqlite3_column_text(statement, 2);
  3744. if(url==nil)
  3745. url="";
  3746. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3747. [item setValue:nsurl forKey:@"picture_path"];
  3748. [item setValue:nsname forKey:@"fash_name"];
  3749. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3750. [item setValue:category forKey:@"category"];
  3751. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3752. i++;
  3753. }
  3754. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3755. sqlite3_finalize(statement);
  3756. }
  3757. DebugLog(@"count:%d",count);
  3758. // [iSalesDB close_db:db];
  3759. return ret;
  3760. }
  3761. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  3762. {
  3763. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3764. [ret setValue:key forKey:@"key"];
  3765. [ret setValue:value forKey:@"val"];
  3766. [ret setValue:@"price" forKey:@"type"];
  3767. return ret;
  3768. }
  3769. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  3770. {
  3771. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3772. [ret setValue:key forKey:@"key"];
  3773. [ret setValue:value forKey:@"val"];
  3774. return ret;
  3775. }
  3776. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  3777. {
  3778. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3779. [ret setValue:@"0" forKey:@"img_count"];
  3780. // sqlite3 *db = [iSalesDB get_db];
  3781. 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 ;
  3782. sqlite3_stmt * statement;
  3783. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3784. {
  3785. int i=0;
  3786. if (sqlite3_step(statement) == SQLITE_ROW)
  3787. {
  3788. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3789. // char *name = (char*)sqlite3_column_text(statement, 1);
  3790. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3791. char *value = (char*)sqlite3_column_text(statement, 0);
  3792. if(value==nil)
  3793. value="";
  3794. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3795. char *key = (char*)sqlite3_column_text(statement, 1);
  3796. if(key==nil)
  3797. key="";
  3798. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  3799. [item setValue:nsvalue forKey:@"val"];
  3800. [item setValue:nskey forKey:@"key"];
  3801. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3802. [ret setValue:@"1" forKey:@"count"];
  3803. i++;
  3804. }
  3805. sqlite3_finalize(statement);
  3806. }
  3807. // [iSalesDB close_db:db];
  3808. return ret;
  3809. }
  3810. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  3811. {
  3812. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3813. [ret setValue:@"0" forKey:@"count"];
  3814. // sqlite3 *db = [iSalesDB get_db];
  3815. 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;
  3816. sqlite3_stmt * statement;
  3817. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3818. {
  3819. int i=0;
  3820. while (sqlite3_step(statement) == SQLITE_ROW)
  3821. {
  3822. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3823. // char *name = (char*)sqlite3_column_text(statement, 1);
  3824. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3825. char *value = (char*)sqlite3_column_text(statement, 0);
  3826. if(value==nil)
  3827. value="";
  3828. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3829. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  3830. if(selector_display==nil)
  3831. selector_display="";
  3832. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  3833. int product_id = sqlite3_column_int(statement, 2);
  3834. char *category = (char*)sqlite3_column_text(statement, 3);
  3835. if(category==nil)
  3836. category="";
  3837. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  3838. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  3839. [item setValue:nsvalue forKey:@"title"];
  3840. [item setValue:url forKey:@"pic_url"];
  3841. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  3842. [params setValue:@"2" forKey:@"count"];
  3843. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  3844. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  3845. [param0 setValue:@"product_id" forKey:@"name"];
  3846. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  3847. [param1 setValue:nscategory forKey:@"val"];
  3848. [param1 setValue:@"category" forKey:@"name"];
  3849. [params setObject:param0 forKey:@"param_0"];
  3850. [params setObject:param1 forKey:@"param_1"];
  3851. [item setObject:params forKey:@"params"];
  3852. [ret setValue:nsselector_display forKey:@"name"];
  3853. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3854. i++;
  3855. }
  3856. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3857. [ret setValue:@"switch" forKey:@"action"];
  3858. sqlite3_finalize(statement);
  3859. }
  3860. // [iSalesDB close_db:db];
  3861. return ret;
  3862. }
  3863. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  3864. {
  3865. // model 在 category search 显示的图片。
  3866. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  3867. // sqlite3 *db = [iSalesDB get_db];
  3868. 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];
  3869. sqlite3_stmt * statement;
  3870. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3871. {
  3872. while (sqlite3_step(statement) == SQLITE_ROW)
  3873. {
  3874. char *url = (char*)sqlite3_column_text(statement, 0);
  3875. if(url==nil)
  3876. url="";
  3877. int type = sqlite3_column_int(statement, 1);
  3878. if(type==0)
  3879. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  3880. else
  3881. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  3882. }
  3883. sqlite3_finalize(statement);
  3884. }
  3885. // [iSalesDB close_db:db];
  3886. return ret;
  3887. }
  3888. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  3889. {
  3890. int item_id=-1;
  3891. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  3892. sqlite3_stmt * statement;
  3893. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3894. {
  3895. if (sqlite3_step(statement) == SQLITE_ROW)
  3896. {
  3897. item_id = sqlite3_column_int(statement, 0);
  3898. }
  3899. sqlite3_finalize(statement);
  3900. }
  3901. return item_id;
  3902. }
  3903. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  3904. {
  3905. // NSString* ret = @"";
  3906. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  3907. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  3908. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  3909. sqlite3_stmt * statement;
  3910. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3911. {
  3912. while (sqlite3_step(statement) == SQLITE_ROW)
  3913. {
  3914. int bitem_id = sqlite3_column_int(statement, 0);
  3915. int bitem_qty = sqlite3_column_int(statement, 1);
  3916. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  3917. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  3918. }
  3919. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  3920. sqlite3_finalize(statement);
  3921. }
  3922. // if(ret==nil)
  3923. // ret=@"";
  3924. *count=arr_count;
  3925. return arr_bundle;
  3926. }
  3927. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  3928. {
  3929. // get default sold qty, return -1 if model not found;
  3930. int ret = -1;
  3931. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  3932. sqlite3_stmt * statement;
  3933. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3934. {
  3935. if (sqlite3_step(statement) == SQLITE_ROW)
  3936. {
  3937. ret = sqlite3_column_int(statement, 0);
  3938. }
  3939. sqlite3_finalize(statement);
  3940. }
  3941. return ret;
  3942. }
  3943. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  3944. {
  3945. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3946. [ret setValue:@"0" forKey:@"img_count"];
  3947. // sqlite3 *db = [iSalesDB get_db];
  3948. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  3949. sqlite3_stmt * statement;
  3950. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3951. {
  3952. int i=0;
  3953. while (sqlite3_step(statement) == SQLITE_ROW)
  3954. {
  3955. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3956. // char *name = (char*)sqlite3_column_text(statement, 1);
  3957. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3958. char *url = (char*)sqlite3_column_text(statement, 0);
  3959. if(url==nil)
  3960. url="";
  3961. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3962. [item setValue:nsurl forKey:@"s"];
  3963. [item setValue:nsurl forKey:@"l"];
  3964. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  3965. i++;
  3966. }
  3967. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  3968. sqlite3_finalize(statement);
  3969. }
  3970. // [iSalesDB close_db:db];
  3971. return ret;
  3972. }
  3973. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  3974. UIApplication * app = [UIApplication sharedApplication];
  3975. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3976. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3977. sqlite3 *db = [iSalesDB get_db];
  3978. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3979. NSString* product_id=params[@"product_id"];
  3980. NSString *item_count_str = params[@"item_count"];
  3981. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  3982. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  3983. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3984. // NSString *sql = @"";
  3985. for(int i=0;i<arr.count;i++)
  3986. {
  3987. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  3988. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  3989. __block int cart_count = 0;
  3990. if (!item_count_str) {
  3991. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  3992. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3993. NSString *model_set = [self textAtColumn:0 statement:stmt];
  3994. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  3995. cart_count = [[model_set_components lastObject] intValue];
  3996. }];
  3997. }
  3998. if(count==0)
  3999. {
  4000. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  4001. sqlite3_stmt *stmt;
  4002. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  4003. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  4004. if (item_count_arr) {
  4005. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  4006. } else {
  4007. sqlite3_bind_int(stmt,2,cart_count);
  4008. }
  4009. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  4010. [iSalesDB execSql:@"ROLLBACK" db:db];
  4011. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  4012. [iSalesDB close_db:db];
  4013. DebugLog(@"add to wishlist error");
  4014. return ret;
  4015. }
  4016. } else {
  4017. int qty = 0;
  4018. if (item_count_arr) {
  4019. qty = [item_count_arr[i] intValue];
  4020. } else {
  4021. qty = cart_count;
  4022. }
  4023. 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]];
  4024. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  4025. [iSalesDB execSql:@"ROLLBACK" db:db];
  4026. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  4027. [iSalesDB close_db:db];
  4028. DebugLog(@"add to wishlist error");
  4029. return ret;
  4030. }
  4031. }
  4032. }
  4033. // [iSalesDB execSql:sql db:db];
  4034. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4035. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4036. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  4037. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  4038. [iSalesDB close_db:db];
  4039. appDelegate.wish_count =count;
  4040. [appDelegate update_count_mark];
  4041. ret[@"result"]= [NSNumber numberWithInt:2];
  4042. return ret;
  4043. }
  4044. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  4045. // 0 category
  4046. // 1 search
  4047. // 2 itemsearch
  4048. NSData *ret = nil;
  4049. NSDictionary *items = nil;
  4050. switch (from) {
  4051. case 0:{
  4052. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  4053. }
  4054. break;
  4055. case 1:{
  4056. items = [[self search:params limited:NO] objectForKey:@"items"];
  4057. }
  4058. break;
  4059. case 2:{
  4060. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  4061. }
  4062. break;
  4063. default:
  4064. break;
  4065. }
  4066. if (!items) {
  4067. return ret;
  4068. }
  4069. int count = [[items objectForKey:@"count"] intValue];
  4070. NSMutableString *product_id_str = [@"" mutableCopy];
  4071. for (int i = 0; i < count; i++) {
  4072. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  4073. NSDictionary *item = [items objectForKey:key];
  4074. NSString *product_id = [item objectForKey:@"product_id"];
  4075. if (i == 0) {
  4076. [product_id_str appendString:product_id];
  4077. } else {
  4078. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  4079. }
  4080. }
  4081. NSString *add_to = [self valueInParams:params key:@"addTo"];
  4082. if ([add_to isEqualToString:@"cart"]) {
  4083. NSString *order_code = [params objectForKey:@"orderCode"];
  4084. if (order_code.length) {
  4085. NSDictionary *newParams = @{
  4086. @"product_id" : product_id_str,
  4087. @"orderCode" : order_code
  4088. };
  4089. ret = [self offline_add2cart:newParams.mutableCopy];
  4090. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4091. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4092. ret = [RAConvertor dict2data:retDic];
  4093. }
  4094. } else if([add_to isEqualToString:@"wishlist"]) {
  4095. NSDictionary *newParams = @{
  4096. @"product_id" : product_id_str
  4097. };
  4098. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  4099. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4100. ret = [RAConvertor dict2data:retDic];
  4101. } else if([add_to isEqualToString:@"portfolio"]) {
  4102. NSDictionary *newParams = @{
  4103. @"product_id" : product_id_str
  4104. };
  4105. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  4106. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4107. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4108. ret = [RAConvertor dict2data:retDic];
  4109. }
  4110. return ret;
  4111. }
  4112. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  4113. {
  4114. return [self addAll:params from:0];
  4115. }
  4116. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  4117. {
  4118. return [self addAll:params from:1];
  4119. }
  4120. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  4121. {
  4122. return [self addAll:params from:2];
  4123. }
  4124. #pragma mark - Jack
  4125. #warning 做SQL操作时转义!!
  4126. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  4127. // "val_227" : {
  4128. // "check" : 1,
  4129. // "value" : "US United States",
  4130. // "value_id" : "228"
  4131. // },
  4132. if (!countryCode) {
  4133. countryCode = @"US";
  4134. }
  4135. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  4136. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  4137. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  4138. int code_id = sqlite3_column_int(stmt, 3); // id
  4139. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4140. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  4141. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  4142. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4143. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  4144. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4145. }
  4146. long n = *count;
  4147. *count = n + 1;
  4148. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  4149. [container setValue:countryDic forKey:key];
  4150. }] mutableCopy];
  4151. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4152. return ret;
  4153. }
  4154. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  4155. countryCode = [self translateSingleQuote:countryCode];
  4156. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  4157. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4158. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  4159. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  4160. if (name == NULL) {
  4161. name = "";
  4162. }
  4163. if (code == NULL) {
  4164. code = "";
  4165. }
  4166. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4167. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4168. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4169. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4170. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  4171. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4172. }
  4173. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4174. [container setValue:stateDic forKey:key];
  4175. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  4176. DebugLog(@"query all state error: %@",err_msg);
  4177. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4178. // [stateDic setValue:@"Other" forKey:@"value"];
  4179. // [stateDic setValue:@"" forKey:@"value_id"];
  4180. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4181. //
  4182. // if (state_code && [@"" isEqualToString:state_code]) {
  4183. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4184. // }
  4185. //
  4186. // NSString *key = [NSString stringWithFormat:@"val_0"];
  4187. // [container setValue:stateDic forKey:key];
  4188. }] mutableCopy];
  4189. [ret removeObjectForKey:@"result"];
  4190. // failure 可以不用了,一样的
  4191. if (ret.allKeys.count == 0) {
  4192. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4193. [stateDic setValue:@"Other" forKey:@"value"];
  4194. [stateDic setValue:@"" forKey:@"value_id"];
  4195. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4196. if (state_code && [@"" isEqualToString:state_code]) {
  4197. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4198. }
  4199. NSString *key = [NSString stringWithFormat:@"val_0"];
  4200. [ret setValue:stateDic forKey:key];
  4201. }
  4202. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4203. return ret;
  4204. }
  4205. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  4206. codeId = [self translateSingleQuote:codeId];
  4207. 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];
  4208. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4209. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  4210. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  4211. if (name == NULL) {
  4212. name = "";
  4213. }
  4214. if (code == NULL) {
  4215. code = "";
  4216. }
  4217. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4218. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4219. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4220. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4221. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  4222. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4223. }
  4224. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4225. [container setValue:stateDic forKey:key];
  4226. }] mutableCopy];
  4227. [ret removeObjectForKey:@"result"];
  4228. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4229. return ret;
  4230. }
  4231. + (NSDictionary *)offline_getPrice {
  4232. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  4233. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4234. char *name = (char *) sqlite3_column_text(stmt, 1);
  4235. int type = sqlite3_column_int(stmt, 2);
  4236. int orderBy = sqlite3_column_int(stmt, 3);
  4237. if (name == NULL) {
  4238. name = "";
  4239. }
  4240. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  4241. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4242. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  4243. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4244. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4245. if (orderBy == 0) {
  4246. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4247. }
  4248. [container setValue:priceDic forKey:key];
  4249. }] mutableCopy];
  4250. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4251. return ret;
  4252. }
  4253. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  4254. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  4255. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  4256. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  4257. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4258. // int _id = sqlite3_column_int(stmt, 0);
  4259. NSString *name = [self textAtColumn:1 statement:stmt];
  4260. NSDictionary *typeDic = @{
  4261. @"value_id" : name,
  4262. @"value" : name,
  4263. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  4264. };
  4265. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  4266. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  4267. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  4268. }];
  4269. return ret;
  4270. }
  4271. + (NSDictionary *)offline_getSalesRep {
  4272. // 首先从offline_login表中取出sales_code
  4273. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4274. NSString *user = app.user;
  4275. user = [self translateSingleQuote:user];
  4276. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  4277. __block NSString *user_code = @"";
  4278. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4279. char *code = (char *)sqlite3_column_text(stmt, 0);
  4280. if (code == NULL) {
  4281. code = "";
  4282. }
  4283. user_code = [NSString stringWithUTF8String:code];
  4284. }];
  4285. // 再取所有salesRep
  4286. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  4287. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4288. // 1 name 2 code 3 salesrep_id
  4289. char *name = (char *)sqlite3_column_text(stmt, 1);
  4290. char *code = (char *)sqlite3_column_text(stmt, 2);
  4291. int salesrep_id = sqlite3_column_int(stmt, 3);
  4292. if (name == NULL) {
  4293. name = "";
  4294. }
  4295. if (code == NULL) {
  4296. code = "";
  4297. }
  4298. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  4299. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  4300. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  4301. // 比较code 相等则check
  4302. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  4303. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4304. }
  4305. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  4306. }] mutableCopy];
  4307. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4308. return ret;
  4309. }
  4310. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  4311. zipcode = [self translateSingleQuote:zipcode];
  4312. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  4313. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4314. char *country = (char *)sqlite3_column_text(stmt, 0);
  4315. char *state = (char *)sqlite3_column_text(stmt, 1);
  4316. char *city = (char *)sqlite3_column_text(stmt, 2);
  4317. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  4318. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  4319. if (country == NULL) {
  4320. country = "";
  4321. }
  4322. if (state == NULL) {
  4323. state = "";
  4324. }
  4325. if (city == NULL) {
  4326. city = "";
  4327. }
  4328. if (country_code == NULL) {
  4329. country_code = "";
  4330. }
  4331. if (state_code == NULL) {
  4332. state_code = "";
  4333. }
  4334. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  4335. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  4336. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  4337. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  4338. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  4339. }] mutableCopy];
  4340. return ret;
  4341. }
  4342. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4343. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  4344. [item setValue:value forKey:valueKey];
  4345. [dic setValue:item forKey:itemKey];
  4346. }
  4347. + (NSString *)countryCodeByid:(NSString *)code_id {
  4348. NSString *ret = nil;
  4349. code_id = [self translateSingleQuote:code_id];
  4350. sqlite3 *db = [iSalesDB get_db];
  4351. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  4352. sqlite3_stmt * statement;
  4353. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4354. while (sqlite3_step(statement) == SQLITE_ROW) {
  4355. char *code = (char *)sqlite3_column_text(statement, 0);
  4356. if (code == NULL) {
  4357. code = "";
  4358. }
  4359. ret = [NSString stringWithUTF8String:code];
  4360. }
  4361. sqlite3_finalize(statement);
  4362. }
  4363. [iSalesDB close_db:db];
  4364. return ret;
  4365. }
  4366. + (NSString *)countryCodeIdByCode:(NSString *)code {
  4367. NSString *ret = nil;
  4368. code = [self translateSingleQuote:code];
  4369. sqlite3 *db = [iSalesDB get_db];
  4370. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  4371. sqlite3_stmt * statement;
  4372. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4373. while (sqlite3_step(statement) == SQLITE_ROW) {
  4374. char *_id = (char *)sqlite3_column_text(statement, 0);
  4375. if (_id == NULL) {
  4376. _id = "";
  4377. }
  4378. ret = [NSString stringWithFormat:@"%s",_id];
  4379. }
  4380. sqlite3_finalize(statement);
  4381. }
  4382. [iSalesDB close_db:db];
  4383. return ret;
  4384. }
  4385. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  4386. NSString *name = nil;
  4387. codeId = [self translateSingleQuote:codeId];
  4388. sqlite3 *db = [iSalesDB get_db];
  4389. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  4390. sqlite3_stmt * statement;
  4391. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4392. while (sqlite3_step(statement) == SQLITE_ROW) {
  4393. char *value = (char *)sqlite3_column_text(statement, 0);
  4394. if (value == NULL) {
  4395. value = "";
  4396. }
  4397. name = [NSString stringWithUTF8String:value];
  4398. }
  4399. sqlite3_finalize(statement);
  4400. }
  4401. [iSalesDB close_db:db];
  4402. return name;
  4403. }
  4404. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  4405. NSString *ret = nil;
  4406. sqlite3 *db = [iSalesDB get_db];
  4407. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  4408. sqlite3_stmt * statement;
  4409. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4410. while (sqlite3_step(statement) == SQLITE_ROW) {
  4411. char *name = (char *)sqlite3_column_text(statement, 0);
  4412. if (name == NULL) {
  4413. name = "";
  4414. }
  4415. ret = [NSString stringWithUTF8String:name];
  4416. }
  4417. sqlite3_finalize(statement);
  4418. }
  4419. [iSalesDB close_db:db];
  4420. return ret;
  4421. }
  4422. + (NSString *)salesRepCodeById:(NSString *)_id {
  4423. NSString *ret = nil;
  4424. _id = [self translateSingleQuote:_id];
  4425. sqlite3 *db = [iSalesDB get_db];
  4426. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  4427. sqlite3_stmt * statement;
  4428. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4429. while (sqlite3_step(statement) == SQLITE_ROW) {
  4430. char *rep = (char *)sqlite3_column_text(statement, 0);
  4431. if (rep == NULL) {
  4432. rep = "";
  4433. }
  4434. ret = [NSString stringWithUTF8String:rep];
  4435. }
  4436. sqlite3_finalize(statement);
  4437. }
  4438. [iSalesDB close_db:db];
  4439. return ret;
  4440. }
  4441. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  4442. char *tx = (char *)sqlite3_column_text(stmt, col);
  4443. if (tx == NULL) {
  4444. tx = "";
  4445. }
  4446. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  4447. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  4448. if (!text) {
  4449. text = @"";
  4450. }
  4451. // 将字符全部为' '的字符串干掉
  4452. int spaceCount = 0;
  4453. for (int i = 0; i < text.length; i++) {
  4454. if ([text characterAtIndex:i] == ' ') {
  4455. spaceCount++;
  4456. }
  4457. }
  4458. if (spaceCount == text.length) {
  4459. text = @"";
  4460. }
  4461. return text;
  4462. }
  4463. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  4464. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4465. NSData *data = [NSData dataWithContentsOfFile:path];
  4466. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4467. return ret;
  4468. }
  4469. + (NSString *)textFileName:(NSString *)name {
  4470. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4471. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  4472. if (!text) {
  4473. text = @"";
  4474. }
  4475. return text;
  4476. }
  4477. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  4478. return [[dic objectForKey:key] mutableCopy];
  4479. }
  4480. + (id)translateSingleQuote:(NSString *)string {
  4481. if ([string isKindOfClass:[NSString class]])
  4482. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4483. return string;
  4484. }
  4485. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  4486. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  4487. }
  4488. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  4489. NSString* ret= nil;
  4490. NSString *sqlQuery = nil;
  4491. // 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
  4492. 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];
  4493. sqlite3_stmt * statement;
  4494. // int count=0;
  4495. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4496. {
  4497. if (sqlite3_step(statement) == SQLITE_ROW)
  4498. {
  4499. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  4500. if(imgurl==nil)
  4501. imgurl="";
  4502. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  4503. ret=nsimgurl;
  4504. }
  4505. sqlite3_finalize(statement);
  4506. }
  4507. else
  4508. {
  4509. [ret setValue:@"8" forKey:@"result"];
  4510. }
  4511. // [iSalesDB close_db:db];
  4512. // DebugLog(@"data string: %@",ret );
  4513. return ret;
  4514. }
  4515. #pragma mark contact list
  4516. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  4517. {
  4518. // contactType = "Sales_Order_Customer";
  4519. // limit = 25;
  4520. // offset = 0;
  4521. // password = 123456;
  4522. // "price_name" = 16;
  4523. // user = EvanK;
  4524. sqlite3 *db = [iSalesDB get_db];
  4525. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  4526. if (contactType) {
  4527. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  4528. } else {
  4529. contactType = @"1 = 1";
  4530. }
  4531. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  4532. DebugLog(@"offline contact list keyword: %@",keyword);
  4533. // advanced search
  4534. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  4535. if (contact_name) {
  4536. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4537. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  4538. } else {
  4539. contact_name = @"";
  4540. }
  4541. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  4542. if (customer_phone) {
  4543. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4544. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  4545. } else {
  4546. customer_phone = @"";
  4547. }
  4548. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  4549. if (customer_fax) {
  4550. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4551. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  4552. } else {
  4553. customer_fax = @"";
  4554. }
  4555. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  4556. if (customer_zipcode) {
  4557. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4558. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  4559. } else {
  4560. customer_zipcode = @"";
  4561. }
  4562. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  4563. if (customer_sales_rep) {
  4564. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4565. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  4566. } else {
  4567. customer_sales_rep = @"";
  4568. }
  4569. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  4570. if (customer_state) {
  4571. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4572. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  4573. } else {
  4574. customer_state = @"";
  4575. }
  4576. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  4577. if (customer_name) {
  4578. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4579. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  4580. } else {
  4581. customer_name = @"";
  4582. }
  4583. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  4584. if (customer_country) {
  4585. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4586. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  4587. } else {
  4588. customer_country = @"";
  4589. }
  4590. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  4591. if (customer_cid) {
  4592. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4593. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  4594. } else {
  4595. customer_cid = @"";
  4596. }
  4597. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  4598. if (customer_city) {
  4599. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4600. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  4601. } else {
  4602. customer_city = @"";
  4603. }
  4604. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  4605. if (customer_address) {
  4606. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4607. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  4608. } else {
  4609. customer_address = @"";
  4610. }
  4611. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  4612. if (customer_email) {
  4613. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4614. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  4615. } else {
  4616. customer_email = @"";
  4617. }
  4618. NSString *price_name = [params valueForKey:@"price_name"];
  4619. if (price_name) {
  4620. if ([price_name containsString:@","]) {
  4621. // 首先从 price表中查处name
  4622. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  4623. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  4624. for (int i = 1;i < pArray.count;i++) {
  4625. NSString *p = pArray[i];
  4626. [mutablePStr appendFormat:@" or type = %@ ",p];
  4627. }
  4628. [mutablePStr appendString:@";"];
  4629. __block NSMutableArray *price_name_array = [NSMutableArray array];
  4630. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4631. char *name = (char *)sqlite3_column_text(stmt, 0);
  4632. if (!name)
  4633. name = "";
  4634. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  4635. }];
  4636. // 再根据name 拼sql
  4637. NSMutableString *mutable_price_name = [NSMutableString string];
  4638. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  4639. for (int i = 1; i < price_name_array.count; i++) {
  4640. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  4641. }
  4642. [mutable_price_name appendString:@")"];
  4643. price_name = mutable_price_name;
  4644. } else {
  4645. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  4646. if ([price_name isEqualToString:@""]) {
  4647. price_name = @"";
  4648. } else {
  4649. __block NSString *price;
  4650. price_name = [self translateSingleQuote:price_name];
  4651. [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) {
  4652. char *p = (char *)sqlite3_column_text(stmt, 0);
  4653. if (p == NULL) {
  4654. p = "";
  4655. }
  4656. price = [NSString stringWithUTF8String:p];
  4657. }];
  4658. if ([price isEqualToString:@""]) {
  4659. price_name = @"";
  4660. } else {
  4661. price = [self translateSingleQuote:price];
  4662. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  4663. }
  4664. }
  4665. }
  4666. } else {
  4667. price_name = @"";
  4668. }
  4669. int limit = [[params valueForKey:@"limit"] intValue];
  4670. int offset = [[params valueForKey:@"offset"] intValue];
  4671. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4672. 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];
  4673. 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];
  4674. // int result= [iSalesDB AddExFunction:db];
  4675. int count =0;
  4676. NSString *sqlQuery = nil;
  4677. if(keyword.length==0)
  4678. {
  4679. // 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];
  4680. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  4681. sqlQuery = sql;
  4682. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  4683. }
  4684. else
  4685. {
  4686. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  4687. keyword = keyword.lowercaseString;
  4688. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4689. 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];
  4690. 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]];
  4691. }
  4692. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4693. sqlite3_stmt * statement;
  4694. [ret setValue:@"2" forKey:@"result"];
  4695. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  4696. // 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";
  4697. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4698. {
  4699. int i = 0;
  4700. while (sqlite3_step(statement) == SQLITE_ROW)
  4701. {
  4702. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4703. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4704. int editable = sqlite3_column_int(statement, 0);
  4705. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4706. NSString *nscompany_name =nil;
  4707. if(company_name==nil)
  4708. nscompany_name=@"";
  4709. else
  4710. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  4711. char *country = (char*)sqlite3_column_text(statement, 2);
  4712. if(country==nil)
  4713. country="";
  4714. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4715. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4716. // if(addr==nil)
  4717. // addr="";
  4718. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4719. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4720. if(zipcode==nil)
  4721. zipcode="";
  4722. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4723. char *state = (char*)sqlite3_column_text(statement, 5);
  4724. if(state==nil)
  4725. state="";
  4726. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4727. char *city = (char*)sqlite3_column_text(statement, 6);
  4728. if(city==nil)
  4729. city="";
  4730. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4731. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4732. // NSString *nscontact_name = nil;
  4733. // if(contact_name==nil)
  4734. // nscontact_name=@"";
  4735. // else
  4736. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4737. char *phone = (char*)sqlite3_column_text(statement, 8);
  4738. NSString *nsphone = nil;
  4739. if(phone==nil)
  4740. nsphone=@"";
  4741. else
  4742. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4743. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4744. if(contact_id==nil)
  4745. contact_id="";
  4746. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4747. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4748. if(addr_1==nil)
  4749. addr_1="";
  4750. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4751. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4752. if(addr_2==nil)
  4753. addr_2="";
  4754. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4755. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4756. if(addr_3==nil)
  4757. addr_3="";
  4758. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4759. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4760. if(addr_4==nil)
  4761. addr_4="";
  4762. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4763. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4764. if(first_name==nil)
  4765. first_name="";
  4766. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4767. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4768. if(last_name==nil)
  4769. last_name="";
  4770. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4771. char *fax = (char*)sqlite3_column_text(statement, 16);
  4772. NSString *nsfax = nil;
  4773. if(fax==nil)
  4774. nsfax=@"";
  4775. else
  4776. {
  4777. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4778. if(nsfax.length>0)
  4779. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  4780. }
  4781. char *email = (char*)sqlite3_column_text(statement, 17);
  4782. NSString *nsemail = nil;
  4783. if(email==nil)
  4784. nsemail=@"";
  4785. else
  4786. {
  4787. nsemail= [[NSString alloc]initWithUTF8String:email];
  4788. if(nsemail.length>0)
  4789. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  4790. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  4791. }
  4792. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4793. [arr_name addObject:nsfirst_name];
  4794. [arr_name addObject:nslast_name];
  4795. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4796. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4797. {
  4798. // decrypt
  4799. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4800. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4801. nsphone=[AESCrypt fastdecrypt:nsphone];
  4802. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4803. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4804. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4805. }
  4806. [arr_addr addObject:nscompany_name];
  4807. [arr_addr addObject:nscontact_name];
  4808. [arr_addr addObject:@"<br>"];
  4809. [arr_addr addObject:nsaddr_1];
  4810. [arr_addr addObject:nsaddr_2];
  4811. [arr_addr addObject:nsaddr_3];
  4812. [arr_addr addObject:nsaddr_4];
  4813. //[arr_addr addObject:nsaddr];
  4814. [arr_addr addObject:nszipcode];
  4815. [arr_addr addObject:nscity];
  4816. [arr_addr addObject:nsstate];
  4817. [arr_addr addObject:nscountry];
  4818. [arr_addr addObject:@"<br>"];
  4819. [arr_addr addObject:nsphone];
  4820. [arr_addr addObject:nsfax];
  4821. [arr_addr addObject:nsemail];
  4822. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  4823. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  4824. [item setValue:name forKey:@"name"];
  4825. [item setValue:nscontact_id forKey:@"contact_id"];
  4826. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  4827. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4828. i++;
  4829. }
  4830. UIApplication * app = [UIApplication sharedApplication];
  4831. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4832. [ret setValue:appDelegate.mode forKey:@"mode"];
  4833. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  4834. sqlite3_finalize(statement);
  4835. }
  4836. [iSalesDB close_db:db];
  4837. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4838. return ret;
  4839. }
  4840. #pragma mark contact Advanced search
  4841. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  4842. {
  4843. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  4844. return [RAConvertor dict2data:contactAdvanceDic];
  4845. }
  4846. #pragma mark create new contact
  4847. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  4848. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  4849. NSData *data = [NSData dataWithContentsOfFile:path];
  4850. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4851. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4852. NSString *countryCode = nil;
  4853. NSString *countryCode_id = nil;
  4854. NSString *stateCode = nil;
  4855. NSString *city = nil;
  4856. NSString *zipCode = nil;
  4857. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4858. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  4859. NSString *code_id = params[@"country"];
  4860. countryCode_id = code_id;
  4861. countryCode = [self countryCodeByid:code_id];
  4862. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  4863. NSString *zip_code = params[@"zipcode"];
  4864. // 剔除全部为空格
  4865. int spaceCount = 0;
  4866. for (int i = 0; i < zip_code.length; i++) {
  4867. if ([zip_code characterAtIndex:i] == ' ') {
  4868. spaceCount++;
  4869. }
  4870. }
  4871. if (spaceCount == zip_code.length) {
  4872. zip_code = @"";
  4873. }
  4874. zipCode = zip_code;
  4875. if (zipCode.length > 0) {
  4876. countryCode_id = params[@"country"];
  4877. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4878. countryCode = [dic valueForKey:@"country_code"];
  4879. if (!countryCode) {
  4880. // countryCode = @"US";
  4881. NSString *code_id = params[@"country"];
  4882. countryCode = [self countryCodeByid:code_id];
  4883. }
  4884. stateCode = [dic valueForKey:@"state_code"];
  4885. if (!stateCode.length) {
  4886. stateCode = params[@"state"];
  4887. }
  4888. city = [dic valueForKey:@"city"];
  4889. if (!city.length) {
  4890. city = params[@"city"];
  4891. }
  4892. // zip code
  4893. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  4894. [zipDic setValue:zipCode forKey:@"value"];
  4895. [section_0 setValue:zipDic forKey:@"item_11"];
  4896. } else {
  4897. NSString *code_id = params[@"country"];
  4898. countryCode = [self countryCodeByid:code_id];
  4899. stateCode = params[@"state"];
  4900. city = params[@"city"];
  4901. }
  4902. }
  4903. } else {
  4904. // default: US United States
  4905. countryCode = @"US";
  4906. countryCode_id = @"228";
  4907. }
  4908. // country
  4909. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4910. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  4911. // state
  4912. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4913. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  4914. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  4915. // city
  4916. if (city) {
  4917. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  4918. [cityDic setValue:city forKey:@"value"];
  4919. [section_0 setValue:cityDic forKey:@"item_13"];
  4920. }
  4921. // price type
  4922. NSDictionary *priceDic = [self offline_getPrice];
  4923. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  4924. // contact type
  4925. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  4926. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  4927. // Sales Rep
  4928. NSDictionary *repDic = [self offline_getSalesRep];
  4929. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4930. [ret setValue:section_0 forKey:@"section_0"];
  4931. return [RAConvertor dict2data:ret];
  4932. }
  4933. #pragma mark save
  4934. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  4935. NSString *addr = nil;
  4936. NSString *contact_name = nil;
  4937. NSString *companyName = [params objectForKey:@"company"];
  4938. if (companyName) {
  4939. companyName = [AESCrypt fastencrypt:companyName];
  4940. } else {
  4941. companyName = @"";
  4942. }
  4943. DebugLog(@"company");
  4944. companyName = [self translateSingleQuote:companyName];
  4945. NSString *addr1 = [params objectForKey:@"address"];
  4946. NSString *addr2 = [params objectForKey:@"address2"];
  4947. NSString *addr3 = [params objectForKey:@"address_3"];
  4948. NSString *addr4 = [params objectForKey:@"address_4"];
  4949. if (!addr2) {
  4950. addr2 = @"";
  4951. }
  4952. if (!addr3) {
  4953. addr3 = @"";
  4954. }
  4955. if (!addr4) {
  4956. addr4 = @"";
  4957. }
  4958. if (!addr1) {
  4959. addr1 = @"";
  4960. }
  4961. DebugLog(@"addr");
  4962. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  4963. addr = [AESCrypt fastencrypt:addr];
  4964. addr = [self translateSingleQuote:addr];
  4965. if (addr1 && ![addr1 isEqualToString:@""]) {
  4966. addr1 = [AESCrypt fastencrypt:addr1];
  4967. }
  4968. addr1 = [self translateSingleQuote:addr1];
  4969. addr2 = [self translateSingleQuote:addr2];
  4970. addr3 = [self translateSingleQuote:addr3];
  4971. addr4 = [self translateSingleQuote:addr4];
  4972. NSString *country = [params objectForKey:@"country"];
  4973. if (country) {
  4974. country = [self countryNameByCountryCodeId:country];
  4975. } else {
  4976. country = @"";
  4977. }
  4978. DebugLog(@"country");
  4979. country = [self translateSingleQuote:country];
  4980. NSString *state = [params objectForKey:@"state"];
  4981. if (!state) {
  4982. state = @"";
  4983. }
  4984. DebugLog(@"state");
  4985. state = [self translateSingleQuote:state];
  4986. NSString *city = [params objectForKey:@"city"];
  4987. if (!city) {
  4988. city = @"";
  4989. }
  4990. city = [self translateSingleQuote:city];
  4991. NSString *zipcode = [params objectForKey:@"zipcode"];
  4992. if (!zipcode) {
  4993. zipcode = @"";
  4994. }
  4995. DebugLog(@"zip");
  4996. zipcode = [self translateSingleQuote:zipcode];
  4997. NSString *fistName = [params objectForKey:@"firstname"];
  4998. if (!fistName) {
  4999. fistName = @"";
  5000. }
  5001. NSString *lastName = [params objectForKey:@"lastname"];
  5002. if (!lastName) {
  5003. lastName = @"";
  5004. }
  5005. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  5006. DebugLog(@"contact_name");
  5007. contact_name = [self translateSingleQuote:contact_name];
  5008. fistName = [self translateSingleQuote:fistName];
  5009. lastName = [self translateSingleQuote:lastName];
  5010. NSString *phone = [params objectForKey:@"phone"];
  5011. if (phone) {
  5012. phone = [AESCrypt fastencrypt:phone];
  5013. } else {
  5014. phone = @"";
  5015. }
  5016. DebugLog(@"PHONE");
  5017. phone = [self translateSingleQuote:phone];
  5018. NSString *fax = [params objectForKey:@"fax"];
  5019. if (!fax) {
  5020. fax = @"";
  5021. }
  5022. DebugLog(@"FAX");
  5023. fax = [self translateSingleQuote:fax];
  5024. NSString *email = [params objectForKey:@"email"];
  5025. if (!email) {
  5026. email = @"";
  5027. }
  5028. DebugLog(@"EMAIL:%@",email);
  5029. email = [self translateSingleQuote:email];
  5030. NSString *notes = [params objectForKey:@"contact_notes"];
  5031. if (!notes) {
  5032. notes = @"";
  5033. }
  5034. DebugLog(@"NOTE:%@",notes);
  5035. notes = [self translateSingleQuote:notes];
  5036. NSString *price = [params objectForKey:@"price_name"];
  5037. if (price) {
  5038. price = [self priceNameByPriceId:price];
  5039. } else {
  5040. price = @"";
  5041. }
  5042. DebugLog(@"PRICE");
  5043. price = [self translateSingleQuote:price];
  5044. NSString *salesRep = [params objectForKey:@"sales_rep"];
  5045. if (salesRep) {
  5046. salesRep = [self salesRepCodeById:salesRep];
  5047. } else {
  5048. salesRep = @"";
  5049. }
  5050. salesRep = [self translateSingleQuote:salesRep];
  5051. NSString *img = [params objectForKey:@"business_card"];
  5052. NSArray *array = [img componentsSeparatedByString:@","];
  5053. NSString *img_0 = array[0];
  5054. if (!img_0) {
  5055. img_0 = @"";
  5056. }
  5057. img_0 = [self translateSingleQuote:img_0];
  5058. NSString *img_1 = array[1];
  5059. if (!img_1) {
  5060. img_1 = @"";
  5061. }
  5062. img_1 = [self translateSingleQuote:img_1];
  5063. NSString *img_2 = array[2];
  5064. if (!img_2) {
  5065. img_2 = @"";
  5066. }
  5067. img_2 = [self translateSingleQuote:img_2];
  5068. NSString *contact_id = [NSUUID UUID].UUIDString;
  5069. NSString *contact_type = [params objectForKey:@"type_name"];
  5070. if (!contact_type) {
  5071. contact_type = @"";
  5072. }
  5073. contact_type = [self translateSingleQuote:contact_type];
  5074. // 判断更新时是否为customer
  5075. if (update) {
  5076. contact_id = [params objectForKey:@"contact_id"];
  5077. if (!contact_id) {
  5078. contact_id = @"";
  5079. }
  5080. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5081. __block int customer = 0;
  5082. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5083. customer = sqlite3_column_int(stmt, 0);
  5084. }];
  5085. isCustomer = customer ? YES : NO;
  5086. }
  5087. NSMutableDictionary *sync_dic = [params mutableCopy];
  5088. if (isCustomer) {
  5089. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5090. } else {
  5091. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5092. }
  5093. NSString *sync_data = nil;
  5094. NSString *sql = nil;
  5095. if (update){
  5096. contact_id = [params objectForKey:@"contact_id"];
  5097. if (!contact_id) {
  5098. contact_id = @"";
  5099. }
  5100. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5101. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5102. sync_data = [RAConvertor dict2string:sync_dic];
  5103. sync_data = [self translateSingleQuote:sync_data];
  5104. 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];
  5105. } else {
  5106. contact_id = [self translateSingleQuote:contact_id];
  5107. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5108. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5109. sync_data = [RAConvertor dict2string:sync_dic];
  5110. sync_data = [self translateSingleQuote:sync_data];
  5111. 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];
  5112. }
  5113. int result = [iSalesDB execSql:sql];
  5114. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5115. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5116. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5117. }
  5118. #pragma mark save new contact
  5119. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5120. {
  5121. return [self offline_saveContact:params update:NO isCustomer:YES];
  5122. }
  5123. #pragma mark edit contact
  5124. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5125. {
  5126. // {
  5127. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5128. // password = 123456;
  5129. // user = EvanK;
  5130. // }
  5131. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5132. NSData *data = [NSData dataWithContentsOfFile:path];
  5133. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5134. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5135. NSString *countryCode = nil;
  5136. NSString *countryCode_id = nil;
  5137. NSString *stateCode = nil;
  5138. /*------contact infor------*/
  5139. __block NSString *country = nil;
  5140. __block NSString *company_name = nil;
  5141. __block NSString *contact_id = params[@"contact_id"];
  5142. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5143. __block NSString *zipcode = nil;
  5144. __block NSString *state = nil; // state_code
  5145. __block NSString *city = nil; //
  5146. __block NSString *firt_name,*last_name;
  5147. __block NSString *phone,*fax,*email;
  5148. __block NSString *notes,*price_type,*sales_rep;
  5149. __block NSString *img_0,*img_1,*img_2;
  5150. __block NSString *contact_type;
  5151. contact_id = [self translateSingleQuote:contact_id];
  5152. 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];
  5153. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5154. country = [self textAtColumn:0 statement:stmt]; // country name
  5155. company_name = [self textAtColumn:1 statement:stmt];
  5156. addr_1 = [self textAtColumn:2 statement:stmt];
  5157. addr_2 = [self textAtColumn:3 statement:stmt];
  5158. addr_3 = [self textAtColumn:4 statement:stmt];
  5159. addr_4 = [self textAtColumn:5 statement:stmt];
  5160. zipcode = [self textAtColumn:6 statement:stmt];
  5161. state = [self textAtColumn:7 statement:stmt]; // state code
  5162. city = [self textAtColumn:8 statement:stmt];
  5163. firt_name = [self textAtColumn:9 statement:stmt];
  5164. last_name = [self textAtColumn:10 statement:stmt];
  5165. phone = [self textAtColumn:11 statement:stmt];
  5166. fax = [self textAtColumn:12 statement:stmt];
  5167. email = [self textAtColumn:13 statement:stmt];
  5168. notes = [self textAtColumn:14 statement:stmt];
  5169. price_type = [self textAtColumn:15 statement:stmt]; // name
  5170. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  5171. img_0 = [self textAtColumn:17 statement:stmt];
  5172. img_1 = [self textAtColumn:18 statement:stmt];
  5173. img_2 = [self textAtColumn:19 statement:stmt];
  5174. contact_type = [self textAtColumn:20 statement:stmt];
  5175. }];
  5176. // decrypt
  5177. if (company_name) {
  5178. company_name = [AESCrypt fastdecrypt:company_name];
  5179. }
  5180. if (addr_1) {
  5181. addr_1 = [AESCrypt fastdecrypt:addr_1];
  5182. }
  5183. if (phone) {
  5184. phone = [AESCrypt fastdecrypt:phone];
  5185. }
  5186. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  5187. countryCode = [iSalesDB jk_queryText:countrySql];
  5188. stateCode = state;
  5189. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5190. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  5191. NSString *code_id = params[@"country"];
  5192. countryCode_id = code_id;
  5193. countryCode = [self countryCodeByid:code_id];
  5194. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  5195. NSString *zip_code = params[@"zipcode"];
  5196. // 剔除全部为空格
  5197. int spaceCount = 0;
  5198. for (int i = 0; i < zip_code.length; i++) {
  5199. if ([zip_code characterAtIndex:i] == ' ') {
  5200. spaceCount++;
  5201. }
  5202. }
  5203. if (spaceCount == zip_code.length) {
  5204. zip_code = @"";
  5205. }
  5206. if (zipcode.length > 0) {
  5207. zipcode = zip_code;
  5208. countryCode_id = params[@"country"];
  5209. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5210. countryCode = [dic valueForKey:@"country_code"];
  5211. if (!countryCode) {
  5212. NSString *code_id = params[@"country"];
  5213. countryCode = [self countryCodeByid:code_id];
  5214. }
  5215. stateCode = [dic valueForKey:@"state_code"];
  5216. if (!stateCode.length) {
  5217. stateCode = params[@"state"];
  5218. }
  5219. city = [dic valueForKey:@"city"];
  5220. if (!city.length) {
  5221. city = params[@"city"];
  5222. }
  5223. // zip code
  5224. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5225. [zipDic setValue:zipcode forKey:@"value"];
  5226. [section_0 setValue:zipDic forKey:@"item_8"];
  5227. } else {
  5228. NSString *code_id = params[@"country"];
  5229. countryCode = [self countryCodeByid:code_id];
  5230. stateCode = params[@"state"];
  5231. city = params[@"city"];
  5232. }
  5233. }
  5234. }
  5235. // 0 Country
  5236. // 1 Company Name
  5237. // 2 Contact ID
  5238. // 3 Picture
  5239. // 4 Address 1
  5240. // 5 Address 2
  5241. // 6 Address 3
  5242. // 7 Address 4
  5243. // 8 Zip Code
  5244. // 9 State/Province
  5245. // 10 City
  5246. // 11 Contact First Name
  5247. // 12 Contact Last Name
  5248. // 13 Phone
  5249. // 14 Fax
  5250. // 15 Email
  5251. // 16 Contact Notes
  5252. // 17 Price Type
  5253. // 18 Contact Type
  5254. // 19 Sales Rep
  5255. // country
  5256. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5257. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5258. // company
  5259. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5260. // contact_id
  5261. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5262. // picture
  5263. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5264. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5265. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5266. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5267. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5268. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5269. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5270. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5271. // addr 1 2 3 4
  5272. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5273. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5274. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5275. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5276. // zip code
  5277. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5278. // state
  5279. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5280. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5281. // city
  5282. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5283. // first last
  5284. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5285. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5286. // phone fax email
  5287. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5288. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5289. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5290. // notes
  5291. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5292. // price
  5293. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5294. for (NSString *key in priceDic.allKeys) {
  5295. if ([key containsString:@"val_"]) {
  5296. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5297. if ([dic[@"value"] isEqualToString:price_type]) {
  5298. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5299. [priceDic setValue:dic forKey:key];
  5300. }
  5301. }
  5302. }
  5303. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5304. // Contact Rep
  5305. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5306. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5307. // Sales Rep
  5308. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  5309. for (NSString *key in repDic.allKeys) {
  5310. if ([key containsString:@"val_"]) {
  5311. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5312. NSString *value = dic[@"value"];
  5313. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5314. if (code && [code isEqualToString:sales_rep]) {
  5315. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5316. [repDic setValue:dic forKey:key];
  5317. }
  5318. }
  5319. }
  5320. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5321. [ret setValue:section_0 forKey:@"section_0"];
  5322. return [RAConvertor dict2data:ret];
  5323. }
  5324. #pragma mark save contact
  5325. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5326. {
  5327. return [self offline_saveContact:params update:YES isCustomer:YES];
  5328. }
  5329. #pragma mark category
  5330. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5331. if (ck) {
  5332. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5333. for (NSString *key in res.allKeys) {
  5334. if (![key isEqualToString:@"count"]) {
  5335. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5336. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5337. if ([val[@"value"] isEqualToString:ck]) {
  5338. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5339. }
  5340. [res setValue:val forKey:key];
  5341. }
  5342. }
  5343. [dic setValue:res forKey:valueKey];
  5344. }
  5345. }
  5346. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5347. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5348. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5349. NSString* category = [params valueForKey:@"category"];
  5350. if (!category || [category isEqualToString:@""]) {
  5351. category = @"%";
  5352. }
  5353. category = [self translateSingleQuote:category];
  5354. int limit = [[params valueForKey:@"limit"] intValue];
  5355. int offset = [[params valueForKey:@"offset"] intValue];
  5356. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5357. NSString *limit_str = @"";
  5358. if (limited) {
  5359. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5360. }
  5361. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5362. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5363. sqlite3 *db = [iSalesDB get_db];
  5364. // [iSalesDB AddExFunction:db];
  5365. int count;
  5366. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5367. 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];
  5368. double price_min = 0;
  5369. double price_max = 0;
  5370. if ([params.allKeys containsObject:@"alert"]) {
  5371. // alert
  5372. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5373. NSString *alert = params[@"alert"];
  5374. if ([alert isEqualToString:@"Display All"]) {
  5375. alert = [NSString stringWithFormat:@""];
  5376. } else {
  5377. alert = [self translateSingleQuote:alert];
  5378. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5379. }
  5380. // available
  5381. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5382. NSString *available = params[@"available"];
  5383. NSString *available_condition;
  5384. if ([available isEqualToString:@"Display All"]) {
  5385. available_condition = @"";
  5386. } else if ([available isEqualToString:@"Available Now"]) {
  5387. available_condition = @"and availability > 0";
  5388. } else {
  5389. available_condition = @"and availability == 0";
  5390. }
  5391. // best seller
  5392. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5393. NSString *best_seller = @"";
  5394. NSString *order_best_seller = @"m.name asc";
  5395. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5396. best_seller = @"and best_seller > 0";
  5397. order_best_seller = @"m.best_seller desc,m.name asc";
  5398. }
  5399. // price
  5400. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5401. NSString *price = params[@"price"];
  5402. price_min = 0;
  5403. price_max = MAXFLOAT;
  5404. if (appDelegate.user && price != nil) {
  5405. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5406. NSMutableString *priceName = [NSMutableString string];
  5407. for (int i = 0; i < priceTypeArray.count; i++) {
  5408. NSString *pricetype = priceTypeArray[i];
  5409. pricetype = [self translateSingleQuote:pricetype];
  5410. if (i == 0) {
  5411. [priceName appendFormat:@"'%@'",pricetype];
  5412. } else {
  5413. [priceName appendFormat:@",'%@'",pricetype];
  5414. }
  5415. }
  5416. if ([price isEqualToString:@"Display All"]) {
  5417. price = [NSString stringWithFormat:@""];
  5418. } else if([price containsString:@"+"]){
  5419. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5420. price_min = [price doubleValue];
  5421. 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];
  5422. } else {
  5423. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5424. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5425. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5426. 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];
  5427. }
  5428. } else {
  5429. price = @"";
  5430. }
  5431. // sold_by_qty : Sold in quantities of %@
  5432. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5433. NSString *qty = params[@"sold_by_qty"];
  5434. if ([qty isEqualToString:@"Display All"]) {
  5435. qty = @"";
  5436. } else {
  5437. qty = [self translateSingleQuote:qty];
  5438. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5439. }
  5440. // cate
  5441. // category = [self translateSingleQuote:category];
  5442. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  5443. // cate mutiple selection
  5444. NSString *category_id = params[@"category"];
  5445. NSMutableArray *cate_id_array = nil;
  5446. NSMutableString *cateWhere = [NSMutableString string];
  5447. if ([category_id isEqualToString:@""] || !category_id) {
  5448. [cateWhere appendString:@"1 = 1"];
  5449. } else {
  5450. if ([category_id containsString:@","]) {
  5451. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5452. } else {
  5453. cate_id_array = [@[category_id] mutableCopy];
  5454. }
  5455. [cateWhere appendString:@"("];
  5456. for (int i = 0; i < cate_id_array.count; i++) {
  5457. if (i == 0) {
  5458. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5459. } else {
  5460. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5461. }
  5462. }
  5463. [cateWhere appendString:@")"];
  5464. }
  5465. // where bestseller > 0 order by bestseller desc
  5466. // sql query: alert availability(int) best_seller(int) price qty
  5467. 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];
  5468. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5469. }
  5470. DebugLog(@"offline category where: %@",where);
  5471. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5472. if (!appDelegate.user) {
  5473. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5474. }
  5475. [ret setValue:filter forKey:@"filter"];
  5476. DebugLog(@"offline_category sql:%@",sqlQuery);
  5477. sqlite3_stmt * statement;
  5478. [ret setValue:@"2" forKey:@"result"];
  5479. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5480. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5481. // int count=0;
  5482. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5483. {
  5484. int i=0;
  5485. while (sqlite3_step(statement) == SQLITE_ROW)
  5486. {
  5487. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5488. char *name = (char*)sqlite3_column_text(statement, 0);
  5489. if(name==nil)
  5490. name="";
  5491. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5492. char *description = (char*)sqlite3_column_text(statement, 1);
  5493. if(description==nil)
  5494. description="";
  5495. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5496. int product_id = sqlite3_column_int(statement, 2);
  5497. int wid = sqlite3_column_int(statement, 3);
  5498. int closeout = sqlite3_column_int(statement, 4);
  5499. int cid = sqlite3_column_int(statement, 5);
  5500. int wisdelete = sqlite3_column_int(statement, 6);
  5501. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  5502. int more_color = sqlite3_column_int(statement, 8);
  5503. // Defaul Category ID
  5504. __block NSString *categoryID = nil;
  5505. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  5506. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5507. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  5508. if(default_category==nil)
  5509. default_category="";
  5510. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  5511. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  5512. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  5513. categoryID = nsdefault_category;
  5514. }];
  5515. if (!categoryID.length) {
  5516. NSString *cateIDs = params[@"category"];
  5517. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  5518. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  5519. for (NSString *cateID in requestCategoryArr) {
  5520. BOOL needBreak = NO;
  5521. for (NSString *itemCateIDBox in itemCategoryArr) {
  5522. if (itemCateIDBox.length > 4) {
  5523. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  5524. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  5525. if ([itemCategoryID isEqualToString:cateID]) {
  5526. needBreak = YES;
  5527. categoryID = itemCategoryID;
  5528. break;
  5529. }
  5530. }
  5531. }
  5532. if (needBreak) {
  5533. break;
  5534. }
  5535. }
  5536. }
  5537. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5538. if(wid !=0 && wisdelete != 1)
  5539. [item setValue:@"true" forKey:@"wish_exists"];
  5540. else
  5541. [item setValue:@"false" forKey:@"wish_exists"];
  5542. if(closeout==0)
  5543. [item setValue:@"false" forKey:@"is_closeout"];
  5544. else
  5545. [item setValue:@"true" forKey:@"is_closeout"];
  5546. if(cid==0)
  5547. [item setValue:@"false" forKey:@"cart_exists"];
  5548. else
  5549. [item setValue:@"true" forKey:@"cart_exists"];
  5550. if (more_color == 0) {
  5551. [item setObject:@(false) forKey:@"more_color"];
  5552. } else if (more_color == 1) {
  5553. [item setObject:@(true) forKey:@"more_color"];
  5554. }
  5555. [item addEntriesFromDictionary:imgjson];
  5556. // [item setValue:nsurl forKey:@"img"];
  5557. [item setValue:nsname forKey:@"name"];
  5558. [item setValue:nsdescription forKey:@"description"];
  5559. if (categoryID) {
  5560. [item setValue:categoryID forKey:@"item_category_id"];
  5561. }
  5562. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5563. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5564. i++;
  5565. }
  5566. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5567. [ret setObject:items forKey:@"items"];
  5568. sqlite3_finalize(statement);
  5569. } else {
  5570. DebugLog(@"nothing...");
  5571. }
  5572. DebugLog(@"count:%d",count);
  5573. [iSalesDB close_db:db];
  5574. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  5575. return ret;
  5576. }
  5577. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  5578. {
  5579. return [self categoryList:params limited:YES];
  5580. }
  5581. # pragma mark item search
  5582. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  5583. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5584. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  5585. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5586. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5587. // category
  5588. NSDictionary *category_menu = [self offline_category_menu];
  5589. [filter setValue:category_menu forKey:@"category"];
  5590. NSString* where= nil;
  5591. NSString* orderby= @"m.name";
  5592. if (!filterSearch) {
  5593. int covertype = [[params valueForKey:@"covertype"] intValue];
  5594. switch (covertype) {
  5595. case 0:
  5596. {
  5597. where=@"m.category like'%%#005#%%'";
  5598. break;
  5599. }
  5600. case 1:
  5601. {
  5602. where=@"m.alert like '%QS%'";
  5603. break;
  5604. }
  5605. case 2:
  5606. {
  5607. where=@"m.availability>0";
  5608. break;
  5609. }
  5610. case 3:
  5611. {
  5612. where=@"m.best_seller>0";
  5613. orderby=@"m.best_seller desc,m.name asc";
  5614. break;
  5615. }
  5616. default:
  5617. where=@"1=1";
  5618. break;
  5619. }
  5620. }
  5621. int limit = [[params valueForKey:@"limit"] intValue];
  5622. int offset = [[params valueForKey:@"offset"] intValue];
  5623. NSString *limit_str = @"";
  5624. if (limited) {
  5625. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  5626. }
  5627. sqlite3 *db = [iSalesDB get_db];
  5628. // [iSalesDB AddExFunction:db];
  5629. int count;
  5630. NSString *sqlQuery = nil;
  5631. where = [where stringByAppendingString:@" and m.is_active = 1"];
  5632. 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];
  5633. double price_min = 0;
  5634. double price_max = 0;
  5635. if (filterSearch) {
  5636. // alert
  5637. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5638. NSString *alert = params[@"alert"];
  5639. if ([alert isEqualToString:@"Display All"]) {
  5640. alert = [NSString stringWithFormat:@""];
  5641. } else {
  5642. alert = [self translateSingleQuote:alert];
  5643. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5644. }
  5645. // available
  5646. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5647. NSString *available = params[@"available"];
  5648. NSString *available_condition;
  5649. if ([available isEqualToString:@"Display All"]) {
  5650. available_condition = @"";
  5651. } else if ([available isEqualToString:@"Available Now"]) {
  5652. available_condition = @"and availability > 0";
  5653. } else {
  5654. available_condition = @"and availability == 0";
  5655. }
  5656. // best seller
  5657. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5658. NSString *best_seller = @"";
  5659. NSString *order_best_seller = @"m.name asc";
  5660. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5661. best_seller = @"and best_seller > 0";
  5662. order_best_seller = @"m.best_seller desc,m.name asc";
  5663. }
  5664. // price
  5665. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5666. NSString *price = params[@"price"];
  5667. price_min = 0;
  5668. price_max = MAXFLOAT;
  5669. if (appDelegate.user) {
  5670. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5671. NSMutableString *priceName = [NSMutableString string];
  5672. for (int i = 0; i < priceTypeArray.count; i++) {
  5673. NSString *pricetype = priceTypeArray[i];
  5674. pricetype = [self translateSingleQuote:pricetype];
  5675. if (i == 0) {
  5676. [priceName appendFormat:@"'%@'",pricetype];
  5677. } else {
  5678. [priceName appendFormat:@",'%@'",pricetype];
  5679. }
  5680. }
  5681. if ([price isEqualToString:@"Display All"]) {
  5682. price = [NSString stringWithFormat:@""];
  5683. } else if([price containsString:@"+"]){
  5684. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5685. price_min = [price doubleValue];
  5686. 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];
  5687. } else {
  5688. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5689. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5690. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5691. 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];
  5692. }
  5693. } else {
  5694. price = @"";
  5695. }
  5696. // sold_by_qty : Sold in quantities of %@
  5697. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5698. NSString *qty = params[@"sold_by_qty"];
  5699. if ([qty isEqualToString:@"Display All"]) {
  5700. qty = @"";
  5701. } else {
  5702. qty = [self translateSingleQuote:qty];
  5703. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5704. }
  5705. // category
  5706. NSString *category_id = params[@"ctgId"];
  5707. NSMutableArray *cate_id_array = nil;
  5708. NSMutableString *cateWhere = [NSMutableString string];
  5709. if ([category_id isEqualToString:@""] || !category_id) {
  5710. [cateWhere appendString:@"1 = 1"];
  5711. } else {
  5712. if ([category_id containsString:@","]) {
  5713. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5714. } else {
  5715. cate_id_array = [@[category_id] mutableCopy];
  5716. }
  5717. /*-----------*/
  5718. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  5719. [cateWhere appendString:@"("];
  5720. for (int i = 0; i < cate_id_array.count; i++) {
  5721. for (NSString *key0 in cateDic.allKeys) {
  5722. if ([key0 containsString:@"category_"]) {
  5723. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  5724. for (NSString *key1 in category0.allKeys) {
  5725. if ([key1 containsString:@"category_"]) {
  5726. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  5727. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5728. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5729. cate_id_array[i] = [category1 objectForKey:@"id"];
  5730. if (i == 0) {
  5731. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5732. } else {
  5733. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5734. }
  5735. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5736. [category0 setValue:category1 forKey:key1];
  5737. [cateDic setValue:category0 forKey:key0];
  5738. }
  5739. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  5740. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5741. cate_id_array[i] = [category0 objectForKey:@"id"];
  5742. if (i == 0) {
  5743. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5744. } else {
  5745. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5746. }
  5747. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5748. [cateDic setValue:category0 forKey:key0];
  5749. }
  5750. }
  5751. }
  5752. }
  5753. }
  5754. }
  5755. [cateWhere appendString:@")"];
  5756. [filter setValue:cateDic forKey:@"category"];
  5757. }
  5758. // where bestseller > 0 order by bestseller desc
  5759. // sql query: alert availability(int) best_seller(int) price qty
  5760. 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];
  5761. // count
  5762. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5763. }
  5764. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  5765. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5766. if (!appDelegate.user) {
  5767. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5768. }
  5769. [ret setValue:filter forKey:@"filter"];
  5770. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  5771. sqlite3_stmt * statement;
  5772. [ret setValue:@"2" forKey:@"result"];
  5773. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5774. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5775. // int count=0;
  5776. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5777. {
  5778. int i=0;
  5779. while (sqlite3_step(statement) == SQLITE_ROW)
  5780. {
  5781. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5782. char *name = (char*)sqlite3_column_text(statement, 0);
  5783. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5784. char *description = (char*)sqlite3_column_text(statement, 1);
  5785. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5786. int product_id = sqlite3_column_int(statement, 2);
  5787. int wid = sqlite3_column_int(statement, 3);
  5788. int closeout = sqlite3_column_int(statement, 4);
  5789. int cid = sqlite3_column_int(statement, 5);
  5790. int wisdelete = sqlite3_column_int(statement, 6);
  5791. int more_color = sqlite3_column_int(statement, 7);
  5792. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5793. if(wid !=0 && wisdelete != 1)
  5794. [item setValue:@"true" forKey:@"wish_exists"];
  5795. else
  5796. [item setValue:@"false" forKey:@"wish_exists"];
  5797. if(closeout==0)
  5798. [item setValue:@"false" forKey:@"is_closeout"];
  5799. else
  5800. [item setValue:@"true" forKey:@"is_closeout"];
  5801. if(cid==0)
  5802. [item setValue:@"false" forKey:@"cart_exists"];
  5803. else
  5804. [item setValue:@"true" forKey:@"cart_exists"];
  5805. if (more_color == 0) {
  5806. [item setObject:@(false) forKey:@"more_color"];
  5807. } else if (more_color == 1) {
  5808. [item setObject:@(true) forKey:@"more_color"];
  5809. }
  5810. [item addEntriesFromDictionary:imgjson];
  5811. // [item setValue:nsurl forKey:@"img"];
  5812. [item setValue:nsname forKey:@"fash_name"];
  5813. [item setValue:nsdescription forKey:@"description"];
  5814. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5815. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5816. i++;
  5817. }
  5818. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5819. [ret setObject:items forKey:@"items"];
  5820. sqlite3_finalize(statement);
  5821. }
  5822. [iSalesDB close_db:db];
  5823. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  5824. return ret;
  5825. }
  5826. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  5827. {
  5828. return [self itemsearch:params limited:YES];
  5829. }
  5830. #pragma mark order detail
  5831. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  5832. if (str1.length == 0) {
  5833. str1 = @"&nbsp";
  5834. }
  5835. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  5836. return str;
  5837. }
  5838. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  5839. {
  5840. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  5841. [fromformatter setDateFormat:from];
  5842. NSDate *date = [fromformatter dateFromString:datetime];
  5843. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  5844. [toformatter setDateFormat:to];
  5845. NSString * ret = [toformatter stringFromDate:date];
  5846. return ret;
  5847. }
  5848. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  5849. // 把毫秒去掉
  5850. if ([dateTime containsString:@"."]) {
  5851. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  5852. }
  5853. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  5854. formatter.dateFormat = formate;
  5855. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  5856. NSDate *date = [formatter dateFromString:dateTime];
  5857. formatter.dateFormat = newFormate;
  5858. NSString *result = [formatter stringFromDate:date];
  5859. return result ? result : @"";
  5860. }
  5861. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  5862. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  5863. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  5864. }
  5865. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  5866. {
  5867. DebugLog(@"offline oderdetail params: %@",params);
  5868. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5869. int orderId = [params[@"orderId"] intValue];
  5870. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  5871. // decrypt card number and card security code
  5872. // 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 ];
  5873. 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,o.must_call 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,must_call from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  5874. sqlite3 *db = [iSalesDB get_db];
  5875. sqlite3_stmt * statement;
  5876. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  5877. NSString *nssoid = @"";
  5878. NSString* orderinfo = @"";
  5879. NSString *moreInfo = @"";
  5880. double handlingFee = 0;
  5881. double payments_and_credist = 0;
  5882. double totalPrice = 0;
  5883. double shippingFee = 0;
  5884. double lift_gate = 0;
  5885. NSString *customer_contact = @"";
  5886. NSString *customer_email = @"";
  5887. NSString *customer_fax = @"";
  5888. NSString *customer_phone = @"";
  5889. NSString *customer_city = @"";
  5890. NSString *customer_state = @"";
  5891. NSString *customer_zipcode = @"";
  5892. NSString *customer_country = @"";
  5893. BOOL must_call = NO;
  5894. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  5895. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  5896. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  5897. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  5898. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  5899. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  5900. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  5901. {
  5902. if (sqlite3_step(statement) == SQLITE_ROW)
  5903. {
  5904. // int order_id = sqlite3_column_int(statement, 0);
  5905. NSString *sign_url = [self textAtColumn:51 statement:statement];
  5906. // ret[@"sign_url"] = sign_url;
  5907. section_1 = @{
  5908. @"data":sign_url,
  5909. @"title":@"Signature",
  5910. @"type":@"sign_url"
  5911. }.mutableCopy;
  5912. [ret setObject:section_1 forKey:@"section_1"];
  5913. customer_contact = [self textAtColumn:52 statement:statement];
  5914. customer_email = [self textAtColumn:53 statement:statement];
  5915. customer_phone = [self textAtColumn:54 statement:statement];
  5916. customer_fax = [self textAtColumn:55 statement:statement];
  5917. customer_city = [self textAtColumn:60 statement:statement];
  5918. customer_state = [self textAtColumn:61 statement:statement];
  5919. customer_zipcode = [self textAtColumn:62 statement:statement];
  5920. customer_country = [self textAtColumn:63 statement:statement];
  5921. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  5922. int offline_edit=sqlite3_column_int(statement, 56);
  5923. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  5924. char *soid = (char*)sqlite3_column_text(statement, 1);
  5925. if(soid==nil)
  5926. soid= "";
  5927. nssoid= [[NSString alloc]initWithUTF8String:soid];
  5928. // so#
  5929. ret[@"so#"] = nssoid;
  5930. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  5931. if(poNumber==nil)
  5932. poNumber= "";
  5933. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  5934. char *create_time = (char*)sqlite3_column_text(statement, 3);
  5935. if(create_time==nil)
  5936. create_time= "";
  5937. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5938. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  5939. int status = sqlite3_column_int(statement, 4);
  5940. int erpStatus = sqlite3_column_int(statement, 49);
  5941. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5942. // status
  5943. if (status > 1 && status != 3) {
  5944. status = erpStatus;
  5945. } else if (status == 3) {
  5946. status = 15;
  5947. }
  5948. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  5949. ret[@"order_status"] = nsstatus;
  5950. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5951. if(company_name==nil)
  5952. company_name= "";
  5953. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5954. // company name
  5955. ret[@"company_name"] = nscompany_name;
  5956. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  5957. if(customer_contact==nil)
  5958. customer_contact= "";
  5959. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  5960. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  5961. if(addr_1==nil)
  5962. addr_1="";
  5963. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  5964. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  5965. if(addr_2==nil)
  5966. addr_2="";
  5967. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  5968. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  5969. if(addr_3==nil)
  5970. addr_3="";
  5971. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  5972. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  5973. if(addr_4==nil)
  5974. addr_4="";
  5975. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  5976. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  5977. [arr_addr addObject:nsaddr_1];
  5978. [arr_addr addObject:nsaddr_2];
  5979. [arr_addr addObject:nsaddr_3];
  5980. [arr_addr addObject:nsaddr_4];
  5981. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  5982. [arr_addr addObject:customer_state];
  5983. [arr_addr addObject:customer_zipcode];
  5984. [arr_addr addObject:customer_country];
  5985. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  5986. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  5987. char *logist = (char*)sqlite3_column_text(statement, 11);
  5988. if(logist==nil)
  5989. logist= "";
  5990. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  5991. if (/*status == -11 || */status == 10 || status == 11) {
  5992. nslogist = [self textAtColumn:59 statement:statement];
  5993. };
  5994. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  5995. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  5996. shipping = @"Shipping To Be Quoted";
  5997. } else {
  5998. shippingFee = sqlite3_column_double(statement, 12);
  5999. }
  6000. // Shipping
  6001. // ret[@"Shipping"] = shipping;
  6002. NSDictionary *shipping_item = @{
  6003. @"title":@"Shipping",
  6004. @"value":shipping
  6005. };
  6006. [price_data setObject:shipping_item forKey:@"item_1"];
  6007. int have_lift_gate = sqlite3_column_int(statement, 17);
  6008. lift_gate = sqlite3_column_double(statement, 13);
  6009. // Liftgate Fee(No loading dock)
  6010. if (!have_lift_gate) {
  6011. lift_gate = 0;
  6012. }
  6013. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6014. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6015. if (sqlite3_column_int(statement, 57)) {
  6016. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  6017. liftgate_value = @"Shipping To Be Quoted";
  6018. }
  6019. NSDictionary *liftgate_item = @{
  6020. @"title":@"Liftgate Fee(No loading dock)",
  6021. @"value":liftgate_value
  6022. };
  6023. [price_data setObject:liftgate_item forKey:@"item_2"];
  6024. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  6025. // [ret removeObjectForKey:@"Shipping"];
  6026. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6027. // }
  6028. //
  6029. // if (have_lift_gate) {
  6030. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6031. // }
  6032. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  6033. if(general_notes==nil)
  6034. general_notes= "";
  6035. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  6036. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  6037. if(internal_notes==nil)
  6038. internal_notes= "";
  6039. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  6040. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  6041. if(payment_type==nil)
  6042. payment_type= "";
  6043. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  6044. // order info
  6045. orderinfo = [self textFileName:@"order_info.html"];
  6046. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  6047. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  6048. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  6049. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  6050. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  6051. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  6052. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  6053. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  6054. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  6055. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  6056. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  6057. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  6058. NSString *payment = nil;
  6059. // id -> show
  6060. __block NSString *show_pay_type = nspayment_type;
  6061. [iSalesDB jk_query:[NSString stringWithFormat:@"select pay_type from payment_type where type_id = '%@';",nspayment_type] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6062. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  6063. if (show_typ_ch != NULL) {
  6064. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  6065. }
  6066. }];
  6067. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  6068. {
  6069. payment = [self textFileName:@"creditcardpayment.html"];
  6070. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  6071. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  6072. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  6073. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  6074. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  6075. NSString *card_type = [self textAtColumn:42 statement:statement];
  6076. if (card_type.length > 0) { // 显示星号
  6077. card_type = @"****";
  6078. }
  6079. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  6080. if (card_number.length > 0 && card_number.length > 4) {
  6081. for (int i = 0; i < card_number.length - 4; i++) {
  6082. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  6083. }
  6084. } else {
  6085. card_number = @"";
  6086. }
  6087. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  6088. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  6089. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  6090. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  6091. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  6092. card_expiration = @"****";
  6093. }
  6094. NSString *card_city = [self textAtColumn:46 statement:statement];
  6095. NSString *card_state = [self textAtColumn:47 statement:statement];
  6096. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  6097. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  6098. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  6099. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  6100. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  6101. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  6102. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  6103. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  6104. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  6105. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  6106. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  6107. }
  6108. else
  6109. {
  6110. payment=[self textFileName:@"normalpayment.html"];
  6111. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  6112. }
  6113. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  6114. ret[@"result"]= [NSNumber numberWithInt:2];
  6115. // more info
  6116. moreInfo = [self textFileName:@"more_info.html"];
  6117. /*****ship to******/
  6118. // ShipToCompany_or_&nbsp
  6119. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  6120. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  6121. NSString *shipToName = [self textAtColumn:19 statement:statement];
  6122. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  6123. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  6124. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6125. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  6126. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6127. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  6128. /*****ship from******/
  6129. // ShipFromCompany_or_&nbsp
  6130. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  6131. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  6132. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  6133. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  6134. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  6135. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6136. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6137. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6138. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  6139. /*****freight to******/
  6140. // FreightBillToCompany_or_&nbsp
  6141. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  6142. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  6143. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  6144. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  6145. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  6146. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6147. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6148. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6149. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  6150. /*****merchandise to******/
  6151. // MerchandiseBillToCompany_or_&nbsp
  6152. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  6153. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  6154. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  6155. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  6156. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  6157. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6158. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6159. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6160. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  6161. /*****return to******/
  6162. // ReturnToCompany_or_&nbsp
  6163. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  6164. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  6165. NSString *returnToName = [self textAtColumn:31 statement:statement];
  6166. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  6167. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  6168. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6169. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6170. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6171. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  6172. //
  6173. // DebugLog(@"more info : %@",moreInfo);
  6174. // handling fee
  6175. handlingFee = sqlite3_column_double(statement, 33);
  6176. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6177. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6178. if (sqlite3_column_int(statement, 58)) {
  6179. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  6180. handling_fee_value = @"Shipping To Be Quoted";
  6181. }
  6182. NSDictionary *handling_fee_item = @{
  6183. @"title":@"Handling Fee",
  6184. @"value":handling_fee_value
  6185. };
  6186. [price_data setObject:handling_fee_item forKey:@"item_3"];
  6187. //
  6188. // customer info
  6189. customerID = [self textAtColumn:36 statement:statement];
  6190. // mode
  6191. ret[@"mode"] = appDelegate.mode;
  6192. // model_count
  6193. ret[@"model_count"] = @(0);
  6194. }
  6195. sqlite3_finalize(statement);
  6196. }
  6197. [iSalesDB close_db:db];
  6198. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  6199. // "customer_email" = "Shui Hu";
  6200. // "customer_fax" = "";
  6201. // "customer_first_name" = F;
  6202. // "customer_last_name" = L;
  6203. // "customer_name" = ",da He Xiang Dong Liu A";
  6204. // "customer_phone" = "Hey Xuan Feng";
  6205. contactInfo[@"customer_phone"] = customer_phone;
  6206. contactInfo[@"customer_fax"] = customer_fax;
  6207. contactInfo[@"customer_email"] = customer_email;
  6208. NSString *first_name = @"";
  6209. NSString *last_name = @"";
  6210. if ([customer_contact isEqualToString:@""]) {
  6211. } else if ([customer_contact containsString:@" "]) {
  6212. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  6213. first_name = [customer_contact substringToIndex:first_space_index];
  6214. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  6215. }
  6216. contactInfo[@"customer_first_name"] = first_name;
  6217. contactInfo[@"customer_last_name"] = last_name;
  6218. ret[@"customerInfo"] = contactInfo;
  6219. // models
  6220. if (nssoid) {
  6221. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  6222. __block double TotalCuft = 0;
  6223. __block double TotalWeight = 0;
  6224. __block int TotalCarton = 0;
  6225. __block double allItemPrice = 0;
  6226. 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];
  6227. sqlite3 *db1 = [iSalesDB get_db];
  6228. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6229. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6230. int product_id = sqlite3_column_int(stmt, 0);
  6231. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6232. int item_id = sqlite3_column_int(stmt, 7);
  6233. NSString* Price=nil;
  6234. if(str_price==nil)
  6235. {
  6236. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  6237. if(price==nil)
  6238. Price=@"No Price.";
  6239. else
  6240. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6241. }
  6242. else
  6243. {
  6244. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6245. }
  6246. double discount = sqlite3_column_double(stmt, 2);
  6247. int item_count = sqlite3_column_int(stmt, 3);
  6248. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  6249. NSString *nsline_note=nil;
  6250. if(line_note!=nil)
  6251. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  6252. // char *name = (char*)sqlite3_column_text(stmt, 5);
  6253. // NSString *nsname = nil;
  6254. // if(name!=nil)
  6255. // nsname= [[NSString alloc]initWithUTF8String:name];
  6256. NSString *nsname = [self textAtColumn:5 statement:stmt];
  6257. // char *description = (char*)sqlite3_column_text(stmt, 6);
  6258. // NSString *nsdescription=nil;
  6259. // if(description!=nil)
  6260. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  6261. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  6262. // int stockUom = sqlite3_column_int(stmt, 8);
  6263. // int _id = sqlite3_column_int(stmt, 9);
  6264. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  6265. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6266. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6267. int carton=[bsubtotaljson[@"carton"] intValue];
  6268. TotalCuft += cuft;
  6269. TotalWeight += weight;
  6270. TotalCarton += carton;
  6271. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6272. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  6273. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  6274. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  6275. itemjson[@"note"]=nsline_note;
  6276. itemjson[@"origin_price"] = Price;
  6277. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6278. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6279. itemjson[@"order_item_status"] = @""; // 暂时不处理
  6280. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6281. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6282. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  6283. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6284. if(itemjson[@"combine"] != nil)
  6285. {
  6286. // int citem=0;
  6287. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6288. for(int bc=0;bc<bcount;bc++)
  6289. {
  6290. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6291. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6292. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6293. subTotal += uprice * modulus * item_count;
  6294. }
  6295. }
  6296. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6297. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  6298. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  6299. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6300. itemjson[@"type"] = @"order_item";
  6301. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  6302. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  6303. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6304. allItemPrice += subTotal;
  6305. }];
  6306. section_3[@"data"] = model_data;
  6307. section_3[@"type"] = @"sub_order";
  6308. section_3[@"title"] = @"Models";
  6309. section_3[@"switch"] = @(false);
  6310. ret[@"section_3"] = section_3;
  6311. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  6312. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  6313. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  6314. // payments/Credits
  6315. // payments_and_credist = sqlite3_column_double(statement, 34);
  6316. payments_and_credist = allItemPrice;
  6317. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6318. NSDictionary *sub_total_item = @{
  6319. @"title":@"Sub-Total",
  6320. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6321. };
  6322. [price_data setObject:sub_total_item forKey:@"item_0"];
  6323. // // total
  6324. // totalPrice = sqlite3_column_double(statement, 35);
  6325. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6326. } else {
  6327. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  6328. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  6329. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  6330. // payments/Credits
  6331. payments_and_credist = 0;
  6332. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6333. NSDictionary *sub_total_item = @{
  6334. @"title":@"Sub-Total",
  6335. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6336. };
  6337. [price_data setObject:sub_total_item forKey:@"item_0"];
  6338. }
  6339. // total
  6340. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6341. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6342. NSDictionary *total_item = @{
  6343. @"title":@"Total",
  6344. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  6345. };
  6346. [price_data setObject:total_item forKey:@"item_4"];
  6347. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  6348. section_4 = @{
  6349. @"data":price_data,
  6350. @"title":@"Price Info",
  6351. @"type":@"price_info"
  6352. }.mutableCopy;
  6353. ret[@"section_4"] = section_4;
  6354. // ret[@"order_info"]= orderinfo;
  6355. section_0 = @{
  6356. @"data":orderinfo,
  6357. @"title":@"Order Info",
  6358. @"type":@"order_info"
  6359. }.mutableCopy;
  6360. ret[@"section_0"] = section_0;
  6361. // ret[@"more_order_info"] = moreInfo;
  6362. section_2 = @{
  6363. @"data":moreInfo,
  6364. @"title":@"More Info",
  6365. @"type":@"more_order_info"
  6366. }.mutableCopy;
  6367. ret[@"section_2"] = section_2;
  6368. ret[@"count"] = @(5);
  6369. return [RAConvertor dict2data:ret];
  6370. }
  6371. #pragma mark order list
  6372. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  6373. NSString *nsstatus = @"";
  6374. switch (status) {
  6375. case 0:
  6376. {
  6377. nsstatus=@"Temp Order";
  6378. break;
  6379. }
  6380. case 1:
  6381. {
  6382. nsstatus=@"Saved Order";
  6383. break;
  6384. }
  6385. case 2: {
  6386. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  6387. break;
  6388. }
  6389. case 3:
  6390. case 15:
  6391. {
  6392. nsstatus=@"Cancelled";
  6393. break;
  6394. }
  6395. case 10:
  6396. {
  6397. nsstatus=@"Quote Submitted";
  6398. break;
  6399. }
  6400. case 11:
  6401. {
  6402. nsstatus=@"Sales Order Submitted";
  6403. break;
  6404. }
  6405. case 12:
  6406. {
  6407. nsstatus=@"Processing";
  6408. break;
  6409. }
  6410. case 13:
  6411. {
  6412. nsstatus=@"Shipped";
  6413. break;
  6414. }
  6415. case 14:
  6416. {
  6417. nsstatus=@"Closed";
  6418. break;
  6419. }
  6420. case -11:
  6421. {
  6422. nsstatus = @"Ready For Submit";
  6423. break;
  6424. }
  6425. default:
  6426. break;
  6427. }
  6428. return nsstatus;
  6429. }
  6430. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  6431. double total = 0;
  6432. __block double payments_and_credist = 0;
  6433. __block double allItemPrice = 0;
  6434. // sqlite3 *db1 = [iSalesDB get_db];
  6435. if (so_id) {
  6436. // 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];
  6437. 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];
  6438. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6439. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6440. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6441. int product_id = sqlite3_column_int(stmt, 0);
  6442. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6443. int discount = sqlite3_column_int(stmt, 2);
  6444. int item_count = sqlite3_column_int(stmt, 3);
  6445. // int item_id = sqlite3_column_int(stmt, 7);
  6446. int item_id = sqlite3_column_int(stmt, 4);
  6447. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  6448. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  6449. NSString* Price=nil;
  6450. if(str_price==nil)
  6451. {
  6452. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  6453. if(price==nil)
  6454. Price=@"No Price.";
  6455. else
  6456. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6457. }
  6458. else
  6459. {
  6460. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6461. }
  6462. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6463. itemjson[@"The unit price"]=Price;
  6464. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6465. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6466. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6467. if(itemjson[@"combine"] != nil)
  6468. {
  6469. // int citem=0;
  6470. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6471. for(int bc=0;bc<bcount;bc++)
  6472. {
  6473. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6474. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6475. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6476. subTotal += uprice * modulus * item_count;
  6477. }
  6478. }
  6479. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6480. allItemPrice += subTotal;
  6481. }];
  6482. payments_and_credist = allItemPrice;
  6483. } else {
  6484. // payments/Credits
  6485. payments_and_credist = 0;
  6486. }
  6487. // lift_gate handlingFee shippingFee
  6488. __block double lift_gate = 0;
  6489. __block double handlingFee = 0;
  6490. __block double shippingFee = 0;
  6491. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  6492. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6493. int have_lift_gate = sqlite3_column_int(stmt, 0);
  6494. if (have_lift_gate) {
  6495. lift_gate = sqlite3_column_double(stmt, 1);
  6496. }
  6497. handlingFee = sqlite3_column_double(stmt, 2);
  6498. shippingFee = sqlite3_column_double(stmt, 3);
  6499. }];
  6500. // total
  6501. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6502. if (close) {
  6503. [iSalesDB close_db:db1];
  6504. }
  6505. return total;
  6506. }
  6507. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  6508. {
  6509. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6510. int limit = [[params valueForKey:@"limit"] intValue];
  6511. int offset = [[params valueForKey:@"offset"] intValue];
  6512. NSString* keyword = [params valueForKey:@"keyWord"];
  6513. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  6514. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  6515. NSString* where=@"1 = 1";
  6516. if(keyword.length>0)
  6517. 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]];
  6518. if (orderStatus.length > 0) {
  6519. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  6520. if (order_status_array.count == 1) {
  6521. int status_value = [[order_status_array firstObject] intValue];
  6522. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  6523. if (status_value == 15 || status_value == 3) {
  6524. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  6525. } else {
  6526. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  6527. }
  6528. } else {
  6529. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  6530. }
  6531. } else if (order_status_array.count > 1) {
  6532. for (int i = 0; i < order_status_array.count;i++) {
  6533. NSString *status = order_status_array[i];
  6534. NSString *condition = @" or";
  6535. if (i == 0) {
  6536. condition = @" and (";
  6537. }
  6538. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  6539. int status_value = [status intValue];
  6540. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  6541. if (status_value == 15 || status_value == 3) {
  6542. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  6543. } else {
  6544. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  6545. }
  6546. } else {
  6547. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  6548. }
  6549. }
  6550. where = [where stringByAppendingString:@" )"];
  6551. }
  6552. }
  6553. 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];
  6554. // DebugLog(@"order list sql: %@",sqlQuery);
  6555. sqlite3 *db = [iSalesDB get_db];
  6556. sqlite3_stmt * statement;
  6557. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6558. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6559. {
  6560. int count=0;
  6561. while (sqlite3_step(statement) == SQLITE_ROW)
  6562. {
  6563. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  6564. int order_id = sqlite3_column_double(statement, 0);
  6565. char *soid = (char*)sqlite3_column_text(statement, 1);
  6566. if(soid==nil)
  6567. soid= "";
  6568. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  6569. int status = sqlite3_column_double(statement, 2);
  6570. int erpStatus = sqlite3_column_double(statement, 9);
  6571. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  6572. if(sales_rep==nil)
  6573. sales_rep= "";
  6574. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  6575. char *create_by = (char*)sqlite3_column_text(statement, 4);
  6576. if(create_by==nil)
  6577. create_by= "";
  6578. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  6579. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6580. if(company_name==nil)
  6581. company_name= "";
  6582. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6583. char *create_time = (char*)sqlite3_column_text(statement, 6);
  6584. if(create_time==nil)
  6585. create_time= "";
  6586. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6587. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  6588. // double total_price = sqlite3_column_double(statement, 7);
  6589. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  6590. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  6591. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6592. int offline_edit = sqlite3_column_int(statement, 10);
  6593. // ": "JH",
  6594. // "": "$8307.00",
  6595. // "": "MOB1608050001",
  6596. // "": "ArpithaT",
  6597. // "": "1st Stage Property Transformations",
  6598. // "": "JANICE SUTTON",
  6599. // "": 2255,
  6600. // "": "08/02/2016 09:49:18",
  6601. // "": 1,
  6602. // "": "Saved Order"
  6603. // "": "1470384050483",
  6604. // "model_count": "6 / 28"
  6605. item[@"sales_rep"]= nssales_rep;
  6606. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  6607. item[@"so#"]= nssoid;
  6608. item[@"create_by"]= nscreate_by;
  6609. item[@"customer_name"]= nscompany_name;
  6610. item[@"customer_contact"] = customer_contact;
  6611. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  6612. item[@"purchase_time"]= nscreate_time;
  6613. int statusCode = status;
  6614. if (statusCode == 2) {
  6615. statusCode = erpStatus;
  6616. } else if (statusCode == 3) {
  6617. statusCode = 15;
  6618. }
  6619. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  6620. item[@"order_status"]= nsstatus;
  6621. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  6622. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  6623. // item[@"model_count"]
  6624. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  6625. count++;
  6626. }
  6627. ret[@"count"]= [NSNumber numberWithInt:count];
  6628. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  6629. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  6630. ret[@"result"]= [NSNumber numberWithInt:2];
  6631. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  6632. ret[@"time_zone"] = @"PST";
  6633. sqlite3_finalize(statement);
  6634. }
  6635. 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];
  6636. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  6637. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6638. [iSalesDB close_db:db];
  6639. return [RAConvertor dict2data:ret];
  6640. }
  6641. #pragma mark update gnotes
  6642. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  6643. {
  6644. DebugLog(@"params: %@",params);
  6645. // comments = Meyoyoyoyoyoyoy;
  6646. // orderCode = MOB1608110001;
  6647. // password = 123456;
  6648. // user = EvanK;
  6649. 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]];
  6650. int ret = [iSalesDB execSql:sql];
  6651. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6652. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6653. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6654. // [dic setValue:@"160409" forKey:@"min_ver"];
  6655. return [RAConvertor dict2data:dic];
  6656. }
  6657. #pragma mark move to wishlist
  6658. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  6659. {
  6660. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6661. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6662. _id = [NSString stringWithFormat:@"(%@)",_id];
  6663. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  6664. sqlite3 *db = [iSalesDB get_db];
  6665. __block NSString *product_id = @"";
  6666. __block NSString *item_count_str = @"";
  6667. // __block NSString *item_id = nil;
  6668. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6669. // product_id = [self textAtColumn:0 statement:stmt];
  6670. int item_count = sqlite3_column_int(stmt, 1);
  6671. // item_id = [self textAtColumn:2 statement:stmt];
  6672. NSString *p_id = [self textAtColumn:0 statement:stmt];
  6673. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  6674. if (p_id.length) {
  6675. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  6676. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  6677. }
  6678. }];
  6679. [iSalesDB close_db:db];
  6680. // 去除第一个,
  6681. if (product_id.length > 1) {
  6682. product_id = [product_id substringFromIndex:1];
  6683. }
  6684. if (item_count_str.length > 1) {
  6685. item_count_str = [item_count_str substringFromIndex:1];
  6686. }
  6687. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6688. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  6689. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  6690. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  6691. sqlite3 *db1 = [iSalesDB get_db];
  6692. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  6693. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6694. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  6695. // 删除
  6696. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  6697. int ret = [iSalesDB execSql:deleteSql db:db1];
  6698. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6699. [iSalesDB close_db:db1];
  6700. return [RAConvertor dict2data:dic];
  6701. }
  6702. #pragma mark cart delete
  6703. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  6704. {
  6705. // cartItemId = 548;
  6706. // orderCode = MOB1608110001;
  6707. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6708. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6709. _id = [NSString stringWithFormat:@"(%@)",_id];
  6710. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  6711. int ret = [iSalesDB execSql:sql];
  6712. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6713. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6714. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6715. // [dic setValue:@"160409" forKey:@"min_ver"];
  6716. return [RAConvertor dict2data:dic];
  6717. }
  6718. #pragma mark set price
  6719. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  6720. {
  6721. DebugLog(@"cart set price params: %@",params);
  6722. // "cartitem_id" = 1;
  6723. // discount = "0.000000";
  6724. // "item_note" = "";
  6725. // price = "269.000000";
  6726. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6727. NSString *notes = [self valueInParams:params key:@"item_note"];
  6728. NSString *discount = [self valueInParams:params key:@"discount"];
  6729. NSString *price = [self valueInParams:params key:@"price"];
  6730. // bool badd_price_changed=false;
  6731. // sqlite3* db=[iSalesDB get_db];
  6732. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6733. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  6734. // NSRange range;
  6735. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  6736. //
  6737. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  6738. // badd_price_changed=true;
  6739. // [iSalesDB close_db:db];
  6740. //
  6741. // if(badd_price_changed)
  6742. // {
  6743. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  6744. // }
  6745. //
  6746. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  6747. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  6748. int ret = [iSalesDB execSql:sql];
  6749. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6750. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6751. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6752. // [dic setValue:@"160409" forKey:@"min_ver"];
  6753. return [RAConvertor dict2data:dic];
  6754. }
  6755. #pragma mark set line notes
  6756. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  6757. {
  6758. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6759. NSString *notes = [self valueInParams:params key:@"notes"];
  6760. notes = [self translateSingleQuote:notes];
  6761. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  6762. int ret = [iSalesDB execSql:sql];
  6763. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6764. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6765. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6766. // [dic setValue:@"160409" forKey:@"min_ver"];
  6767. return [RAConvertor dict2data:dic];
  6768. }
  6769. #pragma mark set qty
  6770. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  6771. {
  6772. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6773. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6774. int item_count = [params[@"inputInt"] intValue];
  6775. // 购买检查数量大于库存
  6776. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6777. if (!appDelegate.can_create_backorder) {
  6778. 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];
  6779. __block BOOL out_of_stock = NO;
  6780. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6781. int availability = sqlite3_column_int(stmt, 0);
  6782. if (availability < item_count) {
  6783. out_of_stock = YES;
  6784. }
  6785. }];
  6786. if (out_of_stock) { // 缺货
  6787. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  6788. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  6789. return [RAConvertor dict2data:dic];
  6790. }
  6791. }
  6792. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  6793. int ret = [iSalesDB execSql:sql];
  6794. __block int item_id = 0;
  6795. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6796. item_id = sqlite3_column_int(stmt, 0);
  6797. }];
  6798. sqlite3 *db = [iSalesDB get_db];
  6799. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6800. [iSalesDB close_db:db];
  6801. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6802. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6803. // [dic setValue:@"160409" forKey:@"min_ver"];
  6804. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  6805. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  6806. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  6807. return [RAConvertor dict2data:dic];
  6808. }
  6809. #pragma mark place order
  6810. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  6811. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6812. if (aname) {
  6813. [dic setValue:aname forKey:@"aname"];
  6814. }
  6815. if (control) {
  6816. [dic setValue:control forKey:@"control"];
  6817. }
  6818. if (keyboard) {
  6819. [dic setValue:keyboard forKey:@"keyboard"];
  6820. }
  6821. if (name) {
  6822. [dic setValue:name forKey:@"name"];
  6823. }
  6824. if (value) {
  6825. [dic setValue:value forKey:@"value"];
  6826. }
  6827. return dic;
  6828. }
  6829. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  6830. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6831. orderCode = [self translateSingleQuote:orderCode];
  6832. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  6833. 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];
  6834. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6835. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  6836. NSString *img0 = [self textAtColumn:1 statement:stmt];
  6837. NSString *img1 = [self textAtColumn:2 statement:stmt];
  6838. NSString *img2 = [self textAtColumn:3 statement:stmt];
  6839. NSString *company_name = [self textAtColumn:4 statement:stmt];
  6840. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  6841. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  6842. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  6843. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  6844. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  6845. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  6846. NSString *email = [self textAtColumn:11 statement:stmt];
  6847. NSString *phone = [self textAtColumn:12 statement:stmt];
  6848. NSString *fax = [self textAtColumn:13 statement:stmt];
  6849. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  6850. NSString *city = [self textAtColumn:15 statement:stmt];
  6851. NSString *state = [self textAtColumn:16 statement:stmt];
  6852. NSString *country = [self textAtColumn:17 statement:stmt];
  6853. NSString *name = [self textAtColumn:18 statement:stmt];
  6854. // contact id
  6855. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  6856. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  6857. [contact_id_dic setValue:@"text" forKey:@"control"];
  6858. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  6859. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  6860. [contact_id_dic setValue:@"true" forKey:@"required"];
  6861. [contact_id_dic setValue:contact_id forKey:@"value"];
  6862. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  6863. // business card
  6864. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  6865. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  6866. [business_card_dic setValue:@"img" forKey:@"control"];
  6867. [business_card_dic setValue:@"1" forKey:@"disable"];
  6868. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  6869. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  6870. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  6871. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  6872. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  6873. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  6874. [business_card_dic setValue:@"business_card" forKey:@"name"];
  6875. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  6876. // fax
  6877. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6878. [customer_dic setValue:fax_dic forKey:@"item_10"];
  6879. // zipcode
  6880. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  6881. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  6882. // city
  6883. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  6884. [customer_dic setValue:city_dic forKey:@"item_12"];
  6885. // state
  6886. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  6887. [customer_dic setValue:state_dic forKey:@"item_13"];
  6888. // country
  6889. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  6890. [customer_dic setValue:country_dic forKey:@"item_14"];
  6891. // company name
  6892. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  6893. [customer_dic setValue:company_dic forKey:@"item_2"];
  6894. // addr_1
  6895. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  6896. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  6897. // addr_2
  6898. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  6899. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  6900. // addr_3
  6901. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  6902. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  6903. // addr_4
  6904. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  6905. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  6906. // Contact
  6907. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  6908. [customer_dic setValue:contact_dic forKey:@"item_7"];
  6909. // email
  6910. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6911. [customer_dic setValue:email_dic forKey:@"item_8"];
  6912. // phone
  6913. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6914. [customer_dic setValue:phone_dic forKey:@"item_9"];
  6915. // title
  6916. [customer_dic setValue:@"Customer" forKey:@"title"];
  6917. // count
  6918. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  6919. }];
  6920. // setting
  6921. NSDictionary *setting = params[@"setting"];
  6922. NSNumber *hide = setting[@"CustomerHide"];
  6923. [customer_dic setValue:hide forKey:@"hide"];
  6924. return customer_dic;
  6925. }
  6926. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6927. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6928. // setting
  6929. NSDictionary *setting = params[@"setting"];
  6930. NSNumber *hide = setting[@"ShipToHide"];
  6931. [dic setValue:hide forKey:@"hide"];
  6932. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6933. orderCode = [self translateSingleQuote:orderCode];
  6934. 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];
  6935. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6936. NSString *cid = [self textAtColumn:0 statement:stmt];
  6937. NSString *name = [self textAtColumn:1 statement:stmt];
  6938. NSString *ext = [self textAtColumn:2 statement:stmt];
  6939. NSString *contact = [self textAtColumn:3 statement:stmt];
  6940. NSString *email = [self textAtColumn:4 statement:stmt];
  6941. NSString *fax = [self textAtColumn:5 statement:stmt];
  6942. NSString *phone = [self textAtColumn:6 statement:stmt];
  6943. // count
  6944. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6945. // title
  6946. [dic setValue:@"Ship To" forKey:@"title"];
  6947. // choose
  6948. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6949. [choose_dic setValue:@"choose" forKey:@"aname"];
  6950. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6951. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6952. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6953. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6954. @"receive_contact" : @"customer_contact",
  6955. @"receive_email" : @"customer_email",
  6956. @"receive_ext" : @"customer_contact_ext",
  6957. @"receive_fax" : @"customer_fax",
  6958. @"receive_name" : @"customer_name",
  6959. @"receive_phone" : @"customer_phone"},
  6960. @"refresh" : [NSNumber numberWithInteger:1],
  6961. @"type" : @"pull"};
  6962. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  6963. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  6964. @"name" : @"Add new address",
  6965. @"refresh" : [NSNumber numberWithInteger:1],
  6966. @"value" : @"new_addr"
  6967. };
  6968. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  6969. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  6970. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6971. @"receive_contact" : @"customer_contact",
  6972. @"receive_email" : @"customer_email",
  6973. @"receive_ext" : @"customer_contact_ext",
  6974. @"receive_fax" : @"customer_fax",
  6975. @"receive_name" : @"customer_name",
  6976. @"receive_phone" : @"customer_phone"},
  6977. @"name" : @"select_ship_to",
  6978. @"refresh" : [NSNumber numberWithInteger:1],
  6979. @"value" : @"Sales_Order_Ship_To"};
  6980. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  6981. [dic setValue:choose_dic forKey:@"item_0"];
  6982. // contact id
  6983. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6984. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6985. [contact_id_dic setValue:@"true" forKey:@"required"];
  6986. [dic setValue:contact_id_dic forKey:@"item_1"];
  6987. // company name
  6988. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6989. [dic setValue:company_name_dic forKey:@"item_2"];
  6990. // address
  6991. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6992. NSNumber *required = setting[@"ShippingToAddressRequire"];
  6993. if ([required integerValue]) {
  6994. [ext_dic setValue:@"true" forKey:@"required"];
  6995. }
  6996. [dic setValue:ext_dic forKey:@"item_3"];
  6997. // contact
  6998. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6999. [dic setValue:contact_dic forKey:@"item_4"];
  7000. // phone
  7001. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7002. [dic setValue:phone_dic forKey:@"item_5"];
  7003. // fax
  7004. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7005. [dic setValue:fax_dic forKey:@"item_6"];
  7006. // email
  7007. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7008. [dic setValue:email_dic forKey:@"item_7"];
  7009. }];
  7010. return dic;
  7011. }
  7012. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  7013. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7014. // setting
  7015. NSDictionary *setting = params[@"setting"];
  7016. NSNumber *hide = setting[@"ShipFromHide"];
  7017. [dic setValue:hide forKey:@"hide"];
  7018. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7019. orderCode = [self translateSingleQuote:orderCode];
  7020. 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];
  7021. 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';";
  7022. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7023. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  7024. __block NSString *name = [self textAtColumn:1 statement:stmt];
  7025. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  7026. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  7027. __block NSString *email = [self textAtColumn:4 statement:stmt];
  7028. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  7029. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  7030. if (!cid.length) {
  7031. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  7032. cid = [self textAtColumn:0 statement:statment];
  7033. name = [self textAtColumn:1 statement:statment];
  7034. ext = [self textAtColumn:2 statement:statment];
  7035. contact = [self textAtColumn:3 statement:statment];
  7036. email = [self textAtColumn:4 statement:statment];
  7037. fax = [self textAtColumn:5 statement:statment];
  7038. phone = [self textAtColumn:6 statement:statment];
  7039. }];
  7040. }
  7041. // count
  7042. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7043. // title
  7044. [dic setValue:@"Ship From" forKey:@"title"];
  7045. // hide
  7046. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7047. // choose
  7048. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7049. [choose_dic setValue:@"choose" forKey:@"aname"];
  7050. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7051. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7052. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  7053. @"key_map" : @{@"sender_cid" : @"customer_cid",
  7054. @"sender_contact" : @"customer_contact",
  7055. @"sender_email" : @"customer_email",
  7056. @"sender_ext" : @"customer_contact_ext",
  7057. @"sender_fax" : @"customer_fax",
  7058. @"sender_name" : @"customer_name",
  7059. @"sender_phone" : @"customer_phone"},
  7060. @"name" : @"select_cid",
  7061. @"refresh" : [NSNumber numberWithInteger:0],
  7062. @"value" : @"Sales_Order_Ship_From"};
  7063. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  7064. [dic setValue:choose_dic forKey:@"item_0"];
  7065. // contact id
  7066. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7067. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7068. [contact_id_dic setValue:@"true" forKey:@"required"];
  7069. [dic setValue:contact_id_dic forKey:@"item_1"];
  7070. // company name
  7071. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7072. [dic setValue:company_name_dic forKey:@"item_2"];
  7073. // address
  7074. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7075. [dic setValue:ext_dic forKey:@"item_3"];
  7076. // contact
  7077. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7078. [dic setValue:contact_dic forKey:@"item_4"];
  7079. // phone
  7080. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7081. [dic setValue:phone_dic forKey:@"item_5"];
  7082. // fax
  7083. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7084. [dic setValue:fax_dic forKey:@"item_6"];
  7085. // email
  7086. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7087. [dic setValue:email_dic forKey:@"item_7"];
  7088. }];
  7089. return dic;
  7090. }
  7091. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  7092. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7093. // setting
  7094. NSDictionary *setting = params[@"setting"];
  7095. NSNumber *hide = setting[@"FreightBillToHide"];
  7096. [dic setValue:hide forKey:@"hide"];
  7097. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7098. orderCode = [self translateSingleQuote:orderCode];
  7099. 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];
  7100. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7101. NSString *cid = [self textAtColumn:0 statement:stmt];
  7102. NSString *name = [self textAtColumn:1 statement:stmt];
  7103. NSString *ext = [self textAtColumn:2 statement:stmt];
  7104. NSString *contact = [self textAtColumn:3 statement:stmt];
  7105. NSString *email = [self textAtColumn:4 statement:stmt];
  7106. NSString *fax = [self textAtColumn:5 statement:stmt];
  7107. NSString *phone = [self textAtColumn:6 statement:stmt];
  7108. // count
  7109. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7110. // title
  7111. [dic setValue:@"Freight Bill To" forKey:@"title"];
  7112. // hide
  7113. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7114. // choose
  7115. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7116. [choose_dic setValue:@"choose" forKey:@"aname"];
  7117. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7118. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7119. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7120. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  7121. @"shipping_billto_contact" : @"receive_contact",
  7122. @"shipping_billto_email" : @"receive_email",
  7123. @"shipping_billto_ext" : @"receive_ext",
  7124. @"shipping_billto_fax" : @"receive_fax",
  7125. @"shipping_billto_name" : @"receive_name",
  7126. @"shipping_billto_phone" : @"receive_phone"},
  7127. @"type" : @"pull"};
  7128. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7129. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7130. @"type" : @"pull",
  7131. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  7132. @"shipping_billto_contact" : @"customer_contact",
  7133. @"shipping_billto_email" : @"customer_email",
  7134. @"shipping_billto_ext" : @"customer_contact_ext",
  7135. @"shipping_billto_fax" : @"customer_fax",
  7136. @"shipping_billto_name" : @"customer_name",
  7137. @"shipping_billto_phone" : @"customer_phone"}
  7138. };
  7139. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7140. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7141. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  7142. @"shipping_billto_contact" : @"sender_contact",
  7143. @"shipping_billto_email" : @"sender_email",
  7144. @"shipping_billto_ext" : @"sender_ext",
  7145. @"shipping_billto_fax" : @"sender_fax",
  7146. @"shipping_billto_name" : @"sender_name",
  7147. @"shipping_billto_phone" : @"sender_phone"},
  7148. @"type" : @"pull"
  7149. };
  7150. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  7151. NSDictionary *select_freight_bill_to_dic = @{
  7152. @"aname" : @"Select freight bill to",
  7153. @"name" : @"select_cid",
  7154. @"refresh" : [NSNumber numberWithInteger:0],
  7155. @"value" : @"Sales_Order_Freight_Bill_To",
  7156. @"key_map" : @{
  7157. @"shipping_billto_cid" : @"customer_cid",
  7158. @"shipping_billto_contact" : @"customer_contact",
  7159. @"shipping_billto_email" : @"customer_email",
  7160. @"shipping_billto_ext" : @"customer_contact_ext",
  7161. @"shipping_billto_fax" : @"customer_fax",
  7162. @"shipping_billto_name" : @"customer_name",
  7163. @"shipping_billto_phone" : @"customer_phone"
  7164. }
  7165. };
  7166. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  7167. [dic setValue:choose_dic forKey:@"item_0"];
  7168. // contact id
  7169. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7170. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7171. [contact_id_dic setValue:@"true" forKey:@"required"];
  7172. [dic setValue:contact_id_dic forKey:@"item_1"];
  7173. // company name
  7174. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7175. [dic setValue:company_name_dic forKey:@"item_2"];
  7176. // address
  7177. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7178. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  7179. if ([ext_required integerValue]) {
  7180. [ext_dic setValue:@"true" forKey:@"required"];
  7181. }
  7182. [dic setValue:ext_dic forKey:@"item_3"];
  7183. // contact
  7184. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7185. [dic setValue:contact_dic forKey:@"item_4"];
  7186. // phone
  7187. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7188. [dic setValue:phone_dic forKey:@"item_5"];
  7189. // fax
  7190. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7191. [dic setValue:fax_dic forKey:@"item_6"];
  7192. // email
  7193. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7194. [dic setValue:email_dic forKey:@"item_7"];
  7195. }];
  7196. return dic;
  7197. }
  7198. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7199. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7200. // setting
  7201. NSDictionary *setting = params[@"setting"];
  7202. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  7203. [dic setValue:hide forKey:@"hide"];
  7204. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7205. orderCode = [self translateSingleQuote:orderCode];
  7206. 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];
  7207. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7208. NSString *cid = [self textAtColumn:0 statement:stmt];
  7209. NSString *name = [self textAtColumn:1 statement:stmt];
  7210. NSString *ext = [self textAtColumn:2 statement:stmt];
  7211. NSString *contact = [self textAtColumn:3 statement:stmt];
  7212. NSString *email = [self textAtColumn:4 statement:stmt];
  7213. NSString *fax = [self textAtColumn:5 statement:stmt];
  7214. NSString *phone = [self textAtColumn:6 statement:stmt];
  7215. // count
  7216. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7217. // title
  7218. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  7219. // hide
  7220. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7221. // choose
  7222. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7223. [choose_dic setValue:@"choose" forKey:@"aname"];
  7224. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7225. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7226. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7227. @"key_map" : @{@"billing_cid" : @"receive_cid",
  7228. @"billing_contact" : @"receive_contact",
  7229. @"billing_email" : @"receive_email",
  7230. @"billing_ext" : @"receive_ext",
  7231. @"billing_fax" : @"receive_fax",
  7232. @"billing_name" : @"receive_name",
  7233. @"billing_phone" : @"receive_phone"},
  7234. @"type" : @"pull"};
  7235. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7236. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7237. @"type" : @"pull",
  7238. @"key_map" : @{@"billing_cid" : @"customer_cid",
  7239. @"billing_contact" : @"customer_contact",
  7240. @"billing_email" : @"customer_email",
  7241. @"billing_ext" : @"customer_contact_ext",
  7242. @"billing_fax" : @"customer_fax",
  7243. @"billing_name" : @"customer_name",
  7244. @"billing_phone" : @"customer_phone"}
  7245. };
  7246. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7247. NSDictionary *select_bill_to_dic = @{
  7248. @"aname" : @"Select bill to",
  7249. @"name" : @"select_cid",
  7250. @"refresh" : [NSNumber numberWithInteger:0],
  7251. @"value" : @"Sales_Order_Merchandise_Bill_To",
  7252. @"key_map" : @{
  7253. @"billing_cid" : @"customer_cid",
  7254. @"billing_contact" : @"customer_contact",
  7255. @"billing_email" : @"customer_email",
  7256. @"billing_ext" : @"customer_contact_ext",
  7257. @"billing_fax" : @"customer_fax",
  7258. @"billing_name" : @"customer_name",
  7259. @"billing_phone" : @"customer_phone"
  7260. }
  7261. };
  7262. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  7263. [dic setValue:choose_dic forKey:@"item_0"];
  7264. // contact id
  7265. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7266. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7267. [contact_id_dic setValue:@"true" forKey:@"required"];
  7268. [dic setValue:contact_id_dic forKey:@"item_1"];
  7269. // company name
  7270. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7271. [dic setValue:company_name_dic forKey:@"item_2"];
  7272. // address
  7273. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7274. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  7275. if ([ext_required integerValue]) {
  7276. [ext_dic setValue:@"true" forKey:@"required"];
  7277. }
  7278. [dic setValue:ext_dic forKey:@"item_3"];
  7279. // contact
  7280. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7281. [dic setValue:contact_dic forKey:@"item_4"];
  7282. // phone
  7283. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7284. [dic setValue:phone_dic forKey:@"item_5"];
  7285. // fax
  7286. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7287. [dic setValue:fax_dic forKey:@"item_6"];
  7288. // email
  7289. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7290. [dic setValue:email_dic forKey:@"item_7"];
  7291. }];
  7292. return dic;
  7293. }
  7294. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7295. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7296. // setting
  7297. NSDictionary *setting = params[@"setting"];
  7298. NSNumber *hide = setting[@"ReturnToHide"];
  7299. [dic setValue:hide forKey:@"hide"];
  7300. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7301. orderCode = [self translateSingleQuote:orderCode];
  7302. 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];
  7303. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7304. NSString *cid = [self textAtColumn:0 statement:stmt];
  7305. NSString *name = [self textAtColumn:1 statement:stmt];
  7306. NSString *ext = [self textAtColumn:2 statement:stmt];
  7307. NSString *contact = [self textAtColumn:3 statement:stmt];
  7308. NSString *email = [self textAtColumn:4 statement:stmt];
  7309. NSString *fax = [self textAtColumn:5 statement:stmt];
  7310. NSString *phone = [self textAtColumn:6 statement:stmt];
  7311. // count
  7312. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7313. // title
  7314. [dic setValue:@"Return To" forKey:@"title"];
  7315. // hide
  7316. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7317. // choose
  7318. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7319. [choose_dic setValue:@"choose" forKey:@"aname"];
  7320. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7321. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  7322. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7323. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  7324. @"returnto_contact" : @"sender_contact",
  7325. @"returnto_email" : @"sender_email",
  7326. @"returnto_ext" : @"sender_ext",
  7327. @"returnto_fax" : @"sender_fax",
  7328. @"returnto_name" : @"sender_name",
  7329. @"returnto_phone" : @"sender_phone"},
  7330. @"type" : @"pull"};
  7331. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  7332. NSDictionary *select_return_to_dic = @{
  7333. @"aname" : @"Select return to",
  7334. @"name" : @"select_cid",
  7335. @"refresh" : [NSNumber numberWithInteger:0],
  7336. @"value" : @"Contact_Return_To",
  7337. @"key_map" : @{
  7338. @"returnto_cid" : @"customer_cid",
  7339. @"returnto_contact" : @"customer_contact",
  7340. @"returnto_email" : @"customer_email",
  7341. @"returnto_ext" : @"customer_contact_ext",
  7342. @"returnto_fax" : @"customer_fax",
  7343. @"returnto_name" : @"customer_name",
  7344. @"returnto_phone" : @"customer_phone"
  7345. }
  7346. };
  7347. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  7348. [dic setValue:choose_dic forKey:@"item_0"];
  7349. // contact id
  7350. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7351. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7352. [contact_id_dic setValue:@"true" forKey:@"required"];
  7353. [dic setValue:contact_id_dic forKey:@"item_1"];
  7354. // company name
  7355. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7356. [dic setValue:company_name_dic forKey:@"item_2"];
  7357. // address
  7358. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7359. [dic setValue:ext_dic forKey:@"item_3"];
  7360. // contact
  7361. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7362. [dic setValue:contact_dic forKey:@"item_4"];
  7363. // phone
  7364. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7365. [dic setValue:phone_dic forKey:@"item_5"];
  7366. // fax
  7367. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7368. [dic setValue:fax_dic forKey:@"item_6"];
  7369. // email
  7370. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7371. [dic setValue:email_dic forKey:@"item_7"];
  7372. }];
  7373. return dic;
  7374. }
  7375. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7376. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7377. orderCode = [self translateSingleQuote:orderCode];
  7378. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7379. __block double TotalCuft = 0;
  7380. __block double TotalWeight = 0;
  7381. __block int TotalCarton = 0;
  7382. __block double payments = 0;
  7383. // setting
  7384. NSDictionary *setting = params[@"setting"];
  7385. NSNumber *hide = setting[@"ModelInformationHide"];
  7386. [dic setValue:hide forKey:@"hide"];
  7387. 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];
  7388. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7389. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7390. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  7391. // item id
  7392. int item_id = sqlite3_column_int(stmt, 0);
  7393. // count
  7394. int item_count = sqlite3_column_int(stmt, 1);
  7395. // stockUom
  7396. int stockUom = sqlite3_column_int(stmt, 2);
  7397. // unit price
  7398. NSString *str_price = [self textAtColumn:3 statement:stmt];
  7399. NSString* Price=nil;
  7400. if([str_price isEqualToString:@""])
  7401. {
  7402. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  7403. if(price==nil)
  7404. Price=@"No Price.";
  7405. else
  7406. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7407. }
  7408. else
  7409. {
  7410. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  7411. }
  7412. // discount
  7413. double discount = sqlite3_column_double(stmt, 4);
  7414. // name
  7415. NSString *name = [self textAtColumn:5 statement:stmt];
  7416. // description
  7417. NSString *description = [self textAtColumn:6 statement:stmt];
  7418. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  7419. // line note
  7420. NSString *line_note = [self textAtColumn:7 statement:stmt];
  7421. int avaulability = sqlite3_column_int(stmt, 8);
  7422. // img
  7423. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  7424. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  7425. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7426. if(combine != nil)
  7427. {
  7428. // int citem=0;
  7429. int bcount=[[combine valueForKey:@"count"] intValue];
  7430. for(int bc=0;bc<bcount;bc++)
  7431. {
  7432. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7433. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7434. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7435. subTotal += uprice * modulus * item_count;
  7436. }
  7437. }
  7438. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  7439. [model_dic setValue:@"model" forKey:@"control"];
  7440. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  7441. [model_dic setValue:description forKey:@"description"];
  7442. [model_dic setValue:line_note forKey:@"note"];
  7443. [model_dic setValue:img forKey:@"img_url"];
  7444. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  7445. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  7446. [model_dic setValue:Price forKey:@"unit_price"];
  7447. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  7448. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  7449. if (combine) {
  7450. [model_dic setValue:combine forKey:@"combine"];
  7451. }
  7452. // well,what under the row is the info for total
  7453. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7454. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7455. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7456. int carton=[bsubtotaljson[@"carton"] intValue];
  7457. TotalCuft += cuft;
  7458. TotalWeight += weight;
  7459. TotalCarton += carton;
  7460. payments += subTotal;
  7461. //---------------------------
  7462. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7463. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  7464. }];
  7465. [dic setValue:@"Model Information" forKey:@"title"];
  7466. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  7467. return dic;
  7468. }
  7469. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  7470. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7471. [dic setValue:@"Remarks Content" forKey:@"title"];
  7472. // setting
  7473. NSDictionary *setting = params[@"setting"];
  7474. NSNumber *hide = setting[@"RemarksContentHide"];
  7475. [dic setValue:hide forKey:@"hide"];
  7476. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7477. orderCode = [self translateSingleQuote:orderCode];
  7478. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  7479. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7480. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  7481. int mustCall = sqlite3_column_int(stmt, 1);
  7482. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  7483. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  7484. NSDictionary *po_dic = @{
  7485. @"aname" : @"PO#",
  7486. @"control" : @"edit",
  7487. @"keyboard" : @"text",
  7488. @"name" : @"poNumber",
  7489. @"value" : poNumber
  7490. };
  7491. NSDictionary *must_call_dic = @{
  7492. @"aname" : @"MUST CALL BEFORE DELIVERY",
  7493. @"control" : @"switch",
  7494. @"name" : @"must_call",
  7495. @"value" : mustCall ? @"true" : @"false"
  7496. };
  7497. NSDictionary *general_notes_dic = @{
  7498. @"aname" : @"General notes",
  7499. @"control" : @"text_view",
  7500. @"keyboard" : @"text",
  7501. @"name" : @"comments",
  7502. @"value" : generalNotes
  7503. };
  7504. // NSDictionary *internal_notes_dic = @{
  7505. // @"aname" : @"Internal notes",
  7506. // @"control" : @"text_view",
  7507. // @"keyboard" : @"text",
  7508. // @"name" : @"internal_notes",
  7509. // @"value" : internalNotes
  7510. // };
  7511. [dic setValue:po_dic forKey:@"item_0"];
  7512. [dic setValue:must_call_dic forKey:@"item_1"];
  7513. [dic setValue:general_notes_dic forKey:@"item_2"];
  7514. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  7515. }];
  7516. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7517. return dic;
  7518. }
  7519. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  7520. // params
  7521. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7522. orderCode = [self translateSingleQuote:orderCode];
  7523. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7524. // setting
  7525. NSDictionary *setting = params[@"setting"];
  7526. NSNumber *hide = setting[@"OrderTotalHide"];
  7527. [dic setValue:hide forKey:@"hide"];
  7528. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  7529. __block double lift_gate_value = 0;
  7530. __block double handling_fee = 0;
  7531. __block double shipping = 0;
  7532. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7533. int lift_gate = sqlite3_column_int(stmt, 0);
  7534. lift_gate_value = sqlite3_column_double(stmt, 1);
  7535. shipping = sqlite3_column_double(stmt, 2);
  7536. handling_fee = sqlite3_column_double(stmt, 3);
  7537. if (!lift_gate) {
  7538. lift_gate_value = 0;
  7539. }
  7540. }];
  7541. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  7542. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  7543. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  7544. double payments = [[total valueForKey:@"payments"] doubleValue];
  7545. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  7546. double totalPrice = payments;
  7547. [dic setValue:@"Order Total" forKey:@"title"];
  7548. NSDictionary *payments_dic = @{
  7549. @"align" : @"right",
  7550. @"aname" : @"Payments/Credits",
  7551. @"control" : @"text",
  7552. @"name" : @"paymentsAndCredits",
  7553. @"type" : @"price",
  7554. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  7555. };
  7556. [dic setValue:payments_dic forKey:@"item_0"];
  7557. // version 1.71 remove
  7558. // NSDictionary *handling_fee_dic = @{
  7559. // @"align" : @"right",
  7560. // @"aname" : @"Handling Fee",
  7561. // @"control" : @"text",
  7562. // @"name" : @"handling_fee_value",
  7563. // @"required" : @"true",
  7564. // @"type" : @"price",
  7565. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  7566. // };
  7567. NSDictionary *shipping_dic = @{
  7568. @"align" : @"right",
  7569. @"aname" : @"Shipping*",
  7570. @"control" : @"text",
  7571. @"name" : @"shipping",
  7572. @"required" : @"true",
  7573. @"type" : @"price",
  7574. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  7575. };
  7576. NSDictionary *lift_gate_dic = @{
  7577. @"align" : @"right",
  7578. @"aname" : @"Liftgate Fee(No Loading Dock)",
  7579. @"control" : @"text",
  7580. @"name" : @"lift_gate_value",
  7581. @"required" : @"true",
  7582. @"type" : @"price",
  7583. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  7584. };
  7585. int item_count = 1;
  7586. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  7587. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  7588. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  7589. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7590. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7591. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  7592. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7593. } else {
  7594. }
  7595. }
  7596. // version 1.71 remove
  7597. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7598. NSDictionary *total_price_dic = @{
  7599. @"align" : @"right",
  7600. @"aname" : @"Total",
  7601. @"control" : @"text",
  7602. @"name" : @"totalPrice",
  7603. @"type" : @"price",
  7604. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  7605. };
  7606. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7607. NSDictionary *total_cuft_dic = @{
  7608. @"align" : @"right",
  7609. @"aname" : @"Total Cuft",
  7610. @"control" : @"text",
  7611. @"name" : @"",
  7612. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  7613. };
  7614. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7615. NSDictionary *total_weight_dic = @{
  7616. @"align" : @"right",
  7617. @"aname" : @"Total Weight",
  7618. @"control" : @"text",
  7619. @"name" : @"",
  7620. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  7621. };
  7622. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7623. NSDictionary *total_carton_dic = @{
  7624. @"align" : @"right",
  7625. @"aname" : @"Total Carton",
  7626. @"control" : @"text",
  7627. @"name" : @"",
  7628. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  7629. };
  7630. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7631. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  7632. return dic;
  7633. }
  7634. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  7635. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7636. orderCode = [self translateSingleQuote:orderCode];
  7637. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7638. // setting
  7639. NSDictionary *setting = params[@"setting"];
  7640. NSNumber *hide = setting[@"SignatureHide"];
  7641. [dic setValue:hide forKey:@"hide"];
  7642. [dic setValue:@"Signature" forKey:@"title"];
  7643. __block NSString *pic_path = @"";
  7644. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  7645. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7646. pic_path = [self textAtColumn:0 statement:stmt];
  7647. }];
  7648. NSDictionary *pic_dic = @{
  7649. @"aname" : @"Signature",
  7650. @"avalue" :pic_path,
  7651. @"control" : @"signature",
  7652. @"name" : @"sign_picpath",
  7653. @"value" : pic_path
  7654. };
  7655. [dic setValue:pic_dic forKey:@"item_0"];
  7656. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7657. return dic;
  7658. }
  7659. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  7660. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7661. // setting
  7662. NSDictionary *setting = params[@"setting"];
  7663. NSNumber *hide = setting[@"ShippingMethodHide"];
  7664. [dic setValue:hide forKey:@"hide"];
  7665. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7666. orderCode = [self translateSingleQuote:orderCode];
  7667. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  7668. __block NSString *logist = @"";
  7669. __block NSString *lift_gate = @"";
  7670. __block int lift_gate_integer = 0;
  7671. __block NSString *logistic_note = @"";
  7672. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7673. logist = [self textAtColumn:0 statement:stmt];
  7674. lift_gate_integer = sqlite3_column_int(stmt, 1);
  7675. logistic_note = [self textAtColumn:2 statement:stmt];
  7676. }];
  7677. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7678. [dic setValue:@"Shipping Method" forKey:@"title"];
  7679. // val_0
  7680. int PERSONAL_PICK_UP_check = 0;
  7681. int USE_MY_CARRIER_check = 0;
  7682. NSString *logistic_note_text = @"";
  7683. int will_call_check = 0;
  7684. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  7685. will_call_check = 1;
  7686. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  7687. PERSONAL_PICK_UP_check = 1;
  7688. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  7689. USE_MY_CARRIER_check = 1;
  7690. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  7691. logistic_note = [logistic_note_array firstObject];
  7692. if (logistic_note_array.count > 1) {
  7693. logistic_note_text = [logistic_note_array lastObject];
  7694. }
  7695. }
  7696. }
  7697. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  7698. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  7699. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  7700. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  7701. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  7702. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7703. NSDictionary *val0_subItem_item0 = @{
  7704. @"aname" : @"Option",
  7705. @"cadedate" : @{
  7706. @"count" : [NSNumber numberWithInteger:2],
  7707. @"val_0" : @{
  7708. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  7709. @"refresh" : [NSNumber numberWithInteger:0],
  7710. @"value" : @"PERSONAL PICK UP",
  7711. @"value_id" : @"PERSONAL PICK UP"
  7712. },
  7713. @"val_1" : @{
  7714. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  7715. @"refresh" : [NSNumber numberWithInteger:0],
  7716. @"sub_item" : @{
  7717. @"count" : [NSNumber numberWithInteger:1],
  7718. @"item_0" : @{
  7719. @"aname" : @"BOL",
  7720. @"control" : @"edit",
  7721. @"keyboard" : @"text",
  7722. @"name" : @"logist_note_text",
  7723. @"refresh" : [NSNumber numberWithInteger:0],
  7724. @"required" : @"false",
  7725. @"value" : logistic_note_text
  7726. }
  7727. },
  7728. @"value" : @"USE MY CARRIER",
  7729. @"value_id" : @"USE MY CARRIER"
  7730. }
  7731. },
  7732. @"control" : @"enum",
  7733. @"name" : @"logistic_note",
  7734. @"required" : @"true",
  7735. @"single_select" : @"true"
  7736. };
  7737. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  7738. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  7739. // val_1
  7740. lift_gate = [self valueInParams:params key:@"lift_gate"];
  7741. if([lift_gate isEqualToString:@""]) {
  7742. if (lift_gate_integer == 1) {
  7743. lift_gate = @"true";
  7744. } else {
  7745. lift_gate = @"false";
  7746. }
  7747. }
  7748. int common_carrier_check = 0;
  7749. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  7750. common_carrier_check = 1;
  7751. [params setValue:lift_gate forKey:@"lift_gate"];
  7752. }
  7753. NSDictionary *val_1 = @{
  7754. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  7755. @"sub_item" : @{
  7756. @"count" : [NSNumber numberWithInteger:1],
  7757. @"item_0" : @{
  7758. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  7759. @"control" : @"switch",
  7760. @"name" : @"lift_gate",
  7761. @"refresh" : [NSNumber numberWithInteger:1],
  7762. @"required" : @"true",
  7763. @"value" : lift_gate
  7764. }
  7765. },
  7766. @"value" : @"COMMON CARRIER",
  7767. @"value_id" : @"COMMON CARRIER"
  7768. };
  7769. // cadedate
  7770. NSDictionary *cadedate = @{
  7771. @"count" : [NSNumber numberWithInteger:2],
  7772. @"val_0" : val_0,
  7773. @"val_1" : val_1
  7774. };
  7775. // item_0
  7776. NSMutableDictionary *item_0 = @{
  7777. @"aname" : @"Shipping",
  7778. @"cadedate" : cadedate,
  7779. @"control" : @"enum",
  7780. @"name" : @"logist",
  7781. @"refresh" : [NSNumber numberWithInteger:1],
  7782. @"single_select" : @"true",
  7783. }.mutableCopy;
  7784. NSNumber *required = setting[@"ShippingMethodRequire"];
  7785. if ([required integerValue]) {
  7786. [item_0 setValue:@"true" forKey:@"required"];
  7787. }
  7788. [dic setValue:item_0 forKey:@"item_0"];
  7789. if ([logist isEqualToString:@"WILL CALL"]) {
  7790. [dic removeObjectForKey:@"lift_gate"];
  7791. }
  7792. return dic;
  7793. }
  7794. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  7795. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7796. orderCode = [self translateSingleQuote:orderCode];
  7797. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  7798. __block int submit_as_integer = 0;
  7799. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7800. submit_as_integer = sqlite3_column_int(stmt, 0);
  7801. }];
  7802. int check11 = 0;
  7803. int check10 = 0;
  7804. if (submit_as_integer == 11) {
  7805. check11 = 1;
  7806. }
  7807. if (submit_as_integer == 10) {
  7808. check10 = 1;
  7809. }
  7810. // section_0
  7811. NSMutableDictionary *dic = @{
  7812. @"count" : @(1),
  7813. @"item_0" : @{
  7814. @"aname" : @"Submit Order As",
  7815. @"cadedate" : @{
  7816. @"count" : @(2),
  7817. @"val_0" : @{
  7818. @"check" : [NSNumber numberWithInteger:check11],
  7819. @"value" : @"Sales Order",
  7820. @"value_id" : @(11)
  7821. },
  7822. @"val_1" : @{
  7823. @"check" : [NSNumber numberWithInteger:check10],
  7824. @"value" : @"Quote",
  7825. @"value_id" : @(10)
  7826. }
  7827. },
  7828. @"control" : @"enum",
  7829. @"name" : @"erpOrderStatus",
  7830. @"required" : @"true",
  7831. @"single_select" : @"true"
  7832. },
  7833. @"title" : @"Order Type"
  7834. }.mutableCopy;
  7835. // setting
  7836. NSDictionary *setting = params[@"setting"];
  7837. NSNumber *hide = setting[@"OrderTypeHide"];
  7838. [dic setValue:hide forKey:@"hide"];
  7839. return dic;
  7840. }
  7841. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  7842. countryCode = [self translateSingleQuote:countryCode];
  7843. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  7844. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7845. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  7846. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  7847. if (name == NULL) {
  7848. name = "";
  7849. }
  7850. if (code == NULL) {
  7851. code = "";
  7852. }
  7853. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7854. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  7855. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  7856. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7857. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  7858. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7859. }
  7860. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  7861. [container setValue:stateDic forKey:key];
  7862. }] mutableCopy];
  7863. [ret removeObjectForKey:@"result"];
  7864. // failure 可以不用了,一样的
  7865. if (ret.allKeys.count == 0) {
  7866. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7867. [stateDic setValue:@"Other" forKey:@"value"];
  7868. [stateDic setValue:@"" forKey:@"value_id"];
  7869. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7870. if (state_code && [@"" isEqualToString:state_code]) {
  7871. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7872. }
  7873. NSString *key = [NSString stringWithFormat:@"val_0"];
  7874. [ret setValue:stateDic forKey:key];
  7875. }
  7876. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  7877. return ret;
  7878. }
  7879. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7880. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7881. orderCode = [self translateSingleQuote:orderCode];
  7882. 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];
  7883. __block NSString *payType = @"";
  7884. __block NSString *firstName = @"";
  7885. __block NSString *lastName = @"";
  7886. __block NSString *addr1 = @"";
  7887. __block NSString *addr2 = @"";
  7888. __block NSString *zipcode = @"";
  7889. __block NSString *cardType = @"";
  7890. __block NSString *cardNumber = @"";
  7891. __block NSString *securityCode = @"";
  7892. __block NSString *month = @"";
  7893. __block NSString *year = @"";
  7894. __block NSString *city = @"";
  7895. __block NSString *state = @"";
  7896. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7897. payType = [self textAtColumn:0 statement:stmt];
  7898. firstName = [self textAtColumn:1 statement:stmt];
  7899. lastName = [self textAtColumn:2 statement:stmt];
  7900. addr1 = [self textAtColumn:3 statement:stmt];
  7901. addr2 = [self textAtColumn:4 statement:stmt];
  7902. zipcode = [self textAtColumn:5 statement:stmt];
  7903. cardType = [self textAtColumn:6 statement:stmt];
  7904. cardNumber = [self textAtColumn:7 statement:stmt];
  7905. securityCode = [self textAtColumn:8 statement:stmt];
  7906. month = [self textAtColumn:9 statement:stmt];
  7907. year = [self textAtColumn:10 statement:stmt];
  7908. city = [self textAtColumn:11 statement:stmt];
  7909. state = [self textAtColumn:12 statement:stmt];
  7910. }];
  7911. NSString *required = @"true";
  7912. // setting
  7913. NSDictionary *setting = params[@"setting"];
  7914. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  7915. if ([requiredNumber integerValue]) {
  7916. required = @"true";
  7917. } else {
  7918. required = @"false";
  7919. }
  7920. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  7921. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  7922. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  7923. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7924. NSString *type_id = [self textAtColumn:0 statement:stmt];
  7925. NSString *type = [self textAtColumn:1 statement:stmt];
  7926. NSMutableDictionary *val = @{
  7927. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  7928. @"value" : type,
  7929. @"value_id" : type_id
  7930. }.mutableCopy;
  7931. if ([type_id isEqualToString:@"Visa/Master"] || [type_id isEqualToString:@"Credit Card"]) {
  7932. NSDictionary *sub_item = @{
  7933. @"count" : @(3),
  7934. @"item_0" : @{
  7935. @"aname" : @"choose",
  7936. @"control" : @"multi_action",
  7937. @"count" : @(1),
  7938. @"item_0" : @{
  7939. @"aname" : @"Same as customer",
  7940. @"key_map" : @{
  7941. @"credit_card_address1" : @"customer_address1",
  7942. @"credit_card_address2" : @"customer_address2",
  7943. @"credit_card_city" : @"customer_city",
  7944. @"credit_card_first_name" : @"customer_first_name",
  7945. @"credit_card_last_name" : @"customer_last_name",
  7946. @"credit_card_state" : @"customer_state",
  7947. @"credit_card_zipcode" : @"customer_zipcode"
  7948. },
  7949. @"type" : @"pull"
  7950. }
  7951. },
  7952. @"item_1" : @{
  7953. @"aname" : @"",
  7954. @"color" : @"red",
  7955. @"control" : @"text",
  7956. @"name" : @"",
  7957. @"value" : @"USA Credit cards only"
  7958. },
  7959. @"item_2" : @{
  7960. @"aname" : @"Fill",
  7961. @"cadedate" : @{
  7962. @"count" : @(2),
  7963. @"val_0" : @{
  7964. @"check" : @(1),
  7965. @"sub_item" : @{
  7966. @"count" : @(11),
  7967. @"item_0" : @{
  7968. @"aname" : @"Type",
  7969. @"cadedate" : @{
  7970. @"count" : @(2),
  7971. @"val_0" : @{
  7972. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  7973. @"value" : @"VISA",
  7974. @"value_id" : @"VISA"/*@(0)*/
  7975. },
  7976. @"val_1" : @{
  7977. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  7978. @"value" : @"MASTER CARD",
  7979. @"value_id" : @"MASTER CARD"/*@(1)*/
  7980. }
  7981. },
  7982. @"control" : @"enum",
  7983. @"name" : @"credit_card_type",
  7984. @"required" : @"true",
  7985. @"single_select" : @"true"
  7986. },
  7987. @"item_1" : @{
  7988. @"aname" : @"Number",
  7989. @"control" : @"edit",
  7990. @"keyboard" : @"int",
  7991. @"length" : @"16",
  7992. @"name" : @"credit_card_number",
  7993. @"required" : @"true",
  7994. @"value" : cardNumber
  7995. },
  7996. @"item_10" : @{
  7997. @"aname" : @"State",
  7998. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  7999. @"control" : @"enum",
  8000. @"enum" : @"true",
  8001. @"name" : @"credit_card_state",
  8002. @"required" : @"true",
  8003. @"single_select" : @"true"
  8004. },
  8005. @"item_2" : @{
  8006. @"aname" : @"Expiration Date",
  8007. @"control" : @"monthpicker",
  8008. @"name" : @"credit_card_expiration",
  8009. @"required" : @"true",
  8010. @"type" : @"date",
  8011. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8012. },
  8013. @"item_3" : @{
  8014. @"aname" : @"Security Code",
  8015. @"control" : @"edit",
  8016. @"keyboard" : @"int",
  8017. @"length" : @"3",
  8018. @"name" : @"credit_card_security_code",
  8019. @"required" : @"true",
  8020. @"value" : securityCode
  8021. },
  8022. @"item_4" : @{
  8023. @"aname" : @"First Name",
  8024. @"control" : @"edit",
  8025. @"keyboard" : @"text",
  8026. @"name" : @"credit_card_first_name",
  8027. @"required" : @"true",
  8028. @"value" : firstName
  8029. },
  8030. @"item_5" : @{
  8031. @"aname" : @"Last Name",
  8032. @"control" : @"edit",
  8033. @"keyboard" : @"text",
  8034. @"name" : @"credit_card_last_name",
  8035. @"required" : @"true",
  8036. @"value" : lastName
  8037. },
  8038. @"item_6" : @{
  8039. @"aname" : @"Address 1",
  8040. @"control" : @"edit",
  8041. @"keyboard" : @"text",
  8042. @"name" : @"credit_card_address1",
  8043. @"required" : @"true",
  8044. @"value" : addr1
  8045. },
  8046. @"item_7" : @{
  8047. @"aname" : @"Address 2",
  8048. @"control" : @"edit",
  8049. @"keyboard" : @"text",
  8050. @"name" : @"credit_card_address2",
  8051. @"value" : addr2
  8052. },
  8053. @"item_8" : @{
  8054. @"aname" : @"zip code",
  8055. @"control" : @"edit",
  8056. @"keyboard" : @"text",
  8057. @"name" : @"credit_card_zipcode",
  8058. @"required" : @"true",
  8059. @"value" : zipcode
  8060. },
  8061. @"item_9" : @{
  8062. @"aname" : @"City",
  8063. @"control" : @"edit",
  8064. @"keyboard" : @"text",
  8065. @"name" : @"credit_card_city",
  8066. @"required" : @"true",
  8067. @"value" : city
  8068. }
  8069. },
  8070. @"value" : @"Fill Now",
  8071. @"value_id" : @""
  8072. },
  8073. @"val_1" : @{
  8074. @"check" : @(0),
  8075. @"value" : @"Fill Later",
  8076. @"value_id" : @""
  8077. }
  8078. },
  8079. @"control" : @"enum",
  8080. @"name" : @"",
  8081. @"single_select" : @"true"
  8082. }
  8083. };
  8084. [val setObject:sub_item forKey:@"sub_item"];
  8085. }
  8086. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  8087. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  8088. }];
  8089. // "section_2"
  8090. NSMutableDictionary *dic = @{
  8091. @"count" : @(1),
  8092. @"item_0" : @{
  8093. @"aname" : @"Payment",
  8094. @"required" : required,
  8095. @"cadedate" : cadedate,
  8096. // @{
  8097. // @"count" : @(6),
  8098. // @"val_3" : @{
  8099. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  8100. // @"value" : @"Check",
  8101. // @"value_id" : @"Check"
  8102. // },
  8103. // @"val_2" : @{
  8104. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  8105. // @"value" : @"Cash",
  8106. // @"value_id" : @"Cash"
  8107. // },
  8108. // @"val_1" : @{
  8109. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  8110. // @"value" : @"NET 60",
  8111. // @"value_id" : @"NET 30"
  8112. // },
  8113. // @"val_4" : @{
  8114. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  8115. // @"value" : @"Wire Transfer",
  8116. // @"value_id" : @"Wire Transfer"
  8117. // },
  8118. // @"val_0" : @{
  8119. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  8120. // @"sub_item" : @{
  8121. // @"count" : @(3),
  8122. // @"item_0" : @{
  8123. // @"aname" : @"choose",
  8124. // @"control" : @"multi_action",
  8125. // @"count" : @(1),
  8126. // @"item_0" : @{
  8127. // @"aname" : @"Same as customer",
  8128. // @"key_map" : @{
  8129. // @"credit_card_address1" : @"customer_address1",
  8130. // @"credit_card_address2" : @"customer_address2",
  8131. // @"credit_card_city" : @"customer_city",
  8132. // @"credit_card_first_name" : @"customer_first_name",
  8133. // @"credit_card_last_name" : @"customer_last_name",
  8134. // @"credit_card_state" : @"customer_state",
  8135. // @"credit_card_zipcode" : @"customer_zipcode"
  8136. // },
  8137. // @"type" : @"pull"
  8138. // }
  8139. // },
  8140. // @"item_1" : @{
  8141. // @"aname" : @"",
  8142. // @"color" : @"red",
  8143. // @"control" : @"text",
  8144. // @"name" : @"",
  8145. // @"value" : @"USA Credit cards only"
  8146. // },
  8147. // @"item_2" : @{
  8148. // @"aname" : @"Fill",
  8149. // @"cadedate" : @{
  8150. // @"count" : @(2),
  8151. // @"val_0" : @{
  8152. // @"check" : @(1),
  8153. // @"sub_item" : @{
  8154. // @"count" : @(11),
  8155. // @"item_0" : @{
  8156. // @"aname" : @"Type",
  8157. // @"cadedate" : @{
  8158. // @"count" : @(2),
  8159. // @"val_0" : @{
  8160. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8161. // @"value" : @"VISA",
  8162. // @"value_id" : @(0)
  8163. // },
  8164. // @"val_1" : @{
  8165. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8166. // @"value" : @"MASTER CARD",
  8167. // @"value_id" : @(1)
  8168. // }
  8169. // },
  8170. // @"control" : @"enum",
  8171. // @"name" : @"credit_card_type",
  8172. // @"required" : @"true",
  8173. // @"single_select" : @"true"
  8174. // },
  8175. // @"item_1" : @{
  8176. // @"aname" : @"Number",
  8177. // @"control" : @"edit",
  8178. // @"keyboard" : @"int",
  8179. // @"length" : @"16",
  8180. // @"name" : @"credit_card_number",
  8181. // @"required" : @"true",
  8182. // @"value" : cardNumber
  8183. // },
  8184. // @"item_10" : @{
  8185. // @"aname" : @"State",
  8186. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8187. // @"control" : @"enum",
  8188. // @"enum" : @"true",
  8189. // @"name" : @"credit_card_state",
  8190. // @"required" : @"true",
  8191. // @"single_select" : @"true"
  8192. // },
  8193. // @"item_2" : @{
  8194. // @"aname" : @"Expiration Date",
  8195. // @"control" : @"monthpicker",
  8196. // @"name" : @"credit_card_expiration",
  8197. // @"required" : @"true",
  8198. // @"type" : @"date",
  8199. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8200. // },
  8201. // @"item_3" : @{
  8202. // @"aname" : @"Security Code",
  8203. // @"control" : @"edit",
  8204. // @"keyboard" : @"int",
  8205. // @"length" : @"3",
  8206. // @"name" : @"credit_card_security_code",
  8207. // @"required" : @"true",
  8208. // @"value" : securityCode
  8209. // },
  8210. // @"item_4" : @{
  8211. // @"aname" : @"First Name",
  8212. // @"control" : @"edit",
  8213. // @"keyboard" : @"text",
  8214. // @"name" : @"credit_card_first_name",
  8215. // @"required" : @"true",
  8216. // @"value" : firstName
  8217. // },
  8218. // @"item_5" : @{
  8219. // @"aname" : @"Last Name",
  8220. // @"control" : @"edit",
  8221. // @"keyboard" : @"text",
  8222. // @"name" : @"credit_card_last_name",
  8223. // @"required" : @"true",
  8224. // @"value" : lastName
  8225. // },
  8226. // @"item_6" : @{
  8227. // @"aname" : @"Address 1",
  8228. // @"control" : @"edit",
  8229. // @"keyboard" : @"text",
  8230. // @"name" : @"credit_card_address1",
  8231. // @"required" : @"true",
  8232. // @"value" : addr1
  8233. // },
  8234. // @"item_7" : @{
  8235. // @"aname" : @"Address 2",
  8236. // @"control" : @"edit",
  8237. // @"keyboard" : @"text",
  8238. // @"name" : @"credit_card_address2",
  8239. // @"value" : addr2
  8240. // },
  8241. // @"item_8" : @{
  8242. // @"aname" : @"zip code",
  8243. // @"control" : @"edit",
  8244. // @"keyboard" : @"text",
  8245. // @"name" : @"credit_card_zipcode",
  8246. // @"required" : @"true",
  8247. // @"value" : zipcode
  8248. // },
  8249. // @"item_9" : @{
  8250. // @"aname" : @"City",
  8251. // @"control" : @"edit",
  8252. // @"keyboard" : @"text",
  8253. // @"name" : @"credit_card_city",
  8254. // @"required" : @"true",
  8255. // @"value" : city
  8256. // }
  8257. // },
  8258. // @"value" : @"Fill Now",
  8259. // @"value_id" : @""
  8260. // },
  8261. // @"val_1" : @{
  8262. // @"check" : @(0),
  8263. // @"value" : @"Fill Later",
  8264. // @"value_id" : @""
  8265. // }
  8266. // },
  8267. // @"control" : @"enum",
  8268. // @"name" : @"",
  8269. // @"single_select" : @"true"
  8270. // }
  8271. // },
  8272. // @"value" : @"Visa/Master",
  8273. // @"value_id" : @"Credit Card"
  8274. // },
  8275. // @"val_5" : @{
  8276. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  8277. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  8278. // @"value_id" : @"FOLLOW EXISTING"
  8279. // }
  8280. // },
  8281. @"control" : @"enum",
  8282. @"name" : @"paymentType",
  8283. @"single_select" : @"true"
  8284. },
  8285. @"title" : @"Payment Information"
  8286. }.mutableCopy;
  8287. NSNumber *hide = setting[@"PaymentInformationHide"];
  8288. [dic setValue:hide forKey:@"hide"];
  8289. return dic;
  8290. }
  8291. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  8292. // params
  8293. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8294. orderCode = [self translateSingleQuote:orderCode];
  8295. // 缺货检查
  8296. 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];
  8297. __block BOOL outOfStock = NO;
  8298. sqlite3 *database = db;
  8299. if (!db) {
  8300. database = [iSalesDB get_db];
  8301. }
  8302. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8303. int availability = sqlite3_column_int(stmt, 0);
  8304. int item_qty = sqlite3_column_int(stmt, 1);
  8305. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  8306. }];
  8307. if (!db) {
  8308. [iSalesDB close_db:database];
  8309. }
  8310. return outOfStock;
  8311. }
  8312. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  8313. {
  8314. sqlite3 *db = [iSalesDB get_db];
  8315. // params
  8316. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8317. orderCode = [self translateSingleQuote:orderCode];
  8318. // 缺货检查
  8319. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8320. if (!appDelegate.can_create_backorder) {
  8321. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  8322. if (out_of_stock) {
  8323. [iSalesDB close_db:db];
  8324. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  8325. [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"];
  8326. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  8327. return [RAConvertor dict2data:resultDic];
  8328. }
  8329. }
  8330. // UISetting
  8331. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8332. NSString *cachefolder = [paths objectAtIndex:0];
  8333. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  8334. NSData* json =nil;
  8335. json=[NSData dataWithContentsOfFile:img_cache];
  8336. NSError *error=nil;
  8337. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  8338. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  8339. [params setObject:setting forKey:@"setting"];
  8340. int section_count = 0;
  8341. // 0 Order Type 1 Shipping Method 2 Payment Information
  8342. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  8343. // 0 Order Type
  8344. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  8345. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  8346. [ret setValue:order_type_dic forKey:key0];
  8347. // 1 Shipping Method
  8348. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  8349. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  8350. [ret setValue:shipping_method_dic forKey:key1];
  8351. // 2 Payment Information
  8352. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  8353. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  8354. [ret setValue:payment_info_dic forKey:key2];
  8355. // 3 Customer
  8356. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  8357. NSDictionary *customer_dic = [self customerDic:params db:db];
  8358. [ret setValue:customer_dic forKey:key3];
  8359. // 4 Ship To
  8360. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  8361. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  8362. [ret setValue:ship_to_dic forKey:key4];
  8363. // 5 Ship From
  8364. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  8365. if (![shipFromDisable integerValue]) {
  8366. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  8367. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  8368. [ret setValue:ship_from_dic forKey:key5];
  8369. }
  8370. // 6 Freight Bill To
  8371. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  8372. if (![freightBillToDisable integerValue]) {
  8373. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  8374. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  8375. [ret setValue:freight_bill_to forKey:key6];
  8376. }
  8377. // 7 Merchandise Bill To
  8378. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  8379. if (![merchandiseBillToDisable integerValue]) {
  8380. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  8381. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  8382. [ret setValue:merchandise_bill_to_dic forKey:key7];
  8383. }
  8384. // 8 Return To
  8385. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  8386. if (![returnToDisable integerValue]) {
  8387. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  8388. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  8389. [ret setValue:return_to_dic forKey:key8];
  8390. }
  8391. // 9 Model Information
  8392. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  8393. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  8394. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  8395. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  8396. [ret setValue:model_info_dic forKey:key9];
  8397. // 10 Remarks Content
  8398. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  8399. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  8400. [ret setValue:remarks_content_dic forKey:key10];
  8401. // 11 Order Total
  8402. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  8403. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  8404. [ret setValue:order_total_dic forKey:key11];
  8405. // 12 Signature
  8406. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  8407. NSDictionary *sign_dic = [self signatureDic:params db:db];
  8408. [ret setValue:sign_dic forKey:key12];
  8409. [ret setValue:@(section_count) forKey:@"section_count"];
  8410. [iSalesDB close_db:db];
  8411. return [RAConvertor dict2data:ret];
  8412. // return nil;
  8413. }
  8414. #pragma mark addr editor
  8415. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  8416. NSMutableDictionary *new_item = [item mutableCopy];
  8417. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  8418. return new_item;
  8419. }
  8420. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  8421. {
  8422. // "is_subaction" = true;
  8423. // orderCode = MOB1608240002;
  8424. // password = 123456;
  8425. // "subaction_tag" = 1;
  8426. // user = EvanK;
  8427. // {
  8428. // "is_subaction" = true;
  8429. // orderCode = MOB1608240002;
  8430. // password = 123456;
  8431. // "refresh_trigger" = zipcode;
  8432. // "subaction_tag" = 1;
  8433. // user = EvanK;
  8434. // }
  8435. NSString *country_code = nil;
  8436. NSString *zipCode = nil;
  8437. NSString *stateCode = nil;
  8438. NSString *city = nil;
  8439. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  8440. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  8441. NSString *code_id = params[@"country"];
  8442. country_code = [self countryCodeByid:code_id];
  8443. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  8444. NSString *zip_code = params[@"zipcode"];
  8445. // 剔除全部为空格
  8446. int spaceCount = 0;
  8447. for (int i = 0; i < zip_code.length; i++) {
  8448. if ([zip_code characterAtIndex:i] == ' ') {
  8449. spaceCount++;
  8450. }
  8451. }
  8452. if (spaceCount == zip_code.length) {
  8453. zip_code = @"";
  8454. }
  8455. zipCode = zip_code;
  8456. if (zipCode.length > 0) {
  8457. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  8458. country_code = [dic valueForKey:@"country_code"];
  8459. if (!country_code) {
  8460. // country_code = @"US";
  8461. NSString *code_id = params[@"country"];
  8462. country_code = [self countryCodeByid:code_id];
  8463. }
  8464. stateCode = [dic valueForKey:@"state_code"];
  8465. if(!stateCode.length) {
  8466. stateCode = params[@"state"];
  8467. }
  8468. city = [dic valueForKey:@"city"];
  8469. if (!city.length) {
  8470. city = params[@"city"];
  8471. }
  8472. // zip code
  8473. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  8474. // [zipDic setValue:zipCode forKey:@"value"];
  8475. // [section_0 setValue:zipDic forKey:@"item_11"];
  8476. } else {
  8477. NSString *code_id = params[@"country"];
  8478. country_code = [self countryCodeByid:code_id];
  8479. stateCode = params[@"state"];
  8480. city = params[@"city"];
  8481. }
  8482. }
  8483. }
  8484. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  8485. // [ret removeObjectForKey:@"up_params"];
  8486. [ret setObject:@"New Address" forKey:@"title"];
  8487. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  8488. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  8489. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  8490. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  8491. // [country_dic removeObjectForKey:@"refresh"];
  8492. // [country_dic removeObjectForKey:@"restore"];
  8493. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  8494. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  8495. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  8496. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  8497. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  8498. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  8499. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  8500. // [zip_code_dic removeObjectForKey:@"refresh"];
  8501. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  8502. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  8503. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  8504. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  8505. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  8506. [cityDic setValue:city ? city : @"" forKey:@"value"];
  8507. [new_section_0 setObject:cityDic forKey:@"item_8"];
  8508. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  8509. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  8510. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  8511. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  8512. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  8513. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  8514. // country
  8515. NSString *countryCode = country_code == nil ? @"US" : country_code;
  8516. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  8517. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  8518. // state
  8519. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  8520. // NSDictionary *tmpDic = @{
  8521. // @"value" : @"Other",
  8522. // @"value_id" : @"",
  8523. // @"check" : [NSNumber numberWithInteger:0]
  8524. // };
  8525. //
  8526. // for (int i = 0; i < allState.allKeys.count; i++) {
  8527. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  8528. //
  8529. // NSDictionary *tmp = allState[key];
  8530. // [allState setValue:tmpDic forKey:key];
  8531. // tmpDic = tmp;
  8532. // }
  8533. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  8534. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  8535. [ret setValue:new_section_0 forKey:@"section_0"];
  8536. return [RAConvertor dict2data:ret];
  8537. }
  8538. #pragma mark save addr
  8539. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  8540. {
  8541. // NSString *companyName = [self valueInParams:params key:@"company"];
  8542. // NSString *addr1 = [self valueInParams:params key:@"address"];
  8543. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  8544. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  8545. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  8546. // NSString *countryId = [self valueInParams:params key:@"country"];
  8547. // NSString *stateCode = [self valueInParams:params key:@"state"];
  8548. // NSString *city = [self valueInParams:params key:@"city"];
  8549. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  8550. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  8551. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  8552. // NSString *phone = [self valueInParams:params key:@"phone"];
  8553. // NSString *fax = [self valueInParams:params key:@"fax"];
  8554. // NSString *email = [self valueInParams:params key:@"email"];
  8555. return [self offline_saveContact:params update:NO isCustomer:NO];
  8556. }
  8557. #pragma mark cancel order
  8558. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  8559. {
  8560. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8561. NSString *order_id = [self valueInParams:params key:@"orderId"];
  8562. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  8563. if (order_id.length) {
  8564. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  8565. }
  8566. int ret = [iSalesDB execSql:sql];
  8567. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8568. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8569. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8570. // [dic setValue:@"160409" forKey:@"min_ver"];
  8571. return [RAConvertor dict2data:dic];
  8572. }
  8573. #pragma mark sign order
  8574. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  8575. {
  8576. //参考 offline_saveBusinesscard
  8577. DebugLog(@"sign order params: %@",params);
  8578. // orderCode = MOB1608240002;
  8579. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  8580. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8581. orderCode = [self translateSingleQuote:orderCode];
  8582. NSString *picPath = [self valueInParams:params key:@"picpath"];
  8583. picPath = [self translateSingleQuote:picPath];
  8584. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  8585. int ret = [iSalesDB execSql:sql];
  8586. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8587. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8588. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8589. // [dic setValue:@"160409" forKey:@"min_ver"];
  8590. return [RAConvertor dict2data:dic];
  8591. }
  8592. #pragma mark save order
  8593. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  8594. NSString *ret = [self valueInParams:params key:key];
  8595. if (translate) {
  8596. ret = [self translateSingleQuote:ret];
  8597. }
  8598. return ret;
  8599. }
  8600. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  8601. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  8602. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  8603. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  8604. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  8605. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  8606. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  8607. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  8608. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  8609. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  8610. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  8611. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  8612. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  8613. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  8614. if (![total_price isEqualToString:@""]) {
  8615. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  8616. } else {
  8617. total_price = @"";
  8618. }
  8619. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  8620. if ([paymentsAndCredits isEqualToString:@""]) {
  8621. paymentsAndCredits = @"";
  8622. } else {
  8623. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  8624. }
  8625. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  8626. if ([handling_fee_value isEqualToString:@""]) {
  8627. handling_fee_value = @"";
  8628. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  8629. handling_fee_value = @"";
  8630. } else {
  8631. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  8632. }
  8633. NSString *handling_fee_placeholder = handling_fee_value;
  8634. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  8635. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  8636. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  8637. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  8638. NSString *lift_gate_placeholder = @"";
  8639. if ([lift_gate_value isEqualToString:@""]) {
  8640. lift_gate_placeholder = @"";
  8641. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  8642. lift_gate_placeholder = @"";
  8643. } else {
  8644. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  8645. }
  8646. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  8647. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  8648. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  8649. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  8650. 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];
  8651. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  8652. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  8653. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  8654. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  8655. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  8656. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  8657. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  8658. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  8659. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  8660. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  8661. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  8662. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  8663. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  8664. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  8665. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  8666. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  8667. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  8668. BOOL number_nil = [credit_card_number isEqualToString:@""];
  8669. if (!number_nil) {
  8670. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  8671. }
  8672. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  8673. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  8674. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  8675. if (!security_code_nil) {
  8676. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  8677. }
  8678. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  8679. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  8680. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  8681. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  8682. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  8683. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  8684. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  8685. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  8686. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  8687. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  8688. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  8689. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  8690. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  8691. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  8692. NSString *contact_id = customer_cid;
  8693. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  8694. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  8695. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  8696. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  8697. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  8698. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  8699. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  8700. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  8701. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  8702. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  8703. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  8704. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  8705. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  8706. //
  8707. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  8708. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  8709. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  8710. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  8711. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  8712. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  8713. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  8714. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  8715. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  8716. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  8717. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  8718. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  8719. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  8720. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  8721. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  8722. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  8723. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  8724. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  8725. 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];
  8726. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  8727. if (receive_cid.length) {
  8728. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  8729. }
  8730. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  8731. if (receive_name.length) {
  8732. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  8733. }
  8734. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  8735. if (receive_ext.length) {
  8736. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  8737. }
  8738. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  8739. if (receive_contact.length) {
  8740. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  8741. }
  8742. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  8743. if (receive_phone.length) {
  8744. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  8745. }
  8746. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  8747. if (receive_email.length) {
  8748. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  8749. }
  8750. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  8751. if (receive_fax.length) {
  8752. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  8753. }
  8754. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  8755. if (sender_cid.length) {
  8756. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  8757. }
  8758. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  8759. if (sender_name.length) {
  8760. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  8761. }
  8762. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  8763. if (sender_ext.length) {
  8764. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  8765. }
  8766. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  8767. if(sender_contact.length) {
  8768. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  8769. }
  8770. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  8771. if (sender_phone.length) {
  8772. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  8773. }
  8774. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  8775. if (sender_fax.length) {
  8776. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  8777. }
  8778. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  8779. if (sender_email.length) {
  8780. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  8781. }
  8782. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  8783. if (shipping_billto_cid.length) {
  8784. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  8785. }
  8786. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  8787. if (shipping_billto_name.length) {
  8788. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  8789. }
  8790. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  8791. if (shipping_billto_ext.length) {
  8792. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  8793. }
  8794. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  8795. if (shipping_billto_contact.length) {
  8796. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  8797. }
  8798. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  8799. if (shipping_billto_phone.length) {
  8800. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  8801. }
  8802. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  8803. if (shipping_billto_fax.length) {
  8804. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  8805. }
  8806. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  8807. if (shipping_billto_email.length) {
  8808. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  8809. }
  8810. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  8811. if (billing_cid.length) {
  8812. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  8813. }
  8814. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  8815. if (billing_name.length) {
  8816. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  8817. }
  8818. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  8819. if (billing_ext.length) {
  8820. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  8821. }
  8822. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  8823. if (billing_contact.length) {
  8824. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  8825. }
  8826. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  8827. if (billing_phone.length) {
  8828. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  8829. }
  8830. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  8831. if (billing_fax.length) {
  8832. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  8833. }
  8834. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  8835. if (billing_email.length) {
  8836. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  8837. }
  8838. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  8839. if (returnto_cid.length) {
  8840. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  8841. }
  8842. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  8843. if (returnto_name.length) {
  8844. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  8845. }
  8846. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  8847. if (returnto_ext.length) {
  8848. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  8849. }
  8850. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  8851. if (returnto_contact.length) {
  8852. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  8853. }
  8854. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  8855. if (returnto_phone.length) {
  8856. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  8857. }
  8858. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  8859. if (returnto_fax.length) {
  8860. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  8861. }
  8862. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  8863. if (returnto_email.length) {
  8864. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  8865. }
  8866. NSString *order_status = @"status = 1,";
  8867. if (submit) {
  8868. order_status = @"status = -11,";
  8869. }
  8870. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  8871. NSString *sync_sql = @"";
  8872. if (submit) {
  8873. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  8874. [param setValue:sales_rep forKey:@"sales_rep"];
  8875. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  8876. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  8877. }
  8878. 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];
  8879. DebugLog(@"save order contactSql: %@",contactSql);
  8880. DebugLog(@"save order orderSql: %@",orderSql);
  8881. // int contact_ret = [iSalesDB execSql:contactSql];
  8882. int order_ret = [iSalesDB execSql:orderSql];
  8883. int ret = order_ret;
  8884. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8885. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8886. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8887. // [dic setValue:@"160409" forKey:@"min_ver"];
  8888. [dic setObject:so_id forKey:@"so#"];
  8889. return [RAConvertor dict2data:dic];
  8890. }
  8891. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  8892. {
  8893. // id foo = nil;
  8894. // NSMutableArray *a = @[].mutableCopy;
  8895. // [a addObject:foo];
  8896. return [self saveorder:param submit:NO];
  8897. }
  8898. #pragma mark add to cart by name
  8899. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  8900. {
  8901. NSString *orderCode = [params objectForKey:@"orderCode"];
  8902. NSString *product_name = [params objectForKey:@"product_name"];
  8903. product_name = [self translateSingleQuote:product_name];
  8904. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  8905. sqlite3 *db = [iSalesDB get_db];
  8906. __block NSMutableString *product_id_string = [NSMutableString string];
  8907. for (int i = 0; i < product_name_array.count; i++) {
  8908. NSString *name = [product_name_array objectAtIndex:i];
  8909. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  8910. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8911. int product_id = sqlite3_column_int(stmt, 0);
  8912. if (i == product_name_array.count - 1) {
  8913. [product_id_string appendFormat:@"%d",product_id];
  8914. } else {
  8915. [product_id_string appendFormat:@"%d,",product_id];
  8916. }
  8917. }];
  8918. }
  8919. if (!orderCode) {
  8920. orderCode = @"";
  8921. }
  8922. NSDictionary *newParams = @{
  8923. @"product_id" : product_id_string,
  8924. @"orderCode" : orderCode
  8925. };
  8926. [iSalesDB close_db:db];
  8927. return [self offline_add2cart:[newParams mutableCopy]];
  8928. }
  8929. #pragma mark reset order
  8930. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  8931. {
  8932. UIApplication * app = [UIApplication sharedApplication];
  8933. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  8934. appDelegate.disable_trigger=true;
  8935. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  8936. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  8937. appDelegate.disable_trigger=false;
  8938. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8939. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  8940. return [RAConvertor dict2data:dic];
  8941. }
  8942. #pragma mark submit order
  8943. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  8944. {
  8945. return [self saveorder:params submit:YES];
  8946. }
  8947. #pragma mark copy order
  8948. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  8949. {
  8950. NSMutableDictionary *ret = @{}.mutableCopy;
  8951. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  8952. sqlite3 *db = [iSalesDB get_db];
  8953. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  8954. // 首先查看联系人是否active
  8955. 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];
  8956. __block int customer_is_active = 1;
  8957. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8958. customer_is_active = sqlite3_column_int(stmt, 0);
  8959. }];
  8960. if (!customer_is_active) {
  8961. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8962. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8963. [iSalesDB close_db:db];
  8964. return [RAConvertor dict2data:ret];
  8965. }
  8966. // new order
  8967. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  8968. NSString *new_order_code = [self get_offline_soid:db];
  8969. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  8970. user = [self translateSingleQuote:user];
  8971. 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
  8972. __block int result = 1;
  8973. result = [iSalesDB execSql:insert_order_sql db:db];
  8974. if (!result) {
  8975. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8976. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8977. [iSalesDB close_db:db];
  8978. return [RAConvertor dict2data:ret];
  8979. }
  8980. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  8981. // __block NSString *product_id = @"";
  8982. __weak typeof(self) weakSelf = self;
  8983. 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];
  8984. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8985. int is_active = sqlite3_column_int(stmt, 1);
  8986. if (is_active) {
  8987. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  8988. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  8989. 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];
  8990. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  8991. }
  8992. }];
  8993. // product_id = [product_id substringFromIndex:1];
  8994. //
  8995. // [self offline_add2cart:@{}.mutableCopy];
  8996. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8997. [iSalesDB close_db:db];
  8998. if (result) {
  8999. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9000. } else {
  9001. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9002. }
  9003. [ret setObject:new_order_code forKey:@"so_id"];
  9004. return [RAConvertor dict2data:ret];
  9005. }
  9006. #pragma mark move wish list to cart
  9007. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  9008. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  9009. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9010. NSString *collectId = params[@"collectId"];
  9011. 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];
  9012. __block NSString *product_id = @"";
  9013. __block NSString *qty = @"";
  9014. __block int number_of_outOfStock = 0;
  9015. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  9016. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9017. int productId = sqlite3_column_int(stmt, 0);
  9018. int item_qty = sqlite3_column_int(stmt, 1);
  9019. int availability = sqlite3_column_int(stmt, 2);
  9020. int _id = sqlite3_column_int(stmt, 3);
  9021. if (!appDelegate.can_create_backorder) {
  9022. // 库存小于购买量为缺货
  9023. if (availability >= item_qty) {
  9024. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9025. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9026. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9027. } else {
  9028. number_of_outOfStock++;
  9029. }
  9030. } else {
  9031. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9032. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9033. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9034. }
  9035. }];
  9036. NSMutableDictionary *retDic = nil;
  9037. if (!appDelegate.can_create_backorder) {
  9038. if (delete_collectId.count == 0) {
  9039. retDic = [NSMutableDictionary dictionary];
  9040. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9041. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9042. return [RAConvertor dict2data:retDic];
  9043. }
  9044. }
  9045. if (product_id.length > 1) {
  9046. product_id = [product_id substringFromIndex:1];
  9047. }
  9048. if (qty.length > 1) {
  9049. qty = [qty substringFromIndex:1];
  9050. }
  9051. NSString *orderCode = params[@"orderCode"];
  9052. if (!orderCode) {
  9053. orderCode = @"";
  9054. }
  9055. NSDictionary *newParams = @{
  9056. @"product_id" : product_id,
  9057. @"orderCode" : orderCode,
  9058. @"qty" : qty
  9059. };
  9060. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  9061. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  9062. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  9063. NSInteger ret = [[[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] valueForKey:@"result"] integerValue];
  9064. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  9065. }
  9066. if (!appDelegate.can_create_backorder) {
  9067. if (number_of_outOfStock > 0) {
  9068. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9069. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9070. }
  9071. }
  9072. return [RAConvertor dict2data:retDic];
  9073. }
  9074. #pragma mark - portfolio
  9075. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  9076. int sort = [[params valueForKey:@"sort"] intValue];
  9077. int offset = [[params valueForKey:@"offset"] intValue];
  9078. int limit = [[params valueForKey:@"limit"] intValue];
  9079. NSString *orderBy = @"";
  9080. switch (sort) {
  9081. case 0:{
  9082. orderBy = @"p.modify_time desc";
  9083. }
  9084. break;
  9085. case 1:{
  9086. orderBy = @"modify_time asc";
  9087. }
  9088. break;
  9089. case 2:{
  9090. orderBy = @"p.name asc";
  9091. }
  9092. break;
  9093. case 3:{
  9094. orderBy = @"p.name desc";
  9095. }
  9096. break;
  9097. case 4:{
  9098. orderBy = @"p.description asc";
  9099. }
  9100. break;
  9101. case 5: {
  9102. orderBy = @"m.default_category asc";
  9103. }
  9104. default:
  9105. break;
  9106. }
  9107. // 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];
  9108. 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];
  9109. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9110. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9111. sqlite3 *db = [iSalesDB get_db];
  9112. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9113. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9114. int product_id = sqlite3_column_int(stmt, 0);
  9115. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9116. NSString *name = [self textAtColumn:1 statement:stmt];
  9117. NSString *description = [self textAtColumn:2 statement:stmt];
  9118. double price = sqlite3_column_double(stmt, 3);
  9119. double discount = sqlite3_column_double(stmt,4);
  9120. int qty = sqlite3_column_int(stmt, 5);
  9121. int percentage = sqlite3_column_int(stmt, 6);
  9122. int item_id = sqlite3_column_int(stmt, 7);
  9123. // int fashion_id = sqlite3_column_int(stmt, 8);
  9124. NSString *img_path = [self textAtColumn:9 statement:stmt];
  9125. NSString *line_note = [self textAtColumn:10 statement:stmt];
  9126. double percent = sqlite3_column_double(stmt, 11);
  9127. NSString *price_null = [self textAtColumn:3 statement:stmt];
  9128. if ([price_null isEqualToString:@"null"]) {
  9129. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9130. }
  9131. NSMutableDictionary *item = @{
  9132. @"linenotes": line_note,
  9133. @"check": @(1),
  9134. @"product_id": product_id_string,
  9135. @"available_qty": @(qty),
  9136. @"available_percent" : @(percent),
  9137. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  9138. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9139. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  9140. @"img": img_path,
  9141. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  9142. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  9143. }.mutableCopy;
  9144. if (percentage) {
  9145. [item removeObjectForKey:@"available_qty"];
  9146. } else {
  9147. [item removeObjectForKey:@"available_percent"];
  9148. }
  9149. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  9150. if ([qty_null isEqualToString:@"null"]) {
  9151. [item removeObjectForKey:@"available_qty"];
  9152. }
  9153. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9154. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9155. }];
  9156. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9157. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9158. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9159. }
  9160. [iSalesDB close_db:db];
  9161. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  9162. [dic setValue:@"" forKey:@"email_content"];
  9163. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  9164. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9165. return [RAConvertor dict2data:dic];
  9166. }
  9167. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  9168. __block int qty = 0;
  9169. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9170. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9171. qty = sqlite3_column_int(stmt, 0);
  9172. }];
  9173. return qty;
  9174. }
  9175. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  9176. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  9177. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  9178. // NSMutableDictionary * values = params[@"replaceValue"];
  9179. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  9180. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  9181. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  9182. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  9183. NSString *pdf_path = @"";
  9184. if (direct) {
  9185. configureParams = [self valueInParams:params key:@"pdfUrl"];
  9186. } else {
  9187. pdf_path = [self valueInParams:params key:@"pdfPath"];
  9188. }
  9189. NSString *create_user = [self valueInParams:params key:@"user"];
  9190. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  9191. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  9192. // model info
  9193. // 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];
  9194. // V1.90 more color
  9195. 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];
  9196. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9197. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  9198. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9199. sqlite3 *db = [iSalesDB get_db];
  9200. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9201. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9202. int product_id = sqlite3_column_int(stmt, 0);
  9203. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9204. double price = sqlite3_column_double(stmt, 1);
  9205. double discount = sqlite3_column_double(stmt,2);
  9206. int qty = sqlite3_column_int(stmt, 3);
  9207. int percentage = sqlite3_column_int(stmt, 4);
  9208. int item_id = sqlite3_column_int(stmt, 5);
  9209. NSString *line_note = [self textAtColumn:6 statement:stmt];
  9210. double percent = sqlite3_column_double(stmt, 7);
  9211. int more_color = sqlite3_column_int(stmt, 8);
  9212. NSString *price_null = [self textAtColumn:1 statement:stmt];
  9213. /* if ([price_null isEqualToString:@"null"]) {
  9214. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9215. }
  9216. */
  9217. [product_ids_string appendFormat:@"%@,",product_id_string];
  9218. // Regular Price
  9219. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  9220. NSString *regular_price_str = [self get_portfolio_price:appDelegate.contact_id item_id:item_id price:regular_price db:db];
  9221. // QTY
  9222. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  9223. if ([qty_null isEqualToString:@"null"]) {
  9224. // 查available
  9225. qty = [self model_QTY:product_id_string db:db];
  9226. }
  9227. if (percentage) {
  9228. qty = qty * percent / 100;
  9229. }
  9230. // Special Price
  9231. if ([price_null isEqualToString:@"null"]) {
  9232. // price = regular price
  9233. price = [regular_price_str doubleValue];
  9234. }
  9235. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  9236. if (![discount_null isEqualToString:@"null"]) {
  9237. price = price * (1 - discount / 100.0);
  9238. }
  9239. NSMutableDictionary *item = @{
  9240. @"line_note": line_note,
  9241. @"product_id": product_id_string,
  9242. @"available_qty": @(qty),
  9243. @"more_color":@(more_color),
  9244. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9245. @"regular_price" : regular_price_str,
  9246. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  9247. }.mutableCopy;
  9248. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9249. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9250. }];
  9251. [iSalesDB close_db:db];
  9252. if (product_ids_string.length > 0) {
  9253. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  9254. }
  9255. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  9256. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  9257. return [RAConvertor dict2data:resultDictionary];
  9258. }
  9259. NSString *model_info = [RAConvertor dict2string:dic];
  9260. // 创建PDF
  9261. // 在preview情况下保存,则不需要新建了
  9262. if (direct) {
  9263. NSMutableDictionary *tear_sheet_params = params;
  9264. // if (tear_sheet_id) {
  9265. // tear_sheet_params = values;
  9266. // }
  9267. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  9268. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  9269. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  9270. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  9271. resultDictionary = pdfInfo.mutableCopy;
  9272. } else { // 创建PDF失败
  9273. return pdfData;
  9274. }
  9275. } else {
  9276. // pdf_path 就是本地路径
  9277. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  9278. }
  9279. // 将文件移动到PDF Cache文件夹
  9280. NSString *newPath = [pdf_path lastPathComponent];
  9281. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9282. NSString *cachefolder = [paths objectAtIndex:0];
  9283. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9284. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  9285. NSFileManager *fileManager = [NSFileManager defaultManager];
  9286. NSError *error = nil;
  9287. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  9288. if (error) { // 移动文件失败
  9289. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9290. return [RAConvertor dict2data:resultDictionary];
  9291. }
  9292. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  9293. pdf_path = [newPath lastPathComponent];
  9294. // 保存信息
  9295. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  9296. NSString *off_params = [RAConvertor dict2string:params];
  9297. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  9298. pdf_path = [self translateSingleQuote:pdf_path];
  9299. create_user = [self translateSingleQuote:create_user];
  9300. tear_note = [self translateSingleQuote:tear_note];
  9301. tear_name = [self translateSingleQuote:tear_name];
  9302. model_info = [self translateSingleQuote:model_info];
  9303. configureParams = [self translateSingleQuote:configureParams];
  9304. off_params = [self translateSingleQuote:off_params];
  9305. 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];
  9306. if (tear_sheet_id) {
  9307. int _id = [tear_sheet_id intValue];
  9308. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  9309. }
  9310. int result = [iSalesDB execSql:save_pdf_sql];
  9311. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9312. //
  9313. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  9314. if (remove_Item) {
  9315. // portfolioId
  9316. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  9317. }
  9318. return [RAConvertor dict2data:resultDictionary];
  9319. }
  9320. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  9321. return [self offline_savePDF:params direct:YES];
  9322. }
  9323. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  9324. return [self offline_savePDF:params direct:NO];
  9325. }
  9326. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  9327. int offset = [[params valueForKey:@"offset"] intValue];
  9328. int limit = [[params valueForKey:@"limit"] intValue];
  9329. NSString *keyword = [params valueForKey:@"keyWord"];
  9330. NSString *where = @"where is_delete is null or is_delete = 0";
  9331. if (keyword.length) {
  9332. keyword = [self translateSingleQuote:keyword];
  9333. 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];
  9334. }
  9335. 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
  9336. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9337. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9338. NSString *cachefolder = [paths objectAtIndex:0];
  9339. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9340. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9341. NSString *name = [self textAtColumn:0 statement:stmt];
  9342. NSString *note = [self textAtColumn:1 statement:stmt];
  9343. NSString *time = [self textAtColumn:2 statement:stmt];
  9344. NSString *user = [self textAtColumn:3 statement:stmt];
  9345. NSString *path = [self textAtColumn:4 statement:stmt];
  9346. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9347. NSString *off_params = [self textAtColumn:7 statement:stmt];
  9348. path = [pdfFolder stringByAppendingPathComponent:path];
  9349. BOOL bdir=NO;
  9350. NSFileManager* fileManager = [NSFileManager defaultManager];
  9351. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  9352. {
  9353. //pdf文件不存在
  9354. path=nil;
  9355. }
  9356. time = [self changeDateTimeFormate:time];
  9357. time = [time stringByAppendingString:@" PST"];
  9358. int sheet_id = sqlite3_column_int(stmt, 5);
  9359. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  9360. item[@"tearsheetsId"]=@(sheet_id);
  9361. item[@"pdf_path"]=path;
  9362. item[@"create_time"]=time;
  9363. item[@"create_user"]=user;
  9364. item[@"tear_note"]=note;
  9365. item[@"tear_name"]=name;
  9366. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  9367. item[@"model_info"]=model_info;
  9368. item[@"off_params"]=off_params;
  9369. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9370. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9371. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9372. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9373. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9374. }];
  9375. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9376. return [RAConvertor dict2data:dic];
  9377. }
  9378. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  9379. NSString *prodct_ids = [params objectForKey:@"product_id"];
  9380. // NSString *user = [params objectForKey:@"user"];
  9381. // 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];
  9382. 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];
  9383. int result = [iSalesDB execSql:sql];
  9384. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9385. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  9386. if (result == RESULT_TRUE) {
  9387. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  9388. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  9389. sqlite3 *db = [iSalesDB get_db];
  9390. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  9391. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  9392. [iSalesDB close_db:db];
  9393. }
  9394. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9395. return [RAConvertor dict2data:dic];
  9396. }
  9397. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  9398. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  9399. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  9400. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  9401. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  9402. NSDictionary *company_item = @{
  9403. @"control": @"text",
  9404. @"keyboard": @"text",
  9405. @"name": @"company_name",
  9406. @"value": COMPANY_FULL_NAME,
  9407. @"aname": @"Company Name"
  9408. };
  9409. [section_0 setObject:company_item forKey:@"item_0"];
  9410. [dic setObject:section_0 forKey:@"section_0"];
  9411. // Regurlar Price
  9412. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  9413. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  9414. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9415. __block long val_count = 0;
  9416. NSString *sql = @"select name,type,order_by from price order by order_by";
  9417. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9418. NSString *name = [self textAtColumn:0 statement:stmt];
  9419. int type = sqlite3_column_int(stmt, 1);
  9420. int order_by = sqlite3_column_int(stmt, 2);
  9421. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9422. NSDictionary *price_dic = @{
  9423. @"value" : name,
  9424. @"value_id" : [NSNumber numberWithInteger:type],
  9425. @"check" : order_by == 0 ? @(1) : @(0)
  9426. };
  9427. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9428. val_count = ++(*count);
  9429. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9430. val_count = 0;
  9431. }];
  9432. [cadedate setObject:@{@"value" : @"None",
  9433. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9434. val_count++;
  9435. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  9436. [price setObject:cadedate forKey:@"cadedate"];
  9437. [section1 setObject:price forKey:@"item_2"];
  9438. [dic setObject:section1 forKey:@"section_1"];
  9439. return [RAConvertor dict2data:dic];
  9440. }
  9441. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  9442. NSString *product_id = [params objectForKey:@"fashionId"];
  9443. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9444. __block int result = RESULT_TRUE;
  9445. __block int qty = 0;
  9446. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9447. qty = sqlite3_column_int(stmt, 0);
  9448. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9449. result = RESULT_FALSE;
  9450. }];
  9451. NSMutableDictionary *dic = @{
  9452. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  9453. @"mode" : @"Regular Mode",
  9454. @"quantity_available" : @(qty),
  9455. @"result" : @(result),
  9456. }.mutableCopy;
  9457. return [RAConvertor dict2data:dic];
  9458. }
  9459. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  9460. NSString *item_ids = [params objectForKey:@"item_id"];
  9461. NSString *line_notes = [params objectForKey:@"notes"];
  9462. NSString *price_str = [params objectForKey:@"price"];
  9463. NSString *discount_str = [params objectForKey:@"discount"];
  9464. NSString *percent = [params objectForKey:@"available_percent"];
  9465. NSString *qty = [params objectForKey:@"available_qty"];
  9466. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  9467. int dot = 0;
  9468. if (line_notes) {
  9469. line_notes = [self translateSingleQuote:line_notes];
  9470. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  9471. sql = [sql stringByAppendingString:line_notes];
  9472. dot = 1;
  9473. } else {
  9474. line_notes = @"";
  9475. }
  9476. if (price_str) {
  9477. if (dot) {
  9478. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  9479. } else {
  9480. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  9481. dot = 1;
  9482. }
  9483. sql = [sql stringByAppendingString:price_str];
  9484. } else {
  9485. price_str = @"";
  9486. }
  9487. if (discount_str) {
  9488. if (dot) {
  9489. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  9490. } else {
  9491. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  9492. dot = 1;
  9493. }
  9494. sql = [sql stringByAppendingString:discount_str];
  9495. } else {
  9496. discount_str = @"";
  9497. }
  9498. if (percent) {
  9499. if (dot) {
  9500. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  9501. } else {
  9502. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  9503. dot = 1;
  9504. }
  9505. sql = [sql stringByAppendingString:percent];
  9506. } else {
  9507. percent = @"";
  9508. }
  9509. if (qty) {
  9510. if (dot) {
  9511. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  9512. } else {
  9513. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  9514. dot = 1;
  9515. }
  9516. sql = [sql stringByAppendingString:qty];
  9517. } else {
  9518. qty = @"";
  9519. }
  9520. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  9521. sql = [sql stringByAppendingString:where];
  9522. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9523. int result = [iSalesDB execSql:sql];
  9524. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9525. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9526. return [RAConvertor dict2data:dic];
  9527. }
  9528. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  9529. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  9530. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9531. sqlite3 *db = [iSalesDB get_db];
  9532. 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];
  9533. int result = [iSalesDB execSql:sql db:db];
  9534. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9535. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9536. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  9537. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  9538. [iSalesDB close_db:db];
  9539. return [RAConvertor dict2data:dic];
  9540. }
  9541. + (void)offline_removePDFWithName:(NSString *)name {
  9542. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9543. NSString *cachefolder = [paths objectAtIndex:0];
  9544. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9545. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  9546. NSFileManager *fileManager = [NSFileManager defaultManager];
  9547. [fileManager removeItemAtPath:path error:nil];
  9548. }
  9549. + (void)offline_clear_PDFCache {
  9550. NSFileManager *fileManager = [NSFileManager defaultManager];
  9551. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9552. NSString *cachefolder = [paths objectAtIndex:0];
  9553. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9554. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  9555. for (NSString *path in pdf_files) {
  9556. [self offline_removePDFWithName:[path lastPathComponent]];
  9557. }
  9558. }
  9559. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  9560. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9561. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  9562. NSString *user = [params objectForKey:@"user"];
  9563. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  9564. if (![create_user isEqualToString:user]) {
  9565. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9566. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9567. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  9568. return [RAConvertor dict2data:dic];
  9569. }
  9570. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  9571. __block int is_local = 0;
  9572. __block NSString *path = @"";
  9573. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9574. is_local = sqlite3_column_int(stmt, 0);
  9575. path = [self textAtColumn:1 statement:stmt];
  9576. }];
  9577. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  9578. if (is_local == 1) {
  9579. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  9580. // 删除文件
  9581. [self offline_removePDFWithName:path];
  9582. }
  9583. int result = [iSalesDB execSql:sql];
  9584. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9585. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9586. return [RAConvertor dict2data:dic];
  9587. }
  9588. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  9589. {
  9590. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9591. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9592. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9593. NSString* where=@"1=1";
  9594. if (ver!=nil) {
  9595. where=@"is_dirty=1";
  9596. }
  9597. 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];
  9598. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9599. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9600. sqlite3 *db = [iSalesDB get_db];
  9601. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9602. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9603. NSInteger _id = sqlite3_column_int(stmt, 0);
  9604. NSInteger product_id = sqlite3_column_int(stmt, 1);
  9605. NSString *name = [self textAtColumn:2 statement:stmt];
  9606. NSString *desc = [self textAtColumn:3 statement:stmt];
  9607. NSInteger item_id = sqlite3_column_int(stmt, 4);
  9608. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  9609. NSInteger qty = sqlite3_column_int(stmt, 6);
  9610. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  9611. double percent = sqlite3_column_double(stmt, 8);
  9612. double price = sqlite3_column_double(stmt, 9);
  9613. double discount = sqlite3_column_double(stmt, 10);
  9614. NSString *img = [self textAtColumn:11 statement:stmt];
  9615. NSString *line_note = [self textAtColumn:12 statement:stmt];
  9616. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  9617. NSString *create_time = [self textAtColumn:14 statement:stmt];
  9618. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  9619. NSString *price_null = [self textAtColumn:9 statement:stmt];
  9620. if ([price_null isEqualToString:@"null"]) {
  9621. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9622. }
  9623. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  9624. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  9625. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  9626. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9627. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  9628. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  9629. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  9630. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  9631. if ([qty_null isEqualToString:@"null"]) {
  9632. // [item setValue:@"null" forKey:@"available_qty"];
  9633. } else {
  9634. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  9635. }
  9636. if ([is_percent_null isEqualToString:@"null"]) {
  9637. // [item setValue:@"null" forKey:@"percentage"];
  9638. } else {
  9639. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  9640. }
  9641. if ([percent_null isEqualToString:@"null"]) {
  9642. // [item setValue:@"null" forKey:@"percent"];
  9643. } else {
  9644. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  9645. }
  9646. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  9647. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  9648. [item setValue:name forKey:@"name"];
  9649. [item setValue:desc forKey:@"description"];
  9650. [item setValue:img forKey:@"img"];
  9651. [item setValue:line_note forKey:@"line_note"];
  9652. [item setValue:create_time forKey:@"createtime"];
  9653. [item setValue:modify_time forKey:@"modifytime"];
  9654. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9655. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9656. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9657. }];
  9658. [iSalesDB close_db:db];
  9659. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9660. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9661. }
  9662. return ret;
  9663. }
  9664. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  9665. {
  9666. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9667. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9668. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9669. NSString* where=@"1=1";
  9670. if (ver!=nil) {
  9671. where=@"is_dirty=1";
  9672. }
  9673. 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];
  9674. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9675. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9676. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9677. NSInteger _id = sqlite3_column_int(stmt, 0);
  9678. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  9679. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  9680. NSString *create_user = [self textAtColumn:3 statement:stmt];
  9681. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  9682. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  9683. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9684. NSString *createtime = [self textAtColumn:7 statement:stmt];
  9685. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  9686. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  9687. NSString *off_params = [self textAtColumn:10 statement:stmt];
  9688. NSString *uuid = [NSUUID UUID].UUIDString;
  9689. int is_delete = sqlite3_column_int(stmt, 11);
  9690. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  9691. int is_local = sqlite3_column_int(stmt, 12);
  9692. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9693. if (![tearsheet_id_null isEqualToString:@"null"]) {
  9694. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  9695. }
  9696. [item setObject:pdf_path forKey:@"pdf_path"];
  9697. [item setObject:create_user forKey:@"create_user"];
  9698. [item setObject:tear_note forKey:@"tear_note"];
  9699. [item setObject:tear_name forKey:@"tear_name"];
  9700. [item setObject:model_info forKey:@"model_info"];
  9701. [item setObject:createtime forKey:@"createtime"];
  9702. [item setObject:modifytime forKey:@"modifytime"];
  9703. [item setObject:urlParams forKey:@"urlParams"];
  9704. [item setObject:off_params forKey:@"off_params"];
  9705. [item setObject:uuid forKey:@"pdf_token"];
  9706. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  9707. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  9708. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  9709. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  9710. } else {
  9711. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9712. }
  9713. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9714. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9715. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9716. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9717. }];
  9718. return ret;
  9719. }
  9720. @end